Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit d828861

Browse files
committed
remove feature flags logic
1 parent 9c47353 commit d828861

File tree

9 files changed

+104
-90
lines changed

9 files changed

+104
-90
lines changed

src/Chainweb/BlockHeader.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ module Chainweb.BlockHeader
3636
, I.decodeEpochStartTime
3737
, I.epochStart
3838

39-
-- * FeatureFlags
40-
, I.FeatureFlags
41-
, I.mkFeatureFlags
42-
, I.encodeFeatureFlags
43-
, I.decodeFeatureFlags
44-
4539
-- * POW Target
4640
, I.powTarget
4741

@@ -95,6 +89,8 @@ module Chainweb.BlockHeader
9589
, I.forkEpochLength
9690
, I.isForkCountBlock
9791
, I.isForkVoteBlock
92+
, I.newForkState
93+
, I.genesisForkState
9894

9995
-- * IsBlockHeader
10096
, I.IsBlockHeader(..)
@@ -130,7 +126,7 @@ import Chainweb.Payload (BlockPayloadHash)
130126
import Chainweb.Version (ChainwebVersionCode)
131127
import Control.Lens (Getter)
132128

133-
blockFlags :: Getter I.BlockHeader I.FeatureFlags
129+
blockFlags :: Getter I.BlockHeader ForkState
134130
blockFlags = I.blockFlags
135131

136132
blockCreationTime :: Getter I.BlockHeader BlockCreationTime

src/Chainweb/BlockHeader/Internal.hs

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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
258253
decodeEpochStartTime = 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
--
303298
data 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

850846
encodeBlockHeaderWithoutHash :: BlockHeader -> Put
851847
encodeBlockHeaderWithoutHash 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
--
898894
decodeBlockHeaderWithoutHash :: Get BlockHeader
899895
decodeBlockHeaderWithoutHash = 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
--
930926
decodeBlockHeader :: Get BlockHeader
931927
decodeBlockHeader = 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
11971189
newBlockHeader 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

src/Chainweb/BlockHeader/Validation.hs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module Chainweb.BlockHeader.Validation
7777
, prop_block_genesis_parent
7878
, prop_block_genesis_target
7979
, prop_block_target
80-
, prop_block_featureFlags
80+
, prop_block_forkVotesReset
8181

8282
-- * Inductive BlockHeader Properties
8383
, prop_block_epoch
@@ -88,6 +88,9 @@ module Chainweb.BlockHeader.Validation
8888
, prop_block_creationTime
8989
, prop_block_adjacent_chainIds
9090
, prop_block_adjacent_parents_version
91+
, prop_block_forkNumber
92+
, prop_block_forkVote
93+
, prop_block_forkVoteCount
9194
) where
9295

9396
import Control.Lens
@@ -650,7 +653,6 @@ validateIntrinsic t b = concat
650653
, [ IncorrectGenesisParent | not (prop_block_genesis_parent b)]
651654
, [ IncorrectGenesisTarget | not (prop_block_genesis_target b)]
652655
, [ BlockInTheFuture | not (prop_block_current t b)]
653-
, [ InvalidFeatureFlags | not (prop_block_featureFlags b)]
654656
, [ AdjacentChainMismatch | not (prop_block_adjacent_chainIds b) ]
655657
, [ InvalidForkVotes | not (prop_block_forkVotesReset b) ]
656658
]
@@ -723,14 +725,14 @@ prop_block_genesis_target b = isGenesisBlockHeader b
723725
prop_block_current :: Time Micros -> BlockHeader -> Bool
724726
prop_block_current t b = BlockCreationTime t >= view blockCreationTime b
725727

726-
prop_block_featureFlags :: BlockHeader -> Bool
727-
prop_block_featureFlags b
728-
| skipFeatureFlagValidationGuard v cid h = True
729-
| otherwise = view blockFlags b == mkFeatureFlags
730-
where
731-
v = _chainwebVersion b
732-
h = view blockHeight b
733-
cid = _chainId b
728+
-- prop_block_featureFlags :: BlockHeader -> Bool
729+
-- prop_block_featureFlags b
730+
-- | skipFeatureFlagValidationGuard v cid h = True
731+
-- | otherwise = view blockFlags b == mkFeatureFlags
732+
-- where
733+
-- v = _chainwebVersion b
734+
-- h = view blockHeight b
735+
-- cid = _chainId b
734736

735737
-- | Verify that the adjacent hashes of the block are for the correct set of
736738
-- chain ids.

src/Chainweb/ForkState.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{-# LANGUAGE DeriveAnyClass #-}
44
{-# LANGUAGE DeriveGeneric #-}
55
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+
{-# LANGUAGE DataKinds #-}
7+
{-# LANGUAGE FlexibleInstances #-}
8+
{-# LANGUAGE MultiParamTypeClasses #-}
9+
{-# LANGUAGE TypeFamilies #-}
610

711
-- |
812
-- Module: Chainweb.ForkState
@@ -35,6 +39,8 @@ module Chainweb.ForkState
3539
) where
3640

3741
import Data.Word
42+
import Chainweb.Crypto.MerkleLog
43+
import Chainweb.MerkleUniverse
3844
import Chainweb.Utils
3945
import Chainweb.Utils.Serialization (Put, Get, putWord64le, getWord64le)
4046
import Control.DeepSeq (NFData)
@@ -90,8 +96,9 @@ import Numeric.Natural
9096
-- released that proposes a fork.
9197
--
9298
newtype ForkState = ForkState { _forkState :: Word64 }
93-
deriving (Eq, Ord, Generic)
99+
deriving (Show, Eq, Ord, Generic)
94100
deriving anyclass (NFData)
101+
deriving newtype (ToJSON, FromJSON)
95102

96103
-- | Little endian encoding of the fork state.
97104
--
@@ -103,6 +110,11 @@ encodeForkState (ForkState h) = putWord64le h
103110
decodeForkState :: Get ForkState
104111
decodeForkState = ForkState <$> getWord64le
105112

113+
instance MerkleHashAlgorithm a => IsMerkleLogEntry a ChainwebHashTag ForkState where
114+
type Tag ForkState = 'ForkStateTag
115+
toMerkleNode = encodeMerkleInputNode encodeForkState
116+
fromMerkleNode = decodeMerkleInputNode decodeForkState
117+
106118
-- ---------------------------------------------------------------------------
107119
-- Fork Number
108120

src/Chainweb/MerkleUniverse.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ data ChainwebHashTag
9090
| MinerDataTag
9191
| CoinbaseOutputTag
9292
| EpochStartTimeTag
93-
| FeatureFlagsTag
93+
| ForkStateTag
9494

9595
-- Event Proofs
9696
| OutputEventsTag
@@ -106,7 +106,7 @@ instance MerkleUniverse ChainwebHashTag where
106106
type MerkleTagVal ChainwebHashTag 'BlockHeightTag = 0x0003
107107
type MerkleTagVal ChainwebHashTag 'BlockWeightTag = 0x0004
108108
type MerkleTagVal ChainwebHashTag 'BlockPayloadHashTag = 0x0005
109-
type MerkleTagVal ChainwebHashTag 'FeatureFlagsTag = 0x0006
109+
type MerkleTagVal ChainwebHashTag 'ForkStateTag = 0x0006
110110
type MerkleTagVal ChainwebHashTag 'BlockCreationTimeTag = 0x0007
111111
type MerkleTagVal ChainwebHashTag 'ChainwebVersionTag = 0x0008
112112
type MerkleTagVal ChainwebHashTag 'PowHashTag = 0x0009

test/lib/Chainweb/Test/Orphans/Internal.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ import P2P.Test.Orphans ()
166166
import System.Logger.Types
167167

168168
import Utils.Logging
169+
import Chainweb.ForkState
169170

170171
-- -------------------------------------------------------------------------- --
171172
-- Utils
@@ -327,8 +328,8 @@ instance Arbitrary BlockCreationTime where
327328
instance Arbitrary EpochStartTime where
328329
arbitrary = EpochStartTime <$> arbitrary
329330

330-
instance Arbitrary FeatureFlags where
331-
arbitrary = return mkFeatureFlags
331+
instance Arbitrary ForkState where
332+
arbitrary = ForkState <$> arbitrary
332333

333334
instance Arbitrary BlockHeader where
334335
arbitrary = arbitrary >>= arbitraryBlockHeaderVersion
@@ -849,7 +850,7 @@ instance Arbitrary MiningConfig where
849850
arbitraryEventPactValue :: Gen PactValue
850851
arbitraryEventPactValue = oneof
851852
[ PLiteral . LString <$> arbitrary
852-
, PLiteral . LInteger <$> (int256ToInteger <$> arbitrary)
853+
, PLiteral . LInteger . int256ToInteger <$> arbitrary
853854
]
854855

855856
-- | Arbitrary Pact events that are supported in events proofs

test/lib/Chainweb/Test/Utils.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,16 @@ trunk g h = do
500500

501501
-- | Generate some new `BlockHeader` based on a parent.
502502
--
503+
-- Adjacent headers are ignored, which means that the resultting header is
504+
-- valid only in a singleton Chainweb.
505+
--
503506
header :: BlockHeader -> Gen BlockHeader
504507
header p = do
505508
nonce <- Nonce <$> chooseAny
506509
return
507510
. fromLog @ChainwebMerkleHashAlgorithm
508511
. newMerkleLog
509-
$ mkFeatureFlags
512+
$ newForkState mempty (ParentHeader p) (view blockForkNumber p)
510513
:+: t'
511514
:+: view blockHash p
512515
:+: target
@@ -527,7 +530,7 @@ header p = do
527530
-- | get arbitrary value for seed.
528531
-- > getArbitrary @BlockHash 0
529532
getArbitrary :: Arbitrary a => Int -> a
530-
getArbitrary seed = unGen arbitrary (mkQCGen 0) seed
533+
getArbitrary = unGen arbitrary (mkQCGen 0)
531534

532535
-- -------------------------------------------------------------------------- --
533536
-- Test Chain Database Configurations
@@ -701,7 +704,7 @@ withChainwebTestServer shouldValidateSpec tls v app =
701704
let
702705
settings =
703706
W.setBeforeMainLoop (putMVar readyVar ()) $
704-
W.setOnExceptionResponse verboseOnExceptionResponse $
707+
W.setOnExceptionResponse verboseOnExceptionResponse
705708
W.defaultSettings
706709
if
707710
| tls -> do

0 commit comments

Comments
 (0)