2019-11-08 10:21:44 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-08 14:55:56 +00:00
|
|
|
"crypto/ecdsa"
|
2019-11-13 10:52:03 +00:00
|
|
|
"fmt"
|
2019-11-08 10:21:44 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-04 07:43:08 +00:00
|
|
|
"github.com/holiman/uint256"
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-06-06 07:45:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-05-20 18:25:53 +00:00
|
|
|
|
2023-08-05 21:33:10 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages/mock"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/accounts/abi/bind"
|
|
|
|
"github.com/ledgerwatch/erigon/accounts/abi/bind/backends"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
|
|
|
"github.com/ledgerwatch/erigon/tests/contracts"
|
2019-11-08 10:21:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInsertIncorrectStateRootDifferentAccounts(t *testing.T) {
|
2019-11-08 14:55:56 +00:00
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(2000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
})
|
2019-11-08 10:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// BLOCK 1
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[0] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2021-06-05 10:00:21 +00:00
|
|
|
if chain.Headers[0].Root == incorrectHeader.Root {
|
2019-11-08 14:55:56 +00:00
|
|
|
t.Fatal("roots are the same")
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-08 14:55:56 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// insert a correct block
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err = GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(data.addresses[1], to, uint256.NewInt(5000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
data.keys[1],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-06-15 09:09:11 +00:00
|
|
|
tx, err := m.DB.BeginRw(context.Background())
|
2021-06-06 07:45:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2019-11-08 14:55:56 +00:00
|
|
|
if !st.Exist(to) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(from); balance.Uint64() != 1000000000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 1000000000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(data.addresses[1]); balance.Uint64() != 999995000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 999995000)
|
2019-11-14 16:14:25 +00:00
|
|
|
}
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(to); balance.Uint64() != 5000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 5000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
func TestInsertIncorrectStateRootSameAccount(t *testing.T) {
|
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(2000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-08 10:21:44 +00:00
|
|
|
})
|
2019-11-08 14:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
// BLOCK 1
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[0] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2021-06-05 10:00:21 +00:00
|
|
|
if chain.Headers[0].Root == incorrectHeader.Root {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Fatal("roots are the same")
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// insert a correct block
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err = GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(5000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-08 10:21:44 +00:00
|
|
|
})
|
2019-11-08 14:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain); err != nil {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
tx, err := m.DB.BeginRo(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2019-11-08 14:55:56 +00:00
|
|
|
if !st.Exist(to) {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(from); balance.Uint64() != 999995000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 999995000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(to); balance.Uint64() != 5000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 5000)
|
2019-11-08 10:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
func TestInsertIncorrectStateRootSameAccountSameAmount(t *testing.T) {
|
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(2000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
})
|
2019-11-08 10:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// BLOCK 1
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[0] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-08 14:55:56 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// insert a correct block
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err = GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain); err != nil {
|
2019-11-08 14:55:56 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
tx, err := m.DB.BeginRo(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2019-11-08 14:55:56 +00:00
|
|
|
if !st.Exist(to) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(from); balance.Uint64() != 999999000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 999999000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(to); balance.Uint64() != 1000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 1000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
func TestInsertIncorrectStateRootAllFundsRoot(t *testing.T) {
|
|
|
|
data := getGenesis(big.NewInt(3000))
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(2000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-08 10:21:44 +00:00
|
|
|
})
|
2019-11-08 14:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
|
|
|
// BLOCK 1
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[0] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// insert a correct block
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err = GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain); err != nil {
|
2019-11-08 14:55:56 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
tx, err := m.DB.BeginRo(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2019-11-08 14:55:56 +00:00
|
|
|
if !st.Exist(to) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(from); balance.Uint64() != 2000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 2000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(to); balance.Uint64() != 1000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 1000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
func TestInsertIncorrectStateRootAllFunds(t *testing.T) {
|
|
|
|
data := getGenesis(big.NewInt(3000))
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(3000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(data.addresses[1], to, uint256.NewInt(2000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
data.keys[1],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
// BLOCK 1
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[0] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[0].Transactions(), chain.Blocks[0].Uncles(), chain.Receipts[0], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-08 14:55:56 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert a correct block
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err = GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-08 14:55:56 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(1000)),
|
2019-11-08 14:55:56 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain); err != nil {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
tx, err := m.DB.BeginRo(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2019-11-08 14:55:56 +00:00
|
|
|
if !st.Exist(to) {
|
2019-11-08 10:21:44 +00:00
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(from); balance.Uint64() != 2000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 2000)
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
2020-05-26 16:53:50 +00:00
|
|
|
if balance := st.GetBalance(to); balance.Uint64() != 1000 {
|
|
|
|
t.Fatalf("got %v, expected %v", balance, 1000)
|
2019-11-08 10:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:21:07 +00:00
|
|
|
func TestAccountDeployIncorrectRoot(t *testing.T) {
|
2019-11-13 10:52:03 +00:00
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
var contractAddress libcommon.Address
|
2020-05-06 08:59:50 +00:00
|
|
|
eipContract := new(contracts.Testcontract)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-13 10:52:03 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(10)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockDeployTestContractTx(data.transactOpts[0], &contractAddress, eipContract),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-11 15:21:07 +00:00
|
|
|
})
|
2019-11-13 10:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 1
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to not exist at the block 0", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[1] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[0].Root
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[1].Transactions(), chain.Blocks[1].Uncles(), chain.Receipts[1], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 2 - INCORRECT
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to not exist at the block 1", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 2 - CORRECT
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to not exist at the block 1", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountCreateIncorrectRoot(t *testing.T) {
|
2019-11-13 10:52:03 +00:00
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
var contractAddress libcommon.Address
|
2020-05-06 08:59:50 +00:00
|
|
|
eipContract := new(contracts.Testcontract)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-13 10:52:03 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(10)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockDeployTestContractTx(data.transactOpts[0], &contractAddress, eipContract),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
2: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockTestContractTx(data.transactOpts[0], eipContract.Create, big.NewInt(2)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-11 15:21:07 +00:00
|
|
|
})
|
2019-11-13 10:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 1
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to not exist at the block 0", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 2
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to exist at the block 2", contractAddress.Hash().String())
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 3 - INCORRECT
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[2] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[2].Transactions(), chain.Blocks[2].Uncles(), chain.Receipts[2], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCK 3
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountUpdateIncorrectRoot(t *testing.T) {
|
2019-11-13 10:52:03 +00:00
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
var contractAddress libcommon.Address
|
2020-05-06 08:59:50 +00:00
|
|
|
eipContract := new(contracts.Testcontract)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-13 10:52:03 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(10)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockDeployTestContractTx(data.transactOpts[0], &contractAddress, eipContract),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
2: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockTestContractTx(data.transactOpts[0], eipContract.Create, big.NewInt(2)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
3: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockTestContractTx(data.transactOpts[0], eipContract.Update, big.NewInt(0)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-11 15:21:07 +00:00
|
|
|
})
|
2019-11-13 10:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 1
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to not exist at the block 0", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 2
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to exist at the block 2", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 3
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCK 4 - INCORRECT
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[3] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[3].Transactions(), chain.Blocks[3].Uncles(), chain.Receipts[3], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCK 4
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountDeleteIncorrectRoot(t *testing.T) {
|
2019-11-13 10:52:03 +00:00
|
|
|
data := getGenesis()
|
|
|
|
from := data.addresses[0]
|
|
|
|
fromKey := data.keys[0]
|
2023-01-13 18:12:18 +00:00
|
|
|
to := libcommon.Address{1}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
var contractAddress libcommon.Address
|
2020-05-06 08:59:50 +00:00
|
|
|
eipContract := new(contracts.Testcontract)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
m, chain, err := GenerateBlocks(t, data.genesisSpec, map[int]txn{
|
2019-11-13 10:52:03 +00:00
|
|
|
0: {
|
2021-06-04 16:25:28 +00:00
|
|
|
getBlockTx(from, to, uint256.NewInt(10)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
1: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockDeployTestContractTx(data.transactOpts[0], &contractAddress, eipContract),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
2: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockTestContractTx(data.transactOpts[0], eipContract.Create, big.NewInt(2)),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
|
|
|
3: {
|
2020-05-06 08:59:50 +00:00
|
|
|
getBlockTestContractTx(data.transactOpts[0], eipContract.Remove),
|
2019-11-13 10:52:03 +00:00
|
|
|
fromKey,
|
|
|
|
},
|
2019-11-11 15:21:07 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCK 1
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(0, 1)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to not exist at the block 0", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 2
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(1, 2)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
err = m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-04-19 03:10:33 +00:00
|
|
|
st := state.New(m.NewStateReader(tx))
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(from) {
|
|
|
|
t.Error("expected account to exist")
|
|
|
|
}
|
2019-11-11 15:21:07 +00:00
|
|
|
|
2021-06-06 07:45:49 +00:00
|
|
|
if !st.Exist(contractAddress) {
|
|
|
|
t.Error("expected contractAddress to exist at the block 1", contractAddress.Hash().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-11-11 15:21:07 +00:00
|
|
|
|
|
|
|
// BLOCK 3
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(2, 3)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCK 4 - INCORRECT
|
2021-06-05 10:00:21 +00:00
|
|
|
incorrectHeader := *chain.Headers[3] // Copy header, not just pointer
|
|
|
|
incorrectHeader.Root = chain.Headers[1].Root
|
2022-12-01 08:15:01 +00:00
|
|
|
incorrectBlock := types.NewBlock(&incorrectHeader, chain.Blocks[3].Transactions(), chain.Blocks[3].Uncles(), chain.Receipts[3], nil)
|
2022-07-26 07:35:38 +00:00
|
|
|
incorrectChain := &core.ChainPack{Blocks: []*types.Block{incorrectBlock}, Headers: []*types.Header{&incorrectHeader}, TopBlock: incorrectBlock}
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(incorrectChain); err == nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCK 4
|
2023-10-05 16:30:19 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(3, 4)); err != nil {
|
2019-11-11 15:21:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
type initialData struct {
|
2019-11-13 10:52:03 +00:00
|
|
|
keys []*ecdsa.PrivateKey
|
2023-01-13 18:12:18 +00:00
|
|
|
addresses []libcommon.Address
|
2019-11-13 10:52:03 +00:00
|
|
|
transactOpts []*bind.TransactOpts
|
2023-03-29 07:27:06 +00:00
|
|
|
genesisSpec *types.Genesis
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getGenesis(funds ...*big.Int) initialData {
|
|
|
|
accountFunds := big.NewInt(1000000000)
|
|
|
|
if len(funds) > 0 {
|
|
|
|
accountFunds = funds[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 3)
|
|
|
|
keys[0], _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
keys[1], _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
|
|
|
keys[2], _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
addresses := make([]libcommon.Address, 0, len(keys))
|
2019-11-13 11:12:58 +00:00
|
|
|
transactOpts := make([]*bind.TransactOpts, 0, len(keys))
|
2023-03-29 07:27:06 +00:00
|
|
|
allocs := types.GenesisAlloc{}
|
2019-11-08 14:55:56 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
addresses = append(addresses, addr)
|
2022-07-13 10:40:39 +00:00
|
|
|
to, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
transactOpts = append(transactOpts, to)
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-03-29 07:27:06 +00:00
|
|
|
allocs[addr] = types.GenesisAccount{Balance: accountFunds}
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return initialData{
|
2019-11-13 10:52:03 +00:00
|
|
|
keys: keys,
|
|
|
|
addresses: addresses,
|
|
|
|
transactOpts: transactOpts,
|
2023-03-29 07:27:06 +00:00
|
|
|
genesisSpec: &types.Genesis{
|
2023-01-13 18:12:18 +00:00
|
|
|
Config: &chain.Config{
|
2022-05-26 10:08:59 +00:00
|
|
|
ChainID: big.NewInt(1),
|
|
|
|
HomesteadBlock: new(big.Int),
|
|
|
|
TangerineWhistleBlock: new(big.Int),
|
|
|
|
SpuriousDragonBlock: big.NewInt(1),
|
|
|
|
ByzantiumBlock: big.NewInt(1),
|
|
|
|
ConstantinopleBlock: big.NewInt(1),
|
2019-11-08 10:21:44 +00:00
|
|
|
},
|
2019-11-08 14:55:56 +00:00
|
|
|
Alloc: allocs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-06-15 09:09:11 +00:00
|
|
|
type txn struct {
|
2019-11-08 14:55:56 +00:00
|
|
|
txFn blockTx
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
func GenerateBlocks(t *testing.T, gspec *types.Genesis, txs map[int]txn) (*mock.MockSentry, *core.ChainPack, error) {
|
2021-06-05 10:00:21 +00:00
|
|
|
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
2023-08-05 21:33:10 +00:00
|
|
|
m := mock.MockWithGenesis(t, gspec, key, false)
|
2019-11-08 14:55:56 +00:00
|
|
|
|
2023-01-24 05:43:04 +00:00
|
|
|
contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit)
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2021-06-05 10:00:21 +00:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, len(txs), func(i int, block *core.BlockGen) {
|
2021-04-22 17:11:37 +00:00
|
|
|
var tx types.Transaction
|
2019-11-13 10:52:03 +00:00
|
|
|
var isContractCall bool
|
2021-04-22 17:11:37 +00:00
|
|
|
signer := types.LatestSignerForChainID(nil)
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
if txToSend, ok := txs[i]; ok {
|
2019-11-13 10:52:03 +00:00
|
|
|
tx, isContractCall = txToSend.txFn(block, contractBackend)
|
2021-04-06 09:52:53 +00:00
|
|
|
var err error
|
2021-04-22 17:11:37 +00:00
|
|
|
tx, err = types.SignTx(tx, *signer, txToSend.key)
|
2019-11-08 10:21:44 +00:00
|
|
|
if err != nil {
|
2019-11-08 14:55:56 +00:00
|
|
|
return
|
2019-11-08 10:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tx != nil {
|
2019-11-13 10:52:03 +00:00
|
|
|
if !isContractCall {
|
2021-04-06 09:52:53 +00:00
|
|
|
err := contractBackend.SendTransaction(context.Background(), tx)
|
2019-11-13 10:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block.AddTx(tx)
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:55:56 +00:00
|
|
|
contractBackend.Commit()
|
2023-06-23 03:07:42 +00:00
|
|
|
})
|
2020-07-09 06:15:28 +00:00
|
|
|
if err != nil {
|
2021-06-05 10:00:21 +00:00
|
|
|
return nil, nil, fmt.Errorf("generate chain: %w", err)
|
2020-06-05 09:25:33 +00:00
|
|
|
}
|
2021-06-05 10:00:21 +00:00
|
|
|
return m, chain, err
|
2019-11-08 14:55:56 +00:00
|
|
|
}
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2021-04-22 17:11:37 +00:00
|
|
|
type blockTx func(_ *core.BlockGen, backend bind.ContractBackend) (types.Transaction, bool)
|
2019-11-08 10:21:44 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func getBlockTx(from libcommon.Address, to libcommon.Address, amount *uint256.Int) blockTx {
|
2021-04-22 17:11:37 +00:00
|
|
|
return func(block *core.BlockGen, _ bind.ContractBackend) (types.Transaction, bool) {
|
2020-06-04 07:43:08 +00:00
|
|
|
return types.NewTransaction(block.TxNonce(from), to, amount, 21000, new(uint256.Int), nil), false
|
2019-11-13 10:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func getBlockDeployTestContractTx(transactOpts *bind.TransactOpts, contractAddress *libcommon.Address, eipContract *contracts.Testcontract) blockTx {
|
2021-04-22 17:11:37 +00:00
|
|
|
return func(_ *core.BlockGen, backend bind.ContractBackend) (types.Transaction, bool) {
|
2020-05-06 08:59:50 +00:00
|
|
|
contractAddressRes, tx, eipContractRes, err := contracts.DeployTestcontract(transactOpts, backend)
|
2019-11-13 10:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*contractAddress = contractAddressRes
|
|
|
|
*eipContract = *eipContractRes
|
|
|
|
|
|
|
|
return tx, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:59:50 +00:00
|
|
|
func getBlockTestContractTx(transactOpts *bind.TransactOpts, contractCall interface{}, newBalance ...*big.Int) blockTx {
|
2021-04-22 17:11:37 +00:00
|
|
|
return func(_ *core.BlockGen, backend bind.ContractBackend) (types.Transaction, bool) {
|
2019-11-13 10:52:03 +00:00
|
|
|
var (
|
2021-04-22 17:11:37 +00:00
|
|
|
tx types.Transaction
|
2019-11-13 10:52:03 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch fn := contractCall.(type) {
|
2021-04-22 17:11:37 +00:00
|
|
|
case func(opts *bind.TransactOpts) (types.Transaction, error):
|
2019-11-13 10:52:03 +00:00
|
|
|
tx, err = fn(transactOpts)
|
2021-04-22 17:11:37 +00:00
|
|
|
case func(opts *bind.TransactOpts, newBalance *big.Int) (types.Transaction, error):
|
2019-11-13 10:52:03 +00:00
|
|
|
if len(newBalance) != 1 {
|
|
|
|
panic("*big.Int type new balance is expected")
|
|
|
|
}
|
|
|
|
tx, err = fn(transactOpts, newBalance[0])
|
|
|
|
default:
|
|
|
|
panic("non expected function type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx, true
|
2019-11-08 10:21:44 +00:00
|
|
|
}
|
|
|
|
}
|