OSDN Git Service

update
[bytom/vapor.git] / database / account_store_test.go
1 package database
2
3 import (
4         "os"
5         "testing"
6
7         "github.com/vapor/common"
8         "github.com/vapor/testutil"
9
10         "github.com/vapor/blockchain/signers"
11         "github.com/vapor/crypto/ed25519/chainkd"
12
13         acc "github.com/vapor/account"
14         dbm "github.com/vapor/database/leveldb"
15         "github.com/vapor/protocol/bc"
16 )
17
18 func TestDeleteAccount(t *testing.T) {
19         testDB := dbm.NewDB("testdb", "leveldb", "temp")
20         defer func() {
21                 testDB.Close()
22                 os.RemoveAll("temp")
23         }()
24
25         cases := []struct {
26                 accounts      []*acc.Account
27                 deleteAccount *acc.Account
28                 want          []*acc.Account
29         }{
30                 {
31                         accounts:      []*acc.Account{},
32                         deleteAccount: &acc.Account{},
33                         want:          []*acc.Account{},
34                 },
35                 {
36                         accounts: []*acc.Account{},
37                         deleteAccount: &acc.Account{
38                                 ID:    "id-1",
39                                 Alias: "alias-1",
40                         },
41                         want: []*acc.Account{},
42                 },
43                 {
44                         accounts: []*acc.Account{
45                                 &acc.Account{
46                                         ID:    "id-1",
47                                         Alias: "alias-1",
48                                 },
49                                 &acc.Account{
50                                         ID:    "id-2",
51                                         Alias: "alias-2",
52                                 },
53                         },
54                         deleteAccount: &acc.Account{},
55                         want: []*acc.Account{
56                                 &acc.Account{
57                                         ID:    "id-1",
58                                         Alias: "alias-1",
59                                 },
60                                 &acc.Account{
61                                         ID:    "id-2",
62                                         Alias: "alias-2",
63                                 },
64                         },
65                 },
66                 {
67                         accounts: []*acc.Account{
68                                 &acc.Account{
69                                         ID:    "id-1",
70                                         Alias: "alias-1",
71                                 },
72                                 &acc.Account{
73                                         ID:    "id-2",
74                                         Alias: "alias-2",
75                                 },
76                         },
77                         deleteAccount: &acc.Account{
78                                 ID:    "id-3",
79                                 Alias: "alias-3",
80                         },
81                         want: []*acc.Account{
82                                 &acc.Account{
83                                         ID:    "id-1",
84                                         Alias: "alias-1",
85                                 },
86                                 &acc.Account{
87                                         ID:    "id-2",
88                                         Alias: "alias-2",
89                                 },
90                         },
91                 },
92                 {
93                         accounts: []*acc.Account{
94                                 &acc.Account{
95                                         ID:    "id-1",
96                                         Alias: "alias-1",
97                                 },
98                                 &acc.Account{
99                                         ID:    "id-2",
100                                         Alias: "alias-2",
101                                 },
102                         },
103                         deleteAccount: &acc.Account{
104                                 ID:    "id-1",
105                                 Alias: "alias-1",
106                         },
107                         want: []*acc.Account{
108                                 &acc.Account{
109                                         ID:    "id-2",
110                                         Alias: "alias-2",
111                                 },
112                         },
113                 },
114         }
115
116         accountStore := NewAccountStore(testDB)
117         for i, c := range cases {
118                 as := accountStore.InitBatch()
119                 // store mock accounts
120                 for _, a := range c.accounts {
121                         if err := as.SetAccount(a); err != nil {
122                                 t.Fatal(err)
123                         }
124                 }
125
126                 // delete account
127                 if err := as.DeleteAccount(c.deleteAccount); err != nil {
128                         t.Fatal(err)
129                 }
130
131                 if err := as.CommitBatch(); err != nil {
132                         t.Fatal(err)
133                 }
134
135                 // get account by deleteAccount.ID, it should print ErrFindAccount
136                 if _, err := as.GetAccountByID(c.deleteAccount.ID); err != acc.ErrFindAccount {
137                         t.Fatal(err)
138                 }
139
140                 for _, a := range c.want {
141                         if _, err := as.GetAccountByID(a.ID); err != nil {
142                                 t.Errorf("case %v: cann't find account, err: %v", i, err)
143                         }
144
145                         if _, err := as.GetAccountByAlias(a.Alias); err != nil {
146                                 t.Errorf("case %v: cann't find account, err: %v", i, err)
147                         }
148                 }
149         }
150 }
151
152 func TestDeleteStandardUTXO(t *testing.T) {
153         testDB := dbm.NewDB("testdb", "leveldb", "temp")
154         defer func() {
155                 testDB.Close()
156                 os.RemoveAll("temp")
157         }()
158
159         cases := []struct {
160                 utxos      []*acc.UTXO
161                 deleteUTXO *acc.UTXO
162                 want       []*acc.UTXO
163         }{
164                 {
165                         utxos:      []*acc.UTXO{},
166                         deleteUTXO: &acc.UTXO{},
167                         want:       []*acc.UTXO{},
168                 },
169                 {
170                         utxos: []*acc.UTXO{
171                                 &acc.UTXO{
172                                         OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
173                                 },
174                         },
175                         deleteUTXO: &acc.UTXO{
176                                 OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
177                         },
178                         want: []*acc.UTXO{},
179                 },
180                 {
181                         utxos: []*acc.UTXO{
182                                 &acc.UTXO{
183                                         OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
184                                 },
185                                 &acc.UTXO{
186                                         OutputID: bc.NewHash([32]byte{0x2e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
187                                 },
188                                 &acc.UTXO{
189                                         OutputID: bc.NewHash([32]byte{0x3f, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
190                                 },
191                                 &acc.UTXO{
192                                         OutputID: bc.NewHash([32]byte{0x5e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
193                                 },
194                                 &acc.UTXO{
195                                         OutputID: bc.NewHash([32]byte{0x6e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
196                                 },
197                                 &acc.UTXO{
198                                         OutputID: bc.NewHash([32]byte{0x7e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
199                                 },
200                         },
201                         deleteUTXO: &acc.UTXO{},
202                         want: []*acc.UTXO{
203                                 &acc.UTXO{
204                                         OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
205                                 },
206                                 &acc.UTXO{
207                                         OutputID: bc.NewHash([32]byte{0x2e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
208                                 },
209                                 &acc.UTXO{
210                                         OutputID: bc.NewHash([32]byte{0x3f, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
211                                 },
212                                 &acc.UTXO{
213                                         OutputID: bc.NewHash([32]byte{0x5e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
214                                 },
215                                 &acc.UTXO{
216                                         OutputID: bc.NewHash([32]byte{0x6e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
217                                 },
218                                 &acc.UTXO{
219                                         OutputID: bc.NewHash([32]byte{0x7e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
220                                 },
221                         },
222                 },
223                 {
224                         utxos: []*acc.UTXO{},
225                         deleteUTXO: &acc.UTXO{
226                                 OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
227                         },
228                         want: []*acc.UTXO{},
229                 },
230                 {
231                         utxos: []*acc.UTXO{
232                                 &acc.UTXO{
233                                         OutputID: bc.NewHash([32]byte{0x0e, 0x04, 0x50, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
234                                 },
235                                 &acc.UTXO{
236                                         OutputID: bc.NewHash([32]byte{0x00, 0x01, 0x02, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
237                                 },
238                                 &acc.UTXO{
239                                         OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
240                                 },
241                                 &acc.UTXO{
242                                         OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x02, 0x39, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
243                                 },
244                         },
245                         deleteUTXO: &acc.UTXO{
246                                 OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x02, 0x39, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
247                         },
248                         want: []*acc.UTXO{
249                                 &acc.UTXO{
250                                         OutputID: bc.NewHash([32]byte{0x0e, 0x04, 0x50, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
251                                 },
252                                 &acc.UTXO{
253                                         OutputID: bc.NewHash([32]byte{0x00, 0x01, 0x02, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
254                                 },
255                                 &acc.UTXO{
256                                         OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
257                                 },
258                         },
259                 },
260         }
261
262         accountStore := NewAccountStore(testDB)
263         for _, c := range cases {
264                 as := accountStore.InitBatch()
265                 // store mock utxos
266                 for _, utxo := range c.utxos {
267                         if err := as.SetStandardUTXO(utxo.OutputID, utxo); err != nil {
268                                 t.Fatal(err)
269                         }
270                 }
271
272                 // delete utxo
273                 as.DeleteStandardUTXO(c.deleteUTXO.OutputID)
274                 if err := as.CommitBatch(); err != nil {
275                         t.Fatal(err)
276                 }
277
278                 // get utxo by outputID
279                 for _, utxo := range c.want {
280                         if _, err := as.GetUTXO(utxo.OutputID); err != nil {
281                                 t.Fatal(err)
282                         }
283                 }
284         }
285 }
286
287 func TestGetAccountIndex(t *testing.T) {
288         testDB := dbm.NewDB("testdb", "leveldb", "temp")
289         defer func() {
290                 testDB.Close()
291                 os.RemoveAll("temp")
292         }()
293
294         cases := []struct {
295                 account      *acc.Account
296                 currentIndex uint64
297                 want         uint64
298         }{
299                 {
300                         account: &acc.Account{
301                                 Signer: &signers.Signer{
302                                         XPubs: []chainkd.XPub{
303                                                 [64]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
304                                                 [64]byte{0x09, 0x09, 0x09, 0x01, 0x01, 0x00, 0x04, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
305                                         },
306                                         KeyIndex: uint64(0),
307                                 },
308                         },
309                         currentIndex: uint64(0),
310                         want:         uint64(0),
311                 },
312                 {
313                         account: &acc.Account{
314                                 Signer: &signers.Signer{
315                                         XPubs: []chainkd.XPub{
316                                                 [64]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
317                                                 [64]byte{0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
318                                         },
319                                         KeyIndex: uint64(1),
320                                 },
321                         },
322                         currentIndex: uint64(1),
323                         want:         uint64(1),
324                 },
325                 {
326                         account: &acc.Account{
327                                 Signer: &signers.Signer{
328                                         XPubs: []chainkd.XPub{
329                                                 [64]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
330                                                 [64]byte{0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
331                                         },
332                                         KeyIndex: uint64(9),
333                                 },
334                         },
335                         currentIndex: uint64(1),
336                         want:         uint64(9),
337                 },
338                 {
339                         account: &acc.Account{
340                                 Signer: &signers.Signer{
341                                         XPubs: []chainkd.XPub{
342                                                 [64]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
343                                                 [64]byte{0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
344                                         },
345                                         KeyIndex: uint64(10),
346                                 },
347                         },
348                         currentIndex: uint64(88),
349                         want:         uint64(88),
350                 },
351                 {
352                         account: &acc.Account{
353                                 Signer: &signers.Signer{
354                                         XPubs:    []chainkd.XPub{},
355                                         KeyIndex: uint64(0),
356                                 },
357                         },
358                         currentIndex: uint64(0),
359                         want:         uint64(0),
360                 },
361                 {
362                         account: &acc.Account{
363                                 Signer: &signers.Signer{
364                                         XPubs: []chainkd.XPub{
365                                                 [64]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
366                                         },
367                                         KeyIndex: uint64(1),
368                                 },
369                         },
370                         currentIndex: uint64(77),
371                         want:         uint64(77),
372                 },
373         }
374
375         accountStore := NewAccountStore(testDB)
376         for i, c := range cases {
377                 as := accountStore.InitBatch()
378                 v := as.(*AccountStore)
379                 v.db.Set(accountIndexKey(c.account.XPubs), common.Unit64ToBytes(c.currentIndex))
380                 as.SetAccountIndex(c.account)
381                 if err := as.CommitBatch(); err != nil {
382                         t.Fatal(err)
383                 }
384                 gotIndex := as.GetAccountIndex(c.account.XPubs)
385                 if !testutil.DeepEqual(gotIndex, c.want) {
386                         t.Errorf("case %v: got incorrect account index, got: %v, want: %v.", i, gotIndex, c.want)
387                 }
388         }
389 }
390
391 func TestGetBip44ContractIndex(t *testing.T) {
392         testDB := dbm.NewDB("testdb", "leveldb", "temp")
393         defer func() {
394                 testDB.Close()
395                 os.RemoveAll("temp")
396         }()
397
398         cases := []struct {
399                 accountID string
400                 change    bool
401                 index     uint64
402         }{
403                 {
404                         accountID: "",
405                         change:    false,
406                         index:     uint64(0),
407                 },
408                 {
409                         accountID: "account1",
410                         change:    true,
411                         index:     uint64(0),
412                 },
413                 {
414                         accountID: "account1",
415                         change:    false,
416                         index:     uint64(0),
417                 },
418                 {
419                         accountID: "account1",
420                         change:    true,
421                         index:     uint64(100),
422                 },
423         }
424
425         accountStore := NewAccountStore(testDB)
426         for i, c := range cases {
427                 as := accountStore.InitBatch()
428                 as.SetBip44ContractIndex(c.accountID, c.change, c.index)
429                 if err := as.CommitBatch(); err != nil {
430                         t.Fatal(err)
431                 }
432
433                 gotIndex := as.GetBip44ContractIndex(c.accountID, c.change)
434                 if !testutil.DeepEqual(gotIndex, c.index) {
435                         t.Errorf("case %v: got incorrect bip44 contract index, got: %v, want: %v.", i, gotIndex, c.index)
436                 }
437         }
438 }
439
440 func TestGetCoinbaseArbitrary(t *testing.T) {
441         testDB := dbm.NewDB("testdb", "leveldb", "temp")
442         defer func() {
443                 testDB.Close()
444                 os.RemoveAll("temp")
445         }()
446
447         cases := []struct {
448                 arbitrary []byte
449         }{
450                 {
451                         arbitrary: []byte{},
452                 },
453                 {
454                         arbitrary: []byte("test arbitrary"),
455                 },
456                 {
457                         arbitrary: []byte("test arbitrary test arbitrary test arbitrary test arbitrary test arbitrary"),
458                 },
459         }
460
461         accountStore := NewAccountStore(testDB)
462         for i, c := range cases {
463                 as := accountStore.InitBatch()
464                 as.SetCoinbaseArbitrary(c.arbitrary)
465                 if err := as.CommitBatch(); err != nil {
466                         t.Fatal(err)
467                 }
468
469                 gotArbitrary := as.GetCoinbaseArbitrary()
470                 if !testutil.DeepEqual(gotArbitrary, c.arbitrary) {
471                         t.Errorf("case %v: got incorrect arbitrary, got: %v, want: %v.", i, gotArbitrary, c.arbitrary)
472                 }
473         }
474 }
475
476 func TestGetContractIndex(t *testing.T) {
477         testDB := dbm.NewDB("testdb", "leveldb", "temp")
478         defer func() {
479                 testDB.Close()
480                 os.RemoveAll("temp")
481         }()
482
483         cases := []struct {
484                 accountID string
485                 index     uint64
486         }{
487                 {
488                         accountID: "",
489                         index:     uint64(0),
490                 },
491                 {
492                         accountID: "account1",
493                         index:     uint64(0),
494                 },
495                 {
496                         accountID: "",
497                         index:     uint64(1000),
498                 },
499                 {
500                         accountID: "account1",
501                         index:     uint64(8888),
502                 },
503         }
504
505         accountStore := NewAccountStore(testDB)
506         for i, c := range cases {
507                 as := accountStore.InitBatch()
508                 as.SetContractIndex(c.accountID, c.index)
509                 if err := as.CommitBatch(); err != nil {
510                         t.Fatal(err)
511                 }
512
513                 gotIndex := as.GetContractIndex(c.accountID)
514                 if !testutil.DeepEqual(gotIndex, c.index) {
515                         t.Errorf("case %v: got contract index, got: %v, want: %v.", i, gotIndex, c.index)
516                 }
517         }
518 }