Skip to content

Commit

Permalink
Generalize forkUpgrades to indexByForkHeights (#1823)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundnoble authored Feb 15, 2024
1 parent c97366d commit f13025b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
18 changes: 8 additions & 10 deletions src/Chainweb/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module Chainweb.Version
, checkAdjacentChainIds

-- ** Utilities for constructing Chainweb Version
, forkUpgrades
, indexByForkHeights
, latestBehaviorAt
, domainAddr2PeerInfo

Expand Down Expand Up @@ -553,17 +553,16 @@ instance HasChainGraph (ChainwebVersion, BlockHeight) where
domainAddr2PeerInfo :: [HostAddress] -> [PeerInfo]
domainAddr2PeerInfo = fmap (PeerInfo Nothing)

-- | Creates a map from fork heights to upgrades.
forkUpgrades
-- | A utility to allow indexing behavior by forks, returning that behavior
-- indexed by the block heights of those forks.
indexByForkHeights
:: ChainwebVersion
-> [(Fork, ChainMap Upgrade)]
-> ChainMap (HashMap BlockHeight Upgrade)
forkUpgrades v = OnChains . foldl' go (HM.empty <$ HS.toMap (chainIds v))
-> [(Fork, ChainMap a)]
-> ChainMap (HashMap BlockHeight a)
indexByForkHeights v = OnChains . foldl' go (HM.empty <$ HS.toMap (chainIds v))
where
conflictError fork h =
error $ "conflicting upgrades at block height " <> show h <> " when adding upgrade for fork " <> show fork
emptyUpgradeError fork =
error $ "empty set of upgrade transactions for fork " <> show fork
error $ "conflicting behavior at block height " <> show h <> " when adding behavior for fork " <> show fork
go acc (fork, txsPerChain) =
HM.unionWith
(HM.unionWithKey (conflictError fork))
Expand All @@ -573,7 +572,6 @@ forkUpgrades v = OnChains . foldl' go (HM.empty <$ HS.toMap (chainIds v))
[ (cid, HM.singleton forkHeight upg)
| cid <- HM.keys acc
, Just upg <- [txsPerChain ^? onChain cid]
, not (null $ _upgradeTransactions upg) || emptyUpgradeError fork
, ForkAtBlockHeight forkHeight <- [v ^?! versionForks . at fork . _Just . onChain cid]
, forkHeight /= maxBound
]
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Version/Mainnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mainnet = ChainwebVersion
]
}
, _versionUpgrades = chainZip HM.union
(forkUpgrades mainnet
(indexByForkHeights mainnet
[ (CoinV2, onChains
[ (unsafeChainId 0, upgrade MN0.transactions)
, (unsafeChainId 1, upgrade MN1.transactions)
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Version/RecapDevelopment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ recapDevnet = ChainwebVersion
Chainweb223Pact -> AllChains $ ForkAtBlockHeight $ BlockHeight 600

, _versionUpgrades = foldr (chainZip HM.union) (AllChains mempty)
[ forkUpgrades recapDevnet
[ indexByForkHeights recapDevnet
[ (CoinV2, onChains [(unsafeChainId i, upgrade RecapDevnet.transactions) | i <- [0..9]])
, (Pact4Coin3, AllChains (Upgrade CoinV3.transactions True))
, (Chainweb214Pact, AllChains (Upgrade CoinV4.transactions True))
Expand Down
4 changes: 3 additions & 1 deletion src/Chainweb/Version/Registry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ validateVersion v = do
, hasAllChains (_genesisBlockTarget $ _versionGenesis v)
, hasAllChains (_genesisTime $ _versionGenesis v)
])]
, [ "validateVersion: some upgrade has no transactions"
| any (any (\upg -> null (_upgradeTransactions upg))) (_versionUpgrades v) ]
]
unless (null errors) $
error $ unlines $ ["errors encountered validating version " <> show v <> ":"] <> errors
error $ unlines $ ["errors encountered validating version", show v] <> errors

-- | Look up a version in the registry by code.
lookupVersionByCode :: HasCallStack => ChainwebVersionCode -> ChainwebVersion
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Version/Testnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ testnet = ChainwebVersion
]
}
, _versionUpgrades = chainZip HM.union
(forkUpgrades testnet
(indexByForkHeights testnet
[ (CoinV2, onChains $
[ (unsafeChainId 0, upgrade MN0.transactions)
, (unsafeChainId 1, upgrade MN1.transactions)
Expand Down
2 changes: 1 addition & 1 deletion test/Chainweb/Test/TestVersions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ cpmTestVersion g v = v
, _genesisTime = AllChains $ BlockCreationTime epoch
}
& versionUpgrades .~ chainZip HM.union
(forkUpgrades v
(indexByForkHeights v
[ (CoinV2, AllChains (upgrade Other.transactions))
, (Pact4Coin3, AllChains (Upgrade CoinV3.transactions True))
, (Chainweb214Pact, AllChains (Upgrade CoinV4.transactions True))
Expand Down

0 comments on commit f13025b

Please sign in to comment.