diff --git a/src/Chainweb/BlockHeader.hs b/src/Chainweb/BlockHeader.hs index 6090d60ffa..4224990a70 100644 --- a/src/Chainweb/BlockHeader.hs +++ b/src/Chainweb/BlockHeader.hs @@ -121,6 +121,8 @@ module Chainweb.BlockHeader import Control.DeepSeq import Control.Exception import Control.Lens hiding ((.=)) +import Control.Monad +import Control.Monad.Reader import Control.Monad.Catch import Data.Aeson @@ -337,7 +339,7 @@ data BlockHeader :: Type where -- the block height of a block is the block height of its parent -- plus one. - , _blockChainwebVersion :: !ChainwebVersionCode + , _blockChainwebVersion :: !ChainwebVersion -- ^ the Chainweb version is a constant for the chain. A chain -- is uniquely identified by its genesis block. Thus this is -- redundant information and thus subject to the inductive property @@ -691,7 +693,7 @@ makeGenesisBlockHeader' -> Nonce -> BlockHeader makeGenesisBlockHeader' v p ct@(BlockCreationTime t) n = - fromLog @ChainwebMerkleHashAlgorithm mlog + fromJuste $ runReaderT (fromLog @ChainwebMerkleHashAlgorithm mlog) v where g = genesisGraph v p cid = _chainId p @@ -754,7 +756,7 @@ genesisGraph v = chainGraphAt v_ . genesisHeight' v_ . _chainId genesisHeight :: HasCallStack => ChainwebVersion -> ChainId -> BlockHeight genesisHeight v c = _blockHeight (genesisBlockHeader v c) -instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader where +instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader (ReaderT ChainwebVersion Maybe) where -- /IMPORTANT/ a types must occur at most once in this list type MerkleLogHeader BlockHeader = @@ -784,12 +786,14 @@ instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader wh :+: _blockChainId bh :+: _blockWeight bh :+: _blockHeight bh - :+: _blockChainwebVersion bh + :+: _versionCode (_blockChainwebVersion bh) :+: _blockEpochStart bh :+: _blockNonce bh :+: MerkleLogBody (blockHashRecordToVector $ _blockAdjacentHashes bh) - fromLog l = BlockHeader + fromLog l = ReaderT $ \cwv -> do + guard (_versionCode cwv == cwvc) + return BlockHeader { _blockFlags = flags , _blockCreationTime = time , _blockHash = BlockHash (MerkleLogHash $ _merkleLogRoot l) @@ -799,10 +803,10 @@ instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader wh , _blockChainId = cid , _blockWeight = weight , _blockHeight = height - , _blockChainwebVersion = cwvc + , _blockChainwebVersion = cwv , _blockEpochStart = es , _blockNonce = nonce - , _blockAdjacentHashes = blockHashRecordFromVector adjGraph cid adjParents + , _blockAdjacentHashes = blockHashRecordFromVector (adjGraph cwv) cid adjParents } where ( flags @@ -818,9 +822,8 @@ instance HasMerkleLog ChainwebMerkleHashAlgorithm ChainwebHashTag BlockHeader wh :+: nonce :+: MerkleLogBody adjParents ) = _merkleLogEntries l - cwv = _chainwebVersion cwvc - adjGraph + adjGraph cwv | height == genesisHeight' cwv cid = chainGraphAt cwv height | otherwise = chainGraphAt cwv (height - 1) @@ -835,7 +838,7 @@ encodeBlockHeaderWithoutHash b = do encodeChainId (_blockChainId b) encodeBlockWeight (_blockWeight b) encodeBlockHeight (_blockHeight b) - encodeChainwebVersionCode (_blockChainwebVersion b) + encodeChainwebVersionCode (_versionCode $ _blockChainwebVersion b) encodeEpochStartTime (_blockEpochStart b) encodeNonce (_blockNonce b) @@ -849,10 +852,10 @@ encodeBlockHeader b = do -- 1. chain id is in graph -- 2. all adjacentParent match adjacents in graph -- -decodeBlockHeaderChecked :: Get BlockHeader -decodeBlockHeaderChecked = do - !bh <- decodeBlockHeader - _ <- checkAdjacentChainIds bh bh (Expected $ _blockAdjacentChainIds bh) +decodeBlockHeaderChecked :: ChainwebVersion -> Get BlockHeader +decodeBlockHeaderChecked ver = do + !bh <- decodeBlockHeader ver + _ <- checkAdjacentChainIds (chainGraphAt ver (_blockHeight bh)) bh (Expected $ _blockAdjacentChainIds bh) return bh -- | Decode and check that @@ -863,17 +866,18 @@ decodeBlockHeaderChecked = do -- decodeBlockHeaderCheckedChainId :: HasChainId p - => Expected p + => ChainwebVersion + -> Expected p -> Get BlockHeader -decodeBlockHeaderCheckedChainId p = do - !bh <- decodeBlockHeaderChecked +decodeBlockHeaderCheckedChainId ver p = do + !bh <- decodeBlockHeaderChecked ver _ <- checkChainId p (Actual (_chainId bh)) return bh -- | Decode a BlockHeader and trust the result -- -decodeBlockHeaderWithoutHash :: Get BlockHeader -decodeBlockHeaderWithoutHash = do +decodeBlockHeaderWithoutHash :: ChainwebVersion -> Get BlockHeader +decodeBlockHeaderWithoutHash cwv = do a0 <- decodeFeatureFlags a1 <- decodeBlockCreationTime a2 <- decodeBlockHash -- parent hash @@ -886,39 +890,46 @@ decodeBlockHeaderWithoutHash = do a9 <- decodeChainwebVersionCode a11 <- decodeEpochStartTime a12 <- decodeNonce - return - $! fromLog @ChainwebMerkleHashAlgorithm - $ newMerkleLog - $ a0 - :+: a1 - :+: a2 - :+: a4 - :+: a5 - :+: a6 - :+: a7 - :+: a8 - :+: a9 - :+: a11 - :+: a12 - :+: MerkleLogBody (blockHashRecordToVector a3) + let merkleLog = + newMerkleLog + $ a0 + :+: a1 + :+: a2 + :+: a4 + :+: a5 + :+: a6 + :+: a7 + :+: a8 + :+: a9 + :+: a11 + :+: a12 + :+: MerkleLogBody (blockHashRecordToVector a3) + case runReaderT (fromLog @ChainwebMerkleHashAlgorithm merkleLog) cwv of + Nothing -> fail "decodeBlockHeaderWithoutHash: ChainwebVersionCode does not match" + Just !bh -> return bh -- | Decode a BlockHeader and trust the result -- -decodeBlockHeader :: Get BlockHeader -decodeBlockHeader = BlockHeader - <$> decodeFeatureFlags - <*> decodeBlockCreationTime - <*> decodeBlockHash -- parent hash - <*> decodeBlockHashRecord - <*> decodeHashTarget - <*> decodeBlockPayloadHash - <*> decodeChainId - <*> decodeBlockWeight - <*> decodeBlockHeight - <*> decodeChainwebVersionCode - <*> decodeEpochStartTime - <*> decodeNonce - <*> decodeBlockHash +decodeBlockHeader :: ChainwebVersion -> Get BlockHeader +decodeBlockHeader cwv = do + BlockHeader + <$> decodeFeatureFlags + <*> decodeBlockCreationTime + <*> decodeBlockHash -- parent hash + <*> decodeBlockHashRecord + <*> decodeHashTarget + <*> decodeBlockPayloadHash + <*> decodeChainId + <*> decodeBlockWeight + <*> decodeBlockHeight + <*> (do + vc <- decodeChainwebVersionCode + unless (vc == _versionCode cwv) $ fail "decodeBlockHeader: ChainwebVersionCode does not match" + return cwv + ) + <*> decodeEpochStartTime + <*> decodeNonce + <*> decodeBlockHash instance ToJSON BlockHeader where toJSON = toJSON . encodeB64UrlNoPaddingText . runPutS . encodeBlockHeader @@ -926,11 +937,11 @@ instance ToJSON BlockHeader where {-# INLINE toJSON #-} {-# INLINE toEncoding #-} -instance FromJSON BlockHeader where - parseJSON = withText "BlockHeader" $ \t -> - case runGetS decodeBlockHeader =<< decodeB64UrlNoPaddingText t of - Left (e :: SomeException) -> fail (sshow e) - (Right !x) -> return x +parseBlockHeaderJSON :: ChainwebVersion -> Value -> Parser BlockHeader +parseBlockHeaderJSON ver = withText "BlockHeader" $ \t -> + case runGetS (decodeBlockHeader ver) =<< decodeB64UrlNoPaddingText t of + Left (e :: SomeException) -> fail (sshow e) + (Right !x) -> return x _blockAdjacentChainIds :: BlockHeader -> HS.HashSet ChainId _blockAdjacentChainIds = @@ -1006,7 +1017,7 @@ blockHeaderProperties (ObjectEncoded b) = , "chainId" .= _chainId b , "weight" .= _blockWeight b , "height" .= _blockHeight b - , "chainwebVersion" .= _versionName (_chainwebVersion b) + , "chainwebVersion" .= _versionName (undefined b) , "epochStart" .= _blockEpochStart b , "featureFlags" .= _blockFlags b , "hash" .= _blockHash b @@ -1019,8 +1030,8 @@ instance ToJSON (ObjectEncoded BlockHeader) where {-# INLINE toJSON #-} {-# INLINE toEncoding #-} -parseBlockHeaderObject :: Object -> Parser BlockHeader -parseBlockHeaderObject o = BlockHeader +parseBlockHeaderObject :: ChainwebVersion -> Value -> Parser BlockHeader +parseBlockHeaderObject ver = withObject "BlockHeader" $ \o -> BlockHeader <$> o .: "featureFlags" <*> o .: "creationTime" <*> o .: "parent" @@ -1030,18 +1041,16 @@ parseBlockHeaderObject o = BlockHeader <*> o .: "chainId" <*> o .: "weight" <*> o .: "height" - -- TODO: lookupVersionByName should probably be deprecated for performance, - -- so perhaps we move this codec outside of the node proper. - <*> (_versionCode . lookupVersionByName <$> (o .: "chainwebVersion")) + <*> (do + vn <- o .: "chainwebVersion" + unless (vn == _versionName ver) + (fail "parseBlockHeaderObject: version name mismatch") + return ver + ) <*> o .: "epochStart" <*> o .: "nonce" <*> o .: "hash" -instance FromJSON (ObjectEncoded BlockHeader) where - parseJSON = withObject "BlockHeader" - $ fmap ObjectEncoded . parseBlockHeaderObject - {-# INLINE parseJSON #-} - -- -------------------------------------------------------------------------- -- -- IsBlockHeader @@ -1057,7 +1066,7 @@ instance IsBlockHeader BlockHeader where -- Create new BlockHeader -- | Creates a new block header. No validation of the input parameters is --- performaned. +-- performed. -- -- It's not guaranteed that the result is a valid block header. It is, however, -- guaranteed by construction that @@ -1089,7 +1098,9 @@ newBlockHeader -- ^ parent block header -> BlockHeader newBlockHeader adj pay nonce t p@(ParentHeader b) = - fromLog @ChainwebMerkleHashAlgorithm $ newMerkleLog + fromJuste $ runReaderT (fromLog @ChainwebMerkleHashAlgorithm merkleLog) v + where + merkleLog = newMerkleLog $ mkFeatureFlags :+: t :+: _blockHash b @@ -1102,7 +1113,6 @@ newBlockHeader adj pay nonce t p@(ParentHeader b) = :+: epochStart p adj t :+: nonce :+: MerkleLogBody (blockHashRecordToVector adjHashes) - where cid = _chainId p v = _chainwebVersion p target = powTarget p adj t diff --git a/src/Chainweb/BlockHeaderDB/Internal.hs b/src/Chainweb/BlockHeaderDB/Internal.hs index 419e950375..42a7318c31 100644 --- a/src/Chainweb/BlockHeaderDB/Internal.hs +++ b/src/Chainweb/BlockHeaderDB/Internal.hs @@ -95,7 +95,7 @@ data Configuration = Configuration newtype RankedBlockHeader = RankedBlockHeader { _getRankedBlockHeader :: BlockHeader } deriving (Show, Generic) deriving anyclass (NFData) - deriving newtype (Hashable, Eq, ToJSON, FromJSON) + deriving newtype (Hashable, Eq, ToJSON) instance HasChainwebVersion RankedBlockHeader where _chainwebVersion = _chainwebVersion . _getRankedBlockHeader @@ -148,8 +148,8 @@ encodeRankedBlockHeader :: RankedBlockHeader -> Put encodeRankedBlockHeader = encodeBlockHeader . _getRankedBlockHeader {-# INLINE encodeRankedBlockHeader #-} -decodeRankedBlockHeader :: Get RankedBlockHeader -decodeRankedBlockHeader = RankedBlockHeader <$!> decodeBlockHeader +decodeRankedBlockHeader :: ChainwebVersion -> Get RankedBlockHeader +decodeRankedBlockHeader v = RankedBlockHeader <$!> decodeBlockHeader v {-# INLINE decodeRankedBlockHeader #-} encodeRankedBlockHash :: RankedBlockHash -> Put @@ -242,13 +242,14 @@ initBlockHeaderDb config = do dbAddChecked db rootEntry return db where + v = _chainwebVersion rootEntry rootEntry = _configRoot config cid = _chainId rootEntry cidNs = T.encodeUtf8 (toText cid) headerTable = newTable (_configRocksDb config) - (Codec (runPutS . encodeRankedBlockHeader) (runGetS decodeRankedBlockHeader)) + (Codec (runPutS . encodeRankedBlockHeader) (runGetS (decodeRankedBlockHeader v))) (Codec (runPutS . encodeRankedBlockHash) (runGetS decodeRankedBlockHash)) ["BlockHeader", cidNs, "header"] @@ -259,7 +260,7 @@ initBlockHeaderDb config = do ["BlockHeader", cidNs, "rank"] !db = BlockHeaderDb cid - (_chainwebVersion rootEntry) + v headerTable rankTable diff --git a/src/Chainweb/BlockHeaderDB/RestAPI.hs b/src/Chainweb/BlockHeaderDB/RestAPI.hs index d3c3cbb2ed..be8932a47a 100644 --- a/src/Chainweb/BlockHeaderDB/RestAPI.hs +++ b/src/Chainweb/BlockHeaderDB/RestAPI.hs @@ -121,9 +121,9 @@ type BlockHeaderPage = Page (NextItem BlockHash) BlockHeader -- | Orphan instance to encode BlockHeaders as OctetStream -- -instance MimeUnrender OctetStream BlockHeader where - mimeUnrender _ = runGetEitherL decodeBlockHeader - {-# INLINE mimeUnrender #-} +-- instance MimeUnrender OctetStream BlockHeader where +-- mimeUnrender _ = runGetEitherL decodeBlockHeader +-- {-# INLINE mimeUnrender #-} -- | Orphan instance to encode BlockHeaders as OctetStream -- @@ -144,17 +144,17 @@ data JsonBlockHeaderObject instance Accept JsonBlockHeaderObject where contentType _ = "application" // "json" /: ("blockheader-encoding", "object") -instance MimeUnrender JsonBlockHeaderObject BlockHeader where - mimeUnrender _ = second _objectEncoded . eitherDecode - {-# INLINE mimeUnrender #-} +-- instance MimeUnrender JsonBlockHeaderObject BlockHeader where +-- mimeUnrender _ = second _objectEncoded . eitherDecode +-- {-# INLINE mimeUnrender #-} instance MimeRender JsonBlockHeaderObject BlockHeader where mimeRender _ = encode . ObjectEncoded {-# INLINE mimeRender #-} -instance MimeUnrender JsonBlockHeaderObject BlockHeaderPage where - mimeUnrender _ = second (fmap _objectEncoded) . eitherDecode - {-# INLINE mimeUnrender #-} +-- instance MimeUnrender JsonBlockHeaderObject BlockHeaderPage where +-- mimeUnrender _ = second (fmap _objectEncoded) . eitherDecode +-- {-# INLINE mimeUnrender #-} instance MimeRender JsonBlockHeaderObject BlockHeaderPage where mimeRender _ = encode . fmap ObjectEncoded @@ -405,13 +405,13 @@ instance ToJSON HeaderUpdate where {-# INLINE toJSON #-} {-# INLINE toEncoding #-} -instance FromJSON HeaderUpdate where - parseJSON = withObject "HeaderUpdate" $ \o -> HeaderUpdate - <$> o .: "header" - <*> o .: "txCount" - <*> o .: "powHash" - <*> o .: "target" - {-# INLINE parseJSON #-} +-- instance FromJSON HeaderUpdate where +-- parseJSON = withObject "HeaderUpdate" $ \o -> HeaderUpdate +-- <$> o .: "header" +-- <*> o .: "txCount" +-- <*> o .: "powHash" +-- <*> o .: "target" +-- {-# INLINE parseJSON #-} type HeaderStreamApi_ = "header" :> "updates" :> Raw diff --git a/src/Chainweb/BlockHeaderDB/RestAPI/Client.hs b/src/Chainweb/BlockHeaderDB/RestAPI/Client.hs index efde6173d3..28d1e0f911 100644 --- a/src/Chainweb/BlockHeaderDB/RestAPI/Client.hs +++ b/src/Chainweb/BlockHeaderDB/RestAPI/Client.hs @@ -19,30 +19,31 @@ -- Stability: experimental -- module Chainweb.BlockHeaderDB.RestAPI.Client -( headerClient_ -, headerClient -, headerClientContentType_ -, headerClientJson -, headerClientJsonPretty -, headerClientJsonBinary - -, hashesClient_ -, hashesClient -, headersClient_ -, headersClient -, headersClientContentType_ -, headersClientJson -, headersClientJsonPretty - -, branchHashesClient_ -, branchHashesClient - -, branchHeadersClient_ -, branchHeadersClient -, branchHeadersClientContentType_ -, branchHeadersClientJson -, branchHeadersClientJsonPretty -) where +-- ( headerClient_ +-- , headerClient +-- , headerClientContentType_ +-- , headerClientJson +-- , headerClientJsonPretty +-- , headerClientJsonBinary + +-- , hashesClient_ +-- , hashesClient +-- , headersClient_ +-- , headersClient +-- , headersClientContentType_ +-- , headersClientJson +-- , headersClientJsonPretty + +-- , branchHashesClient_ +-- , branchHashesClient + +-- , branchHeadersClient_ +-- , branchHeadersClient +-- , branchHeadersClientContentType_ +-- , branchHeadersClientJson +-- , branchHeadersClientJsonPretty +-- ) where +where import Control.Monad.Identity @@ -68,270 +69,270 @@ import Chainweb.Version -- -------------------------------------------------------------------------- -- -- GET Header Client -headerClient_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => DbKey BlockHeaderDb - -> ClientM BlockHeader -headerClient_ = headerClientContentType_ @v @c @OctetStream - -headerClient - :: ChainwebVersion - -> ChainId - -> DbKey BlockHeaderDb - -> ClientM BlockHeader -headerClient = headerClientJsonBinary - -headerClientContentType_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) (ct :: Type) x - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => Accept ct - => SupportedRespBodyContentType ct x BlockHeader - => (HeaderApi v c) ~ x - => BlockHash - -> ClientM BlockHeader -headerClientContentType_ = client (Proxy @(SetRespBodyContentType ct x)) - -headerClientJson - :: ChainwebVersion - -> ChainId - -> DbKey BlockHeaderDb - -> ClientM BlockHeader -headerClientJson v c k = runIdentity $ do - (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) - (SomeSing (SChainId :: Sing c)) <- return $ toSing c - return $ headerClientContentType_ @v @c @JSON k - -headerClientJsonPretty - :: ChainwebVersion - -> ChainId - -> BlockHash - -> ClientM BlockHeader -headerClientJsonPretty v c k = runIdentity $ do - (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) - (SomeSing (SChainId :: Sing c)) <- return $ toSing c - return $ headerClientContentType_ @v @c @JsonBlockHeaderObject k - -headerClientJsonBinary - :: ChainwebVersion - -> ChainId - -> BlockHash - -> ClientM BlockHeader -headerClientJsonBinary v c k = runIdentity $ do - (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) - (SomeSing (SChainId :: Sing c)) <- return $ toSing c - return $ headerClientContentType_ @v @c @OctetStream k - --- -------------------------------------------------------------------------- -- --- Headers Client - -headersClient_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> ClientM BlockHeaderPage -headersClient_ = headersClientContentType_ @v @c @JSON - -headersClient - :: ChainwebVersion - -- ^ The remote chainweb that you wish to query from. - -> ChainId - -- ^ The remote chain within the web that you wish to query from. - -> Maybe Limit - -- ^ The number of responses per-`Page` to return. - -> Maybe (NextItem BlockHash) - -- ^ The first header you want to see within the `Page`. - -- `Page` contains a field `_pageNext`, which can be used - -- to produce the value needed for subsequent calls. - -> Maybe MinRank - -- ^ Filter: no header of `BlockHeight` lower than this will be returned. - -> Maybe MaxRank - -- ^ Filter: no header of `BlockHeight` higher than this will be returned. - -> ClientM BlockHeaderPage -headersClient = headersClientJson - -headersClientContentType_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) (ct :: Type) x - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => Accept ct - => HeadersApi v c ~ x - => SupportedRespBodyContentType ct x BlockHeaderPage - => Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> ClientM BlockHeaderPage -headersClientContentType_ = client $ Proxy @(SetRespBodyContentType ct (HeadersApi v c)) - -headersClientJson - :: ChainwebVersion - -- ^ The remote chainweb that you wish to query from. - -> ChainId - -- ^ The remote chain within the web that you wish to query from. - -> Maybe Limit - -- ^ The number of responses per-`Page` to return. - -> Maybe (NextItem BlockHash) - -- ^ The first header you want to see within the `Page`. - -- `Page` contains a field `_pageNext`, which can be used - -- to produce the value needed for subsequent calls. - -> Maybe MinRank - -- ^ Filter: no header of `BlockHeight` lower than this will be returned. - -> Maybe MaxRank - -- ^ Filter: no header of `BlockHeight` higher than this will be returned. - -> ClientM BlockHeaderPage -headersClientJson v c limit start minr maxr = runIdentity $ do - (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) - (SomeSing (SChainId :: Sing c)) <- return $ toSing c - return $ headersClientContentType_ @v @c @JSON limit start minr maxr - -headersClientJsonPretty - :: ChainwebVersion - -- ^ The remote chainweb that you wish to query from. - -> ChainId - -- ^ The remote chain within the web that you wish to query from. - -> Maybe Limit - -- ^ The number of responses per-`Page` to return. - -> Maybe (NextItem BlockHash) - -- ^ The first header you want to see within the `Page`. - -- `Page` contains a field `_pageNext`, which can be used - -- to produce the value needed for subsequent calls. - -> Maybe MinRank - -- ^ Filter: no header of `BlockHeight` lower than this will be returned. - -> Maybe MaxRank - -- ^ Filter: no header of `BlockHeight` higher than this will be returned. - -> ClientM BlockHeaderPage -headersClientJsonPretty (FromSingChainwebVersion (SChainwebVersion :: Sing v)) c limit start minr maxr = runIdentity $ do - (SomeSing (SChainId :: Sing c)) <- return $ toSing c - return $ headersClientContentType_ @v @c @JsonBlockHeaderObject limit start minr maxr - --- -------------------------------------------------------------------------- -- --- Branch Hashes Client - -branchHashesClient_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHashPage -branchHashesClient_ = client (branchHashesApi @v @c) - -branchHashesClient - :: ChainwebVersion - -> ChainId - -> Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHashPage -branchHashesClient v c limit start minr maxr bounds = runIdentity $ do - SomeSing (SChainwebVersion :: Sing v) <- return $ toSing (_versionName v) - SomeSing (SChainId :: Sing c) <- return $ toSing c - return $ branchHashesClient_ @v @c limit start minr maxr bounds - --- -------------------------------------------------------------------------- -- --- Branch Headers Client - -branchHeadersClient_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHeaderPage -branchHeadersClient_ = branchHeadersClientContentType_ @v @c @JSON - -branchHeadersClient - :: ChainwebVersion - -> ChainId - -> Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHeaderPage -branchHeadersClient = branchHeadersClientJson - -branchHeadersClientContentType_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) (ct :: Type) api - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => HeadersApi v c ~ api - => SupportedRespBodyContentType ct api BlockHeaderPage - => Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHeaderPage -branchHeadersClientContentType_ = client - $ Proxy @(SetRespBodyContentType ct (BranchHeadersApi v c)) - -branchHeadersClientJson - :: ChainwebVersion - -> ChainId - -> Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHeaderPage -branchHeadersClientJson v c limit start minr maxr bounds = runIdentity $ do - SomeSing (SChainwebVersion :: Sing v) <- return $ toSing (_versionName v) - SomeSing (SChainId :: Sing c) <- return $ toSing c - return $ branchHeadersClientContentType_ @v @c @JSON limit start minr maxr bounds - -branchHeadersClientJsonPretty - :: ChainwebVersion - -> ChainId - -> Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> BranchBounds BlockHeaderDb - -> ClientM BlockHeaderPage -branchHeadersClientJsonPretty v c limit start minr maxr bounds = runIdentity $ do - SomeSing (SChainwebVersion :: Sing v) <- return $ toSing (_versionName v) - SomeSing (SChainId :: Sing c) <- return $ toSing c - return $ branchHeadersClientContentType_ @v @c @JsonBlockHeaderObject limit start minr maxr bounds - --- -------------------------------------------------------------------------- -- --- Hashes Client - -hashesClient_ - :: forall (v :: ChainwebVersionT) (c :: ChainIdT) - . KnownChainwebVersionSymbol v - => KnownChainIdSymbol c - => Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> ClientM BlockHashPage -hashesClient_ = client (hashesApi @v @c) - -hashesClient - :: ChainwebVersion - -> ChainId - -> Maybe Limit - -> Maybe (NextItem BlockHash) - -> Maybe MinRank - -> Maybe MaxRank - -> ClientM BlockHashPage -hashesClient v c limit start minr maxr = runIdentity $ do - (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) - (SomeSing (SChainId :: Sing c)) <- return $ toSing c - return $ hashesClient_ @v @c limit start minr maxr +-- headerClient_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => DbKey BlockHeaderDb +-- -> ClientM BlockHeader +-- headerClient_ = headerClientContentType_ @v @c @OctetStream + +-- headerClient +-- :: ChainwebVersion +-- -> ChainId +-- -> DbKey BlockHeaderDb +-- -> ClientM BlockHeader +-- headerClient = headerClientJsonBinary + +-- headerClientContentType_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) (ct :: Type) x +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => Accept ct +-- => SupportedRespBodyContentType ct x BlockHeader +-- => (HeaderApi v c) ~ x +-- => BlockHash +-- -> ClientM BlockHeader +-- headerClientContentType_ = client (Proxy @(SetRespBodyContentType ct x)) + +-- headerClientJson +-- :: ChainwebVersion +-- -> ChainId +-- -> DbKey BlockHeaderDb +-- -> ClientM BlockHeader +-- headerClientJson v c k = runIdentity $ do +-- (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) +-- (SomeSing (SChainId :: Sing c)) <- return $ toSing c +-- return $ headerClientContentType_ @v @c @JSON k + +-- headerClientJsonPretty +-- :: ChainwebVersion +-- -> ChainId +-- -> BlockHash +-- -> ClientM BlockHeader +-- headerClientJsonPretty v c k = runIdentity $ do +-- (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) +-- (SomeSing (SChainId :: Sing c)) <- return $ toSing c +-- return $ headerClientContentType_ @v @c @JsonBlockHeaderObject k + +-- headerClientJsonBinary +-- :: ChainwebVersion +-- -> ChainId +-- -> BlockHash +-- -> ClientM BlockHeader +-- headerClientJsonBinary v c k = runIdentity $ do +-- (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) +-- (SomeSing (SChainId :: Sing c)) <- return $ toSing c +-- return $ headerClientContentType_ @v @c @OctetStream k + +-- -- -------------------------------------------------------------------------- -- +-- -- Headers Client + +-- headersClient_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> ClientM BlockHeaderPage +-- headersClient_ = headersClientContentType_ @v @c @JSON + +-- headersClient +-- :: ChainwebVersion +-- -- ^ The remote chainweb that you wish to query from. +-- -> ChainId +-- -- ^ The remote chain within the web that you wish to query from. +-- -> Maybe Limit +-- -- ^ The number of responses per-`Page` to return. +-- -> Maybe (NextItem BlockHash) +-- -- ^ The first header you want to see within the `Page`. +-- -- `Page` contains a field `_pageNext`, which can be used +-- -- to produce the value needed for subsequent calls. +-- -> Maybe MinRank +-- -- ^ Filter: no header of `BlockHeight` lower than this will be returned. +-- -> Maybe MaxRank +-- -- ^ Filter: no header of `BlockHeight` higher than this will be returned. +-- -> ClientM BlockHeaderPage +-- headersClient = headersClientJson + +-- headersClientContentType_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) (ct :: Type) x +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => Accept ct +-- => HeadersApi v c ~ x +-- => SupportedRespBodyContentType ct x BlockHeaderPage +-- => Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> ClientM BlockHeaderPage +-- headersClientContentType_ = client $ Proxy @(SetRespBodyContentType ct (HeadersApi v c)) + +-- headersClientJson +-- :: ChainwebVersion +-- -- ^ The remote chainweb that you wish to query from. +-- -> ChainId +-- -- ^ The remote chain within the web that you wish to query from. +-- -> Maybe Limit +-- -- ^ The number of responses per-`Page` to return. +-- -> Maybe (NextItem BlockHash) +-- -- ^ The first header you want to see within the `Page`. +-- -- `Page` contains a field `_pageNext`, which can be used +-- -- to produce the value needed for subsequent calls. +-- -> Maybe MinRank +-- -- ^ Filter: no header of `BlockHeight` lower than this will be returned. +-- -> Maybe MaxRank +-- -- ^ Filter: no header of `BlockHeight` higher than this will be returned. +-- -> ClientM BlockHeaderPage +-- headersClientJson v c limit start minr maxr = runIdentity $ do +-- (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) +-- (SomeSing (SChainId :: Sing c)) <- return $ toSing c +-- return $ headersClientContentType_ @v @c @JSON limit start minr maxr + +-- headersClientJsonPretty +-- :: ChainwebVersion +-- -- ^ The remote chainweb that you wish to query from. +-- -> ChainId +-- -- ^ The remote chain within the web that you wish to query from. +-- -> Maybe Limit +-- -- ^ The number of responses per-`Page` to return. +-- -> Maybe (NextItem BlockHash) +-- -- ^ The first header you want to see within the `Page`. +-- -- `Page` contains a field `_pageNext`, which can be used +-- -- to produce the value needed for subsequent calls. +-- -> Maybe MinRank +-- -- ^ Filter: no header of `BlockHeight` lower than this will be returned. +-- -> Maybe MaxRank +-- -- ^ Filter: no header of `BlockHeight` higher than this will be returned. +-- -> ClientM BlockHeaderPage +-- headersClientJsonPretty (FromSingChainwebVersion (SChainwebVersion :: Sing v)) c limit start minr maxr = runIdentity $ do +-- (SomeSing (SChainId :: Sing c)) <- return $ toSing c +-- return $ headersClientContentType_ @v @c @JsonBlockHeaderObject limit start minr maxr + +-- -- -------------------------------------------------------------------------- -- +-- -- Branch Hashes Client + +-- branchHashesClient_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHashPage +-- branchHashesClient_ = client (branchHashesApi @v @c) + +-- branchHashesClient +-- :: ChainwebVersion +-- -> ChainId +-- -> Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHashPage +-- branchHashesClient v c limit start minr maxr bounds = runIdentity $ do +-- SomeSing (SChainwebVersion :: Sing v) <- return $ toSing (_versionName v) +-- SomeSing (SChainId :: Sing c) <- return $ toSing c +-- return $ branchHashesClient_ @v @c limit start minr maxr bounds + +-- -- -------------------------------------------------------------------------- -- +-- -- Branch Headers Client + +-- branchHeadersClient_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHeaderPage +-- branchHeadersClient_ = branchHeadersClientContentType_ @v @c @JSON + +-- branchHeadersClient +-- :: ChainwebVersion +-- -> ChainId +-- -> Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHeaderPage +-- branchHeadersClient = branchHeadersClientJson + +-- branchHeadersClientContentType_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) (ct :: Type) api +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => HeadersApi v c ~ api +-- => SupportedRespBodyContentType ct api BlockHeaderPage +-- => Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHeaderPage +-- branchHeadersClientContentType_ = client +-- $ Proxy @(SetRespBodyContentType ct (BranchHeadersApi v c)) + +-- branchHeadersClientJson +-- :: ChainwebVersion +-- -> ChainId +-- -> Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHeaderPage +-- branchHeadersClientJson v c limit start minr maxr bounds = runIdentity $ do +-- SomeSing (SChainwebVersion :: Sing v) <- return $ toSing (_versionName v) +-- SomeSing (SChainId :: Sing c) <- return $ toSing c +-- return $ branchHeadersClientContentType_ @v @c @JSON limit start minr maxr bounds + +-- branchHeadersClientJsonPretty +-- :: ChainwebVersion +-- -> ChainId +-- -> Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> BranchBounds BlockHeaderDb +-- -> ClientM BlockHeaderPage +-- branchHeadersClientJsonPretty v c limit start minr maxr bounds = runIdentity $ do +-- SomeSing (SChainwebVersion :: Sing v) <- return $ toSing (_versionName v) +-- SomeSing (SChainId :: Sing c) <- return $ toSing c +-- return $ branchHeadersClientContentType_ @v @c @JsonBlockHeaderObject limit start minr maxr bounds + +-- -- -------------------------------------------------------------------------- -- +-- -- Hashes Client + +-- hashesClient_ +-- :: forall (v :: ChainwebVersionT) (c :: ChainIdT) +-- . KnownChainwebVersionSymbol v +-- => KnownChainIdSymbol c +-- => Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> ClientM BlockHashPage +-- hashesClient_ = client (hashesApi @v @c) + +-- hashesClient +-- :: ChainwebVersion +-- -> ChainId +-- -> Maybe Limit +-- -> Maybe (NextItem BlockHash) +-- -> Maybe MinRank +-- -> Maybe MaxRank +-- -> ClientM BlockHashPage +-- hashesClient v c limit start minr maxr = runIdentity $ do +-- (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) +-- (SomeSing (SChainId :: Sing c)) <- return $ toSing c +-- return $ hashesClient_ @v @c limit start minr maxr diff --git a/src/Chainweb/Crypto/MerkleLog.hs b/src/Chainweb/Crypto/MerkleLog.hs index 64c18df8ba..471d377ebc 100644 --- a/src/Chainweb/Crypto/MerkleLog.hs +++ b/src/Chainweb/Crypto/MerkleLog.hs @@ -438,7 +438,7 @@ data MerkleLog a u (h :: [Type]) (b :: Type) = MerkleLog -- 2. a body that consists of a monomorphic sequence of 'IsMerkleEntry' -- instances. -- -class (MerkleUniverse u, MerkleHashAlgorithm a) => HasMerkleLog a u b | b -> u where +class (MerkleUniverse u, MerkleHashAlgorithm a) => HasMerkleLog a u b m | b -> u, b -> m where type MerkleLogHeader b :: [Type] -- The header of the Merkle log representation of the type. @@ -453,7 +453,7 @@ class (MerkleUniverse u, MerkleHashAlgorithm a) => HasMerkleLog a u b | b -> u w -- 'merkleLog' can be used to create the 'MerkleLog' from the root and -- the entries without forcing the computation of the Merkle tree field. - fromLog :: MerkleLog a u (MerkleLogHeader b) (MerkleLogBody b) -> b + fromLog :: MerkleLog a u (MerkleLogHeader b) (MerkleLogBody b) -> m b -- ^ Recover a value from a Merkle log. type MkLogType a u b = MerkleLog a u (MerkleLogHeader b) (MerkleLogBody b) @@ -589,8 +589,8 @@ bodySize = V.length . body -- be cached. -- computeMerkleLogRoot - :: forall a u b - . HasMerkleLog a u b + :: forall a u b m + . HasMerkleLog a u b m => b -> MerkleRoot a computeMerkleLogRoot = merkleRoot . _merkleLogTree . toLog @a @@ -613,10 +613,10 @@ computeMerkleLogRoot = merkleRoot . _merkleLogTree . toLog @a -- but, again, our current use case wouldn't justify the overhead. -- headerProof - :: forall c a u b m + :: forall c a u b m m' . MonadThrow m => HasHeader a u c (MkLogType a u b) - => HasMerkleLog a u b + => HasMerkleLog a u b m' => b -> m (MerkleProof a) headerProof = uncurry3 merkleProof . headerTree @c @a @@ -629,9 +629,9 @@ headerProof = uncurry3 merkleProof . headerTree @c @a -- Merkle tree and should be used for the leaf tree in the nested proof. -- headerTree - :: forall c a u b + :: forall c a u b m . HasHeader a u c (MkLogType a u b) - => HasMerkleLog a u b + => HasMerkleLog a u b m => b -> (MerkleNodeType a B.ByteString, Int, MerkleTree a) headerTree b = (node, p, _merkleLogTree @a mlog) @@ -648,9 +648,9 @@ headerTree b = (node, p, _merkleLogTree @a mlog) -- the subject. It should be used for inner trees in the nested proof. -- headerTree_ - :: forall c a u b + :: forall c a u b m . HasHeader a u c (MkLogType a u b) - => HasMerkleLog a u b + => HasMerkleLog a u b m => b -> (Int, MerkleTree a) headerTree_ b = (p, _merkleLogTree @a mlog) @@ -666,9 +666,9 @@ headerTree_ b = (p, _merkleLogTree @a mlog) -- 'MerkleLog' value. -- bodyProof - :: forall a u b m + :: forall a u b m m' . MonadThrow m - => HasMerkleLog a u b + => HasMerkleLog a u b m' => b -> Int -- ^ the index in the body of the log @@ -683,8 +683,8 @@ bodyProof b = uncurry3 merkleProof . bodyTree @a b -- Merkle tree and should be used for the leaf tree in the nested proof. -- bodyTree - :: forall a u b - . HasMerkleLog a u b + :: forall a u b m + . HasMerkleLog a u b m => b -> Int -- ^ the index in the body of the log @@ -702,8 +702,8 @@ bodyTree b i = (node, i_, _merkleLogTree @a mlog) -- the subject. It should be used for inner trees in the nested proof. -- bodyTree_ - :: forall a u b - . HasMerkleLog a u b + :: forall a u b m + . HasMerkleLog a u b m => b -> Int -- ^ the index in the body of the log diff --git a/src/Chainweb/Cut/Create.hs b/src/Chainweb/Cut/Create.hs index 2dd49bcc4d..450e76293c 100644 --- a/src/Chainweb/Cut/Create.hs +++ b/src/Chainweb/Cut/Create.hs @@ -311,8 +311,8 @@ newWorkHeaderPure hdb creationTime extension phash = do -- | Get all adjacent parent headers for a new block header for a given cut. -- -- This yields the same result as 'blockAdjacentParentHeaders', however, it is --- more efficent when the cut and the adjacent parent hashes are already known. --- Also, it works accross graph changes. It is not checked whether the given +-- more efficient when the cut and the adjacent parent hashes are already known. +-- Also, it works across graph changes. It is not checked whether the given -- adjacent parent hashes are consistent with the cut. -- -- Only those parents are included that are not block parent hashes of genesis @@ -365,8 +365,8 @@ newtype SolvedWork = SolvedWork BlockHeader encodeSolvedWork :: SolvedWork -> Put encodeSolvedWork (SolvedWork hdr) = encodeBlockHeaderWithoutHash hdr -decodeSolvedWork :: Get SolvedWork -decodeSolvedWork = SolvedWork <$> decodeBlockHeaderWithoutHash +decodeSolvedWork :: ChainwebVersion -> Get SolvedWork +decodeSolvedWork cwv = SolvedWork <$> decodeBlockHeaderWithoutHash cwv data InvalidSolvedHeader = InvalidSolvedHeader BlockHeader T.Text deriving (Show, Eq, Ord, Generic) diff --git a/src/Chainweb/Cut/CutHashes.hs b/src/Chainweb/Cut/CutHashes.hs index 2826098575..858bc10dbb 100644 --- a/src/Chainweb/Cut/CutHashes.hs +++ b/src/Chainweb/Cut/CutHashes.hs @@ -339,16 +339,16 @@ instance ToJSON CutHashes where {-# INLINE toJSON #-} {-# INLINE toEncoding #-} -instance FromJSON CutHashes where - parseJSON = withObject "CutHashes" $ \o -> CutHashes - <$> o .: "hashes" - <*> o .: "origin" - <*> o .: "weight" - <*> o .: "height" - <*> (fabricateVersionWithName <$> o .: "instance") - <*> o .: "id" - <*> o .:? "headers" .!= mempty - <*> o .:? "payloads" .!= mempty +-- instance FromJSON CutHashes where +-- parseJSON = withObject "CutHashes" $ \o -> CutHashes +-- <$> o .: "hashes" +-- <*> o .: "origin" +-- <*> o .: "weight" +-- <*> o .: "height" +-- <*> (fabricateVersionWithName <$> o .: "instance") +-- <*> o .: "id" +-- <*> o .:? "headers" .!= mempty +-- <*> o .:? "payloads" .!= mempty -- | Compute a 'CutHashes' structure from a 'Cut'. The result doesn't include -- any block headers or payloads. diff --git a/src/Chainweb/CutDB.hs b/src/Chainweb/CutDB.hs index ceadd73b76..e7f65b82a8 100644 --- a/src/Chainweb/CutDB.hs +++ b/src/Chainweb/CutDB.hs @@ -239,7 +239,7 @@ cutHashesTable rdb = Casify $ newTable rdb valueCodec keyCodec ["CutHashes"] keyCodec = Codec (\(a,b,c) -> runPutS $ encodeCutHeightBe a >> encodeBlockWeightBe b >> encodeCutId c) (runGetS $ (,,) <$> decodeCutHeightBe <*> decodeBlockWeightBe <*> decodeCutId) - valueCodec = Codec encodeToByteString decodeStrictOrThrow' + valueCodec = Codec encodeToByteString undefined -- decodeStrictOrThrow' -- -------------------------------------------------------------------------- -- -- Exceptions diff --git a/src/Chainweb/CutDB/RestAPI.hs b/src/Chainweb/CutDB/RestAPI.hs index adf03104aa..4ce829f1b2 100644 --- a/src/Chainweb/CutDB/RestAPI.hs +++ b/src/Chainweb/CutDB/RestAPI.hs @@ -24,7 +24,7 @@ module Chainweb.CutDB.RestAPI , cutApi -- * Some Cut API -, someCutApi +-- , someCutApi ) where import Data.Proxy @@ -84,5 +84,5 @@ cutApi = Proxy -- -------------------------------------------------------------------------- -- -- Some Cut Api -someCutApi :: ChainwebVersion -> SomeApi -someCutApi (FromSingChainwebVersion (SChainwebVersion :: Sing v)) = SomeApi $ cutApi @v +-- someCutApi :: ChainwebVersion -> SomeApi +-- someCutApi (FromSingChainwebVersion (SChainwebVersion :: Sing v)) = SomeApi $ cutApi @v diff --git a/src/Chainweb/CutDB/RestAPI/Client.hs b/src/Chainweb/CutDB/RestAPI/Client.hs index 1ca26c7c5e..7164cd5374 100644 --- a/src/Chainweb/CutDB/RestAPI/Client.hs +++ b/src/Chainweb/CutDB/RestAPI/Client.hs @@ -11,9 +11,9 @@ -- Client implementation of the 'Cut' REST API. -- module Chainweb.CutDB.RestAPI.Client -( cutGetClient -, cutGetClientLimit -, cutPutClient +-- ( cutGetClient +-- , cutGetClientLimit +( cutPutClient ) where import Servant.API (NoContent(..)) @@ -30,18 +30,18 @@ import Chainweb.Version -- -------------------------------------------------------------------------- -- -- GET Cut Client -cutGetClient - :: ChainwebVersion - -> ClientM CutHashes -cutGetClient (FromSingChainwebVersion (SChainwebVersion :: Sing v)) - = client (cutGetApi @v) Nothing - -cutGetClientLimit - :: ChainwebVersion - -> MaxRank - -> ClientM CutHashes -cutGetClientLimit (FromSingChainwebVersion (SChainwebVersion :: Sing v)) - = client (cutGetApi @v) . Just +-- cutGetClient +-- :: ChainwebVersion +-- -> ClientM CutHashes +-- cutGetClient (FromSingChainwebVersion (SChainwebVersion :: Sing v)) +-- = client (cutGetApi @v) Nothing + +-- cutGetClientLimit +-- :: ChainwebVersion +-- -> MaxRank +-- -> ClientM CutHashes +-- cutGetClientLimit (FromSingChainwebVersion (SChainwebVersion :: Sing v)) +-- = client (cutGetApi @v) . Just -- -------------------------------------------------------------------------- -- -- PUT Cut Client diff --git a/src/Chainweb/CutDB/RestAPI/Server.hs b/src/Chainweb/CutDB/RestAPI/Server.hs index 2167fafd9e..81d3297bcd 100644 --- a/src/Chainweb/CutDB/RestAPI/Server.hs +++ b/src/Chainweb/CutDB/RestAPI/Server.hs @@ -103,8 +103,8 @@ cutGetServer (CutDbT db) = liftIO . cutGetHandler db -- Some Cut Server someCutServerT :: PeerDb -> SomeCutDb tbl -> SomeServer -someCutServerT pdb (SomeCutDb (db :: CutDbT tbl v)) = - SomeServer (Proxy @(CutApi v)) (cutServer pdb db) +someCutServerT pdb (SomeCutDb (db :: CutDbT tbl v)) = undefined + -- SomeServer (Proxy @(CutApi v)) (cutServer pdb db) someCutServer :: ChainwebVersion -> PeerDb -> CutDb tbl -> SomeServer someCutServer v pdb = someCutServerT pdb . someCutDbVal v diff --git a/src/Chainweb/CutDB/Sync.hs b/src/Chainweb/CutDB/Sync.hs index a7af7bc2c6..4d4cc77360 100644 --- a/src/Chainweb/CutDB/Sync.hs +++ b/src/Chainweb/CutDB/Sync.hs @@ -67,7 +67,7 @@ getCut :: CutClientEnv -> CutHeight -> IO CutHashes -getCut (CutClientEnv v env) h = runClientThrowM (cutGetClientLimit v (int h)) env +getCut (CutClientEnv v env) h = undefined -- runClientThrowM (cutGetClientLimit v (int h)) env -- -------------------------------------------------------------------------- -- -- Sync Session diff --git a/src/Chainweb/Miner/Coordinator.hs b/src/Chainweb/Miner/Coordinator.hs index 084e3d3aac..3f29712078 100644 --- a/src/Chainweb/Miner/Coordinator.hs +++ b/src/Chainweb/Miner/Coordinator.hs @@ -123,6 +123,9 @@ data MiningCoordination logger tbl = MiningCoordination , _coordPrimedWork :: !(TVar PrimedWork) } +instance HasChainwebVersion (MiningCoordination logger tbl) where + _chainwebVersion = _chainwebVersion . _coordCutDb + -- | Precached payloads for Private Miners. This allows new work requests to be -- made as often as desired, without clogging the Pact queue. -- diff --git a/src/Chainweb/Miner/Core.hs b/src/Chainweb/Miner/Core.hs index dd1e726c9f..8ae142de35 100644 --- a/src/Chainweb/Miner/Core.hs +++ b/src/Chainweb/Miner/Core.hs @@ -46,6 +46,7 @@ import Chainweb.Difficulty import Chainweb.Time hiding (second) import Chainweb.Utils import Chainweb.Utils.Serialization +import Chainweb.Version --- @@ -94,10 +95,11 @@ timestampPosition = 8 mine :: forall a . HashAlgorithm a - => Nonce + => ChainwebVersion + -> Nonce -> WorkHeader -> IO SolvedWork -mine orig work = do +mine cwv orig work = do when (bufSize < noncePosition + sizeOf (0 :: Word64)) $ error "Chainweb.Miner.Core.mine: Buffer is too small to receive the nonce" BA.withByteArray tbytes $ \trgPtr -> do @@ -135,7 +137,7 @@ mine orig work = do -- Start outer mining loop t <- getCurrentTimeIntegral go0 100000 t orig - runGetS decodeSolvedWork new + runGetS (decodeSolvedWork cwv) new where tbytes = runPutS $ encodeHashTarget (_workHeaderTarget work) hbytes = BS.fromShort $ _workHeaderBytes work diff --git a/src/Chainweb/Miner/Miners.hs b/src/Chainweb/Miner/Miners.hs index 1abed1d328..98bfe0039d 100644 --- a/src/Chainweb/Miner/Miners.hs +++ b/src/Chainweb/Miner/Miners.hs @@ -106,7 +106,7 @@ localTest lf v coord m cdb gen miners = go :: BlockHeight -> WorkHeader -> IO SolvedWork go height w = do MWC.geometric1 t gen >>= threadDelay - runGetS decodeSolvedWork $ BS.fromShort $ _workHeaderBytes w + runGetS (decodeSolvedWork (_chainwebVersion cdb)) $ BS.fromShort $ _workHeaderBytes w where t :: Double t = int graphOrder / (int (_minerCount miners) * meanBlockTime * 1_000_000) @@ -148,4 +148,4 @@ localPOW lf coord m cdb = runForever lf "Chainweb.Miner.Miners.localPOW" $ do void $ awaitNewCut cdb c where go :: WorkHeader -> IO SolvedWork - go = mine @Blake2s_256 (Nonce 0) + go = mine @Blake2s_256 (_chainwebVersion cdb) (Nonce 0) diff --git a/src/Chainweb/Miner/RestAPI/Server.hs b/src/Chainweb/Miner/RestAPI/Server.hs index ee14a92cbe..ae3bbb23de 100644 --- a/src/Chainweb/Miner/RestAPI/Server.hs +++ b/src/Chainweb/Miner/RestAPI/Server.hs @@ -95,7 +95,7 @@ solvedHandler -> HeaderBytes -> Handler NoContent solvedHandler mr (HeaderBytes bytes) = do - liftIO (try $ runGetS decodeSolvedWork bytes) >>= \case + liftIO (try $ runGetS (decodeSolvedWork (_chainwebVersion mr)) bytes) >>= \case Left (DecodeException e) -> throwError $ setErrText ("Decoding error: " <> e) err400 Left _ -> diff --git a/src/Chainweb/Payload.hs b/src/Chainweb/Payload.hs index 095eee8b8c..c72101d5c4 100644 --- a/src/Chainweb/Payload.hs +++ b/src/Chainweb/Payload.hs @@ -118,6 +118,7 @@ import qualified Data.Aeson.Types as A import qualified Data.ByteArray as BA import qualified Data.ByteString as B import qualified Data.ByteString.Lazy as BL +import Data.Functor.Identity import Data.Hashable import Data.MerkleLog import qualified Data.Text as T @@ -388,7 +389,7 @@ instance IsCasValue (BlockPayload_ a) where type CasKeyType (BlockPayload_ a) = BlockPayloadHash_ a casKey = _blockPayloadPayloadHash -instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockPayload_ a) where +instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockPayload_ a) Identity where type MerkleLogHeader (BlockPayload_ a) = '[BlockTransactionsHash_ a, BlockOutputsHash_ a] type MerkleLogBody (BlockPayload_ a) = Void @@ -399,7 +400,7 @@ instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockPayload_ :+: _blockPayloadOutputsHash a :+: emptyBody - fromLog l = BlockPayload + fromLog l = Identity $ BlockPayload { _blockPayloadPayloadHash = BlockPayloadHash $ MerkleLogHash $ _merkleLogRoot l , _blockPayloadTransactionsHash = txHash , _blockPayloadOutputsHash = outHash @@ -503,7 +504,7 @@ instance IsCasValue (BlockTransactions_ a) where type CasKeyType (BlockTransactions_ a) = BlockTransactionsHash_ a casKey = _blockTransactionsHash -instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockTransactions_ a) where +instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockTransactions_ a) Identity where type MerkleLogHeader (BlockTransactions_ a) = '[MinerData] type MerkleLogBody (BlockTransactions_ a) = Transaction @@ -512,7 +513,7 @@ instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockTransacti BlockTransactionsHash (MerkleLogHash (!root)) = _blockTransactionsHash a !entries = _blockMinerData a :+: MerkleLogBody (_blockTransactions a) - fromLog l = BlockTransactions + fromLog l = Identity $ BlockTransactions { _blockTransactionsHash = BlockTransactionsHash $! MerkleLogHash $! _merkleLogRoot l , _blockTransactions = txs , _blockMinerData = mi @@ -648,7 +649,7 @@ instance IsCasValue (BlockOutputs_ a) where type CasKeyType (BlockOutputs_ a) = BlockOutputsHash_ a casKey = _blockOutputsHash -instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockOutputs_ a) where +instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockOutputs_ a) Identity where type MerkleLogHeader (BlockOutputs_ a) = '[CoinbaseOutput] type MerkleLogBody (BlockOutputs_ a) = TransactionOutput @@ -657,7 +658,7 @@ instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockOutputs_ BlockOutputsHash (MerkleLogHash (!root)) = _blockOutputsHash a !entries = _blockCoinbaseOutput a :+: MerkleLogBody (_blockOutputs a) - fromLog l = BlockOutputs + fromLog l = Identity $ BlockOutputs { _blockOutputsHash = BlockOutputsHash $! MerkleLogHash $! _merkleLogRoot l , _blockOutputs = outs , _blockCoinbaseOutput = co @@ -809,7 +810,7 @@ newBlockTransactions newBlockTransactions mi txs = (tree, blockTxs) where mlog = newTransactionLog mi txs - blockTxs = fromLog mlog + blockTxs = runIdentity $ fromLog mlog tree = TransactionTree { _transactionTreeHash = _blockTransactionsHash blockTxs , _transactionTree = _merkleLogTree mlog @@ -853,7 +854,7 @@ newBlockOutputs newBlockOutputs co outs = (tree, blkOuts) where mlog = newBlockOutputLog co outs - blkOuts = fromLog mlog + blkOuts = runIdentity $ fromLog mlog tree = OutputTree { _outputTreeHash = _blockOutputsHash blkOuts , _outputTree = _merkleLogTree mlog @@ -887,7 +888,7 @@ blockPayload -> BlockOutputs_ a -> BlockPayload_ a blockPayload txs outs - = fromLog $! newMerkleLog @a @ChainwebHashTag + = runIdentity $ fromLog $! newMerkleLog @a @ChainwebHashTag $ _blockTransactionsHash txs :+: _blockOutputsHash outs :+: emptyBody @@ -994,12 +995,12 @@ verifyPayloadData p where -- forces the transactions Merkle Tree txs :: BlockTransactions_ a - txs = fromLog @a $ newTransactionLog + txs = runIdentity $ fromLog @a $ newTransactionLog (_payloadDataMiner p) (_payloadDataTransactions p) -- forces the BlockPayload Merkle Tree - bp = fromLog @a $ newMerkleLog + bp = runIdentity $ fromLog @a $ newMerkleLog $ _payloadDataTransactionsHash p :+: _payloadDataOutputsHash p :+: emptyBody diff --git a/src/Chainweb/SPV/EventProof.hs b/src/Chainweb/SPV/EventProof.hs index ef25e9b65f..c154c417b7 100644 --- a/src/Chainweb/SPV/EventProof.hs +++ b/src/Chainweb/SPV/EventProof.hs @@ -112,6 +112,7 @@ import qualified Data.ByteString.Base16 as B16 import qualified Data.ByteString.Short as BS import Data.Decimal import Data.Foldable +import Data.Functor.Identity import Data.Hashable import Data.MerkleLog hiding (Actual, Expected) import qualified Data.Text as T @@ -473,7 +474,7 @@ data BlockEvents_ a = BlockEvents } deriving (Show, Eq, Generic) -instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockEvents_ a) where +instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockEvents_ a) Identity where type MerkleLogHeader (BlockEvents_ a) = '[] type MerkleLogBody (BlockEvents_ a) = OutputEvents toLog a = merkleLog root entries @@ -481,12 +482,12 @@ instance MerkleHashAlgorithm a => HasMerkleLog a ChainwebHashTag (BlockEvents_ a BlockEventsHash (MerkleLogHash !root) = _blockEventsHash a !entries = MerkleLogBody (_blockEventsEvents a) - fromLog l = BlockEvents + fromLog l = Identity $ BlockEvents { _blockEventsHash = BlockEventsHash $! MerkleLogHash $! _merkleLogRoot l , _blockEventsEvents = es } where - (MerkleLogBody es) = _merkleLogEntries l + MerkleLogBody es = _merkleLogEntries l -- | Smart Constructor for 'BlockEvents' -- @@ -495,7 +496,7 @@ blockEvents . MerkleHashAlgorithm a => V.Vector OutputEvents -> BlockEvents_ a -blockEvents = fromLog . newMerkleLog @a . MerkleLogBody +blockEvents = runIdentity . fromLog . newMerkleLog @a . MerkleLogBody type BlockEventsLog a = MkLogType a ChainwebHashTag (BlockEvents_ a) diff --git a/src/Chainweb/TreeDB/RemoteDB.hs b/src/Chainweb/TreeDB/RemoteDB.hs index 2eefecfc84..268c3f65b2 100644 --- a/src/Chainweb/TreeDB/RemoteDB.hs +++ b/src/Chainweb/TreeDB/RemoteDB.hs @@ -66,35 +66,35 @@ instance TreeDb RemoteDb where lookup (RemoteDb env alog ver cid) k = hush <$> runClientM client env where client = logServantError alog "failed to query tree db entry" - $ headerClient ver cid k + $ undefined -- headerClient ver cid k keys (RemoteDb env alog ver cid) next limit minr maxr f = f $ callAndPage client next 0 env where client :: Maybe (NextItem BlockHash) -> ClientM (Page (NextItem BlockHash) BlockHash) client nxt = logServantError alog "failed to query tree db keys" - $ hashesClient ver cid limit nxt minr maxr + $ undefined -- hashesClient ver cid limit nxt minr maxr entries (RemoteDb env alog ver cid) next limit minr maxr f = f $ callAndPage client next 0 env where client :: Maybe (NextItem BlockHash) -> ClientM (Page (NextItem BlockHash) BlockHeader) client nxt = logServantError alog "failed to query tree db entries" - $ headersClient ver cid limit nxt minr maxr + $ undefined -- headersClient ver cid limit nxt minr maxr branchKeys (RemoteDb env alog ver cid) next limit minr maxr lower upper f = f $ callAndPage client next 0 env where client :: Maybe (NextItem BlockHash) -> ClientM (Page (NextItem BlockHash) BlockHash) client nxt = logServantError alog "failed to query remote branch keys" - $ branchHashesClient ver cid limit nxt minr maxr (BranchBounds lower upper) + $ undefined -- branchHashesClient ver cid limit nxt minr maxr (BranchBounds lower upper) branchEntries (RemoteDb env alog ver cid) next limit minr maxr lower upper f = f $ callAndPage client next 0 env where client :: Maybe (NextItem BlockHash) -> ClientM (Page (NextItem BlockHash) BlockHeader) client nxt = logServantError alog "failed to query remote branch entries" - $ branchHeadersClient ver cid limit nxt minr maxr (BranchBounds lower upper) + $ undefined -- branchHeadersClient ver cid limit nxt minr maxr (BranchBounds lower upper) -- We could either use the cut or create a new API -- maxEntry (RemoteDb env alog ver cid) e = diff --git a/src/Chainweb/Version/Registry.hs b/src/Chainweb/Version/Registry.hs index f85ec99e77..f0633dcc09 100644 --- a/src/Chainweb/Version/Registry.hs +++ b/src/Chainweb/Version/Registry.hs @@ -15,7 +15,7 @@ -- module Chainweb.Version.Registry ( registerVersion - , lookupVersionByCode + -- , lookupVersionByCode , lookupVersionByName , fabricateVersionWithName , knownVersions @@ -89,27 +89,27 @@ validateVersion v = do unless (null errors) $ error $ unlines $ ["errors encountered validating version " <> show v <> ":"] <> errors --- | Look up a version in the registry by code. -lookupVersionByCode :: HasCallStack => ChainwebVersionCode -> ChainwebVersion -lookupVersionByCode code - -- these two cases exist to ensure that the mainnet and testnet versions - -- cannot be accidentally replaced and are the most performant to look up. - -- registering them is still allowed, as long as they are not conflicting. - | code == _versionCode mainnet = mainnet - | code == _versionCode testnet = testnet - | otherwise = - -- Setting the version code here allows us to delay doing the lookup in - -- the case that we don't actually need the version, just the code. - lookupVersion & versionCode .~ code - where - lookupVersion = unsafeDupablePerformIO $ do - m <- readIORef versionMap - return $ fromMaybe (error notRegistered) $ - HM.lookup code m - notRegistered - | code == _versionCode devnet = "devnet version used but not registered, remember to do so after it's configured" - | code == _versionCode fastDevnet = "fastDevnet version used but not registered, remember to do so after it's configured" - | otherwise = "version not registered with code " <> show code <> ", have you seen Chainweb.Test.TestVersions.legalizeTestVersion?" +-- -- | Look up a version in the registry by code. +-- lookupVersionByCode :: HasCallStack => ChainwebVersionCode -> ChainwebVersion +-- lookupVersionByCode code +-- -- these two cases exist to ensure that the mainnet and testnet versions +-- -- cannot be accidentally replaced and are the most performant to look up. +-- -- registering them is still allowed, as long as they are not conflicting. +-- | code == _versionCode mainnet = mainnet +-- | code == _versionCode testnet = testnet +-- | otherwise = +-- -- Setting the version code here allows us to delay doing the lookup in +-- -- the case that we don't actually need the version, just the code. +-- lookupVersion & versionCode .~ code +-- where +-- lookupVersion = unsafeDupablePerformIO $ do +-- m <- readIORef versionMap +-- return $ fromMaybe (error notRegistered) $ +-- HM.lookup code m +-- notRegistered +-- | code == _versionCode devnet = "devnet version used but not registered, remember to do so after it's configured" +-- | code == _versionCode fastDevnet = "fastDevnet version used but not registered, remember to do so after it's configured" +-- | otherwise = "version not registered with code " <> show code <> ", have you seen Chainweb.Test.TestVersions.legalizeTestVersion?" -- TODO: ideally all uses of this are deprecated. currently in use in -- ObjectEncoded block header decoder and CutHashes decoder. @@ -143,5 +143,5 @@ findKnownVersion vn = Nothing -> fail $ T.unpack (getChainwebVersionName vn) <> " is not a known version: try development, mainnet01 or testnet04" Just v -> return v -instance HasChainwebVersion ChainwebVersionCode where - _chainwebVersion = lookupVersionByCode \ No newline at end of file +-- instance HasChainwebVersion ChainwebVersionCode where +-- _chainwebVersion = lookupVersionByCode \ No newline at end of file diff --git a/test/Chainweb/Test/Cut.hs b/test/Chainweb/Test/Cut.hs index c9262bcebe..8df7853dec 100644 --- a/test/Chainweb/Test/Cut.hs +++ b/test/Chainweb/Test/Cut.hs @@ -145,13 +145,13 @@ arbitraryBlockTimeOffset lower upper = do -- | Solve Work. Doesn't check that the nonce and the time are valid. -- -solveWork :: HasCallStack => WorkHeader -> Nonce -> Time Micros -> SolvedWork -solveWork w n t = - case runGetS decodeBlockHeaderWithoutHash $ BS.fromShort $ _workHeaderBytes w of - Nothing -> error "Chainwb.Test.Cut.solveWork: Invalid work header bytes" +solveWork :: ChainwebVersion -> HasCallStack => WorkHeader -> Nonce -> Time Micros -> SolvedWork +solveWork v w n t = + case runGetS (decodeBlockHeaderWithoutHash v) $ BS.fromShort $ _workHeaderBytes w of + Nothing -> error "Chainweb.Test.Cut.solveWork: Invalid work header bytes" Just hdr -> SolvedWork $ fromJuste - $ runGetS decodeBlockHeaderWithoutHash + $ runGetS (decodeBlockHeaderWithoutHash v) $ runPutS $ encodeBlockHeaderWithoutHash -- After injecting the nonce and the creation time will have to do a @@ -207,7 +207,7 @@ testMine' -> Cut -> IO (Either MineFailure (T2 BlockHeader Cut)) testMine' wdb n t payloadHash i c = - try (createNewCut (chainLookupM wdb) n (t c (_chainId i)) payloadHash i c) >>= \case + try (createNewCut (_chainwebVersion wdb) (chainLookupM wdb) n (t c (_chainId i)) payloadHash i c) >>= \case Right p@(T2 h _) -> Right p <$ insertWebBlockHeaderDb wdb h e -> return e @@ -215,15 +215,16 @@ testMineWithPayloadHash :: forall cid hdb . HasChainId cid => ChainValueCasLookup hdb BlockHeader - => hdb + => ChainwebVersion + -> hdb -> Nonce -> Time Micros -> BlockPayloadHash -> cid -> Cut -> IO (Either MineFailure (T2 BlockHeader Cut)) -testMineWithPayloadHash db n t ph cid c = try - $ createNewCut (chainLookupM db) n t ph cid c +testMineWithPayloadHash v db n t ph cid c = try + $ createNewCut v (chainLookupM db) n t ph cid c -- | Create a new block. Only produces a new cut but doesn't insert it into the -- chain database. @@ -234,17 +235,18 @@ createNewCut :: HasCallStack => MonadCatch m => HasChainId cid - => (ChainValue BlockHash -> m BlockHeader) + => ChainwebVersion + -> (ChainValue BlockHash -> m BlockHeader) -> Nonce -> Time Micros -> BlockPayloadHash -> cid -> Cut -> m (T2 BlockHeader Cut) -createNewCut hdb n t pay i c = do +createNewCut v hdb n t pay i c = do extension <- fromMaybeM BadAdjacents $ getCutExtension c i - work <- newWorkHeaderPure hdb (BlockCreationTime t) extension pay - (h, mc') <- extendCut c pay (solveWork work n t) + work <- newWorkHeaderPure v hdb (BlockCreationTime t) extension pay + (h, mc') <- extendCut c pay (solveWork v work n t) `catch` \(InvalidSolvedHeader _ msg) -> throwM $ InvalidHeader msg c' <- fromMaybeM BadAdjacents mc' return $ T2 h c' @@ -257,14 +259,15 @@ createNewCut1Second . HasCallStack => MonadCatch m => HasChainId cid - => (ChainValue BlockHash -> m BlockHeader) + => ChainwebVersion + -> (ChainValue BlockHash -> m BlockHeader) -> Nonce -> BlockPayloadHash -> cid -> Cut -> m (T2 BlockHeader Cut) -createNewCut1Second db n p i c - = createNewCut db n (offsetBlockTime Time.second c (_chainId i)) p i c +createNewCut1Second v db n p i c + = createNewCut v db n (offsetBlockTime Time.second c (_chainId i)) p i c -- -------------------------------------------------------------------------- -- -- Arbitrary Cuts @@ -297,7 +300,7 @@ arbitraryCut v = T.sized $ \s -> do n <- Nonce <$> T.arbitrary let pay = _payloadWithOutputsPayloadHash $ testPayload $ B8.intercalate "," [ sshow v, sshow cid, "TEST PAYLOAD"] - case try (createNewCut1Second (testLookup db) n pay cid c) of + case try (createNewCut1Second v (testLookup db) n pay cid c) of Left e -> throw e Right (Left BadAdjacents) -> return Nothing Right (Left e) -> throw e diff --git a/test/Chainweb/Test/CutDB.hs b/test/Chainweb/Test/CutDB.hs index 491a0bbf0d..cae7e8c118 100644 --- a/test/Chainweb/Test/CutDB.hs +++ b/test/Chainweb/Test/CutDB.hs @@ -406,7 +406,7 @@ tryMineForChain miner webPact cutDb c cid = do outputs <- _webPactNewBlock webPact miner parent let payloadHash = _payloadWithOutputsPayloadHash outputs t <- getCurrentTimeIntegral - x <- testMineWithPayloadHash wdb (Nonce 0) t payloadHash cid c + x <- testMineWithPayloadHash (_chainwebVersion cutDb) wdb (Nonce 0) t payloadHash cid c case x of Right (T2 h c') -> do addCutHashes cutDb (cutToCutHashes Nothing c') diff --git a/test/Chainweb/Test/Mempool/Consensus.hs b/test/Chainweb/Test/Mempool/Consensus.hs index 644ccc0bdd..a74e72c32f 100644 --- a/test/Chainweb/Test/Mempool/Consensus.hs +++ b/test/Chainweb/Test/Mempool/Consensus.hs @@ -10,6 +10,7 @@ module Chainweb.Test.Mempool.Consensus import Control.Monad.IO.Class import Control.Monad.Trans.Resource +import Control.Monad.Reader import qualified Data.ByteString.Short as SB import Data.Foldable (toList) @@ -46,6 +47,7 @@ import Chainweb.Mempool.Consensus import Chainweb.Mempool.Mempool import Chainweb.MerkleUniverse import Chainweb.Payload +import Chainweb.Utils import Chainweb.Test.Orphans.Time () import Chainweb.Test.Utils import Chainweb.Test.Utils.BlockHeader @@ -230,7 +232,7 @@ genTree -> PropertyM IO (Tree BlockTrans) genTree db mapRef h allTxs = do (takenNow, theRest) <- takeTrans allTxs - next <- header' h + next <- header' (_chainwebVersion db) h liftIO $ unsafeInsertBlockHeaderDb db next listOfOne <- preForkTrunk db mapRef next theRest newNode mapRef @@ -262,7 +264,7 @@ preForkTrunk -> HashSet TransactionHash -> PropertyM IO (Forest BlockTrans) preForkTrunk db mapRef h avail = do - next <- header' h + next <- header' (_chainwebVersion db) h liftIO $ unsafeInsertBlockHeaderDb db next (takenNow, theRest) <- takeTrans avail children <- frequencyM @@ -290,9 +292,9 @@ fork -> HashSet TransactionHash -> PropertyM IO (Forest BlockTrans) fork db mapRef h avail = do - nextLeft <- header' h + nextLeft <- header' (_chainwebVersion db) h liftIO $ unsafeInsertBlockHeaderDb db nextLeft - nextRight <- header' h + nextRight <- header' (_chainwebVersion db) h liftIO $ unsafeInsertBlockHeaderDb db nextRight (takenNow, theRest) <- takeTrans avail @@ -321,7 +323,7 @@ postForkTrunk -> Int -> PropertyM IO (Forest BlockTrans) postForkTrunk db mapRef h avail count = do - next <- header' h + next <- header' (_chainwebVersion db) h (takenNow, theRest) <- takeTrans avail children <- if count == 0 then return [] @@ -335,12 +337,14 @@ postForkTrunk db mapRef h avail count = do ---------------------------------------------------------------------------------------------------- -- TODO: does this test really has to go that low-level? Let try to refactor it use --- existing functionlity for creating a test block chain. +-- existing functionality for creating a test block chain. -- -header' :: BlockHeader -> PropertyM IO BlockHeader -header' h = do +header' :: ChainwebVersion -> BlockHeader -> PropertyM IO BlockHeader +header' v h = do nonce <- Nonce <$> pick chooseAny return + . fromJuste + . flip runReaderT v . fromLog @ChainwebMerkleHashAlgorithm . newMerkleLog $ mkFeatureFlags @@ -358,7 +362,6 @@ header' h = do where BlockCreationTime t = _blockCreationTime h target = powTarget (ParentHeader h) mempty t' - v = _chainwebVersion h t' = BlockCreationTime (scaleTimeSpan (10 :: Int) second `add` t) ---------------------------------------------------------------------------------------------------- diff --git a/test/Chainweb/Test/Orphans/Internal.hs b/test/Chainweb/Test/Orphans/Internal.hs index 399e0f194a..43ad289dca 100644 --- a/test/Chainweb/Test/Orphans/Internal.hs +++ b/test/Chainweb/Test/Orphans/Internal.hs @@ -2,6 +2,8 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DerivingVia #-} +{-# LANGUAGE DeriveFoldable #-} +{-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} @@ -9,6 +11,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} +{-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} @@ -62,6 +65,7 @@ module Chainweb.Test.Orphans.Internal import Control.Applicative import Control.Monad import Control.Monad.Catch +import Control.Monad.Reader import Crypto.Hash.Algorithms @@ -360,7 +364,7 @@ arbitraryBlockHeaderVersionHeightChain arbitraryBlockHeaderVersionHeightChain v h cid | isWebChain (chainGraphAt v h) cid = do t <- chooseEnum (epoch, add (scaleTimeSpan @Int (365 * 200) day) epoch) - fromLog @ChainwebMerkleHashAlgorithm . newMerkleLog <$> entries t + fromJuste . flip runReaderT v . fromLog @ChainwebMerkleHashAlgorithm . newMerkleLog <$> entries t | otherwise = discard where entries t @@ -490,10 +494,10 @@ arbitraryPayloadMerkleTree = oneof ] arbitraryMerkleTree - :: forall a b + :: forall a b m . MerkleHashAlgorithm a => Arbitrary b - => HasMerkleLog a ChainwebHashTag b + => HasMerkleLog a ChainwebHashTag b m => Gen (MerkleTree a) arbitraryMerkleTree = _merkleLogTree <$> (toLog @a <$> arbitrary @b) @@ -508,8 +512,8 @@ instance Arbitrary MerkleRootType where ] arbitraryMerkleProof - :: forall a b h t - . HasMerkleLog a ChainwebHashTag b + :: forall a b h t m + . HasMerkleLog a ChainwebHashTag b m => Arbitrary b => MerkleHashAlgorithm a => MerkleLogHeader b ~ (h ': t) -- TODO: drop this constraint? @@ -531,8 +535,8 @@ arbitraryMerkleProof = do in (b, hs, bs) <$ guard (bs + hs > 0) arbitraryMerkleBodyProof - :: forall a b - . HasMerkleLog a ChainwebHashTag b + :: forall a b m + . HasMerkleLog a ChainwebHashTag b m => Arbitrary b => MerkleHashAlgorithm a => Gen (MerkleProof a) @@ -548,8 +552,8 @@ arbitraryMerkleBodyProof = do in (b, s) <$ guard (s > 0) arbitraryMerkleHeaderProof - :: forall a b h t - . HasMerkleLog a ChainwebHashTag b + :: forall a b h t m + . HasMerkleLog a ChainwebHashTag b m => Arbitrary b => MerkleHashAlgorithm a => MerkleLogHeader b ~ (h ': t) @@ -565,8 +569,8 @@ arbitraryMerkleHeaderProof = do -- details. Proposals for making this nicer are welcome. -- mkHeaderProof - :: forall a b h t m - . HasMerkleLog a ChainwebHashTag b + :: forall a b h t m m' + . HasMerkleLog a ChainwebHashTag b m' => MonadThrow m => MerkleHashAlgorithm a => MerkleLogHeader b ~ (h ': t) diff --git a/test/Chainweb/Test/Pact/PactReplay.hs b/test/Chainweb/Test/Pact/PactReplay.hs index 7187edf6af..e7d2722a05 100644 --- a/test/Chainweb/Test/Pact/PactReplay.hs +++ b/test/Chainweb/Test/Pact/PactReplay.hs @@ -44,7 +44,7 @@ import Chainweb.Test.Utils import Chainweb.Test.TestVersions import Chainweb.Time import Chainweb.TreeDB -import Chainweb.Utils (sshow, tryAllSynchronous, catchAllSynchronous, T3(..)) +import Chainweb.Utils (fromJuste, sshow, tryAllSynchronous, catchAllSynchronous, T3(..)) import Chainweb.Version import Chainweb.Version.Utils @@ -321,12 +321,16 @@ mineBlock ph nonce iop = timeout 5000000 go >>= \case mv <- r >>= newBlock noMiner ph payload <- assertNotLeft =<< takeMVar mv - let bh = newBlockHeader - mempty - (_payloadWithOutputsPayloadHash payload) - nonce - creationTime - ph + let + bh = + fromJuste + $ newBlockHeader + testVer + mempty + (_payloadWithOutputsPayloadHash payload) + nonce + creationTime + ph mv' <- r >>= validateBlock bh (payloadWithOutputsToPayloadData payload) void $ assertNotLeft =<< takeMVar mv' diff --git a/test/Chainweb/Test/Pact/PactSingleChainTest.hs b/test/Chainweb/Test/Pact/PactSingleChainTest.hs index c6f3cf3c89..e731d58e61 100644 --- a/test/Chainweb/Test/Pact/PactSingleChainTest.hs +++ b/test/Chainweb/Test/Pact/PactSingleChainTest.hs @@ -310,12 +310,14 @@ blockGasLimitTest _ reqIO = testCase "blockGasLimitTest" $ do (CommandResult (RequestKey (Hash "h")) Nothing (PactResult $ Right $ pString "output") 0 Nothing Nothing Nothing []) payload = toPayloadWithOutputs noMiner block - bh = newBlockHeader - mempty - (_payloadWithOutputsPayloadHash payload) - (Nonce 0) - (BlockCreationTime $ Time $ TimeSpan 0) - (ParentHeader $ genesisBlockHeader testVersion cid) + bh = fromJuste $ + newBlockHeader + testVersion + mempty + (_payloadWithOutputsPayloadHash payload) + (Nonce 0) + (BlockCreationTime $ Time $ TimeSpan 0) + (ParentHeader $ genesisBlockHeader testVersion cid) validateBlock bh (payloadWithOutputsToPayloadData payload) q >>= takeMVar -- we consume slightly more than the maximum block gas limit and provoke an error. useGas 2_000_001 >>= \case diff --git a/test/Chainweb/Test/Pact/TTL.hs b/test/Chainweb/Test/Pact/TTL.hs index 3979e67f29..21b24b4b41 100644 --- a/test/Chainweb/Test/Pact/TTL.hs +++ b/test/Chainweb/Test/Pact/TTL.hs @@ -222,12 +222,16 @@ doNewBlock ctxIO mempool parent nonce t = do mv <- newBlock noMiner parent $ _ctxQueue ctx payload <- assertNotLeft =<< takeMVar mv - let bh = newBlockHeader - mempty - (_payloadWithOutputsPayloadHash payload) - nonce - creationTime - parent + let + bh = + fromJuste + $ newBlockHeader + testVer + mempty + (_payloadWithOutputsPayloadHash payload) + nonce + creationTime + parent -- no need for mining, since testVer uses a trivial target return $ T2 bh payload where diff --git a/test/Chainweb/Test/Pact/Utils.hs b/test/Chainweb/Test/Pact/Utils.hs index abe29dd80f..0fdb81cc37 100644 --- a/test/Chainweb/Test/Pact/Utils.hs +++ b/test/Chainweb/Test/Pact/Utils.hs @@ -860,7 +860,7 @@ someTestVersionHeader = someBlockHeader someTestVersion 10 someBlockHeader :: ChainwebVersion -> BlockHeight -> BlockHeader someBlockHeader v 0 = genesisBlockHeader v (unsafeChainId 0) someBlockHeader v h = (!! (int h - 1)) - $ testBlockHeaders + $ testBlockHeaders v $ ParentHeader $ genesisBlockHeader v (unsafeChainId 0) diff --git a/test/Chainweb/Test/RestAPI.hs b/test/Chainweb/Test/RestAPI.hs index c0ae9fd465..f34aaaa699 100644 --- a/test/Chainweb/Test/RestAPI.hs +++ b/test/Chainweb/Test/RestAPI.hs @@ -86,7 +86,7 @@ genesisBh db = head <$> headers db missingKey :: MonadIO m => BlockHeaderDb -> m (DbKey BlockHeaderDb) missingKey db = key . head - . testBlockHeadersWithNonce (Nonce 34523) + . testBlockHeadersWithNonce (_chainwebVersion db) (Nonce 34523) . ParentHeader <$> genesisBh db @@ -211,7 +211,7 @@ simpleClientSession envIO cid = (Actual gen1) void $ liftIO $ step "put 3 new blocks" - let newHeaders = take 3 $ testBlockHeaders (ParentHeader gbh0) + let newHeaders = take 3 $ testBlockHeaders version (ParentHeader gbh0) liftIO $ traverse_ (unsafeInsertBlockHeaderDb db) newHeaders void $ liftIO $ step "headersClient: get all 4 block headers" @@ -250,7 +250,7 @@ simpleClientSession envIO cid = $ branchHeadersClient version cid Nothing Nothing Nothing Nothing bounds let limit = 32 - let blockHeaders = testBlockHeaders (ParentHeader gbh0) + let blockHeaders = testBlockHeaders version (ParentHeader gbh0) let maxBlockHeaders = take limit blockHeaders let excessBlockHeaders = take (limit + 1) blockHeaders @@ -373,7 +373,7 @@ simpleClientSession envIO cid = -- branch hashes with fork void $ liftIO $ step "headerPutClient: put 3 new blocks on a new fork" - let newHeaders2 = take 3 $ testBlockHeadersWithNonce (Nonce 17) (ParentHeader gbh0) + let newHeaders2 = take 3 $ testBlockHeadersWithNonce version (Nonce 17) (ParentHeader gbh0) liftIO $ traverse_ (unsafeInsertBlockHeaderDb db) newHeaders2 let lower = last newHeaders diff --git a/test/Chainweb/Test/Roundtrips.hs b/test/Chainweb/Test/Roundtrips.hs index 696b24c870..24d63160ae 100644 --- a/test/Chainweb/Test/Roundtrips.hs +++ b/test/Chainweb/Test/Roundtrips.hs @@ -67,7 +67,7 @@ import Chainweb.RestAPI.NodeInfo import Chainweb.SPV import Chainweb.SPV.EventProof import Chainweb.SPV.PayloadProof -import Chainweb.Test.Orphans.Internal (EventPactValue(..), ProofPactEvent(..)) +import Chainweb.Test.Orphans.Internal (EventPactValue(..), ProofPactEvent(..), arbitraryBlockHeaderVersion) import Chainweb.Test.SPV.EventProof hiding (tests) import Chainweb.Test.Utils import Chainweb.Time @@ -201,8 +201,8 @@ encodeDecodeTests = testGroup "Encode-Decode roundtrips" ] -- Mining - , testProperty "SolvedWork" - $ prop_encodeDecode decodeSolvedWork encodeSolvedWork + , testProperty "SolvedWork" $ \v -> forAll (SolvedWork <$> arbitraryBlockHeaderVersion v) + $ prop_encodeDecode (decodeSolvedWork v) encodeSolvedWork -- FIXME: decoding depends on version and block height (which is something -- that we should fix) diff --git a/test/Chainweb/Test/TreeDB.hs b/test/Chainweb/Test/TreeDB.hs index 6bddb978bc..37872a55da 100644 --- a/test/Chainweb/Test/TreeDB.hs +++ b/test/Chainweb/Test/TreeDB.hs @@ -320,7 +320,7 @@ prop_forkEntry f i j = do a = take (int i) $ branch (Nonce 0) g b = take (int j) $ branch (Nonce 1) g - branch n x = view (from isoBH) <$> testBlockHeadersWithNonce n (ParentHeader $ view isoBH x) + branch n x = view (from isoBH) <$> testBlockHeadersWithNonce toyVersion n (ParentHeader $ view isoBH x) -- -------------------------------------------------------------------------- -- -- forward branch entries diff --git a/test/Chainweb/Test/Utils.hs b/test/Chainweb/Test/Utils.hs index 2a20a4a72a..2330379b97 100644 --- a/test/Chainweb/Test/Utils.hs +++ b/test/Chainweb/Test/Utils.hs @@ -137,6 +137,7 @@ import Control.Lens import Control.Monad import Control.Monad.Catch (finally, bracket) import Control.Monad.IO.Class +import Control.Monad.Reader import Control.Monad.Trans.Resource import Control.Retry @@ -381,7 +382,7 @@ genesisBlockHeaderForChain v i insertN :: Int -> BlockHeader -> BlockHeaderDb -> IO () insertN n g db = traverse_ (unsafeInsertBlockHeaderDb db) bhs where - bhs = take n $ testBlockHeaders $ ParentHeader g + bhs = take n $ testBlockHeaders (_chainwebVersion db) $ ParentHeader g -- | Payload hashes are generated using 'testBlockPayloadFromParent_', which -- includes the nonce. They payloads can be recovered using @@ -392,7 +393,7 @@ insertN_ s n g db = do traverse_ (unsafeInsertBlockHeaderDb db) bhs return bhs where - bhs = take (int n) $ testBlockHeadersWithNonce s $ ParentHeader g + bhs = take (int n) $ testBlockHeadersWithNonce (_chainwebVersion db) s $ ParentHeader g -- | Useful for terminal-based debugging. A @Tree BlockHeader@ can be obtained -- from any `TreeDb` via `toTree`. @@ -436,43 +437,45 @@ data Growth = Randomly | AtMost BlockHeight deriving (Eq, Ord, Show) tree :: ChainwebVersion -> Growth -> Gen (Tree BlockHeader) tree v g = do h <- genesis v - Node h <$> forest g h + Node h <$> forest v g h -- | Generate a sane, legal genesis block for 'Test' chainweb instance -- genesis :: ChainwebVersion -> Gen BlockHeader genesis v = either (error . sshow) return $ genesisBlockHeaderForChain v 0 -forest :: Growth -> BlockHeader -> Gen (Forest BlockHeader) -forest Randomly h = randomTrunk h -forest g@(AtMost n) h | n < _blockHeight h = pure [] - | otherwise = fixedTrunk g h +forest :: ChainwebVersion -> Growth -> BlockHeader -> Gen (Forest BlockHeader) +forest v Randomly h = randomTrunk v h +forest v g@(AtMost n) h | n < _blockHeight h = pure [] + | otherwise = fixedTrunk v g h -fixedTrunk :: Growth -> BlockHeader -> Gen (Forest BlockHeader) -fixedTrunk g h = frequency [ (1, sequenceA [fork h, trunk g h]) - , (5, sequenceA [trunk g h]) ] +fixedTrunk :: ChainwebVersion -> Growth -> BlockHeader -> Gen (Forest BlockHeader) +fixedTrunk v g h = frequency [ (1, sequenceA [fork v h, trunk v g h]) + , (5, sequenceA [trunk v g h]) ] -randomTrunk :: BlockHeader -> Gen (Forest BlockHeader) -randomTrunk h = frequency [ (2, pure []) - , (4, sequenceA [fork h, trunk Randomly h]) - , (18, sequenceA [trunk Randomly h]) ] +randomTrunk :: ChainwebVersion -> BlockHeader -> Gen (Forest BlockHeader) +randomTrunk v h = frequency [ (2, pure []) + , (4, sequenceA [fork v h, trunk v Randomly h]) + , (18, sequenceA [trunk v Randomly h]) ] -fork :: BlockHeader -> Gen (Tree BlockHeader) -fork h = do - next <- header h - Node next <$> frequency [ (1, pure []), (1, sequenceA [fork next]) ] +fork :: ChainwebVersion -> BlockHeader -> Gen (Tree BlockHeader) +fork v h = do + next <- header v h + Node next <$> frequency [ (1, pure []), (1, sequenceA [fork v next]) ] -trunk :: Growth -> BlockHeader -> Gen (Tree BlockHeader) -trunk g h = do - next <- header h - Node next <$> forest g next +trunk :: ChainwebVersion -> Growth -> BlockHeader -> Gen (Tree BlockHeader) +trunk v g h = do + next <- header v h + Node next <$> forest v g next -- | Generate some new `BlockHeader` based on a parent. -- -header :: BlockHeader -> Gen BlockHeader -header p = do +header :: ChainwebVersion -> BlockHeader -> Gen BlockHeader +header v p = do nonce <- Nonce <$> chooseAny return + . fromJuste + . flip runReaderT v . fromLog @ChainwebMerkleHashAlgorithm . newMerkleLog $ mkFeatureFlags @@ -524,7 +527,7 @@ linearBlockHeaderDbs n dbs = do where populateDb (_, db) = do gbh0 <- root db - traverse_ (unsafeInsertBlockHeaderDb db) . take (int n) . testBlockHeaders $ ParentHeader gbh0 + traverse_ (unsafeInsertBlockHeaderDb db) . take (int n) . testBlockHeaders (_chainwebVersion db) $ ParentHeader gbh0 starBlockHeaderDbs :: Natural @@ -536,9 +539,9 @@ starBlockHeaderDbs n dbs = do where populateDb (_, db) = do gbh0 <- root db - traverse_ (\i -> unsafeInsertBlockHeaderDb db . newEntry i $ ParentHeader gbh0) [0 .. (int n-1)] + traverse_ (\i -> unsafeInsertBlockHeaderDb db . newEntry db i $ ParentHeader gbh0) [0 .. (int n-1)] - newEntry i h = head $ testBlockHeadersWithNonce (Nonce i) h + newEntry db i h = head $ testBlockHeadersWithNonce (_chainwebVersion db) (Nonce i) h -- -------------------------------------------------------------------------- -- -- Tasty TestTree Server and Client Environment diff --git a/test/Chainweb/Test/Utils/BlockHeader.hs b/test/Chainweb/Test/Utils/BlockHeader.hs index ec93a8d6de..6587f2495b 100644 --- a/test/Chainweb/Test/Utils/BlockHeader.hs +++ b/test/Chainweb/Test/Utils/BlockHeader.hs @@ -128,7 +128,8 @@ testGetNewAdjacentParentHeaders v hdb = itraverse select . _getBlockHashRecord | otherwise = Right . ParentHeader <$> hdb (ChainValue cid h) testBlockHeader - :: HM.HashMap ChainId ParentHeader + :: ChainwebVersion + -> HM.HashMap ChainId ParentHeader -- ^ Adjacent parent hashes -> Nonce -- ^ Randomness to affect the block hash. It is also included into @@ -136,8 +137,8 @@ testBlockHeader -> ParentHeader -- ^ parent block header -> BlockHeader -testBlockHeader adj nonce p@(ParentHeader b) = - newBlockHeader adj payload nonce (BlockCreationTime $ add second t) p +testBlockHeader cwv adj nonce p@(ParentHeader b) = + fromJuste $ newBlockHeader cwv adj payload nonce (BlockCreationTime $ add second t) p where payload = _payloadWithOutputsPayloadHash $ testBlockPayloadFromParent_ nonce p BlockCreationTime t = _blockCreationTime b @@ -147,17 +148,17 @@ testBlockHeader adj nonce p@(ParentHeader b) = -- -- Should only be used for testing purposes. -- -testBlockHeaders :: ParentHeader -> [BlockHeader] -testBlockHeaders (ParentHeader p) = L.unfoldr (Just . (id &&& id) . f) p +testBlockHeaders :: ChainwebVersion -> ParentHeader -> [BlockHeader] +testBlockHeaders cwv (ParentHeader p) = L.unfoldr (Just . (id &&& id) . f) p where - f b = testBlockHeader mempty (_blockNonce b) $ ParentHeader b + f b = testBlockHeader cwv mempty (_blockNonce b) $ ParentHeader b -- | Given a `BlockHeader` of some initial parent, generate an infinite stream -- of `BlockHeader`s which form a legal chain. -- -- Should only be used for testing purposes. -- -testBlockHeadersWithNonce :: Nonce -> ParentHeader -> [BlockHeader] -testBlockHeadersWithNonce n (ParentHeader p) = L.unfoldr (Just . (id &&& id) . f) p +testBlockHeadersWithNonce :: ChainwebVersion -> Nonce -> ParentHeader -> [BlockHeader] +testBlockHeadersWithNonce cwv n (ParentHeader p) = L.unfoldr (Just . (id &&& id) . f) p where - f b = testBlockHeader mempty n $ ParentHeader b + f b = testBlockHeader cwv mempty n $ ParentHeader b diff --git a/test/Chainweb/Test/Utils/TestHeader.hs b/test/Chainweb/Test/Utils/TestHeader.hs index 59f11c3f51..9f5d9c14a8 100644 --- a/test/Chainweb/Test/Utils/TestHeader.hs +++ b/test/Chainweb/Test/Utils/TestHeader.hs @@ -58,6 +58,7 @@ import Chainweb.BlockHeader import Chainweb.BlockHeight import Chainweb.ChainValue import Chainweb.Test.Orphans.Internal +import Chainweb.Utils import Chainweb.Version import Chainweb.Storage.Table @@ -153,7 +154,7 @@ arbitraryTestHeaderHeight v cid h = do <$> HM.insert cid (_parentHeader parent) as t <- BlockCreationTime <$> chooseEnum (pt, maxBound) return $ TestHeader - { _testHeaderHdr = newBlockHeader (ParentHeader <$> as) payloadHash nonce t parent + { _testHeaderHdr = fromJuste $ newBlockHeader v (ParentHeader <$> as) payloadHash nonce t parent , _testHeaderParent = parent , _testHeaderAdjs = toList $ ParentHeader <$> as }