add tests

This commit is contained in:
Arpit Temani 2022-09-22 13:19:59 +05:30
parent fe7ca40384
commit 9f85e71935
3 changed files with 409 additions and 110 deletions

View file

@ -1,11 +1,8 @@
//go:build integration
package bor
import (
"crypto/ecdsa"
"encoding/json"
"io/ioutil"
"math/big"
"os"
"testing"
@ -25,7 +22,7 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/assert"
"gotest.tools/assert"
)
var (
@ -36,9 +33,9 @@ var (
keys = []*ecdsa.PrivateKey{pkey1, pkey2}
)
func initMiner(genesis *core.Genesis, privKey *ecdsa.PrivateKey) (*node.Node, *eth.Ethereum, error) {
func InitMiner(genesis *core.Genesis, privKey *ecdsa.PrivateKey) (*node.Node, *eth.Ethereum, error) {
// Define the basic configurations for the Ethereum node
datadir, _ := ioutil.TempDir("", "")
datadir := os.TempDir()
config := &node.Config{
Name: "geth",
@ -94,10 +91,10 @@ func initMiner(genesis *core.Genesis, privKey *ecdsa.PrivateKey) (*node.Node, *e
return stack, ethBackend, err
}
func initGenesis(t *testing.T, faucets []*ecdsa.PrivateKey) *core.Genesis {
func InitGenesis(t *testing.T, faucets []*ecdsa.PrivateKey, fileLocation string) *core.Genesis {
// sprint size = 8 in genesis
genesisData, err := ioutil.ReadFile("./testdata/genesis_2val.json")
genesisData, err := os.ReadFile(fileLocation)
if err != nil {
t.Fatalf("%s", err)
}
@ -114,7 +111,8 @@ func initGenesis(t *testing.T, faucets []*ecdsa.PrivateKey) *core.Genesis {
return genesis
}
func TestValidatorWentOffline(t *testing.T) {
// Sprint length change tests
func TestValidatorsBlockProduction(t *testing.T) {
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
fdlimit.Raise(2048)
@ -126,16 +124,15 @@ func TestValidatorWentOffline(t *testing.T) {
}
// Create an Ethash network based off of the Ropsten config
genesis := initGenesis(t, faucets)
genesis := InitGenesis(t, faucets, "./testdata/genesis_sprint_length_change.json")
var (
stacks []*node.Node
nodes []*eth.Ethereum
enodes []*enode.Node
)
for i := 0; i < 2; i++ {
// Start the node and wait until it's up
stack, ethBackend, err := initMiner(genesis, keys[i])
stack, ethBackend, err := InitMiner(genesis, keys[i])
if err != nil {
panic(err)
}
@ -149,7 +146,6 @@ func TestValidatorWentOffline(t *testing.T) {
stack.Server().AddPeer(n)
}
// Start tracking the node and its enode
stacks = append(stacks, stack)
nodes = append(nodes, ethBackend)
enodes = append(enodes, stack.Server().Self())
}
@ -164,36 +160,21 @@ func TestValidatorWentOffline(t *testing.T) {
for {
// for block 1 to 8, the primary validator is node0
// for block 9 to 16, the primary validator is node1
// for block 17 to 24, the primary validator is node0
// for block 25 to 32, the primary validator is node1
// for block 0 to 7, the primary validator is node0
// for block 8 to 15, the primary validator is node1
// for block 16 to 19, the primary validator is node0
// for block 20 to 23, the primary validator is node1
blockHeaderVal0 := nodes[0].BlockChain().CurrentHeader()
// we remove peer connection between node0 and node1
if blockHeaderVal0.Number.Uint64() == 9 {
stacks[0].Server().RemovePeer(enodes[1])
}
// here, node1 is the primary validator, node0 will sign out-of-turn
// we add peer connection between node1 and node0
if blockHeaderVal0.Number.Uint64() == 14 {
stacks[0].Server().AddPeer(enodes[1])
}
// reorg happens here, node1 has higher difficulty, it will replace blocks by node0
if blockHeaderVal0.Number.Uint64() == 30 {
if blockHeaderVal0.Number.Uint64() == 24 {
break
}
}
// check block 10 miner ; expected author is node1 signer
blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(10)
blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(10)
// check block 7 miner ; expected author is node0 signer
blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(7)
blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(7)
authorVal0, err := nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
@ -203,76 +184,64 @@ func TestValidatorWentOffline(t *testing.T) {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 10
// check both nodes have the same block 7
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[1].AccountManager().Accounts()[0])
// check block 11 miner ; expected author is node1 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(11)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(11)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 11
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[1].AccountManager().Accounts()[0])
// check block 12 miner ; expected author is node1 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(12)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(12)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 12
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[1].AccountManager().Accounts()[0])
// check block 17 miner ; expected author is node0 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(17)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(17)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 17
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
// check block mined by node0
assert.Equal(t, authorVal0, nodes[0].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[0].AccountManager().Accounts()[0])
// check block 15 miner ; expected author is node1 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(15)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(15)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 15
assert.Equal(t, authorVal0, authorVal1)
// check block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check block 19 miner ; expected author is node0 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(19)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(19)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 19
assert.Equal(t, authorVal0, authorVal1)
// check block mined by node0
assert.Equal(t, authorVal0, nodes[0].AccountManager().Accounts()[0])
// check block 23 miner ; expected author is node1 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(23)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(23)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 23
assert.Equal(t, authorVal0, authorVal1)
// check block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
}

View file

@ -4,18 +4,25 @@ package bor
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"math/big"
"os"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ethereum/go-ethereum/consensus/bor"
"github.com/ethereum/go-ethereum/consensus/bor/clerk"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
@ -26,11 +33,268 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/tests/bor/mocks"
)
var (
// addr1 = 0x71562b71999873DB5b286dF957af199Ec94617F7
pkey1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
// addr2 = 0x9fB29AAc15b9A4B7F17c3385939b007540f4d791
pkey2, _ = crypto.HexToECDSA("9b28f36fbd67381120752d6172ecdcf10e06ab2d9a1367aac00cdcd6ac7855d3")
keys = []*ecdsa.PrivateKey{pkey1, pkey2}
)
func InitMiner(genesis *core.Genesis, privKey *ecdsa.PrivateKey) (*node.Node, *eth.Ethereum, error) {
// Define the basic configurations for the Ethereum node
datadir, _ := ioutil.TempDir("", "")
config := &node.Config{
Name: "geth",
Version: params.Version,
DataDir: datadir,
P2P: p2p.Config{
ListenAddr: "0.0.0.0:0",
NoDiscovery: true,
MaxPeers: 25,
},
UseLightweightKDF: true,
}
// Create the node and configure a full Ethereum node on it
stack, err := node.New(config)
if err != nil {
return nil, nil, err
}
ethBackend, err := eth.New(stack, &ethconfig.Config{
Genesis: genesis,
NetworkId: genesis.Config.ChainID.Uint64(),
SyncMode: downloader.FullSync,
DatabaseCache: 256,
DatabaseHandles: 256,
TxPool: core.DefaultTxPoolConfig,
GPO: ethconfig.Defaults.GPO,
Ethash: ethconfig.Defaults.Ethash,
Miner: miner.Config{
Etherbase: crypto.PubkeyToAddress(privKey.PublicKey),
GasCeil: genesis.GasLimit * 11 / 10,
GasPrice: big.NewInt(1),
Recommit: time.Second,
},
WithoutHeimdall: true,
})
if err != nil {
return nil, nil, err
}
// register backend to account manager with keystore for signing
keydir := stack.KeyStoreDir()
n, p := keystore.StandardScryptN, keystore.StandardScryptP
kStore := keystore.NewKeyStore(keydir, n, p)
kStore.ImportECDSA(privKey, "")
acc := kStore.Accounts()[0]
kStore.Unlock(acc, "")
// proceed to authorize the local account manager in any case
ethBackend.AccountManager().AddBackend(kStore)
// ethBackend.AccountManager().AddBackend()
err = stack.Start()
return stack, ethBackend, err
}
func InitGenesis(t *testing.T, faucets []*ecdsa.PrivateKey, fileLocation string) *core.Genesis {
// sprint size = 8 in genesis
genesisData, err := ioutil.ReadFile(fileLocation)
if err != nil {
t.Fatalf("%s", err)
}
genesis := &core.Genesis{}
if err := json.Unmarshal(genesisData, genesis); err != nil {
t.Fatalf("%s", err)
}
genesis.Config.ChainID = big.NewInt(15001)
genesis.Config.EIP150Hash = common.Hash{}
return genesis
}
func TestValidatorWentOffline(t *testing.T) {
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
fdlimit.Raise(2048)
// Generate a batch of accounts to seal and fund with
faucets := make([]*ecdsa.PrivateKey, 128)
for i := 0; i < len(faucets); i++ {
faucets[i], _ = crypto.GenerateKey()
}
// Create an Ethash network based off of the Ropsten config
genesis := InitGenesis(t, faucets, "./testdata/genesis_2val.json")
var (
stacks []*node.Node
nodes []*eth.Ethereum
enodes []*enode.Node
)
for i := 0; i < 2; i++ {
// Start the node and wait until it's up
stack, ethBackend, err := InitMiner(genesis, keys[i])
if err != nil {
panic(err)
}
defer stack.Close()
for stack.Server().NodeInfo().Ports.Listener == 0 {
time.Sleep(250 * time.Millisecond)
}
// Connect the node to all the previous ones
for _, n := range enodes {
stack.Server().AddPeer(n)
}
// Start tracking the node and its enode
stacks = append(stacks, stack)
nodes = append(nodes, ethBackend)
enodes = append(enodes, stack.Server().Self())
}
// Iterate over all the nodes and start mining
time.Sleep(3 * time.Second)
for _, node := range nodes {
if err := node.StartMining(1); err != nil {
panic(err)
}
}
for {
// for block 1 to 8, the primary validator is node0
// for block 9 to 16, the primary validator is node1
// for block 17 to 24, the primary validator is node0
// for block 25 to 32, the primary validator is node1
blockHeaderVal0 := nodes[0].BlockChain().CurrentHeader()
// we remove peer connection between node0 and node1
if blockHeaderVal0.Number.Uint64() == 9 {
stacks[0].Server().RemovePeer(enodes[1])
}
// here, node1 is the primary validator, node0 will sign out-of-turn
// we add peer connection between node1 and node0
if blockHeaderVal0.Number.Uint64() == 14 {
stacks[0].Server().AddPeer(enodes[1])
}
// reorg happens here, node1 has higher difficulty, it will replace blocks by node0
if blockHeaderVal0.Number.Uint64() == 30 {
break
}
}
// check block 10 miner ; expected author is node1 signer
blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(10)
blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(10)
authorVal0, err := nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err := nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 10
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[1].AccountManager().Accounts()[0])
// check block 11 miner ; expected author is node1 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(11)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(11)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 11
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[1].AccountManager().Accounts()[0])
// check block 12 miner ; expected author is node1 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(12)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(12)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 12
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[1].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[1].AccountManager().Accounts()[0])
// check block 17 miner ; expected author is node0 signer
blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(17)
blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(17)
authorVal0, err = nodes[0].Engine().Author(blockHeaderVal0)
if err != nil {
log.Error("Error in getting author", "err", err)
}
authorVal1, err = nodes[1].Engine().Author(blockHeaderVal1)
if err != nil {
log.Error("Error in getting author", "err", err)
}
// check both nodes have the same block 17
assert.Equal(t, authorVal0, authorVal1)
// check node0 has block mined by node1
assert.Equal(t, authorVal0, nodes[0].AccountManager().Accounts()[0])
// check node1 has block mined by node1
assert.Equal(t, authorVal1, nodes[0].AccountManager().Accounts()[0])
}
func TestInsertingSpanSizeBlocks(t *testing.T) {
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
chain := init.ethereum.BlockChain()
@ -341,13 +605,13 @@ func TestSignerNotFound(t *testing.T) {
// TestEIP1559Transition tests the following:
//
// 1. A transaction whose gasFeeCap is greater than the baseFee is valid.
// 2. Gas accounting for access lists on EIP-1559 transactions is correct.
// 3. Only the transaction's tip will be received by the coinbase.
// 4. The transaction sender pays for both the tip and baseFee.
// 5. The coinbase receives only the partially realized tip when
// gasFeeCap - gasTipCap < baseFee.
// 6. Legacy transaction behave as expected (e.g. gasPrice = gasFeeCap = gasTipCap).
// 1. A transaction whose gasFeeCap is greater than the baseFee is valid.
// 2. Gas accounting for access lists on EIP-1559 transactions is correct.
// 3. Only the transaction's tip will be received by the coinbase.
// 4. The transaction sender pays for both the tip and baseFee.
// 5. The coinbase receives only the partially realized tip when
// gasFeeCap - gasTipCap < baseFee.
// 6. Legacy transaction behave as expected (e.g. gasPrice = gasFeeCap = gasTipCap).
func TestEIP1559Transition(t *testing.T) {
var (
aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa")

File diff suppressed because one or more lines are too long