github.com/dusk-network/dusk-blockchain@v0.6.1/pkg/core/consensus/agreement/handler_test.go (about) 1 // This Source Code Form is subject to the terms of the MIT License. 2 // If a copy of the MIT License was not distributed with this 3 // file, you can obtain one at https://opensource.org/licenses/MIT. 4 // 5 // Copyright (c) DUSK NETWORK. All rights reserved. 6 7 package agreement 8 9 import ( 10 "bytes" 11 "testing" 12 13 "github.com/dusk-network/dusk-blockchain/pkg/core/consensus" 14 "github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/message" 15 crypto "github.com/dusk-network/dusk-crypto/hash" 16 "github.com/sirupsen/logrus" 17 assert "github.com/stretchr/testify/require" 18 ) 19 20 // TestMockAgreementEvent tests the general layout of a mock Agreement (i.e. the BitSet). 21 func TestMockAgreementEvent(t *testing.T) { 22 p, keys := consensus.MockProvisioners(50) 23 hash, _ := crypto.RandEntropy(32) 24 ev := message.MockAgreement(hash, 1, 3, keys, p) 25 assert.NotEqual(t, 0, ev.VotesPerStep[0].BitSet) 26 assert.NotEqual(t, 0, ev.VotesPerStep[1].BitSet) 27 } 28 29 func TestVoteVerification(t *testing.T) { 30 // mocking voters 31 p, keys := consensus.MockProvisioners(3) 32 hash, _ := crypto.RandEntropy(32) 33 ev := message.MockAgreement(hash, 1, 3, keys, p) 34 handler := NewHandler(keys[0], *p, []byte{0, 0, 0, 0}) 35 assert.NoError(t, handler.Verify(ev), "problems in verification logic") 36 } 37 38 func TestGetVoterKeys(t *testing.T) { 39 p, keys := consensus.MockProvisioners(3) 40 hash, _ := crypto.RandEntropy(32) 41 ev := message.MockAgreement(hash, 1, 3, keys, p) 42 handler := NewHandler(keys[0], *p, []byte{0, 0, 0, 0}) 43 44 voterKeys, err := handler.getVoterKeys(ev) 45 assert.Nil(t, err) 46 47 // Ensure voterKeys only contains keys from `keys` 48 for _, key := range voterKeys { 49 found := false 50 51 for _, k := range keys { 52 if bytes.Equal(k.BLSPubKey, key) { 53 found = true 54 } 55 } 56 57 assert.True(t, found) 58 } 59 } 60 61 func BenchmarkAgreementVerification(b *testing.B) { 62 logrus.SetLevel(logrus.ErrorLevel) 63 64 const ( 65 workersCount = 4 66 provisionersCount = 64 67 msgCount = 50 68 69 round = uint64(999) 70 step = uint8(3) 71 ) 72 73 b.StopTimer() 74 75 // mocking voters 76 p, keys := consensus.MockProvisioners(provisionersCount) 77 hash, _ := crypto.RandEntropy(32) 78 ev := message.MockAgreement(hash, round, step, keys, p) 79 handler := NewHandler(keys[0], *p, []byte{0, 0, 0}) 80 81 // Pre-generate committees for this round first and second reduction steps so 82 // that handler.lock overhead is excluded 83 _ = handler.Committee(round, step-1) 84 _ = handler.Committee(round, step) 85 86 a := &Accumulator{ 87 handler: handler, 88 verificationChan: make(chan message.Agreement, 100), 89 eventChan: make(chan message.Agreement, 100), 90 CollectedVotesChan: make(chan []message.Agreement, 1), 91 storeMap: newStoreMap(), 92 workersQuitChan: make(chan struct{}), 93 } 94 95 a.CreateWorkers(workersCount) 96 97 b.StartTimer() 98 99 for i := 0; i < msgCount; i++ { 100 a.Process(ev) 101 } 102 103 // Drain all events to ensure all messages are processed 104 c := msgCount 105 for range a.eventChan { 106 c-- 107 if c == 0 { 108 return 109 } 110 } 111 112 b.StopTimer() 113 114 a.Stop() 115 }