From a5c2448d886814c1819433ea1471e8f86c50b5d9 Mon Sep 17 00:00:00 2001 From: Edmund Noble Date: Tue, 3 Sep 2024 11:23:57 -0400 Subject: [PATCH] fix tests Change-Id: I4ce486756b840818bf55fe7c20b4495ed7c02d3d --- chainweb.cabal | 1 + src/Chainweb/Block.hs | 12 + src/Chainweb/BlockHeader.hs | 4 +- src/Chainweb/BlockHeader/Validation.hs | 2 +- src/Chainweb/BlockHeaderDB/RestAPI.hs | 8 +- src/Chainweb/BlockHeaderDB/RestAPI/Server.hs | 1 + src/Chainweb/ChainId.hs | 11 +- src/Chainweb/Chainweb/Configuration.hs | 6 +- .../Pact/Backend/ChainwebPactCoreDb.hs | 2 +- src/Chainweb/Pact/PactService.hs | 2 +- .../Pact/PactService/Pact5/ExecBlock.hs | 21 +- src/Chainweb/Pact/Types.hs | 2 +- src/Chainweb/Pact4/ModuleCache.hs | 2 +- src/Chainweb/Pact4/TransactionExec.hs | 2 +- src/Chainweb/Pact5/TransactionExec.hs | 10 +- src/Chainweb/Payload.hs | 2 +- src/Chainweb/Payload/PayloadStore.hs | 2 +- src/Chainweb/Rosetta/Internal.hs | 2 +- src/Chainweb/Version.hs | 6 +- src/Chainweb/Version/Guards.hs | 2 +- src/Chainweb/Version/Mainnet.hs | 2 +- src/Chainweb/Version/Testnet.hs | 2 +- src/Chainweb/Version/Utils.hs | 2 +- test/Chainweb/Test/BlockHeader/Genesis.hs | 22 +- test/Chainweb/Test/Cut/TestBlockDb.hs | 21 ++ test/Chainweb/Test/Pact5/PactServiceTest.hs | 214 +++++++++--------- .../Test/Pact5/TransactionExecTest.hs | 45 ---- test/Chainweb/Test/RestAPI.hs | 5 +- test/Chainweb/Test/RestAPI/Client_.hs | 2 +- test/Chainweb/Test/Rosetta/RestAPI.hs | 4 +- test/Chainweb/Test/Utils.hs | 5 + 31 files changed, 214 insertions(+), 210 deletions(-) create mode 100644 src/Chainweb/Block.hs diff --git a/chainweb.cabal b/chainweb.cabal index 466b69f150..41159be18a 100644 --- a/chainweb.cabal +++ b/chainweb.cabal @@ -133,6 +133,7 @@ library cc-options: -DSQLITE_CORE exposed-modules: Chainweb.Backup + , Chainweb.Block , Chainweb.BlockCreationTime , Chainweb.BlockHash , Chainweb.BlockHeader diff --git a/src/Chainweb/Block.hs b/src/Chainweb/Block.hs new file mode 100644 index 0000000000..56aefc7557 --- /dev/null +++ b/src/Chainweb/Block.hs @@ -0,0 +1,12 @@ +module Chainweb.Block + (Block(..)) + where + +import Chainweb.BlockHeader +import Chainweb.Payload + +data Block = Block + { _blockHeader :: !BlockHeader + , _blockPayloadWithOutputs :: !PayloadWithOutputs + } + deriving (Eq, Show) diff --git a/src/Chainweb/BlockHeader.hs b/src/Chainweb/BlockHeader.hs index c5ccd7a6b4..0ee3ea3199 100644 --- a/src/Chainweb/BlockHeader.hs +++ b/src/Chainweb/BlockHeader.hs @@ -685,7 +685,7 @@ makeGenesisBlockHeaders v = HM.fromList [ (cid, makeGenesisBlockHeader v cid) | makeGenesisBlockHeader :: ChainwebVersion -> ChainId -> BlockHeader makeGenesisBlockHeader v cid = - makeGenesisBlockHeader' v cid (_genesisTime (_versionGenesis v) ^?! onChain cid) (Nonce 0) + makeGenesisBlockHeader' v cid (_genesisTime (_versionGenesis v) ^?! atChain cid) (Nonce 0) -- this version does not rely on the genesis block headers, but just the version graphs genesisHeightSlow :: HasCallStack => ChainwebVersion -> ChainId -> BlockHeight @@ -717,7 +717,7 @@ makeGenesisBlockHeader' v p ct@(BlockCreationTime t) n = $ mkFeatureFlags :+: ct :+: genesisParentBlockHash v cid - :+: (v ^?! versionGenesis . genesisBlockTarget . onChain cid) + :+: (v ^?! versionGenesis . genesisBlockTarget . atChain cid) :+: genesisBlockPayloadHash v cid :+: cid :+: BlockWeight 0 diff --git a/src/Chainweb/BlockHeader/Validation.hs b/src/Chainweb/BlockHeader/Validation.hs index 87c4ebfc2a..5e38054322 100644 --- a/src/Chainweb/BlockHeader/Validation.hs +++ b/src/Chainweb/BlockHeader/Validation.hs @@ -693,7 +693,7 @@ prop_block_genesis_parent b prop_block_genesis_target :: BlockHeader -> Bool prop_block_genesis_target b = isGenesisBlockHeader b - ==> _blockTarget b == _chainwebVersion b ^?! versionGenesis . genesisBlockTarget . onChain (_chainId b) + ==> _blockTarget b == _chainwebVersion b ^?! versionGenesis . genesisBlockTarget . atChain (_chainId b) prop_block_current :: Time Micros -> BlockHeader -> Bool prop_block_current t b = BlockCreationTime t >= _blockCreationTime b diff --git a/src/Chainweb/BlockHeaderDB/RestAPI.hs b/src/Chainweb/BlockHeaderDB/RestAPI.hs index 9bfe9c351a..3892b62eec 100644 --- a/src/Chainweb/BlockHeaderDB/RestAPI.hs +++ b/src/Chainweb/BlockHeaderDB/RestAPI.hs @@ -49,7 +49,6 @@ module Chainweb.BlockHeaderDB.RestAPI -- * API types BlockHashPage , BlockHeaderPage -, Block(..) , BlockPage -- * Encodings @@ -101,6 +100,7 @@ import Network.HTTP.Media ((//), (/:)) import Servant.API -- internal modules +import Chainweb.Block import Chainweb.BlockHash import Chainweb.BlockHeader import Chainweb.BlockHeaderDB @@ -120,12 +120,6 @@ type BlockHashPage = Page (NextItem BlockHash) BlockHash type BlockHeaderPage = Page (NextItem BlockHash) BlockHeader -data Block = Block - { _blockHeader :: !BlockHeader - , _blockPayloadWithOutputs :: !PayloadWithOutputs - } - deriving (Eq, Show) - -- because this endpoint is only used on the service API, we assume clients -- want object-encoded block headers. blockProperties :: KeyValue e kv => Block -> [kv] diff --git a/src/Chainweb/BlockHeaderDB/RestAPI/Server.hs b/src/Chainweb/BlockHeaderDB/RestAPI/Server.hs index cfafe0112e..6705494115 100644 --- a/src/Chainweb/BlockHeaderDB/RestAPI/Server.hs +++ b/src/Chainweb/BlockHeaderDB/RestAPI/Server.hs @@ -76,6 +76,7 @@ import Chainweb.RestAPI.Utils import Chainweb.TreeDB import Chainweb.Utils.Paging import Chainweb.Version +import Chainweb.Block -- -------------------------------------------------------------------------- -- -- Handler Tools diff --git a/src/Chainweb/ChainId.hs b/src/Chainweb/ChainId.hs index 473ee6ec73..6f9839f7f9 100644 --- a/src/Chainweb/ChainId.hs +++ b/src/Chainweb/ChainId.hs @@ -59,8 +59,9 @@ module Chainweb.ChainId -- * Mapping from chain IDs to values , ChainMap(..) -, onChain +, atChain , onChains +, onChain , chainZip ) where @@ -276,6 +277,10 @@ data ChainMap a = AllChains a | OnChains (HashMap ChainId a) onChains :: [(ChainId, a)] -> ChainMap a onChains = OnChains . HM.fromList +-- | A smart constructor, @onChain c a = OnChains (HM.singleton c a)@. +onChain :: ChainId -> a -> ChainMap a +onChain c a = OnChains (HM.singleton c a) + -- | Zips two `ChainMap`s on their chain IDs. chainZip :: (a -> a -> a) -> ChainMap a -> ChainMap a -> ChainMap a chainZip f (OnChains l) (OnChains r) = OnChains $ HM.unionWith f l r @@ -296,7 +301,7 @@ instance FromJSON a => FromJSON (ChainMap a) where makePrisms ''ChainMap -- | Provides access to the value at a `ChainId`, if it exists. -onChain :: ChainId -> Fold (ChainMap a) a -onChain cid = folding $ \case +atChain :: ChainId -> Fold (ChainMap a) a +atChain cid = folding $ \case OnChains m -> m ^. at cid AllChains a -> Just a diff --git a/src/Chainweb/Chainweb/Configuration.hs b/src/Chainweb/Chainweb/Configuration.hs index 84df0ee18e..fc5eea89c0 100644 --- a/src/Chainweb/Chainweb/Configuration.hs +++ b/src/Chainweb/Chainweb/Configuration.hs @@ -617,10 +617,10 @@ parseVersion = constructVersion maybe (_versionUpgrades winningVersion) (\fub' -> OnChains $ HM.mapWithKey (\cid _ -> - case winningVersion ^?! versionForks . at fub' . _Just . onChain cid of + case winningVersion ^?! versionForks . at fub' . _Just . atChain cid of ForkNever -> error "Chainweb.Chainweb.Configuration.parseVersion: the fork upper bound never occurs in this version." - ForkAtBlockHeight fubHeight -> HM.filterWithKey (\bh _ -> bh <= fubHeight) (winningVersion ^?! versionUpgrades . onChain cid) - ForkAtGenesis -> winningVersion ^?! versionUpgrades . onChain cid + ForkAtBlockHeight fubHeight -> HM.filterWithKey (\bh _ -> bh <= fubHeight) (winningVersion ^?! versionUpgrades . atChain cid) + ForkAtGenesis -> winningVersion ^?! versionUpgrades . atChain cid ) (HS.toMap (chainIds winningVersion)) ) fub diff --git a/src/Chainweb/Pact/Backend/ChainwebPactCoreDb.hs b/src/Chainweb/Pact/Backend/ChainwebPactCoreDb.hs index 3ee5b72c2c..cae1c08bb0 100644 --- a/src/Chainweb/Pact/Backend/ChainwebPactCoreDb.hs +++ b/src/Chainweb/Pact/Backend/ChainwebPactCoreDb.hs @@ -613,7 +613,7 @@ doCommit = view blockHandlerMode >>= \case m -> do txrs <- if m == Transactional then do - modify' $ over latestTxId (\(TxId tid) -> TxId (tid + 1)) + modify' $ over latestTxId (\(TxId tid) -> TxId (succ tid)) pending <- use (bsPendingTxOrError "commit") persistIntraBlockWrites <- view blockHandlerPersistIntraBlockWrites -- merge pending tx into pending block data diff --git a/src/Chainweb/Pact/PactService.hs b/src/Chainweb/Pact/PactService.hs index 583c6f0b49..7fd7994707 100644 --- a/src/Chainweb/Pact/PactService.hs +++ b/src/Chainweb/Pact/PactService.hs @@ -254,7 +254,7 @@ initialPayloadState initialPayloadState v cid | v ^. versionCheats . disablePact = pure () | otherwise = initializeCoinContract v cid $ - v ^?! versionGenesis . genesisBlockPayload . onChain cid + v ^?! versionGenesis . genesisBlockPayload . atChain cid initializeCoinContract :: forall tbl logger. (CanReadablePayloadCas tbl, Logger logger) diff --git a/src/Chainweb/Pact/PactService/Pact5/ExecBlock.hs b/src/Chainweb/Pact/PactService/Pact5/ExecBlock.hs index 2fbd54a4c8..da192a4540 100644 --- a/src/Chainweb/Pact/PactService/Pact5/ExecBlock.hs +++ b/src/Chainweb/Pact/PactService/Pact5/ExecBlock.hs @@ -141,10 +141,9 @@ pact5TransactionsFromPayload :: PayloadData -> IO (Vector Pact5.Transaction) pact5TransactionsFromPayload plData = do - vtrans <- fmap V.fromList $ - mapM toCWTransaction $ + vtrans <- mapM toCWTransaction $ toList (_payloadDataTransactions plData) - let (theLefts, theRights) = partitionEithers $ V.toList vtrans + let (theLefts, theRights) = partitionEithers vtrans unless (null theLefts) $ do let ls = map T.pack theLefts throwM $ TransactionDecodeFailure $ "Failed to decode pact transactions: " @@ -167,6 +166,7 @@ continueBlock -> BlockInProgress Pact5 -> PactBlockM logger tbl (BlockInProgress Pact5) continueBlock mpAccess blockInProgress = do + pbBlockHandle .= _blockInProgressHandle blockInProgress liftPactServiceM $ logDebug "starting continueBlock" -- update the mempool, ensuring that we reintroduce any transactions that -- were removed due to being completed in a block on a different fork. @@ -207,15 +207,18 @@ continueBlock mpAccess blockInProgress = do (V.fromList $ fmap pact5RequestKeyToTransactionHash $ concat invalids) let !blockInProgress' = blockInProgress - & blockInProgressHandle .~ finalBlockHandle - & blockInProgressTransactions . transactionPairs %~ (\txs -> txs <> V.fromList (concat $ reverse valids)) - & blockInProgressRemainingGasLimit .~ finalGasLimit + & blockInProgressHandle .~ + finalBlockHandle + & blockInProgressTransactions . transactionPairs .~ + startTxs <> V.fromList (concat valids) + & blockInProgressRemainingGasLimit .~ + finalGasLimit return blockInProgress' where blockParentHeader = _parentHeader $ _blockInProgressParentHeader blockInProgress - refill fetchLimit txTimeLimit = go [] [] + refill fetchLimit txTimeLimit blockFillState = over _2 reverse <$> go [] [] blockFillState where go :: [CompletedTransactions] -> [InvalidTransactions] @@ -239,7 +242,7 @@ continueBlock mpAccess blockInProgress = do else do -- all request keys from mempool -- badlist vs included - (newCompletedTransactions, newInvalidTransactions, newGasLimit, timedOut) <- + (newCompletedTransactions, newInvalidTransactions, newBlockGasLimit, timedOut) <- execNewTransactions (_blockInProgressMiner blockInProgress) prevRemainingGas txTimeLimit newTxs liftPactServiceM $ do logDebug $ "Refill: included request keys: " <> sshow @[Hash] (fmap (unRequestKey . _crReqKey . snd) newCompletedTransactions) @@ -247,7 +250,7 @@ continueBlock mpAccess blockInProgress = do let newBlockFillState = BlockFill { _bfCount = succ prevFillCount - , _bfGasLimit = newGasLimit + , _bfGasLimit = newBlockGasLimit , _bfTxHashes = flip (foldr (S.insert . pact5RequestKeyToTransactionHash . view (_2 . Pact5.crReqKey))) diff --git a/src/Chainweb/Pact/Types.hs b/src/Chainweb/Pact/Types.hs index 60c14c233d..5d27e031a2 100644 --- a/src/Chainweb/Pact/Types.hs +++ b/src/Chainweb/Pact/Types.hs @@ -1335,7 +1335,7 @@ pact5CommandToBytes tx = Transaction pact5CommandResultToBytes :: Pact5.CommandResult Pact5.Hash TxFailedError -> TransactionOutput pact5CommandResultToBytes cr = TransactionOutput - { _transactionOutputBytes = traceShowId $ + { _transactionOutputBytes = -- TODO: pact5, error codes J.encodeStrict (fmap (sshow @_ @Text) cr) } diff --git a/src/Chainweb/Pact4/ModuleCache.hs b/src/Chainweb/Pact4/ModuleCache.hs index 6dc035f14d..d586b4db38 100644 --- a/src/Chainweb/Pact4/ModuleCache.hs +++ b/src/Chainweb/Pact4/ModuleCache.hs @@ -120,7 +120,7 @@ moduleCacheKeys (ModuleCache a) = fst <$> LHM.toList a -- it uses genesisHeight which is from BlockHeader which imports Guards cleanModuleCache :: ChainwebVersion -> ChainId -> BlockHeight -> Bool cleanModuleCache v cid bh = - case v ^?! versionForks . at Chainweb217Pact . _Just . onChain cid of + case v ^?! versionForks . at Chainweb217Pact . _Just . atChain cid of ForkAtBlockHeight bh' -> bh == bh' ForkAtGenesis -> bh == genesisHeight v cid ForkNever -> False diff --git a/src/Chainweb/Pact4/TransactionExec.hs b/src/Chainweb/Pact4/TransactionExec.hs index 665d867dcb..e3181c008b 100644 --- a/src/Chainweb/Pact4/TransactionExec.hs +++ b/src/Chainweb/Pact4/TransactionExec.hs @@ -805,7 +805,7 @@ applyUpgrades -> TransactionM logger p (Maybe ModuleCache) applyUpgrades v cid height | Just (ForSomePactVersion Pact4T upg) <- - v ^? versionUpgrades . onChain cid . ix height = applyUpgrade upg + v ^? versionUpgrades . atChain cid . ix height = applyUpgrade upg | cleanModuleCache v cid height = filterModuleCache | otherwise = return Nothing where diff --git a/src/Chainweb/Pact5/TransactionExec.hs b/src/Chainweb/Pact5/TransactionExec.hs index c171eb110b..8be9dedde5 100644 --- a/src/Chainweb/Pact5/TransactionExec.hs +++ b/src/Chainweb/Pact5/TransactionExec.hs @@ -467,7 +467,7 @@ applyCmd logger maybeGasLogger pactDb txCtx spv initialGas cmd = do , _crTxId = _erTxId payloadResult , _crResult = -- TODO: don't use `last` here for GHC 9.10 compat - PactResultOk $ compileValueToPactValue $ last $ traceShowId (_erOutput payloadResult) + PactResultOk $ compileValueToPactValue $ last $ _erOutput payloadResult , _crGas = gasUsed , _crLogs = Just $ _erLogs buyGasResult <> _erLogs payloadResult <> _erLogs redeemGasResult , _crContinuation = _erExec payloadResult @@ -570,7 +570,7 @@ applyUpgrades applyUpgrades logger db txCtx | Just (ForPact5 upg) <- _chainwebVersion txCtx ^? versionUpgrades - . onChain (_chainId txCtx) + . atChain (_chainId txCtx) . ix (ctxCurrentBlockHeight txCtx) = applyUpgrade upg | otherwise = return () @@ -705,6 +705,8 @@ buyGas -> Command (Payload PublicMeta ParsedCode) -> IO (Either Pact5BuyGasError EvalResult) buyGas logger db txCtx cmd = do + logFunctionText logger L.Debug $ + "buying gas for " <> sshow (_cmdHash cmd) -- TODO: use quirked gas? let gasPayerCaps = [ cap @@ -795,6 +797,8 @@ redeemGas :: (Logger logger) -> IO (Either Pact5RedeemGasError EvalResult) redeemGas logger pactDb txCtx gasUsed maybeFundTxPactId cmd | isChainweb224Pact, Nothing <- maybeFundTxPactId = do + logFunctionText logger L.Debug $ + "redeeming gas (post-2.24) for " <> sshow (_cmdHash cmd) -- if we're past chainweb 2.24, we don't use defpacts for gas; see 'pact/coin-contract/coin.pact#redeem-gas' let (redeemGasTerm, redeemGasData) = mkRedeemGasTerm mid mks sender gasTotal gasFee @@ -817,6 +821,8 @@ redeemGas logger pactDb txCtx gasUsed maybeFundTxPactId cmd pure $ Right evalResult | not isChainweb224Pact, Just fundTxPactId <- maybeFundTxPactId = do + logFunctionText logger L.Debug $ + "redeeming gas (pre-2.24) for " <> sshow (_cmdHash cmd) -- before chainweb 2.24, we use defpacts for gas; see: 'pact/coin-contract/coin.pact#fund-tx' let redeemGasData = PObject $ Map.singleton "fee" (PDecimal $ _pact5GasSupply gasFee) evalContinuation Transactional diff --git a/src/Chainweb/Payload.hs b/src/Chainweb/Payload.hs index e2303fac45..0dd63a5cf1 100644 --- a/src/Chainweb/Payload.hs +++ b/src/Chainweb/Payload.hs @@ -737,7 +737,7 @@ decodePayloadDataList = runGetS $ do encodePayloadWithOutputsList :: PayloadWithOutputsList -> B.ByteString encodePayloadWithOutputsList (PayloadWithOutputsList xs) = runPutS $ do putWord64be (fromIntegral $ length xs) - forM_ xs putPayloadWithOutputs + forM_ xs putPayloadWithOutputs decodePayloadWithOutputsList :: (MonadThrow m) => B.ByteString -> m PayloadWithOutputsList decodePayloadWithOutputsList = runGetS $ do diff --git a/src/Chainweb/Payload/PayloadStore.hs b/src/Chainweb/Payload/PayloadStore.hs index 6391409d25..083b92c81b 100644 --- a/src/Chainweb/Payload/PayloadStore.hs +++ b/src/Chainweb/Payload/PayloadStore.hs @@ -334,7 +334,7 @@ initializePayloadDb initializePayloadDb v db = traverse_ initForChain $ chainIds v where initForChain cid = - addNewPayload db (genesisHeight v cid) $ v ^?! versionGenesis . genesisBlockPayload . onChain cid + addNewPayload db (genesisHeight v cid) $ v ^?! versionGenesis . genesisBlockPayload . atChain cid -- -------------------------------------------------------------------------- -- -- Insert new Payload diff --git a/src/Chainweb/Rosetta/Internal.hs b/src/Chainweb/Rosetta/Internal.hs index f382bc484d..2db1bfc9d8 100644 --- a/src/Chainweb/Rosetta/Internal.hs +++ b/src/Chainweb/Rosetta/Internal.hs @@ -124,7 +124,7 @@ matchLogs -> ExceptT RosettaFailure Handler tx matchLogs typ bh logs coinbase txs | bheight == genesisHeight v cid = matchGenesis - | Just (ForPact4 upg) <- v ^? versionUpgrades . onChain cid . at bheight . _Just = matchRemediation upg + | Just (ForPact4 upg) <- v ^? versionUpgrades . atChain cid . at bheight . _Just = matchRemediation upg -- TODO: integrate pact 5? | otherwise = matchRest where diff --git a/src/Chainweb/Version.hs b/src/Chainweb/Version.hs index 61ea30be9d..ce394719f2 100644 --- a/src/Chainweb/Version.hs +++ b/src/Chainweb/Version.hs @@ -539,7 +539,7 @@ makeLensesWith (lensRules & generateLazyPatterns .~ True) 'VersionDefaults makeLensesWith (lensRules & generateLazyPatterns .~ True) 'VersionQuirks genesisBlockPayloadHash :: ChainwebVersion -> ChainId -> BlockPayloadHash -genesisBlockPayloadHash v cid = v ^?! versionGenesis . genesisBlockPayload . onChain cid . to _payloadWithOutputsPayloadHash +genesisBlockPayloadHash v cid = v ^?! versionGenesis . genesisBlockPayload . atChain cid . to _payloadWithOutputsPayloadHash instance HasTextRepresentation ChainwebVersionName where toText = getChainwebVersionName @@ -669,8 +669,8 @@ indexByForkHeights v = OnChains . foldl' go (HM.empty <$ HS.toMap (chainIds v)) newTxs = HM.fromList $ [ (cid, HM.singleton forkHeight upg) | cid <- HM.keys acc - , Just upg <- [txsPerChain ^? onChain cid] - , ForkAtBlockHeight forkHeight <- [v ^?! versionForks . at fork . _Just . onChain cid] + , Just upg <- [txsPerChain ^? atChain cid] + , ForkAtBlockHeight forkHeight <- [v ^?! versionForks . at fork . _Just . atChain cid] , forkHeight /= maxBound ] diff --git a/src/Chainweb/Version/Guards.hs b/src/Chainweb/Version/Guards.hs index a4a10a00f3..f035daceb4 100644 --- a/src/Chainweb/Version/Guards.hs +++ b/src/Chainweb/Version/Guards.hs @@ -74,7 +74,7 @@ import Chainweb.Version import Chainweb.Utils.Rule getForkHeight :: Fork -> ChainwebVersion -> ChainId -> ForkHeight -getForkHeight fork v cid = v ^?! versionForks . at fork . _Just . onChain cid +getForkHeight fork v cid = v ^?! versionForks . at fork . _Just . atChain cid checkFork :: (BlockHeight -> ForkHeight -> Bool) diff --git a/src/Chainweb/Version/Mainnet.hs b/src/Chainweb/Version/Mainnet.hs index 73e0d89de7..fd6fb7ca3a 100644 --- a/src/Chainweb/Version/Mainnet.hs +++ b/src/Chainweb/Version/Mainnet.hs @@ -159,7 +159,7 @@ mainnet = ChainwebVersion , _versionWindow = WindowWidth 120 , _versionHeaderBaseSizeBytes = 318 - 110 , _versionMaxBlockGasLimit = - (succ $ mainnet ^?! versionForks . at Chainweb216Pact . _Just . onChain (unsafeChainId 0) . _ForkAtBlockHeight, Just 180_000) `Above` + (succ $ mainnet ^?! versionForks . at Chainweb216Pact . _Just . atChain (unsafeChainId 0) . _ForkAtBlockHeight, Just 180_000) `Above` End Nothing , _versionBootstraps = domainAddr2PeerInfo mainnetBootstrapHosts , _versionGenesis = VersionGenesis diff --git a/src/Chainweb/Version/Testnet.hs b/src/Chainweb/Version/Testnet.hs index 875c34c372..45983fad15 100644 --- a/src/Chainweb/Version/Testnet.hs +++ b/src/Chainweb/Version/Testnet.hs @@ -139,7 +139,7 @@ testnet = ChainwebVersion , _versionWindow = WindowWidth 120 , _versionHeaderBaseSizeBytes = 318 - 110 , _versionMaxBlockGasLimit = - (succ $ testnet ^?! versionForks . at Chainweb216Pact . _Just . onChain (unsafeChainId 0) . _ForkAtBlockHeight, Just 180_000) `Above` + (succ $ testnet ^?! versionForks . at Chainweb216Pact . _Just . atChain (unsafeChainId 0) . _ForkAtBlockHeight, Just 180_000) `Above` End Nothing , _versionBootstraps = domainAddr2PeerInfo testnetBootstrapHosts , _versionGenesis = VersionGenesis diff --git a/src/Chainweb/Version/Utils.hs b/src/Chainweb/Version/Utils.hs index 5eb92ad3fc..51a88daef6 100644 --- a/src/Chainweb/Version/Utils.hs +++ b/src/Chainweb/Version/Utils.hs @@ -463,7 +463,7 @@ verifiersAt v cid bh = M.restrictKeys allVerifierPlugins activeVerifierNames where activeVerifierNames = - case measureRule bh $ _versionVerifierPluginNames v ^?! onChain cid of + case measureRule bh $ _versionVerifierPluginNames v ^?! atChain cid of Bottom vs -> vs Top (_, vs) -> vs Between (_, vs) _ -> vs diff --git a/test/Chainweb/Test/BlockHeader/Genesis.hs b/test/Chainweb/Test/BlockHeader/Genesis.hs index 61740efc5b..f03fb73cbc 100644 --- a/test/Chainweb/Test/BlockHeader/Genesis.hs +++ b/test/Chainweb/Test/BlockHeader/Genesis.hs @@ -70,36 +70,36 @@ graphTransitionTargetTests :: TestTree graphTransitionTargetTests = testGroup "graph transition genesis targets" -- mainnet20InitialHashTarget properties [ testProperty "mainnet20InitialHashTarget deserialization" $ - Just (Mainnet01 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) === (HashTarget . (4 *) <$> decodePowHashNat64 "DOordl9cgfs4ZTBdFnbjRW5th-hW-pL33DIAAAAAAAA") + Just (Mainnet01 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) === (HashTarget . (4 *) <$> decodePowHashNat64 "DOordl9cgfs4ZTBdFnbjRW5th-hW-pL33DIAAAAAAAA") , testProperty "mainnet20InitialHashTarget json deserialization" $ - Just (Mainnet01 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) === (HashTarget . (4 *) <$> decodePowHashNatJson "DOordl9cgfs4ZTBdFnbjRW5th-hW-pL33DIAAAAAAAA") + Just (Mainnet01 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) === (HashTarget . (4 *) <$> decodePowHashNatJson "DOordl9cgfs4ZTBdFnbjRW5th-hW-pL33DIAAAAAAAA") , testProperties "mainnet old chains" $ forChain Mainnet01 maxTarget . unsafeChainId <$> [0..9] , testProperties "mainnet new chains" $ - forChain Mainnet01 (Mainnet01 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) . unsafeChainId <$> [10..19] + forChain Mainnet01 (Mainnet01 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) . unsafeChainId <$> [10..19] -- testnet20InitialHashTarget properties , testProperty "testnet20InitialHashTarget deserialization" $ - Just (Testnet04 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) === (HashTarget <$> decodePowHashNat64 "NZIklpW6xujSPrX3gyhXInfxxOS6JDjkW_GbGwAAAAA") + Just (Testnet04 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) === (HashTarget <$> decodePowHashNat64 "NZIklpW6xujSPrX3gyhXInfxxOS6JDjkW_GbGwAAAAA") , testProperty "testnet20InitialHashTarget json deserialization" $ - Just (Testnet04 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) === (HashTarget <$> decodePowHashNatJson "NZIklpW6xujSPrX3gyhXInfxxOS6JDjkW_GbGwAAAAA") + Just (Testnet04 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) === (HashTarget <$> decodePowHashNatJson "NZIklpW6xujSPrX3gyhXInfxxOS6JDjkW_GbGwAAAAA") , testProperties "testnet old chains" $ forChain Testnet04 maxTarget . unsafeChainId <$> [0..9] , testProperties "testnet new chains" $ - forChain Testnet04 (Testnet04 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) . unsafeChainId <$> [10..19] + forChain Testnet04 (Testnet04 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) . unsafeChainId <$> [10..19] -- Cross check targets to ensure that the values are as expected , testProperty "cross check testnet20InitialHashTarget and mainnet20InitialHashTarget" $ - _hashTarget (Testnet04 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) `div` - _hashTarget (Mainnet01 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) + _hashTarget (Testnet04 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) `div` + _hashTarget (Mainnet01 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) === PowHashNat 8893 , testProperty "cross check development and testnet20InitialHashTarget" $ - _hashTarget (RecapDevelopment ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) `div` - _hashTarget (Testnet04 ^?! versionGenesis . genesisBlockTarget . onChain (unsafeChainId 10)) + _hashTarget (RecapDevelopment ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) `div` + _hashTarget (Testnet04 ^?! versionGenesis . genesisBlockTarget . atChain (unsafeChainId 10)) === PowHashNat 20321 ] where - forChain v target cid = (show cid, v ^?! versionGenesis . genesisBlockTarget . onChain cid === target) + forChain v target cid = (show cid, v ^?! versionGenesis . genesisBlockTarget . atChain cid === target) decodePowHashNat64 t = runGetS decodePowHashNat =<< decodeB64UrlNoPaddingText t decodePowHashNatJson t = decodeStrictOrThrow' @_ @PowHashNat $ "\"" <> t <> "\"" diff --git a/test/Chainweb/Test/Cut/TestBlockDb.hs b/test/Chainweb/Test/Cut/TestBlockDb.hs index ccdaa39ce7..623228db67 100644 --- a/test/Chainweb/Test/Cut/TestBlockDb.hs +++ b/test/Chainweb/Test/Cut/TestBlockDb.hs @@ -14,6 +14,9 @@ module Chainweb.Test.Cut.TestBlockDb , mkTestBlockDb , addTestBlockDb , getParentTestBlockDb + , getParentBlockTestBlockDb + , getCutTestBlockDb + , setCutTestBlockDb , getBlockHeaderDb -- convenience export , RocksDbTable @@ -23,6 +26,7 @@ import Control.Concurrent.MVar import Control.Monad.Catch import qualified Data.HashMap.Strict as HM +import Chainweb.Block import Chainweb.BlockHeader import Chainweb.BlockHeaderDB import Chainweb.ChainId @@ -38,6 +42,7 @@ import Chainweb.WebBlockHeaderDB import Chainweb.Storage.Table.RocksDB import Chainweb.BlockHeight +import Control.Monad data TestBlockDb = TestBlockDb { _bdbWebBlockHeaderDb :: WebBlockHeaderDb @@ -103,6 +108,22 @@ getParentTestBlockDb (TestBlockDb _ _ cmv) cid = do fromMaybeM (userError $ "Internal error, parent not found for cid " ++ show cid) $ HM.lookup cid $ _cutMap c +-- | Get header for chain on current cut. +getParentBlockTestBlockDb :: TestBlockDb -> ChainId -> IO Block +getParentBlockTestBlockDb tdb cid = do + bh <- getParentTestBlockDb tdb cid + pwo <- fromJuste <$> lookupPayloadWithHeight (_bdbPayloadDb tdb) (Just $ _blockHeight bh) (_blockPayloadHash bh) + return Block + { _blockHeader = bh + , _blockPayloadWithOutputs = pwo + } + +getCutTestBlockDb :: TestBlockDb -> IO Cut +getCutTestBlockDb (TestBlockDb _ _ cmv) = readMVar cmv + +setCutTestBlockDb :: TestBlockDb -> Cut -> IO () +setCutTestBlockDb (TestBlockDb _ _ cmv) c = void $ swapMVar cmv c + -- | Convenience accessor getBlockHeaderDb :: MonadThrow m => ChainId -> TestBlockDb -> m BlockHeaderDb getBlockHeaderDb cid (TestBlockDb wdb _ _) = diff --git a/test/Chainweb/Test/Pact5/PactServiceTest.hs b/test/Chainweb/Test/Pact5/PactServiceTest.hs index 015e34b097..0444857104 100644 --- a/test/Chainweb/Test/Pact5/PactServiceTest.hs +++ b/test/Chainweb/Test/Pact5/PactServiceTest.hs @@ -9,6 +9,7 @@ #-} {-# options_ghc -fno-warn-gadt-mono-local-binds #-} +{-# LANGUAGE TupleSections #-} module Chainweb.Test.Pact5.PactServiceTest ( tests @@ -54,7 +55,7 @@ import Chainweb.Pact5.Types import Chainweb.Payload import Chainweb.Payload (PayloadWithOutputs_ (_payloadWithOutputsPayloadHash), Transaction (Transaction)) import Chainweb.Storage.Table.RocksDB -import Chainweb.Test.Cut.TestBlockDb (TestBlockDb (_bdbPayloadDb, _bdbWebBlockHeaderDb), mkTestBlockDb, addTestBlockDb, getParentTestBlockDb) +import Chainweb.Test.Cut.TestBlockDb (TestBlockDb (_bdbPayloadDb, _bdbWebBlockHeaderDb), mkTestBlockDb, addTestBlockDb, getParentTestBlockDb, getCutTestBlockDb, setCutTestBlockDb) import Chainweb.Test.Pact4.Utils (stdoutDummyLogger, stdoutDummyLogger, withBlockHeaderDb) import Chainweb.Test.Pact4.Utils (testPactServiceConfig) import Chainweb.Test.Pact5.CmdBuilder @@ -137,8 +138,12 @@ import System.LogLevel (LogLevel(..)) import Test.Tasty import Test.Tasty.HUnit (assertBool, assertEqual, assertFailure, testCase) import Test.Tasty.Hedgehog -import Text.Show.Pretty () +import Text.Show.Pretty (pPrint) import qualified Hedgehog.Gen as Gen +import qualified Data.HashSet as HashSet +import qualified Data.HashMap.Strict as HashMap +import Chainweb.Block (Block(_blockPayloadWithOutputs)) +import qualified Data.Text.IO as T insertMempool :: MempoolBackend Pact4.UnparsedTransaction -> InsertType -> [Pact5.Transaction] -> IO () insertMempool mp insertType txs = do @@ -179,25 +184,19 @@ simpleEndToEnd baseRdb = runResourceT $ do -- The mempool expires txs based on current time, but newBlock expires txs based on parent creation time. -- So by running an empty block with the creationTime set to the current time, we get these goals to align -- for future blocks we run. - headerOfEmptyBlock <- mineEmptyBlock tdb (ParentHeader (gh v cid)) pactQueue + advanceAllChains tdb pactQueue $ OnChains mempty - let parent = ParentHeader headerOfEmptyBlock + parent <- ParentHeader <$> getParentTestBlockDb tdb cid do let Time creationTime = _bct $ add second $ _blockCreationTime $ _parentHeader parent - cmd1 <- buildCwCmd v (simpleTransfer creationTime 1.0) - cmd2 <- buildCwCmd v (simpleTransfer creationTime 2.0) + cmd1 <- buildCwCmd v (transferCmd 1.0) + cmd2 <- buildCwCmd v (transferCmd 2.0) insertMempool mempool CheckedInsert [cmd1, cmd2] - blockInProgress <- throwIfNotPact5 =<< newBlock noMiner NewBlockFill parent pactQueue - let pwo = blockInProgressToPayloadWithOutputs blockInProgress - let creationTime = add second $ _blockCreationTime $ _parentHeader parent - let blockHeader = newBlockHeader mempty (_payloadWithOutputsPayloadHash pwo) (Nonce 1234) creationTime parent - -- This threadDelay makes it so that we are not too fast for block validation. - threadDelay 1_000_000 - pwo' <- validateBlock blockHeader (CheckablePayloadWithOutputs pwo) pactQueue - addTestBlockDb tdb (_blockHeight blockHeader) (Nonce 1234) (\_ _ -> (_bct creationTime)) cid pwo - assertEqual "payloadWithOutputs are the same before and after validation" pwo pwo' + advanceAllChains tdb pactQueue $ onChain cid $ \ph -> do + blockInProgress <- throwIfNotPact5 =<< throwIfNoHistory =<< newBlock noMiner NewBlockFill parent pactQueue + return $ blockInProgressToPayloadWithOutputs blockInProgress return () @@ -224,20 +223,19 @@ newBlockEmpty baseRdb = runResourceT $ do -- The mempool expires txs based on current time, but newBlock expires txs based on parent creation time. -- So by running an empty block with the creationTime set to the current time, we get these goals to align -- for future blocks we run. - headerOfEmptyBlock <- mineEmptyBlock tdb (ParentHeader (gh v cid)) pactQueue + advanceAllChains tdb pactQueue $ OnChains mempty - let parent = ParentHeader headerOfEmptyBlock + parent <- ParentHeader <$> getParentTestBlockDb tdb cid - let Time txCreationTime = _bct $ add second $ _blockCreationTime $ _parentHeader parent - cmd <- buildCwCmd v (simpleTransfer txCreationTime 1.0) + cmd <- buildCwCmd v (transferCmd 1.0) insertMempool mempool CheckedInsert [cmd] - -- Test that NewBlockEmpty ignores the mempool - emptyBip <- throwIfNotPact5 =<< newBlock noMiner NewBlockEmpty parent pactQueue + -- -- Test that NewBlockEmpty ignores the mempool + emptyBip <- throwIfNotPact5 =<< throwIfNoHistory =<< newBlock noMiner NewBlockEmpty parent pactQueue let emptyPwo = blockInProgressToPayloadWithOutputs emptyBip assertEqual "empty block has no transactions" 0 (Vector.length $ _payloadWithOutputsTransactions emptyPwo) - nonEmptyBip <- throwIfNotPact5 =<< newBlock noMiner NewBlockFill parent pactQueue + nonEmptyBip <- throwIfNotPact5 =<< throwIfNoHistory =<< newBlock noMiner NewBlockFill parent pactQueue let nonEmptyPwo = blockInProgressToPayloadWithOutputs nonEmptyBip assertEqual "non-empty block has transactions" 1 (Vector.length $ _payloadWithOutputsTransactions nonEmptyPwo) @@ -255,7 +253,7 @@ continueBlockSpec baseRdb = runResourceT $ do pactExecutionServiceVar <- newMVar (mkPactExecutionService pactQueue) let mempoolCfg = validatingMempoolConfig cid v (Pact4.GasLimit 150_000) (Pact4.GasPrice 1e-8) pactExecutionServiceVar - let logger = genericLogger Debug Text.putStrLn --stdoutDummyLogger + let logger = genericLogger Error Text.putStrLn --stdoutDummyLogger withInMemoryMempool_ logger mempoolCfg v $ \mempool -> do mempoolConsensus <- liftIO $ mkMempoolConsensus mempool bhdb (Just (_bdbPayloadDb tdb)) let mempoolAccess = pactMemPoolAccess mempoolConsensus logger @@ -266,69 +264,56 @@ continueBlockSpec baseRdb = runResourceT $ do -- The mempool expires txs based on current time, but newBlock expires txs based on parent creation time. -- So by running an empty block with the creationTime set to the current time, we get these goals to align -- for future blocks we run. - headerOfEmptyBlock <- mineEmptyBlock tdb (ParentHeader (gh v cid)) pactQueue - - let Time txCreationTime = _bct $ add second $ _blockCreationTime $ headerOfEmptyBlock - cmd1 <- buildCwCmd v (simpleTransfer txCreationTime 1.0) - cmd2 <- buildCwCmd v (simpleTransfer txCreationTime 2.0) - cmd3 <- buildCwCmd v (simpleTransfer txCreationTime 3.0) - - - insertMempool mempool CheckedInsert [cmd1] - bipStart <- throwIfNotPact5 =<< newBlock noMiner NewBlockFill (ParentHeader headerOfEmptyBlock) pactQueue - let ParentHeader parentHeader = _blockInProgressParentHeader bipStart - - insertMempool mempool CheckedInsert [cmd2] - bipContinued <- throwIfNoHistory =<< continueBlock bipStart pactQueue - - insertMempool mempool CheckedInsert [cmd3] - bipFinal <- throwIfNoHistory =<< continueBlock bipContinued pactQueue - - putStrLn "" - putStr "bipStart: " >> print bipStart - putStr "bipContinued: " >> print bipContinued - putStr "bipFinal: " >> print bipFinal - - -- We must make progress on the same parent header - assertEqual "same parent header after continuing block" - (_blockInProgressParentHeader bipStart) - (_blockInProgressParentHeader bipContinued) - assertBool "made progress (1)" - (bipStart /= bipContinued) - assertEqual "same parent header after finishing block" - (_blockInProgressParentHeader bipContinued) - (_blockInProgressParentHeader bipFinal) - assertBool "made progress (2)" - (bipContinued /= bipFinal) - - -- Add block to database - let pwoContinued = blockInProgressToPayloadWithOutputs bipFinal - let creationTime = add millisecond $ _blockCreationTime parentHeader - forM_ (chainIds v) $ \c -> do - let o | c == cid = pwoContinued - | otherwise = emptyPayload - addTestBlockDb tdb (succ $ _blockHeight parentHeader) (Nonce 0) (\_ _ -> _bct creationTime) c o - - putStr "pwoContinued: " - print pwoContinued - - -- A continued block must be valid - nextHeader <- getParentTestBlockDb tdb cid - -- This threadDelay makes it so that we are not too fast for block validation. - threadDelay 1_000_000 - _ <- validateBlock nextHeader (CheckablePayloadWithOutputs pwoContinued) pactQueue - - -- reset to parent - {- - pactSyncToBlock parentHeader pactQueue + advanceAllChains tdb pactQueue $ OnChains mempty + startCut <- getCutTestBlockDb tdb + headerOfEmptyBlock <- getParentTestBlockDb tdb cid + + -- construct some transactions that we plan to put into the block + cmd1 <- buildCwCmd v (transferCmd 1.0) + cmd2 <- buildCwCmd v (transferCmd 2.0) + cmd3 <- buildCwCmd v (transferCmd 3.0) + + -- insert all transactions insertMempool mempool CheckedInsert [cmd1, cmd2, cmd3] - bipAllAtOnce <- throwIfNotPact5 =<< newBlock noMiner NewBlockFill (ParentHeader headerOfEmptyBlock) pactQueue - let pwoAllAtOnce = blockInProgressToPayloadWithOutputs bipAllAtOnce - assertEqual "a continued block, and one that's done all at once, should be exactly equal" - pwoContinued - pwoAllAtOnce - _ <- validateBlock nextHeader (CheckablePayloadWithOutputs pwoAllAtOnce) pactQueue - -} + + -- construct a new block with all of said transactions + advanceAllChains tdb pactQueue $ onChain cid $ \ph -> do + bipAllAtOnce <- throwIfNotPact5 =<< throwIfNoHistory =<< + newBlock noMiner NewBlockFill (ParentHeader ph) pactQueue + return $ blockInProgressToPayloadWithOutputs bipAllAtOnce + + -- reset back to the empty block for the next phase + -- next, produce the same block by repeatedly extending a block + -- with the same transactions as were included in the original. + -- note that this will reinsert all txs in the full block into the + -- mempool, so we need to clear it after, or else the block will + -- contain all of the transactions before we extend it. + revert tdb pactQueue startCut + mempoolClear mempool + advanceAllChains tdb pactQueue $ onChain cid $ \ph -> do + insertMempool mempool CheckedInsert [cmd3] + bipStart <- throwIfNotPact5 =<< throwIfNoHistory =<< + newBlock noMiner NewBlockFill (ParentHeader ph) pactQueue + + insertMempool mempool CheckedInsert [cmd2] + bipContinued <- throwIfNoHistory =<< continueBlock bipStart pactQueue + + insertMempool mempool CheckedInsert [cmd1] + bipFinal <- throwIfNoHistory =<< continueBlock bipContinued pactQueue + + -- We must make progress on the same parent header + assertEqual "same parent header after continuing block" + (_blockInProgressParentHeader bipStart) + (_blockInProgressParentHeader bipContinued) + assertBool "made progress (1)" + (bipStart /= bipContinued) + assertEqual "same parent header after finishing block" + (_blockInProgressParentHeader bipContinued) + (_blockInProgressParentHeader bipFinal) + assertBool "made progress (2)" + (bipContinued /= bipFinal) + + return $ blockInProgressToPayloadWithOutputs bipFinal pure () @@ -360,44 +345,59 @@ tests = do -} cid = unsafeChainId 0 -gh = genesisBlockHeader v = instantCpmTestVersion singletonChainGraph coinModuleName :: ModuleName coinModuleName = ModuleName "coin" Nothing -mineEmptyBlock :: TestBlockDb -> ParentHeader -> PactQueue -> IO BlockHeader -mineEmptyBlock tdb parent pactQueue = do - bip <- throwIfNotPact5 =<< newBlock noMiner NewBlockEmpty parent pactQueue - let pwo = blockInProgressToPayloadWithOutputs bip - creationTime <- BlockCreationTime <$> getCurrentTimeIntegral - let blockHeader = newBlockHeader mempty (_payloadWithOutputsPayloadHash pwo) (Nonce 1234) creationTime parent - pwo' <- validateBlock blockHeader (CheckablePayloadWithOutputs pwo) pactQueue - assertEqual "payloadWithOutputs are the same before and after validation" pwo pwo' - addTestBlockDb tdb (_blockHeight blockHeader) (Nonce 1234) (\_ _ -> (_bct creationTime)) cid pwo - pure blockHeader - -throwIfNotPact5 :: Historical (ForSomePactVersion f) -> IO (f Pact5) +-- this mines a block on *all chains*. if you don't specify a payload on a chain, +-- it adds empty blocks! +advanceAllChains tdb pactQueue m = + forM_ (HashSet.toList (chainIds v)) $ \c -> do + ph <- getParentTestBlockDb tdb c + creationTime <- getCurrentTimeIntegral + let makeEmptyBlock ph = do + bip <- throwIfNotPact5 =<< throwIfNoHistory =<< newBlock noMiner NewBlockEmpty (ParentHeader ph) pactQueue + return $! blockInProgressToPayloadWithOutputs bip + + payload <- fromMaybe makeEmptyBlock (m ^? atChain cid) ph + True <- addTestBlockDb tdb + (succ $ _blockHeight ph) + (Nonce 0) + (\_ _ -> creationTime) + c + payload + ph' <- getParentTestBlockDb tdb c + payload' <- validateBlock ph' (CheckablePayloadWithOutputs payload) pactQueue + assertEqual "payloads must not be altered by validateBlock" payload payload' + return () + + +revert tdb q c = do + setCutTestBlockDb tdb c + forM_ (HashSet.toList (chainIds v)) $ \chain -> do + ph <- getParentTestBlockDb tdb chain + pactSyncToBlock ph q + +throwIfNotPact5 :: ForSomePactVersion f -> IO (f Pact5) throwIfNotPact5 h = case h of - NoHistory -> do - assertFailure "throwIfNotPact5: NoHistory" - Historical (ForSomePactVersion Pact4T _) -> do + ForSomePactVersion Pact4T _ -> do assertFailure "throwIfNotPact5: should be pact5" - Historical (ForSomePactVersion Pact5T a) -> do + ForSomePactVersion Pact5T a -> do pure a -simpleTransfer :: TimeSpan Micros -> Double -> CmdBuilder -simpleTransfer creationTime transferAmount = defaultCmd +transferCmd :: Decimal -> CmdBuilder +transferCmd transferAmount = defaultCmd { _cbRPC = mkExec' $ "(coin.transfer \"sender00\" \"sender01\" " <> sshow transferAmount <> ")" , _cbSigners = [ mkEd25519Signer' sender00 [ CapToken (QualifiedName "GAS" (ModuleName "coin" Nothing)) [] - , CapToken (QualifiedName "TRANSFER" coinModuleName) [PString "sender00", PString "sender01", PDecimal 1_000_000_000] + , CapToken (QualifiedName "TRANSFER" coinModuleName) [PString "sender00", PString "sender01", PDecimal transferAmount] ] ] , _cbSender = "sender00" , _cbChainId = cid - , _cbGasPrice = GasPrice 2 + -- for ordering the transactions as they appear in the block + , _cbGasPrice = GasPrice transferAmount , _cbGasLimit = GasLimit (Gas 1000) - , _cbCreationTime = Just (TxCreationTime $ fromIntegral $ timeSpanToSeconds creationTime) } diff --git a/test/Chainweb/Test/Pact5/TransactionExecTest.hs b/test/Chainweb/Test/Pact5/TransactionExecTest.hs index 1d2a94ff61..14e035b823 100644 --- a/test/Chainweb/Test/Pact5/TransactionExecTest.hs +++ b/test/Chainweb/Test/Pact5/TransactionExecTest.hs @@ -514,7 +514,6 @@ applyLocalSpec baseRdb = runResourceT $ do , pt _crMetaData $ match _Just continue ] - endSender00Bal <- readBal pactDb "sender00" assertEqual "ending balance should be equal" startSender00Bal endSender00Bal endMinerBal <- readBal pactDb "NoMiner" @@ -643,25 +642,6 @@ applyCmdVerifierSpec baseRdb = runResourceT $ do -- reflects buyGas gas usage, as well as that of the payload , pt _crGas . equals $ Gas 60158 , pt _crContinuation . equals $ Nothing - , pt _crLogs . match _Just $ - PT.list - [ satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "sender00" - ] - , satAll - [ pt _txDomain . equals $ "SYS:Modules" - , pt _txKey . equals $ "free.m" - ] - , satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "NoMiner" - ] - , satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "sender00" - ] - ] ] let baseCmd = defaultCmd @@ -699,17 +679,6 @@ applyCmdVerifierSpec baseRdb = runResourceT $ do -- reflects buyGas gas usage, as well as that of the payload , pt _crGas . equals $ Gas 300 , pt _crContinuation . equals $ Nothing - , pt _crLogs . match _Just $ - PT.list - [ satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "sender00" - ] - , satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "NoMiner" - ] - ] ] -- Invoke module when verifier capability is present. Should succeed. @@ -740,20 +709,6 @@ applyCmdVerifierSpec baseRdb = runResourceT $ do , pt _crGas . equals $ Gas 168 , pt _crContinuation . equals $ Nothing , pt _crMetaData . equals $ Nothing - , pt _crLogs . match _Just $ PT.list - [ satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "sender00" - ] - , satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "NoMiner" - ] - , satAll - [ pt _txDomain . equals $ "USER_coin_coin-table" - , pt _txKey . equals $ "sender00" - ] - ] ] return () diff --git a/test/Chainweb/Test/RestAPI.hs b/test/Chainweb/Test/RestAPI.hs index 68df76041f..2c45887a34 100644 --- a/test/Chainweb/Test/RestAPI.hs +++ b/test/Chainweb/Test/RestAPI.hs @@ -46,11 +46,12 @@ import Text.Read (readEither) -- internal modules +import Chainweb.Block import Chainweb.BlockHash (BlockHash) import Chainweb.BlockHeader import Chainweb.BlockHeaderDB import Chainweb.BlockHeaderDB.Internal (unsafeInsertBlockHeaderDb) -import Chainweb.BlockHeaderDB.RestAPI (Block(..)) +import Chainweb.BlockHeaderDB.RestAPI import Chainweb.ChainId import Chainweb.Graph import Chainweb.Mempool.Mempool (MempoolBackend, MockTx) @@ -235,7 +236,7 @@ simpleClientSession envIO cid = [] -> liftIO $ assertFailure "blocksClient did return empty result" (h:_) -> return h assertExpectation "block client returned wrong entry" - (Expected (Block gbh0 (version ^?! versionGenesis . genesisBlockPayload . onChain cid))) + (Expected (Block gbh0 (version ^?! versionGenesis . genesisBlockPayload . atChain cid))) (Actual gen1Block) void $ liftIO $ step "put 3 new blocks" diff --git a/test/Chainweb/Test/RestAPI/Client_.hs b/test/Chainweb/Test/RestAPI/Client_.hs index 77e88ba85a..9260da9d2c 100644 --- a/test/Chainweb/Test/RestAPI/Client_.hs +++ b/test/Chainweb/Test/RestAPI/Client_.hs @@ -45,6 +45,7 @@ import Servant.API.ContentTypes -- internal modules +import Chainweb.Block import Chainweb.BlockHash import Chainweb.BlockHeader import Chainweb.BlockHeaderDB @@ -195,4 +196,3 @@ branchBlocksClient' v c = runIdentity $ do (SomeSing (SChainwebVersion :: Sing v)) <- return $ toSing (_versionName v) (SomeSing (SChainId :: Sing c)) <- return $ toSing c return $ client_ @(BranchBlocksApi v c) - \ No newline at end of file diff --git a/test/Chainweb/Test/Rosetta/RestAPI.hs b/test/Chainweb/Test/Rosetta/RestAPI.hs index 8e521b4be9..28ce2524c0 100644 --- a/test/Chainweb/Test/Rosetta/RestAPI.hs +++ b/test/Chainweb/Test/Rosetta/RestAPI.hs @@ -343,7 +343,7 @@ blockCoinV2RemediationTests _ envIo = _ -> assertFailure $ "coin v2 remediation block should have at least 3 transactions:" ++ " coinbase + 2 remediations" where - bhCoinV2Rem = v ^?! versionForks . at CoinV2 . _Just . onChain cid . _ForkAtBlockHeight . to getBlockHeight + bhCoinV2Rem = v ^?! versionForks . at CoinV2 . _Just . atChain cid . _ForkAtBlockHeight . to getBlockHeight req h = BlockReq nid $ PartialBlockId (Just h) Nothing block20ChainRemediationTests :: RosettaTest @@ -418,7 +418,7 @@ blockCoinV3RemediationTests _ envIo = _ -> assertFailure $ "coin v3 remediation block should have at least 3 transactions:" ++ " coinbase + 2 remediations" where - bhCoinV3Rem = v ^?! versionForks . at Pact4Coin3 . _Just . onChain cid . _ForkAtBlockHeight . to getBlockHeight + bhCoinV3Rem = v ^?! versionForks . at Pact4Coin3 . _Just . atChain cid . _ForkAtBlockHeight . to getBlockHeight req h = BlockReq nid $ PartialBlockId (Just h) Nothing -- | Rosetta construction endpoints tests (i.e. tx formatting and submission) diff --git a/test/Chainweb/Test/Utils.hs b/test/Chainweb/Test/Utils.hs index 8dd8d7f955..1643c530b1 100644 --- a/test/Chainweb/Test/Utils.hs +++ b/test/Chainweb/Test/Utils.hs @@ -27,6 +27,7 @@ module Chainweb.Test.Utils , withResource' , withResourceT , independentSequentialTestGroup +, unsafeHeadOf -- * Test RocksDb , testRocksDb @@ -233,6 +234,7 @@ import qualified P2P.Node.PeerDB as P2P import P2P.Peer import Chainweb.Test.Utils.APIValidation +import Data.Semigroup -- -------------------------------------------------------------------------- -- -- Intialize Test BlockHeader DB @@ -1156,3 +1158,6 @@ independentSequentialTestGroup tn tts = (mvarIO >>= takeMVar) (\_ -> mvarIO >>= flip putMVar ()) $ \_ -> tt + +unsafeHeadOf :: HasCallStack => Getting (Endo a) s a -> s -> a +unsafeHeadOf l s = s ^?! l