Skip to content

Commit

Permalink
Revert "internal/blocktest: add package for shared test code (ethereu…
Browse files Browse the repository at this point in the history
…m#27270)"

This reverts commit 9a984bc.
  • Loading branch information
devopsbo3 authored Nov 10, 2023
1 parent 9e72f39 commit 1cf6d5d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 66 deletions.
28 changes: 26 additions & 2 deletions core/rawdb/accessors_indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,42 @@ package rawdb

import (
"bytes"
"hash"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/blocktest"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

var newHasher = blocktest.NewHasher
// testHasher is the helper tool for transaction/receipt list hashing.
// The original hasher is trie, in order to get rid of import cycle,
// use the testing hasher instead.
type testHasher struct {
hasher hash.Hash
}

func newHasher() *testHasher {
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
}

func (h *testHasher) Reset() {
h.hasher.Reset()
}

func (h *testHasher) Update(key, val []byte) error {
h.hasher.Write(key)
h.hasher.Write(val)
return nil
}

func (h *testHasher) Hash() common.Hash {
return common.BytesToHash(h.hasher.Sum(nil))
}

// Tests that positional lookup metadata can be stored and retrieved.
func TestLookupStorage(t *testing.T) {
Expand Down
30 changes: 28 additions & 2 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ package types

import (
"bytes"
"hash"
"math/big"
"reflect"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/blocktest"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

// from bcValidBlockTest.json, "SimpleTx"
Expand Down Expand Up @@ -216,6 +217,31 @@ func BenchmarkEncodeBlock(b *testing.B) {
}
}

// testHasher is the helper tool for transaction/receipt list hashing.
// The original hasher is trie, in order to get rid of import cycle,
// use the testing hasher instead.
type testHasher struct {
hasher hash.Hash
}

func newHasher() *testHasher {
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
}

func (h *testHasher) Reset() {
h.hasher.Reset()
}

func (h *testHasher) Update(key, val []byte) error {
h.hasher.Write(key)
h.hasher.Write(val)
return nil
}

func (h *testHasher) Hash() common.Hash {
return common.BytesToHash(h.hasher.Sum(nil))
}

func makeBenchBlock() *Block {
var (
key, _ = crypto.GenerateKey()
Expand Down Expand Up @@ -254,7 +280,7 @@ func makeBenchBlock() *Block {
Extra: []byte("benchmark uncle"),
}
}
return NewBlock(header, txs, uncles, receipts, blocktest.NewHasher())
return NewBlock(header, txs, uncles, receipts, newHasher())
}

func TestRlpDecodeParentHash(t *testing.T) {
Expand Down
59 changes: 0 additions & 59 deletions internal/blocktest/test_hash.go

This file was deleted.

32 changes: 29 additions & 3 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/ecdsa"
"encoding/json"
"errors"
"hash"
"math/big"
"reflect"
"testing"
Expand All @@ -41,10 +42,10 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/blocktest"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -667,6 +668,31 @@ func hex2Bytes(str string) *hexutil.Bytes {
return &rpcBytes
}

// testHasher is the helper tool for transaction/receipt list hashing.
// The original hasher is trie, in order to get rid of import cycle,
// use the testing hasher instead.
type testHasher struct {
hasher hash.Hash
}

func newHasher() *testHasher {
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
}

func (h *testHasher) Reset() {
h.hasher.Reset()
}

func (h *testHasher) Update(key, val []byte) error {
h.hasher.Write(key)
h.hasher.Write(val)
return nil
}

func (h *testHasher) Hash() common.Hash {
return common.BytesToHash(h.hasher.Sum(nil))
}

func TestRPCMarshalBlock(t *testing.T) {
t.Parallel()
var (
Expand Down Expand Up @@ -697,7 +723,7 @@ func TestRPCMarshalBlock(t *testing.T) {
}
txs = append(txs, tx)
}
block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, txs, nil, nil, blocktest.NewHasher())
block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, txs, nil, nil, newHasher())

var testSuite = []struct {
inclTx bool
Expand Down Expand Up @@ -769,7 +795,7 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
Address: common.Address{0x12, 0x34},
Amount: 10,
}
pending = types.NewBlockWithWithdrawals(&types.Header{Number: big.NewInt(11), Time: 42}, []*types.Transaction{tx}, nil, nil, []*types.Withdrawal{withdrawal}, blocktest.NewHasher())
pending = types.NewBlockWithWithdrawals(&types.Header{Number: big.NewInt(11), Time: 42}, []*types.Transaction{tx}, nil, nil, []*types.Withdrawal{withdrawal}, newHasher())
)
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1]
Expand Down

0 comments on commit 1cf6d5d

Please sign in to comment.