update code
This commit is contained in:
parent
2481bb4bf5
commit
7dfe3ea489
167
pkg/eth/api.go
167
pkg/eth/api.go
@ -19,19 +19,25 @@ package eth
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cerc-io/plugeth-statediff"
|
||||
ipld_direct_state "github.com/cerc-io/ipld-eth-statedb/direct_by_leaf"
|
||||
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state"
|
||||
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie"
|
||||
statediff "github.com/cerc-io/plugeth-statediff"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
@ -39,9 +45,9 @@ import (
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/holiman/uint256"
|
||||
|
||||
"github.com/cerc-io/ipld-eth-server/v5/pkg/log"
|
||||
ipld_direct_state "github.com/cerc-io/ipld-eth-statedb/direct_by_leaf"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -106,6 +112,25 @@ Headers and blocks
|
||||
|
||||
*/
|
||||
|
||||
// decodeHash parses a hex-encoded 32-byte hash. The input may optionally
|
||||
// be prefixed by 0x and can have a byte length up to 32.
|
||||
func decodeHash(s string) (h common.Hash, inputLength int, err error) {
|
||||
if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") {
|
||||
s = s[2:]
|
||||
}
|
||||
if (len(s) & 1) > 0 {
|
||||
s = "0" + s
|
||||
}
|
||||
b, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
return common.Hash{}, 0, errors.New("hex string invalid")
|
||||
}
|
||||
if len(b) > 32 {
|
||||
return common.Hash{}, len(b), errors.New("hex string too long, want at most 32 bytes")
|
||||
}
|
||||
return common.BytesToHash(b), len(b), nil
|
||||
}
|
||||
|
||||
// GetHeaderByNumber returns the requested canonical block header.
|
||||
// * When blockNr is -1 the chain head is returned.
|
||||
// * We cannot support pending block calls since we do not have an active miner
|
||||
@ -443,7 +468,7 @@ func (pea *PublicEthAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Contex
|
||||
// GetTransactionByHash returns the transaction for the given hash
|
||||
// eth ipld-eth-server cannot currently handle pending/tx_pool txs
|
||||
func (pea *PublicEthAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) {
|
||||
tx, blockHash, blockNumber, index, err := pea.B.GetTransaction(ctx, hash)
|
||||
_, tx, blockHash, blockNumber, index, err := pea.B.GetTransaction(ctx, hash)
|
||||
if tx != nil && err == nil {
|
||||
header, err := pea.B.HeaderByHash(ctx, blockHash)
|
||||
if err != nil {
|
||||
@ -465,7 +490,7 @@ func (pea *PublicEthAPI) GetTransactionByHash(ctx context.Context, hash common.H
|
||||
// GetRawTransactionByHash returns the bytes of the transaction for the given hash.
|
||||
func (pea *PublicEthAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
|
||||
// Retrieve a finalized transaction, or a pooled otherwise
|
||||
tx, _, _, _, err := pea.B.GetTransaction(ctx, hash)
|
||||
_, tx, _, _, _, err := pea.B.GetTransaction(ctx, hash)
|
||||
if tx != nil && err == nil {
|
||||
return tx.MarshalBinary()
|
||||
}
|
||||
@ -580,7 +605,7 @@ func (pea *PublicEthAPI) GetTransactionReceipt(ctx context.Context, hash common.
|
||||
|
||||
func (pea *PublicEthAPI) localGetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
|
||||
// TODO: this can be optimized for Postgres
|
||||
tx, blockHash, blockNumber, index, err := pea.B.GetTransaction(ctx, hash)
|
||||
_, tx, blockHash, blockNumber, index, err := pea.B.GetTransaction(ctx, hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -595,7 +620,11 @@ func (pea *PublicEthAPI) localGetTransactionReceipt(ctx context.Context, hash co
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = receipts.DeriveFields(pea.B.Config.ChainConfig, blockHash, blockNumber, block.BaseFee(), block.Transactions())
|
||||
var blobGasPrice *big.Int
|
||||
if excessBlobGas := block.ExcessBlobGas(); excessBlobGas != nil {
|
||||
blobGasPrice = eip4844.CalcBlobFee(*excessBlobGas)
|
||||
}
|
||||
err = receipts.DeriveFields(pea.B.Config.ChainConfig, blockHash, blockNumber, block.Time(), block.BaseFee(), blobGasPrice, block.Transactions())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -793,7 +822,7 @@ func (pea *PublicEthAPI) localGetBalance(ctx context.Context, address common.Add
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return (*hexutil.Big)(account.Balance), nil
|
||||
return (*hexutil.Big)(account.Balance.ToBig()), nil
|
||||
}
|
||||
|
||||
// GetStorageAt returns the storage from the state at the given address, key and
|
||||
@ -822,7 +851,7 @@ func (pea *PublicEthAPI) GetStorageAt(ctx context.Context, address common.Addres
|
||||
return value[:], nil
|
||||
}
|
||||
if pea.config.ProxyOnError {
|
||||
log.Warnxf(ctx, "Missing eth_getStorageAt(%s, %s, %s)", address.Hash().String(), key, blockNrOrHash.String())
|
||||
log.Warnxf(ctx, "Missing eth_getStorageAt(%s, %s, %s)", address, key, blockNrOrHash.String())
|
||||
var res hexutil.Bytes
|
||||
if err := pea.rpc.CallContext(ctx, &res, "eth_getStorageAt", address, key, blockNrOrHash); res != nil && err == nil {
|
||||
return res, nil
|
||||
@ -868,57 +897,95 @@ func (pea *PublicEthAPI) GetProof(ctx context.Context, address common.Address, s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// proofList implements ethdb.KeyValueWriter and collects the proofs as
|
||||
// hex-strings for delivery to rpc-caller.
|
||||
type proofList []string
|
||||
|
||||
func (n *proofList) Put(key []byte, value []byte) error {
|
||||
*n = append(*n, hexutil.Encode(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *proofList) Delete(key []byte) error {
|
||||
panic("not supported")
|
||||
}
|
||||
|
||||
// this continues to use ipfs-ethdb based geth StateDB as it requires trie access
|
||||
func (pea *PublicEthAPI) localGetProof(ctx context.Context, address common.Address, storageKeys []string, blockNrOrHash rpc.BlockNumberOrHash) (*AccountResult, error) {
|
||||
state, _, err := pea.B.IPLDTrieStateDBAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
||||
if state == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
storageTrie, err := state.StorageTrie(address)
|
||||
if storageTrie == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storageHash := types.EmptyRootHash
|
||||
codeHash := state.GetCodeHash(address)
|
||||
storageProof := make([]StorageResult, len(storageKeys))
|
||||
|
||||
// if we have a storageTrie, (which means the account exists), we can update the storagehash
|
||||
if storageTrie != nil {
|
||||
storageHash = storageTrie.Hash()
|
||||
} else {
|
||||
// no storageTrie means the account does not exist, so the codeHash is the hash of an empty bytearray.
|
||||
codeHash = crypto.Keccak256Hash(nil)
|
||||
}
|
||||
|
||||
// create the proof for the storageKeys
|
||||
for i, key := range storageKeys {
|
||||
if storageTrie != nil {
|
||||
proof, storageError := state.GetStorageProof(address, common.HexToHash(key))
|
||||
if storageError != nil {
|
||||
return nil, storageError
|
||||
}
|
||||
storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), toHexSlice(proof)}
|
||||
} else {
|
||||
storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}}
|
||||
var (
|
||||
keys = make([]common.Hash, len(storageKeys))
|
||||
keyLengths = make([]int, len(storageKeys))
|
||||
storageProof = make([]StorageResult, len(storageKeys))
|
||||
)
|
||||
// Deserialize all keys. This prevents state access on invalid input.
|
||||
for i, hexKey := range storageKeys {
|
||||
var err error
|
||||
keys[i], keyLengths[i], err = decodeHash(hexKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// create the accountProof
|
||||
accountProof, proofErr := state.GetProof(address)
|
||||
if proofErr != nil {
|
||||
return nil, proofErr
|
||||
statedb, header, err := pea.B.IPLDTrieStateDBAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
||||
// statedb, header, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
||||
if statedb == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
codeHash := statedb.GetCodeHash(address)
|
||||
storageRoot := statedb.GetStorageRoot(address)
|
||||
|
||||
if len(keys) > 0 {
|
||||
var storageTrie state.Trie
|
||||
if storageRoot != types.EmptyRootHash && storageRoot != (common.Hash{}) {
|
||||
id := trie.StorageTrieID(header.Root, crypto.Keccak256Hash(address.Bytes()), storageRoot)
|
||||
st, err := trie.NewStateTrie(id, statedb.Database().TrieDB())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storageTrie = st
|
||||
}
|
||||
// Create the proofs for the storageKeys.
|
||||
for i, key := range keys {
|
||||
// Output key encoding is a bit special: if the input was a 32-byte hash, it is
|
||||
// returned as such. Otherwise, we apply the QUANTITY encoding mandated by the
|
||||
// JSON-RPC spec for getProof. This behavior exists to preserve backwards
|
||||
// compatibility with older client versions.
|
||||
var outputKey string
|
||||
if keyLengths[i] != 32 {
|
||||
outputKey = hexutil.EncodeBig(key.Big())
|
||||
} else {
|
||||
outputKey = hexutil.Encode(key[:])
|
||||
}
|
||||
if storageTrie == nil {
|
||||
storageProof[i] = StorageResult{outputKey, &hexutil.Big{}, []string{}}
|
||||
continue
|
||||
}
|
||||
var proof proofList
|
||||
if err := storageTrie.Prove(crypto.Keccak256(key.Bytes()), &proof); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := (*hexutil.Big)(statedb.GetState(address, key).Big())
|
||||
storageProof[i] = StorageResult{outputKey, value, proof}
|
||||
}
|
||||
}
|
||||
// Create the accountProof.
|
||||
tr, err := trie.NewStateTrie(trie.StateTrieID(header.Root), statedb.Database().TrieDB())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var accountProof proofList
|
||||
if err := tr.Prove(crypto.Keccak256(address.Bytes()), &accountProof); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
balance := statedb.GetBalance(address).ToBig()
|
||||
return &AccountResult{
|
||||
Address: address,
|
||||
AccountProof: toHexSlice(accountProof),
|
||||
Balance: (*hexutil.Big)(state.GetBalance(address)),
|
||||
AccountProof: accountProof,
|
||||
Balance: (*hexutil.Big)(balance),
|
||||
CodeHash: codeHash,
|
||||
Nonce: hexutil.Uint64(state.GetNonce(address)),
|
||||
StorageHash: storageHash,
|
||||
Nonce: hexutil.Uint64(statedb.GetNonce(address)),
|
||||
StorageHash: storageRoot,
|
||||
StorageProof: storageProof,
|
||||
}, state.Error()
|
||||
}, statedb.Error()
|
||||
}
|
||||
|
||||
// GetSlice returns a slice of state or storage nodes from a provided root to a provided path and past it to a certain depth
|
||||
@ -989,7 +1056,7 @@ func (diff *StateOverride) Apply(state *ipld_direct_state.StateDB) error {
|
||||
}
|
||||
// Override account balance.
|
||||
if account.Balance != nil {
|
||||
state.SetBalance(addr, (*big.Int)(*account.Balance))
|
||||
state.SetBalance(addr, uint256.MustFromBig((*big.Int)(*account.Balance)))
|
||||
}
|
||||
if account.State != nil && account.StateDiff != nil {
|
||||
return fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
|
||||
|
||||
@ -19,7 +19,6 @@ package eth_api_test
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
"github.com/cerc-io/plugeth-statediff/indexer/interfaces"
|
||||
"github.com/cerc-io/plugeth-statediff/indexer/ipld"
|
||||
@ -44,8 +43,8 @@ import (
|
||||
var (
|
||||
randomAddr = common.HexToAddress("0x1C3ab14BBaD3D99F4203bd7a11aCB94882050E6f")
|
||||
randomHash = crypto.Keccak256Hash(randomAddr.Bytes())
|
||||
number = rpc.BlockNumber(test_helpers.BlockNumber.Int64())
|
||||
londonBlockNum = rpc.BlockNumber(test_helpers.LondonBlockNum.Int64())
|
||||
number = rpc.BlockNumber(test_helpers.BlockNumber1)
|
||||
londonBlockNum = rpc.BlockNumber(test_helpers.LondonBlockNum)
|
||||
wrongNumber = number + 1
|
||||
blockHash = test_helpers.MockBlock.Header().Hash()
|
||||
baseFee = test_helpers.MockLondonBlock.BaseFee()
|
||||
@ -293,9 +292,7 @@ var _ = Describe("API", func() {
|
||||
Describe("eth_blockNumber", func() {
|
||||
It("Retrieves the head block number", func() {
|
||||
bn := api.BlockNumber()
|
||||
ubn := (uint64)(bn)
|
||||
subn := strconv.FormatUint(ubn, 10)
|
||||
Expect(subn).To(Equal(test_helpers.LondonBlockNum.String()))
|
||||
Expect(bn).To(Equal(hexutil.Uint64(test_helpers.LondonBlockNum)))
|
||||
})
|
||||
})
|
||||
|
||||
@ -1104,20 +1101,20 @@ var _ = Describe("API", func() {
|
||||
It("Retrieves the eth balance for the provided account address at the block with the provided number", func() {
|
||||
bal, err := api.GetBalance(ctx, test_helpers.AccountAddresss, rpc.BlockNumberOrHashWithNumber(number))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(test_helpers.AccountBalance)))
|
||||
Expect(bal).To(Equal((*hexutil.Big)(test_helpers.AccountBalance.ToBig())))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddress, rpc.BlockNumberOrHashWithNumber(number))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal.ToInt().Cmp(common.Big0)).To(Equal(0))
|
||||
})
|
||||
It("Retrieves the eth balance for the provided account address at the block with the provided hash", func() {
|
||||
bal, err := api.GetBalance(ctx, test_helpers.AccountAddresss, rpc.BlockNumberOrHashWithHash(blockHash, true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(test_helpers.AccountBalance)))
|
||||
Expect(bal).To(Equal((*hexutil.Big)(test_helpers.AccountBalance.ToBig())))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddress, rpc.BlockNumberOrHashWithHash(blockHash, true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal.ToInt().Cmp(common.Big0)).To(Equal(0))
|
||||
})
|
||||
It("Retrieves the eth balance for the non-existing account address at the block with the provided hash", func() {
|
||||
bal, err := api.GetBalance(ctx, randomAddr, rpc.BlockNumberOrHashWithHash(blockHash, true))
|
||||
|
||||
@ -25,6 +25,12 @@ import (
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
validator "github.com/cerc-io/eth-ipfs-state-validator/v5/pkg"
|
||||
ipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0"
|
||||
ipld_direct_state "github.com/cerc-io/ipld-eth-statedb/direct_by_leaf"
|
||||
ipld_sql "github.com/cerc-io/ipld-eth-statedb/sql"
|
||||
ipld_trie_state "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state"
|
||||
ipld_trie "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie"
|
||||
"github.com/cerc-io/plugeth-statediff/indexer/ipld"
|
||||
"github.com/cerc-io/plugeth-statediff/utils"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
@ -42,15 +48,11 @@ import (
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
validator "github.com/cerc-io/eth-ipfs-state-validator/v5/pkg"
|
||||
ipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0"
|
||||
"github.com/cerc-io/ipld-eth-server/v5/pkg/log"
|
||||
"github.com/cerc-io/ipld-eth-server/v5/pkg/shared"
|
||||
ipld_direct_state "github.com/cerc-io/ipld-eth-statedb/direct_by_leaf"
|
||||
ipld_sql "github.com/cerc-io/ipld-eth-statedb/sql"
|
||||
ipld_trie_state "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -80,7 +82,7 @@ type Backend struct {
|
||||
// ethereum interfaces
|
||||
EthDB ethdb.Database
|
||||
// We use this state.Database for eth_call and any place we don't need trie access
|
||||
IpldDirectStateDatabase ipld_direct_state.StateDatabase
|
||||
IpldDirectStateDatabase ipld_direct_state.Database
|
||||
// We use this where state must be accessed by trie
|
||||
IpldTrieStateDatabase ipld_trie_state.Database
|
||||
|
||||
@ -115,7 +117,7 @@ func NewEthBackend(db *sqlx.DB, c *Config) (*Backend, error) {
|
||||
DB: db,
|
||||
Retriever: r,
|
||||
EthDB: ethDB,
|
||||
IpldDirectStateDatabase: ipld_direct_state.NewStateDatabase(driver),
|
||||
IpldDirectStateDatabase: ipld_direct_state.NewDatabase(driver),
|
||||
IpldTrieStateDatabase: ipld_trie_state.NewDatabase(ethDB),
|
||||
Config: c,
|
||||
}, nil
|
||||
@ -494,7 +496,7 @@ func (b *Backend) GetReceiptsByBlockHashAndNumber(tx *sqlx.Tx, hash common.Hash,
|
||||
|
||||
// GetTransaction retrieves a tx by hash
|
||||
// It also returns the blockhash, blocknumber, and tx index associated with the transaction
|
||||
func (b *Backend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
|
||||
func (b *Backend) GetTransaction(ctx context.Context, txHash common.Hash) (bool, *types.Transaction, common.Hash, uint64, uint64, error) {
|
||||
type txRes struct {
|
||||
Data []byte `db:"data"`
|
||||
HeaderID string `db:"header_id"`
|
||||
@ -503,22 +505,22 @@ func (b *Backend) GetTransaction(ctx context.Context, txHash common.Hash) (*type
|
||||
}
|
||||
var res = make([]txRes, 0)
|
||||
if err := b.DB.Select(&res, RetrieveRPCTransaction, txHash.String()); err != nil {
|
||||
return nil, common.Hash{}, 0, 0, err
|
||||
return false, nil, common.Hash{}, 0, 0, err
|
||||
}
|
||||
|
||||
if len(res) == 0 {
|
||||
return nil, common.Hash{}, 0, 0, errTxHashNotFound
|
||||
return false, nil, common.Hash{}, 0, 0, errTxHashNotFound
|
||||
} else if len(res) > 1 {
|
||||
// a transaction can be part of a only one canonical block
|
||||
return nil, common.Hash{}, 0, 0, errTxHashInMultipleBlocks
|
||||
return false, nil, common.Hash{}, 0, 0, errTxHashInMultipleBlocks
|
||||
}
|
||||
|
||||
var transaction types.Transaction
|
||||
if err := transaction.UnmarshalBinary(res[0].Data); err != nil {
|
||||
return nil, common.Hash{}, 0, 0, err
|
||||
return false, nil, common.Hash{}, 0, 0, err
|
||||
}
|
||||
|
||||
return &transaction, common.HexToHash(res[0].HeaderID), res[0].BlockNumber, res[0].Index, nil
|
||||
return true, &transaction, common.HexToHash(res[0].HeaderID), res[0].BlockNumber, res[0].Index, nil
|
||||
}
|
||||
|
||||
// GetReceipts retrieves receipts for provided block hash
|
||||
@ -778,7 +780,7 @@ func (b *Backend) GetAccountByHash(ctx context.Context, address common.Address,
|
||||
}
|
||||
return &types.StateAccount{
|
||||
Nonce: acctRecord.Nonce,
|
||||
Balance: balance,
|
||||
Balance: uint256.MustFromBig(balance),
|
||||
Root: common.HexToHash(acctRecord.Root),
|
||||
CodeHash: acctRecord.CodeHash,
|
||||
}, nil
|
||||
@ -917,7 +919,10 @@ func (b *Backend) GetSlice(path string, depth int, root common.Hash, storage boo
|
||||
var t ipld_trie_state.Trie
|
||||
var err error
|
||||
if storage {
|
||||
t, err = b.IpldTrieStateDatabase.OpenStorageTrie(common.Hash{}, common.Hash{}, root)
|
||||
// Note 1: once Verkle tries are used, this will be the same as state trie
|
||||
// Note 2: a dummy hash is passed as owner here, and is only used to signal to ipld-eth-statedb
|
||||
// that a storage, not state trie is being accessed
|
||||
t, err = b.IpldTrieStateDatabase.OpenStorageTrie(common.Hash{}, common.Hash{1}, root, nil)
|
||||
} else {
|
||||
t, err = b.IpldTrieStateDatabase.OpenTrie(root)
|
||||
}
|
||||
@ -929,21 +934,24 @@ func (b *Backend) GetSlice(path string, depth int, root common.Hash, storage boo
|
||||
// Convert the head hex path to a decoded byte path
|
||||
headPath := common.FromHex(path)
|
||||
|
||||
// Convert the Trie object to its concrete type for raw node access
|
||||
stateTrie := t.(*ipld_trie.StateTrie)
|
||||
|
||||
// Get Stem nodes
|
||||
err = b.getSliceStem(headPath, t, response, &metaData, storage)
|
||||
err = b.getSliceStem(headPath, stateTrie, response, &metaData, storage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get Head node
|
||||
err = b.getSliceHead(headPath, t, response, &metaData, storage)
|
||||
err = b.getSliceHead(headPath, stateTrie, response, &metaData, storage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if depth > 0 {
|
||||
// Get Slice nodes
|
||||
err = b.getSliceTrie(headPath, t, response, &metaData, depth, storage)
|
||||
err = b.getSliceTrie(headPath, stateTrie, response, &metaData, depth, storage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -954,7 +962,7 @@ func (b *Backend) GetSlice(path string, depth int, root common.Hash, storage boo
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (b *Backend) getSliceStem(headPath []byte, t ipld_trie_state.Trie, response *GetSliceResponse, metaData *metaDataFields, storage bool) error {
|
||||
func (b *Backend) getSliceStem(headPath []byte, t *ipld_trie.StateTrie, response *GetSliceResponse, metaData *metaDataFields, storage bool) error {
|
||||
leavesFetchTime := int64(0)
|
||||
totalStemStartTime := makeTimestamp()
|
||||
|
||||
@ -963,7 +971,7 @@ func (b *Backend) getSliceStem(headPath []byte, t ipld_trie_state.Trie, response
|
||||
// nodePath := make([]byte, len(headPath[:i]))
|
||||
nodePath := headPath[:i]
|
||||
|
||||
rawNode, _, err := t.TryGetNode(utils.HexToCompact(nodePath))
|
||||
rawNode, _, err := t.GetNode(utils.HexToCompact(nodePath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -1002,10 +1010,10 @@ func (b *Backend) getSliceStem(headPath []byte, t ipld_trie_state.Trie, response
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Backend) getSliceHead(headPath []byte, t ipld_trie_state.Trie, response *GetSliceResponse, metaData *metaDataFields, storage bool) error {
|
||||
func (b *Backend) getSliceHead(headPath []byte, t *ipld_trie.StateTrie, response *GetSliceResponse, metaData *metaDataFields, storage bool) error {
|
||||
totalHeadStartTime := makeTimestamp()
|
||||
|
||||
rawNode, _, err := t.TryGetNode(utils.HexToCompact(headPath))
|
||||
rawNode, _, err := t.GetNode(utils.HexToCompact(headPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -1043,7 +1051,10 @@ func (b *Backend) getSliceHead(headPath []byte, t ipld_trie_state.Trie, response
|
||||
}
|
||||
|
||||
func (b *Backend) getSliceTrie(headPath []byte, t ipld_trie_state.Trie, response *GetSliceResponse, metaData *metaDataFields, depth int, storage bool) error {
|
||||
it, timeTaken := getIteratorAtPath(t, headPath)
|
||||
it, timeTaken, err := getIteratorAtPath(t, headPath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
metaData.trieLoadingTime += timeTaken
|
||||
|
||||
leavesFetchTime := int64(0)
|
||||
|
||||
@ -317,21 +317,28 @@ func toBlockNumArg(number *big.Int) string {
|
||||
return hexutil.EncodeBig(number)
|
||||
}
|
||||
|
||||
func getIteratorAtPath(t state.Trie, startKey []byte) (trie.NodeIterator, int64) {
|
||||
func getIteratorAtPath(t state.Trie, startKey []byte) (trie.NodeIterator, int64, error) {
|
||||
startTime := makeTimestamp()
|
||||
var it trie.NodeIterator
|
||||
var err error
|
||||
|
||||
if len(startKey)%2 != 0 {
|
||||
// Zero-pad for odd-length keys, required by HexToKeyBytes()
|
||||
startKey = append(startKey, 0)
|
||||
it = t.NodeIterator(nodeiter.HexToKeyBytes(startKey))
|
||||
it, err = t.NodeIterator(nodeiter.HexToKeyBytes(startKey))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
} else {
|
||||
it = t.NodeIterator(nodeiter.HexToKeyBytes(startKey))
|
||||
it, err = t.NodeIterator(nodeiter.HexToKeyBytes(startKey))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
// Step to the required node (not required if original startKey was odd-length)
|
||||
it.Next(true)
|
||||
}
|
||||
|
||||
return it, makeTimestamp() - startTime
|
||||
return it, makeTimestamp() - startTime, nil
|
||||
}
|
||||
|
||||
func fillSliceNodeData(
|
||||
@ -351,7 +358,7 @@ func fillSliceNodeData(
|
||||
if node.NodeType == Leaf && !storage {
|
||||
stateLeafKey, storageRoot, code, err := extractContractAccountInfo(sdb, node, nodeElements)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("GetSlice account lookup error: %s", err.Error())
|
||||
return 0, fmt.Errorf("GetSlice account lookup error: %w", err)
|
||||
}
|
||||
|
||||
if len(code) > 0 {
|
||||
@ -387,7 +394,7 @@ func extractContractAccountInfo(sdb state.Database, node StateNode, nodeElements
|
||||
|
||||
// Extract codeHash and get code
|
||||
codeHash := common.BytesToHash(account.CodeHash)
|
||||
codeBytes, err := sdb.ContractCode(codeHash)
|
||||
codeBytes, err := sdb.ContractCode(common.Address{}, codeHash)
|
||||
if err != nil {
|
||||
return "", "", nil, err
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
|
||||
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie"
|
||||
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/triedb"
|
||||
)
|
||||
|
||||
// NodeType for explicitly setting type of node
|
||||
@ -73,7 +74,7 @@ type StorageNode struct {
|
||||
LeafKey []byte `json:"leafKey"`
|
||||
}
|
||||
|
||||
func ResolveNode(path []byte, node []byte, trieDB *trie.Database) (StateNode, []interface{}, error) {
|
||||
func ResolveNode(path []byte, node []byte, trieDB *triedb.Database) (StateNode, []interface{}, error) {
|
||||
var nodeElements []interface{}
|
||||
if err := rlp.DecodeBytes(node, &nodeElements); err != nil {
|
||||
return StateNode{}, nil, err
|
||||
@ -93,7 +94,7 @@ func ResolveNode(path []byte, node []byte, trieDB *trie.Database) (StateNode, []
|
||||
}
|
||||
|
||||
// ResolveNodeIt return the state diff node pointed by the iterator.
|
||||
func ResolveNodeIt(it trie.NodeIterator, trieDB *trie.Database) (StateNode, []interface{}, error) {
|
||||
func ResolveNodeIt(it trie.NodeIterator, trieDB *triedb.Database) (StateNode, []interface{}, error) {
|
||||
node, err := it.NodeBlob(), it.Error()
|
||||
if err != nil {
|
||||
return StateNode{}, nil, err
|
||||
|
||||
@ -1,8 +1,15 @@
|
||||
package eth_state_test
|
||||
|
||||
import (
|
||||
"github.com/cerc-io/ipld-eth-server/v5/pkg/eth"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/format"
|
||||
"github.com/onsi/gomega/types"
|
||||
|
||||
"github.com/cerc-io/ipld-eth-server/v5/pkg/eth"
|
||||
)
|
||||
|
||||
func CheckGetSliceResponse(sliceResponse eth.GetSliceResponse, expectedResponse eth.GetSliceResponse) {
|
||||
@ -11,3 +18,54 @@ func CheckGetSliceResponse(sliceResponse eth.GetSliceResponse, expectedResponse
|
||||
Expect(sliceResponse.Leaves).To(Equal(expectedResponse.Leaves))
|
||||
Expect(sliceResponse.MetaData.NodeStats).To(Equal(expectedResponse.MetaData.NodeStats))
|
||||
}
|
||||
|
||||
// EqualBigInt compares a hexutil.Big for equality with a big.Int or hexutil.Big value.
|
||||
// It is an error for both actual and expected to be nil. Use BeNil() instead.
|
||||
func EqualBigInt(expected *big.Int) types.GomegaMatcher {
|
||||
return &BigIntEqualMatcher{
|
||||
Expected: expected,
|
||||
}
|
||||
}
|
||||
|
||||
// EqualBigHex compares a hexutil.Big for equality with a big.Int or hexutil.Big value.
|
||||
// It is an error for both actual and expected to be nil. Use BeNil() instead.
|
||||
func EqualBigHex(expected *hexutil.Big) types.GomegaMatcher {
|
||||
return &BigIntEqualMatcher{
|
||||
Expected: expected.ToInt(),
|
||||
}
|
||||
}
|
||||
|
||||
type BigIntEqualMatcher struct {
|
||||
Expected *big.Int
|
||||
}
|
||||
|
||||
func (matcher *BigIntEqualMatcher) Match(actual interface{}) (success bool, err error) {
|
||||
if actual == nil && matcher.Expected == nil {
|
||||
return false, fmt.Errorf("Refusing to compare <nil> to <nil>.\nBe explicit and use BeNil() instead. This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.")
|
||||
}
|
||||
|
||||
var asInt *big.Int
|
||||
switch casted := actual.(type) {
|
||||
case *big.Int:
|
||||
asInt = casted
|
||||
case *hexutil.Big:
|
||||
asInt = (*big.Int)(casted)
|
||||
default:
|
||||
return false, fmt.Errorf("BigIntEqualMatcher expects a hexutil.Big or big.Int. Got:\n%s", format.Object(actual, 1))
|
||||
}
|
||||
return matcher.Expected.Cmp(asInt) == 0, nil
|
||||
}
|
||||
|
||||
func (matcher *BigIntEqualMatcher) FailureMessage(actual interface{}) (message string) {
|
||||
actualString, actualOK := actual.(string)
|
||||
expectedString := matcher.Expected.String()
|
||||
if actualOK {
|
||||
return format.MessageWithDiff(actualString, "to equal", expectedString)
|
||||
}
|
||||
|
||||
return format.Message(actual, "to equal", matcher.Expected)
|
||||
}
|
||||
|
||||
func (matcher *BigIntEqualMatcher) NegatedFailureMessage(actual interface{}) (message string) {
|
||||
return format.Message(actual, "not to equal", matcher.Expected)
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ var (
|
||||
parsedABI abi.ABI
|
||||
randomAddress = common.HexToAddress("0x9F4203bd7a11aCB94882050E6f1C3ab14BBaD3D9")
|
||||
randomHash = crypto.Keccak256Hash(randomAddress.Bytes())
|
||||
number = rpc.BlockNumber(test_helpers.BlockNumber.Int64())
|
||||
number = rpc.BlockNumber(test_helpers.BlockNumber1)
|
||||
|
||||
block1StateRoot = common.HexToHash("0xa1f614839ebdd58677df2c9d66a3e0acc9462acc49fad6006d0b6e5d2b98ed21")
|
||||
rootDataHashBlock1 = "a1f614839ebdd58677df2c9d66a3e0acc9462acc49fad6006d0b6e5d2b98ed21"
|
||||
@ -294,198 +294,198 @@ var _ = Describe("eth state reading tests", func() {
|
||||
It("Retrieves account balance by block number", func() {
|
||||
bal, err := api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithNumber(0))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock0))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock0))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithNumber(1))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithNumber(1))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt((common.Big0)))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithNumber(1))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt((common.Big0)))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithNumber(1))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithNumber(2))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithNumber(2))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithNumber(2))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithNumber(2))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithNumber(3))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithNumber(3))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock3))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock3))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithNumber(3))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithNumber(3))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithNumber(4))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithNumber(4))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock4))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock4))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithNumber(4))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithNumber(4))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithNumber(5))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock5))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock5))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithNumber(5))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock4))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock4))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithNumber(5))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithNumber(5))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
})
|
||||
It("Retrieves account balance by block hash", func() {
|
||||
bal, err := api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithHash(blocks[0].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock0))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock0))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithHash(blocks[1].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithHash(blocks[1].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt((common.Big0)))
|
||||
|
||||
_, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithHash(blocks[1].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt((common.Big0)))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithHash(blocks[1].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithHash(blocks[2].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithHash(blocks[2].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock2))
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithHash(blocks[2].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithHash(blocks[2].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithHash(blocks[3].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithHash(blocks[3].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock3))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock3))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithHash(blocks[3].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithHash(blocks[3].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithHash(blocks[4].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock1))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock1))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithHash(blocks[4].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock4))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock4))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithHash(blocks[4].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithHash(blocks[4].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithHash(blocks[5].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct1BalanceBlock5))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct1BalanceBlock5))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithHash(blocks[5].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedAcct2BalanceBlock4))
|
||||
Expect(bal).To(EqualBigHex(expectedAcct2BalanceBlock4))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithHash(blocks[5].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedContractBalance))
|
||||
Expect(bal).To(EqualBigHex(expectedContractBalance))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.TestBankAddress, rpc.BlockNumberOrHashWithHash(blocks[5].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal(expectedBankBalanceBlock2))
|
||||
Expect(bal).To(EqualBigHex(expectedBankBalanceBlock2))
|
||||
})
|
||||
It("Returns 0 if account balance not found by block number", func() {
|
||||
bal, err := api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithNumber(0))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt(common.Big0))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithNumber(0))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt(common.Big0))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithNumber(0))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt(common.Big0))
|
||||
})
|
||||
It("Returns 0 if account balance not found by block hash", func() {
|
||||
bal, err := api.GetBalance(ctx, test_helpers.Account1Addr, rpc.BlockNumberOrHashWithHash(blocks[0].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt(common.Big0))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.Account2Addr, rpc.BlockNumberOrHashWithHash(blocks[0].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt(common.Big0))
|
||||
|
||||
bal, err = api.GetBalance(ctx, test_helpers.ContractAddr, rpc.BlockNumberOrHashWithHash(blocks[0].Hash(), true))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(bal).To(Equal((*hexutil.Big)(common.Big0)))
|
||||
Expect(bal).To(EqualBigInt(common.Big0))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@ -31,6 +31,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/ipfs/go-cid"
|
||||
|
||||
"github.com/cerc-io/ipld-eth-server/v5/pkg/eth"
|
||||
@ -40,10 +41,11 @@ import (
|
||||
// Test variables
|
||||
var (
|
||||
// block data
|
||||
BlockNumber = big.NewInt(1)
|
||||
MockHeader = types.Header{
|
||||
BlockNumber1 = int64(1)
|
||||
BlockTime1 = uint64(0)
|
||||
MockHeader = types.Header{
|
||||
Time: 0,
|
||||
Number: new(big.Int).Set(BlockNumber),
|
||||
Number: big.NewInt(BlockNumber1),
|
||||
Root: common.HexToHash("0x0"),
|
||||
TxHash: common.HexToHash("0x0"),
|
||||
ReceiptHash: common.HexToHash("0x0"),
|
||||
@ -54,7 +56,7 @@ var (
|
||||
MockUncles = []*types.Header{
|
||||
{
|
||||
Time: 1,
|
||||
Number: new(big.Int).Add(BlockNumber, big.NewInt(1)),
|
||||
Number: big.NewInt(BlockNumber1 + 1),
|
||||
Root: common.HexToHash("0x1"),
|
||||
TxHash: common.HexToHash("0x1"),
|
||||
ReceiptHash: common.HexToHash("0x1"),
|
||||
@ -64,7 +66,7 @@ var (
|
||||
},
|
||||
{
|
||||
Time: 2,
|
||||
Number: new(big.Int).Add(BlockNumber, big.NewInt(2)),
|
||||
Number: big.NewInt(BlockNumber1 + 2),
|
||||
Root: common.HexToHash("0x2"),
|
||||
TxHash: common.HexToHash("0x2"),
|
||||
ReceiptHash: common.HexToHash("0x2"),
|
||||
@ -76,7 +78,7 @@ var (
|
||||
MockBlock = createNewBlock(&MockHeader, MockTransactions, MockUncles, MockReceipts, trie.NewEmpty(nil))
|
||||
MockChildHeader = types.Header{
|
||||
Time: 0,
|
||||
Number: new(big.Int).Add(BlockNumber, common.Big1),
|
||||
Number: big.NewInt(BlockNumber1 + 1),
|
||||
Root: common.HexToHash("0x0"),
|
||||
TxHash: common.HexToHash("0x0"),
|
||||
ReceiptHash: common.HexToHash("0x0"),
|
||||
@ -104,7 +106,7 @@ var (
|
||||
Address: Address,
|
||||
Topics: []common.Hash{mockTopic11, mockTopic12},
|
||||
Data: []byte{},
|
||||
BlockNumber: BlockNumber.Uint64(),
|
||||
BlockNumber: uint64(BlockNumber1),
|
||||
TxIndex: 0,
|
||||
Index: 0,
|
||||
}
|
||||
@ -112,7 +114,7 @@ var (
|
||||
Address: AnotherAddress,
|
||||
Topics: []common.Hash{mockTopic21, mockTopic22},
|
||||
Data: []byte{},
|
||||
BlockNumber: BlockNumber.Uint64(),
|
||||
BlockNumber: uint64(BlockNumber1),
|
||||
TxIndex: 1,
|
||||
Index: 1,
|
||||
}
|
||||
@ -120,7 +122,7 @@ var (
|
||||
Address: AnotherAddress1,
|
||||
Topics: []common.Hash{mockTopic31},
|
||||
Data: []byte{},
|
||||
BlockNumber: BlockNumber.Uint64(),
|
||||
BlockNumber: uint64(BlockNumber1),
|
||||
TxIndex: 2,
|
||||
Index: 2,
|
||||
}
|
||||
@ -129,7 +131,7 @@ var (
|
||||
Address: AnotherAddress1,
|
||||
Topics: []common.Hash{mockTopic41, mockTopic42, mockTopic43},
|
||||
Data: []byte{},
|
||||
BlockNumber: BlockNumber.Uint64(),
|
||||
BlockNumber: uint64(BlockNumber1),
|
||||
TxIndex: 2,
|
||||
Index: 3,
|
||||
}
|
||||
@ -137,7 +139,7 @@ var (
|
||||
Address: AnotherAddress1,
|
||||
Topics: []common.Hash{mockTopic51},
|
||||
Data: []byte{},
|
||||
BlockNumber: BlockNumber.Uint64(),
|
||||
BlockNumber: uint64(BlockNumber1),
|
||||
TxIndex: 2,
|
||||
Index: 4,
|
||||
}
|
||||
@ -145,7 +147,7 @@ var (
|
||||
Address: AnotherAddress2,
|
||||
Topics: []common.Hash{mockTopic61},
|
||||
Data: []byte{},
|
||||
BlockNumber: BlockNumber.Uint64(),
|
||||
BlockNumber: uint64(BlockNumber1),
|
||||
TxIndex: 3,
|
||||
Index: 5,
|
||||
}
|
||||
@ -217,7 +219,7 @@ var (
|
||||
ContractLeafKey = crypto.Keccak256(ContractAddress[:])
|
||||
ContractAccount = types.StateAccount{
|
||||
Nonce: uint64(1),
|
||||
Balance: big.NewInt(0),
|
||||
Balance: uint256.NewInt(0),
|
||||
CodeHash: CodeHash.Bytes(),
|
||||
Root: common.HexToHash(ContractRoot),
|
||||
}
|
||||
@ -229,7 +231,7 @@ var (
|
||||
})
|
||||
|
||||
nonce0 = uint64(0)
|
||||
AccountBalance = big.NewInt(1000)
|
||||
AccountBalance = uint256.NewInt(1000)
|
||||
AccountRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
|
||||
AccountCodeHash = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
|
||||
AccountAddresss = common.HexToAddress("0x0D3ab14BBaD3D99F4203bd7a11aCB94882050E7e")
|
||||
@ -290,21 +292,22 @@ var (
|
||||
StateNodes: MockStateNodes,
|
||||
}
|
||||
|
||||
LondonBlockNum = new(big.Int).Add(BlockNumber, big.NewInt(2))
|
||||
LondonBlockNum = (BlockNumber1 + 2)
|
||||
LondonBlockTime = BlockTime1 + 1
|
||||
MockLondonHeader = types.Header{
|
||||
Time: 0,
|
||||
Number: LondonBlockNum,
|
||||
Number: big.NewInt(LondonBlockNum),
|
||||
Root: common.HexToHash("0x00"),
|
||||
Difficulty: big.NewInt(5000000),
|
||||
Extra: []byte{},
|
||||
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||
}
|
||||
|
||||
MockLondonTransactions, MockLondonReceipts, _ = createDynamicTransactionsAndReceipts(LondonBlockNum)
|
||||
MockLondonTransactions, MockLondonReceipts, _ = createDynamicTransactionsAndReceipts(big.NewInt(LondonBlockNum), LondonBlockTime)
|
||||
MockLondonUncles = []*types.Header{
|
||||
{
|
||||
Time: 1,
|
||||
Number: new(big.Int).Add(BlockNumber, big.NewInt(1)),
|
||||
Number: big.NewInt(BlockNumber1 + 1),
|
||||
ParentHash: common.HexToHash("0x2"),
|
||||
Root: common.HexToHash("0x1"),
|
||||
TxHash: common.HexToHash("0x1"),
|
||||
@ -314,7 +317,7 @@ var (
|
||||
},
|
||||
{
|
||||
Time: 2,
|
||||
Number: new(big.Int).Add(BlockNumber, big.NewInt(1)),
|
||||
Number: big.NewInt(BlockNumber1 + 1),
|
||||
ParentHash: common.HexToHash("0x1"),
|
||||
Root: common.HexToHash("0x2"),
|
||||
TxHash: common.HexToHash("0x2"),
|
||||
@ -338,7 +341,7 @@ func createNewBlock(header *types.Header, txs []*types.Transaction, uncles []*ty
|
||||
}
|
||||
|
||||
// createDynamicTransactionsAndReceipts is a helper function to generate signed mock transactions and mock receipts with mock logs
|
||||
func createDynamicTransactionsAndReceipts(blockNumber *big.Int) (types.Transactions, types.Receipts, common.Address) {
|
||||
func createDynamicTransactionsAndReceipts(blockNumber *big.Int, blockTime uint64) (types.Transactions, types.Receipts, common.Address) {
|
||||
// make transactions
|
||||
config := *params.TestChainConfig
|
||||
config.LondonBlock = blockNumber
|
||||
@ -353,7 +356,7 @@ func createDynamicTransactionsAndReceipts(blockNumber *big.Int) (types.Transacti
|
||||
Data: []byte{},
|
||||
})
|
||||
|
||||
transactionSigner := types.MakeSigner(&config, blockNumber)
|
||||
transactionSigner := types.MakeSigner(&config, blockNumber, blockTime)
|
||||
mockCurve := elliptic.P256()
|
||||
mockPrvKey, err := ecdsa.GenerateKey(mockCurve, rand.Reader)
|
||||
if err != nil {
|
||||
@ -391,7 +394,7 @@ func createLegacyTransactionsAndReceipts() (types.Transactions, types.Receipts,
|
||||
trx2 := types.NewTransaction(1, AnotherAddress, big.NewInt(2000), 100, big.NewInt(200), []byte{})
|
||||
trx3 := types.NewContractCreation(2, big.NewInt(1500), 75, big.NewInt(150), ContractCode)
|
||||
trx4 := types.NewTransaction(3, AnotherAddress1, big.NewInt(2000), 100, big.NewInt(200), []byte{})
|
||||
transactionSigner := types.MakeSigner(params.MainnetChainConfig, new(big.Int).Set(BlockNumber))
|
||||
transactionSigner := types.MakeSigner(params.MainnetChainConfig, big.NewInt(BlockNumber1), 0)
|
||||
mockCurve := elliptic.P256()
|
||||
mockPrvKey, err := ecdsa.GenerateKey(mockCurve, rand.Reader)
|
||||
if err != nil {
|
||||
|
||||
@ -64,7 +64,7 @@ func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) {
|
||||
if err != nil {
|
||||
return hexutil.Big{}, err
|
||||
}
|
||||
return hexutil.Big(*state.GetBalance(a.address)), nil
|
||||
return hexutil.Big(*state.GetBalance(a.address).ToBig()), nil
|
||||
}
|
||||
|
||||
func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user