Skip to content

Commit 251a97d

Browse files
committed
Extend ForkHeight to manage by ForkNumber
1 parent 968588b commit 251a97d

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

src/Chainweb/Chainweb/Configuration.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,12 @@ parseVersion = constructVersion
605605
maybe (_versionUpgrades winningVersion) (\fub' ->
606606
OnChains $ HM.mapWithKey
607607
(\cid _ ->
608-
case winningVersion ^?! versionForks . at fub' . _Just . atChain cid of
608+
let currentUpgrades = winningVersion ^?! versionUpgrades . atChain cid
609+
in case winningVersion ^?! versionForks . at fub' . _Just . atChain cid of
609610
ForkNever -> error "Chainweb.Chainweb.Configuration.parseVersion: the fork upper bound never occurs in this version."
610-
ForkAtBlockHeight fubHeight -> HM.filterWithKey (\bh _ -> bh <= fubHeight) (winningVersion ^?! versionUpgrades . atChain cid)
611+
ForkAtBlockHeight fubHeight -> HM.filterWithKey (\bh _ -> bh <= fubHeight) currentUpgrades
611612
ForkAtGenesis -> winningVersion ^?! versionUpgrades . atChain cid
613+
ForkAtForkNumber _ -> currentUpgrades -- For now, version upgrades were only allowed at blok heights
612614
)
613615
(HS.toMap (chainIds winningVersion))
614616
) fub

src/Chainweb/Pact4/ModuleCache.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ cleanModuleCache v cid bh =
7272
ForkAtBlockHeight bh' -> bh == bh'
7373
ForkAtGenesis -> bh == genesisHeight v cid
7474
ForkNever -> False
75+
ForkAtForkNumber _ -> error "ChainWeb217Pact is not supposed to be indexed by a ForkNumber"

src/Chainweb/Version.hs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,38 @@ instance FromJSON Fork where
322322
instance FromJSONKey Fork where
323323
fromJSONKey = FromJSONKeyTextParser $ either fail return . eitherFromText
324324

325-
data ForkHeight = ForkAtBlockHeight !BlockHeight | ForkAtGenesis | ForkNever
326-
deriving stock (Generic, Eq, Ord, Show)
325+
data ForkHeight = ForkAtForkNumber !ForkNumber | ForkAtBlockHeight !BlockHeight | ForkAtGenesis | ForkNever
326+
deriving stock (Generic, Eq, Show)
327327
deriving anyclass (Hashable, NFData)
328328

329+
instance Ord ForkHeight where
330+
compare ForkAtGenesis ForkAtGenesis = EQ
331+
compare ForkNever ForkNever = EQ
332+
compare (ForkAtForkNumber a) (ForkAtForkNumber b) = compare a b
333+
compare (ForkAtBlockHeight a) (ForkAtBlockHeight b) = compare a b
334+
compare ForkAtGenesis _ = LT
335+
compare _ ForkAtGenesis = GT
336+
compare ForkNever _ = GT
337+
compare _ ForkNever = LT
338+
compare (ForkAtForkNumber fn) (ForkAtBlockHeight _)
339+
| fn == 0 = LT
340+
| otherwise = GT
341+
compare (ForkAtBlockHeight _) (ForkAtForkNumber fn)
342+
| fn == 0 = GT
343+
| otherwise = LT
344+
345+
-- We consider the following ordering for Forks:
346+
-- - ForkAtGenesis
347+
-- - ForkNumber = 0 (unusual case)
348+
-- - BlockHeihgt = 0 (unusual case)
349+
-- - Blockkheight = 1
350+
-- ..
351+
-- - BlockHeight = n
352+
-- - ForkNumber = 1
353+
-- ..
354+
-- - ForkNumber = n
355+
-- - ForkNever
356+
329357
makePrisms ''ForkHeight
330358

331359
newtype ChainwebVersionName =

src/Chainweb/Version/Guards.hs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
{-# LANGUAGE ImportQualifiedPost #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
44

5+
-- TODO Remove this when checkFork' will be used for real
6+
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
7+
58
-- |
69
-- Module: Chainweb.Version.Guards
710
-- Copyright: Copyright © 2023 Kadena LLC.
@@ -70,6 +73,7 @@ import Chainweb.BlockHeight
7073
import Chainweb.ChainId
7174
import Chainweb.Pact4.Transaction qualified as Pact4
7275
import Chainweb.Utils.Rule
76+
import Chainweb.ForkState
7377
import Chainweb.Version
7478
import Control.Lens
7579
import Data.Word (Word64)
@@ -86,32 +90,33 @@ import Pact.Types.Scheme (PPKScheme(ED25519, WebAuthn))
8690
getForkHeight :: Fork -> ChainwebVersion -> ChainId -> ForkHeight
8791
getForkHeight fork v cid = v ^?! versionForks . at fork . _Just . atChain cid
8892

93+
-- Check Fork by height
8994
checkFork
90-
:: (BlockHeight -> ForkHeight -> Bool)
95+
:: (ForkHeight -> ForkHeight -> Bool)
9196
-> Fork -> ChainwebVersion -> ChainId -> BlockHeight -> Bool
92-
checkFork p f v cid h = p h (getForkHeight f v cid)
97+
checkFork p f v cid h = p (ForkAtBlockHeight h) (getForkHeight f v cid)
98+
99+
-- CheckFork by forkNumber
100+
checkFork'
101+
:: (ForkHeight -> ForkHeight -> Bool)
102+
-> Fork -> ChainwebVersion -> ChainId -> ForkNumber -> Bool
103+
checkFork' p f v cid fn = p (ForkAtForkNumber fn) (getForkHeight f v cid)
104+
93105

94-
after :: BlockHeight -> ForkHeight -> Bool
95-
after bh (ForkAtBlockHeight bh') = bh > bh'
96-
after _ ForkAtGenesis = True
97-
after _ ForkNever = False
106+
after :: ForkHeight -> ForkHeight -> Bool
107+
after = (>)
98108

99-
atOrAfter :: BlockHeight -> ForkHeight -> Bool
100-
atOrAfter bh (ForkAtBlockHeight bh') = bh >= bh'
101-
atOrAfter _ ForkAtGenesis = True
102-
atOrAfter _ ForkNever = False
109+
atOrAfter :: ForkHeight -> ForkHeight -> Bool
110+
atOrAfter = (>=)
103111

104-
before :: BlockHeight -> ForkHeight -> Bool
105-
before bh (ForkAtBlockHeight bh') = bh < bh'
106-
before _ ForkAtGenesis = False
107-
before _ ForkNever = True
112+
before :: ForkHeight -> ForkHeight -> Bool
113+
before = (<)
108114

109115
-- Intended for forks that intend to run upgrades at exactly one height, and so
110116
-- can't be "pre-activated" for genesis.
111-
atNotGenesis :: BlockHeight -> ForkHeight -> Bool
112-
atNotGenesis bh (ForkAtBlockHeight bh') = bh == bh'
117+
atNotGenesis :: ForkHeight -> ForkHeight -> Bool
113118
atNotGenesis _ ForkAtGenesis = error "fork cannot be at genesis"
114-
atNotGenesis _ ForkNever = False
119+
atNotGenesis fh fh' = fh == fh'
115120

116121
-- -------------------------------------------------------------------------- --
117122
-- Header Validation Guards

0 commit comments

Comments
 (0)