github.com/turingchain2020/turingchain@v1.1.21/system/store/mavl/mavl_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mavl 6 7 import ( 8 "io/ioutil" 9 "os" 10 "testing" 11 12 "fmt" 13 "time" 14 15 "encoding/json" 16 17 "github.com/turingchain2020/turingchain/account" 18 "github.com/turingchain2020/turingchain/common" 19 "github.com/turingchain2020/turingchain/queue" 20 drivers "github.com/turingchain2020/turingchain/system/store" 21 "github.com/turingchain2020/turingchain/types" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 const MaxKeylenth int = 64 26 27 func newStoreCfg(dir string) *types.Store { 28 return &types.Store{Name: "mavl_test", Driver: "leveldb", DbPath: dir, DbCache: 100} 29 } 30 31 func TestKvdbNewClose(t *testing.T) { 32 dir, err := ioutil.TempDir("", "example") 33 assert.Nil(t, err) 34 defer os.RemoveAll(dir) // clean up 35 os.RemoveAll(dir) //删除已存在目录 36 var storeCfg = newStoreCfg(dir) 37 store := New(storeCfg, nil, nil).(*Store) 38 assert.NotNil(t, store) 39 40 store.Close() 41 } 42 43 func TestKvddbSetGet(t *testing.T) { 44 dir, err := ioutil.TempDir("", "example") 45 assert.Nil(t, err) 46 defer os.RemoveAll(dir) // clean up 47 os.RemoveAll(dir) //删除已存在目录 48 var storeCfg = newStoreCfg(dir) 49 store := New(storeCfg, nil, nil).(*Store) 50 assert.NotNil(t, store) 51 52 keys0 := [][]byte{[]byte("mk1"), []byte("mk2")} 53 get0 := &types.StoreGet{StateHash: drivers.EmptyRoot[:], Keys: keys0} 54 values0 := store.Get(get0) 55 mlog.Info("info", "info", values0) 56 // Get exist key, result nil 57 assert.Len(t, values0, 2) 58 assert.Equal(t, []byte(nil), values0[0]) 59 assert.Equal(t, []byte(nil), values0[1]) 60 61 var kv []*types.KeyValue 62 kv = append(kv, &types.KeyValue{Key: []byte("k1"), Value: []byte("v1")}) 63 kv = append(kv, &types.KeyValue{Key: []byte("k2"), Value: []byte("v2")}) 64 datas := &types.StoreSet{ 65 StateHash: drivers.EmptyRoot[:], 66 KV: kv, 67 } 68 hash, err := store.Set(datas, true) 69 assert.Nil(t, err) 70 keys := [][]byte{[]byte("k1"), []byte("k2")} 71 get1 := &types.StoreGet{StateHash: hash, Keys: keys} 72 73 values := store.Get(get1) 74 assert.Len(t, values, 2) 75 assert.Equal(t, []byte("v1"), values[0]) 76 assert.Equal(t, []byte("v2"), values[1]) 77 78 keys = [][]byte{[]byte("k1")} 79 get2 := &types.StoreGet{StateHash: hash, Keys: keys} 80 values2 := store.Get(get2) 81 assert.Len(t, values2, 1) 82 assert.Equal(t, []byte("v1"), values2[0]) 83 84 get3 := &types.StoreGet{StateHash: drivers.EmptyRoot[:], Keys: keys} 85 values3 := store.Get(get3) 86 assert.Len(t, values3, 1) 87 assert.Equal(t, []byte(nil), values3[0]) 88 } 89 90 func TestKvdbMemSet(t *testing.T) { 91 dir, err := ioutil.TempDir("", "example") 92 assert.Nil(t, err) 93 defer os.RemoveAll(dir) // clean up 94 os.RemoveAll(dir) //删除已存在目录 95 var storeCfg = newStoreCfg(dir) 96 store := New(storeCfg, nil, nil).(*Store) 97 assert.NotNil(t, store) 98 99 var kv []*types.KeyValue 100 kv = append(kv, &types.KeyValue{Key: []byte("mk1"), Value: []byte("v1")}) 101 kv = append(kv, &types.KeyValue{Key: []byte("mk2"), Value: []byte("v2")}) 102 datas := &types.StoreSet{ 103 StateHash: drivers.EmptyRoot[:], 104 KV: kv, 105 } 106 hash, err := store.MemSet(datas, true) 107 assert.Nil(t, err) 108 keys := [][]byte{[]byte("mk1"), []byte("mk2")} 109 get1 := &types.StoreGet{StateHash: hash, Keys: keys} 110 111 values := store.Get(get1) 112 assert.Len(t, values, 2) 113 114 actHash, _ := store.Commit(&types.ReqHash{Hash: hash}) 115 assert.Equal(t, hash, actHash) 116 117 notExistHash, _ := store.Commit(&types.ReqHash{Hash: drivers.EmptyRoot[:]}) 118 assert.Nil(t, notExistHash) 119 } 120 121 func TestKvdbMemSetUpgrade(t *testing.T) { 122 dir, err := ioutil.TempDir("", "example") 123 assert.Nil(t, err) 124 defer os.RemoveAll(dir) // clean up 125 os.RemoveAll(dir) //删除已存在目录 126 var storeCfg = newStoreCfg(dir) 127 store := New(storeCfg, nil, nil).(*Store) 128 assert.NotNil(t, store) 129 130 var kv []*types.KeyValue 131 kv = append(kv, &types.KeyValue{Key: []byte("mk1"), Value: []byte("v1")}) 132 kv = append(kv, &types.KeyValue{Key: []byte("mk2"), Value: []byte("v2")}) 133 datas := &types.StoreSet{ 134 StateHash: drivers.EmptyRoot[:], 135 KV: kv, 136 } 137 hash, err := store.MemSetUpgrade(datas, true) 138 assert.Nil(t, err) 139 keys := [][]byte{[]byte("mk1"), []byte("mk2")} 140 get1 := &types.StoreGet{StateHash: hash, Keys: keys} 141 142 values := store.Get(get1) 143 assert.Len(t, values, 2) 144 145 hash1, err := store.CommitUpgrade(&types.ReqHash{Hash: hash}) 146 assert.Nil(t, err) 147 assert.Equal(t, hash, hash1) 148 } 149 150 func TestKvdbRollback(t *testing.T) { 151 dir, err := ioutil.TempDir("", "example") 152 assert.Nil(t, err) 153 defer os.RemoveAll(dir) // clean up 154 os.RemoveAll(dir) //删除已存在目录 155 var storeCfg = newStoreCfg(dir) 156 store := New(storeCfg, nil, nil).(*Store) 157 assert.NotNil(t, store) 158 159 var kv []*types.KeyValue 160 kv = append(kv, &types.KeyValue{Key: []byte("mk1"), Value: []byte("v1")}) 161 kv = append(kv, &types.KeyValue{Key: []byte("mk2"), Value: []byte("v2")}) 162 datas := &types.StoreSet{ 163 StateHash: drivers.EmptyRoot[:], 164 KV: kv, 165 } 166 hash, err := store.MemSet(datas, true) 167 assert.Nil(t, err) 168 keys := [][]byte{[]byte("mk1"), []byte("mk2")} 169 get1 := &types.StoreGet{StateHash: hash, Keys: keys} 170 values := store.Get(get1) 171 assert.Len(t, values, 2) 172 173 actHash, _ := store.Rollback(&types.ReqHash{Hash: hash}) 174 assert.Equal(t, hash, actHash) 175 176 notExistHash, _ := store.Rollback(&types.ReqHash{Hash: drivers.EmptyRoot[:]}) 177 assert.Nil(t, notExistHash) 178 } 179 180 func TestProcEvent(t *testing.T) { 181 dir, err := ioutil.TempDir("", "example") 182 assert.Nil(t, err) 183 defer os.RemoveAll(dir) // clean up 184 os.RemoveAll(dir) //删除已存在目录 185 var storeCfg = newStoreCfg(dir) 186 store := New(storeCfg, nil, nil).(*Store) 187 assert.NotNil(t, store) 188 189 store.ProcEvent(nil) 190 store.ProcEvent(&queue.Message{}) 191 } 192 193 func TestDel(t *testing.T) { 194 dir, err := ioutil.TempDir("", "example") 195 assert.Nil(t, err) 196 defer os.RemoveAll(dir) // clean up 197 os.RemoveAll(dir) //删除已存在目录 198 var storeCfg = newStoreCfg(dir) 199 store := New(storeCfg, nil, nil).(*Store) 200 assert.NotNil(t, store) 201 202 store.Del(nil) 203 } 204 205 var checkKVResult []*types.KeyValue 206 207 func checkKV(k, v []byte) bool { 208 checkKVResult = append(checkKVResult, 209 &types.KeyValue{Key: k, Value: v}) 210 mlog.Debug("checkKV", "key", string(k), "value", string(v)) 211 return false 212 } 213 func TestKvdbIterate(t *testing.T) { 214 dir, err := ioutil.TempDir("", "example") 215 assert.Nil(t, err) 216 defer os.RemoveAll(dir) // clean up 217 os.RemoveAll(dir) //删除已存在目录 218 var storeCfg = newStoreCfg(dir) 219 store := New(storeCfg, nil, nil).(*Store) 220 assert.NotNil(t, store) 221 222 var kv []*types.KeyValue 223 kv = append(kv, &types.KeyValue{Key: []byte("mk1"), Value: []byte("v1")}) 224 kv = append(kv, &types.KeyValue{Key: []byte("mk2"), Value: []byte("v2")}) 225 datas := &types.StoreSet{ 226 StateHash: drivers.EmptyRoot[:], 227 KV: kv, 228 } 229 hash, err := store.Set(datas, true) 230 assert.Nil(t, err) 231 store.IterateRangeByStateHash(hash, []byte("mk1"), []byte("mk3"), true, checkKV) 232 assert.Len(t, checkKVResult, 2) 233 assert.Equal(t, []byte("v1"), checkKVResult[0].Value) 234 assert.Equal(t, []byte("v2"), checkKVResult[1].Value) 235 } 236 237 type StatTool struct { 238 Amount int64 239 AmountActive int64 240 AmountFrozen int64 241 } 242 243 func (t *StatTool) AddItem(value [][]byte) { 244 for i := 0; i < len(value); i++ { 245 var acc types.Account 246 err := types.Decode(value[i], &acc) 247 if err != nil { 248 return 249 } 250 t.Amount += acc.Balance 251 t.Amount += acc.Frozen 252 253 t.AmountActive += acc.Balance 254 t.AmountFrozen += acc.Frozen 255 } 256 } 257 258 func (t *StatTool) Reset() { 259 t.Amount = 0 260 t.AmountActive = 0 261 t.AmountFrozen = 0 262 } 263 264 func genPrefixEdge(prefix []byte) (r []byte) { 265 for j := 0; j < len(prefix); j++ { 266 r = append(r, prefix[j]) 267 } 268 269 i := len(prefix) - 1 270 for i >= 0 { 271 if r[i] < 0xff { 272 r[i]++ 273 break 274 } else { 275 i-- 276 } 277 } 278 279 return r 280 } 281 func TestIterateCallBack_Mode1(t *testing.T) { 282 dir, err := ioutil.TempDir("", "example") 283 assert.Nil(t, err) 284 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 285 defer os.RemoveAll(dir) // clean up 286 os.RemoveAll(dir) //删除已存在目录 287 var storeCfg = newStoreCfg(dir) 288 store := New(storeCfg, nil, nil).(*Store) 289 assert.NotNil(t, store) 290 //mavldb.EnableMavlPrefix(true) 291 //defer mavldb.EnableMavlPrefix(false) 292 293 //var accountdb *account.DB 294 accountdb := account.NewCoinsAccount(cfg) 295 key := "mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 296 prefix := "mavl-coins-trc-exec-" 297 execAddr1 := "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 298 addr := "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 299 var acc = &types.Account{ 300 Currency: 0, 301 Balance: 1, 302 Frozen: 1, 303 Addr: addr, 304 } 305 306 datas := &types.StoreSet{ 307 StateHash: drivers.EmptyRoot[:], 308 KV: accountdb.GetExecKVSet(execAddr1, acc), 309 Height: 0} 310 hash0, err := store.Set(datas, true) 311 assert.Nil(t, err) 312 313 execAddr2 := "26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 314 datas = &types.StoreSet{ 315 StateHash: hash0, 316 KV: accountdb.GetExecKVSet(execAddr2, acc), 317 Height: 1} 318 hash1, err := store.Set(datas, true) 319 assert.Nil(t, err) 320 321 execAddr3 := "36htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 322 datas = &types.StoreSet{ 323 StateHash: hash1, 324 KV: accountdb.GetExecKVSet(execAddr3, acc), 325 Height: 2} 326 hash2, err := store.Set(datas, true) 327 328 assert.Nil(t, err) 329 330 fmt.Println("func TestIterateCallBack------test case1-------") 331 req := &types.StoreList{StateHash: hash2, Start: []byte(prefix), Suffix: []byte(addr), End: genPrefixEdge([]byte(prefix)), Count: 5, Mode: 1} 332 query := drivers.NewStoreListQuery(store, req) 333 resp2 := query.Run() 334 tool := &StatTool{} 335 tool.AddItem(resp2.Values) 336 assert.Equal(t, int64(3), resp2.Num) 337 assert.Equal(t, (0), len(resp2.NextKey)) 338 assert.Equal(t, (3), len(resp2.Keys)) 339 assert.Equal(t, (3), len(resp2.Values)) 340 assert.Equal(t, int64(6), tool.Amount) 341 assert.Equal(t, int64(3), tool.AmountActive) 342 assert.Equal(t, int64(3), tool.AmountFrozen) 343 tool.Reset() 344 345 fmt.Println("func TestIterateCallBack------test case2-------") 346 resp1 := &types.StoreListReply{} 347 resp1.Suffix = []byte(addr) 348 resp1.Start = []byte(prefix) 349 resp1.End = genPrefixEdge([]byte(prefix)) 350 resp1.Count = 5 351 resp1.Mode = 1 352 353 query = &drivers.StorelistQuery{StoreListReply: resp1} 354 store.IterateRangeByStateHash(hash1, resp1.Start, resp1.End, true, query.IterateCallBack) 355 356 tool.AddItem(resp1.Values) 357 assert.Equal(t, int64(2), resp1.Num) 358 assert.Equal(t, (0), len(resp1.NextKey)) 359 assert.Equal(t, (2), len(resp1.Keys)) 360 assert.Equal(t, (2), len(resp1.Values)) 361 assert.Equal(t, int64(4), tool.Amount) 362 assert.Equal(t, int64(2), tool.AmountActive) 363 assert.Equal(t, int64(2), tool.AmountFrozen) 364 tool.Reset() 365 366 fmt.Println("func TestIterateCallBack------test case3-------") 367 resp0 := &types.StoreListReply{} 368 resp0.Suffix = []byte(addr) 369 resp0.Start = []byte(prefix) 370 resp0.End = genPrefixEdge([]byte(prefix)) 371 resp0.Count = 5 372 resp0.Mode = 1 373 query = &drivers.StorelistQuery{StoreListReply: resp0} 374 store.IterateRangeByStateHash(hash0, resp0.Start, resp0.End, true, query.IterateCallBack) 375 376 tool.AddItem(resp0.Values) 377 assert.Equal(t, int64(1), resp0.Num) 378 assert.Equal(t, 0, len(resp0.NextKey)) 379 assert.Equal(t, 1, len(resp0.Keys)) 380 assert.Equal(t, 1, len(resp0.Values)) 381 assert.Equal(t, int64(2), tool.Amount) 382 assert.Equal(t, int64(1), tool.AmountActive) 383 assert.Equal(t, int64(1), tool.AmountFrozen) 384 tool.Reset() 385 386 fmt.Println("func TestIterateCallBack------test case4-------") 387 resp := &types.StoreListReply{} 388 resp.Suffix = []byte(addr) 389 resp.Start = []byte(prefix) 390 resp.End = genPrefixEdge([]byte(prefix)) 391 resp.Count = 1 392 resp.Mode = 1 393 query = &drivers.StorelistQuery{StoreListReply: resp} 394 store.IterateRangeByStateHash(hash2, resp.Start, resp.End, true, query.IterateCallBack) 395 396 tool.AddItem(resp.Values) 397 assert.Equal(t, int64(1), resp.Num) 398 assert.Equal(t, len([]byte(key)), len(resp.NextKey)) 399 assert.Equal(t, (1), len(resp.Keys)) 400 assert.Equal(t, (1), len(resp.Values)) 401 assert.Equal(t, int64(2), tool.Amount) 402 assert.Equal(t, int64(1), tool.AmountActive) 403 assert.Equal(t, int64(1), tool.AmountFrozen) 404 tool.Reset() 405 406 fmt.Println("func TestIterateCallBack------test case5-------") 407 resp = &types.StoreListReply{} 408 resp.Suffix = []byte(addr) 409 resp.Start = []byte(prefix) 410 resp.End = genPrefixEdge([]byte(prefix)) 411 resp.Count = 2 412 resp.Mode = 1 413 query = &drivers.StorelistQuery{StoreListReply: resp} 414 store.IterateRangeByStateHash(hash2, resp.Start, resp.End, true, query.IterateCallBack) 415 416 tool.AddItem(resp.Values) 417 assert.Equal(t, int64(2), resp.Num) 418 assert.Equal(t, len([]byte(key)), len(resp.NextKey)) 419 assert.Equal(t, (2), len(resp.Keys)) 420 assert.Equal(t, (2), len(resp.Values)) 421 assert.Equal(t, int64(4), tool.Amount) 422 assert.Equal(t, int64(2), tool.AmountActive) 423 assert.Equal(t, int64(2), tool.AmountFrozen) 424 tool.Reset() 425 } 426 func TestIterateCallBack_Mode2(t *testing.T) { 427 dir, err := ioutil.TempDir("", "example") 428 assert.Nil(t, err) 429 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 430 defer os.RemoveAll(dir) // clean up 431 os.RemoveAll(dir) //删除已存在目录 432 var storeCfg = newStoreCfg(dir) 433 store := New(storeCfg, nil, nil).(*Store) 434 assert.NotNil(t, store) 435 //mavldb.EnableMavlPrefix(true) 436 //defer mavldb.EnableMavlPrefix(false) 437 438 //var accountdb *account.DB 439 accountdb := account.NewCoinsAccount(cfg) 440 key := "mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 441 prefix := "mavl-coins-trc-exec-" 442 execAddr1 := "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 443 addr := "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 444 var acc = &types.Account{ 445 Currency: 0, 446 Balance: 1, 447 Frozen: 1, 448 Addr: addr, 449 } 450 datas := &types.StoreSet{ 451 StateHash: drivers.EmptyRoot[:], 452 KV: accountdb.GetExecKVSet(execAddr1, acc), 453 Height: 0} 454 hash0, err := store.Set(datas, true) 455 assert.Nil(t, err) 456 457 execAddr2 := "26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 458 datas = &types.StoreSet{ 459 StateHash: hash0, 460 KV: accountdb.GetExecKVSet(execAddr2, acc), 461 Height: 1} 462 hash1, err := store.Set(datas, true) 463 assert.Nil(t, err) 464 465 execAddr3 := "36htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 466 datas = &types.StoreSet{ 467 StateHash: hash1, 468 KV: accountdb.GetExecKVSet(execAddr3, acc), 469 Height: 2} 470 hash2, err := store.Set(datas, true) 471 472 assert.Nil(t, err) 473 474 fmt.Println("func TestIterateCallBack------test case1-------") 475 resp2 := &types.StoreListReply{} 476 resp2.Suffix = []byte(addr) 477 resp2.Start = []byte(prefix) 478 resp2.End = genPrefixEdge([]byte(prefix)) 479 resp2.Count = 5 480 resp2.Mode = 2 481 482 query := &drivers.StorelistQuery{StoreListReply: resp2} 483 store.IterateRangeByStateHash(hash2, resp2.Start, nil, true, query.IterateCallBack) 484 tool := &StatTool{} 485 tool.AddItem(resp2.Values) 486 assert.Equal(t, int64(3), resp2.Num) 487 assert.Equal(t, (0), len(resp2.NextKey)) 488 assert.Equal(t, (3), len(resp2.Keys)) 489 assert.Equal(t, (3), len(resp2.Values)) 490 assert.Equal(t, int64(6), tool.Amount) 491 assert.Equal(t, int64(3), tool.AmountActive) 492 assert.Equal(t, int64(3), tool.AmountFrozen) 493 tool.Reset() 494 495 fmt.Println("func TestIterateCallBack------test case2-------") 496 resp1 := &types.StoreListReply{} 497 resp1.Suffix = []byte(addr) 498 resp1.Start = []byte(prefix) 499 resp1.End = genPrefixEdge([]byte(prefix)) 500 resp1.Count = 5 501 resp1.Mode = 2 502 query = &drivers.StorelistQuery{StoreListReply: resp1} 503 store.IterateRangeByStateHash(hash1, resp1.Start, resp1.End, true, query.IterateCallBack) 504 505 tool.AddItem(resp1.Values) 506 assert.Equal(t, int64(2), resp1.Num) 507 assert.Equal(t, (0), len(resp1.NextKey)) 508 assert.Equal(t, (2), len(resp1.Keys)) 509 assert.Equal(t, (2), len(resp1.Values)) 510 assert.Equal(t, int64(4), tool.Amount) 511 assert.Equal(t, int64(2), tool.AmountActive) 512 assert.Equal(t, int64(2), tool.AmountFrozen) 513 tool.Reset() 514 515 fmt.Println("func TestIterateCallBack------test case3-------") 516 resp0 := &types.StoreListReply{} 517 resp0.Suffix = []byte(addr) 518 resp0.Start = []byte(prefix) 519 resp0.End = genPrefixEdge([]byte(prefix)) 520 resp0.Count = 5 521 resp0.Mode = 2 522 query = &drivers.StorelistQuery{StoreListReply: resp0} 523 store.IterateRangeByStateHash(hash0, resp0.Start, nil, true, query.IterateCallBack) 524 525 tool.AddItem(resp0.Values) 526 assert.Equal(t, int64(1), resp0.Num) 527 assert.Equal(t, (0), len(resp0.NextKey)) 528 assert.Equal(t, (1), len(resp0.Keys)) 529 assert.Equal(t, (1), len(resp0.Values)) 530 assert.Equal(t, int64(2), tool.Amount) 531 assert.Equal(t, int64(1), tool.AmountActive) 532 assert.Equal(t, int64(1), tool.AmountFrozen) 533 tool.Reset() 534 535 fmt.Println("func TestIterateCallBack------test case4-------") 536 resp := &types.StoreListReply{} 537 resp.Suffix = []byte(addr) 538 resp.Start = []byte(prefix) 539 resp.End = genPrefixEdge([]byte(prefix)) 540 resp.Count = 1 541 resp.Mode = 2 542 query = &drivers.StorelistQuery{StoreListReply: resp} 543 store.IterateRangeByStateHash(hash2, resp.Start, nil, true, query.IterateCallBack) 544 545 tool.AddItem(resp.Values) 546 assert.Equal(t, int64(1), resp.Num) 547 assert.Equal(t, len([]byte(key)), len(resp.NextKey)) 548 assert.Equal(t, (1), len(resp.Keys)) 549 assert.Equal(t, (1), len(resp.Values)) 550 assert.Equal(t, int64(2), tool.Amount) 551 assert.Equal(t, int64(1), tool.AmountActive) 552 assert.Equal(t, int64(1), tool.AmountFrozen) 553 tool.Reset() 554 555 fmt.Println("func TestIterateCallBack------test case5-------") 556 resp = &types.StoreListReply{} 557 resp.Suffix = []byte(addr) 558 resp.Start = []byte(prefix) 559 resp.End = genPrefixEdge([]byte(prefix)) 560 resp.Count = 2 561 resp.Mode = 2 562 query = &drivers.StorelistQuery{StoreListReply: resp} 563 store.IterateRangeByStateHash(hash2, resp.Start, nil, true, query.IterateCallBack) 564 565 tool.AddItem(resp.Values) 566 assert.Equal(t, int64(2), resp.Num) 567 assert.Equal(t, len([]byte(key)), len(resp.NextKey)) 568 assert.Equal(t, (2), len(resp.Keys)) 569 assert.Equal(t, (2), len(resp.Values)) 570 assert.Equal(t, int64(4), tool.Amount) 571 assert.Equal(t, int64(2), tool.AmountActive) 572 assert.Equal(t, int64(2), tool.AmountFrozen) 573 tool.Reset() 574 575 fmt.Println("func TestIterateCallBack------test case6-------") 576 resp = &types.StoreListReply{} 577 resp.End = []byte(addr) 578 resp.Start = []byte("mavl-coins-trc-exec-26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:") 579 resp.End = genPrefixEdge([]byte("mavl-coins-trc-exec-26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:")) 580 resp.Count = 1 581 resp.Mode = 2 582 query = &drivers.StorelistQuery{StoreListReply: resp} 583 store.IterateRangeByStateHash(hash2, resp.Start, resp.End, true, query.IterateCallBack) 584 tool.AddItem(resp.Values) 585 assert.Equal(t, int64(1), resp.Num) 586 assert.Equal(t, len([]byte(key)), len(resp.NextKey)) 587 assert.Equal(t, (1), len(resp.Keys)) 588 assert.Equal(t, (1), len(resp.Values)) 589 assert.Equal(t, int64(2), tool.Amount) 590 assert.Equal(t, int64(1), tool.AmountActive) 591 assert.Equal(t, int64(1), tool.AmountFrozen) 592 tool.Reset() 593 } 594 595 func GetRandomString(length int) string { 596 return common.GetRandPrintString(20, length) 597 } 598 599 func TestKvdbIterateTimes(t *testing.T) { 600 checkKVResult = checkKVResult[:0] 601 dir, err := ioutil.TempDir("", "example") 602 assert.Nil(t, err) 603 defer os.RemoveAll(dir) // clean up 604 os.RemoveAll(dir) //删除已存在目录 605 var storeCfg = newStoreCfg(dir) 606 store := New(storeCfg, nil, nil).(*Store) 607 assert.NotNil(t, store) 608 609 var kv []*types.KeyValue 610 var key string 611 var value string 612 613 for i := 0; i < 1000; i++ { 614 key = GetRandomString(MaxKeylenth) 615 value = fmt.Sprintf("v%d", i) 616 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 617 } 618 datas := &types.StoreSet{ 619 StateHash: drivers.EmptyRoot[:], 620 KV: kv, 621 } 622 hash, err := store.Set(datas, true) 623 assert.Nil(t, err) 624 start := time.Now() 625 store.IterateRangeByStateHash(hash, nil, nil, true, checkKV) 626 end := time.Now() 627 fmt.Println("mavl cost time is", end.Sub(start)) 628 assert.Len(t, checkKVResult, 1000) 629 } 630 631 func BenchmarkGet(b *testing.B) { 632 dir, err := ioutil.TempDir("", "example") 633 assert.Nil(b, err) 634 defer os.RemoveAll(dir) // clean up 635 os.RemoveAll(dir) //删除已存在目录 636 var storeCfg = newStoreCfg(dir) 637 subcfg := &subConfig{ 638 EnableMavlPrefix: true, 639 } 640 sub, err := json.Marshal(subcfg) 641 assert.Nil(b, err) 642 store := New(storeCfg, sub, nil).(*Store) 643 assert.NotNil(b, store) 644 var kv []*types.KeyValue 645 var keys [][]byte 646 var hash = drivers.EmptyRoot[:] 647 fmt.Println("N = ", b.N) 648 for i := 0; i < b.N; i++ { 649 key := GetRandomString(MaxKeylenth) 650 value := fmt.Sprintf("%s%d", key, i) 651 keys = append(keys, []byte(key)) 652 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 653 if i%10000 == 0 { 654 datas := &types.StoreSet{StateHash: hash, KV: kv} 655 hash, err = store.Set(datas, true) 656 assert.Nil(b, err) 657 kv = nil 658 } 659 } 660 if kv != nil { 661 datas := &types.StoreSet{StateHash: hash, KV: kv} 662 hash, err = store.Set(datas, true) 663 assert.Nil(b, err) 664 } 665 666 start := time.Now() 667 b.ResetTimer() 668 for _, key := range keys { 669 getData := &types.StoreGet{ 670 StateHash: hash, 671 Keys: [][]byte{key}, 672 } 673 store.Get(getData) 674 } 675 end := time.Now() 676 fmt.Println("mavl BenchmarkGet cost time is", end.Sub(start), "num is", b.N) 677 b.StopTimer() 678 } 679 680 //这个用例测试Store.Get接口,一次调用会返回一组kvs(30对kv);前一个用例每次查询一个kv。 681 func BenchmarkStoreGetKvs4N(b *testing.B) { 682 dir, err := ioutil.TempDir("", "example") 683 assert.Nil(b, err) 684 defer os.RemoveAll(dir) // clean up 685 os.RemoveAll(dir) //删除已存在目录 686 var storeCfg = newStoreCfg(dir) 687 store := New(storeCfg, nil, nil).(*Store) 688 assert.NotNil(b, store) 689 690 var kv []*types.KeyValue 691 var key string 692 var value string 693 var keys [][]byte 694 695 kvnum := 30 696 for i := 0; i < kvnum; i++ { 697 key = GetRandomString(MaxKeylenth) 698 value = fmt.Sprintf("v%d", i) 699 keys = append(keys, []byte(key)) 700 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 701 } 702 datas := &types.StoreSet{ 703 StateHash: drivers.EmptyRoot[:], 704 KV: kv, 705 } 706 hash, err := store.Set(datas, true) 707 assert.Nil(b, err) 708 getData := &types.StoreGet{ 709 StateHash: hash, 710 Keys: keys, 711 } 712 713 start := time.Now() 714 b.ResetTimer() 715 for i := 0; i < b.N; i++ { 716 values := store.Get(getData) 717 assert.Len(b, values, kvnum) 718 } 719 end := time.Now() 720 fmt.Println("mavl BenchmarkStoreGetKvs4N cost time is", end.Sub(start), "num is", b.N) 721 722 b.StopTimer() 723 } 724 725 //这个用例测试Store.Get接口,一次调用会返回一组kvs(30对kv),数据构造模拟真实情况,N条数据、N次查询。 726 func BenchmarkStoreGetKvsForNN(b *testing.B) { 727 dir, err := ioutil.TempDir("", "example") 728 assert.Nil(b, err) 729 defer os.RemoveAll(dir) // clean up 730 os.RemoveAll(dir) //删除已存在目录 731 var storeCfg = newStoreCfg(dir) 732 store := New(storeCfg, nil, nil).(*Store) 733 assert.NotNil(b, store) 734 735 var kv []*types.KeyValue 736 var key string 737 var value string 738 var keys [][]byte 739 740 for i := 0; i < 30; i++ { 741 key = GetRandomString(MaxKeylenth) 742 value = fmt.Sprintf("v%d", i) 743 keys = append(keys, []byte(key)) 744 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 745 } 746 datas := &types.StoreSet{ 747 StateHash: drivers.EmptyRoot[:], 748 KV: kv, 749 } 750 751 var hashes [][]byte 752 for i := 0; i < b.N; i++ { 753 datas.Height = int64(i) 754 value = fmt.Sprintf("vv%d", i) 755 for j := 0; j < 10; j++ { 756 datas.KV[j].Value = []byte(value) 757 } 758 hash, err := store.MemSet(datas, true) 759 assert.Nil(b, err) 760 req := &types.ReqHash{ 761 Hash: hash, 762 } 763 _, err = store.Commit(req) 764 assert.NoError(b, err, "NoError") 765 datas.StateHash = hash 766 hashes = append(hashes, hash) 767 } 768 769 start := time.Now() 770 b.ResetTimer() 771 772 getData := &types.StoreGet{ 773 StateHash: hashes[0], 774 Keys: keys, 775 } 776 777 for i := 0; i < b.N; i++ { 778 getData.StateHash = hashes[i] 779 store.Get(getData) 780 } 781 end := time.Now() 782 fmt.Println("mavl BenchmarkStoreGetKvsForNN cost time is", end.Sub(start), "num is", b.N) 783 b.StopTimer() 784 } 785 786 //这个用例测试Store.Get接口,一次调用会返回一组kvs(30对kv),数据构造模拟真实情况,预置10000条数据,重复调用10000次。 787 func BenchmarkStoreGetKvsFor10000(b *testing.B) { 788 dir, err := ioutil.TempDir("", "example") 789 assert.Nil(b, err) 790 defer os.RemoveAll(dir) // clean up 791 os.RemoveAll(dir) //删除已存在目录 792 var storeCfg = newStoreCfg(dir) 793 store := New(storeCfg, nil, nil).(*Store) 794 assert.NotNil(b, store) 795 796 var kv []*types.KeyValue 797 var key string 798 var value string 799 var keys [][]byte 800 801 for i := 0; i < 30; i++ { 802 key = GetRandomString(MaxKeylenth) 803 value = fmt.Sprintf("v%d", i) 804 keys = append(keys, []byte(key)) 805 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 806 } 807 datas := &types.StoreSet{ 808 StateHash: drivers.EmptyRoot[:], 809 KV: kv, 810 } 811 812 var hashes [][]byte 813 blocks := 10000 814 times := 10000 815 start1 := time.Now() 816 for i := 0; i < blocks; i++ { 817 datas.Height = int64(i) 818 value = fmt.Sprintf("vv%d", i) 819 for j := 0; j < 30; j++ { 820 datas.KV[j].Value = []byte(value) 821 } 822 hash, err := store.MemSet(datas, true) 823 assert.Nil(b, err) 824 req := &types.ReqHash{ 825 Hash: hash, 826 } 827 _, err = store.Commit(req) 828 assert.NoError(b, err, "NoError") 829 datas.StateHash = hash 830 hashes = append(hashes, hash) 831 } 832 end1 := time.Now() 833 834 start := time.Now() 835 b.ResetTimer() 836 837 getData := &types.StoreGet{ 838 StateHash: hashes[0], 839 Keys: keys, 840 } 841 842 for i := 0; i < times; i++ { 843 getData.StateHash = hashes[i] 844 store.Get(getData) 845 } 846 end := time.Now() 847 fmt.Println("mavl BenchmarkStoreGetKvsFor10000 MemSet&Commit cost time is ", end1.Sub(start1), "blocks is", blocks) 848 fmt.Println("mavl BenchmarkStoreGetKvsFor10000 Get cost time is", end.Sub(start), "num is ", times, ",blocks is ", blocks) 849 b.StopTimer() 850 } 851 852 func BenchmarkSet(b *testing.B) { 853 dir, err := ioutil.TempDir("", "example") 854 assert.Nil(b, err) 855 defer os.RemoveAll(dir) // clean up 856 os.RemoveAll(dir) //删除已存在目录 857 var storeCfg = newStoreCfg(dir) 858 subcfg := &subConfig{ 859 EnableMavlPrefix: true, 860 } 861 sub, err := json.Marshal(subcfg) 862 assert.Nil(b, err) 863 store := New(storeCfg, sub, nil).(*Store) 864 assert.NotNil(b, store) 865 var kv []*types.KeyValue 866 var keys [][]byte 867 var hash = drivers.EmptyRoot[:] 868 start := time.Now() 869 for i := 0; i < b.N; i++ { 870 key := GetRandomString(MaxKeylenth) 871 value := fmt.Sprintf("%s%d", key, i) 872 keys = append(keys, []byte(key)) 873 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 874 if i%10000 == 0 { 875 datas := &types.StoreSet{StateHash: hash, KV: kv} 876 hash, err = store.Set(datas, true) 877 assert.Nil(b, err) 878 kv = nil 879 } 880 } 881 if kv != nil { 882 datas := &types.StoreSet{StateHash: hash, KV: kv} 883 _, err = store.Set(datas, true) 884 assert.Nil(b, err) 885 } 886 end := time.Now() 887 fmt.Println("mavl BenchmarkSet cost time is", end.Sub(start), "num is", b.N) 888 } 889 890 //这个用例测试Store.Set接口,一次调用保存一组kvs(30对)到数据库中。 891 func BenchmarkStoreSetKvs(b *testing.B) { 892 dir, err := ioutil.TempDir("", "example") 893 assert.Nil(b, err) 894 defer os.RemoveAll(dir) // clean up 895 os.RemoveAll(dir) //删除已存在目录 896 var storeCfg = newStoreCfg(dir) 897 store := New(storeCfg, nil, nil).(*Store) 898 assert.NotNil(b, store) 899 900 var kv []*types.KeyValue 901 var key string 902 var value string 903 var keys [][]byte 904 905 for i := 0; i < 30; i++ { 906 key = GetRandomString(MaxKeylenth) 907 value = fmt.Sprintf("v%d", i) 908 keys = append(keys, []byte(key)) 909 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 910 } 911 datas := &types.StoreSet{ 912 StateHash: drivers.EmptyRoot[:], 913 KV: kv, 914 } 915 start := time.Now() 916 b.ResetTimer() 917 for i := 0; i < b.N; i++ { 918 hash, err := store.Set(datas, true) 919 assert.Nil(b, err) 920 assert.NotNil(b, hash) 921 } 922 end := time.Now() 923 fmt.Println("mavl BenchmarkSet cost time is", end.Sub(start), "num is", b.N) 924 } 925 926 func BenchmarkMemSet(b *testing.B) { 927 dir, err := ioutil.TempDir("", "example") 928 assert.Nil(b, err) 929 defer os.RemoveAll(dir) // clean up 930 os.RemoveAll(dir) //删除已存在目录 931 var storeCfg = newStoreCfg(dir) 932 store := New(storeCfg, nil, nil).(*Store) 933 assert.NotNil(b, store) 934 var kv []*types.KeyValue 935 var key string 936 var value string 937 var keys [][]byte 938 939 for i := 0; i < b.N; i++ { 940 key = GetRandomString(MaxKeylenth) 941 value = fmt.Sprintf("v%d", i) 942 keys = append(keys, []byte(key)) 943 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 944 } 945 datas := &types.StoreSet{ 946 StateHash: drivers.EmptyRoot[:], 947 KV: kv, 948 } 949 start := time.Now() 950 b.ResetTimer() 951 hash, err := store.MemSet(datas, true) 952 assert.Nil(b, err) 953 assert.NotNil(b, hash) 954 end := time.Now() 955 fmt.Println("mavl BenchmarkMemSet cost time is", end.Sub(start), "num is", b.N) 956 } 957 958 //这个用例测试Store.MemSet接口,一次调用保存一组kvs(30对)到数据库中。 959 func BenchmarkStoreMemSet(b *testing.B) { 960 dir, err := ioutil.TempDir("", "example") 961 assert.Nil(b, err) 962 defer os.RemoveAll(dir) // clean up 963 os.RemoveAll(dir) //删除已存在目录 964 var storeCfg = newStoreCfg(dir) 965 store := New(storeCfg, nil, nil).(*Store) 966 assert.NotNil(b, store) 967 968 var kv []*types.KeyValue 969 var key string 970 var value string 971 var keys [][]byte 972 973 for i := 0; i < 30; i++ { 974 key = GetRandomString(MaxKeylenth) 975 value = fmt.Sprintf("v%d", i) 976 keys = append(keys, []byte(key)) 977 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 978 } 979 datas := &types.StoreSet{ 980 StateHash: drivers.EmptyRoot[:], 981 KV: kv, 982 } 983 start := time.Now() 984 b.ResetTimer() 985 for i := 0; i < b.N; i++ { 986 hash, err := store.MemSet(datas, true) 987 assert.Nil(b, err) 988 assert.NotNil(b, hash) 989 } 990 end := time.Now() 991 fmt.Println("mavl BenchmarkMemSet cost time is", end.Sub(start), "num is", b.N) 992 } 993 994 func BenchmarkCommit(b *testing.B) { 995 dir, err := ioutil.TempDir("", "example") 996 assert.Nil(b, err) 997 defer os.RemoveAll(dir) // clean up 998 os.RemoveAll(dir) //删除已存在目录 999 var storeCfg = newStoreCfg(dir) 1000 store := New(storeCfg, nil, nil).(*Store) 1001 assert.NotNil(b, store) 1002 1003 var kv []*types.KeyValue 1004 var key string 1005 var value string 1006 var keys [][]byte 1007 1008 for i := 0; i < b.N; i++ { 1009 key = GetRandomString(MaxKeylenth) 1010 value = fmt.Sprintf("v%d", i) 1011 keys = append(keys, []byte(key)) 1012 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 1013 } 1014 datas := &types.StoreSet{ 1015 StateHash: drivers.EmptyRoot[:], 1016 KV: kv, 1017 } 1018 hash, err := store.MemSet(datas, true) 1019 assert.Nil(b, err) 1020 req := &types.ReqHash{ 1021 Hash: hash, 1022 } 1023 1024 start := time.Now() 1025 b.ResetTimer() 1026 _, err = store.Commit(req) 1027 assert.NoError(b, err, "NoError") 1028 end := time.Now() 1029 fmt.Println("mavl BenchmarkCommit cost time is", end.Sub(start), "num is", b.N) 1030 b.StopTimer() 1031 } 1032 1033 //模拟真实的数据提交操作,数据之间的关系也保持正确(hash计算),统计的时间包括MemSet和Commit,可以减去之前用例中测试出来的MemSet的时间来估算Commit耗时 1034 func BenchmarkStoreCommit(b *testing.B) { 1035 dir, err := ioutil.TempDir("", "example") 1036 assert.Nil(b, err) 1037 defer os.RemoveAll(dir) // clean up 1038 os.RemoveAll(dir) //删除已存在目录 1039 var storeCfg = newStoreCfg(dir) 1040 store := New(storeCfg, nil, nil).(*Store) 1041 assert.NotNil(b, store) 1042 1043 var kv []*types.KeyValue 1044 var key string 1045 var value string 1046 var keys [][]byte 1047 1048 for i := 0; i < 30; i++ { 1049 key = GetRandomString(MaxKeylenth) 1050 value = fmt.Sprintf("v%d", i) 1051 keys = append(keys, []byte(key)) 1052 kv = append(kv, &types.KeyValue{Key: []byte(key), Value: []byte(value)}) 1053 } 1054 datas := &types.StoreSet{ 1055 StateHash: drivers.EmptyRoot[:], 1056 KV: kv, 1057 } 1058 1059 start := time.Now() 1060 b.ResetTimer() 1061 for i := 0; i < b.N; i++ { 1062 datas.Height = int64(i) 1063 value = fmt.Sprintf("vv%d", i) 1064 for j := 0; j < 10; j++ { 1065 datas.KV[j].Value = []byte(value) 1066 } 1067 hash, err := store.MemSet(datas, true) 1068 assert.Nil(b, err) 1069 req := &types.ReqHash{ 1070 Hash: hash, 1071 } 1072 _, err = store.Commit(req) 1073 assert.NoError(b, err, "NoError") 1074 datas.StateHash = hash 1075 } 1076 end := time.Now() 1077 fmt.Println("mavl BenchmarkCommit cost time is", end.Sub(start), "num is", b.N) 1078 b.StopTimer() 1079 }