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,
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 {
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)
// 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

View file

@ -49,6 +49,7 @@ type BlockGen struct {
receipts []*types.Receipt
uncles []*types.Header
withdrawals []*types.Withdrawal
inclusionListSummary []*types.InclusionListEntry
engine consensus.Engine
}
@ -345,7 +346,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
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)
if err != nil {
panic(err)

View file

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

View file

@ -138,6 +138,7 @@ type ConsensusAPI struct {
newPayloadLock sync.Mutex // Lock for the NewPayload method
coolMapThatIsNotAMemLeak map[common.Hash][]*types.Transaction
coolMapThatIsNotAMemLeak2 map[common.Hash]*types.InclusionListSummary
}
// 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),
invalidTipsets: make(map[common.Hash]*types.Header),
coolMapThatIsNotAMemLeak: make(map[common.Hash][]*types.Transaction),
coolMapThatIsNotAMemLeak2: make(map[common.Hash]*types.InclusionListSummary),
}
eth.Downloader().SetBadBlockCallback(api.setInvalidAncestor)
return api
@ -346,6 +348,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
api.eth.BlockChain().SetFinalized(finalBlock.Header())
// Clear the inclusionList for that block
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
if update.SafeBlockHash != (common.Hash{}) {
@ -374,6 +377,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
BeaconRoot: payloadAttributes.BeaconRoot,
Version: payloadVersion,
InclusionList: api.coolMapThatIsNotAMemLeak[update.HeadBlockHash],
InclusionListSummary: api.coolMapThatIsNotAMemLeak2[update.HeadBlockHash],
}
id := args.Id()
// 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) {
if len(addresses) != len(transactions) {
func (api *ConsensusAPI) NewInclusionListV1(inclusionListSummary *types.InclusionListSummary, transactions [][]byte, parentBlockHash common.Hash) (*engine.InclusionListStatusV1, error) {
if len(inclusionListSummary.Summary) != len(transactions) {
return inclusionListError("number of transactions do not match addresses"), nil
}
txs, err := engine.DecodeTransactions(transactions)
@ -919,8 +923,8 @@ func (api *ConsensusAPI) NewInclusionListV1(addresses []common.Address, transact
if err != nil {
return inclusionListError(fmt.Sprintf("Invalid signer at index %v, error: %v", index, err)), nil
}
if addresses[index] != sender {
return inclusionListError(fmt.Sprintf("Invalid sender at index %v, got %v want %v", index, sender, addresses[index])), nil
if inclusionListSummary.Summary[index].Address != sender {
return inclusionListError(fmt.Sprintf("Invalid sender at index %v, got %v want %v", index, sender, inclusionListSummary.Summary[index].Address)), nil
}
maxGas += tx.Gas()
}
@ -935,6 +939,7 @@ func (api *ConsensusAPI) NewInclusionListV1(addresses []common.Address, transact
return inclusionListError(err.Error()), nil
}
api.coolMapThatIsNotAMemLeak[parentBlockHash] = txs
api.coolMapThatIsNotAMemLeak2[parentBlockHash] = inclusionListSummary
return &engine.InclusionListStatusV1{Status: engine.VALID}, nil
}

View file

@ -136,7 +136,7 @@ func TestSimulatedBeaconSendWithdrawals(t *testing.T) {
return
}
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))
hashes = append(hashes, block.Hash())
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
}
@ -410,7 +410,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
hashes = append(hashes, hash)
if tt.available[j] && len(bodies) < tt.expected {
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)
Version engine.PayloadVersion // Versioning byte for payload id calculation.
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.
@ -191,6 +192,7 @@ func (miner *Miner) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
beaconRoot: args.BeaconRoot,
noTxs: true,
inclusionList: args.InclusionList,
inclusionListSummary: args.InclusionListSummary,
}
empty := miner.generateWork(emptyParams)
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)
beaconRoot *common.Hash // The beacon root (cancun field).
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
}
@ -119,7 +120,11 @@ func (miner *Miner) generateWork(params *generateParams) *newPayloadResult {
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)
if err != nil {
return &newPayloadResult{err: err}

View file

@ -273,7 +273,7 @@ func TestNotify(t *testing.T) {
}
notifier.Notify(id, msg)
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 {
t.Errorf("have:\n%v\nwant:\n%v\n", have, want)
}