go-ethereum/consensus/XDPoS/engines/engine_v2/snapshot_test.go
Liam 6c5fe34615 v2 miner function implementation and happy path (#22)
* New struct in consensus/XDPoS/utils/types.go, util functions, and test. (#14)

* define vote, timeout, sync info, qc, tc, extra fields in types.go, add test in types_test.go

* add json tag in types.go, refine encoder decoder of extra fields

* refactor types.go utils.go

* re-write types, comments

* add Hash SigHash for types, and tests

* define Round type

* remove unnecessary logs

* add v2 engine functions placeholder

* typo fix on the consensus v2 function placeholders

* add countdown timer

* make initilised private to countdown

* add v2 specific config struct

* rename some config variables

* Implement BFT Message receiver (#13)

* fix or skip tests due to PR-136 changes

* add bft receiver functions

* add bft receiver functions

* rename tc to TimeoutCert

* implement more functions

* New struct in consensus/XDPoS/utils/types.go, util functions, and test. (#14)

* define vote, timeout, sync info, qc, tc, extra fields in types.go, add test in types_test.go

* add json tag in types.go, refine encoder decoder of extra fields

* refactor types.go utils.go

* re-write types, comments

* add Hash SigHash for types, and tests

* define Round type

* remove unnecessary logs

* add temp functions

* add v2 engine functions placeholder

* typo fix on the consensus v2 function placeholders

* add countdown timer

* make initilised private to countdown

* push verify function

* add test on receiving vote

* revert type change

* add async on broadcast function

* add quit initial

* fix test

Co-authored-by: Jianrong <wjrjerome@gmail.com>
Co-authored-by: wgr523 <wgr523@gmail.com>

* generate and verify timeout message

* Consensus V2 variable, timeout pool (#19)

* fill in XDPoS_v2 variables and processQC/TC

* add timeout pool, refine engine variables

* refactor type functions

* solve a small pointer bug

* create general pool and its test, refine engine

* refine pool, add xdpos v2 config cert threshold

* refine config

* vote and timeout handlers

* fix pool test

* bft miner preparation

* review comment improvement

* update

* relocate tests

* add and remove comment

* fix the syntax error

* update network layer and add handler functions (#23)

* update network layer and add handler functions

* fix test syntax error

* add ProcessQC implementation

* add ProcessQC tests

* add snapshot test

* add wait qc process

* remove testing files

* add route snapshot

* fix merge issue

* add default v2 behaviour (#24)

* add v2 ecrecover functions and refactor test

* fix all the tests

* put minimun lock variable

* debugging prepare and seal v2 blocks

* Trigger proposeBlockHandler after v2 block received and verified in fetcher

* skip snapshot apply related tests

* update test check

* rename bfter to bft handler and ignore normal behviour

* fix bugs during local 4 node run

* fix test

* fix sync info test

* fix bugs during local 4 node run

* rebase and fix bug

* remove hook validators function"

Co-authored-by: wgr523 <wgr523@gmail.com>
Co-authored-by: Jianrong <wjrjerome@gmail.com>
2021-12-30 11:45:18 +11:00

122 lines
3.2 KiB
Go

package engine_v2
import (
"fmt"
"io/ioutil"
"math/big"
"testing"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/ethdb/leveldb"
"github.com/stretchr/testify/assert"
)
func TestGetMasterNodes(t *testing.T) {
masterNodes := []common.Address{
{4}, {3}, {2}, {1},
}
snap := newSnapshot(nil, 1, common.Hash{}, utils.Round(1), nil, masterNodes)
sortedNodes := snap.GetMasterNodes()
for i := range masterNodes {
if masterNodes[i] != sortedNodes[3-i] {
t.Error("should get sorted master nodes list", i, sortedNodes[i])
return
}
}
}
func TestApplyNewSnapshot(t *testing.T) {
t.Skip("apply has been temporary commented out")
snap := newSnapshot(nil, 1, common.Hash{}, utils.Round(1), nil, nil)
extra := utils.ExtraFields_v2{
Round: 10,
QuorumCert: &utils.QuorumCert{
ProposedBlockInfo: &utils.BlockInfo{},
},
}
extraBytes, err := extra.EncodeToBytes()
assert.Nil(t, err)
headers := []*types.Header{
{Number: big.NewInt(2)},
{Number: big.NewInt(3)},
{Number: big.NewInt(4)},
{
Number: big.NewInt(5),
Extra: extraBytes,
},
}
newSnap, err := snap.apply(headers)
assert.Nil(t, err)
if newSnap.Number != 5 {
t.Error("newSnapshot number should have last header number")
}
if newSnap.Hash != headers[3].Hash() {
t.Error("newSnapshot hash should equal the last header given")
}
if newSnap.Round != 10 {
t.Error("newSnapshot round number should also have last header round number")
}
}
func TestApplyWithWrongHeader(t *testing.T) {
t.Skip("apply has been temporary commented out")
snap := newSnapshot(nil, 1, common.Hash{}, utils.Round(1), nil, nil)
headers := []*types.Header{
{Number: big.NewInt(3)},
}
_, err := snap.apply(headers)
assert.Equal(t, err, utils.ErrInvalidChild)
snap = newSnapshot(nil, 1, common.Hash{}, utils.Round(1), nil, nil)
headers = []*types.Header{
{Number: big.NewInt(2)},
{Number: big.NewInt(4)},
}
_, err = snap.apply(headers)
assert.Equal(t, err, utils.ErrInvalidHeaderOrder)
}
// Should perform deep copy
func TestCopySnapshot(t *testing.T) {
masterNodes := []common.Address{
{4}, {3}, {2}, {1},
}
snap := newSnapshot(nil, 1, common.Hash{}, utils.Round(1), nil, masterNodes)
newSnapshot := snap.copy()
if newSnapshot == snap {
t.Error("should return given different memory address")
}
for node := range snap.MasterNodes {
if _, ok := newSnapshot.MasterNodes[node]; !ok {
t.Error("snapshot masternodes should copy to new object")
}
}
}
func TestStoreLoadSnapshot(t *testing.T) {
snap := newSnapshot(nil, 1, common.Hash{0x1}, utils.Round(1), nil, nil)
dir, err := ioutil.TempDir("", "snapshot-test")
if err != nil {
panic(fmt.Sprintf("can't create temporary directory: %v", err))
}
db, err := leveldb.New(dir, 256, 0, "")
if err != nil {
panic(fmt.Sprintf("can't create temporary database: %v", err))
}
lddb := rawdb.NewDatabase(db)
err = storeSnapshot(snap, lddb)
if err != nil {
t.Error("store snapshot failed", err)
}
restoredSnapshot, err := loadSnapshot(nil, lddb, snap.Hash)
if err != nil || restoredSnapshot.Hash != snap.Hash {
t.Error("load snapshot failed", err)
}
}