go-ethereum/eth/tracers/internal/tracetest/firehose/firehose_test.go
2025-01-27 14:23:16 -05:00

99 lines
3.2 KiB
Go

package firehose_test
import (
"encoding/json"
"math/big"
"path/filepath"
"strings"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/trie"
"github.com/stretchr/testify/require"
)
func TestFirehoseChain(t *testing.T) {
context := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Coinbase: common.Address{},
BlockNumber: new(big.Int).SetUint64(uint64(1)),
Time: 1,
Difficulty: big.NewInt(2),
GasLimit: uint64(1000000),
BaseFee: big.NewInt(8),
}
tracer, err := tracers.NewFirehoseFromRawJSON(json.RawMessage(`{
"applyBackwardsCompatibility": true,
"_private": {
"flushToTestBuffer": true,
"ignoreGenesisBlock": true
}
}`))
require.NoError(t, err)
hooks := tracers.NewTracingHooksFromFirehose(tracer)
genesis, blockchain := newBlockchain(t, types.GenesisAlloc{}, context, hooks)
block := types.NewBlock(&types.Header{
ParentHash: genesis.ToBlock().Hash(),
Number: context.BlockNumber,
Difficulty: context.Difficulty,
Coinbase: context.Coinbase,
Time: context.Time,
GasLimit: context.GasLimit,
BaseFee: context.BaseFee,
ParentBeaconRoot: ptr(common.Hash{}),
}, nil, nil, trie.NewStackTrie(nil))
blockchain.SetBlockValidatorAndProcessorForTesting(
ignoreValidateStateValidator{core.NewBlockValidator(genesis.Config, blockchain)},
core.NewStateProcessor(genesis.Config, blockchain.HeaderChain()),
)
n, err := blockchain.InsertChain(types.Blocks{block})
require.NoError(t, err)
require.Equal(t, 1, n)
genesisLine, blockLines, unknownLines := readTracerFirehoseLines(t, tracer)
require.Len(t, unknownLines, 0, "Lines:\n%s", strings.Join(slicesMap(unknownLines, func(l unknownLine) string { return "- '" + string(l) + "'" }), "\n"))
require.NotNil(t, genesisLine)
blockLines.assertEquals(t, filepath.Join("testdata", t.Name()),
firehoseBlockLineParams{"1", "8e6ee4b1054d94df1d8a51fb983447dc2e27a854590c3ac0061f994284be8150", "0", "845bad515694a416bab4b8d44e22cf97a8c894a8502110ab807883940e185ce0", "0", "1000000000"},
)
}
func TestFirehosePrestate(t *testing.T) {
testFolders := []string{
"./testdata/TestFirehosePrestate/keccak256_too_few_memory_bytes_get_padded",
"./testdata/TestFirehosePrestate/keccak256_wrong_diff",
}
for _, folder := range testFolders {
name := filepath.Base(folder)
t.Run(name, func(t *testing.T) {
tracer, err := tracers.NewFirehoseFromRawJSON(json.RawMessage(`{
"applyBackwardsCompatibility": true,
"_private": {
"flushToTestBuffer": true
}
}`))
require.NoError(t, err)
runPrestateBlock(t, filepath.Join(folder, "prestate.json"), tracers.NewTracingHooksFromFirehose(tracer))
genesisLine, blockLines, unknownLines := readTracerFirehoseLines(t, tracer)
require.Len(t, unknownLines, 0, "Lines:\n%s", strings.Join(slicesMap(unknownLines, func(l unknownLine) string { return "- '" + string(l) + "'" }), "\n"))
require.NotNil(t, genesisLine)
blockLines.assertOnlyBlockEquals(t, folder, 1)
})
}
}