Skip to content

Commit

Permalink
update AddBlocksToStateWithFixedBranches
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jan 31, 2022
1 parent 0ef9ea3 commit 6b841f4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestHandleChainReorg_WithReorg_NoTransactions(t *testing.T) {
height := 5
branch := 3
branches := map[int]int{branch: 1}
state.AddBlocksToStateWithFixedBranches(t, s.blockState.(*state.BlockState), height, branches, 0)
state.AddBlocksToStateWithFixedBranches(t, s.blockState.(*state.BlockState), height, branches)

leaves := s.blockState.(*state.BlockState).Leaves()
require.Equal(t, 2, len(leaves))
Expand Down
8 changes: 8 additions & 0 deletions dot/rpc/modules/system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,19 @@ func setupSystemModule(t *testing.T) *SystemModule {

err = chain.Storage.StoreTrie(ts, nil)
require.NoError(t, err)

digest := types.NewDigest()
prd, err := types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()
require.NoError(t, err)
err = digest.Add(*prd)
require.NoError(t, err)

err = chain.Block.AddBlock(&types.Block{
Header: types.Header{
Number: big.NewInt(3),
ParentHash: chain.Block.BestBlockHash(),
StateRoot: ts.MustRoot(),
Digest: digest,
},
Body: types.Body{},
})
Expand Down
2 changes: 1 addition & 1 deletion dot/state/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int,

// AddBlocksToStateWithFixedBranches adds blocks to a BlockState up to depth, with fixed branches
// branches are provided with a map of depth -> # of branches
func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, depth int, branches map[int]int, r byte) {
func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, depth int, branches map[int]int) {
previousHash := blockState.BestBlockHash()
tb := []testBranch{}
arrivalTime := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func TestService_checkOrGetDescendantHash(t *testing.T) {
branches := map[int]int{
8: 1,
}
state.AddBlocksToStateWithFixedBranches(t, s.blockState.(*state.BlockState), 16, branches, 1)
state.AddBlocksToStateWithFixedBranches(t, s.blockState.(*state.BlockState), 16, branches)

// base case
ancestor, err := s.blockState.GetHashByNumber(big.NewInt(1))
Expand Down
47 changes: 23 additions & 24 deletions lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package grandpa

import (
"math/big"
"math/rand"
"sort"
"sync"
"testing"
Expand Down Expand Up @@ -181,7 +180,7 @@ func TestGetVotesForBlock_NoDescendantVotes(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

// 1/3 of voters equivocate; ie. vote for both blocks
Expand Down Expand Up @@ -218,7 +217,7 @@ func TestGetVotesForBlock_DescendantVotes(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

a, err := st.Block.GetHeader(leaves[0])
Expand Down Expand Up @@ -270,7 +269,7 @@ func TestGetPossibleSelectedAncestors_SameAncestor(t *testing.T) {
// this creates a tree with 3 branches all starting at depth 6
branches := make(map[int]int)
branches[6] = 2
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, 0)
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)

leaves := gs.blockState.Leaves()
require.Equal(t, 3, len(leaves))
Expand Down Expand Up @@ -326,7 +325,7 @@ func TestGetPossibleSelectedAncestors_VaryingAncestor(t *testing.T) {
branches := make(map[int]int)
branches[6] = 1
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)

leaves := gs.blockState.Leaves()
require.Equal(t, 3, len(leaves))
Expand Down Expand Up @@ -382,7 +381,7 @@ func TestGetPossibleSelectedAncestors_VaryingAncestor_MoreBranches(t *testing.T)
branches := make(map[int]int)
branches[6] = 2
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)

leaves := gs.blockState.Leaves()
require.Equal(t, 4, len(leaves))
Expand Down Expand Up @@ -442,7 +441,7 @@ func TestGetPossibleSelectedBlocks_OneBlock(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -477,7 +476,7 @@ func TestGetPossibleSelectedBlocks_EqualVotes_SameAncestor(t *testing.T) {
branches := make(map[int]int)
branches[6] = 2

state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()
require.Equal(t, 3, len(leaves))

Expand Down Expand Up @@ -525,7 +524,7 @@ func TestGetPossibleSelectedBlocks_EqualVotes_VaryingAncestor(t *testing.T) {
branches := make(map[int]int)
branches[6] = 1
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)

leaves := gs.blockState.Leaves()
require.Equal(t, 3, len(leaves))
Expand Down Expand Up @@ -573,7 +572,7 @@ func TestGetPossibleSelectedBlocks_OneThirdEquivocating(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

// 1/3 of voters equivocate; ie. vote for both blocks
Expand Down Expand Up @@ -616,7 +615,7 @@ func TestGetPossibleSelectedBlocks_MoreThanOneThirdEquivocating(t *testing.T) {
branches := make(map[int]int)
branches[6] = 1
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

// this tests a byzantine case where >1/3 of voters equivocate; ie. vote for multiple blocks
Expand Down Expand Up @@ -664,7 +663,7 @@ func TestGetPreVotedBlock_OneBlock(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -698,7 +697,7 @@ func TestGetPreVotedBlock_MultipleCandidates(t *testing.T) {
branches := make(map[int]int)
branches[6] = 1
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)

leaves := gs.blockState.Leaves()
require.Equal(t, 3, len(leaves))
Expand Down Expand Up @@ -749,7 +748,7 @@ func TestGetPreVotedBlock_EvenMoreCandidates(t *testing.T) {
branches[5] = 1
branches[6] = 1
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(0))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)

leaves := gs.blockState.Leaves()
require.Equal(t, 6, len(leaves))
Expand Down Expand Up @@ -817,7 +816,7 @@ func TestIsCompletable(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -855,7 +854,7 @@ func TestFindParentWithNumber(t *testing.T) {

// no branches needed
branches := make(map[int]int)
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

v, err := NewVoteFromHash(leaves[0], st.Block)
Expand All @@ -877,7 +876,7 @@ func TestGetBestFinalCandidate_OneBlock(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -916,7 +915,7 @@ func TestGetBestFinalCandidate_PrecommitAncestor(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -960,7 +959,7 @@ func TestGetBestFinalCandidate_NoPrecommit(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -997,7 +996,7 @@ func TestGetBestFinalCandidate_PrecommitOnAnotherChain(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -1093,7 +1092,7 @@ func TestIsFinalisable_True(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -1131,7 +1130,7 @@ func TestIsFinalisable_False(t *testing.T) {

branches := make(map[int]int)
branches[2] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 3, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 3, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -1176,7 +1175,7 @@ func TestGetGrandpaGHOST_CommonAncestor(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -1213,7 +1212,7 @@ func TestGetGrandpaGHOST_MultipleCandidates(t *testing.T) {
branches := make(map[int]int)
branches[3] = 1
branches[7] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, byte(rand.Intn(256)))
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()
require.Equal(t, 3, len(leaves))

Expand Down
3 changes: 1 addition & 2 deletions lib/grandpa/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ func TestPlayGrandpaRound_WithEquivocation(t *testing.T) {
fins := make([]chan GrandpaMessage, len(kr.Keys))

done := false
r := byte(rand.Intn(256))

for i := range gss {
gs, in, out, fin := setupGrandpa(t, kr.Keys[i])
Expand All @@ -434,7 +433,7 @@ func TestPlayGrandpaRound_WithEquivocation(t *testing.T) {
// this creates a tree with 2 branches starting at depth 2
branches := make(map[int]int)
branches[2] = 1
state.AddBlocksToStateWithFixedBranches(t, gs.blockState.(*state.BlockState), 4, branches, r)
state.AddBlocksToStateWithFixedBranches(t, gs.blockState.(*state.BlockState), 4, branches)
}

// should have blocktree for all nodes
Expand Down
8 changes: 4 additions & 4 deletions lib/grandpa/vote_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestCheckForEquivocation_WithEquivocation(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, 0)
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

vote1, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestCheckForEquivocation_WithExistingEquivocation(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, 0)
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

vote1, err := NewVoteFromHash(leaves[1], gs.blockState)
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestValidateMessage_Equivocation(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, 0)
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

voteA, err := NewVoteFromHash(leaves[0], st.Block)
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestValidateMessage_IsNotDescendant(t *testing.T) {

branches := make(map[int]int)
branches[6] = 1
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches, 0)
state.AddBlocksToStateWithFixedBranches(t, st.Block, 8, branches)
leaves := gs.blockState.Leaves()

gs.head, err = gs.blockState.GetHeader(leaves[0])
Expand Down

0 comments on commit 6b841f4

Please sign in to comment.