Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 migration #655

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/Frontend/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ import Frontend.Foundation
import Frontend.Messages
import Frontend.Network.NodeInfo
import Frontend.Storage
import Frontend.VersionedStore hiding (Cheese(..))
import Frontend.VersionedStore
import Frontend.Log


Expand Down
47 changes: 26 additions & 21 deletions frontend/src/Frontend/VersionedStore/V2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,37 @@ fromMultiSet = ($ []) . Map.foldrWithKey (\k i -> (.) (dlrep k i)) id
| n == 0 = id
| otherwise = (v:) . dlrep v (n - 1)

data Cheese a b = That b | These a b

instance Functor (Cheese a) where
fmap f (That b) = That (f b)
fmap f (These a b) = These a (f b)

instance (Semigroup a) => Applicative (Cheese a) where
pure = That
That f <*> That x = That (f x)
That f <*> These b x = These b (f x)
These a f <*> That x = These a (f x)
These a f <*> These b x = These (a <> b) (f x)
-- It is equivalent to "These () a" except that we take out the
-- "This" constructor. With it gone, we still get the same applicative
-- operations from "These" (sans "This") but we don't have to account for
-- the "This" constructor in situations where we know it is impossible to
-- produce it. This is also morally equivalent to Either () b with a
-- different applicative instance
data Deez b = Dis b | Dat b
Copy link
Member

@emilypi emilypi Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly misleading comment.

These a b 
~ a + b + a*b

But

These () a 
~ 1 + a + 1*a 
~ 1 + a + a 
~ Maybe (Either a a)

and

Either () b 
~ 1 + b
~ Maybe b

You want a newtype for Either a a here to get the modified Applicative.


instance Functor Deez where
fmap f (Dis b) = Dis (f b)
fmap f (Dat b) = Dat (f b)

instance Applicative Deez where
pure = Dis
Dis f <*> Dis x = Dis (f x)
Dis f <*> Dat x = Dat (f x)
Dat f <*> Dis x = Dat (f x)
Dat f <*> Dat x = Dat (f x)

convertNodeRefs :: Map NetworkName [NodeRef] -> Map NetworkName [NodeRef]
convertNodeRefs = fmap migrate
where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the nested where necessary ?

migrate = replaceRefsWith "api.chainweb.com" mainnetNodeRefs . replaceRefsWith "api.testnet.chainweb.com" testnetNodeRefs
where
replaceRefsWith ref baseRefs refs =
refs
& on (Map.mergeA Map.dropMissing Map.preserveMissing (Map.zipWithMaybeAMatched (\_ _ _ -> These () Nothing))) toMultiSet baseRefs
& \case
That m -> m
These () m -> addRef ref m -- if we hit this case, there were matching keys
& fromMultiSet
addRef (unsafeParseNodeRef -> ref) = Map.insert ref 1
replaceRefsWith ref baseRefs refs =
refs
& on (Map.mergeA Map.dropMissing Map.preserveMissing (Map.zipWithMaybeAMatched (\_ _ _ -> Dat Nothing))) toMultiSet baseRefs
& \case
Dis m -> m
Dat m -> addRef ref m -- if we hit this case, there were matching keys
& fromMultiSet
addRef (unsafeParseNodeRef -> ref) = Map.insert ref 1
testnetNodeRefs = unsafeParseNodeRef <$>
[ "us1.testnet.chainweb.com"
, "us2.testnet.chainweb.com"
Expand Down