Compare commits
8 Commits
505938f2a9
...
746755e415
Author | SHA1 | Date |
---|---|---|
|
746755e415 | |
|
dcb4f05791 | |
|
eb0e1a54e8 | |
|
02e46d01f1 | |
|
2bfa0ea65e | |
|
1dcdcdfab3 | |
|
49bd6de5e9 | |
|
e82f83f14f |
|
@ -24,6 +24,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -2309,6 +2310,101 @@ func TestSimulateV1(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSimulateV1ChainLinkage(t *testing.T) {
|
||||||
|
var (
|
||||||
|
acc = newTestAccount()
|
||||||
|
sender = acc.addr
|
||||||
|
contractAddr = common.Address{0xaa, 0xaa}
|
||||||
|
recipient = common.Address{0xbb, 0xbb}
|
||||||
|
gspec = &core.Genesis{
|
||||||
|
Config: params.MergedTestChainConfig,
|
||||||
|
Alloc: types.GenesisAlloc{
|
||||||
|
sender: {Balance: big.NewInt(params.Ether)},
|
||||||
|
contractAddr: {Code: common.Hex2Bytes("5f35405f8114600f575f5260205ff35b5f80fd")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
signer = types.LatestSigner(params.MergedTestChainConfig)
|
||||||
|
)
|
||||||
|
backend := newTestBackend(t, 1, gspec, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {
|
||||||
|
tx := types.MustSignNewTx(acc.key, signer, &types.LegacyTx{
|
||||||
|
Nonce: uint64(i),
|
||||||
|
GasPrice: b.BaseFee(),
|
||||||
|
Gas: params.TxGas,
|
||||||
|
To: &recipient,
|
||||||
|
Value: big.NewInt(500),
|
||||||
|
})
|
||||||
|
b.AddTx(tx)
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
stateDB, baseHeader, err := backend.StateAndHeaderByNumberOrHash(ctx, rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get state and header: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sim := &simulator{
|
||||||
|
b: backend,
|
||||||
|
state: stateDB,
|
||||||
|
base: baseHeader,
|
||||||
|
chainConfig: backend.ChainConfig(),
|
||||||
|
gp: new(core.GasPool).AddGas(math.MaxUint64),
|
||||||
|
traceTransfers: false,
|
||||||
|
validate: false,
|
||||||
|
fullTx: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
call1 = TransactionArgs{
|
||||||
|
From: &sender,
|
||||||
|
To: &recipient,
|
||||||
|
Value: (*hexutil.Big)(big.NewInt(1000)),
|
||||||
|
}
|
||||||
|
call2 = TransactionArgs{
|
||||||
|
From: &sender,
|
||||||
|
To: &recipient,
|
||||||
|
Value: (*hexutil.Big)(big.NewInt(2000)),
|
||||||
|
}
|
||||||
|
call3a = TransactionArgs{
|
||||||
|
From: &sender,
|
||||||
|
To: &contractAddr,
|
||||||
|
Input: uint256ToBytes(uint256.NewInt(baseHeader.Number.Uint64() + 1)),
|
||||||
|
Gas: newUint64(1000000),
|
||||||
|
}
|
||||||
|
call3b = TransactionArgs{
|
||||||
|
From: &sender,
|
||||||
|
To: &contractAddr,
|
||||||
|
Input: uint256ToBytes(uint256.NewInt(baseHeader.Number.Uint64() + 2)),
|
||||||
|
Gas: newUint64(1000000),
|
||||||
|
}
|
||||||
|
blocks = []simBlock{
|
||||||
|
{Calls: []TransactionArgs{call1}},
|
||||||
|
{Calls: []TransactionArgs{call2}},
|
||||||
|
{Calls: []TransactionArgs{call3a, call3b}},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
results, err := sim.execute(ctx, blocks)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("simulation execution failed: %v", err)
|
||||||
|
}
|
||||||
|
require.Equal(t, 3, len(results), "expected 3 simulated blocks")
|
||||||
|
|
||||||
|
// Check linkages of simulated blocks:
|
||||||
|
// Verify that block2's parent hash equals block1's hash.
|
||||||
|
block1 := results[0].Block
|
||||||
|
block2 := results[1].Block
|
||||||
|
block3 := results[2].Block
|
||||||
|
require.Equal(t, block1.ParentHash(), baseHeader.Hash(), "parent hash of block1 should equal hash of base block")
|
||||||
|
require.Equal(t, block1.Hash(), block2.Header().ParentHash, "parent hash of block2 should equal hash of block1")
|
||||||
|
require.Equal(t, block2.Hash(), block3.Header().ParentHash, "parent hash of block3 should equal hash of block2")
|
||||||
|
|
||||||
|
// In block3, two calls were executed to our contract.
|
||||||
|
// The first call in block3 should return the blockhash for block1 (i.e. block1.Hash()),
|
||||||
|
// whereas the second call should return the blockhash for block2 (i.e. block2.Hash()).
|
||||||
|
require.Equal(t, block1.Hash().Bytes(), []byte(results[2].Calls[0].ReturnValue), "returned blockhash for block1 does not match")
|
||||||
|
require.Equal(t, block2.Hash().Bytes(), []byte(results[2].Calls[1].ReturnValue), "returned blockhash for block2 does not match")
|
||||||
|
}
|
||||||
|
|
||||||
func TestSignTransaction(t *testing.T) {
|
func TestSignTransaction(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
// Initialize test accounts
|
// Initialize test accounts
|
||||||
|
|
|
@ -75,13 +75,14 @@ func (r *simCallResult) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
// simBlockResult is the result of a simulated block.
|
// simBlockResult is the result of a simulated block.
|
||||||
type simBlockResult struct {
|
type simBlockResult struct {
|
||||||
sim *simulator
|
fullTx bool
|
||||||
|
chainConfig *params.ChainConfig
|
||||||
Block *types.Block
|
Block *types.Block
|
||||||
Calls []simCallResult
|
Calls []simCallResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *simBlockResult) MarshalJSON() ([]byte, error) {
|
func (r *simBlockResult) MarshalJSON() ([]byte, error) {
|
||||||
blockData := RPCMarshalBlock(r.Block, true, r.sim.fullTx, r.sim.chainConfig)
|
blockData := RPCMarshalBlock(r.Block, true, r.fullTx, r.chainConfig)
|
||||||
blockData["calls"] = r.Calls
|
blockData["calls"] = r.Calls
|
||||||
return json.Marshal(blockData)
|
return json.Marshal(blockData)
|
||||||
}
|
}
|
||||||
|
@ -148,7 +149,7 @@ func (sim *simulator) execute(ctx context.Context, blocks []simBlock) ([]*simBlo
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
results[bi] = &simBlockResult{sim: sim, Block: result, Calls: callResults}
|
results[bi] = &simBlockResult{fullTx: sim.fullTx, chainConfig: sim.chainConfig, Block: result, Calls: callResults}
|
||||||
parent = result.Header()
|
parent = result.Header()
|
||||||
prevHeaders = append(prevHeaders, parent)
|
prevHeaders = append(prevHeaders, parent)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue