forked from yihuang/go-block-stm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm_test.go
174 lines (158 loc) · 3.93 KB
/
stm_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package block_stm
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math/rand"
"testing"
storetypes "cosmossdk.io/store/types"
"github.com/test-go/testify/require"
)
func accountName(i int64) string {
return fmt.Sprintf("account%05d", i)
}
func testBlock(size int, accounts int) *MockBlock {
txs := make([]Tx, size)
g := rand.New(rand.NewSource(0))
for i := 0; i < size; i++ {
sender := g.Int63n(int64(accounts))
receiver := g.Int63n(int64(accounts))
txs[i] = BankTransferTx(i, accountName(sender), accountName(receiver), 1)
}
return NewMockBlock(txs)
}
func iterateBlock(size int, accounts int) *MockBlock {
txs := make([]Tx, size)
g := rand.New(rand.NewSource(0))
for i := 0; i < size; i++ {
sender := g.Int63n(int64(accounts))
receiver := g.Int63n(int64(accounts))
txs[i] = IterateTx(i, accountName(sender), accountName(receiver), 1)
}
return NewMockBlock(txs)
}
func noConflictBlock(size int) *MockBlock {
txs := make([]Tx, size)
for i := 0; i < size; i++ {
sender := accountName(int64(i))
txs[i] = BankTransferTx(i, sender, sender, 1)
}
return NewMockBlock(txs)
}
func worstCaseBlock(size int) *MockBlock {
txs := make([]Tx, size)
for i := 0; i < size; i++ {
// all transactions are from the same account
sender := "account0"
txs[i] = BankTransferTx(i, sender, sender, 1)
}
return NewMockBlock(txs)
}
func determisticBlock() *MockBlock {
return NewMockBlock([]Tx{
NoopTx(0, "account0"),
NoopTx(1, "account1"),
NoopTx(2, "account1"),
NoopTx(3, "account1"),
NoopTx(4, "account3"),
NoopTx(5, "account1"),
NoopTx(6, "account4"),
NoopTx(7, "account5"),
NoopTx(8, "account6"),
})
}
func TestSTM(t *testing.T) {
stores := map[storetypes.StoreKey]int{StoreKeyAuth: 0, StoreKeyBank: 1}
testCases := []struct {
name string
blk *MockBlock
executors int
}{
{
name: "testBlock(100,80),10",
blk: testBlock(100, 80),
executors: 10,
},
{
name: "testBlock(100,3),10",
blk: testBlock(100, 3),
executors: 10,
},
{
name: "determisticBlock(),5",
blk: determisticBlock(),
executors: 5,
},
{
name: "noConflictBlock(100),5",
blk: noConflictBlock(100),
executors: 5,
},
{
name: "worstCaseBlock(100),5",
blk: worstCaseBlock(100),
executors: 5,
},
{
name: "iterateBlock(100,80),10",
blk: iterateBlock(100, 80),
executors: 10,
},
{
name: "iterateBlock(100,10),10",
blk: iterateBlock(100, 10),
executors: 10,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
storage := NewMultiMemDB(stores)
require.NoError(t,
ExecuteBlock(context.Background(), tc.blk.Size(), stores, storage, tc.executors, tc.blk.ExecuteTx),
)
for _, err := range tc.blk.Results {
require.NoError(t, err)
}
crossCheck := NewMultiMemDB(stores)
runSequential(crossCheck, tc.blk)
// check parallel execution matches sequential execution
for store := range stores {
require.True(t, StoreEqual(crossCheck.GetKVStore(store), storage.GetKVStore(store)))
}
// check total nonce increased the same amount as the number of transactions
var total uint64
store := storage.GetKVStore(StoreKeyAuth)
it := store.Iterator(nil, nil)
defer it.Close()
for ; it.Valid(); it.Next() {
if !bytes.HasPrefix(it.Key(), []byte("nonce")) {
continue
}
total += binary.BigEndian.Uint64(it.Value())
continue
}
require.Equal(t, uint64(tc.blk.Size()), total)
})
}
}
func StoreEqual(a, b storetypes.KVStore) bool {
// compare with iterators
iter1 := a.Iterator(nil, nil)
iter2 := b.Iterator(nil, nil)
defer iter1.Close()
defer iter2.Close()
for {
if !iter1.Valid() && !iter2.Valid() {
return true
}
if !iter1.Valid() || !iter2.Valid() {
return false
}
if !bytes.Equal(iter1.Key(), iter2.Key()) || !bytes.Equal(iter1.Value(), iter2.Value()) {
return false
}
iter1.Next()
iter2.Next()
}
}