mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
core: verify receipts in test
This commit is contained in:
parent
fd9a83315e
commit
bf10184c9e
1 changed files with 35 additions and 13 deletions
|
|
@ -19,8 +19,10 @@ package core
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/consensus/beacon"
|
"github.com/ethereum/go-ethereum/consensus/beacon"
|
||||||
"github.com/ethereum/go-ethereum/consensus/ethash"
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
|
|
@ -52,9 +54,8 @@ func TestGeneratePOSChain(t *testing.T) {
|
||||||
Difficulty: common.Big1,
|
Difficulty: common.Big1,
|
||||||
GasLimit: 5_000_000,
|
GasLimit: 5_000_000,
|
||||||
}
|
}
|
||||||
gendb = rawdb.NewMemoryDatabase()
|
gendb = rawdb.NewMemoryDatabase()
|
||||||
signer = types.LatestSigner(gspec.Config)
|
db = rawdb.NewMemoryDatabase()
|
||||||
db = rawdb.NewMemoryDatabase()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
config.TerminalTotalDifficultyPassed = true
|
config.TerminalTotalDifficultyPassed = true
|
||||||
|
|
@ -82,10 +83,20 @@ func TestGeneratePOSChain(t *testing.T) {
|
||||||
}
|
}
|
||||||
genesis := gspec.MustCommit(gendb, trie.NewDatabase(gendb, trie.HashDefaults))
|
genesis := gspec.MustCommit(gendb, trie.NewDatabase(gendb, trie.HashDefaults))
|
||||||
|
|
||||||
chain, _ := GenerateChain(gspec.Config, genesis, beacon.NewFaker(), gendb, 4, func(i int, gen *BlockGen) {
|
genchain, genreceipts := GenerateChain(gspec.Config, genesis, beacon.NewFaker(), gendb, 4, func(i int, gen *BlockGen) {
|
||||||
gen.SetParentBeaconRoot(common.Hash{byte(i + 1)})
|
gen.SetParentBeaconRoot(common.Hash{byte(i + 1)})
|
||||||
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(address), address, big.NewInt(1000), params.TxGas, new(big.Int).Add(gen.BaseFee(), common.Big1), nil), signer, key)
|
|
||||||
|
// Add value transfer tx.
|
||||||
|
tx := types.MustSignNewTx(key, gen.Signer(), &types.LegacyTx{
|
||||||
|
Nonce: gen.TxNonce(address),
|
||||||
|
To: &address,
|
||||||
|
Value: big.NewInt(1000),
|
||||||
|
Gas: params.TxGas,
|
||||||
|
GasPrice: new(big.Int).Add(gen.BaseFee(), common.Big1),
|
||||||
|
})
|
||||||
gen.AddTx(tx)
|
gen.AddTx(tx)
|
||||||
|
|
||||||
|
// Add withdrawals.
|
||||||
if i == 1 {
|
if i == 1 {
|
||||||
gen.AddWithdrawal(&types.Withdrawal{
|
gen.AddWithdrawal(&types.Withdrawal{
|
||||||
Validator: 42,
|
Validator: 42,
|
||||||
|
|
@ -116,20 +127,31 @@ func TestGeneratePOSChain(t *testing.T) {
|
||||||
blockchain, _ := NewBlockChain(db, nil, gspec, nil, beacon.NewFaker(), vm.Config{}, nil, nil)
|
blockchain, _ := NewBlockChain(db, nil, gspec, nil, beacon.NewFaker(), vm.Config{}, nil, nil)
|
||||||
defer blockchain.Stop()
|
defer blockchain.Stop()
|
||||||
|
|
||||||
if i, err := blockchain.InsertChain(chain); err != nil {
|
if i, err := blockchain.InsertChain(genchain); err != nil {
|
||||||
fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err)
|
t.Fatalf("insert error (block %d): %v\n", genchain[i].NumberU64(), err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// enforce that withdrawal indexes are monotonically increasing from 0
|
// enforce that withdrawal indexes are monotonically increasing from 0
|
||||||
var (
|
var (
|
||||||
withdrawalIndex uint64
|
withdrawalIndex uint64
|
||||||
head = blockchain.CurrentBlock().Number.Uint64()
|
|
||||||
)
|
)
|
||||||
for i := 0; i < int(head); i++ {
|
for i := range genchain {
|
||||||
block := blockchain.GetBlockByNumber(uint64(i))
|
blocknum := genchain[i].NumberU64()
|
||||||
|
block := blockchain.GetBlockByNumber(blocknum)
|
||||||
if block == nil {
|
if block == nil {
|
||||||
t.Fatalf("block %d not found", i)
|
t.Fatalf("block %d not found", blocknum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify receipts.
|
||||||
|
genBlockReceipts := genreceipts[i]
|
||||||
|
for _, r := range genBlockReceipts {
|
||||||
|
if r.Logs == nil {
|
||||||
|
r.Logs = []*types.Log{} // patch up empty logs to match decoded from db
|
||||||
|
}
|
||||||
|
}
|
||||||
|
blockchainReceipts := blockchain.GetReceiptsByHash(block.Hash())
|
||||||
|
if !reflect.DeepEqual(genBlockReceipts, blockchainReceipts) {
|
||||||
|
t.Fatalf("receipts mismatch\ngenerated: %s\nblockchain: %s", spew.Sdump(genBlockReceipts), spew.Sdump(blockchainReceipts))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify withdrawals.
|
// Verify withdrawals.
|
||||||
|
|
@ -144,7 +166,7 @@ func TestGeneratePOSChain(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify parent beacon root.
|
// Verify parent beacon root.
|
||||||
want := common.Hash{byte(i)}
|
want := common.Hash{byte(blocknum)}
|
||||||
if got := block.BeaconRoot(); *got != want {
|
if got := block.BeaconRoot(); *got != want {
|
||||||
t.Fatalf("block %d, wrong parent beacon root: got %s, want %s", i, got, want)
|
t.Fatalf("block %d, wrong parent beacon root: got %s, want %s", i, got, want)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue