Skip to content

Commit

Permalink
Merge pull request #458 from ElrondNetwork/EN-4088-integration-test-b…
Browse files Browse the repository at this point in the history
…lock-double-sign

En 4088 integration test block double sign
  • Loading branch information
iulianpascalau authored Sep 23, 2019
2 parents 223a27e + 68452d5 commit 1bd5484
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
103 changes: 102 additions & 1 deletion integrationTests/sync/basicSync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/integrationTests"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/stretchr/testify/assert"
)

var stepDelay = time.Second
var delayP2pBootstrap = time.Second * 2
var stepSync = time.Second * 2

func TestSyncWorksInShard_EmptyBlocksNoForks(t *testing.T) {
if testing.Short() {
Expand Down Expand Up @@ -74,16 +77,101 @@ func TestSyncWorksInShard_EmptyBlocksNoForks(t *testing.T) {
for i := 0; i < numRoundsToTest; i++ {
integrationTests.ProposeBlock(nodes, idxProposers, round, nonce)

time.Sleep(stepDelay)
time.Sleep(stepSync)

round = integrationTests.IncrementAndPrintRound(round)
updateRound(nodes, round)
nonce++
}

time.Sleep(stepSync)

testAllNodesHaveTheSameBlockHeightInBlockchain(t, nodes)
}

func TestSyncWorksInShard_EmptyBlocksDoubleSign(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
}

maxShards := uint32(1)
shardId := uint32(0)
numNodesPerShard := 6

advertiser := integrationTests.CreateMessengerWithKadDht(context.Background(), "")
_ = advertiser.Bootstrap()
advertiserAddr := integrationTests.GetConnectableAddress(advertiser)

nodes := make([]*integrationTests.TestProcessorNode, numNodesPerShard)
for i := 0; i < numNodesPerShard; i++ {
nodes[i] = integrationTests.NewTestSyncNode(
maxShards,
shardId,
shardId,
advertiserAddr,
)
}

idxProposerShard0 := 0
idxProposers := []int{idxProposerShard0}

defer func() {
_ = advertiser.Close()
for _, n := range nodes {
_ = n.Messenger.Close()
}
}()

for _, n := range nodes {
_ = n.Messenger.Bootstrap()
_ = n.StartSync()
}

fmt.Println("Delaying for nodes p2p bootstrap...")
time.Sleep(delayP2pBootstrap)

round := uint64(0)
nonce := uint64(0)
round = integrationTests.IncrementAndPrintRound(round)
updateRound(nodes, round)
nonce++

numRoundsToTest := 2
for i := 0; i < numRoundsToTest; i++ {
integrationTests.ProposeBlock(nodes, idxProposers, round, nonce)

time.Sleep(stepSync)

round = integrationTests.IncrementAndPrintRound(round)
updateRound(nodes, round)
nonce++
}

time.Sleep(stepSync)

pubKeysVariant1 := []byte("1")
pubKeysVariant2 := []byte("2")

proposeBlockWithPubKeyBitmap(nodes[idxProposerShard0], round, nonce, pubKeysVariant1)
proposeBlockWithPubKeyBitmap(nodes[1], round, nonce, pubKeysVariant2)

time.Sleep(stepDelay)

round = integrationTests.IncrementAndPrintRound(round)
updateRound(nodes, round)

stepDelayForkResolving := 4 * stepDelay
time.Sleep(stepDelayForkResolving)

testAllNodesHaveTheSameBlockHeightInBlockchain(t, nodes)
testAllNodesHaveSameLastBlock(t, nodes)
}

func proposeBlockWithPubKeyBitmap(n *integrationTests.TestProcessorNode, round uint64, nonce uint64, pubKeys []byte) {
body, header, _ := n.ProposeBlock(round, nonce)
header.SetPubKeysBitmap(pubKeys)
n.BroadcastBlock(body, header)
n.CommitBlock(body, header)
}

func testAllNodesHaveTheSameBlockHeightInBlockchain(t *testing.T, nodes []*integrationTests.TestProcessorNode) {
Expand All @@ -97,6 +185,19 @@ func testAllNodesHaveTheSameBlockHeightInBlockchain(t *testing.T, nodes []*integ
}
}

func testAllNodesHaveSameLastBlock(t *testing.T, nodes []*integrationTests.TestProcessorNode) {
mapBlocksByHash := make(map[string]data.HeaderHandler)

for _, n := range nodes {
hdr := n.BlockChain.GetCurrentBlockHeader()
buff, _ := core.CalculateHash(integrationTests.TestMarshalizer, integrationTests.TestHasher, hdr)

mapBlocksByHash[string(buff)] = hdr
}

assert.Equal(t, 1, len(mapBlocksByHash))
}

func updateRound(nodes []*integrationTests.TestProcessorNode, round uint64) {
for _, n := range nodes {
n.Rounder.IndexField = int64(round)
Expand Down
2 changes: 1 addition & 1 deletion integrationTests/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func IncrementAndPrintRound(round uint64) uint64 {
return round
}

// ProposeBlock proposes a block with SC txs for every shard
// ProposeBlock proposes a block for every shard
func ProposeBlock(nodes []*TestProcessorNode, idxProposers []int, round uint64, nonce uint64) {
fmt.Println("All shards propose blocks...")
for idx, n := range nodes {
Expand Down

0 comments on commit 1bd5484

Please sign in to comment.