forked from crypto-org-chain/go-block-stm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmvmemory_test.go
139 lines (112 loc) · 3.54 KB
/
mvmemory_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
package block_stm
import (
"testing"
storetypes "cosmossdk.io/store/types"
"github.com/test-go/testify/require"
)
func TestMVMemoryRecord(t *testing.T) {
stores := map[storetypes.StoreKey]int{StoreKeyAuth: 0}
storage := NewMultiMemDB(stores)
mv := NewMVMemory(16, stores)
scheduler := NewScheduler(16)
var views []*MultiMVMemoryView
for i := TxnIndex(0); i < 3; i++ {
version := TxnVersion{i, 0}
view := mv.View(version.Index, storage, scheduler)
store := view.GetKVStore(StoreKeyAuth)
_ = store.Get([]byte("a"))
_ = store.Get([]byte("d"))
store.Set([]byte("a"), []byte("1"))
store.Set([]byte("b"), []byte("1"))
store.Set([]byte("c"), []byte("1"))
views = append(views, view)
}
for i, view := range views {
wroteNewLocation := mv.Record(TxnVersion{TxnIndex(i), 0}, view)
require.True(t, wroteNewLocation)
}
require.True(t, mv.ValidateReadSet(0))
require.False(t, mv.ValidateReadSet(1))
require.False(t, mv.ValidateReadSet(2))
// abort 2 and 3
mv.ConvertWritesToEstimates(1)
mv.ConvertWritesToEstimates(2)
resultCh := make(chan struct{}, 1)
go func() {
view := mv.View(3, storage, scheduler)
store := view.GetKVStore(StoreKeyAuth)
// will wait for tx 2
store.Get([]byte("a"))
wroteNewLocation := mv.Record(TxnVersion{3, 1}, view)
require.False(t, wroteNewLocation)
require.True(t, mv.ValidateReadSet(3))
resultCh <- struct{}{}
}()
{
data := mv.GetMVStore(0).(*MVData)
value, version, estimate := data.Read(Key("a"), 1)
require.False(t, estimate)
require.Equal(t, []byte("1"), value)
require.Equal(t, TxnVersion{0, 0}, version)
_, version, estimate = data.Read(Key("a"), 2)
require.True(t, estimate)
require.Equal(t, TxnIndex(1), version.Index)
_, version, estimate = data.Read(Key("a"), 3)
require.True(t, estimate)
require.Equal(t, TxnIndex(2), version.Index)
}
// rerun tx 1
{
view := mv.View(1, storage, scheduler)
store := view.GetKVStore(StoreKeyAuth)
_ = store.Get([]byte("a"))
_ = store.Get([]byte("d"))
store.Set([]byte("a"), []byte("2"))
store.Set([]byte("b"), []byte("2"))
store.Set([]byte("c"), []byte("2"))
wroteNewLocation := mv.Record(TxnVersion{1, 1}, view)
require.False(t, wroteNewLocation)
require.True(t, mv.ValidateReadSet(1))
}
// rerun tx 2
// don't write `c` this time
{
version := TxnVersion{2, 1}
view := mv.View(version.Index, storage, scheduler)
store := view.GetKVStore(StoreKeyAuth)
_ = store.Get([]byte("a"))
_ = store.Get([]byte("d"))
store.Set([]byte("a"), []byte("3"))
store.Set([]byte("b"), []byte("3"))
wroteNewLocation := mv.Record(version, view)
require.False(t, wroteNewLocation)
require.True(t, mv.ValidateReadSet(2))
scheduler.FinishExecution(version, wroteNewLocation)
// wait for dependency to finish
<-resultCh
}
// run tx 3
{
view := mv.View(3, storage, scheduler)
store := view.GetKVStore(StoreKeyAuth)
_ = store.Get([]byte("a"))
wroteNewLocation := mv.Record(TxnVersion{3, 1}, view)
require.False(t, wroteNewLocation)
require.True(t, mv.ValidateReadSet(3))
}
{
data := mv.GetMVStore(0).(*MVData)
value, version, estimate := data.Read(Key("a"), 2)
require.False(t, estimate)
require.Equal(t, []byte("2"), value)
require.Equal(t, TxnVersion{1, 1}, version)
value, version, estimate = data.Read(Key("a"), 3)
require.False(t, estimate)
require.Equal(t, []byte("3"), value)
require.Equal(t, TxnVersion{2, 1}, version)
value, version, estimate = data.Read(Key("c"), 3)
require.False(t, estimate)
require.Equal(t, []byte("2"), value)
require.Equal(t, TxnVersion{1, 1}, version)
}
}