2023-03-07 13:26:19 +03:00
// Copyright 2023 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 ethapi
import (
2024-01-17 17:06:14 +03:00
"bytes"
2023-05-02 11:28:43 +03:00
"context"
"crypto/ecdsa"
2024-02-08 21:53:32 +03:00
"crypto/sha256"
2023-03-07 13:26:19 +03:00
"encoding/json"
2023-05-02 11:28:43 +03:00
"errors"
2023-07-13 13:47:32 +03:00
"fmt"
2024-09-06 12:31:00 +03:00
"maps"
2023-03-07 13:26:19 +03:00
"math/big"
2023-08-22 03:53:29 +03:00
"os"
"path/filepath"
2023-05-02 11:28:43 +03:00
"reflect"
2024-03-25 09:50:18 +03:00
"slices"
2024-09-06 12:31:00 +03:00
"strings"
2023-03-07 13:26:19 +03:00
"testing"
2023-05-02 11:28:43 +03:00
"time"
2023-03-07 13:26:19 +03:00
2023-05-02 11:28:43 +03:00
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
2024-01-17 17:06:14 +03:00
"github.com/ethereum/go-ethereum/accounts/keystore"
2023-03-07 13:26:19 +03:00
"github.com/ethereum/go-ethereum/common"
2023-05-02 11:28:43 +03:00
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
2023-08-22 03:53:29 +03:00
"github.com/ethereum/go-ethereum/consensus/beacon"
2023-05-02 11:28:43 +03:00
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
2023-03-07 13:26:19 +03:00
"github.com/ethereum/go-ethereum/core/types"
2023-05-02 11:28:43 +03:00
"github.com/ethereum/go-ethereum/core/vm"
2023-03-07 13:26:19 +03:00
"github.com/ethereum/go-ethereum/crypto"
2024-02-08 21:53:32 +03:00
"github.com/ethereum/go-ethereum/crypto/kzg4844"
2023-05-02 11:28:43 +03:00
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
2023-07-11 15:57:02 +03:00
"github.com/ethereum/go-ethereum/internal/blocktest"
2023-03-07 13:26:19 +03:00
"github.com/ethereum/go-ethereum/params"
2023-05-02 11:28:43 +03:00
"github.com/ethereum/go-ethereum/rpc"
2024-09-06 12:31:00 +03:00
"github.com/ethereum/go-ethereum/triedb"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
2023-03-07 13:26:19 +03:00
)
2023-08-22 03:53:29 +03:00
func testTransactionMarshal ( t * testing . T , tests [ ] txData , config * params . ChainConfig ) {
2023-03-07 13:26:19 +03:00
var (
signer = types . LatestSigner ( config )
key , _ = crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
)
2023-08-22 03:53:29 +03:00
2023-03-07 13:26:19 +03:00
for i , tt := range tests {
var tx2 types . Transaction
2023-05-29 15:52:08 +03:00
tx , err := types . SignNewTx ( key , signer , tt . Tx )
2023-03-07 13:26:19 +03:00
if err != nil {
t . Fatalf ( "test %d: signing failed: %v" , i , err )
}
// Regular transaction
if data , err := json . Marshal ( tx ) ; err != nil {
t . Fatalf ( "test %d: marshalling failed; %v" , i , err )
} else if err = tx2 . UnmarshalJSON ( data ) ; err != nil {
t . Fatalf ( "test %d: sunmarshal failed: %v" , i , err )
} else if want , have := tx . Hash ( ) , tx2 . Hash ( ) ; want != have {
t . Fatalf ( "test %d: stx changed, want %x have %x" , i , want , have )
}
2023-05-29 15:52:08 +03:00
// rpcTransaction
2023-04-21 12:52:02 +03:00
rpcTx := newRPCTransaction ( tx , common . Hash { } , 0 , 0 , 0 , nil , config )
2023-03-07 13:26:19 +03:00
if data , err := json . Marshal ( rpcTx ) ; err != nil {
t . Fatalf ( "test %d: marshalling failed; %v" , i , err )
} else if err = tx2 . UnmarshalJSON ( data ) ; err != nil {
t . Fatalf ( "test %d: unmarshal failed: %v" , i , err )
} else if want , have := tx . Hash ( ) , tx2 . Hash ( ) ; want != have {
t . Fatalf ( "test %d: tx changed, want %x have %x" , i , want , have )
2023-05-29 15:52:08 +03:00
} else {
want , have := tt . Want , string ( data )
require . JSONEqf ( t , want , have , "test %d: rpc json not match, want %s have %s" , i , want , have )
2023-03-07 13:26:19 +03:00
}
}
}
2023-08-22 03:53:29 +03:00
func TestTransaction_RoundTripRpcJSON ( t * testing . T ) {
2024-09-16 09:44:47 +03:00
t . Parallel ( )
2023-08-22 03:53:29 +03:00
var (
config = params . AllEthashProtocolChanges
tests = allTransactionTypes ( common . Address { 0xde , 0xad } , config )
)
testTransactionMarshal ( t , tests , config )
}
func TestTransactionBlobTx ( t * testing . T ) {
2024-09-16 09:44:47 +03:00
t . Parallel ( )
2023-08-22 03:53:29 +03:00
config := * params . TestChainConfig
config . ShanghaiTime = new ( uint64 )
config . CancunTime = new ( uint64 )
tests := allBlobTxs ( common . Address { 0xde , 0xad } , & config )
testTransactionMarshal ( t , tests , & config )
}
2023-05-29 15:52:08 +03:00
type txData struct {
Tx types . TxData
Want string
}
func allTransactionTypes ( addr common . Address , config * params . ChainConfig ) [ ] txData {
return [ ] txData {
{
Tx : & types . LegacyTx {
Nonce : 5 ,
GasPrice : big . NewInt ( 6 ) ,
Gas : 7 ,
To : & addr ,
Value : big . NewInt ( 8 ) ,
Data : [ ] byte { 0 , 1 , 2 , 3 , 4 } ,
V : big . NewInt ( 9 ) ,
R : big . NewInt ( 10 ) ,
S : big . NewInt ( 11 ) ,
} ,
2023-08-05 01:54:55 +03:00
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x7" ,
"gasPrice" : "0x6" ,
"hash" : "0x5f3240454cd09a5d8b1c5d651eefae7a339262875bcd2d0e6676f3d989967008" ,
"input" : "0x0001020304" ,
"nonce" : "0x5" ,
"to" : "0xdead000000000000000000000000000000000000" ,
"transactionIndex" : null ,
"value" : "0x8" ,
"type" : "0x0" ,
"chainId" : "0x539" ,
"v" : "0xa96" ,
"r" : "0xbc85e96592b95f7160825d837abb407f009df9ebe8f1b9158a4b8dd093377f75" ,
"s" : "0x1b55ea3af5574c536967b039ba6999ef6c89cf22fc04bcb296e0e8b0b9b576f5"
} ` ,
2023-05-29 15:52:08 +03:00
} , {
Tx : & types . LegacyTx {
Nonce : 5 ,
GasPrice : big . NewInt ( 6 ) ,
Gas : 7 ,
To : nil ,
Value : big . NewInt ( 8 ) ,
Data : [ ] byte { 0 , 1 , 2 , 3 , 4 } ,
V : big . NewInt ( 32 ) ,
R : big . NewInt ( 10 ) ,
S : big . NewInt ( 11 ) ,
} ,
2023-08-05 01:54:55 +03:00
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x7" ,
"gasPrice" : "0x6" ,
"hash" : "0x806e97f9d712b6cb7e781122001380a2837531b0fc1e5f5d78174ad4cb699873" ,
"input" : "0x0001020304" ,
"nonce" : "0x5" ,
"to" : null ,
"transactionIndex" : null ,
"value" : "0x8" ,
"type" : "0x0" ,
"chainId" : "0x539" ,
"v" : "0xa96" ,
"r" : "0x9dc28b267b6ad4e4af6fe9289668f9305c2eb7a3241567860699e478af06835a" ,
"s" : "0xa0b51a071aa9bed2cd70aedea859779dff039e3630ea38497d95202e9b1fec7"
} ` ,
2023-05-29 15:52:08 +03:00
} ,
{
Tx : & types . AccessListTx {
ChainID : config . ChainID ,
Nonce : 5 ,
GasPrice : big . NewInt ( 6 ) ,
Gas : 7 ,
To : & addr ,
Value : big . NewInt ( 8 ) ,
Data : [ ] byte { 0 , 1 , 2 , 3 , 4 } ,
AccessList : types . AccessList {
types . AccessTuple {
Address : common . Address { 0x2 } ,
StorageKeys : [ ] common . Hash { types . EmptyRootHash } ,
} ,
2023-03-07 13:26:19 +03:00
} ,
2023-05-29 15:52:08 +03:00
V : big . NewInt ( 32 ) ,
R : big . NewInt ( 10 ) ,
S : big . NewInt ( 11 ) ,
2023-03-07 13:26:19 +03:00
} ,
2023-08-05 01:54:55 +03:00
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x7" ,
"gasPrice" : "0x6" ,
"hash" : "0x121347468ee5fe0a29f02b49b4ffd1c8342bc4255146bb686cd07117f79e7129" ,
"input" : "0x0001020304" ,
"nonce" : "0x5" ,
"to" : "0xdead000000000000000000000000000000000000" ,
"transactionIndex" : null ,
"value" : "0x8" ,
"type" : "0x1" ,
"accessList" : [
{
"address" : "0x0200000000000000000000000000000000000000" ,
"storageKeys" : [
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
]
}
] ,
"chainId" : "0x539" ,
"v" : "0x0" ,
"r" : "0xf372ad499239ae11d91d34c559ffc5dab4daffc0069e03afcabdcdf231a0c16b" ,
"s" : "0x28573161d1f9472fa0fd4752533609e72f06414f7ab5588699a7141f65d2abf" ,
"yParity" : "0x0"
} ` ,
2023-05-29 15:52:08 +03:00
} , {
Tx : & types . AccessListTx {
ChainID : config . ChainID ,
Nonce : 5 ,
GasPrice : big . NewInt ( 6 ) ,
Gas : 7 ,
To : nil ,
Value : big . NewInt ( 8 ) ,
Data : [ ] byte { 0 , 1 , 2 , 3 , 4 } ,
AccessList : types . AccessList {
types . AccessTuple {
Address : common . Address { 0x2 } ,
StorageKeys : [ ] common . Hash { types . EmptyRootHash } ,
} ,
2023-03-07 13:26:19 +03:00
} ,
2023-05-29 15:52:08 +03:00
V : big . NewInt ( 32 ) ,
R : big . NewInt ( 10 ) ,
S : big . NewInt ( 11 ) ,
2023-03-07 13:26:19 +03:00
} ,
2023-08-05 01:54:55 +03:00
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x7" ,
"gasPrice" : "0x6" ,
"hash" : "0x067c3baebede8027b0f828a9d933be545f7caaec623b00684ac0659726e2055b" ,
"input" : "0x0001020304" ,
"nonce" : "0x5" ,
"to" : null ,
"transactionIndex" : null ,
"value" : "0x8" ,
"type" : "0x1" ,
"accessList" : [
{
"address" : "0x0200000000000000000000000000000000000000" ,
"storageKeys" : [
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
]
}
] ,
"chainId" : "0x539" ,
"v" : "0x1" ,
"r" : "0x542981b5130d4613897fbab144796cb36d3cb3d7807d47d9c7f89ca7745b085c" ,
"s" : "0x7425b9dd6c5deaa42e4ede35d0c4570c4624f68c28d812c10d806ffdf86ce63" ,
"yParity" : "0x1"
} ` ,
2023-05-29 15:52:08 +03:00
} , {
Tx : & types . DynamicFeeTx {
ChainID : config . ChainID ,
Nonce : 5 ,
GasTipCap : big . NewInt ( 6 ) ,
GasFeeCap : big . NewInt ( 9 ) ,
Gas : 7 ,
To : & addr ,
Value : big . NewInt ( 8 ) ,
Data : [ ] byte { 0 , 1 , 2 , 3 , 4 } ,
AccessList : types . AccessList {
types . AccessTuple {
Address : common . Address { 0x2 } ,
StorageKeys : [ ] common . Hash { types . EmptyRootHash } ,
} ,
2023-03-07 13:26:19 +03:00
} ,
2023-05-29 15:52:08 +03:00
V : big . NewInt ( 32 ) ,
R : big . NewInt ( 10 ) ,
S : big . NewInt ( 11 ) ,
2023-03-07 13:26:19 +03:00
} ,
2023-08-05 01:54:55 +03:00
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x7" ,
"gasPrice" : "0x9" ,
"maxFeePerGas" : "0x9" ,
"maxPriorityFeePerGas" : "0x6" ,
"hash" : "0xb63e0b146b34c3e9cb7fbabb5b3c081254a7ded6f1b65324b5898cc0545d79ff" ,
"input" : "0x0001020304" ,
"nonce" : "0x5" ,
"to" : "0xdead000000000000000000000000000000000000" ,
"transactionIndex" : null ,
"value" : "0x8" ,
"type" : "0x2" ,
"accessList" : [
{
"address" : "0x0200000000000000000000000000000000000000" ,
"storageKeys" : [
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
]
}
] ,
"chainId" : "0x539" ,
"v" : "0x1" ,
"r" : "0x3b167e05418a8932cd53d7578711fe1a76b9b96c48642402bb94978b7a107e80" ,
"s" : "0x22f98a332d15ea2cc80386c1ebaa31b0afebfa79ebc7d039a1e0074418301fef" ,
"yParity" : "0x1"
} ` ,
2023-05-29 15:52:08 +03:00
} , {
Tx : & types . DynamicFeeTx {
ChainID : config . ChainID ,
Nonce : 5 ,
GasTipCap : big . NewInt ( 6 ) ,
GasFeeCap : big . NewInt ( 9 ) ,
Gas : 7 ,
To : nil ,
Value : big . NewInt ( 8 ) ,
Data : [ ] byte { 0 , 1 , 2 , 3 , 4 } ,
AccessList : types . AccessList { } ,
V : big . NewInt ( 32 ) ,
R : big . NewInt ( 10 ) ,
S : big . NewInt ( 11 ) ,
} ,
2023-08-05 01:54:55 +03:00
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x7" ,
"gasPrice" : "0x9" ,
"maxFeePerGas" : "0x9" ,
"maxPriorityFeePerGas" : "0x6" ,
"hash" : "0xcbab17ee031a9d5b5a09dff909f0a28aedb9b295ac0635d8710d11c7b806ec68" ,
"input" : "0x0001020304" ,
"nonce" : "0x5" ,
"to" : null ,
"transactionIndex" : null ,
"value" : "0x8" ,
"type" : "0x2" ,
"accessList" : [ ] ,
"chainId" : "0x539" ,
"v" : "0x0" ,
"r" : "0x6446b8a682db7e619fc6b4f6d1f708f6a17351a41c7fbd63665f469bc78b41b9" ,
"s" : "0x7626abc15834f391a117c63450047309dbf84c5ce3e8e609b607062641e2de43" ,
"yParity" : "0x0"
} ` ,
2023-03-07 13:26:19 +03:00
} ,
}
}
2023-05-02 11:28:43 +03:00
2023-08-22 03:53:29 +03:00
func allBlobTxs ( addr common . Address , config * params . ChainConfig ) [ ] txData {
return [ ] txData {
{
Tx : & types . BlobTx {
Nonce : 6 ,
GasTipCap : uint256 . NewInt ( 1 ) ,
GasFeeCap : uint256 . NewInt ( 5 ) ,
Gas : 6 ,
To : addr ,
BlobFeeCap : uint256 . NewInt ( 1 ) ,
BlobHashes : [ ] common . Hash { { 1 } } ,
Value : new ( uint256 . Int ) ,
V : uint256 . NewInt ( 32 ) ,
R : uint256 . NewInt ( 10 ) ,
S : uint256 . NewInt ( 11 ) ,
} ,
Want : ` {
"blockHash" : null ,
"blockNumber" : null ,
"from" : "0x71562b71999873db5b286df957af199ec94617f7" ,
"gas" : "0x6" ,
"gasPrice" : "0x5" ,
"maxFeePerGas" : "0x5" ,
"maxPriorityFeePerGas" : "0x1" ,
"maxFeePerBlobGas" : "0x1" ,
"hash" : "0x1f2b59a20e61efc615ad0cbe936379d6bbea6f938aafaf35eb1da05d8e7f46a3" ,
"input" : "0x" ,
"nonce" : "0x6" ,
"to" : "0xdead000000000000000000000000000000000000" ,
"transactionIndex" : null ,
"value" : "0x0" ,
"type" : "0x3" ,
"accessList" : [ ] ,
"chainId" : "0x1" ,
"blobVersionedHashes" : [
"0x0100000000000000000000000000000000000000000000000000000000000000"
] ,
"v" : "0x0" ,
"r" : "0x618be8908e0e5320f8f3b48042a079fe5a335ebd4ed1422a7d2207cd45d872bc" ,
"s" : "0x27b2bc6c80e849a8e8b764d4549d8c2efac3441e73cf37054eb0a9b9f8e89b27" ,
"yParity" : "0x0"
} ` ,
} ,
}
}
2024-01-17 17:06:14 +03:00
func newTestAccountManager ( t * testing . T ) ( * accounts . Manager , accounts . Account ) {
var (
dir = t . TempDir ( )
am = accounts . NewManager ( & accounts . Config { InsecureUnlockAllowed : true } )
b = keystore . NewKeyStore ( dir , 2 , 1 )
testKey , _ = crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
)
acc , err := b . ImportECDSA ( testKey , "" )
if err != nil {
t . Fatalf ( "failed to create test account: %v" , err )
}
if err := b . Unlock ( acc , "" ) ; err != nil {
t . Fatalf ( "failed to unlock account: %v\n" , err )
}
am . AddBackend ( b )
return am , acc
}
2023-05-02 11:28:43 +03:00
type testBackend struct {
2023-05-29 15:52:08 +03:00
db ethdb . Database
chain * core . BlockChain
pending * types . Block
2024-01-17 17:06:14 +03:00
accman * accounts . Manager
acc accounts . Account
2023-05-02 11:28:43 +03:00
}
2023-08-22 03:53:29 +03:00
func newTestBackend ( t * testing . T , n int , gspec * core . Genesis , engine consensus . Engine , generator func ( i int , b * core . BlockGen ) ) * testBackend {
2023-05-02 11:28:43 +03:00
var (
cacheConfig = & core . CacheConfig {
TrieCleanLimit : 256 ,
TrieDirtyLimit : 256 ,
TrieTimeLimit : 5 * time . Minute ,
SnapshotLimit : 0 ,
TrieDirtyDisabled : true , // Archive mode
}
)
2024-01-17 17:06:14 +03:00
accman , acc := newTestAccountManager ( t )
2024-02-16 21:05:33 +03:00
gspec . Alloc [ acc . Address ] = types . Account { Balance : big . NewInt ( params . Ether ) }
2023-05-02 11:28:43 +03:00
// Generate blocks for testing
2023-07-13 13:47:32 +03:00
db , blocks , _ := core . GenerateChainWithGenesis ( gspec , engine , n , generator )
txlookupLimit := uint64 ( 0 )
2024-09-04 16:03:06 +03:00
chain , err := core . NewBlockChain ( db , cacheConfig , gspec , nil , engine , vm . Config { } , & txlookupLimit )
2023-05-02 11:28:43 +03:00
if err != nil {
t . Fatalf ( "failed to create tester chain: %v" , err )
}
if n , err := chain . InsertChain ( blocks ) ; err != nil {
t . Fatalf ( "block %d: failed to insert into chain: %v" , n , err )
}
2023-07-13 13:47:32 +03:00
2024-01-17 17:06:14 +03:00
backend := & testBackend { db : db , chain : chain , accman : accman , acc : acc }
2023-05-02 11:28:43 +03:00
return backend
}
2023-05-29 15:52:08 +03:00
func ( b * testBackend ) setPendingBlock ( block * types . Block ) {
b . pending = block
}
2023-05-02 11:28:43 +03:00
func ( b testBackend ) SyncProgress ( ) ethereum . SyncProgress { return ethereum . SyncProgress { } }
func ( b testBackend ) SuggestGasTipCap ( ctx context . Context ) ( * big . Int , error ) {
return big . NewInt ( 0 ) , nil
}
2024-04-22 13:17:06 +03:00
func ( b testBackend ) FeeHistory ( ctx context . Context , blockCount uint64 , lastBlock rpc . BlockNumber , rewardPercentiles [ ] float64 ) ( * big . Int , [ ] [ ] * big . Int , [ ] * big . Int , [ ] float64 , [ ] * big . Int , [ ] float64 , error ) {
return nil , nil , nil , nil , nil , nil , nil
2023-05-02 11:28:43 +03:00
}
2024-04-22 13:17:06 +03:00
func ( b testBackend ) BlobBaseFee ( ctx context . Context ) * big . Int { return new ( big . Int ) }
func ( b testBackend ) ChainDb ( ) ethdb . Database { return b . db }
func ( b testBackend ) AccountManager ( ) * accounts . Manager { return b . accman }
func ( b testBackend ) ExtRPCEnabled ( ) bool { return false }
func ( b testBackend ) RPCGasCap ( ) uint64 { return 10000000 }
func ( b testBackend ) RPCEVMTimeout ( ) time . Duration { return time . Second }
func ( b testBackend ) RPCTxFeeCap ( ) float64 { return 0 }
func ( b testBackend ) UnprotectedAllowed ( ) bool { return false }
func ( b testBackend ) SetHead ( number uint64 ) { }
2023-05-02 11:28:43 +03:00
func ( b testBackend ) HeaderByNumber ( ctx context . Context , number rpc . BlockNumber ) ( * types . Header , error ) {
if number == rpc . LatestBlockNumber {
return b . chain . CurrentBlock ( ) , nil
}
2023-05-29 15:52:08 +03:00
if number == rpc . PendingBlockNumber && b . pending != nil {
return b . pending . Header ( ) , nil
}
2023-05-02 11:28:43 +03:00
return b . chain . GetHeaderByNumber ( uint64 ( number ) ) , nil
}
func ( b testBackend ) HeaderByHash ( ctx context . Context , hash common . Hash ) ( * types . Header , error ) {
2023-05-29 15:52:08 +03:00
return b . chain . GetHeaderByHash ( hash ) , nil
2023-05-02 11:28:43 +03:00
}
func ( b testBackend ) HeaderByNumberOrHash ( ctx context . Context , blockNrOrHash rpc . BlockNumberOrHash ) ( * types . Header , error ) {
2023-05-29 15:52:08 +03:00
if blockNr , ok := blockNrOrHash . Number ( ) ; ok {
return b . HeaderByNumber ( ctx , blockNr )
}
if blockHash , ok := blockNrOrHash . Hash ( ) ; ok {
return b . HeaderByHash ( ctx , blockHash )
}
panic ( "unknown type rpc.BlockNumberOrHash" )
2023-05-02 11:28:43 +03:00
}
2024-09-06 12:31:00 +03:00
func ( b testBackend ) CurrentHeader ( ) * types . Header { return b . chain . CurrentHeader ( ) }
2023-05-29 15:52:08 +03:00
func ( b testBackend ) CurrentBlock ( ) * types . Header { return b . chain . CurrentBlock ( ) }
2023-05-02 11:28:43 +03:00
func ( b testBackend ) BlockByNumber ( ctx context . Context , number rpc . BlockNumber ) ( * types . Block , error ) {
if number == rpc . LatestBlockNumber {
head := b . chain . CurrentBlock ( )
return b . chain . GetBlock ( head . Hash ( ) , head . Number . Uint64 ( ) ) , nil
}
2023-05-29 15:52:08 +03:00
if number == rpc . PendingBlockNumber {
return b . pending , nil
}
2023-05-02 11:28:43 +03:00
return b . chain . GetBlockByNumber ( uint64 ( number ) ) , nil
}
func ( b testBackend ) BlockByHash ( ctx context . Context , hash common . Hash ) ( * types . Block , error ) {
2023-05-29 15:52:08 +03:00
return b . chain . GetBlockByHash ( hash ) , nil
2023-05-02 11:28:43 +03:00
}
func ( b testBackend ) BlockByNumberOrHash ( ctx context . Context , blockNrOrHash rpc . BlockNumberOrHash ) ( * types . Block , error ) {
if blockNr , ok := blockNrOrHash . Number ( ) ; ok {
return b . BlockByNumber ( ctx , blockNr )
}
2023-05-29 15:52:08 +03:00
if blockHash , ok := blockNrOrHash . Hash ( ) ; ok {
return b . BlockByHash ( ctx , blockHash )
}
panic ( "unknown type rpc.BlockNumberOrHash" )
2023-05-02 11:28:43 +03:00
}
func ( b testBackend ) GetBody ( ctx context . Context , hash common . Hash , number rpc . BlockNumber ) ( * types . Body , error ) {
return b . chain . GetBlock ( hash , uint64 ( number . Int64 ( ) ) ) . Body ( ) , nil
}
func ( b testBackend ) StateAndHeaderByNumber ( ctx context . Context , number rpc . BlockNumber ) ( * state . StateDB , * types . Header , error ) {
if number == rpc . PendingBlockNumber {
panic ( "pending state not implemented" )
}
header , err := b . HeaderByNumber ( ctx , number )
if err != nil {
return nil , nil , err
}
if header == nil {
return nil , nil , errors . New ( "header not found" )
}
stateDb , err := b . chain . StateAt ( header . Root )
return stateDb , header , err
}
func ( b testBackend ) StateAndHeaderByNumberOrHash ( ctx context . Context , blockNrOrHash rpc . BlockNumberOrHash ) ( * state . StateDB , * types . Header , error ) {
if blockNr , ok := blockNrOrHash . Number ( ) ; ok {
return b . StateAndHeaderByNumber ( ctx , blockNr )
}
panic ( "only implemented for number" )
}
2024-03-06 15:45:03 +03:00
func ( b testBackend ) Pending ( ) ( * types . Block , types . Receipts , * state . StateDB ) { panic ( "implement me" ) }
2023-05-02 11:28:43 +03:00
func ( b testBackend ) GetReceipts ( ctx context . Context , hash common . Hash ) ( types . Receipts , error ) {
2023-07-13 13:47:32 +03:00
header , err := b . HeaderByHash ( ctx , hash )
if header == nil || err != nil {
return nil , err
}
receipts := rawdb . ReadReceipts ( b . db , hash , header . Number . Uint64 ( ) , header . Time , b . chain . Config ( ) )
return receipts , nil
2023-05-02 11:28:43 +03:00
}
2023-05-29 15:52:08 +03:00
func ( b testBackend ) GetTd ( ctx context . Context , hash common . Hash ) * big . Int {
if b . pending != nil && hash == b . pending . Hash ( ) {
return nil
}
return big . NewInt ( 1 )
}
2023-11-24 14:26:42 +03:00
func ( b testBackend ) GetEVM ( ctx context . Context , msg * core . Message , state * state . StateDB , header * types . Header , vmConfig * vm . Config , blockContext * vm . BlockContext ) * vm . EVM {
2023-05-02 11:28:43 +03:00
if vmConfig == nil {
vmConfig = b . chain . GetVMConfig ( )
}
txContext := core . NewEVMTxContext ( msg )
context := core . NewEVMBlockContext ( header , b . chain , nil )
if blockContext != nil {
context = * blockContext
}
2023-11-24 14:26:42 +03:00
return vm . NewEVM ( context , txContext , state , b . chain . Config ( ) , * vmConfig )
2023-05-02 11:28:43 +03:00
}
func ( b testBackend ) SubscribeChainEvent ( ch chan <- core . ChainEvent ) event . Subscription {
panic ( "implement me" )
}
func ( b testBackend ) SubscribeChainHeadEvent ( ch chan <- core . ChainHeadEvent ) event . Subscription {
panic ( "implement me" )
}
func ( b testBackend ) SendTx ( ctx context . Context , signedTx * types . Transaction ) error {
panic ( "implement me" )
}
2024-01-22 23:05:18 +03:00
func ( b testBackend ) GetTransaction ( ctx context . Context , txHash common . Hash ) ( bool , * types . Transaction , common . Hash , uint64 , uint64 , error ) {
2023-07-13 13:47:32 +03:00
tx , blockHash , blockNumber , index := rawdb . ReadTransaction ( b . db , txHash )
2024-01-22 23:05:18 +03:00
return true , tx , blockHash , blockNumber , index , nil
2023-05-02 11:28:43 +03:00
}
func ( b testBackend ) GetPoolTransactions ( ) ( types . Transactions , error ) { panic ( "implement me" ) }
func ( b testBackend ) GetPoolTransaction ( txHash common . Hash ) * types . Transaction { panic ( "implement me" ) }
func ( b testBackend ) GetPoolNonce ( ctx context . Context , addr common . Address ) ( uint64 , error ) {
2024-01-17 17:06:14 +03:00
return 0 , nil
2023-05-02 11:28:43 +03:00
}
func ( b testBackend ) Stats ( ) ( pending int , queued int ) { panic ( "implement me" ) }
2023-06-16 15:29:40 +03:00
func ( b testBackend ) TxPoolContent ( ) ( map [ common . Address ] [ ] * types . Transaction , map [ common . Address ] [ ] * types . Transaction ) {
2023-05-02 11:28:43 +03:00
panic ( "implement me" )
}
2023-06-16 15:29:40 +03:00
func ( b testBackend ) TxPoolContentFrom ( addr common . Address ) ( [ ] * types . Transaction , [ ] * types . Transaction ) {
2023-05-02 11:28:43 +03:00
panic ( "implement me" )
}
func ( b testBackend ) SubscribeNewTxsEvent ( events chan <- core . NewTxsEvent ) event . Subscription {
panic ( "implement me" )
}
func ( b testBackend ) ChainConfig ( ) * params . ChainConfig { return b . chain . Config ( ) }
func ( b testBackend ) Engine ( ) consensus . Engine { return b . chain . Engine ( ) }
func ( b testBackend ) GetLogs ( ctx context . Context , blockHash common . Hash , number uint64 ) ( [ ] [ ] * types . Log , error ) {
panic ( "implement me" )
}
func ( b testBackend ) SubscribeRemovedLogsEvent ( ch chan <- core . RemovedLogsEvent ) event . Subscription {
panic ( "implement me" )
}
func ( b testBackend ) SubscribeLogsEvent ( ch chan <- [ ] * types . Log ) event . Subscription {
panic ( "implement me" )
}
func ( b testBackend ) BloomStatus ( ) ( uint64 , uint64 ) { panic ( "implement me" ) }
func ( b testBackend ) ServiceFilter ( ctx context . Context , session * bloombits . MatcherSession ) {
panic ( "implement me" )
}
func TestEstimateGas ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
accounts = newAccounts ( 2 )
genesis = & core . Genesis {
2024-01-17 17:06:14 +03:00
Config : params . MergedTestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc {
2023-05-02 11:28:43 +03:00
accounts [ 0 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
accounts [ 1 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
} ,
}
genBlocks = 10
signer = types . HomesteadSigner { }
randomAccounts = newAccounts ( 2 )
)
2024-01-17 17:06:14 +03:00
api := NewBlockChainAPI ( newTestBackend ( t , genBlocks , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
2023-05-02 11:28:43 +03:00
// Transfer from account[0] to account[1]
// value: 1000 wei
// fee: 0 wei
tx , _ := types . SignTx ( types . NewTx ( & types . LegacyTx { Nonce : uint64 ( i ) , To : & accounts [ 1 ] . addr , Value : big . NewInt ( 1000 ) , Gas : params . TxGas , GasPrice : b . BaseFee ( ) , Data : nil } ) , signer , accounts [ 0 ] . key )
b . AddTx ( tx )
2024-01-17 17:06:14 +03:00
b . SetPoS ( )
2023-05-02 11:28:43 +03:00
} ) )
var testSuite = [ ] struct {
blockNumber rpc . BlockNumber
call TransactionArgs
2023-08-07 17:03:57 +03:00
overrides StateOverride
2023-05-02 11:28:43 +03:00
expectErr error
want uint64
} {
// simple transfer on latest block
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : nil ,
want : 21000 ,
} ,
// simple transfer with insufficient funds on latest block
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & randomAccounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : core . ErrInsufficientFunds ,
want : 21000 ,
} ,
// empty create
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs { } ,
expectErr : nil ,
want : 53000 ,
} ,
2023-08-07 17:03:57 +03:00
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs { } ,
overrides : StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount { Balance : newRPCBalance ( new ( big . Int ) . Mul ( big . NewInt ( 1 ) , big . NewInt ( params . Ether ) ) ) } ,
} ,
expectErr : nil ,
want : 53000 ,
} ,
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
overrides : StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount { Balance : newRPCBalance ( big . NewInt ( 0 ) ) } ,
} ,
expectErr : core . ErrInsufficientFunds ,
} ,
2023-11-08 12:22:08 +03:00
// Test for a bug where the gas price was set to zero but the basefee non-zero
//
// contract BasefeeChecker {
// constructor() {
// require(tx.gasprice >= block.basefee);
// if (tx.gasprice > 0) {
// require(block.basefee > 0);
// }
// }
//}
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
Input : hex2Bytes ( "6080604052348015600f57600080fd5b50483a1015601c57600080fd5b60003a111560315760004811603057600080fd5b5b603f80603e6000396000f3fe6080604052600080fdfea264697066735822122060729c2cee02b10748fae5200f1c9da4661963354973d9154c13a8e9ce9dee1564736f6c63430008130033" ) ,
GasPrice : ( * hexutil . Big ) ( big . NewInt ( 1_000_000_000 ) ) , // Legacy as pricing
} ,
expectErr : nil ,
want : 67617 ,
} ,
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
Input : hex2Bytes ( "6080604052348015600f57600080fd5b50483a1015601c57600080fd5b60003a111560315760004811603057600080fd5b5b603f80603e6000396000f3fe6080604052600080fdfea264697066735822122060729c2cee02b10748fae5200f1c9da4661963354973d9154c13a8e9ce9dee1564736f6c63430008130033" ) ,
MaxFeePerGas : ( * hexutil . Big ) ( big . NewInt ( 1_000_000_000 ) ) , // 1559 gas pricing
} ,
expectErr : nil ,
want : 67617 ,
} ,
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
Input : hex2Bytes ( "6080604052348015600f57600080fd5b50483a1015601c57600080fd5b60003a111560315760004811603057600080fd5b5b603f80603e6000396000f3fe6080604052600080fdfea264697066735822122060729c2cee02b10748fae5200f1c9da4661963354973d9154c13a8e9ce9dee1564736f6c63430008130033" ) ,
GasPrice : nil , // No legacy gas pricing
MaxFeePerGas : nil , // No 1559 gas pricing
} ,
expectErr : nil ,
want : 67595 ,
} ,
2024-01-17 17:06:14 +03:00
// Blobs should have no effect on gas estimate
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
2024-05-27 09:39:39 +03:00
BlobHashes : [ ] common . Hash { { 0x01 , 0x22 } } ,
2024-01-17 17:06:14 +03:00
BlobFeeCap : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
} ,
want : 21000 ,
} ,
2023-05-02 11:28:43 +03:00
}
for i , tc := range testSuite {
2023-08-07 17:03:57 +03:00
result , err := api . EstimateGas ( context . Background ( ) , tc . call , & rpc . BlockNumberOrHash { BlockNumber : & tc . blockNumber } , & tc . overrides )
2023-05-02 11:28:43 +03:00
if tc . expectErr != nil {
if err == nil {
t . Errorf ( "test %d: want error %v, have nothing" , i , tc . expectErr )
continue
}
if ! errors . Is ( err , tc . expectErr ) {
t . Errorf ( "test %d: error mismatch, want %v, have %v" , i , tc . expectErr , err )
}
continue
}
if err != nil {
t . Errorf ( "test %d: want no error, have %v" , i , err )
continue
}
2023-11-28 23:31:47 +03:00
if float64 ( result ) > float64 ( tc . want ) * ( 1 + estimateGasErrorRatio ) {
2023-05-02 11:28:43 +03:00
t . Errorf ( "test %d, result mismatch, have\n%v\n, want\n%v\n" , i , uint64 ( result ) , tc . want )
}
}
}
func TestCall ( t * testing . T ) {
t . Parallel ( )
2024-07-23 15:54:35 +03:00
2023-05-02 11:28:43 +03:00
// Initialize test accounts
var (
accounts = newAccounts ( 3 )
2024-07-23 15:54:35 +03:00
dad = common . HexToAddress ( "0x0000000000000000000000000000000000000dad" )
2023-05-02 11:28:43 +03:00
genesis = & core . Genesis {
2024-01-17 17:06:14 +03:00
Config : params . MergedTestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc {
2023-05-02 11:28:43 +03:00
accounts [ 0 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
accounts [ 1 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
accounts [ 2 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
2024-07-23 15:54:35 +03:00
dad : {
Balance : big . NewInt ( params . Ether ) ,
Nonce : 1 ,
Storage : map [ common . Hash ] common . Hash {
common . Hash { } : common . HexToHash ( "0x0000000000000000000000000000000000000000000000000000000000000001" ) ,
} ,
} ,
2023-05-02 11:28:43 +03:00
} ,
}
genBlocks = 10
signer = types . HomesteadSigner { }
)
2024-01-17 17:06:14 +03:00
api := NewBlockChainAPI ( newTestBackend ( t , genBlocks , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
2023-05-02 11:28:43 +03:00
// Transfer from account[0] to account[1]
// value: 1000 wei
// fee: 0 wei
tx , _ := types . SignTx ( types . NewTx ( & types . LegacyTx { Nonce : uint64 ( i ) , To : & accounts [ 1 ] . addr , Value : big . NewInt ( 1000 ) , Gas : params . TxGas , GasPrice : b . BaseFee ( ) , Data : nil } ) , signer , accounts [ 0 ] . key )
b . AddTx ( tx )
2024-01-17 17:06:14 +03:00
b . SetPoS ( )
2023-05-02 11:28:43 +03:00
} ) )
randomAccounts := newAccounts ( 3 )
var testSuite = [ ] struct {
2024-09-06 12:31:00 +03:00
name string
2023-05-02 11:28:43 +03:00
blockNumber rpc . BlockNumber
overrides StateOverride
call TransactionArgs
blockOverrides BlockOverrides
expectErr error
want string
} {
// transfer on genesis
{
2024-09-06 12:31:00 +03:00
name : "transfer-on-genesis" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . BlockNumber ( 0 ) ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : nil ,
want : "0x" ,
} ,
// transfer on the head
{
2024-09-06 12:31:00 +03:00
name : "transfer-on-the-head" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . BlockNumber ( genBlocks ) ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : nil ,
want : "0x" ,
} ,
// transfer on a non-existent block, error expects
{
2024-09-06 12:31:00 +03:00
name : "transfer-non-existent-block" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . BlockNumber ( genBlocks + 1 ) ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : errors . New ( "header not found" ) ,
} ,
// transfer on the latest block
{
2024-09-06 12:31:00 +03:00
name : "transfer-latest-block" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 0 ] . addr ,
To : & accounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : nil ,
want : "0x" ,
} ,
// Call which can only succeed if state is state overridden
{
2024-09-06 12:31:00 +03:00
name : "state-override-success" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
overrides : StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount { Balance : newRPCBalance ( new ( big . Int ) . Mul ( big . NewInt ( 1 ) , big . NewInt ( params . Ether ) ) ) } ,
} ,
want : "0x" ,
} ,
// Invalid call without state overriding
{
2024-09-06 12:31:00 +03:00
name : "insufficient-funds-simple" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
expectErr : core . ErrInsufficientFunds ,
} ,
// Successful simple contract call
//
// // SPDX-License-Identifier: GPL-3.0
//
// pragma solidity >=0.7.0 <0.8.0;
//
// /**
// * @title Storage
// * @dev Store & retrieve value in a variable
// */
// contract Storage {
// uint256 public number;
// constructor() {
// number = block.number;
// }
// }
{
2024-09-06 12:31:00 +03:00
name : "simple-contract-call" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
Data : hex2Bytes ( "8381f58a" ) , // call number()
} ,
overrides : StateOverride {
randomAccounts [ 2 ] . addr : OverrideAccount {
Code : hex2Bytes ( "6080604052348015600f57600080fd5b506004361060285760003560e01c80638381f58a14602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea2646970667358221220eab35ffa6ab2adfe380772a48b8ba78e82a1b820a18fcb6f59aa4efb20a5f60064736f6c63430007040033" ) ,
2024-07-25 02:01:59 +03:00
StateDiff : map [ common . Hash ] common . Hash { { } : common . BigToHash ( big . NewInt ( 123 ) ) } ,
2023-05-02 11:28:43 +03:00
} ,
} ,
want : "0x000000000000000000000000000000000000000000000000000000000000007b" ,
} ,
// Block overrides should work
{
2024-09-06 12:31:00 +03:00
name : "block-override" ,
2023-05-02 11:28:43 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 1 ] . addr ,
Input : & hexutil . Bytes {
0x43 , // NUMBER
0x60 , 0x00 , 0x52 , // MSTORE offset 0
0x60 , 0x20 , 0x60 , 0x00 , 0xf3 ,
} ,
} ,
blockOverrides : BlockOverrides { Number : ( * hexutil . Big ) ( big . NewInt ( 11 ) ) } ,
want : "0x000000000000000000000000000000000000000000000000000000000000000b" ,
} ,
2024-09-06 12:31:00 +03:00
// Clear storage trie
{
name : "clear-storage-trie" ,
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 1 ] . addr ,
// Yul:
// object "Test" {
// code {
// let dad := 0x0000000000000000000000000000000000000dad
// if eq(balance(dad), 0) {
// revert(0, 0)
// }
// let slot := sload(0)
// mstore(0, slot)
// return(0, 32)
// }
// }
Input : hex2Bytes ( "610dad6000813103600f57600080fd5b6000548060005260206000f3" ) ,
} ,
overrides : StateOverride {
dad : OverrideAccount {
State : map [ common . Hash ] common . Hash { } ,
} ,
} ,
want : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
} ,
2024-01-17 17:06:14 +03:00
// Invalid blob tx
{
2024-09-06 12:31:00 +03:00
name : "invalid-blob-tx" ,
2024-01-17 17:06:14 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 1 ] . addr ,
Input : & hexutil . Bytes { 0x00 } ,
BlobHashes : [ ] common . Hash { } ,
} ,
expectErr : core . ErrBlobTxCreate ,
} ,
// BLOBHASH opcode
{
2024-09-06 12:31:00 +03:00
name : "blobhash-opcode" ,
2024-01-17 17:06:14 +03:00
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 1 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
2024-05-27 09:39:39 +03:00
BlobHashes : [ ] common . Hash { { 0x01 , 0x22 } } ,
2024-01-17 17:06:14 +03:00
BlobFeeCap : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
} ,
overrides : StateOverride {
randomAccounts [ 2 ] . addr : {
Code : hex2Bytes ( "60004960005260206000f3" ) ,
} ,
} ,
want : "0x0122000000000000000000000000000000000000000000000000000000000000" ,
} ,
2024-07-23 15:54:35 +03:00
// Clear the entire storage set
{
blockNumber : rpc . LatestBlockNumber ,
call : TransactionArgs {
From : & accounts [ 1 ] . addr ,
// Yul:
// object "Test" {
// code {
// let dad := 0x0000000000000000000000000000000000000dad
// if eq(balance(dad), 0) {
// revert(0, 0)
// }
// let slot := sload(0)
// mstore(0, slot)
// return(0, 32)
// }
// }
Input : hex2Bytes ( "610dad6000813103600f57600080fd5b6000548060005260206000f3" ) ,
} ,
overrides : StateOverride {
dad : OverrideAccount {
2024-07-26 06:02:37 +03:00
State : map [ common . Hash ] common . Hash { } ,
2024-07-23 15:54:35 +03:00
} ,
} ,
want : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
} ,
2023-05-02 11:28:43 +03:00
}
2024-09-06 12:31:00 +03:00
for _ , tc := range testSuite {
2023-09-26 15:22:11 +03:00
result , err := api . Call ( context . Background ( ) , tc . call , & rpc . BlockNumberOrHash { BlockNumber : & tc . blockNumber } , & tc . overrides , & tc . blockOverrides )
2023-05-02 11:28:43 +03:00
if tc . expectErr != nil {
if err == nil {
2024-09-06 12:31:00 +03:00
t . Errorf ( "test %s: want error %v, have nothing" , tc . name , tc . expectErr )
2023-05-02 11:28:43 +03:00
continue
}
if ! errors . Is ( err , tc . expectErr ) {
// Second try
if ! reflect . DeepEqual ( err , tc . expectErr ) {
2024-09-06 12:31:00 +03:00
t . Errorf ( "test %s: error mismatch, want %v, have %v" , tc . name , tc . expectErr , err )
2023-05-02 11:28:43 +03:00
}
}
continue
}
if err != nil {
2024-09-06 12:31:00 +03:00
t . Errorf ( "test %s: want no error, have %v" , tc . name , err )
2023-05-02 11:28:43 +03:00
continue
}
if ! reflect . DeepEqual ( result . String ( ) , tc . want ) {
2024-09-06 12:31:00 +03:00
t . Errorf ( "test %s, result mismatch, have\n%v\n, want\n%v\n" , tc . name , result . String ( ) , tc . want )
}
}
}
func TestSimulateV1 ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
accounts = newAccounts ( 3 )
fixedAccount = newTestAccount ( )
genBlocks = 10
signer = types . HomesteadSigner { }
cac = common . HexToAddress ( "0x0000000000000000000000000000000000000cac" )
bab = common . HexToAddress ( "0x0000000000000000000000000000000000000bab" )
coinbase = "0x000000000000000000000000000000000000ffff"
genesis = & core . Genesis {
Config : params . TestChainConfig ,
Alloc : types . GenesisAlloc {
accounts [ 0 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
accounts [ 1 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
accounts [ 2 ] . addr : { Balance : big . NewInt ( params . Ether ) } ,
// Yul:
// object "Test" {
// code {
// let dad := 0x0000000000000000000000000000000000000dad
// selfdestruct(dad)
// }
// }
cac : { Balance : big . NewInt ( params . Ether ) , Code : common . Hex2Bytes ( "610dad80ff" ) } ,
bab : {
Balance : big . NewInt ( 1 ) ,
// object "Test" {
// code {
// let value1 := sload(1)
// let value2 := sload(2)
//
// // Shift value1 by 128 bits to the left by multiplying it with 2^128
// value1 := mul(value1, 0x100000000000000000000000000000000)
//
// // Concatenate value1 and value2
// let concatenatedValue := add(value1, value2)
//
// // Store the result in memory and return it
// mstore(0, concatenatedValue)
// return(0, 0x20)
// }
// }
Code : common . FromHex ( "0x600154600254700100000000000000000000000000000000820291508082018060005260206000f3" ) ,
Storage : map [ common . Hash ] common . Hash {
common . BigToHash ( big . NewInt ( 1 ) ) : common . BigToHash ( big . NewInt ( 10 ) ) ,
common . BigToHash ( big . NewInt ( 2 ) ) : common . BigToHash ( big . NewInt ( 12 ) ) ,
} ,
} ,
} ,
2023-05-02 11:28:43 +03:00
}
2024-09-06 12:31:00 +03:00
sha256Address = common . BytesToAddress ( [ ] byte { 0x02 } )
)
api := NewBlockChainAPI ( newTestBackend ( t , genBlocks , genesis , ethash . NewFaker ( ) , func ( i int , b * core . BlockGen ) {
b . SetCoinbase ( common . HexToAddress ( coinbase ) )
// Transfer from account[0] to account[1]
// value: 1000 wei
// fee: 0 wei
tx , _ := types . SignTx ( types . NewTx ( & types . LegacyTx {
Nonce : uint64 ( i ) ,
To : & accounts [ 1 ] . addr ,
Value : big . NewInt ( 1000 ) ,
Gas : params . TxGas ,
GasPrice : b . BaseFee ( ) ,
Data : nil ,
} ) , signer , accounts [ 0 ] . key )
b . AddTx ( tx )
} ) )
var (
randomAccounts = newAccounts ( 4 )
latest = rpc . BlockNumberOrHashWithNumber ( rpc . LatestBlockNumber )
includeTransfers = true
validation = true
)
type log struct {
Address common . Address ` json:"address" `
Topics [ ] common . Hash ` json:"topics" `
Data hexutil . Bytes ` json:"data" `
BlockNumber hexutil . Uint64 ` json:"blockNumber" `
// Skip txHash
//TxHash common.Hash `json:"transactionHash" gencodec:"required"`
TxIndex hexutil . Uint ` json:"transactionIndex" `
//BlockHash common.Hash `json:"blockHash"`
Index hexutil . Uint ` json:"logIndex" `
}
type callErr struct {
Message string
Code int
}
type callRes struct {
ReturnValue string ` json:"returnData" `
Error callErr
Logs [ ] log
GasUsed string
Status string
}
type blockRes struct {
Number string
//Hash string
// Ignore timestamp
GasLimit string
GasUsed string
Miner string
BaseFeePerGas string
Calls [ ] callRes
}
var testSuite = [ ] struct {
name string
blocks [ ] simBlock
tag rpc . BlockNumberOrHash
includeTransfers * bool
validation * bool
expectErr error
want [ ] blockRes
} {
// State build-up over calls:
// First value transfer OK after state override.
// Second one should succeed because of first transfer.
{
name : "simple" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount { Balance : newRPCBalance ( big . NewInt ( 1000 ) ) } ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} , {
From : & randomAccounts [ 1 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} , {
To : & randomAccounts [ 3 ] . addr ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xf618" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} , {
// State build-up over blocks.
name : "simple-multi-block" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount { Balance : newRPCBalance ( big . NewInt ( 2000 ) ) } ,
} ,
Calls : [ ] TransactionArgs {
{
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} , {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 3 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
} ,
} , {
StateOverrides : & StateOverride {
randomAccounts [ 3 ] . addr : OverrideAccount { Balance : newRPCBalance ( big . NewInt ( 0 ) ) } ,
} ,
Calls : [ ] TransactionArgs {
{
From : & randomAccounts [ 1 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} ,
} ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xa410" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xc" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x5208" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} , {
// insufficient funds
name : "insufficient-funds" ,
tag : latest ,
blocks : [ ] simBlock { {
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
} } ,
} } ,
want : nil ,
expectErr : & invalidTxError { Message : fmt . Sprintf ( "err: insufficient funds for gas * price + value: address %s have 0 want 1000 (supplied gas 4712388)" , randomAccounts [ 0 ] . addr . String ( ) ) , Code : errCodeInsufficientFunds } ,
} , {
// EVM error
name : "evm-error" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : OverrideAccount { Code : hex2Bytes ( "f3" ) } ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x47e7c4" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
Error : callErr { Message : "stack underflow (0 <=> 2)" , Code : errCodeVMError } ,
GasUsed : "0x47e7c4" ,
Logs : [ ] log { } ,
Status : "0x0" ,
} } ,
} } ,
} , {
// Block overrides should work, each call is simulated on a different block number
name : "block-overrides" ,
tag : latest ,
blocks : [ ] simBlock { {
BlockOverrides : & BlockOverrides {
Number : ( * hexutil . Big ) ( big . NewInt ( 11 ) ) ,
FeeRecipient : & cac ,
} ,
Calls : [ ] TransactionArgs {
{
From : & accounts [ 0 ] . addr ,
Input : & hexutil . Bytes {
0x43 , // NUMBER
0x60 , 0x00 , 0x52 , // MSTORE offset 0
0x60 , 0x20 , 0x60 , 0x00 , 0xf3 , // RETURN
} ,
} ,
} ,
} , {
BlockOverrides : & BlockOverrides {
Number : ( * hexutil . Big ) ( big . NewInt ( 12 ) ) ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 1 ] . addr ,
Input : & hexutil . Bytes {
0x43 , // NUMBER
0x60 , 0x00 , 0x52 , // MSTORE offset 0
0x60 , 0x20 , 0x60 , 0x00 , 0xf3 ,
} ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xe891" ,
Miner : strings . ToLower ( cac . String ( ) ) ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x000000000000000000000000000000000000000000000000000000000000000b" ,
GasUsed : "0xe891" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xc" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xe891" ,
Miner : strings . ToLower ( cac . String ( ) ) ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x000000000000000000000000000000000000000000000000000000000000000c" ,
GasUsed : "0xe891" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Block numbers must be in order.
{
name : "block-number-order" ,
tag : latest ,
blocks : [ ] simBlock { {
BlockOverrides : & BlockOverrides {
Number : ( * hexutil . Big ) ( big . NewInt ( 12 ) ) ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 1 ] . addr ,
Input : & hexutil . Bytes {
0x43 , // NUMBER
0x60 , 0x00 , 0x52 , // MSTORE offset 0
0x60 , 0x20 , 0x60 , 0x00 , 0xf3 , // RETURN
} ,
} } ,
} , {
BlockOverrides : & BlockOverrides {
Number : ( * hexutil . Big ) ( big . NewInt ( 11 ) ) ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
Input : & hexutil . Bytes {
0x43 , // NUMBER
0x60 , 0x00 , 0x52 , // MSTORE offset 0
0x60 , 0x20 , 0x60 , 0x00 , 0xf3 , // RETURN
} ,
} } ,
} } ,
want : [ ] blockRes { } ,
expectErr : & invalidBlockNumberError { message : "block numbers must be in order: 11 <= 12" } ,
} ,
// Test on solidity storage example. Set value in one call, read in next.
{
name : "storage-contract" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : OverrideAccount {
Code : hex2Bytes ( "608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100d9565b60405180910390f35b610073600480360381019061006e919061009d565b61007e565b005b60008054905090565b8060008190555050565b60008135905061009781610103565b92915050565b6000602082840312156100b3576100b26100fe565b5b60006100c184828501610088565b91505092915050565b6100d3816100f4565b82525050565b60006020820190506100ee60008301846100ca565b92915050565b6000819050919050565b600080fd5b61010c816100f4565b811461011757600080fd5b5056fea2646970667358221220404e37f487a89a932dca5e77faaf6ca2de3b991f93d230604b1b8daaef64766264736f6c63430008070033" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
// Set value to 5
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
Input : hex2Bytes ( "6057361d0000000000000000000000000000000000000000000000000000000000000005" ) ,
} , {
// Read value
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
Input : hex2Bytes ( "2e64cec1" ) ,
} ,
} ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x10683" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0xaacc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x0000000000000000000000000000000000000000000000000000000000000005" ,
GasUsed : "0x5bb7" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Test logs output.
{
name : "logs" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : OverrideAccount {
// Yul code:
// object "Test" {
// code {
// let hash:u256 := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
// log1(0, 0, hash)
// return (0, 0)
// }
// }
Code : hex2Bytes ( "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80600080a1600080f3" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x5508" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
Logs : [ ] log { {
Address : randomAccounts [ 2 ] . addr ,
Topics : [ ] common . Hash { common . HexToHash ( "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ) } ,
BlockNumber : hexutil . Uint64 ( 11 ) ,
Data : hexutil . Bytes { } ,
} } ,
GasUsed : "0x5508" ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Test ecrecover override
{
name : "ecrecover-override" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : OverrideAccount {
// Yul code that returns ecrecover(0, 0, 0, 0).
// object "Test" {
// code {
// // Free memory pointer
// let free_ptr := mload(0x40)
//
// // Initialize inputs with zeros
// mstore(free_ptr, 0) // Hash
// mstore(add(free_ptr, 0x20), 0) // v
// mstore(add(free_ptr, 0x40), 0) // r
// mstore(add(free_ptr, 0x60), 0) // s
//
// // Call ecrecover precompile (at address 1) with all 0 inputs
// let success := staticcall(gas(), 1, free_ptr, 0x80, free_ptr, 0x20)
//
// // Check if the call was successful
// if eq(success, 0) {
// revert(0, 0)
// }
//
// // Return the recovered address
// return(free_ptr, 0x14)
// }
// }
Code : hex2Bytes ( "6040516000815260006020820152600060408201526000606082015260208160808360015afa60008103603157600080fd5b601482f3" ) ,
} ,
common . BytesToAddress ( [ ] byte { 0x01 } ) : OverrideAccount {
// Yul code that returns the address of the caller.
// object "Test" {
// code {
// let c := caller()
// mstore(0, c)
// return(0xc, 0x14)
// }
// }
Code : hex2Bytes ( "33806000526014600cf3" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x52f6" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
// Caller is in this case the contract that invokes ecrecover.
ReturnValue : strings . ToLower ( randomAccounts [ 2 ] . addr . String ( ) ) ,
GasUsed : "0x52f6" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Test moving the sha256 precompile.
{
name : "precompile-move" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
sha256Address : OverrideAccount {
// Yul code that returns the calldata.
// object "Test" {
// code {
// let size := calldatasize() // Get the size of the calldata
//
// // Allocate memory to store the calldata
// let memPtr := msize()
//
// // Copy calldata to memory
// calldatacopy(memPtr, 0, size)
//
// // Return the calldata from memory
// return(memPtr, size)
// }
// }
Code : hex2Bytes ( "365981600082378181f3" ) ,
MovePrecompileTo : & randomAccounts [ 2 ] . addr ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
Input : hex2Bytes ( "0000000000000000000000000000000000000000000000000000000000000001" ) ,
} , {
From : & randomAccounts [ 0 ] . addr ,
To : & sha256Address ,
Input : hex2Bytes ( "0000000000000000000000000000000000000000000000000000000000000001" ) ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xa58c" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5" ,
GasUsed : "0x52dc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x0000000000000000000000000000000000000000000000000000000000000001" ,
GasUsed : "0x52b0" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Test ether transfers.
{
name : "transfer-logs" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount {
Balance : newRPCBalance ( big . NewInt ( 100 ) ) ,
// Yul code that transfers 100 wei to address passed in calldata:
// object "Test" {
// code {
// let recipient := shr(96, calldataload(0))
// let value := 100
// let success := call(gas(), recipient, value, 0, 0, 0, 0)
// if eq(success, 0) {
// revert(0, 0)
// }
// }
// }
Code : hex2Bytes ( "60003560601c606460008060008084865af160008103601d57600080fd5b505050" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 0 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 50 ) ) ,
Input : hex2Bytes ( strings . TrimPrefix ( fixedAccount . addr . String ( ) , "0x" ) ) ,
} } ,
} } ,
includeTransfers : & includeTransfers ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x77dc" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x77dc" ,
Logs : [ ] log { {
Address : transferAddress ,
Topics : [ ] common . Hash {
transferTopic ,
addressToHash ( accounts [ 0 ] . addr ) ,
addressToHash ( randomAccounts [ 0 ] . addr ) ,
} ,
Data : hexutil . Bytes ( common . BigToHash ( big . NewInt ( 50 ) ) . Bytes ( ) ) ,
BlockNumber : hexutil . Uint64 ( 11 ) ,
} , {
Address : transferAddress ,
Topics : [ ] common . Hash {
transferTopic ,
addressToHash ( randomAccounts [ 0 ] . addr ) ,
addressToHash ( fixedAccount . addr ) ,
} ,
Data : hexutil . Bytes ( common . BigToHash ( big . NewInt ( 100 ) ) . Bytes ( ) ) ,
BlockNumber : hexutil . Uint64 ( 11 ) ,
Index : hexutil . Uint ( 1 ) ,
} } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Tests selfdestructed contract.
{
name : "selfdestruct" ,
tag : latest ,
blocks : [ ] simBlock { {
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & cac ,
} , {
From : & accounts [ 0 ] . addr ,
// Check that cac is selfdestructed and balance transferred to dad.
// object "Test" {
// code {
// let cac := 0x0000000000000000000000000000000000000cac
// let dad := 0x0000000000000000000000000000000000000dad
// if gt(balance(cac), 0) {
// revert(0, 0)
// }
// if gt(extcodesize(cac), 0) {
// revert(0, 0)
// }
// if eq(balance(dad), 0) {
// revert(0, 0)
// }
// }
// }
Input : hex2Bytes ( "610cac610dad600082311115601357600080fd5b6000823b1115602157600080fd5b6000813103602e57600080fd5b5050" ) ,
} } ,
} , {
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
Input : hex2Bytes ( "610cac610dad600082311115601357600080fd5b6000823b1115602157600080fd5b6000813103602e57600080fd5b5050" ) ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x1b83f" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0xd166" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0xe6d9" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xc" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xe6d9" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0xe6d9" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Enable validation checks.
{
name : "validation-checks" ,
tag : latest ,
blocks : [ ] simBlock { {
Calls : [ ] TransactionArgs { {
From : & accounts [ 2 ] . addr ,
To : & cac ,
Nonce : newUint64 ( 2 ) ,
} } ,
} } ,
validation : & validation ,
want : nil ,
expectErr : & invalidTxError { Message : fmt . Sprintf ( "err: nonce too high: address %s, tx: 2 state: 0 (supplied gas 4712388)" , accounts [ 2 ] . addr ) , Code : errCodeNonceTooHigh } ,
} ,
// Contract sends tx in validation mode.
{
name : "validation-checks-from-contract" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : OverrideAccount {
Balance : newRPCBalance ( big . NewInt ( 2098640803896784 ) ) ,
Code : hex2Bytes ( "00" ) ,
Nonce : newUint64 ( 1 ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 2 ] . addr ,
To : & cac ,
Nonce : newUint64 ( 1 ) ,
MaxFeePerGas : newInt ( 233138868 ) ,
MaxPriorityFeePerGas : newInt ( 1 ) ,
} } ,
} } ,
validation : & validation ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xd166" ,
Miner : coinbase ,
BaseFeePerGas : "0xde56ab3" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0xd166" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Successful validation
{
name : "validation-checks-success" ,
tag : latest ,
blocks : [ ] simBlock { {
BlockOverrides : & BlockOverrides {
BaseFeePerGas : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
} ,
StateOverrides : & StateOverride {
randomAccounts [ 0 ] . addr : OverrideAccount { Balance : newRPCBalance ( big . NewInt ( 10000000 ) ) } ,
} ,
Calls : [ ] TransactionArgs { {
From : & randomAccounts [ 0 ] . addr ,
To : & randomAccounts [ 1 ] . addr ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1000 ) ) ,
MaxFeePerGas : ( * hexutil . Big ) ( big . NewInt ( 2 ) ) ,
} } ,
} } ,
validation : & validation ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x5208" ,
Miner : coinbase ,
BaseFeePerGas : "0x1" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x5208" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
// Clear storage.
{
name : "clear-storage" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : {
Code : newBytes ( genesis . Alloc [ bab ] . Code ) ,
StateDiff : map [ common . Hash ] common . Hash {
common . BigToHash ( big . NewInt ( 1 ) ) : common . BigToHash ( big . NewInt ( 2 ) ) ,
common . BigToHash ( big . NewInt ( 2 ) ) : common . BigToHash ( big . NewInt ( 3 ) ) ,
} ,
} ,
bab : {
State : map [ common . Hash ] common . Hash {
common . BigToHash ( big . NewInt ( 1 ) ) : common . BigToHash ( big . NewInt ( 1 ) ) ,
} ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
} , {
From : & accounts [ 0 ] . addr ,
To : & bab ,
} } ,
} , {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : {
State : map [ common . Hash ] common . Hash {
common . BigToHash ( big . NewInt ( 1 ) ) : common . BigToHash ( big . NewInt ( 5 ) ) ,
} ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xc542" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x0000000000000000000000000000000200000000000000000000000000000003" ,
GasUsed : "0x62a1" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x0000000000000000000000000000000100000000000000000000000000000000" ,
GasUsed : "0x62a1" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xc" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x62a1" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x0000000000000000000000000000000500000000000000000000000000000000" ,
GasUsed : "0x62a1" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
{
name : "blockhash-opcode" ,
tag : latest ,
blocks : [ ] simBlock { {
BlockOverrides : & BlockOverrides {
Number : ( * hexutil . Big ) ( big . NewInt ( 12 ) ) ,
} ,
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : {
Code : hex2Bytes ( "600035804060008103601057600080fd5b5050" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// Phantom block after base.
Input : uint256ToBytes ( uint256 . NewInt ( 11 ) ) ,
} , {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// Canonical block.
Input : uint256ToBytes ( uint256 . NewInt ( 8 ) ) ,
} , {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// base block.
Input : uint256ToBytes ( uint256 . NewInt ( 10 ) ) ,
} } ,
} , {
BlockOverrides : & BlockOverrides {
Number : ( * hexutil . Big ) ( big . NewInt ( 16 ) ) ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// blocks[0]
Input : uint256ToBytes ( uint256 . NewInt ( 12 ) ) ,
} , {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// Phantom after blocks[0]
Input : uint256ToBytes ( uint256 . NewInt ( 13 ) ) ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x0" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { } ,
} , {
Number : "0xc" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xf864" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x52cc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0x52cc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0x52cc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xd" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x0" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { } ,
} , {
Number : "0xe" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x0" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { } ,
} , {
Number : "0xf" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x0" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { } ,
} , {
Number : "0x10" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xa598" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x" ,
GasUsed : "0x52cc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x" ,
GasUsed : "0x52cc" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
{
name : "basefee-non-validation" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : {
// Yul code:
// object "Test" {
// code {
// // Get the gas price from the transaction
// let gasPrice := gasprice()
//
// // Get the base fee from the block
// let baseFee := basefee()
//
// // Store gasPrice and baseFee in memory
// mstore(0x0, gasPrice)
// mstore(0x20, baseFee)
//
// // Return the data
// return(0x0, 0x40)
// }
// }
Code : hex2Bytes ( "3a489060005260205260406000f3" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// 0 gas price
} , {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// non-zero gas price
MaxPriorityFeePerGas : newInt ( 1 ) ,
MaxFeePerGas : newInt ( 2 ) ,
} ,
} ,
} , {
BlockOverrides : & BlockOverrides {
BaseFeePerGas : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// 0 gas price
} , {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
// non-zero gas price
MaxPriorityFeePerGas : newInt ( 1 ) ,
MaxFeePerGas : newInt ( 2 ) ,
} ,
} ,
} , {
// Base fee should be 0 to zero even if it was set in previous block.
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
} } ,
} } ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xa44e" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
GasUsed : "0x5227" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000" ,
GasUsed : "0x5227" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xc" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0xa44e" ,
Miner : coinbase ,
BaseFeePerGas : "0x1" ,
Calls : [ ] callRes { {
ReturnValue : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" ,
GasUsed : "0x5227" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} , {
ReturnValue : "0x00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001" ,
GasUsed : "0x5227" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} , {
Number : "0xd" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x5227" ,
Miner : coinbase ,
BaseFeePerGas : "0x0" ,
Calls : [ ] callRes { {
ReturnValue : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
GasUsed : "0x5227" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} , {
name : "basefee-validation-mode" ,
tag : latest ,
blocks : [ ] simBlock { {
StateOverrides : & StateOverride {
randomAccounts [ 2 ] . addr : {
// Yul code:
// object "Test" {
// code {
// // Get the gas price from the transaction
// let gasPrice := gasprice()
//
// // Get the base fee from the block
// let baseFee := basefee()
//
// // Store gasPrice and baseFee in memory
// mstore(0x0, gasPrice)
// mstore(0x20, baseFee)
//
// // Return the data
// return(0x0, 0x40)
// }
// }
Code : hex2Bytes ( "3a489060005260205260406000f3" ) ,
} ,
} ,
Calls : [ ] TransactionArgs { {
From : & accounts [ 0 ] . addr ,
To : & randomAccounts [ 2 ] . addr ,
MaxFeePerGas : newInt ( 233138868 ) ,
MaxPriorityFeePerGas : newInt ( 1 ) ,
} } ,
} } ,
validation : & validation ,
want : [ ] blockRes { {
Number : "0xb" ,
GasLimit : "0x47e7c4" ,
GasUsed : "0x5227" ,
Miner : coinbase ,
BaseFeePerGas : "0xde56ab3" ,
Calls : [ ] callRes { {
ReturnValue : "0x000000000000000000000000000000000000000000000000000000000de56ab4000000000000000000000000000000000000000000000000000000000de56ab3" ,
GasUsed : "0x5227" ,
Logs : [ ] log { } ,
Status : "0x1" ,
} } ,
} } ,
} ,
}
for _ , tc := range testSuite {
t . Run ( tc . name , func ( t * testing . T ) {
opts := simOpts { BlockStateCalls : tc . blocks }
if tc . includeTransfers != nil && * tc . includeTransfers {
opts . TraceTransfers = true
}
if tc . validation != nil && * tc . validation {
opts . Validation = true
}
result , err := api . SimulateV1 ( context . Background ( ) , opts , & tc . tag )
if tc . expectErr != nil {
if err == nil {
t . Fatalf ( "test %s: want error %v, have nothing" , tc . name , tc . expectErr )
}
if ! errors . Is ( err , tc . expectErr ) {
// Second try
if ! reflect . DeepEqual ( err , tc . expectErr ) {
t . Errorf ( "test %s: error mismatch, want %v, have %v" , tc . name , tc . expectErr , err )
}
}
return
}
if err != nil {
t . Fatalf ( "test %s: want no error, have %v" , tc . name , err )
}
// Turn result into res-struct
var have [ ] blockRes
resBytes , _ := json . Marshal ( result )
if err := json . Unmarshal ( resBytes , & have ) ; err != nil {
t . Fatalf ( "failed to unmarshal result: %v" , err )
}
if ! reflect . DeepEqual ( have , tc . want ) {
core/state: move state log mechanism to a separate layer (#30569)
This PR moves the logging/tracing-facilities out of `*state.StateDB`,
in to a wrapping struct which implements `vm.StateDB` instead.
In most places, it is a pretty straight-forward change:
- First, hoisting the invocations from state objects up to the statedb.
- Then making the mutation-methods simply return the previous value, so
that the external logging layer could log everything.
Some internal code uses the direct object-accessors to mutate the state,
particularly in testing and in setting up state overrides, which means
that these changes are unobservable for the hooked layer. Thus, configuring
the overrides are not necessarily part of the API we want to publish.
The trickiest part about the layering is that when the selfdestructs are
finally deleted during `Finalise`, there's the possibility that someone
sent some ether to it, which is burnt at that point, and thus needs to
be logged. The hooked layer reaches into the inner layer to figure out
these events.
In package `vm`, the conversion from `state.StateDB + hooks` into a
hooked `vm.StateDB` is performed where needed.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2024-10-23 09:03:36 +03:00
t . Log ( string ( resBytes ) )
2024-09-06 12:31:00 +03:00
t . Errorf ( "test %s, result mismatch, have\n%v\n, want\n%v\n" , tc . name , have , tc . want )
}
} )
2023-05-02 11:28:43 +03:00
}
}
2024-01-17 17:06:14 +03:00
func TestSignTransaction ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
key , _ = crypto . HexToECDSA ( "8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
to = crypto . PubkeyToAddress ( key . PublicKey )
genesis = & core . Genesis {
Config : params . MergedTestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc { } ,
2024-01-17 17:06:14 +03:00
}
)
b := newTestBackend ( t , 1 , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
b . SetPoS ( )
} )
api := NewTransactionAPI ( b , nil )
res , err := api . FillTransaction ( context . Background ( ) , TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
} )
if err != nil {
t . Fatalf ( "failed to fill tx defaults: %v\n" , err )
}
res , err = api . SignTransaction ( context . Background ( ) , argsFromTransaction ( res . Tx , b . acc . Address ) )
if err != nil {
t . Fatalf ( "failed to sign tx: %v\n" , err )
}
tx , err := json . Marshal ( res . Tx )
if err != nil {
t . Fatal ( err )
}
expect := ` { "type":"0x2","chainId":"0x1","nonce":"0x0","to":"0x703c4b2bd70c169f5717101caee543299fc946c7","gas":"0x5208","gasPrice":null,"maxPriorityFeePerGas":"0x0","maxFeePerGas":"0x684ee180","value":"0x1","input":"0x","accessList":[],"v":"0x0","r":"0x8fabeb142d585dd9247f459f7e6fe77e2520c88d50ba5d220da1533cea8b34e1","s":"0x582dd68b21aef36ba23f34e49607329c20d981d30404daf749077f5606785ce7","yParity":"0x0","hash":"0x93927839207cfbec395da84b8a2bc38b7b65d2cb2819e9fef1f091f5b1d4cc8f"} `
if ! bytes . Equal ( tx , [ ] byte ( expect ) ) {
t . Errorf ( "result mismatch. Have:\n%s\nWant:\n%s\n" , tx , expect )
}
}
func TestSignBlobTransaction ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
key , _ = crypto . HexToECDSA ( "8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
to = crypto . PubkeyToAddress ( key . PublicKey )
genesis = & core . Genesis {
Config : params . MergedTestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc { } ,
2024-01-17 17:06:14 +03:00
}
)
b := newTestBackend ( t , 1 , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
b . SetPoS ( )
} )
api := NewTransactionAPI ( b , nil )
res , err := api . FillTransaction ( context . Background ( ) , TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
BlobHashes : [ ] common . Hash { { 0x01 , 0x22 } } ,
} )
if err != nil {
t . Fatalf ( "failed to fill tx defaults: %v\n" , err )
}
_ , err = api . SignTransaction ( context . Background ( ) , argsFromTransaction ( res . Tx , b . acc . Address ) )
2024-04-05 20:29:44 +03:00
if err != nil {
t . Fatalf ( "should not fail on blob transaction" )
2024-01-17 17:06:14 +03:00
}
}
func TestSendBlobTransaction ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
key , _ = crypto . HexToECDSA ( "8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
to = crypto . PubkeyToAddress ( key . PublicKey )
genesis = & core . Genesis {
Config : params . MergedTestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc { } ,
2024-01-17 17:06:14 +03:00
}
)
b := newTestBackend ( t , 1 , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
b . SetPoS ( )
} )
api := NewTransactionAPI ( b , nil )
res , err := api . FillTransaction ( context . Background ( ) , TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
2024-05-27 09:39:39 +03:00
BlobHashes : [ ] common . Hash { { 0x01 , 0x22 } } ,
2024-01-17 17:06:14 +03:00
} )
if err != nil {
t . Fatalf ( "failed to fill tx defaults: %v\n" , err )
}
_ , err = api . SendTransaction ( context . Background ( ) , argsFromTransaction ( res . Tx , b . acc . Address ) )
if err == nil {
t . Errorf ( "sending tx should have failed" )
} else if ! errors . Is ( err , errBlobTxNotSupported ) {
t . Errorf ( "unexpected error. Have %v, want %v\n" , err , errBlobTxNotSupported )
}
}
2024-02-08 21:53:32 +03:00
func TestFillBlobTransaction ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
key , _ = crypto . HexToECDSA ( "8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
to = crypto . PubkeyToAddress ( key . PublicKey )
genesis = & core . Genesis {
Config : params . MergedTestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc { } ,
2024-02-08 21:53:32 +03:00
}
2024-03-13 09:51:46 +03:00
emptyBlob = new ( kzg4844 . Blob )
emptyBlobs = [ ] kzg4844 . Blob { * emptyBlob }
2024-02-08 21:53:32 +03:00
emptyBlobCommit , _ = kzg4844 . BlobToCommitment ( emptyBlob )
emptyBlobProof , _ = kzg4844 . ComputeBlobProof ( emptyBlob , emptyBlobCommit )
emptyBlobHash common . Hash = kzg4844 . CalcBlobHashV1 ( sha256 . New ( ) , & emptyBlobCommit )
)
b := newTestBackend ( t , 1 , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
b . SetPoS ( )
} )
api := NewTransactionAPI ( b , nil )
type result struct {
Hashes [ ] common . Hash
Sidecar * types . BlobTxSidecar
}
suite := [ ] struct {
name string
args TransactionArgs
err string
want * result
} {
{
name : "TestInvalidParamsCombination1" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
Blobs : [ ] kzg4844 . Blob { { } } ,
Proofs : [ ] kzg4844 . Proof { { } } ,
} ,
err : ` blob proofs provided while commitments were not ` ,
} ,
{
name : "TestInvalidParamsCombination2" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
Blobs : [ ] kzg4844 . Blob { { } } ,
Commitments : [ ] kzg4844 . Commitment { { } } ,
} ,
err : ` blob commitments provided while proofs were not ` ,
} ,
{
name : "TestInvalidParamsCount1" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
Blobs : [ ] kzg4844 . Blob { { } } ,
Commitments : [ ] kzg4844 . Commitment { { } , { } } ,
Proofs : [ ] kzg4844 . Proof { { } , { } } ,
} ,
err : ` number of blobs and commitments mismatch (have=2, want=1) ` ,
} ,
{
name : "TestInvalidParamsCount2" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
Blobs : [ ] kzg4844 . Blob { { } , { } } ,
Commitments : [ ] kzg4844 . Commitment { { } , { } } ,
Proofs : [ ] kzg4844 . Proof { { } } ,
} ,
err : ` number of blobs and proofs mismatch (have=1, want=2) ` ,
} ,
{
name : "TestInvalidProofVerification" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
Blobs : [ ] kzg4844 . Blob { { } , { } } ,
Commitments : [ ] kzg4844 . Commitment { { } , { } } ,
Proofs : [ ] kzg4844 . Proof { { } , { } } ,
} ,
err : ` failed to verify blob proof: short buffer ` ,
} ,
{
name : "TestGenerateBlobHashes" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
Commitments : [ ] kzg4844 . Commitment { emptyBlobCommit } ,
Proofs : [ ] kzg4844 . Proof { emptyBlobProof } ,
} ,
want : & result {
Hashes : [ ] common . Hash { emptyBlobHash } ,
Sidecar : & types . BlobTxSidecar {
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
Commitments : [ ] kzg4844 . Commitment { emptyBlobCommit } ,
Proofs : [ ] kzg4844 . Proof { emptyBlobProof } ,
} ,
} ,
} ,
{
name : "TestValidBlobHashes" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
BlobHashes : [ ] common . Hash { emptyBlobHash } ,
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
Commitments : [ ] kzg4844 . Commitment { emptyBlobCommit } ,
Proofs : [ ] kzg4844 . Proof { emptyBlobProof } ,
} ,
want : & result {
Hashes : [ ] common . Hash { emptyBlobHash } ,
Sidecar : & types . BlobTxSidecar {
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
Commitments : [ ] kzg4844 . Commitment { emptyBlobCommit } ,
Proofs : [ ] kzg4844 . Proof { emptyBlobProof } ,
} ,
} ,
} ,
{
name : "TestInvalidBlobHashes" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
BlobHashes : [ ] common . Hash { { 0x01 , 0x22 } } ,
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
Commitments : [ ] kzg4844 . Commitment { emptyBlobCommit } ,
Proofs : [ ] kzg4844 . Proof { emptyBlobProof } ,
} ,
err : fmt . Sprintf ( "blob hash verification failed (have=%s, want=%s)" , common . Hash { 0x01 , 0x22 } , emptyBlobHash ) ,
} ,
{
name : "TestGenerateBlobProofs" ,
args : TransactionArgs {
From : & b . acc . Address ,
To : & to ,
Value : ( * hexutil . Big ) ( big . NewInt ( 1 ) ) ,
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
} ,
want : & result {
Hashes : [ ] common . Hash { emptyBlobHash } ,
Sidecar : & types . BlobTxSidecar {
2024-03-13 09:51:46 +03:00
Blobs : emptyBlobs ,
2024-02-08 21:53:32 +03:00
Commitments : [ ] kzg4844 . Commitment { emptyBlobCommit } ,
Proofs : [ ] kzg4844 . Proof { emptyBlobProof } ,
} ,
} ,
} ,
}
for _ , tc := range suite {
t . Run ( tc . name , func ( t * testing . T ) {
2024-09-16 09:44:47 +03:00
t . Parallel ( )
2024-02-08 21:53:32 +03:00
res , err := api . FillTransaction ( context . Background ( ) , tc . args )
if len ( tc . err ) > 0 {
if err == nil {
t . Fatalf ( "missing error. want: %s" , tc . err )
2024-03-06 08:23:35 +03:00
} else if err . Error ( ) != tc . err {
2024-02-08 21:53:32 +03:00
t . Fatalf ( "error mismatch. want: %s, have: %s" , tc . err , err . Error ( ) )
}
return
}
if err != nil && len ( tc . err ) == 0 {
t . Fatalf ( "expected no error. have: %s" , err )
}
if res == nil {
t . Fatal ( "result missing" )
}
want , err := json . Marshal ( tc . want )
if err != nil {
t . Fatalf ( "failed to encode expected: %v" , err )
}
have , err := json . Marshal ( result { Hashes : res . Tx . BlobHashes ( ) , Sidecar : res . Tx . BlobTxSidecar ( ) } )
if err != nil {
t . Fatalf ( "failed to encode computed sidecar: %v" , err )
}
if ! bytes . Equal ( have , want ) {
t . Errorf ( "blob sidecar mismatch. Have: %s, want: %s" , have , want )
}
} )
}
}
2024-01-17 17:06:14 +03:00
func argsFromTransaction ( tx * types . Transaction , from common . Address ) TransactionArgs {
var (
2024-02-28 20:38:21 +03:00
gas = tx . Gas ( )
nonce = tx . Nonce ( )
input = tx . Data ( )
accessList * types . AccessList
2024-01-17 17:06:14 +03:00
)
2024-02-28 20:38:21 +03:00
if acl := tx . AccessList ( ) ; acl != nil {
accessList = & acl
}
2024-01-17 17:06:14 +03:00
return TransactionArgs {
From : & from ,
To : tx . To ( ) ,
Gas : ( * hexutil . Uint64 ) ( & gas ) ,
MaxFeePerGas : ( * hexutil . Big ) ( tx . GasFeeCap ( ) ) ,
MaxPriorityFeePerGas : ( * hexutil . Big ) ( tx . GasTipCap ( ) ) ,
Value : ( * hexutil . Big ) ( tx . Value ( ) ) ,
Nonce : ( * hexutil . Uint64 ) ( & nonce ) ,
Input : ( * hexutil . Bytes ) ( & input ) ,
ChainID : ( * hexutil . Big ) ( tx . ChainId ( ) ) ,
2024-02-28 20:38:21 +03:00
AccessList : accessList ,
BlobFeeCap : ( * hexutil . Big ) ( tx . BlobGasFeeCap ( ) ) ,
BlobHashes : tx . BlobHashes ( ) ,
2024-01-17 17:06:14 +03:00
}
}
2023-11-27 17:20:09 +03:00
type account struct {
2023-05-02 11:28:43 +03:00
key * ecdsa . PrivateKey
addr common . Address
}
2023-11-27 17:20:09 +03:00
func newAccounts ( n int ) ( accounts [ ] account ) {
2023-05-02 11:28:43 +03:00
for i := 0 ; i < n ; i ++ {
key , _ := crypto . GenerateKey ( )
addr := crypto . PubkeyToAddress ( key . PublicKey )
2023-11-27 17:20:09 +03:00
accounts = append ( accounts , account { key : key , addr : addr } )
2023-05-02 11:28:43 +03:00
}
2023-11-27 17:20:09 +03:00
slices . SortFunc ( accounts , func ( a , b account ) int { return a . addr . Cmp ( b . addr ) } )
2023-05-02 11:28:43 +03:00
return accounts
}
2024-09-06 12:31:00 +03:00
func newTestAccount ( ) account {
// testKey is a private key to use for funding a tester account.
key , _ := crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
// testAddr is the Ethereum address of the tester account.
addr := crypto . PubkeyToAddress ( key . PublicKey )
return account { key : key , addr : addr }
}
2024-07-25 02:01:59 +03:00
func newRPCBalance ( balance * big . Int ) * hexutil . Big {
2023-05-02 11:28:43 +03:00
rpcBalance := ( * hexutil . Big ) ( balance )
2024-07-25 02:01:59 +03:00
return rpcBalance
2023-05-02 11:28:43 +03:00
}
func hex2Bytes ( str string ) * hexutil . Bytes {
2024-09-06 12:31:00 +03:00
rpcBytes := hexutil . Bytes ( common . FromHex ( str ) )
return & rpcBytes
}
func newUint64 ( v uint64 ) * hexutil . Uint64 {
rpcUint64 := hexutil . Uint64 ( v )
return & rpcUint64
}
func newBytes ( b [ ] byte ) * hexutil . Bytes {
rpcBytes := hexutil . Bytes ( b )
2023-05-02 11:28:43 +03:00
return & rpcBytes
}
2023-05-16 11:40:47 +03:00
2024-09-06 12:31:00 +03:00
func uint256ToBytes ( v * uint256 . Int ) * hexutil . Bytes {
b := v . Bytes32 ( )
r := hexutil . Bytes ( b [ : ] )
return & r
}
2023-05-16 11:40:47 +03:00
func TestRPCMarshalBlock ( t * testing . T ) {
2023-05-29 15:52:08 +03:00
t . Parallel ( )
2023-05-16 11:40:47 +03:00
var (
txs [ ] * types . Transaction
to = common . BytesToAddress ( [ ] byte { 0x11 } )
)
for i := uint64 ( 1 ) ; i <= 4 ; i ++ {
var tx * types . Transaction
if i % 2 == 0 {
tx = types . NewTx ( & types . LegacyTx {
Nonce : i ,
GasPrice : big . NewInt ( 11111 ) ,
Gas : 1111 ,
To : & to ,
Value : big . NewInt ( 111 ) ,
Data : [ ] byte { 0x11 , 0x11 , 0x11 } ,
} )
} else {
tx = types . NewTx ( & types . AccessListTx {
ChainID : big . NewInt ( 1337 ) ,
Nonce : i ,
GasPrice : big . NewInt ( 11111 ) ,
Gas : 1111 ,
To : & to ,
Value : big . NewInt ( 111 ) ,
Data : [ ] byte { 0x11 , 0x11 , 0x11 } ,
} )
}
txs = append ( txs , tx )
}
2024-04-30 15:55:08 +03:00
block := types . NewBlock ( & types . Header { Number : big . NewInt ( 100 ) } , & types . Body { Transactions : txs } , nil , blocktest . NewHasher ( ) )
2023-05-16 11:40:47 +03:00
var testSuite = [ ] struct {
inclTx bool
fullTx bool
want string
} {
// without txs
{
inclTx : false ,
fullTx : false ,
2023-08-05 01:54:55 +03:00
want : ` {
"difficulty" : "0x0" ,
"extraData" : "0x" ,
"gasLimit" : "0x0" ,
"gasUsed" : "0x0" ,
"hash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
"miner" : "0x0000000000000000000000000000000000000000" ,
"mixHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"nonce" : "0x0000000000000000" ,
"number" : "0x64" ,
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"receiptsRoot" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" ,
"sha3Uncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" ,
"size" : "0x296" ,
"stateRoot" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"timestamp" : "0x0" ,
"transactionsRoot" : "0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e" ,
"uncles" : [ ]
} ` ,
2023-05-16 11:40:47 +03:00
} ,
// only tx hashes
{
inclTx : true ,
fullTx : false ,
2023-08-05 01:54:55 +03:00
want : ` {
"difficulty" : "0x0" ,
"extraData" : "0x" ,
"gasLimit" : "0x0" ,
"gasUsed" : "0x0" ,
"hash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
"miner" : "0x0000000000000000000000000000000000000000" ,
"mixHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"nonce" : "0x0000000000000000" ,
"number" : "0x64" ,
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"receiptsRoot" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" ,
"sha3Uncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" ,
"size" : "0x296" ,
"stateRoot" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"timestamp" : "0x0" ,
"transactions" : [
"0x7d39df979e34172322c64983a9ad48302c2b889e55bda35324afecf043a77605" ,
"0x9bba4c34e57c875ff57ac8d172805a26ae912006985395dc1bdf8f44140a7bf4" ,
"0x98909ea1ff040da6be56bc4231d484de1414b3c1dac372d69293a4beb9032cb5" ,
"0x12e1f81207b40c3bdcc13c0ee18f5f86af6d31754d57a0ea1b0d4cfef21abef1"
] ,
"transactionsRoot" : "0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e" ,
"uncles" : [ ]
} ` ,
2023-05-16 11:40:47 +03:00
} ,
// full tx details
{
inclTx : true ,
fullTx : true ,
2023-08-05 01:54:55 +03:00
want : ` {
"difficulty" : "0x0" ,
"extraData" : "0x" ,
"gasLimit" : "0x0" ,
"gasUsed" : "0x0" ,
"hash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
"miner" : "0x0000000000000000000000000000000000000000" ,
"mixHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"nonce" : "0x0000000000000000" ,
"number" : "0x64" ,
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"receiptsRoot" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" ,
"sha3Uncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" ,
"size" : "0x296" ,
"stateRoot" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"timestamp" : "0x0" ,
"transactions" : [
{
"blockHash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"blockNumber" : "0x64" ,
"from" : "0x0000000000000000000000000000000000000000" ,
"gas" : "0x457" ,
"gasPrice" : "0x2b67" ,
"hash" : "0x7d39df979e34172322c64983a9ad48302c2b889e55bda35324afecf043a77605" ,
"input" : "0x111111" ,
"nonce" : "0x1" ,
"to" : "0x0000000000000000000000000000000000000011" ,
"transactionIndex" : "0x0" ,
"value" : "0x6f" ,
"type" : "0x1" ,
"accessList" : [ ] ,
"chainId" : "0x539" ,
"v" : "0x0" ,
"r" : "0x0" ,
"s" : "0x0" ,
"yParity" : "0x0"
} ,
{
"blockHash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"blockNumber" : "0x64" ,
"from" : "0x0000000000000000000000000000000000000000" ,
"gas" : "0x457" ,
"gasPrice" : "0x2b67" ,
"hash" : "0x9bba4c34e57c875ff57ac8d172805a26ae912006985395dc1bdf8f44140a7bf4" ,
"input" : "0x111111" ,
"nonce" : "0x2" ,
"to" : "0x0000000000000000000000000000000000000011" ,
"transactionIndex" : "0x1" ,
"value" : "0x6f" ,
"type" : "0x0" ,
"chainId" : "0x7fffffffffffffee" ,
"v" : "0x0" ,
"r" : "0x0" ,
"s" : "0x0"
} ,
{
"blockHash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"blockNumber" : "0x64" ,
"from" : "0x0000000000000000000000000000000000000000" ,
"gas" : "0x457" ,
"gasPrice" : "0x2b67" ,
"hash" : "0x98909ea1ff040da6be56bc4231d484de1414b3c1dac372d69293a4beb9032cb5" ,
"input" : "0x111111" ,
"nonce" : "0x3" ,
"to" : "0x0000000000000000000000000000000000000011" ,
"transactionIndex" : "0x2" ,
"value" : "0x6f" ,
"type" : "0x1" ,
"accessList" : [ ] ,
"chainId" : "0x539" ,
"v" : "0x0" ,
"r" : "0x0" ,
"s" : "0x0" ,
"yParity" : "0x0"
} ,
{
"blockHash" : "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee" ,
"blockNumber" : "0x64" ,
"from" : "0x0000000000000000000000000000000000000000" ,
"gas" : "0x457" ,
"gasPrice" : "0x2b67" ,
"hash" : "0x12e1f81207b40c3bdcc13c0ee18f5f86af6d31754d57a0ea1b0d4cfef21abef1" ,
"input" : "0x111111" ,
"nonce" : "0x4" ,
"to" : "0x0000000000000000000000000000000000000011" ,
"transactionIndex" : "0x3" ,
"value" : "0x6f" ,
"type" : "0x0" ,
"chainId" : "0x7fffffffffffffee" ,
"v" : "0x0" ,
"r" : "0x0" ,
"s" : "0x0"
}
] ,
"transactionsRoot" : "0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e" ,
"uncles" : [ ]
} ` ,
2023-05-16 11:40:47 +03:00
} ,
}
for i , tc := range testSuite {
2023-06-13 10:02:11 +03:00
resp := RPCMarshalBlock ( block , tc . inclTx , tc . fullTx , params . MainnetChainConfig )
2023-05-16 11:40:47 +03:00
out , err := json . Marshal ( resp )
if err != nil {
t . Errorf ( "test %d: json marshal error: %v" , i , err )
continue
}
2023-08-22 03:53:29 +03:00
require . JSONEqf ( t , tc . want , string ( out ) , "test %d" , i )
2023-05-16 11:40:47 +03:00
}
}
2023-05-29 15:52:08 +03:00
func TestRPCGetBlockOrHeader ( t * testing . T ) {
t . Parallel ( )
// Initialize test accounts
var (
acc1Key , _ = crypto . HexToECDSA ( "8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
acc2Key , _ = crypto . HexToECDSA ( "49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee" )
acc1Addr = crypto . PubkeyToAddress ( acc1Key . PublicKey )
acc2Addr = crypto . PubkeyToAddress ( acc2Key . PublicKey )
genesis = & core . Genesis {
Config : params . TestChainConfig ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc {
2023-05-29 15:52:08 +03:00
acc1Addr : { Balance : big . NewInt ( params . Ether ) } ,
acc2Addr : { Balance : big . NewInt ( params . Ether ) } ,
} ,
}
genBlocks = 10
signer = types . HomesteadSigner { }
tx = types . NewTx ( & types . LegacyTx {
Nonce : 11 ,
GasPrice : big . NewInt ( 11111 ) ,
Gas : 1111 ,
To : & acc2Addr ,
Value : big . NewInt ( 111 ) ,
Data : [ ] byte { 0x11 , 0x11 , 0x11 } ,
} )
withdrawal = & types . Withdrawal {
Index : 0 ,
Validator : 1 ,
Address : common . Address { 0x12 , 0x34 } ,
Amount : 10 ,
}
2024-04-30 15:55:08 +03:00
pending = types . NewBlock ( & types . Header { Number : big . NewInt ( 11 ) , Time : 42 } , & types . Body { Transactions : types . Transactions { tx } , Withdrawals : types . Withdrawals { withdrawal } } , nil , blocktest . NewHasher ( ) )
2023-05-29 15:52:08 +03:00
)
2023-08-22 03:53:29 +03:00
backend := newTestBackend ( t , genBlocks , genesis , ethash . NewFaker ( ) , func ( i int , b * core . BlockGen ) {
2023-05-29 15:52:08 +03:00
// Transfer from account[0] to account[1]
// value: 1000 wei
// fee: 0 wei
tx , _ := types . SignTx ( types . NewTx ( & types . LegacyTx { Nonce : uint64 ( i ) , To : & acc2Addr , Value : big . NewInt ( 1000 ) , Gas : params . TxGas , GasPrice : b . BaseFee ( ) , Data : nil } ) , signer , acc1Key )
b . AddTx ( tx )
} )
backend . setPendingBlock ( pending )
api := NewBlockChainAPI ( backend )
blockHashes := make ( [ ] common . Hash , genBlocks + 1 )
ctx := context . Background ( )
for i := 0 ; i <= genBlocks ; i ++ {
header , err := backend . HeaderByNumber ( ctx , rpc . BlockNumber ( i ) )
if err != nil {
t . Errorf ( "failed to get block: %d err: %v" , i , err )
}
blockHashes [ i ] = header . Hash ( )
}
pendingHash := pending . Hash ( )
var testSuite = [ ] struct {
blockNumber rpc . BlockNumber
blockHash * common . Hash
fullTx bool
reqHeader bool
2023-08-22 03:53:29 +03:00
file string
2023-05-29 15:52:08 +03:00
expectErr error
} {
// 0. latest header
{
blockNumber : rpc . LatestBlockNumber ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "tag-latest" ,
2023-05-29 15:52:08 +03:00
} ,
// 1. genesis header
{
blockNumber : rpc . BlockNumber ( 0 ) ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "number-0" ,
2023-05-29 15:52:08 +03:00
} ,
// 2. #1 header
{
blockNumber : rpc . BlockNumber ( 1 ) ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "number-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 3. latest-1 header
{
blockNumber : rpc . BlockNumber ( 9 ) ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "number-latest-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 4. latest+1 header
{
blockNumber : rpc . BlockNumber ( 11 ) ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "number-latest+1" ,
2023-05-29 15:52:08 +03:00
} ,
// 5. pending header
{
blockNumber : rpc . PendingBlockNumber ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "tag-pending" ,
2023-05-29 15:52:08 +03:00
} ,
// 6. latest block
{
blockNumber : rpc . LatestBlockNumber ,
2023-08-22 03:53:29 +03:00
file : "tag-latest" ,
2023-05-29 15:52:08 +03:00
} ,
// 7. genesis block
{
blockNumber : rpc . BlockNumber ( 0 ) ,
2023-08-22 03:53:29 +03:00
file : "number-0" ,
2023-05-29 15:52:08 +03:00
} ,
// 8. #1 block
{
blockNumber : rpc . BlockNumber ( 1 ) ,
2023-08-22 03:53:29 +03:00
file : "number-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 9. latest-1 block
{
blockNumber : rpc . BlockNumber ( 9 ) ,
fullTx : true ,
2023-08-22 03:53:29 +03:00
file : "number-latest-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 10. latest+1 block
{
blockNumber : rpc . BlockNumber ( 11 ) ,
fullTx : true ,
2023-08-22 03:53:29 +03:00
file : "number-latest+1" ,
2023-05-29 15:52:08 +03:00
} ,
// 11. pending block
{
blockNumber : rpc . PendingBlockNumber ,
2023-08-22 03:53:29 +03:00
file : "tag-pending" ,
2023-05-29 15:52:08 +03:00
} ,
// 12. pending block + fullTx
{
blockNumber : rpc . PendingBlockNumber ,
fullTx : true ,
2023-08-22 03:53:29 +03:00
file : "tag-pending-fullTx" ,
2023-05-29 15:52:08 +03:00
} ,
// 13. latest header by hash
{
blockHash : & blockHashes [ len ( blockHashes ) - 1 ] ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "hash-latest" ,
2023-05-29 15:52:08 +03:00
} ,
// 14. genesis header by hash
{
blockHash : & blockHashes [ 0 ] ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "hash-0" ,
2023-05-29 15:52:08 +03:00
} ,
// 15. #1 header
{
blockHash : & blockHashes [ 1 ] ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "hash-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 16. latest-1 header
{
blockHash : & blockHashes [ len ( blockHashes ) - 2 ] ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "hash-latest-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 17. empty hash
{
blockHash : & common . Hash { } ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : "hash-empty" ,
2023-05-29 15:52:08 +03:00
} ,
// 18. pending hash
{
blockHash : & pendingHash ,
reqHeader : true ,
2023-08-22 03:53:29 +03:00
file : ` hash-pending ` ,
2023-05-29 15:52:08 +03:00
} ,
// 19. latest block
{
blockHash : & blockHashes [ len ( blockHashes ) - 1 ] ,
2023-08-22 03:53:29 +03:00
file : "hash-latest" ,
2023-05-29 15:52:08 +03:00
} ,
// 20. genesis block
{
blockHash : & blockHashes [ 0 ] ,
2023-08-22 03:53:29 +03:00
file : "hash-genesis" ,
2023-05-29 15:52:08 +03:00
} ,
// 21. #1 block
{
blockHash : & blockHashes [ 1 ] ,
2023-08-22 03:53:29 +03:00
file : "hash-1" ,
2023-05-29 15:52:08 +03:00
} ,
// 22. latest-1 block
{
blockHash : & blockHashes [ len ( blockHashes ) - 2 ] ,
fullTx : true ,
2023-08-22 03:53:29 +03:00
file : "hash-latest-1-fullTx" ,
2023-05-29 15:52:08 +03:00
} ,
// 23. empty hash + body
{
blockHash : & common . Hash { } ,
fullTx : true ,
2023-08-22 03:53:29 +03:00
file : "hash-empty-fullTx" ,
2023-05-29 15:52:08 +03:00
} ,
// 24. pending block
{
blockHash : & pendingHash ,
2023-08-22 03:53:29 +03:00
file : ` hash-pending ` ,
2023-05-29 15:52:08 +03:00
} ,
// 25. pending block + fullTx
{
blockHash : & pendingHash ,
fullTx : true ,
2023-08-22 03:53:29 +03:00
file : "hash-pending-fullTx" ,
2023-05-29 15:52:08 +03:00
} ,
}
for i , tt := range testSuite {
var (
result map [ string ] interface { }
err error
2023-08-22 03:53:29 +03:00
rpc string
2023-05-29 15:52:08 +03:00
)
if tt . blockHash != nil {
if tt . reqHeader {
result = api . GetHeaderByHash ( context . Background ( ) , * tt . blockHash )
2023-08-22 03:53:29 +03:00
rpc = "eth_getHeaderByHash"
2023-05-29 15:52:08 +03:00
} else {
result , err = api . GetBlockByHash ( context . Background ( ) , * tt . blockHash , tt . fullTx )
2023-08-22 03:53:29 +03:00
rpc = "eth_getBlockByHash"
2023-05-29 15:52:08 +03:00
}
} else {
if tt . reqHeader {
result , err = api . GetHeaderByNumber ( context . Background ( ) , tt . blockNumber )
2023-08-22 03:53:29 +03:00
rpc = "eth_getHeaderByNumber"
2023-05-29 15:52:08 +03:00
} else {
result , err = api . GetBlockByNumber ( context . Background ( ) , tt . blockNumber , tt . fullTx )
2023-08-22 03:53:29 +03:00
rpc = "eth_getBlockByNumber"
2023-05-29 15:52:08 +03:00
}
}
if tt . expectErr != nil {
if err == nil {
t . Errorf ( "test %d: want error %v, have nothing" , i , tt . expectErr )
continue
}
if ! errors . Is ( err , tt . expectErr ) {
t . Errorf ( "test %d: error mismatch, want %v, have %v" , i , tt . expectErr , err )
}
continue
}
if err != nil {
t . Errorf ( "test %d: want no error, have %v" , i , err )
continue
}
2023-08-22 03:53:29 +03:00
testRPCResponseWithFile ( t , i , result , rpc , tt . file )
2023-05-29 15:52:08 +03:00
}
}
2023-07-13 13:47:32 +03:00
2023-08-15 15:35:48 +03:00
func setupReceiptBackend ( t * testing . T , genBlocks int ) ( * testBackend , [ ] common . Hash ) {
2024-01-17 17:06:14 +03:00
config := * params . MergedTestChainConfig
2023-07-13 13:47:32 +03:00
var (
acc1Key , _ = crypto . HexToECDSA ( "8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
acc2Key , _ = crypto . HexToECDSA ( "49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee" )
acc1Addr = crypto . PubkeyToAddress ( acc1Key . PublicKey )
acc2Addr = crypto . PubkeyToAddress ( acc2Key . PublicKey )
contract = common . HexToAddress ( "0000000000000000000000000000000000031ec7" )
genesis = & core . Genesis {
2023-08-22 03:53:29 +03:00
Config : & config ,
ExcessBlobGas : new ( uint64 ) ,
BlobGasUsed : new ( uint64 ) ,
2024-02-16 21:05:33 +03:00
Alloc : types . GenesisAlloc {
2023-07-13 13:47:32 +03:00
acc1Addr : { Balance : big . NewInt ( params . Ether ) } ,
acc2Addr : { Balance : big . NewInt ( params . Ether ) } ,
// // SPDX-License-Identifier: GPL-3.0
// pragma solidity >=0.7.0 <0.9.0;
//
// contract Token {
// event Transfer(address indexed from, address indexed to, uint256 value);
// function transfer(address to, uint256 value) public returns (bool) {
// emit Transfer(msg.sender, to, value);
// return true;
// }
// }
contract : { Balance : big . NewInt ( params . Ether ) , Code : common . FromHex ( "0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063a9059cbb14610030575b600080fd5b61004a6004803603810190610045919061016a565b610060565b60405161005791906101c5565b60405180910390f35b60008273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516100bf91906101ef565b60405180910390a36001905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610101826100d6565b9050919050565b610111816100f6565b811461011c57600080fd5b50565b60008135905061012e81610108565b92915050565b6000819050919050565b61014781610134565b811461015257600080fd5b50565b6000813590506101648161013e565b92915050565b60008060408385031215610181576101806100d1565b5b600061018f8582860161011f565b92505060206101a085828601610155565b9150509250929050565b60008115159050919050565b6101bf816101aa565b82525050565b60006020820190506101da60008301846101b6565b92915050565b6101e981610134565b82525050565b600060208201905061020460008301846101e0565b9291505056fea2646970667358221220b469033f4b77b9565ee84e0a2f04d496b18160d26034d54f9487e57788fd36d564736f6c63430008120033" ) } ,
} ,
}
2023-08-15 15:35:48 +03:00
signer = types . LatestSignerForChainID ( params . TestChainConfig . ChainID )
txHashes = make ( [ ] common . Hash , genBlocks )
2023-07-13 13:47:32 +03:00
)
2023-08-22 03:53:29 +03:00
backend := newTestBackend ( t , genBlocks , genesis , beacon . New ( ethash . NewFaker ( ) ) , func ( i int , b * core . BlockGen ) {
2023-07-13 13:47:32 +03:00
var (
tx * types . Transaction
err error
)
2024-02-19 18:29:59 +03:00
b . SetPoS ( )
2023-07-13 13:47:32 +03:00
switch i {
case 0 :
// transfer 1000wei
tx , err = types . SignTx ( types . NewTx ( & types . LegacyTx { Nonce : uint64 ( i ) , To : & acc2Addr , Value : big . NewInt ( 1000 ) , Gas : params . TxGas , GasPrice : b . BaseFee ( ) , Data : nil } ) , types . HomesteadSigner { } , acc1Key )
case 1 :
// create contract
tx , err = types . SignTx ( types . NewTx ( & types . LegacyTx { Nonce : uint64 ( i ) , To : nil , Gas : 53100 , GasPrice : b . BaseFee ( ) , Data : common . FromHex ( "0x60806040" ) } ) , signer , acc1Key )
case 2 :
// with logs
// transfer(address to, uint256 value)
data := fmt . Sprintf ( "0xa9059cbb%s%s" , common . HexToHash ( common . BigToAddress ( big . NewInt ( int64 ( i + 1 ) ) ) . Hex ( ) ) . String ( ) [ 2 : ] , common . BytesToHash ( [ ] byte { byte ( i + 11 ) } ) . String ( ) [ 2 : ] )
tx , err = types . SignTx ( types . NewTx ( & types . LegacyTx { Nonce : uint64 ( i ) , To : & contract , Gas : 60000 , GasPrice : b . BaseFee ( ) , Data : common . FromHex ( data ) } ) , signer , acc1Key )
case 3 :
// dynamic fee with logs
// transfer(address to, uint256 value)
data := fmt . Sprintf ( "0xa9059cbb%s%s" , common . HexToHash ( common . BigToAddress ( big . NewInt ( int64 ( i + 1 ) ) ) . Hex ( ) ) . String ( ) [ 2 : ] , common . BytesToHash ( [ ] byte { byte ( i + 11 ) } ) . String ( ) [ 2 : ] )
fee := big . NewInt ( 500 )
fee . Add ( fee , b . BaseFee ( ) )
tx , err = types . SignTx ( types . NewTx ( & types . DynamicFeeTx { Nonce : uint64 ( i ) , To : & contract , Gas : 60000 , Value : big . NewInt ( 1 ) , GasTipCap : big . NewInt ( 500 ) , GasFeeCap : fee , Data : common . FromHex ( data ) } ) , signer , acc1Key )
case 4 :
// access list with contract create
accessList := types . AccessList { {
Address : contract ,
StorageKeys : [ ] common . Hash { { 0 } } ,
} }
tx , err = types . SignTx ( types . NewTx ( & types . AccessListTx { Nonce : uint64 ( i ) , To : nil , Gas : 58100 , GasPrice : b . BaseFee ( ) , Data : common . FromHex ( "0x60806040" ) , AccessList : accessList } ) , signer , acc1Key )
2023-08-22 03:53:29 +03:00
case 5 :
// blob tx
fee := big . NewInt ( 500 )
fee . Add ( fee , b . BaseFee ( ) )
tx , err = types . SignTx ( types . NewTx ( & types . BlobTx {
Nonce : uint64 ( i ) ,
GasTipCap : uint256 . NewInt ( 1 ) ,
GasFeeCap : uint256 . MustFromBig ( fee ) ,
Gas : params . TxGas ,
To : acc2Addr ,
BlobFeeCap : uint256 . NewInt ( 1 ) ,
BlobHashes : [ ] common . Hash { { 1 } } ,
Value : new ( uint256 . Int ) ,
} ) , signer , acc1Key )
2023-07-13 13:47:32 +03:00
}
if err != nil {
t . Errorf ( "failed to sign tx: %v" , err )
}
if tx != nil {
b . AddTx ( tx )
txHashes [ i ] = tx . Hash ( )
}
} )
2023-08-15 15:35:48 +03:00
return backend , txHashes
}
func TestRPCGetTransactionReceipt ( t * testing . T ) {
t . Parallel ( )
var (
2023-08-22 03:53:29 +03:00
backend , txHashes = setupReceiptBackend ( t , 6 )
2023-08-15 15:35:48 +03:00
api = NewTransactionAPI ( backend , new ( AddrLocker ) )
)
2023-07-13 13:47:32 +03:00
var testSuite = [ ] struct {
txHash common . Hash
2023-08-22 03:53:29 +03:00
file string
2023-07-13 13:47:32 +03:00
} {
// 0. normal success
{
txHash : txHashes [ 0 ] ,
2023-08-22 03:53:29 +03:00
file : "normal-transfer-tx" ,
2023-07-13 13:47:32 +03:00
} ,
// 1. create contract
{
txHash : txHashes [ 1 ] ,
2023-08-22 03:53:29 +03:00
file : "create-contract-tx" ,
2023-07-13 13:47:32 +03:00
} ,
// 2. with logs success
{
txHash : txHashes [ 2 ] ,
2023-08-22 03:53:29 +03:00
file : "with-logs" ,
2023-07-13 13:47:32 +03:00
} ,
// 3. dynamic tx with logs success
{
txHash : txHashes [ 3 ] ,
2023-08-22 03:53:29 +03:00
file : ` dynamic-tx-with-logs ` ,
2023-07-13 13:47:32 +03:00
} ,
// 4. access list tx with create contract
{
txHash : txHashes [ 4 ] ,
2023-08-22 03:53:29 +03:00
file : "create-contract-with-access-list" ,
2023-07-13 13:47:32 +03:00
} ,
// 5. txhash empty
{
txHash : common . Hash { } ,
2023-08-22 03:53:29 +03:00
file : "txhash-empty" ,
2023-07-13 13:47:32 +03:00
} ,
// 6. txhash not found
{
txHash : common . HexToHash ( "deadbeef" ) ,
2023-08-22 03:53:29 +03:00
file : "txhash-notfound" ,
} ,
// 7. blob tx
{
txHash : txHashes [ 5 ] ,
file : "blob-tx" ,
2023-07-13 13:47:32 +03:00
} ,
}
for i , tt := range testSuite {
var (
result interface { }
err error
)
result , err = api . GetTransactionReceipt ( context . Background ( ) , tt . txHash )
if err != nil {
t . Errorf ( "test %d: want no error, have %v" , i , err )
continue
}
2023-08-22 03:53:29 +03:00
testRPCResponseWithFile ( t , i , result , "eth_getTransactionReceipt" , tt . file )
2023-07-13 13:47:32 +03:00
}
}
2023-08-15 15:35:48 +03:00
func TestRPCGetBlockReceipts ( t * testing . T ) {
t . Parallel ( )
var (
2023-08-22 03:53:29 +03:00
genBlocks = 6
2023-08-15 15:35:48 +03:00
backend , _ = setupReceiptBackend ( t , genBlocks )
api = NewBlockChainAPI ( backend )
)
blockHashes := make ( [ ] common . Hash , genBlocks + 1 )
ctx := context . Background ( )
for i := 0 ; i <= genBlocks ; i ++ {
header , err := backend . HeaderByNumber ( ctx , rpc . BlockNumber ( i ) )
if err != nil {
t . Errorf ( "failed to get block: %d err: %v" , i , err )
}
blockHashes [ i ] = header . Hash ( )
}
var testSuite = [ ] struct {
test rpc . BlockNumberOrHash
2023-08-22 03:53:29 +03:00
file string
2023-08-15 15:35:48 +03:00
} {
// 0. block without any txs(hash)
{
test : rpc . BlockNumberOrHashWithHash ( blockHashes [ 0 ] , false ) ,
2023-08-22 03:53:29 +03:00
file : "number-0" ,
2023-08-15 15:35:48 +03:00
} ,
// 1. block without any txs(number)
{
test : rpc . BlockNumberOrHashWithNumber ( 0 ) ,
2023-08-22 03:53:29 +03:00
file : "number-1" ,
2023-08-15 15:35:48 +03:00
} ,
// 2. earliest tag
{
test : rpc . BlockNumberOrHashWithNumber ( rpc . EarliestBlockNumber ) ,
2023-08-22 03:53:29 +03:00
file : "tag-earliest" ,
2023-08-15 15:35:48 +03:00
} ,
// 3. latest tag
{
test : rpc . BlockNumberOrHashWithNumber ( rpc . LatestBlockNumber ) ,
2023-08-22 03:53:29 +03:00
file : "tag-latest" ,
2023-08-15 15:35:48 +03:00
} ,
// 4. block with legacy transfer tx(hash)
{
test : rpc . BlockNumberOrHashWithHash ( blockHashes [ 1 ] , false ) ,
2023-08-22 03:53:29 +03:00
file : "block-with-legacy-transfer-tx" ,
2023-08-15 15:35:48 +03:00
} ,
// 5. block with contract create tx(number)
{
test : rpc . BlockNumberOrHashWithNumber ( rpc . BlockNumber ( 2 ) ) ,
2023-08-22 03:53:29 +03:00
file : "block-with-contract-create-tx" ,
2023-08-15 15:35:48 +03:00
} ,
// 6. block with legacy contract call tx(hash)
{
test : rpc . BlockNumberOrHashWithHash ( blockHashes [ 3 ] , false ) ,
2023-08-22 03:53:29 +03:00
file : "block-with-legacy-contract-call-tx" ,
2023-08-15 15:35:48 +03:00
} ,
// 7. block with dynamic fee tx(number)
{
test : rpc . BlockNumberOrHashWithNumber ( rpc . BlockNumber ( 4 ) ) ,
2023-08-22 03:53:29 +03:00
file : "block-with-dynamic-fee-tx" ,
2023-08-15 15:35:48 +03:00
} ,
// 8. block is empty
{
test : rpc . BlockNumberOrHashWithHash ( common . Hash { } , false ) ,
2023-08-22 03:53:29 +03:00
file : "hash-empty" ,
2023-08-15 15:35:48 +03:00
} ,
// 9. block is not found
{
test : rpc . BlockNumberOrHashWithHash ( common . HexToHash ( "deadbeef" ) , false ) ,
2023-08-22 03:53:29 +03:00
file : "hash-notfound" ,
2023-08-15 15:35:48 +03:00
} ,
// 10. block is not found
{
test : rpc . BlockNumberOrHashWithNumber ( rpc . BlockNumber ( genBlocks + 1 ) ) ,
2023-08-22 03:53:29 +03:00
file : "block-notfound" ,
} ,
// 11. block with blob tx
{
test : rpc . BlockNumberOrHashWithNumber ( rpc . BlockNumber ( 6 ) ) ,
file : "block-with-blob-tx" ,
2023-08-15 15:35:48 +03:00
} ,
}
for i , tt := range testSuite {
var (
result interface { }
err error
)
result , err = api . GetBlockReceipts ( context . Background ( ) , tt . test )
if err != nil {
t . Errorf ( "test %d: want no error, have %v" , i , err )
continue
}
2023-08-22 03:53:29 +03:00
testRPCResponseWithFile ( t , i , result , "eth_getBlockReceipts" , tt . file )
}
}
2024-09-06 12:31:00 +03:00
type precompileContract struct { }
func ( p * precompileContract ) RequiredGas ( input [ ] byte ) uint64 { return 0 }
func ( p * precompileContract ) Run ( input [ ] byte ) ( [ ] byte , error ) { return nil , nil }
func TestStateOverrideMovePrecompile ( t * testing . T ) {
db := state . NewDatabase ( triedb . NewDatabase ( rawdb . NewMemoryDatabase ( ) , nil ) , nil )
statedb , err := state . New ( common . Hash { } , db )
if err != nil {
t . Fatalf ( "failed to create statedb: %v" , err )
}
precompiles := map [ common . Address ] vm . PrecompiledContract {
common . BytesToAddress ( [ ] byte { 0x1 } ) : & precompileContract { } ,
common . BytesToAddress ( [ ] byte { 0x2 } ) : & precompileContract { } ,
}
bytes2Addr := func ( b [ ] byte ) * common . Address {
a := common . BytesToAddress ( b )
return & a
}
var testSuite = [ ] struct {
overrides StateOverride
expectedPrecompiles map [ common . Address ] struct { }
fail bool
} {
{
overrides : StateOverride {
common . BytesToAddress ( [ ] byte { 0x1 } ) : {
Code : hex2Bytes ( "0xff" ) ,
MovePrecompileTo : bytes2Addr ( [ ] byte { 0x2 } ) ,
} ,
common . BytesToAddress ( [ ] byte { 0x2 } ) : {
Code : hex2Bytes ( "0x00" ) ,
} ,
} ,
// 0x2 has already been touched by the moveTo.
fail : true ,
} , {
overrides : StateOverride {
common . BytesToAddress ( [ ] byte { 0x1 } ) : {
Code : hex2Bytes ( "0xff" ) ,
MovePrecompileTo : bytes2Addr ( [ ] byte { 0xff } ) ,
} ,
common . BytesToAddress ( [ ] byte { 0x3 } ) : {
Code : hex2Bytes ( "0x00" ) ,
MovePrecompileTo : bytes2Addr ( [ ] byte { 0xfe } ) ,
} ,
} ,
// 0x3 is not a precompile.
fail : true ,
} , {
overrides : StateOverride {
common . BytesToAddress ( [ ] byte { 0x1 } ) : {
Code : hex2Bytes ( "0xff" ) ,
MovePrecompileTo : bytes2Addr ( [ ] byte { 0xff } ) ,
} ,
common . BytesToAddress ( [ ] byte { 0x2 } ) : {
Code : hex2Bytes ( "0x00" ) ,
MovePrecompileTo : bytes2Addr ( [ ] byte { 0xfe } ) ,
} ,
} ,
expectedPrecompiles : map [ common . Address ] struct { } { common . BytesToAddress ( [ ] byte { 0xfe } ) : { } , common . BytesToAddress ( [ ] byte { 0xff } ) : { } } ,
} ,
}
for i , tt := range testSuite {
cpy := maps . Clone ( precompiles )
// Apply overrides
err := tt . overrides . Apply ( statedb , cpy )
if tt . fail {
if err == nil {
t . Errorf ( "test %d: want error, have nothing" , i )
}
continue
}
if err != nil {
t . Errorf ( "test %d: want no error, have %v" , i , err )
continue
}
// Precompile keys
if len ( cpy ) != len ( tt . expectedPrecompiles ) {
t . Errorf ( "test %d: precompile mismatch, want %d, have %d" , i , len ( tt . expectedPrecompiles ) , len ( cpy ) )
}
for k := range tt . expectedPrecompiles {
if _ , ok := cpy [ k ] ; ! ok {
t . Errorf ( "test %d: precompile not found: %s" , i , k . String ( ) )
}
}
}
}
2023-08-22 03:53:29 +03:00
func testRPCResponseWithFile ( t * testing . T , testid int , result interface { } , rpc string , file string ) {
data , err := json . MarshalIndent ( result , "" , " " )
if err != nil {
t . Errorf ( "test %d: json marshal error" , testid )
return
}
outputFile := filepath . Join ( "testdata" , fmt . Sprintf ( "%s-%s.json" , rpc , file ) )
if os . Getenv ( "WRITE_TEST_FILES" ) != "" {
os . WriteFile ( outputFile , data , 0644 )
}
want , err := os . ReadFile ( outputFile )
if err != nil {
t . Fatalf ( "error reading expected test file: %s output: %v" , outputFile , err )
2023-08-15 15:35:48 +03:00
}
2023-08-22 03:53:29 +03:00
require . JSONEqf ( t , string ( want ) , string ( data ) , "test %d: json not match, want: %s, have: %s" , testid , string ( want ) , string ( data ) )
2023-08-15 15:35:48 +03:00
}
2024-09-06 12:31:00 +03:00
func addressToHash ( a common . Address ) common . Hash {
return common . BytesToHash ( a . Bytes ( ) )
}