mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
Merge branch 'ethereum:master' into main
This commit is contained in:
commit
c67193f308
22 changed files with 306 additions and 161 deletions
|
|
@ -33,6 +33,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
intRegex = regexp.MustCompile(`(u)?int([0-9]*)`)
|
||||||
|
)
|
||||||
|
|
||||||
func isKeyWord(arg string) bool {
|
func isKeyWord(arg string) bool {
|
||||||
switch arg {
|
switch arg {
|
||||||
case "break":
|
case "break":
|
||||||
|
|
@ -299,7 +303,7 @@ func bindBasicType(kind abi.Type) string {
|
||||||
case abi.AddressTy:
|
case abi.AddressTy:
|
||||||
return "common.Address"
|
return "common.Address"
|
||||||
case abi.IntTy, abi.UintTy:
|
case abi.IntTy, abi.UintTy:
|
||||||
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
|
parts := intRegex.FindStringSubmatch(kind.String())
|
||||||
switch parts[2] {
|
switch parts[2] {
|
||||||
case "8", "16", "32", "64":
|
case "8", "16", "32", "64":
|
||||||
return fmt.Sprintf("%sint%s", parts[1], parts[2])
|
return fmt.Sprintf("%sint%s", parts[1], parts[2])
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/internal/utesting"
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
|
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -501,6 +502,36 @@ func FindnodeAmplificationWrongIP(t *utesting.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ENRRequest(t *utesting.T) {
|
||||||
|
t.Log(`This test sends an ENRRequest packet and expects a response containing a valid ENR.`)
|
||||||
|
|
||||||
|
te := newTestEnv(Remote, Listen1, Listen2)
|
||||||
|
defer te.close()
|
||||||
|
bond(t, te)
|
||||||
|
|
||||||
|
req := &v4wire.ENRRequest{Expiration: futureExpiration()}
|
||||||
|
hash := te.send(te.l1, req)
|
||||||
|
|
||||||
|
response, _, err := te.read(te.l1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("read error:", err)
|
||||||
|
}
|
||||||
|
enrResp, ok := response.(*v4wire.ENRResponse)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected ENRResponse packet, got %T", response)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(enrResp.ReplyTok, hash) {
|
||||||
|
t.Errorf("wrong hash in response packet: got %x, want %x", enrResp.ReplyTok, hash)
|
||||||
|
}
|
||||||
|
node, err := enode.New(enode.ValidSchemes, &enrResp.Record)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("invalid record in response: %v", err)
|
||||||
|
}
|
||||||
|
if node.ID() != te.remote.ID() {
|
||||||
|
t.Errorf("wrong node ID in response: got %v, want %v", node.ID(), te.remote.ID())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var AllTests = []utesting.Test{
|
var AllTests = []utesting.Test{
|
||||||
{Name: "Ping/Basic", Fn: BasicPing},
|
{Name: "Ping/Basic", Fn: BasicPing},
|
||||||
{Name: "Ping/WrongTo", Fn: PingWrongTo},
|
{Name: "Ping/WrongTo", Fn: PingWrongTo},
|
||||||
|
|
@ -510,6 +541,7 @@ var AllTests = []utesting.Test{
|
||||||
{Name: "Ping/PastExpiration", Fn: PingPastExpiration},
|
{Name: "Ping/PastExpiration", Fn: PingPastExpiration},
|
||||||
{Name: "Ping/WrongPacketType", Fn: WrongPacketType},
|
{Name: "Ping/WrongPacketType", Fn: WrongPacketType},
|
||||||
{Name: "Ping/BondThenPingWithWrongFrom", Fn: BondThenPingWithWrongFrom},
|
{Name: "Ping/BondThenPingWithWrongFrom", Fn: BondThenPingWithWrongFrom},
|
||||||
|
{Name: "ENRRequest", Fn: ENRRequest},
|
||||||
{Name: "Findnode/WithoutEndpointProof", Fn: FindnodeWithoutEndpointProof},
|
{Name: "Findnode/WithoutEndpointProof", Fn: FindnodeWithoutEndpointProof},
|
||||||
{Name: "Findnode/BasicFindnode", Fn: BasicFindnode},
|
{Name: "Findnode/BasicFindnode", Fn: BasicFindnode},
|
||||||
{Name: "Findnode/UnsolicitedNeighbors", Fn: UnsolicitedNeighbors},
|
{Name: "Findnode/UnsolicitedNeighbors", Fn: UnsolicitedNeighbors},
|
||||||
|
|
|
||||||
|
|
@ -34,11 +34,10 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"math/bits"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const uintBits = 32 << (uint64(^uint(0)) >> 63)
|
|
||||||
|
|
||||||
// Errors
|
// Errors
|
||||||
var (
|
var (
|
||||||
ErrEmptyString = &decError{"empty hex string"}
|
ErrEmptyString = &decError{"empty hex string"}
|
||||||
|
|
@ -48,7 +47,7 @@ var (
|
||||||
ErrEmptyNumber = &decError{"hex string \"0x\""}
|
ErrEmptyNumber = &decError{"hex string \"0x\""}
|
||||||
ErrLeadingZero = &decError{"hex number with leading zero digits"}
|
ErrLeadingZero = &decError{"hex number with leading zero digits"}
|
||||||
ErrUint64Range = &decError{"hex number > 64 bits"}
|
ErrUint64Range = &decError{"hex number > 64 bits"}
|
||||||
ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)}
|
ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", bits.UintSize)}
|
||||||
ErrBig256Range = &decError{"hex number > 256 bits"}
|
ErrBig256Range = &decError{"hex number > 256 bits"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"math/bits"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
|
@ -384,7 +385,7 @@ func TestUnmarshalUint(t *testing.T) {
|
||||||
for _, test := range unmarshalUintTests {
|
for _, test := range unmarshalUintTests {
|
||||||
var v Uint
|
var v Uint
|
||||||
err := json.Unmarshal([]byte(test.input), &v)
|
err := json.Unmarshal([]byte(test.input), &v)
|
||||||
if uintBits == 32 && test.wantErr32bit != nil {
|
if bits.UintSize == 32 && test.wantErr32bit != nil {
|
||||||
checkError(t, test.input, err, test.wantErr32bit)
|
checkError(t, test.input, err, test.wantErr32bit)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ type authorizationMarshaling struct {
|
||||||
|
|
||||||
// SignSetCode creates a signed the SetCode authorization.
|
// SignSetCode creates a signed the SetCode authorization.
|
||||||
func SignSetCode(prv *ecdsa.PrivateKey, auth SetCodeAuthorization) (SetCodeAuthorization, error) {
|
func SignSetCode(prv *ecdsa.PrivateKey, auth SetCodeAuthorization) (SetCodeAuthorization, error) {
|
||||||
sighash := auth.sigHash()
|
sighash := auth.SigHash()
|
||||||
sig, err := crypto.Sign(sighash[:], prv)
|
sig, err := crypto.Sign(sighash[:], prv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return SetCodeAuthorization{}, err
|
return SetCodeAuthorization{}, err
|
||||||
|
|
@ -105,7 +105,8 @@ func SignSetCode(prv *ecdsa.PrivateKey, auth SetCodeAuthorization) (SetCodeAutho
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *SetCodeAuthorization) sigHash() common.Hash {
|
// SigHash returns the hash of SetCodeAuthorization for signing.
|
||||||
|
func (a *SetCodeAuthorization) SigHash() common.Hash {
|
||||||
return prefixedRlpHash(0x05, []any{
|
return prefixedRlpHash(0x05, []any{
|
||||||
a.ChainID,
|
a.ChainID,
|
||||||
a.Address,
|
a.Address,
|
||||||
|
|
@ -115,7 +116,7 @@ func (a *SetCodeAuthorization) sigHash() common.Hash {
|
||||||
|
|
||||||
// Authority recovers the the authorizing account of an authorization.
|
// Authority recovers the the authorizing account of an authorization.
|
||||||
func (a *SetCodeAuthorization) Authority() (common.Address, error) {
|
func (a *SetCodeAuthorization) Authority() (common.Address, error) {
|
||||||
sighash := a.sigHash()
|
sighash := a.SigHash()
|
||||||
if !crypto.ValidateSignatureValues(a.V, a.R.ToBig(), a.S.ToBig(), true) {
|
if !crypto.ValidateSignatureValues(a.V, a.R.ToBig(), a.S.ToBig(), true) {
|
||||||
return common.Address{}, ErrInvalidSig
|
return common.Address{}, ErrInvalidSig
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,16 +25,16 @@ const (
|
||||||
set7BitsMask = uint16(0b111_1111)
|
set7BitsMask = uint16(0b111_1111)
|
||||||
)
|
)
|
||||||
|
|
||||||
// bitvec is a bit vector which maps bytes in a program.
|
// BitVec is a bit vector which maps bytes in a program.
|
||||||
// An unset bit means the byte is an opcode, a set bit means
|
// An unset bit means the byte is an opcode, a set bit means
|
||||||
// it's data (i.e. argument of PUSHxx).
|
// it's data (i.e. argument of PUSHxx).
|
||||||
type bitvec []byte
|
type BitVec []byte
|
||||||
|
|
||||||
func (bits bitvec) set1(pos uint64) {
|
func (bits BitVec) set1(pos uint64) {
|
||||||
bits[pos/8] |= 1 << (pos % 8)
|
bits[pos/8] |= 1 << (pos % 8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bits bitvec) setN(flag uint16, pos uint64) {
|
func (bits BitVec) setN(flag uint16, pos uint64) {
|
||||||
a := flag << (pos % 8)
|
a := flag << (pos % 8)
|
||||||
bits[pos/8] |= byte(a)
|
bits[pos/8] |= byte(a)
|
||||||
if b := byte(a >> 8); b != 0 {
|
if b := byte(a >> 8); b != 0 {
|
||||||
|
|
@ -42,13 +42,13 @@ func (bits bitvec) setN(flag uint16, pos uint64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bits bitvec) set8(pos uint64) {
|
func (bits BitVec) set8(pos uint64) {
|
||||||
a := byte(0xFF << (pos % 8))
|
a := byte(0xFF << (pos % 8))
|
||||||
bits[pos/8] |= a
|
bits[pos/8] |= a
|
||||||
bits[pos/8+1] = ^a
|
bits[pos/8+1] = ^a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bits bitvec) set16(pos uint64) {
|
func (bits BitVec) set16(pos uint64) {
|
||||||
a := byte(0xFF << (pos % 8))
|
a := byte(0xFF << (pos % 8))
|
||||||
bits[pos/8] |= a
|
bits[pos/8] |= a
|
||||||
bits[pos/8+1] = 0xFF
|
bits[pos/8+1] = 0xFF
|
||||||
|
|
@ -56,23 +56,23 @@ func (bits bitvec) set16(pos uint64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// codeSegment checks if the position is in a code segment.
|
// codeSegment checks if the position is in a code segment.
|
||||||
func (bits *bitvec) codeSegment(pos uint64) bool {
|
func (bits *BitVec) codeSegment(pos uint64) bool {
|
||||||
return (((*bits)[pos/8] >> (pos % 8)) & 1) == 0
|
return (((*bits)[pos/8] >> (pos % 8)) & 1) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// codeBitmap collects data locations in code.
|
// codeBitmap collects data locations in code.
|
||||||
func codeBitmap(code []byte) bitvec {
|
func codeBitmap(code []byte) BitVec {
|
||||||
// The bitmap is 4 bytes longer than necessary, in case the code
|
// The bitmap is 4 bytes longer than necessary, in case the code
|
||||||
// ends with a PUSH32, the algorithm will set bits on the
|
// ends with a PUSH32, the algorithm will set bits on the
|
||||||
// bitvector outside the bounds of the actual code.
|
// bitvector outside the bounds of the actual code.
|
||||||
bits := make(bitvec, len(code)/8+1+4)
|
bits := make(BitVec, len(code)/8+1+4)
|
||||||
return codeBitmapInternal(code, bits)
|
return codeBitmapInternal(code, bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
// codeBitmapInternal is the internal implementation of codeBitmap.
|
// codeBitmapInternal is the internal implementation of codeBitmap.
|
||||||
// It exists for the purpose of being able to run benchmark tests
|
// It exists for the purpose of being able to run benchmark tests
|
||||||
// without dynamic allocations affecting the results.
|
// without dynamic allocations affecting the results.
|
||||||
func codeBitmapInternal(code, bits bitvec) bitvec {
|
func codeBitmapInternal(code, bits BitVec) BitVec {
|
||||||
for pc := uint64(0); pc < uint64(len(code)); {
|
for pc := uint64(0); pc < uint64(len(code)); {
|
||||||
op := OpCode(code[pc])
|
op := OpCode(code[pc])
|
||||||
pc++
|
pc++
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
|
||||||
for i := range code {
|
for i := range code {
|
||||||
code[i] = byte(op)
|
code[i] = byte(op)
|
||||||
}
|
}
|
||||||
bits := make(bitvec, len(code)/8+1+4)
|
bits := make(BitVec, len(code)/8+1+4)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
clear(bits)
|
clear(bits)
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,8 @@ type Contract struct {
|
||||||
caller common.Address
|
caller common.Address
|
||||||
address common.Address
|
address common.Address
|
||||||
|
|
||||||
jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
|
jumpDests JumpDestCache // Aggregated result of JUMPDEST analysis.
|
||||||
analysis bitvec // Locally cached result of JUMPDEST analysis
|
analysis BitVec // Locally cached result of JUMPDEST analysis
|
||||||
|
|
||||||
Code []byte
|
Code []byte
|
||||||
CodeHash common.Hash
|
CodeHash common.Hash
|
||||||
|
|
@ -47,15 +47,15 @@ type Contract struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContract returns a new contract environment for the execution of EVM.
|
// NewContract returns a new contract environment for the execution of EVM.
|
||||||
func NewContract(caller common.Address, address common.Address, value *uint256.Int, gas uint64, jumpDests map[common.Hash]bitvec) *Contract {
|
func NewContract(caller common.Address, address common.Address, value *uint256.Int, gas uint64, jumpDests JumpDestCache) *Contract {
|
||||||
// Initialize the jump analysis map if it's nil, mostly for tests
|
// Initialize the jump analysis cache if it's nil, mostly for tests
|
||||||
if jumpDests == nil {
|
if jumpDests == nil {
|
||||||
jumpDests = make(map[common.Hash]bitvec)
|
jumpDests = newMapJumpDests()
|
||||||
}
|
}
|
||||||
return &Contract{
|
return &Contract{
|
||||||
caller: caller,
|
caller: caller,
|
||||||
address: address,
|
address: address,
|
||||||
jumpdests: jumpDests,
|
jumpDests: jumpDests,
|
||||||
Gas: gas,
|
Gas: gas,
|
||||||
value: value,
|
value: value,
|
||||||
}
|
}
|
||||||
|
|
@ -87,12 +87,12 @@ func (c *Contract) isCode(udest uint64) bool {
|
||||||
// contracts ( not temporary initcode), we store the analysis in a map
|
// contracts ( not temporary initcode), we store the analysis in a map
|
||||||
if c.CodeHash != (common.Hash{}) {
|
if c.CodeHash != (common.Hash{}) {
|
||||||
// Does parent context have the analysis?
|
// Does parent context have the analysis?
|
||||||
analysis, exist := c.jumpdests[c.CodeHash]
|
analysis, exist := c.jumpDests.Load(c.CodeHash)
|
||||||
if !exist {
|
if !exist {
|
||||||
// Do the analysis and save in parent context
|
// Do the analysis and save in parent context
|
||||||
// We do not need to store it in c.analysis
|
// We do not need to store it in c.analysis
|
||||||
analysis = codeBitmap(c.Code)
|
analysis = codeBitmap(c.Code)
|
||||||
c.jumpdests[c.CodeHash] = analysis
|
c.jumpDests.Store(c.CodeHash, analysis)
|
||||||
}
|
}
|
||||||
// Also stash it in current contract for faster access
|
// Also stash it in current contract for faster access
|
||||||
c.analysis = analysis
|
c.analysis = analysis
|
||||||
|
|
|
||||||
|
|
@ -122,9 +122,8 @@ type EVM struct {
|
||||||
// precompiles holds the precompiled contracts for the current epoch
|
// precompiles holds the precompiled contracts for the current epoch
|
||||||
precompiles map[common.Address]PrecompiledContract
|
precompiles map[common.Address]PrecompiledContract
|
||||||
|
|
||||||
// jumpDests is the aggregated result of JUMPDEST analysis made through
|
// jumpDests stores results of JUMPDEST analysis.
|
||||||
// the life cycle of EVM.
|
jumpDests JumpDestCache
|
||||||
jumpDests map[common.Hash]bitvec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEVM constructs an EVM instance with the supplied block context, state
|
// NewEVM constructs an EVM instance with the supplied block context, state
|
||||||
|
|
@ -138,7 +137,7 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon
|
||||||
Config: config,
|
Config: config,
|
||||||
chainConfig: chainConfig,
|
chainConfig: chainConfig,
|
||||||
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
|
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
|
||||||
jumpDests: make(map[common.Hash]bitvec),
|
jumpDests: newMapJumpDests(),
|
||||||
}
|
}
|
||||||
evm.precompiles = activePrecompiledContracts(evm.chainRules)
|
evm.precompiles = activePrecompiledContracts(evm.chainRules)
|
||||||
evm.interpreter = NewEVMInterpreter(evm)
|
evm.interpreter = NewEVMInterpreter(evm)
|
||||||
|
|
@ -152,6 +151,11 @@ func (evm *EVM) SetPrecompiles(precompiles PrecompiledContracts) {
|
||||||
evm.precompiles = precompiles
|
evm.precompiles = precompiles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetJumpDestCache configures the analysis cache.
|
||||||
|
func (evm *EVM) SetJumpDestCache(jumpDests JumpDestCache) {
|
||||||
|
evm.jumpDests = jumpDests
|
||||||
|
}
|
||||||
|
|
||||||
// SetTxContext resets the EVM with a new transaction context.
|
// SetTxContext resets the EVM with a new transaction context.
|
||||||
// This is not threadsafe and should only be done very cautiously.
|
// This is not threadsafe and should only be done very cautiously.
|
||||||
func (evm *EVM) SetTxContext(txCtx TxContext) {
|
func (evm *EVM) SetTxContext(txCtx TxContext) {
|
||||||
|
|
|
||||||
47
core/vm/jumpdests.go
Normal file
47
core/vm/jumpdests.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
// Copyright 2024 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package vm
|
||||||
|
|
||||||
|
import "github.com/ethereum/go-ethereum/common"
|
||||||
|
|
||||||
|
// JumpDestCache represents the cache of jumpdest analysis results.
|
||||||
|
type JumpDestCache interface {
|
||||||
|
// Load retrieves the cached jumpdest analysis for the given code hash.
|
||||||
|
// Returns the BitVec and true if found, or nil and false if not cached.
|
||||||
|
Load(codeHash common.Hash) (BitVec, bool)
|
||||||
|
|
||||||
|
// Store saves the jumpdest analysis for the given code hash.
|
||||||
|
Store(codeHash common.Hash, vec BitVec)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapJumpDests is the default implementation of JumpDests using a map.
|
||||||
|
// This implementation is not thread-safe and is meant to be used per EVM instance.
|
||||||
|
type mapJumpDests map[common.Hash]BitVec
|
||||||
|
|
||||||
|
// newMapJumpDests creates a new map-based JumpDests implementation.
|
||||||
|
func newMapJumpDests() JumpDestCache {
|
||||||
|
return make(mapJumpDests)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j mapJumpDests) Load(codeHash common.Hash) (BitVec, bool) {
|
||||||
|
vec, ok := j[codeHash]
|
||||||
|
return vec, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j mapJumpDests) Store(codeHash common.Hash, vec BitVec) {
|
||||||
|
j[codeHash] = vec
|
||||||
|
}
|
||||||
|
|
@ -154,7 +154,7 @@ type Config struct {
|
||||||
// RPCEVMTimeout is the global timeout for eth-call.
|
// RPCEVMTimeout is the global timeout for eth-call.
|
||||||
RPCEVMTimeout time.Duration
|
RPCEVMTimeout time.Duration
|
||||||
|
|
||||||
// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
|
// RPCTxFeeCap is the global transaction fee (price * gas limit) cap for
|
||||||
// send-transaction variants. The unit is ether.
|
// send-transaction variants. The unit is ether.
|
||||||
RPCTxFeeCap float64
|
RPCTxFeeCap float64
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,8 @@ var (
|
||||||
errInvalidTopic = errors.New("invalid topic(s)")
|
errInvalidTopic = errors.New("invalid topic(s)")
|
||||||
errFilterNotFound = errors.New("filter not found")
|
errFilterNotFound = errors.New("filter not found")
|
||||||
errInvalidBlockRange = errors.New("invalid block range params")
|
errInvalidBlockRange = errors.New("invalid block range params")
|
||||||
|
errUnknownBlock = errors.New("unknown block")
|
||||||
|
errBlockHashWithRange = errors.New("can't specify fromBlock/toBlock with blockHash")
|
||||||
errPendingLogsUnsupported = errors.New("pending logs are not supported")
|
errPendingLogsUnsupported = errors.New("pending logs are not supported")
|
||||||
errExceedMaxTopics = errors.New("exceed max topics")
|
errExceedMaxTopics = errors.New("exceed max topics")
|
||||||
errExceedMaxAddresses = errors.New("exceed max addresses")
|
errExceedMaxAddresses = errors.New("exceed max addresses")
|
||||||
|
|
@ -348,8 +350,13 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type
|
||||||
if len(crit.Addresses) > maxAddresses {
|
if len(crit.Addresses) > maxAddresses {
|
||||||
return nil, errExceedMaxAddresses
|
return nil, errExceedMaxAddresses
|
||||||
}
|
}
|
||||||
|
|
||||||
var filter *Filter
|
var filter *Filter
|
||||||
if crit.BlockHash != nil {
|
if crit.BlockHash != nil {
|
||||||
|
if crit.FromBlock != nil || crit.ToBlock != nil {
|
||||||
|
return nil, errBlockHashWithRange
|
||||||
|
}
|
||||||
|
|
||||||
// Block filter requested, construct a single-shot filter
|
// Block filter requested, construct a single-shot filter
|
||||||
filter = api.sys.NewBlockFilter(*crit.BlockHash, crit.Addresses, crit.Topics)
|
filter = api.sys.NewBlockFilter(*crit.BlockHash, crit.Addresses, crit.Topics)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -372,6 +379,7 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type
|
||||||
// Construct the range filter
|
// Construct the range filter
|
||||||
filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics)
|
filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the filter and return all the logs
|
// Run the filter and return all the logs
|
||||||
logs, err := filter.Logs(ctx)
|
logs, err := filter.Logs(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if header == nil {
|
if header == nil {
|
||||||
return nil, errors.New("unknown block")
|
return nil, errUnknownBlock
|
||||||
}
|
}
|
||||||
if header.Number.Uint64() < f.sys.backend.HistoryPruningCutoff() {
|
if header.Number.Uint64() < f.sys.backend.HistoryPruningCutoff() {
|
||||||
return nil, &history.PrunedHistoryError{}
|
return nil, &history.PrunedHistoryError{}
|
||||||
|
|
@ -456,7 +456,6 @@ func (f *Filter) blockLogs(ctx context.Context, header *types.Header) ([]*types.
|
||||||
|
|
||||||
// checkMatches checks if the receipts belonging to the given header contain any log events that
|
// checkMatches checks if the receipts belonging to the given header contain any log events that
|
||||||
// match the filter criteria. This function is called when the bloom filter signals a potential match.
|
// match the filter criteria. This function is called when the bloom filter signals a potential match.
|
||||||
// skipFilter signals all logs of the given block are requested.
|
|
||||||
func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*types.Log, error) {
|
func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*types.Log, error) {
|
||||||
hash := header.Hash()
|
hash := header.Hash()
|
||||||
// Logs in cache are partially filled with context data
|
// Logs in cache are partially filled with context data
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ type EventSystem struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEventSystem creates a new manager that listens for event on the given mux,
|
// NewEventSystem creates a new manager that listens for event on the given mux,
|
||||||
// parses and filters them. It uses the all map to retrieve filter changes. The
|
// parses and filters them. It uses an internal map to retrieve filter changes. The
|
||||||
// work loop holds its own index that is used to forward events to filters.
|
// work loop holds its own index that is used to forward events to filters.
|
||||||
//
|
//
|
||||||
// The returned manager has a loop that needs to be stopped with the Stop function
|
// The returned manager has a loop that needs to be stopped with the Stop function
|
||||||
|
|
|
||||||
|
|
@ -450,24 +450,65 @@ func TestInvalidGetLogsRequest(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db = rawdb.NewMemoryDatabase()
|
genesis = &core.Genesis{
|
||||||
_, sys = newTestFilterSystem(db, Config{})
|
Config: params.TestChainConfig,
|
||||||
api = NewFilterAPI(sys)
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||||
blockHash = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
|
}
|
||||||
|
db, blocks, _ = core.GenerateChainWithGenesis(genesis, ethash.NewFaker(), 10, func(i int, gen *core.BlockGen) {})
|
||||||
|
_, sys = newTestFilterSystem(db, Config{})
|
||||||
|
api = NewFilterAPI(sys)
|
||||||
|
blockHash = blocks[0].Hash()
|
||||||
|
unknownBlockHash = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reason: Cannot specify both BlockHash and FromBlock/ToBlock)
|
// Insert the blocks into the chain so filter can look them up
|
||||||
testCases := []FilterCriteria{
|
blockchain, err := core.NewBlockChain(db, genesis, ethash.NewFaker(), nil)
|
||||||
0: {BlockHash: &blockHash, FromBlock: big.NewInt(100)},
|
if err != nil {
|
||||||
1: {BlockHash: &blockHash, ToBlock: big.NewInt(500)},
|
t.Fatalf("failed to create tester chain: %v", err)
|
||||||
2: {BlockHash: &blockHash, FromBlock: big.NewInt(rpc.LatestBlockNumber.Int64())},
|
}
|
||||||
3: {BlockHash: &blockHash, Topics: [][]common.Hash{{}, {}, {}, {}, {}}},
|
if n, err := blockchain.InsertChain(blocks); err != nil {
|
||||||
4: {BlockHash: &blockHash, Addresses: make([]common.Address, maxAddresses+1)},
|
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type testcase struct {
|
||||||
|
f FilterCriteria
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
testCases := []testcase{
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &blockHash, FromBlock: big.NewInt(100)},
|
||||||
|
err: errBlockHashWithRange,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &blockHash, ToBlock: big.NewInt(500)},
|
||||||
|
err: errBlockHashWithRange,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &blockHash, FromBlock: big.NewInt(rpc.LatestBlockNumber.Int64())},
|
||||||
|
err: errBlockHashWithRange,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &unknownBlockHash},
|
||||||
|
err: errUnknownBlock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &blockHash, Topics: [][]common.Hash{{}, {}, {}, {}, {}}},
|
||||||
|
err: errExceedMaxTopics,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &blockHash, Topics: [][]common.Hash{{}, {}, {}, {}, {}}},
|
||||||
|
err: errExceedMaxTopics,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f: FilterCriteria{BlockHash: &blockHash, Addresses: make([]common.Address, maxAddresses+1)},
|
||||||
|
err: errExceedMaxAddresses,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range testCases {
|
for i, test := range testCases {
|
||||||
if _, err := api.GetLogs(context.Background(), test); err == nil {
|
_, err := api.GetLogs(context.Background(), test.f)
|
||||||
t.Errorf("Expected Logs for case #%d to fail", i)
|
if !errors.Is(err, test.err) {
|
||||||
|
t.Errorf("case %d: wrong error: %q\nwant: %q", i, err, test.err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
181
trie/hasher.go
181
trie/hasher.go
|
|
@ -17,6 +17,8 @@
|
||||||
package trie
|
package trie
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
|
@ -54,7 +56,7 @@ func returnHasherToPool(h *hasher) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash collapses a node down into a hash node.
|
// hash collapses a node down into a hash node.
|
||||||
func (h *hasher) hash(n node, force bool) node {
|
func (h *hasher) hash(n node, force bool) []byte {
|
||||||
// Return the cached hash if it's available
|
// Return the cached hash if it's available
|
||||||
if hash, _ := n.cache(); hash != nil {
|
if hash, _ := n.cache(); hash != nil {
|
||||||
return hash
|
return hash
|
||||||
|
|
@ -62,101 +64,110 @@ func (h *hasher) hash(n node, force bool) node {
|
||||||
// Trie not processed yet, walk the children
|
// Trie not processed yet, walk the children
|
||||||
switch n := n.(type) {
|
switch n := n.(type) {
|
||||||
case *shortNode:
|
case *shortNode:
|
||||||
collapsed := h.hashShortNodeChildren(n)
|
enc := h.encodeShortNode(n)
|
||||||
hashed := h.shortnodeToHash(collapsed, force)
|
if len(enc) < 32 && !force {
|
||||||
if hn, ok := hashed.(hashNode); ok {
|
// Nodes smaller than 32 bytes are embedded directly in their parent.
|
||||||
n.flags.hash = hn
|
// In such cases, return the raw encoded blob instead of the node hash.
|
||||||
} else {
|
// It's essential to deep-copy the node blob, as the underlying buffer
|
||||||
n.flags.hash = nil
|
// of enc will be reused later.
|
||||||
|
buf := make([]byte, len(enc))
|
||||||
|
copy(buf, enc)
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
return hashed
|
hash := h.hashData(enc)
|
||||||
|
n.flags.hash = hash
|
||||||
|
return hash
|
||||||
|
|
||||||
case *fullNode:
|
case *fullNode:
|
||||||
collapsed := h.hashFullNodeChildren(n)
|
enc := h.encodeFullNode(n)
|
||||||
hashed := h.fullnodeToHash(collapsed, force)
|
if len(enc) < 32 && !force {
|
||||||
if hn, ok := hashed.(hashNode); ok {
|
// Nodes smaller than 32 bytes are embedded directly in their parent.
|
||||||
n.flags.hash = hn
|
// In such cases, return the raw encoded blob instead of the node hash.
|
||||||
} else {
|
// It's essential to deep-copy the node blob, as the underlying buffer
|
||||||
n.flags.hash = nil
|
// of enc will be reused later.
|
||||||
|
buf := make([]byte, len(enc))
|
||||||
|
copy(buf, enc)
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
return hashed
|
hash := h.hashData(enc)
|
||||||
default:
|
n.flags.hash = hash
|
||||||
// Value and hash nodes don't have children, so they're left as were
|
return hash
|
||||||
|
|
||||||
|
case hashNode:
|
||||||
|
// hash nodes don't have children, so they're left as were
|
||||||
return n
|
return n
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// hashShortNodeChildren returns a copy of the supplied shortNode, with its child
|
|
||||||
// being replaced by either the hash or an embedded node if the child is small.
|
|
||||||
func (h *hasher) hashShortNodeChildren(n *shortNode) *shortNode {
|
|
||||||
var collapsed shortNode
|
|
||||||
collapsed.Key = hexToCompact(n.Key)
|
|
||||||
switch n.Val.(type) {
|
|
||||||
case *fullNode, *shortNode:
|
|
||||||
collapsed.Val = h.hash(n.Val, false)
|
|
||||||
default:
|
default:
|
||||||
collapsed.Val = n.Val
|
panic(fmt.Errorf("unexpected node type, %T", n))
|
||||||
}
|
}
|
||||||
return &collapsed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashFullNodeChildren returns a copy of the supplied fullNode, with its child
|
// encodeShortNode encodes the provided shortNode into the bytes. Notably, the
|
||||||
// being replaced by either the hash or an embedded node if the child is small.
|
// return slice must be deep-copied explicitly, otherwise the underlying slice
|
||||||
func (h *hasher) hashFullNodeChildren(n *fullNode) *fullNode {
|
// will be reused later.
|
||||||
var children [17]node
|
func (h *hasher) encodeShortNode(n *shortNode) []byte {
|
||||||
|
// Encode leaf node
|
||||||
|
if hasTerm(n.Key) {
|
||||||
|
var ln leafNodeEncoder
|
||||||
|
ln.Key = hexToCompact(n.Key)
|
||||||
|
ln.Val = n.Val.(valueNode)
|
||||||
|
ln.encode(h.encbuf)
|
||||||
|
return h.encodedBytes()
|
||||||
|
}
|
||||||
|
// Encode extension node
|
||||||
|
var en extNodeEncoder
|
||||||
|
en.Key = hexToCompact(n.Key)
|
||||||
|
en.Val = h.hash(n.Val, false)
|
||||||
|
en.encode(h.encbuf)
|
||||||
|
return h.encodedBytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
// fnEncoderPool is the pool for storing shared fullNode encoder to mitigate
|
||||||
|
// the significant memory allocation overhead.
|
||||||
|
var fnEncoderPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
var enc fullnodeEncoder
|
||||||
|
return &enc
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// encodeFullNode encodes the provided fullNode into the bytes. Notably, the
|
||||||
|
// return slice must be deep-copied explicitly, otherwise the underlying slice
|
||||||
|
// will be reused later.
|
||||||
|
func (h *hasher) encodeFullNode(n *fullNode) []byte {
|
||||||
|
fn := fnEncoderPool.Get().(*fullnodeEncoder)
|
||||||
|
fn.reset()
|
||||||
|
|
||||||
if h.parallel {
|
if h.parallel {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < 16; i++ {
|
for i := 0; i < 16; i++ {
|
||||||
if child := n.Children[i]; child != nil {
|
if n.Children[i] == nil {
|
||||||
wg.Add(1)
|
continue
|
||||||
go func(i int) {
|
|
||||||
hasher := newHasher(false)
|
|
||||||
children[i] = hasher.hash(child, false)
|
|
||||||
returnHasherToPool(hasher)
|
|
||||||
wg.Done()
|
|
||||||
}(i)
|
|
||||||
} else {
|
|
||||||
children[i] = nilValueNode
|
|
||||||
}
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
h := newHasher(false)
|
||||||
|
fn.Children[i] = h.hash(n.Children[i], false)
|
||||||
|
returnHasherToPool(h)
|
||||||
|
}(i)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
} else {
|
} else {
|
||||||
for i := 0; i < 16; i++ {
|
for i := 0; i < 16; i++ {
|
||||||
if child := n.Children[i]; child != nil {
|
if child := n.Children[i]; child != nil {
|
||||||
children[i] = h.hash(child, false)
|
fn.Children[i] = h.hash(child, false)
|
||||||
} else {
|
|
||||||
children[i] = nilValueNode
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if n.Children[16] != nil {
|
if n.Children[16] != nil {
|
||||||
children[16] = n.Children[16]
|
fn.Children[16] = n.Children[16].(valueNode)
|
||||||
}
|
}
|
||||||
return &fullNode{flags: nodeFlag{}, Children: children}
|
fn.encode(h.encbuf)
|
||||||
}
|
fnEncoderPool.Put(fn)
|
||||||
|
|
||||||
// shortNodeToHash computes the hash of the given shortNode. The shortNode must
|
return h.encodedBytes()
|
||||||
// first be collapsed, with its key converted to compact form. If the RLP-encoded
|
|
||||||
// node data is smaller than 32 bytes, the node itself is returned.
|
|
||||||
func (h *hasher) shortnodeToHash(n *shortNode, force bool) node {
|
|
||||||
n.encode(h.encbuf)
|
|
||||||
enc := h.encodedBytes()
|
|
||||||
|
|
||||||
if len(enc) < 32 && !force {
|
|
||||||
return n // Nodes smaller than 32 bytes are stored inside their parent
|
|
||||||
}
|
|
||||||
return h.hashData(enc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// fullnodeToHash computes the hash of the given fullNode. If the RLP-encoded
|
|
||||||
// node data is smaller than 32 bytes, the node itself is returned.
|
|
||||||
func (h *hasher) fullnodeToHash(n *fullNode, force bool) node {
|
|
||||||
n.encode(h.encbuf)
|
|
||||||
enc := h.encodedBytes()
|
|
||||||
|
|
||||||
if len(enc) < 32 && !force {
|
|
||||||
return n // Nodes smaller than 32 bytes are stored inside their parent
|
|
||||||
}
|
|
||||||
return h.hashData(enc)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// encodedBytes returns the result of the last encoding operation on h.encbuf.
|
// encodedBytes returns the result of the last encoding operation on h.encbuf.
|
||||||
|
|
@ -175,9 +186,10 @@ func (h *hasher) encodedBytes() []byte {
|
||||||
return h.tmp
|
return h.tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashData hashes the provided data
|
// hashData hashes the provided data. It is safe to modify the returned slice after
|
||||||
func (h *hasher) hashData(data []byte) hashNode {
|
// the function returns.
|
||||||
n := make(hashNode, 32)
|
func (h *hasher) hashData(data []byte) []byte {
|
||||||
|
n := make([]byte, 32)
|
||||||
h.sha.Reset()
|
h.sha.Reset()
|
||||||
h.sha.Write(data)
|
h.sha.Write(data)
|
||||||
h.sha.Read(n)
|
h.sha.Read(n)
|
||||||
|
|
@ -192,20 +204,17 @@ func (h *hasher) hashDataTo(dst, data []byte) {
|
||||||
h.sha.Read(dst)
|
h.sha.Read(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
// proofHash is used to construct trie proofs, and returns the 'collapsed'
|
// proofHash is used to construct trie proofs, returning the rlp-encoded node blobs.
|
||||||
// node (for later RLP encoding) as well as the hashed node -- unless the
|
// Note, only resolved node (shortNode or fullNode) is expected for proofing.
|
||||||
// node is smaller than 32 bytes, in which case it will be returned as is.
|
//
|
||||||
// This method does not do anything on value- or hash-nodes.
|
// It is safe to modify the returned slice after the function returns.
|
||||||
func (h *hasher) proofHash(original node) (collapsed, hashed node) {
|
func (h *hasher) proofHash(original node) []byte {
|
||||||
switch n := original.(type) {
|
switch n := original.(type) {
|
||||||
case *shortNode:
|
case *shortNode:
|
||||||
sn := h.hashShortNodeChildren(n)
|
return bytes.Clone(h.encodeShortNode(n))
|
||||||
return sn, h.shortnodeToHash(sn, false)
|
|
||||||
case *fullNode:
|
case *fullNode:
|
||||||
fn := h.hashFullNodeChildren(n)
|
return bytes.Clone(h.encodeFullNode(n))
|
||||||
return fn, h.fullnodeToHash(fn, false)
|
|
||||||
default:
|
default:
|
||||||
// Value and hash nodes don't have children, so they're left as were
|
panic(fmt.Errorf("unexpected node type, %T", original))
|
||||||
return n, n
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -240,9 +240,9 @@ func (it *nodeIterator) LeafProof() [][]byte {
|
||||||
|
|
||||||
for i, item := range it.stack[:len(it.stack)-1] {
|
for i, item := range it.stack[:len(it.stack)-1] {
|
||||||
// Gather nodes that end up as hash nodes (or the root)
|
// Gather nodes that end up as hash nodes (or the root)
|
||||||
node, hashed := hasher.proofHash(item.node)
|
enc := hasher.proofHash(item.node)
|
||||||
if _, ok := hashed.(hashNode); ok || i == 0 {
|
if len(enc) >= 32 || i == 0 {
|
||||||
proofs = append(proofs, nodeToBytes(node))
|
proofs = append(proofs, enc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return proofs
|
return proofs
|
||||||
|
|
|
||||||
|
|
@ -68,10 +68,6 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// nilValueNode is used when collapsing internal trie nodes for hashing, since
|
|
||||||
// unset children need to serialize correctly.
|
|
||||||
var nilValueNode = valueNode(nil)
|
|
||||||
|
|
||||||
// EncodeRLP encodes a full node into the consensus RLP format.
|
// EncodeRLP encodes a full node into the consensus RLP format.
|
||||||
func (n *fullNode) EncodeRLP(w io.Writer) error {
|
func (n *fullNode) EncodeRLP(w io.Writer) error {
|
||||||
eb := rlp.NewEncoderBuffer(w)
|
eb := rlp.NewEncoderBuffer(w)
|
||||||
|
|
|
||||||
|
|
@ -42,18 +42,29 @@ func (n *fullNode) encode(w rlp.EncoderBuffer) {
|
||||||
|
|
||||||
func (n *fullnodeEncoder) encode(w rlp.EncoderBuffer) {
|
func (n *fullnodeEncoder) encode(w rlp.EncoderBuffer) {
|
||||||
offset := w.List()
|
offset := w.List()
|
||||||
for _, c := range n.Children {
|
for i, c := range n.Children {
|
||||||
if c == nil {
|
if len(c) == 0 {
|
||||||
w.Write(rlp.EmptyString)
|
w.Write(rlp.EmptyString)
|
||||||
} else if len(c) < 32 {
|
|
||||||
w.Write(c) // rawNode
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteBytes(c) // hashNode
|
// valueNode or hashNode
|
||||||
|
if i == 16 || len(c) >= 32 {
|
||||||
|
w.WriteBytes(c)
|
||||||
|
} else {
|
||||||
|
w.Write(c) // rawNode
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.ListEnd(offset)
|
w.ListEnd(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *fullnodeEncoder) reset() {
|
||||||
|
for i, c := range n.Children {
|
||||||
|
if len(c) != 0 {
|
||||||
|
n.Children[i] = n.Children[i][:0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (n *shortNode) encode(w rlp.EncoderBuffer) {
|
func (n *shortNode) encode(w rlp.EncoderBuffer) {
|
||||||
offset := w.List()
|
offset := w.List()
|
||||||
w.WriteBytes(n.Key)
|
w.WriteBytes(n.Key)
|
||||||
|
|
@ -70,7 +81,7 @@ func (n *extNodeEncoder) encode(w rlp.EncoderBuffer) {
|
||||||
w.WriteBytes(n.Key)
|
w.WriteBytes(n.Key)
|
||||||
|
|
||||||
if n.Val == nil {
|
if n.Val == nil {
|
||||||
w.Write(rlp.EmptyString)
|
w.Write(rlp.EmptyString) // theoretically impossible to happen
|
||||||
} else if len(n.Val) < 32 {
|
} else if len(n.Val) < 32 {
|
||||||
w.Write(n.Val) // rawNode
|
w.Write(n.Val) // rawNode
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
@ -85,16 +86,9 @@ func (t *Trie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
|
||||||
defer returnHasherToPool(hasher)
|
defer returnHasherToPool(hasher)
|
||||||
|
|
||||||
for i, n := range nodes {
|
for i, n := range nodes {
|
||||||
var hn node
|
enc := hasher.proofHash(n)
|
||||||
n, hn = hasher.proofHash(n)
|
if len(enc) >= 32 || i == 0 {
|
||||||
if hash, ok := hn.(hashNode); ok || i == 0 {
|
proofDb.Put(crypto.Keccak256(enc), enc)
|
||||||
// If the node's database encoding is a hash (or is the
|
|
||||||
// root node), it becomes a proof element.
|
|
||||||
enc := nodeToBytes(n)
|
|
||||||
if !ok {
|
|
||||||
hash = hasher.hashData(enc)
|
|
||||||
}
|
|
||||||
proofDb.Put(hash, enc)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -626,7 +626,7 @@ func (t *Trie) resolveAndTrack(n hashNode, prefix []byte) (node, error) {
|
||||||
// Hash returns the root hash of the trie. It does not write to the
|
// Hash returns the root hash of the trie. It does not write to the
|
||||||
// database and can be used even if the trie doesn't have one.
|
// database and can be used even if the trie doesn't have one.
|
||||||
func (t *Trie) Hash() common.Hash {
|
func (t *Trie) Hash() common.Hash {
|
||||||
return common.BytesToHash(t.hashRoot().(hashNode))
|
return common.BytesToHash(t.hashRoot())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit collects all dirty nodes in the trie and replaces them with the
|
// Commit collects all dirty nodes in the trie and replaces them with the
|
||||||
|
|
@ -677,9 +677,9 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashRoot calculates the root hash of the given trie
|
// hashRoot calculates the root hash of the given trie
|
||||||
func (t *Trie) hashRoot() node {
|
func (t *Trie) hashRoot() []byte {
|
||||||
if t.root == nil {
|
if t.root == nil {
|
||||||
return hashNode(types.EmptyRootHash.Bytes())
|
return types.EmptyRootHash.Bytes()
|
||||||
}
|
}
|
||||||
// If the number of changes is below 100, we let one thread handle it
|
// If the number of changes is below 100, we let one thread handle it
|
||||||
h := newHasher(t.unhashed >= 100)
|
h := newHasher(t.unhashed >= 100)
|
||||||
|
|
|
||||||
|
|
@ -863,7 +863,6 @@ func (s *spongeDb) Flush() {
|
||||||
s.sponge.Write([]byte(key))
|
s.sponge.Write([]byte(key))
|
||||||
s.sponge.Write([]byte(s.values[key]))
|
s.sponge.Write([]byte(s.values[key]))
|
||||||
}
|
}
|
||||||
fmt.Println(len(s.keys))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb
|
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue