github.com/turingchain2020/turingchain@v1.1.21/account/account_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 account 6 7 import ( 8 "fmt" 9 "testing" 10 11 "strings" 12 13 "github.com/turingchain2020/turingchain/client" 14 "github.com/turingchain2020/turingchain/client/mocks" 15 "github.com/turingchain2020/turingchain/common" 16 "github.com/turingchain2020/turingchain/common/address" 17 "github.com/turingchain2020/turingchain/common/db" 18 "github.com/turingchain2020/turingchain/queue" 19 "github.com/turingchain2020/turingchain/types" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/mock" 22 "github.com/stretchr/testify/require" 23 ) 24 25 var ( 26 addr1 = "14ZTV2wHG3uPHnA5cBJmNxAxxvbzS7Z5mE" 27 addr2 = "24ZTV2wHG3uPHnA5cBJmNxAxxvbzS7Z5mE" 28 addr3 = "34ZTV2wHG3uPHnA5cBJmNxAxxvbzS7Z5mE" 29 addr4 = "44ZTV2wHG3uPHnA5cBJmNxAxxvbzS7Z5mE" 30 ) 31 32 func GenerAccDb() (*DB, *DB) { 33 //构造账户数据库 34 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 35 accCoin := NewCoinsAccount(cfg) 36 stroedb, _ := db.NewGoMemDB("gomemdb", "test", 128) 37 accCoin.SetDB(stroedb) 38 39 accToken, _ := NewAccountDB(cfg, "token", "test", nil) 40 stroedb2, _ := db.NewGoMemDB("gomemdb", "test", 128) 41 accToken.SetDB(stroedb2) 42 43 return accCoin, accToken 44 } 45 46 func (acc *DB) GenerAccData() { 47 // 加入账户 48 account := &types.Account{ 49 Balance: 1000 * 1e8, 50 Addr: addr1, 51 } 52 acc.SaveAccount(account) 53 54 account.Balance = 900 * 1e8 55 account.Addr = addr2 56 acc.SaveAccount(account) 57 58 account.Balance = 800 * 1e8 59 account.Addr = addr3 60 acc.SaveAccount(account) 61 62 account.Balance = 700 * 1e8 63 account.Addr = addr4 64 acc.SaveAccount(account) 65 } 66 67 func TestCheckTransfer(t *testing.T) { 68 accCoin, tokenCoin := GenerAccDb() 69 accCoin.GenerAccData() 70 tokenCoin.GenerAccData() 71 72 err := accCoin.CheckTransfer(addr1, addr2, 10*1e8) 73 require.NoError(t, err) 74 75 err = tokenCoin.CheckTransfer(addr3, addr4, 10*1e8) 76 require.NoError(t, err) 77 } 78 79 func TestTransfer(t *testing.T) { 80 accCoin, tokenCoin := GenerAccDb() 81 accCoin.GenerAccData() 82 tokenCoin.GenerAccData() 83 84 _, err := accCoin.Transfer(addr1, addr2, 10*1e8) 85 require.NoError(t, err) 86 t.Logf("Coin from addr balance [%d] to addr balance [%d]", 87 accCoin.LoadAccount(addr1).Balance, 88 accCoin.LoadAccount(addr2).Balance) 89 require.Equal(t, int64(1000*1e8-10*1e8), accCoin.LoadAccount(addr1).Balance) 90 require.Equal(t, int64(900*1e8+10*1e8), accCoin.LoadAccount(addr2).Balance) 91 92 _, err = tokenCoin.Transfer(addr3, addr4, 10*1e8) 93 require.NoError(t, err) 94 95 t.Logf("token from addr balance [%d] to addr balance [%d]", 96 tokenCoin.LoadAccount(addr3).Balance, 97 tokenCoin.LoadAccount(addr4).Balance) 98 require.Equal(t, int64(800*1e8-10*1e8), tokenCoin.LoadAccount(addr3).Balance) 99 require.Equal(t, int64(700*1e8+10*1e8), tokenCoin.LoadAccount(addr4).Balance) 100 } 101 102 func TestDepositBalance(t *testing.T) { 103 accCoin, tokenCoin := GenerAccDb() 104 accCoin.GenerAccData() 105 tokenCoin.GenerAccData() 106 107 _, err := accCoin.depositBalance(addr1, 20*1e8) 108 require.NoError(t, err) 109 t.Logf("Coin deposit balance [%d]", accCoin.LoadAccount(addr1).Balance) 110 require.Equal(t, int64(1000*1e8+20*1e8), accCoin.LoadAccount(addr1).Balance) 111 112 _, err = tokenCoin.depositBalance(addr1, 30*1e8) 113 require.NoError(t, err) 114 t.Logf("token deposit balance [%d]", tokenCoin.LoadAccount(addr1).Balance) 115 require.Equal(t, int64(1000*1e8+30*1e8), tokenCoin.LoadAccount(addr1).Balance) 116 } 117 118 func initEnv() queue.Queue { 119 var q = queue.New("channel") 120 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 121 q.SetConfig(cfg) 122 return q 123 } 124 125 func initQueAPI(q queue.Queue) (client.QueueProtocolAPI, error) { 126 return client.New(q.Client(), nil) 127 } 128 129 func blockchainProcess(q queue.Queue) { 130 go func() { 131 client := q.Client() 132 client.Sub("blockchain") 133 for msg := range client.Recv() { 134 switch msg.Ty { 135 case types.EventGetLastHeader: 136 header := &types.Header{StateHash: []byte("111111111111111111111")} 137 msg.Reply(client.NewMessage("account", types.EventHeader, header)) 138 default: 139 msg.ReplyErr("Do not support", types.ErrNotSupport) 140 } 141 } 142 }() 143 } 144 145 func storeProcess(q queue.Queue) { 146 go func() { 147 client := q.Client() 148 client.Sub("store") 149 for msg := range client.Recv() { 150 switch msg.Ty { 151 case types.EventStoreGet: 152 //datas := msg.GetData().(*types.StoreGet) 153 //fmt.Println("EventStoreGet data = %v", datas) 154 155 values := make([][]byte, 2) 156 account := &types.Account{ 157 Balance: 1000 * 1e8, 158 Addr: addr1, 159 } 160 value := types.Encode(account) 161 values = append(values[:0], value) 162 msg.Reply(client.NewMessage("", types.EventStoreGetReply, &types.StoreReplyValue{Values: values})) 163 case types.EventStoreGetTotalCoins: 164 req := msg.GetData().(*types.IterateRangeByStateHash) 165 resp := &types.ReplyGetTotalCoins{} 166 resp.Count = req.Count 167 msg.Reply(client.NewMessage("", types.EventGetTotalCoinsReply, resp)) 168 default: 169 msg.ReplyErr("Do not support", types.ErrNotSupport) 170 } 171 } 172 }() 173 } 174 175 func TestLoadAccounts(t *testing.T) { 176 q := initEnv() 177 qAPI, _ := initQueAPI(q) 178 blockchainProcess(q) 179 storeProcess(q) 180 181 accCoin, _ := GenerAccDb() 182 addrs := make([]string, 1) 183 addrs[0] = addr1 184 accs, err := accCoin.LoadAccounts(qAPI, addrs) 185 require.NoError(t, err) 186 t.Logf("LoadAccounts is %v", accs) 187 } 188 189 func TestGetTotalCoins(t *testing.T) { 190 q := initEnv() 191 qAPI, _ := initQueAPI(q) 192 storeProcess(q) 193 194 accCoin, _ := GenerAccDb() 195 symbols := [2]string{"trc", ""} 196 for _, symbol := range symbols { 197 reqTotalCoin := &types.ReqGetTotalCoins{ 198 Symbol: symbol, 199 Count: 100000, 200 } 201 rsp, err := accCoin.GetTotalCoins(qAPI, reqTotalCoin) 202 require.NoError(t, err) 203 t.Logf("GetTotalCoins is %v", rsp) 204 require.Equal(t, int64(100000), rsp.Count) 205 } 206 } 207 208 func TestAccountName(t *testing.T) { 209 stroedb, _ := db.NewGoMemDB("gomemdb", "test", 128) 210 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 211 accCoin := NewCoinsAccount(cfg) 212 accCoin.SetDB(stroedb) 213 coinsAddr := address.ExecAddress("coins") 214 t.Log("coinsAddr:", coinsAddr) 215 216 accToken, _ := NewAccountDB(cfg, "token", "test", nil) 217 accToken.SetDB(stroedb) 218 tokenAddr := address.ExecAddress("token") 219 t.Log("tokenAddr:", tokenAddr) 220 221 tradeAddr := address.ExecAddress("trade") 222 t.Log("tradeAddr:", tradeAddr) 223 224 paraAddr := address.ExecAddress("paracross") 225 t.Log("paraAddr:", paraAddr) 226 227 myAddr := "13DP8mVru5Rtu6CrjXQMvLsjvve3epRR1i" 228 t.Log("paraAddr:", accToken.ExecAddress("paracross")) 229 t.Log("user.p.guodun.paraAddr:", accToken.ExecAddress("user.p.guodun.paracross")) 230 fromAcc := accToken.LoadExecAccount(myAddr, paraAddr) 231 t.Log("myAddr of paracorss", fromAcc) 232 } 233 234 func TestGetExecBalance(t *testing.T) { 235 key := "mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 236 addr := "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 237 //prefix1 := "mavl-coins-trc-exec-" 238 //prefix2 := "mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:" 239 240 /*req := &types.StoreList { 241 StateHash: []byte("0000000000000000"), 242 Start: []byte(prefix1), 243 End: []byte(addr), 244 Count: 2, 245 Mode: 2, 246 }*/ 247 248 fmt.Println("TestGetExecBalance---------------test case 1_1------------------------") 249 in := &types.ReqGetExecBalance{ 250 Symbol: "trc", 251 StateHash: []byte("0000000000"), 252 Addr: []byte(addr), 253 Execer: "coins", 254 } 255 256 in.Count = 2 257 reply, err := getExecBalance(storeList1_1, in) 258 assert.Nil(t, err) 259 assert.Equal(t, int64(4), reply.Amount) 260 assert.Equal(t, int64(2), reply.AmountFrozen) 261 assert.Equal(t, int64(2), reply.AmountActive) 262 assert.Equal(t, 2, len(reply.Items)) 263 assert.Equal(t, len([]byte(key)), len(reply.NextKey)) 264 fmt.Println("TestGetExecBalance---------------test case 1_2------------------------") 265 reply, err = getExecBalance(storeList1_2, in) 266 assert.Nil(t, err) 267 assert.Equal(t, int64(2), reply.Amount) 268 assert.Equal(t, int64(1), reply.AmountFrozen) 269 assert.Equal(t, int64(1), reply.AmountActive) 270 assert.Equal(t, 1, len(reply.Items)) 271 assert.Equal(t, 0, len(reply.NextKey)) 272 273 fmt.Println("TestGetExecBalance---------------test case 2------------------------") 274 in.Count = 3 275 reply, err = getExecBalance(storeList2, in) 276 assert.Nil(t, err) 277 assert.Equal(t, int64(6), reply.Amount) 278 assert.Equal(t, int64(3), reply.AmountFrozen) 279 assert.Equal(t, int64(3), reply.AmountActive) 280 assert.Equal(t, 3, len(reply.Items)) 281 assert.Equal(t, 0, len(reply.NextKey)) 282 283 fmt.Println("TestGetExecBalance---------------test case 3------------------------") 284 in.ExecAddr = []byte("16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp") 285 reply, err = getExecBalance(storeList3, in) 286 assert.NotNil(t, err) 287 288 fmt.Println("TestGetExecBalance---------------test case 4------------------------") 289 in.ExecAddr = []byte("26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp") 290 reply, err = getExecBalance(storeList3, in) 291 assert.Nil(t, err) 292 assert.Equal(t, int64(2), reply.Amount) 293 assert.Equal(t, int64(1), reply.AmountFrozen) 294 assert.Equal(t, int64(1), reply.AmountActive) 295 assert.Equal(t, 1, len(reply.Items)) 296 } 297 298 func cloneByte(v []byte) []byte { 299 value := make([]byte, len(v)) 300 copy(value, v) 301 return value 302 } 303 304 func storeList1_1(req *types.StoreList) (reply *types.StoreListReply, err error) { 305 reply = &types.StoreListReply{ 306 Start: req.Start, 307 End: req.End, 308 Count: req.Count, 309 Mode: req.Mode, 310 } 311 var acc = &types.Account{ 312 Currency: 0, 313 Balance: 1, 314 Frozen: 1, 315 Addr: string(req.End), 316 } 317 value := types.Encode(acc) 318 319 key1 := []byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 320 key2 := []byte("mavl-coins-trc-exec-26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 321 key3 := []byte("mavl-coins-trc-exec-36htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 322 reply.Num = 2 323 reply.Keys = append(reply.Keys, key1) 324 reply.Keys = append(reply.Keys, key2) 325 reply.Values = append(reply.Values, cloneByte(value)) 326 reply.Values = append(reply.Values, cloneByte(value)) 327 reply.NextKey = key3 328 return reply, nil 329 } 330 331 func storeList1_2(req *types.StoreList) (reply *types.StoreListReply, err error) { 332 reply = &types.StoreListReply{ 333 Start: req.Start, 334 End: req.End, 335 Count: req.Count, 336 Mode: req.Mode, 337 } 338 var acc = &types.Account{ 339 Currency: 0, 340 Balance: 1, 341 Frozen: 1, 342 Addr: string(req.End), 343 } 344 value := types.Encode(acc) 345 //key1 := []byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 346 //key2 := []byte("mavl-coins-trc-exec-26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 347 key3 := []byte("mavl-coins-trc-exec-36htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 348 reply.Num = 1 349 reply.Keys = append(reply.Keys, key3) 350 reply.Values = append(reply.Values, cloneByte(value)) 351 return reply, nil 352 } 353 354 func storeList2(req *types.StoreList) (reply *types.StoreListReply, err error) { 355 reply = &types.StoreListReply{ 356 Start: req.Start, 357 End: req.End, 358 Count: req.Count, 359 Mode: req.Mode, 360 } 361 var acc = &types.Account{ 362 Currency: 0, 363 Balance: 1, 364 Frozen: 1, 365 Addr: string(req.End), 366 } 367 value := types.Encode(acc) 368 369 key1 := []byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 370 key2 := []byte("mavl-coins-trc-exec-26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 371 key3 := []byte("mavl-coins-trc-exec-36htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 372 373 reply.Num = 3 374 reply.Keys = append(reply.Keys, key1) 375 reply.Keys = append(reply.Keys, key2) 376 reply.Keys = append(reply.Keys, key3) 377 reply.Values = append(reply.Values, cloneByte(value)) 378 reply.Values = append(reply.Values, cloneByte(value)) 379 reply.Values = append(reply.Values, cloneByte(value)) 380 381 return reply, nil 382 } 383 384 func storeList3(req *types.StoreList) (reply *types.StoreListReply, err error) { 385 reply = &types.StoreListReply{ 386 Start: req.Start, 387 End: req.End, 388 Count: req.Count, 389 Mode: req.Mode, 390 } 391 var acc = &types.Account{ 392 Currency: 0, 393 Balance: 1, 394 Frozen: 1, 395 Addr: string(req.End), 396 } 397 value := types.Encode(acc) 398 399 //key1 := []byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 400 key2 := []byte("mavl-coins-trc-exec-26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 401 //key3 := []byte("mavl-coins-trc-exec-36htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP") 402 403 reply.Num = 1 404 //reply.Keys = append(reply.Keys, key1) 405 reply.Keys = append(reply.Keys, key2) 406 //reply.Keys = append(reply.Keys, key3) 407 //reply.Values = append(reply.Values, cloneByte(value)) 408 reply.Values = append(reply.Values, cloneByte(value)) 409 //reply.Values = append(reply.Values, cloneByte(value)) 410 411 return reply, nil 412 } 413 414 func getExecBalance(callback func(*types.StoreList) (*types.StoreListReply, error), in *types.ReqGetExecBalance) (reply *types.ReplyGetExecBalance, err error) { 415 req := &types.StoreList{} 416 req.StateHash = in.StateHash 417 418 prefix := symbolExecPrefix(in.Execer, in.Symbol) 419 if len(in.ExecAddr) > 0 { 420 prefix = prefix + "-" + string(in.ExecAddr) + ":" 421 } else { 422 prefix = prefix + "-" 423 } 424 425 req.Start = []byte(prefix) 426 req.End = in.Addr 427 req.Mode = 2 //1:为[start,end)模式,按前缀或者范围进行查找。2:为prefix + suffix遍历模式,先按前缀查找,再判断后缀是否满足条件。 428 req.Count = in.Count 429 430 if len(in.NextKey) > 0 { 431 req.Start = in.NextKey 432 } 433 434 reply = &types.ReplyGetExecBalance{} 435 fmt.Println("DB.GetExecBalance", "hash", common.ToHex(req.StateHash), "Prefix", string(req.Start), "Addr", string(req.End)) 436 437 res, err := callback(req) 438 fmt.Println("send one req....") 439 if err != nil { 440 err = types.ErrTypeAsset 441 return nil, err 442 } 443 444 for i := 0; i < len(res.Keys); i++ { 445 strKey := string(res.Keys[i]) 446 fmt.Println("DB.GetExecBalance process one record", "key", strKey) 447 if !strings.HasPrefix(strKey, prefix) { 448 fmt.Println("accountDB.GetExecBalance key does not match prefix", "key:", strKey, " prefix:", prefix) 449 return nil, types.ErrTypeAsset 450 } 451 //如果prefix形如:mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp: ,则是查找addr在一个合约地址上的余额,找到一个值即可结束。 452 if strings.HasSuffix(prefix, ":") { 453 addr := strKey[len(prefix):] 454 execAddr := []byte(prefix[(len(prefix) - len(addr) - 1):(len(prefix) - 1)]) 455 fmt.Println("DB.GetExecBalance record for specific exec addr.", "execAddr:", string(execAddr), " addr:", addr) 456 reply.AddItem(execAddr, res.Values[i]) 457 } else { 458 combinAddr := strKey[len(prefix):] 459 addrs := strings.Split(combinAddr, ":") 460 if 2 != len(addrs) { 461 fmt.Println("accountDB.GetExecBalance key does not contain exec-addr & addr", "key", strKey, "combinAddr", combinAddr) 462 return nil, types.ErrTypeAsset 463 } 464 fmt.Println("DB.GetExecBalance", "execAddr", addrs[0], "addr", addrs[1]) 465 reply.AddItem([]byte(addrs[0]), res.Values[i]) 466 } 467 } 468 469 reply.NextKey = res.NextKey 470 471 return reply, nil 472 } 473 474 func TestGetExecBalance2(t *testing.T) { 475 accCoin := NewCoinsAccount(types.NewTuringchainConfig(types.GetDefaultCfgstring())) 476 key := "mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp:1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 477 execAddr := "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp" 478 addr := "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 479 480 fmt.Println("-------------TestGetExecBalance2---test case1---") 481 api := new(mocks.QueueProtocolAPI) 482 in := &types.ReqGetExecBalance{} 483 api.On("StoreList", mock.Anything).Return(&types.StoreListReply{}, nil) 484 _, err := accCoin.GetExecBalance(api, in) 485 assert.Nil(t, err) 486 487 fmt.Println("-------------TestGetExecBalance2---test case2---") 488 in.Symbol = "trc" 489 in.Execer = "coins" 490 in.Addr = []byte(addr) 491 in.ExecAddr = []byte(execAddr) 492 493 api.On("StoreList", mock.Anything).Return(&types.StoreListReply{}, nil) 494 _, err = accCoin.GetExecBalance(api, in) 495 assert.Nil(t, err) 496 497 in.NextKey = []byte(key) 498 fmt.Println("-------------TestGetExecBalance2---test case3---") 499 api.On("StoreList", mock.Anything).Return(&types.StoreListReply{}, nil) 500 _, err = accCoin.GetExecBalance(api, in) 501 assert.Nil(t, err) 502 503 fmt.Println("-------------TestGetExecBalance2---test case4---") 504 api = new(mocks.QueueProtocolAPI) 505 api.On("StoreList", mock.Anything).Return(nil, types.ErrInvalidParam) 506 _, err = accCoin.GetExecBalance(api, in) 507 assert.NotNil(t, err) 508 509 /* 510 fmt.Println("TestGetExecBalance---------------test case 1_1------------------------") 511 in := &types.ReqGetExecBalance{ 512 Symbol: "trc", 513 StateHash: []byte("0000000000"), 514 Addr: []byte(addr), 515 Execer: "coins", 516 } 517 518 in.Count = 2 519 reply, err := getExecBalance(storeList1_1, in) 520 assert.Nil(t, err) 521 assert.Equal(t, int64(4), reply.Amount) 522 assert.Equal(t, int64(2), reply.AmountFrozen) 523 assert.Equal(t, int64(2), reply.AmountActive) 524 assert.Equal(t, 2, len(reply.Items)) 525 assert.Equal(t, len([]byte(key)), len(reply.NextKey)) 526 fmt.Println("TestGetExecBalance---------------test case 1_2------------------------") 527 reply, err = getExecBalance(storeList1_2, in) 528 assert.Nil(t, err) 529 assert.Equal(t, int64(2), reply.Amount) 530 assert.Equal(t, int64(1), reply.AmountFrozen) 531 assert.Equal(t, int64(1), reply.AmountActive) 532 assert.Equal(t, 1, len(reply.Items)) 533 assert.Equal(t, 0, len(reply.NextKey)) 534 535 fmt.Println("TestGetExecBalance---------------test case 2------------------------") 536 in.Count = 3 537 reply, err = getExecBalance(storeList2, in) 538 assert.Nil(t, err) 539 assert.Equal(t, int64(6), reply.Amount) 540 assert.Equal(t, int64(3), reply.AmountFrozen) 541 assert.Equal(t, int64(3), reply.AmountActive) 542 assert.Equal(t, 3, len(reply.Items)) 543 assert.Equal(t, 0, len(reply.NextKey)) 544 545 fmt.Println("TestGetExecBalance---------------test case 3------------------------") 546 in.ExecAddr = []byte("16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp") 547 reply, err = getExecBalance(storeList3, in) 548 assert.NotNil(t, err) 549 550 fmt.Println("TestGetExecBalance---------------test case 4------------------------") 551 in.ExecAddr = []byte("26htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp") 552 reply, err = getExecBalance(storeList3, in) 553 assert.Nil(t, err) 554 assert.Equal(t, int64(2), reply.Amount) 555 assert.Equal(t, int64(1), reply.AmountFrozen) 556 assert.Equal(t, int64(1), reply.AmountActive) 557 assert.Equal(t, 1, len(reply.Items)) 558 */ 559 } 560 561 func TestGetBalance(t *testing.T) { 562 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 563 accCoin := NewCoinsAccount(cfg) 564 addr := "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP" 565 566 fmt.Println("-------------TestGetExecBalance2---test case1---") 567 api := new(mocks.QueueProtocolAPI) 568 in := &types.ReqBalance{} 569 in.Addresses = append(in.Addresses, addr) 570 api.On("StoreList", mock.Anything).Return(&types.StoreListReply{}, nil) 571 api.On("GetLastHeader", mock.Anything).Return(&types.Header{StateHash: []byte("111111111111111111111")}, nil) 572 api.On("StoreGet", mock.Anything).Return(&types.StoreReplyValue{Values: make([][]byte, 1)}, nil) 573 api.On("GetConfig", mock.Anything).Return(cfg) 574 _, err := accCoin.GetBalance(api, in) 575 assert.Nil(t, err) 576 577 fmt.Println("-------------TestGetExecBalance2---test case2---") 578 in.StateHash = "111111111111111111111" 579 _, err = accCoin.GetBalance(api, in) 580 assert.Nil(t, err) 581 582 fmt.Println("-------------TestGetExecBalance2---test case3---") 583 in.Execer = "coins" 584 //api.On("StoreList", mock.Anything).Return(nil, types.ErrInvalidParam) 585 _, err = accCoin.GetBalance(api, in) 586 t.Log(err) 587 assert.Nil(t, err) 588 } 589 590 func TestDB_Mint(t *testing.T) { 591 _, tokenCoin := GenerAccDb() 592 tokenCoin.GenerAccData() 593 594 _, err := tokenCoin.Mint(addr1, 10*1e8) 595 require.NoError(t, err) 596 t.Logf("Token mint addr balance [%d]", tokenCoin.LoadAccount(addr1).Balance) 597 require.Equal(t, int64(1000*1e8+10*1e8), tokenCoin.LoadAccount(addr1).Balance) 598 } 599 600 func TestDB_Burn(t *testing.T) { 601 _, tokenCoin := GenerAccDb() 602 tokenCoin.GenerAccData() 603 604 _, err := tokenCoin.Burn(addr1, 10*1e8) 605 require.NoError(t, err) 606 t.Logf("Token mint addr balance [%d]", tokenCoin.LoadAccount(addr1).Balance) 607 require.Equal(t, int64(1000*1e8-10*1e8), tokenCoin.LoadAccount(addr1).Balance) 608 }