@@ -67,12 +67,6 @@ module Chainweb.BlockHeader.Internal
6767, decodeEpochStartTime
6868, epochStart
6969
70- -- * FeatureFlags
71- , FeatureFlags
72- , mkFeatureFlags
73- , encodeFeatureFlags
74- , decodeFeatureFlags
75-
7670-- * POW Target
7771, powTarget
7872
@@ -143,6 +137,7 @@ module Chainweb.BlockHeader.Internal
143137, isForkVoteBlock
144138, newForkState
145139, isLastForkEpochBlock
140+ , genesisForkState
146141
147142-- * CAS Constraint
148143, BlockHeaderCas
@@ -258,28 +253,28 @@ decodeEpochStartTime :: Get EpochStartTime
258253decodeEpochStartTime = EpochStartTime <$> decodeTime
259254
260255-- -----------------------------------------------------------------------------
261- -- Feature Flags
256+ -- [Deprecated] Feature Flags
262257--
263258-- Deprecated: renamed into 'blockForkState'
264259
265- newtype FeatureFlags = FeatureFlags Word64
266- deriving stock (Show , Eq , Generic )
267- deriving anyclass (NFData )
268- deriving newtype (ToJSON , FromJSON )
269-
270- encodeFeatureFlags :: FeatureFlags -> Put
271- encodeFeatureFlags (FeatureFlags ff) = putWord64le ff
272-
273- decodeFeatureFlags :: Get FeatureFlags
274- decodeFeatureFlags = FeatureFlags <$> getWord64le
275-
276- instance MerkleHashAlgorithm a => IsMerkleLogEntry a ChainwebHashTag FeatureFlags where
277- type Tag FeatureFlags = 'FeatureFlagsTag
278- toMerkleNode = encodeMerkleInputNode encodeFeatureFlags
279- fromMerkleNode = decodeMerkleInputNode decodeFeatureFlags
280-
281- mkFeatureFlags :: FeatureFlags
282- mkFeatureFlags = FeatureFlags 0x0
260+ -- newtype FeatureFlags = FeatureFlags Word64
261+ -- deriving stock (Show, Eq, Generic)
262+ -- deriving anyclass (NFData)
263+ -- deriving newtype (ToJSON, FromJSON)
264+ --
265+ -- encodeFeatureFlags :: FeatureFlags -> Put
266+ -- encodeFeatureFlags (FeatureFlags ff) = putWord64le ff
267+ --
268+ -- decodeFeatureFlags :: Get FeatureFlags
269+ -- decodeFeatureFlags = FeatureFlags <$> getWord64le
270+ --
271+ -- instance MerkleHashAlgorithm a => IsMerkleLogEntry a ChainwebHashTag FeatureFlags where
272+ -- type Tag FeatureFlags = 'ForkStateTag
273+ -- toMerkleNode = encodeMerkleInputNode encodeFeatureFlags
274+ -- fromMerkleNode = decodeMerkleInputNode decodeFeatureFlags
275+ --
276+ -- mkFeatureFlags :: FeatureFlags
277+ -- mkFeatureFlags = FeatureFlags 0x0
283278
284279-- -------------------------------------------------------------------------- --
285280-- Block Header
@@ -302,9 +297,10 @@ mkFeatureFlags = FeatureFlags 0x0
302297--
303298data BlockHeader :: Type where
304299 BlockHeader ::
305- { _blockFlags :: {-# UNPACK #-} ! FeatureFlags
306- -- ^ An 8-byte bitmask reserved for the future addition of boolean
307- -- "feature flags".
300+ { _blockFlags :: {-# UNPACK #-} ! ForkState
301+ -- ^ Fork state of the block. This used to be called "feature
302+ -- flags". The old name is still used in textual encodings (e.g.
303+ -- JSON).
308304
309305 , _blockCreationTime :: {-# UNPACK #-} ! BlockCreationTime
310306 -- ^ The time when the block was creates as recorded by the miner
@@ -739,7 +735,7 @@ makeGenesisBlockHeader' v p ct@(BlockCreationTime t) n =
739735 cid = _chainId p
740736
741737 mlog = newMerkleLog
742- $ mkFeatureFlags
738+ $ genesisForkState
743739 :+: ct
744740 :+: genesisParentBlockHash v cid
745741 :+: (v ^?! versionGenesis . genesisBlockTarget . atChain cid)
@@ -781,7 +777,7 @@ instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader wh
781777
782778 -- /IMPORTANT/ a types must occur at most once in this list
783779 type MerkleLogHeader BlockHeader =
784- '[ FeatureFlags
780+ '[ ForkState
785781 , BlockCreationTime
786782 , BlockHash
787783 , HashTarget
@@ -849,7 +845,7 @@ instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader wh
849845
850846encodeBlockHeaderWithoutHash :: BlockHeader -> Put
851847encodeBlockHeaderWithoutHash b = do
852- encodeFeatureFlags (_blockFlags b)
848+ encodeForkState (_blockFlags b)
853849 encodeBlockCreationTime (_blockCreationTime b)
854850 encodeBlockHash (_blockParent b)
855851 encodeBlockHashRecord (_blockAdjacentHashes b)
@@ -897,7 +893,7 @@ decodeBlockHeaderCheckedChainId p = do
897893--
898894decodeBlockHeaderWithoutHash :: Get BlockHeader
899895decodeBlockHeaderWithoutHash = do
900- a0 <- decodeFeatureFlags
896+ a0 <- decodeForkState
901897 a1 <- decodeBlockCreationTime
902898 a2 <- decodeBlockHash -- parent hash
903899 a3 <- decodeBlockHashRecord
@@ -929,7 +925,7 @@ decodeBlockHeaderWithoutHash = do
929925--
930926decodeBlockHeader :: Get BlockHeader
931927decodeBlockHeader = BlockHeader
932- <$> decodeFeatureFlags
928+ <$> decodeForkState
933929 <*> decodeBlockCreationTime
934930 <*> decodeBlockHash -- parent hash
935931 <*> decodeBlockHashRecord
@@ -1107,16 +1103,6 @@ isForkCountBlock hdr = not (isForkVoteBlock hdr)
11071103
11081104-- | New Fork State computation
11091105--
1110- -- The Boolean parameter indicates whether the block votes "yes" (True) to
1111- -- increasing the fork number.
1112- --
1113- -- Callers of this function must not just unconditionally vote "yes". Instead,
1114- -- they should vote "yes" only if the current fork number is less than the
1115- -- maximum fork number that the version of the code supports.
1116- --
1117- -- TODO: replace the Boolean parameter with a 'maxSupportedForkNumber'
1118- -- parameter.
1119- --
11201106-- * isForkEpochStart -> forkNumber is deterministically increased
11211107-- * isForkEpochStart -> forkVote is nondeterministically reset to 0 or forkStep
11221108-- * forkVoteBlock && not isForkVoteStart -> forkVotes are non-deterministically monotonicly increasing
@@ -1127,13 +1113,14 @@ newForkState
11271113 -- ^ Adjacent parent headers
11281114 -> ParentHeader
11291115 -- Parent block header
1130- -> Bool
1131- -- ^ Non-deterministcally selected vote (True = yes, False = no)
1116+ -> ForkNumber
1117+ -- ^ Target fork number. Vote "yes" to increase fork number, if the fork
1118+ -- number of the parent header is less than this value.
11321119 -> ForkState
1133- newForkState as p vote
1120+ newForkState as p targetFork
11341121 | isLastForkEpochBlock (view parentHeader p) = cur
11351122 -- reset votes and vote
1136- & forkVotes .~ (if vote then addVote else id ) resetVotes
1123+ & forkVotes .~ (if vote then addVote resetVotes else resetVotes)
11371124 -- based on current vote count decide whether to increase fork number
11381125 & forkNumber %~ (if decideVotes curVotes then succ else id )
11391126 | isForkVoteBlock (view parentHeader p) = cur
@@ -1143,10 +1130,15 @@ newForkState as p vote
11431130 -- do one vote counting step
11441131 & forkVotes .~ countVotes allParentVotes
11451132 where
1133+ vote = curNumber < targetFork
11461134 cur = view (parentHeader . blockForkState) p
1135+ curNumber = view (parentHeader . blockForkNumber) p
11471136 curVotes = view (parentHeader . blockForkVotes ) p
11481137 allParentVotes = view (parentHeader . blockForkVotes) <$> (p : HM. elems as)
11491138
1139+ genesisForkState :: ForkState
1140+ genesisForkState = ForkState 0
1141+
11501142-- -------------------------------------------------------------------------- --
11511143-- IsBlockHeader
11521144
@@ -1196,7 +1188,7 @@ newBlockHeader
11961188 -> BlockHeader
11971189newBlockHeader adj pay nonce t p@ (ParentHeader b) =
11981190 fromLog @ ChainwebMerkleHashAlgorithm $ newMerkleLog
1199- $ mkFeatureFlags
1191+ $ newForkState adj p (_blockForkNumber b)
12001192 :+: t
12011193 :+: _blockHash b
12021194 :+: target
0 commit comments