-
Notifications
You must be signed in to change notification settings - Fork 42
/
ledger_test.go
66 lines (47 loc) · 1.92 KB
/
ledger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// +build unit
package wavelet
import (
"github.com/perlin-network/wavelet/conf"
"github.com/perlin-network/wavelet/sys"
"github.com/stretchr/testify/assert"
"testing"
)
func TestLedger_FilterInvalidVotes(t *testing.T) {
testnet, err := NewTestNetwork()
if !assert.NoError(t, err) {
return
}
defer testnet.Cleanup()
alice, err := testnet.AddNode()
if !assert.NoError(t, err) {
return
}
var transactions []*Transaction
var ids []TransactionID
current := NewBlock(uint64(100+conf.GetPruningLimit()), alice.ledger.accounts.tree.Checksum(), ids...)
// Create transactions.
for i := 0; i < conf.GetSnowballK()*2; i++ {
tx := NewTransaction(alice.Keys(), uint64(i), current.Index, sys.TagTransfer, nil)
transactions = append(transactions, &tx)
ids = append(ids, tx.ID)
}
alice.ledger.transactions.BatchUnsafeAdd(transactions)
// Crete a single transaction with an invalid height.
invalid := NewTransaction(alice.Keys(), uint64(conf.GetSnowballK()*2), current.Index-uint64(conf.GetPruningLimit()), sys.TagTransfer, nil)
alice.ledger.transactions.BatchUnsafeAdd([]*Transaction{&invalid})
// Create valid block proposals.
var proposals []Block
for i := 1; i <= conf.GetSnowballK(); i++ {
proposal := NewBlock(current.Index+1, alice.ledger.accounts.tree.Checksum(), ids[:i]...)
proposals = append(proposals, proposal)
}
// Create a single block proposal containing the transaction with invalid height.
proposals = append(proposals, NewBlock(current.Index+1, alice.ledger.accounts.tree.Checksum(), append(ids, invalid.ID)...))
votes := make([]Vote, 0, len(proposals))
for _, proposal := range proposals {
votes = append(votes, &finalizationVote{voter: alice.client.ID(), block: &proposal, tally: float64(1) / float64(len(proposals))})
}
assert.NotNil(t, votes[len(votes)-1].(*finalizationVote).block)
alice.ledger.filterInvalidVotes(¤t, votes)
assert.Nil(t, votes[len(votes)-1].(*finalizationVote).block)
}