forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcounters_to_array_test.go
154 lines (131 loc) · 4.04 KB
/
counters_to_array_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
package migrations
import (
"context"
"encoding/json"
"testing"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/zk/hermez_db"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/require"
)
type testCases struct {
testName string
mapCounters map[uint64]map[string]int
arrayCounters map[uint64][vm.CounterTypesCount]int
expectedCounters map[uint64][vm.CounterTypesCount]int
}
func TestCountersToArray(t *testing.T) {
testCases := []testCases{
{
testName: "only map entries",
mapCounters: map[uint64]map[string]int{
1: {string(vm.CounterKeyNames[vm.A]): 1, string(vm.CounterKeyNames[vm.B]): 2},
2: {string(vm.CounterKeyNames[vm.A]): 3, string(vm.CounterKeyNames[vm.B]): 4},
},
arrayCounters: map[uint64][vm.CounterTypesCount]int{},
expectedCounters: map[uint64][vm.CounterTypesCount]int{
1: {0, 1, 2, 0, 0, 0, 0, 0},
2: {0, 3, 4, 0, 0, 0, 0, 0},
},
},
{
testName: "only array entries",
mapCounters: map[uint64]map[string]int{},
arrayCounters: map[uint64][vm.CounterTypesCount]int{
1: {0, 1, 2, 0, 0, 0, 0, 0},
2: {0, 3, 4, 0, 0, 0, 0, 0},
},
expectedCounters: map[uint64][vm.CounterTypesCount]int{
1: {0, 1, 2, 0, 0, 0, 0, 0},
2: {0, 3, 4, 0, 0, 0, 0, 0},
},
},
{
testName: "arrays and maps entries",
mapCounters: map[uint64]map[string]int{
1: {string(vm.CounterKeyNames[vm.A]): 1, string(vm.CounterKeyNames[vm.B]): 2},
2: {string(vm.CounterKeyNames[vm.A]): 3, string(vm.CounterKeyNames[vm.B]): 4},
},
arrayCounters: map[uint64][vm.CounterTypesCount]int{
3: {2, 1, 2, 0, 0, 0, 0, 0},
4: {1, 3, 4, 0, 0, 0, 0, 0},
},
expectedCounters: map[uint64][vm.CounterTypesCount]int{
1: {0, 1, 2, 0, 0, 0, 0, 0},
2: {0, 3, 4, 0, 0, 0, 0, 0},
3: {2, 1, 2, 0, 0, 0, 0, 0},
4: {1, 3, 4, 0, 0, 0, 0, 0},
},
},
}
for _, tc := range testCases {
require, tmpDir, db := require.New(t), t.TempDir(), memdb.NewTestDB(t)
err := prepareDbCounters(db, tc.mapCounters, tc.arrayCounters)
require.NoError(err)
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = []Migration{countersToArray}
err = migrator.Apply(db, tmpDir, log.New())
require.NoError(err)
err = assertDbCounters(t, db, tc.testName, tc.expectedCounters)
require.NoError(err)
}
}
func prepareDbCounters(db kv.RwDB, mapCounters map[uint64]map[string]int, arrayCounters map[uint64][vm.CounterTypesCount]int) error {
tx, err := db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
if err = tx.CreateBucket(kv.BATCH_COUNTERS); err != nil {
return err
}
for l2BlockNo, countersMap := range mapCounters {
countersMapBytes, err := json.Marshal(countersMap)
if err != nil {
return err
}
if err = tx.Put(kv.BATCH_COUNTERS, hermez_db.Uint64ToBytes(l2BlockNo), countersMapBytes); err != nil {
return err
}
}
for l2BlockNo, countersArray := range arrayCounters {
countersArrayBytes, err := json.Marshal(countersArray)
if err != nil {
return err
}
if err = tx.Put(kv.BATCH_COUNTERS, hermez_db.Uint64ToBytes(l2BlockNo), countersArrayBytes); err != nil {
return err
}
}
return tx.Commit()
}
func assertDbCounters(t *testing.T, db kv.RwDB, testName string, expectedCounters map[uint64][vm.CounterTypesCount]int) error {
tx, err := db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
c, err := tx.Cursor(kv.BATCH_COUNTERS)
if err != nil {
return err
}
defer c.Close()
actualCounters := map[uint64][vm.CounterTypesCount]int{}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
var counters []int
if err = json.Unmarshal(v, &counters); err != nil {
return err
}
l2BlockNo := hermez_db.BytesToUint64(k)
blockCounters := [vm.CounterTypesCount]int{}
copy(blockCounters[:], counters)
actualCounters[l2BlockNo] = blockCounters
}
require.Equal(t, expectedCounters, actualCounters, testName)
return tx.Commit()
}