all: fix tests

This commit is contained in:
Marius van der Wijden 2024-03-21 07:55:23 +01:00
parent 43dd6b968c
commit 1d8d879764
10 changed files with 81 additions and 63 deletions

View file

@ -261,7 +261,10 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
ParentBeaconRoot: beaconRoot, ParentBeaconRoot: beaconRoot,
InclusionListSummaryRoot: inclusionRoot, InclusionListSummaryRoot: inclusionRoot,
} }
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals).WithInclusionList(params.InclusionListSummary.Summary) block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals)
if inclusionRoot != nil {
block = block.WithInclusionList(params.InclusionListSummary.Summary)
}
if block.Hash() != params.BlockHash { if block.Hash() != params.BlockHash {
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash()) return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
} }

View file

@ -387,7 +387,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
header.Root = state.IntermediateRoot(true) header.Root = state.IntermediateRoot(true)
// Assemble and return the final block. // Assemble and return the final block.
return types.NewBlockWithWithdrawals(header, body.Transactions, body.Uncles, receipts, body.Withdrawals, trie.NewStackTrie(nil)), nil return types.NewBlockWithWithdrawals(header, body.Transactions, body.Uncles, receipts, body.Withdrawals, trie.NewStackTrie(nil)).WithInclusionList(body.InclusionListSummary), nil
} }
// Seal generates a new sealing request for the given input block and pushes // Seal generates a new sealing request for the given input block and pushes

View file

@ -49,6 +49,7 @@ type BlockGen struct {
receipts []*types.Receipt receipts []*types.Receipt
uncles []*types.Header uncles []*types.Header
withdrawals []*types.Withdrawal withdrawals []*types.Withdrawal
inclusionListSummary []*types.InclusionListEntry
engine consensus.Engine engine consensus.Engine
} }
@ -345,7 +346,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
gen(i, b) gen(i, b)
} }
body := types.Body{Transactions: b.txs, Uncles: b.uncles, Withdrawals: b.withdrawals} body := types.Body{Transactions: b.txs, Uncles: b.uncles, Withdrawals: b.withdrawals, InclusionListSummary: b.inclusionListSummary}
block, err := b.engine.FinalizeAndAssemble(cm, b.header, statedb, &body, b.receipts) block, err := b.engine.FinalizeAndAssemble(cm, b.header, statedb, &body, b.receipts)
if err != nil { if err != nil {
panic(err) panic(err)

View file

@ -320,7 +320,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode(&eb); err != nil { if err := s.Decode(&eb); err != nil {
return err return err
} }
b.header, b.uncles, b.transactions, b.withdrawals = eb.Header, eb.Uncles, eb.Txs, eb.Withdrawals b.header, b.uncles, b.transactions, b.withdrawals, b.inclusionListSummary = eb.Header, eb.Uncles, eb.Txs, eb.Withdrawals, eb.InclusionListSummary
b.size.Store(rlp.ListSize(size)) b.size.Store(rlp.ListSize(size))
return nil return nil
} }
@ -332,6 +332,7 @@ func (b *Block) EncodeRLP(w io.Writer) error {
Txs: b.transactions, Txs: b.transactions,
Uncles: b.uncles, Uncles: b.uncles,
Withdrawals: b.withdrawals, Withdrawals: b.withdrawals,
InclusionListSummary: b.inclusionListSummary,
}) })
} }
@ -495,6 +496,7 @@ func (b *Block) WithInclusionList(summary []*InclusionListEntry) *Block {
header: b.header, header: b.header,
transactions: b.transactions, transactions: b.transactions,
uncles: b.uncles, uncles: b.uncles,
withdrawals: b.withdrawals,
} }
if summary != nil { if summary != nil {
block.inclusionListSummary = make([]*InclusionListEntry, len(summary)) block.inclusionListSummary = make([]*InclusionListEntry, len(summary))

View file

@ -138,6 +138,7 @@ type ConsensusAPI struct {
newPayloadLock sync.Mutex // Lock for the NewPayload method newPayloadLock sync.Mutex // Lock for the NewPayload method
coolMapThatIsNotAMemLeak map[common.Hash][]*types.Transaction coolMapThatIsNotAMemLeak map[common.Hash][]*types.Transaction
coolMapThatIsNotAMemLeak2 map[common.Hash]*types.InclusionListSummary
} }
// NewConsensusAPI creates a new consensus api for the given backend. // NewConsensusAPI creates a new consensus api for the given backend.
@ -160,6 +161,7 @@ func newConsensusAPIWithoutHeartbeat(eth *eth.Ethereum) *ConsensusAPI {
invalidBlocksHits: make(map[common.Hash]int), invalidBlocksHits: make(map[common.Hash]int),
invalidTipsets: make(map[common.Hash]*types.Header), invalidTipsets: make(map[common.Hash]*types.Header),
coolMapThatIsNotAMemLeak: make(map[common.Hash][]*types.Transaction), coolMapThatIsNotAMemLeak: make(map[common.Hash][]*types.Transaction),
coolMapThatIsNotAMemLeak2: make(map[common.Hash]*types.InclusionListSummary),
} }
eth.Downloader().SetBadBlockCallback(api.setInvalidAncestor) eth.Downloader().SetBadBlockCallback(api.setInvalidAncestor)
return api return api
@ -346,6 +348,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
api.eth.BlockChain().SetFinalized(finalBlock.Header()) api.eth.BlockChain().SetFinalized(finalBlock.Header())
// Clear the inclusionList for that block // Clear the inclusionList for that block
delete(api.coolMapThatIsNotAMemLeak, update.FinalizedBlockHash) delete(api.coolMapThatIsNotAMemLeak, update.FinalizedBlockHash)
delete(api.coolMapThatIsNotAMemLeak2, update.FinalizedBlockHash)
} }
// Check if the safe block hash is in our canonical tree, if not something is wrong // Check if the safe block hash is in our canonical tree, if not something is wrong
if update.SafeBlockHash != (common.Hash{}) { if update.SafeBlockHash != (common.Hash{}) {
@ -374,6 +377,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
BeaconRoot: payloadAttributes.BeaconRoot, BeaconRoot: payloadAttributes.BeaconRoot,
Version: payloadVersion, Version: payloadVersion,
InclusionList: api.coolMapThatIsNotAMemLeak[update.HeadBlockHash], InclusionList: api.coolMapThatIsNotAMemLeak[update.HeadBlockHash],
InclusionListSummary: api.coolMapThatIsNotAMemLeak2[update.HeadBlockHash],
} }
id := args.Id() id := args.Id()
// If we already are busy generating this work, then we do not need // If we already are busy generating this work, then we do not need
@ -904,8 +908,8 @@ func getBody(block *types.Block) *engine.ExecutionPayloadBodyV1 {
} }
} }
func (api *ConsensusAPI) NewInclusionListV1(addresses []common.Address, transactions [][]byte, parentBlockHash common.Hash) (*engine.InclusionListStatusV1, error) { func (api *ConsensusAPI) NewInclusionListV1(inclusionListSummary *types.InclusionListSummary, transactions [][]byte, parentBlockHash common.Hash) (*engine.InclusionListStatusV1, error) {
if len(addresses) != len(transactions) { if len(inclusionListSummary.Summary) != len(transactions) {
return inclusionListError("number of transactions do not match addresses"), nil return inclusionListError("number of transactions do not match addresses"), nil
} }
txs, err := engine.DecodeTransactions(transactions) txs, err := engine.DecodeTransactions(transactions)
@ -919,8 +923,8 @@ func (api *ConsensusAPI) NewInclusionListV1(addresses []common.Address, transact
if err != nil { if err != nil {
return inclusionListError(fmt.Sprintf("Invalid signer at index %v, error: %v", index, err)), nil return inclusionListError(fmt.Sprintf("Invalid signer at index %v, error: %v", index, err)), nil
} }
if addresses[index] != sender { if inclusionListSummary.Summary[index].Address != sender {
return inclusionListError(fmt.Sprintf("Invalid sender at index %v, got %v want %v", index, sender, addresses[index])), nil return inclusionListError(fmt.Sprintf("Invalid sender at index %v, got %v want %v", index, sender, inclusionListSummary.Summary[index].Address)), nil
} }
maxGas += tx.Gas() maxGas += tx.Gas()
} }
@ -935,6 +939,7 @@ func (api *ConsensusAPI) NewInclusionListV1(addresses []common.Address, transact
return inclusionListError(err.Error()), nil return inclusionListError(err.Error()), nil
} }
api.coolMapThatIsNotAMemLeak[parentBlockHash] = txs api.coolMapThatIsNotAMemLeak[parentBlockHash] = txs
api.coolMapThatIsNotAMemLeak2[parentBlockHash] = inclusionListSummary
return &engine.InclusionListStatusV1{Status: engine.VALID}, nil return &engine.InclusionListStatusV1{Status: engine.VALID}, nil
} }

View file

@ -136,7 +136,7 @@ func TestSimulatedBeaconSendWithdrawals(t *testing.T) {
return return
} }
case <-timer.C: case <-timer.C:
t.Fatal("timed out without including all withdrawals/txs") t.Fatalf("timed out without including all withdrawals/txs, want %v got %v", len(withdrawals), len(includedWithdrawals))
} }
} }
} }

View file

@ -400,7 +400,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
block := backend.chain.GetBlockByNumber(uint64(num)) block := backend.chain.GetBlockByNumber(uint64(num))
hashes = append(hashes, block.Hash()) hashes = append(hashes, block.Hash())
if len(bodies) < tt.expected { if len(bodies) < tt.expected {
bodies = append(bodies, &BlockBody{Transactions: block.Transactions(), Uncles: block.Uncles(), Withdrawals: block.Withdrawals()}) bodies = append(bodies, &BlockBody{Transactions: block.Transactions(), Uncles: block.Uncles(), Withdrawals: block.Withdrawals(), InclusionListSummary: block.InclusionListSummary()})
} }
break break
} }
@ -410,7 +410,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
hashes = append(hashes, hash) hashes = append(hashes, hash)
if tt.available[j] && len(bodies) < tt.expected { if tt.available[j] && len(bodies) < tt.expected {
block := backend.chain.GetBlockByHash(hash) block := backend.chain.GetBlockByHash(hash)
bodies = append(bodies, &BlockBody{Transactions: block.Transactions(), Uncles: block.Uncles(), Withdrawals: block.Withdrawals()}) bodies = append(bodies, &BlockBody{Transactions: block.Transactions(), Uncles: block.Uncles(), Withdrawals: block.Withdrawals(), InclusionListSummary: block.InclusionListSummary()})
} }
} }

View file

@ -43,6 +43,7 @@ type BuildPayloadArgs struct {
BeaconRoot *common.Hash // The provided beaconRoot (Cancun) BeaconRoot *common.Hash // The provided beaconRoot (Cancun)
Version engine.PayloadVersion // Versioning byte for payload id calculation. Version engine.PayloadVersion // Versioning byte for payload id calculation.
InclusionList []*types.Transaction // Mandatory Inclusion List InclusionList []*types.Transaction // Mandatory Inclusion List
InclusionListSummary *types.InclusionListSummary // Inclusion List Summary
} }
// Id computes an 8-byte identifier by hashing the components of the payload arguments. // Id computes an 8-byte identifier by hashing the components of the payload arguments.
@ -191,6 +192,7 @@ func (miner *Miner) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
beaconRoot: args.BeaconRoot, beaconRoot: args.BeaconRoot,
noTxs: true, noTxs: true,
inclusionList: args.InclusionList, inclusionList: args.InclusionList,
inclusionListSummary: args.InclusionListSummary,
} }
empty := miner.generateWork(emptyParams) empty := miner.generateWork(emptyParams)
if empty.err != nil { if empty.err != nil {

View file

@ -85,6 +85,7 @@ type generateParams struct {
withdrawals types.Withdrawals // List of withdrawals to include in block (shanghai field) withdrawals types.Withdrawals // List of withdrawals to include in block (shanghai field)
beaconRoot *common.Hash // The beacon root (cancun field). beaconRoot *common.Hash // The beacon root (cancun field).
noTxs bool // Flag whether an empty block without any transaction is expected noTxs bool // Flag whether an empty block without any transaction is expected
inclusionListSummary *types.InclusionListSummary // Inclusion List summary
inclusionList []*types.Transaction // Mandatory transactions to include inclusionList []*types.Transaction // Mandatory transactions to include
} }
@ -119,7 +120,11 @@ func (miner *Miner) generateWork(params *generateParams) *newPayloadResult {
log.Warn("Block building is interrupted", "allowance", common.PrettyDuration(miner.config.Recommit)) log.Warn("Block building is interrupted", "allowance", common.PrettyDuration(miner.config.Recommit))
} }
} }
body := types.Body{Transactions: work.txs, Withdrawals: params.withdrawals} var summary []*types.InclusionListEntry
if params.inclusionListSummary != nil {
summary = params.inclusionListSummary.Summary
}
body := types.Body{Transactions: work.txs, Withdrawals: params.withdrawals, InclusionListSummary: summary}
block, err := miner.engine.FinalizeAndAssemble(miner.chain, work.header, work.state, &body, work.receipts) block, err := miner.engine.FinalizeAndAssemble(miner.chain, work.header, work.state, &body, work.receipts)
if err != nil { if err != nil {
return &newPayloadResult{err: err} return &newPayloadResult{err: err}

View file

@ -273,7 +273,7 @@ func TestNotify(t *testing.T) {
} }
notifier.Notify(id, msg) notifier.Notify(id, msg)
have := strings.TrimSpace(out.String()) have := strings.TrimSpace(out.String())
want := `{"jsonrpc":"2.0","method":"_subscription","params":{"subscription":"test","result":{"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000001","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":null,"number":"0x64","gasLimit":"0x0","gasUsed":"0x0","timestamp":"0x0","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":null,"withdrawalsRoot":null,"blobGasUsed":null,"excessBlobGas":null,"parentBeaconBlockRoot":null,"hash":"0xe5fb877dde471b45b9742bb4bb4b3d74a761e2fb7cb849a3d2b687eed90fb604"}}}` want := `{"jsonrpc":"2.0","method":"_subscription","params":{"subscription":"test","result":{"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000001","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":null,"number":"0x64","gasLimit":"0x0","gasUsed":"0x0","timestamp":"0x0","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":null,"withdrawalsRoot":null,"blobGasUsed":null,"excessBlobGas":null,"parentBeaconBlockRoot":null,"inclusionListSummaryRoot":null,"hash":"0xe5fb877dde471b45b9742bb4bb4b3d74a761e2fb7cb849a3d2b687eed90fb604"}}}`
if have != want { if have != want {
t.Errorf("have:\n%v\nwant:\n%v\n", have, want) t.Errorf("have:\n%v\nwant:\n%v\n", have, want)
} }