2023-01-24 05:43:04 +00:00
|
|
|
package core_test
|
2022-01-14 19:06:35 +00:00
|
|
|
|
|
|
|
import (
|
2022-04-29 07:37:02 +00:00
|
|
|
"context"
|
2022-11-21 16:21:59 +00:00
|
|
|
"math/big"
|
2022-04-29 07:37:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-11-21 16:21:59 +00:00
|
|
|
"github.com/holiman/uint256"
|
2023-10-18 06:37:39 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain/networkname"
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-05-08 08:30:15 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/datadir"
|
2022-04-29 07:37:02 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2023-01-24 05:43:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
2023-05-08 08:30:15 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/state/temporal"
|
2023-01-24 05:43:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2022-11-21 16:21:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2022-01-14 19:06:35 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2023-05-17 06:36:06 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-01-14 19:06:35 +00:00
|
|
|
)
|
|
|
|
|
2023-03-20 15:44:22 +00:00
|
|
|
func TestGenesisBlockHashes(t *testing.T) {
|
2023-11-17 03:52:40 +00:00
|
|
|
t.Parallel()
|
2023-05-17 06:36:06 +00:00
|
|
|
logger := log.New()
|
2023-06-14 03:01:00 +00:00
|
|
|
_, db, _ := temporal.NewTestDB(t, datadir.New(t.TempDir()), nil)
|
2022-04-29 07:37:02 +00:00
|
|
|
check := func(network string) {
|
2023-03-20 15:44:22 +00:00
|
|
|
genesis := core.GenesisBlockByChainName(network)
|
2022-04-29 07:37:02 +00:00
|
|
|
tx, err := db.BeginRw(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2023-06-15 06:11:51 +00:00
|
|
|
_, block, err := core.WriteGenesisBlock(tx, genesis, nil, "", logger)
|
2022-04-29 07:37:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
expect := params.GenesisHashByChainName(network)
|
|
|
|
require.NotNil(t, expect, network)
|
|
|
|
require.Equal(t, block.Hash().Bytes(), expect.Bytes(), network)
|
|
|
|
}
|
|
|
|
for _, network := range networkname.All {
|
|
|
|
check(network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:44:22 +00:00
|
|
|
func TestGenesisBlockRoots(t *testing.T) {
|
2023-11-17 03:52:40 +00:00
|
|
|
t.Parallel()
|
2022-11-21 16:21:59 +00:00
|
|
|
require := require.New(t)
|
|
|
|
var err error
|
|
|
|
|
2023-03-29 07:27:06 +00:00
|
|
|
block, _, _ := core.GenesisToBlock(core.MainnetGenesisBlock(), "")
|
2022-11-21 16:21:59 +00:00
|
|
|
if block.Hash() != params.MainnetGenesisHash {
|
|
|
|
t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
|
|
|
|
}
|
|
|
|
|
2023-03-29 07:27:06 +00:00
|
|
|
block, _, err = core.GenesisToBlock(core.GnosisGenesisBlock(), "")
|
2022-11-21 16:21:59 +00:00
|
|
|
require.NoError(err)
|
|
|
|
if block.Root() != params.GnosisGenesisStateRoot {
|
|
|
|
t.Errorf("wrong Gnosis Chain genesis state root, got %v, want %v", block.Root(), params.GnosisGenesisStateRoot)
|
|
|
|
}
|
|
|
|
if block.Hash() != params.GnosisGenesisHash {
|
|
|
|
t.Errorf("wrong Gnosis Chain genesis hash, got %v, want %v", block.Hash(), params.GnosisGenesisHash)
|
|
|
|
}
|
|
|
|
|
2023-03-29 07:27:06 +00:00
|
|
|
block, _, err = core.GenesisToBlock(core.ChiadoGenesisBlock(), "")
|
2022-11-21 16:21:59 +00:00
|
|
|
require.NoError(err)
|
|
|
|
if block.Root() != params.ChiadoGenesisStateRoot {
|
|
|
|
t.Errorf("wrong Chiado genesis state root, got %v, want %v", block.Root(), params.ChiadoGenesisStateRoot)
|
|
|
|
}
|
|
|
|
if block.Hash() != params.ChiadoGenesisHash {
|
|
|
|
t.Errorf("wrong Chiado genesis hash, got %v, want %v", block.Hash(), params.ChiadoGenesisHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 07:37:02 +00:00
|
|
|
func TestCommitGenesisIdempotency(t *testing.T) {
|
2023-11-17 03:52:40 +00:00
|
|
|
t.Parallel()
|
2023-05-17 06:36:06 +00:00
|
|
|
logger := log.New()
|
2023-06-14 03:01:00 +00:00
|
|
|
_, db, _ := temporal.NewTestDB(t, datadir.New(t.TempDir()), nil)
|
2023-05-08 08:30:15 +00:00
|
|
|
tx, err := db.BeginRw(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-03-20 15:44:22 +00:00
|
|
|
genesis := core.GenesisBlockByChainName(networkname.MainnetChainName)
|
2023-06-15 06:11:51 +00:00
|
|
|
_, _, err = core.WriteGenesisBlock(tx, genesis, nil, "", logger)
|
2022-04-29 07:37:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
seq, err := tx.ReadSequence(kv.EthTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(2), seq)
|
|
|
|
|
2023-06-15 06:11:51 +00:00
|
|
|
_, _, err = core.WriteGenesisBlock(tx, genesis, nil, "", logger)
|
2022-04-29 07:37:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
seq, err = tx.ReadSequence(kv.EthTx)
|
2022-01-14 19:06:35 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-29 07:37:02 +00:00
|
|
|
require.Equal(t, uint64(2), seq)
|
2022-01-14 19:06:35 +00:00
|
|
|
}
|
2022-11-21 16:21:59 +00:00
|
|
|
|
|
|
|
func TestAllocConstructor(t *testing.T) {
|
2023-11-17 03:52:40 +00:00
|
|
|
t.Parallel()
|
2022-11-21 16:21:59 +00:00
|
|
|
require := require.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-05-17 06:36:06 +00:00
|
|
|
logger := log.New()
|
2022-11-21 16:21:59 +00:00
|
|
|
// This deployment code initially sets contract's 0th storage to 0x2a
|
|
|
|
// and its 1st storage to 0x01c9.
|
|
|
|
deploymentCode := common.FromHex("602a5f556101c960015560048060135f395ff35f355f55")
|
|
|
|
|
|
|
|
funds := big.NewInt(1000000000)
|
2023-01-13 18:12:18 +00:00
|
|
|
address := libcommon.HexToAddress("0x1000000000000000000000000000000000000001")
|
2023-03-29 07:27:06 +00:00
|
|
|
genSpec := &types.Genesis{
|
2022-11-21 16:21:59 +00:00
|
|
|
Config: params.AllProtocolChanges,
|
2023-03-29 07:27:06 +00:00
|
|
|
Alloc: types.GenesisAlloc{
|
2022-11-21 16:21:59 +00:00
|
|
|
address: {Constructor: deploymentCode, Balance: funds},
|
|
|
|
},
|
|
|
|
}
|
2023-05-08 08:30:15 +00:00
|
|
|
|
2023-06-14 03:01:00 +00:00
|
|
|
historyV3, db, _ := temporal.NewTestDB(t, datadir.New(t.TempDir()), nil)
|
2023-05-17 06:36:06 +00:00
|
|
|
_, _, err := core.CommitGenesisBlock(db, genSpec, "", logger)
|
2022-11-21 16:21:59 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
tx, err := db.BeginRo(context.Background())
|
|
|
|
require.NoError(err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-01-24 05:43:04 +00:00
|
|
|
//TODO: support historyV3
|
2023-05-08 08:30:15 +00:00
|
|
|
reader, err := rpchelper.CreateHistoryStateReader(tx, 1, 0, historyV3, genSpec.Config.ChainName)
|
2023-01-24 05:43:04 +00:00
|
|
|
require.NoError(err)
|
|
|
|
state := state.New(reader)
|
2022-11-21 16:21:59 +00:00
|
|
|
balance := state.GetBalance(address)
|
|
|
|
assert.Equal(funds, balance.ToBig())
|
|
|
|
code := state.GetCode(address)
|
|
|
|
assert.Equal(common.FromHex("5f355f55"), code)
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
key0 := libcommon.HexToHash("0000000000000000000000000000000000000000000000000000000000000000")
|
2022-11-21 16:21:59 +00:00
|
|
|
storage0 := &uint256.Int{}
|
|
|
|
state.GetState(address, &key0, storage0)
|
|
|
|
assert.Equal(uint256.NewInt(0x2a), storage0)
|
2023-01-13 18:12:18 +00:00
|
|
|
key1 := libcommon.HexToHash("0000000000000000000000000000000000000000000000000000000000000001")
|
2022-11-21 16:21:59 +00:00
|
|
|
storage1 := &uint256.Int{}
|
|
|
|
state.GetState(address, &key1, storage1)
|
|
|
|
assert.Equal(uint256.NewInt(0x01c9), storage1)
|
|
|
|
}
|