github.com/turingchain2020/turingchain@v1.1.21/rpc/jrpchandler_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 rpc 6 7 import ( 8 "bytes" 9 "errors" 10 "testing" 11 12 "encoding/hex" 13 14 "github.com/turingchain2020/turingchain/account" 15 "github.com/turingchain2020/turingchain/client" 16 "github.com/turingchain2020/turingchain/client/mocks" 17 "github.com/turingchain2020/turingchain/common" 18 rpctypes "github.com/turingchain2020/turingchain/rpc/types" 19 _ "github.com/turingchain2020/turingchain/system" 20 cty "github.com/turingchain2020/turingchain/system/dapp/coins/types" 21 mty "github.com/turingchain2020/turingchain/system/dapp/manage/types" 22 "github.com/turingchain2020/turingchain/types" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/mock" 25 ) 26 27 func TestDecodeLogErr(t *testing.T) { 28 enc := "0001020304050607" 29 dec := []byte{0, 1, 2, 3, 4, 5, 6, 7} 30 31 hex.EncodeToString(dec) 32 rlog := &rpctypes.ReceiptLog{ 33 Ty: types.TyLogErr, 34 Log: "0x" + enc, 35 } 36 37 logs := []*rpctypes.ReceiptLog{} 38 logs = append(logs, rlog) 39 40 var data = &rpctypes.ReceiptData{ 41 Ty: 1, 42 Logs: logs, 43 } 44 result, err := rpctypes.DecodeLog([]byte("coins"), data) 45 assert.Nil(t, err) 46 assert.NotNil(t, result) 47 assert.Equal(t, "LogErr", result.Logs[0].TyName) 48 assert.Equal(t, int32(types.TyLogErr), result.Logs[0].Ty) 49 } 50 51 func TestDecodeLogFee(t *testing.T) { 52 var account = &types.Account{} 53 var logTmp = &types.ReceiptAccountTransfer{ 54 Prev: account, 55 Current: account, 56 } 57 58 dec := types.Encode(logTmp) 59 60 strdec := hex.EncodeToString(dec) 61 rlog := &rpctypes.ReceiptLog{ 62 Ty: types.TyLogFee, 63 Log: "0x" + strdec, 64 } 65 66 logs := []*rpctypes.ReceiptLog{} 67 logs = append(logs, rlog) 68 69 var data = &rpctypes.ReceiptData{ 70 Ty: 5, 71 Logs: logs, 72 } 73 result, err := rpctypes.DecodeLog([]byte("coins"), data) 74 assert.Nil(t, err) 75 assert.NotNil(t, result) 76 assert.Equal(t, "LogFee", result.Logs[0].TyName) 77 } 78 79 func TestDecodeLogTransfer(t *testing.T) { 80 var account = &types.Account{} 81 var logTmp = &types.ReceiptAccountTransfer{ 82 Prev: account, 83 Current: account, 84 } 85 86 dec := types.Encode(logTmp) 87 88 strdec := hex.EncodeToString(dec) 89 rlog := &rpctypes.ReceiptLog{ 90 Ty: types.TyLogTransfer, 91 Log: "0x" + strdec, 92 } 93 94 logs := []*rpctypes.ReceiptLog{} 95 logs = append(logs, rlog) 96 97 var data = &rpctypes.ReceiptData{ 98 Ty: 5, 99 Logs: logs, 100 } 101 result, err := rpctypes.DecodeLog([]byte("coins"), data) 102 assert.Nil(t, err) 103 assert.NotNil(t, result) 104 assert.Equal(t, "LogTransfer", result.Logs[0].TyName) 105 } 106 107 func TestDecodeLogGenesis(t *testing.T) { 108 enc := "0001020304050607" 109 110 rlog := &rpctypes.ReceiptLog{ 111 Ty: types.TyLogGenesis, 112 Log: "0x" + enc, 113 } 114 115 logs := []*rpctypes.ReceiptLog{} 116 logs = append(logs, rlog) 117 118 var data = &rpctypes.ReceiptData{ 119 Ty: 5, 120 Logs: logs, 121 } 122 result, err := rpctypes.DecodeLog([]byte("coins"), data) 123 assert.Nil(t, err) 124 assert.NotNil(t, result) 125 //这个已经废弃 126 assert.Equal(t, "unkownType", result.Logs[0].TyName) 127 } 128 129 func TestDecodeLogDeposit(t *testing.T) { 130 var account = &types.Account{} 131 var logTmp = &types.ReceiptAccountTransfer{ 132 Prev: account, 133 Current: account, 134 } 135 136 dec := types.Encode(logTmp) 137 138 strdec := hex.EncodeToString(dec) 139 rlog := &rpctypes.ReceiptLog{ 140 Ty: types.TyLogDeposit, 141 Log: "0x" + strdec, 142 } 143 144 logs := []*rpctypes.ReceiptLog{} 145 logs = append(logs, rlog) 146 147 var data = &rpctypes.ReceiptData{ 148 Ty: 5, 149 Logs: logs, 150 } 151 result, err := rpctypes.DecodeLog([]byte("coins"), data) 152 assert.Nil(t, err) 153 assert.NotNil(t, result) 154 assert.Equal(t, "LogDeposit", result.Logs[0].TyName) 155 } 156 157 func TestDecodeLogExecTransfer(t *testing.T) { 158 var account = &types.Account{} 159 var logTmp = &types.ReceiptExecAccountTransfer{ 160 Prev: account, 161 Current: account, 162 } 163 164 dec := types.Encode(logTmp) 165 166 strdec := hex.EncodeToString(dec) 167 rlog := &rpctypes.ReceiptLog{ 168 Ty: types.TyLogExecTransfer, 169 Log: "0x" + strdec, 170 } 171 172 logs := []*rpctypes.ReceiptLog{} 173 logs = append(logs, rlog) 174 175 var data = &rpctypes.ReceiptData{ 176 Ty: 5, 177 Logs: logs, 178 } 179 result, err := rpctypes.DecodeLog([]byte("coins"), data) 180 assert.Nil(t, err) 181 assert.NotNil(t, result) 182 assert.Equal(t, "LogExecTransfer", result.Logs[0].TyName) 183 } 184 185 func TestDecodeLogExecWithdraw(t *testing.T) { 186 var account = &types.Account{} 187 var logTmp = &types.ReceiptExecAccountTransfer{ 188 Prev: account, 189 Current: account, 190 } 191 192 dec := types.Encode(logTmp) 193 194 strdec := hex.EncodeToString(dec) 195 rlog := &rpctypes.ReceiptLog{ 196 Ty: types.TyLogExecWithdraw, 197 Log: "0x" + strdec, 198 } 199 200 logs := []*rpctypes.ReceiptLog{} 201 logs = append(logs, rlog) 202 203 var data = &rpctypes.ReceiptData{ 204 Ty: 5, 205 Logs: logs, 206 } 207 result, err := rpctypes.DecodeLog([]byte("coins"), data) 208 assert.Nil(t, err) 209 assert.NotNil(t, result) 210 assert.Equal(t, "LogExecWithdraw", result.Logs[0].TyName) 211 } 212 213 func TestDecodeLogExecDeposit(t *testing.T) { 214 var account = &types.Account{} 215 var logTmp = &types.ReceiptExecAccountTransfer{ 216 Prev: account, 217 Current: account, 218 } 219 220 dec := types.Encode(logTmp) 221 222 strdec := hex.EncodeToString(dec) 223 rlog := &rpctypes.ReceiptLog{ 224 Ty: types.TyLogExecDeposit, 225 Log: "0x" + strdec, 226 } 227 228 logs := []*rpctypes.ReceiptLog{} 229 logs = append(logs, rlog) 230 231 var data = &rpctypes.ReceiptData{ 232 Ty: 5, 233 Logs: logs, 234 } 235 result, err := rpctypes.DecodeLog([]byte("coins"), data) 236 assert.Nil(t, err) 237 assert.NotNil(t, result) 238 assert.Equal(t, "LogExecDeposit", result.Logs[0].TyName) 239 } 240 241 func TestDecodeLogExecFrozen(t *testing.T) { 242 var account = &types.Account{} 243 var logTmp = &types.ReceiptExecAccountTransfer{ 244 Prev: account, 245 Current: account, 246 } 247 248 dec := types.Encode(logTmp) 249 250 strdec := hex.EncodeToString(dec) 251 rlog := &rpctypes.ReceiptLog{ 252 Ty: types.TyLogExecFrozen, 253 Log: "0x" + strdec, 254 } 255 256 logs := []*rpctypes.ReceiptLog{} 257 logs = append(logs, rlog) 258 259 var data = &rpctypes.ReceiptData{ 260 Ty: 5, 261 Logs: logs, 262 } 263 result, err := rpctypes.DecodeLog([]byte("coins"), data) 264 assert.Nil(t, err) 265 assert.NotNil(t, result) 266 assert.Equal(t, "LogExecFrozen", result.Logs[0].TyName) 267 } 268 269 func TestDecodeLogExecActive(t *testing.T) { 270 var account = &types.Account{} 271 var logTmp = &types.ReceiptExecAccountTransfer{ 272 Prev: account, 273 Current: account, 274 } 275 276 dec := types.Encode(logTmp) 277 278 strdec := hex.EncodeToString(dec) 279 rlog := &rpctypes.ReceiptLog{ 280 Ty: types.TyLogExecActive, 281 Log: "0x" + strdec, 282 } 283 284 logs := []*rpctypes.ReceiptLog{} 285 logs = append(logs, rlog) 286 287 var data = &rpctypes.ReceiptData{ 288 Ty: 5, 289 Logs: logs, 290 } 291 result, err := rpctypes.DecodeLog([]byte("coins"), data) 292 assert.Nil(t, err) 293 assert.NotNil(t, result) 294 assert.Equal(t, "LogExecActive", result.Logs[0].TyName) 295 } 296 297 func TestDecodeLogGenesisTransfer(t *testing.T) { 298 var account = &types.Account{} 299 var logTmp = &types.ReceiptAccountTransfer{ 300 Prev: account, 301 Current: account, 302 } 303 304 dec := types.Encode(logTmp) 305 306 strdec := hex.EncodeToString(dec) 307 rlog := &rpctypes.ReceiptLog{ 308 Ty: types.TyLogGenesisTransfer, 309 Log: "0x" + strdec, 310 } 311 312 logs := []*rpctypes.ReceiptLog{} 313 logs = append(logs, rlog) 314 315 var data = &rpctypes.ReceiptData{ 316 Ty: 5, 317 Logs: logs, 318 } 319 result, err := rpctypes.DecodeLog([]byte("coins"), data) 320 assert.Nil(t, err) 321 assert.NotNil(t, result) 322 assert.Equal(t, "LogGenesisTransfer", result.Logs[0].TyName) 323 } 324 325 func TestDecodeLogGenesisDeposit(t *testing.T) { 326 var account = &types.Account{} 327 var logTmp = &types.ReceiptAccountTransfer{ 328 Prev: account, 329 Current: account, 330 } 331 332 dec := types.Encode(logTmp) 333 334 strdec := hex.EncodeToString(dec) 335 rlog := &rpctypes.ReceiptLog{ 336 Ty: types.TyLogGenesisDeposit, 337 Log: "0x" + strdec, 338 } 339 340 logs := []*rpctypes.ReceiptLog{} 341 logs = append(logs, rlog) 342 343 var data = &rpctypes.ReceiptData{ 344 Ty: 5, 345 Logs: logs, 346 } 347 result, err := rpctypes.DecodeLog([]byte("coins"), data) 348 assert.Nil(t, err) 349 assert.NotNil(t, result) 350 assert.Equal(t, "LogGenesisDeposit", result.Logs[0].TyName) 351 } 352 353 func TestDecodeLogModifyConfig(t *testing.T) { 354 var logTmp = &types.ReceiptConfig{} 355 dec := types.Encode(logTmp) 356 strdec := hex.EncodeToString(dec) 357 rlog := &rpctypes.ReceiptLog{ 358 Ty: mty.TyLogModifyConfig, 359 Log: "0x" + strdec, 360 } 361 362 logs := []*rpctypes.ReceiptLog{} 363 logs = append(logs, rlog) 364 365 var data = &rpctypes.ReceiptData{ 366 Ty: 5, 367 Logs: logs, 368 } 369 result, err := rpctypes.DecodeLog([]byte("manage"), data) 370 assert.Nil(t, err) 371 assert.NotNil(t, result) 372 assert.Equal(t, "LogModifyConfig", result.Logs[0].TyName) 373 } 374 375 func newTestTuringchain(api client.QueueProtocolAPI) *Turingchain { 376 types.AssertConfig(api) 377 return &Turingchain{ 378 cli: channelClient{ 379 QueueProtocolAPI: api, 380 accountdb: account.NewCoinsAccount(api.GetConfig()), 381 }, 382 } 383 } 384 385 func TestTuringchain_CreateRawTransaction(t *testing.T) { 386 api := new(mocks.QueueProtocolAPI) 387 // var result interface{} 388 // api.On("CreateRawTransaction", nil, &result).Return() 389 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 390 api.On("GetConfig", mock.Anything).Return(cfg) 391 testTuringchain := newTestTuringchain(api) 392 var testResult interface{} 393 err := testTuringchain.CreateRawTransaction(nil, &testResult) 394 assert.Nil(t, testResult) 395 assert.NotNil(t, err) 396 397 tx := &rpctypes.CreateTx{ 398 To: "184wj4nsgVxKyz2NhM3Yb5RK5Ap6AFRFq2", 399 Amount: 10, 400 Fee: 1, 401 Note: "12312", 402 IsWithdraw: false, 403 IsToken: false, 404 TokenSymbol: "", 405 ExecName: cfg.ExecName("coins"), 406 } 407 408 err = testTuringchain.CreateRawTransaction(tx, &testResult) 409 assert.NotNil(t, testResult) 410 assert.Nil(t, err) 411 } 412 413 func TestTuringchain_ReWriteRawTx(t *testing.T) { 414 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 415 api := new(mocks.QueueProtocolAPI) 416 api.On("GetConfig", mock.Anything).Return(cfg) 417 testTuringchain := newTestTuringchain(api) 418 txHex1 := "0a05636f696e73122c18010a281080c2d72f222131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b7120a08d0630a696c0b3f78dd9ec083a2131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b71" 419 //txHex2 := "0a05636f696e73122d18010a29108084af5f222231484c53426e7437486e486a7857797a636a6f573863663259745550663337594d6320a08d0630dbc4cbf6fbc4e1d0533a2231484c53426e7437486e486a7857797a636a6f573863663259745550663337594d63" 420 421 reTx := &rpctypes.ReWriteRawTx{ 422 Tx: txHex1, 423 Fee: 29977777777, 424 Expire: "130s", 425 To: "aabbccdd", 426 Index: 0, 427 } 428 var testResult interface{} 429 err := testTuringchain.ReWriteRawTx(reTx, &testResult) 430 assert.Nil(t, err) 431 assert.NotNil(t, testResult) 432 assert.NotEqual(t, txHex1, testResult) 433 txData, err := common.FromHex(testResult.(string)) 434 assert.Nil(t, err) 435 tx := &types.Transaction{} 436 err = types.Decode(txData, tx) 437 assert.Nil(t, err) 438 assert.Equal(t, tx.Fee, reTx.Fee) 439 assert.Equal(t, reTx.To, tx.To) 440 441 } 442 443 func TestTuringchain_CreateTxGroup(t *testing.T) { 444 api := new(mocks.QueueProtocolAPI) 445 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 446 api.On("GetConfig", mock.Anything).Return(cfg) 447 testTuringchain := newTestTuringchain(api) 448 var testResult interface{} 449 api.On("GetProperFee", mock.Anything).Return(nil, nil) 450 err := testTuringchain.CreateRawTxGroup(nil, &testResult) 451 assert.Nil(t, testResult) 452 assert.NotNil(t, err) 453 454 txHex1 := "0a05636f696e73122c18010a281080c2d72f222131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b7120a08d0630a696c0b3f78dd9ec083a2131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b71" 455 txHex2 := "0a05636f696e73122d18010a29108084af5f222231484c53426e7437486e486a7857797a636a6f573863663259745550663337594d6320a08d0630dbc4cbf6fbc4e1d0533a2231484c53426e7437486e486a7857797a636a6f573863663259745550663337594d63" 456 txs := &types.CreateTransactionGroup{ 457 Txs: []string{txHex1, txHex2}, 458 } 459 err = testTuringchain.CreateRawTxGroup(txs, &testResult) 460 assert.Nil(t, err) 461 tx, err := decodeTx(testResult.(string)) 462 assert.Nil(t, err) 463 tg, err := tx.GetTxGroup() 464 assert.Nil(t, err) 465 if len(tg.GetTxs()) != 2 { 466 t.Error("Test createtxgroup failed") 467 return 468 } 469 err = tx.Check(cfg, 0, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee()) 470 assert.Nil(t, err) 471 } 472 473 func TestTuringchain_SendTransaction(t *testing.T) { 474 //if types.IsPara() { 475 // t.Skip() 476 // return 477 //} 478 api := new(mocks.QueueProtocolAPI) 479 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 480 api.On("GetConfig", mock.Anything).Return(cfg) 481 tx := &types.Transaction{} 482 api.On("SendTx", tx).Return(nil, errors.New("error value")) 483 testTuringchain := newTestTuringchain(api) 484 var testResult interface{} 485 data := rpctypes.RawParm{ 486 Data: "", 487 } 488 err := testTuringchain.SendTransaction(data, &testResult) 489 t.Log(err) 490 assert.Equal(t, nil, testResult) 491 assert.NotNil(t, err) 492 493 mock.AssertExpectationsForObjects(t, api) 494 } 495 496 func TestTuringchain_SendTransactionSync(t *testing.T) { 497 api := new(mocks.QueueProtocolAPI) 498 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 499 api.On("GetConfig", mock.Anything).Return(cfg) 500 tx := &types.Transaction{} 501 hash := tx.Hash() 502 api.On("SendTx", tx).Return(&types.Reply{IsOk: true, Msg: hash}, nil) 503 api.On("QueryTx", mock.Anything).Return(&types.TransactionDetail{}, nil) 504 testTuringchain := newTestTuringchain(api) 505 var testResult interface{} 506 data := rpctypes.RawParm{ 507 Data: common.ToHex(types.Encode(tx)), 508 } 509 err := testTuringchain.SendTransactionSync(data, &testResult) 510 t.Log(err) 511 assert.Equal(t, common.ToHex(hash), testResult.(string)) 512 assert.Nil(t, err) 513 } 514 515 func TestTuringchain_GetHexTxByHash(t *testing.T) { 516 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 517 api := new(mocks.QueueProtocolAPI) 518 api.On("GetConfig", mock.Anything).Return(cfg) 519 api.On("QueryTx", &types.ReqHash{Hash: []byte("")}).Return(nil, errors.New("error value")) 520 testTuringchain := newTestTuringchain(api) 521 var testResult interface{} 522 data := rpctypes.QueryParm{ 523 Hash: "", 524 } 525 err := testTuringchain.GetHexTxByHash(data, &testResult) 526 t.Log(err) 527 assert.Equal(t, nil, testResult) 528 assert.NotNil(t, err) 529 530 mock.AssertExpectationsForObjects(t, api) 531 } 532 533 func TestTuringchain_QueryTransaction(t *testing.T) { 534 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 535 api := new(mocks.QueueProtocolAPI) 536 api.On("GetConfig", mock.Anything).Return(cfg) 537 api.On("QueryTx", &types.ReqHash{Hash: []byte("")}).Return(nil, errors.New("error value")) 538 testTuringchain := newTestTuringchain(api) 539 var testResult interface{} 540 data := rpctypes.QueryParm{ 541 Hash: "", 542 } 543 err := testTuringchain.QueryTransaction(data, &testResult) 544 t.Log(err) 545 assert.Equal(t, nil, testResult) 546 assert.NotNil(t, err) 547 548 mock.AssertExpectationsForObjects(t, api) 549 } 550 551 func TestTuringchain_QueryTransactionOk(t *testing.T) { 552 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 553 data := rpctypes.QueryParm{ 554 Hash: "", 555 } 556 var act = &cty.CoinsAction{ 557 Ty: 1, 558 } 559 payload := types.Encode(act) 560 var tx = &types.Transaction{ 561 Execer: []byte(cfg.ExecName("ticket")), 562 Payload: payload, 563 } 564 565 var logTmp = &types.ReceiptAccountTransfer{} 566 567 dec := types.Encode(logTmp) 568 569 strdec := hex.EncodeToString(dec) 570 strdec = "0x" + strdec 571 572 rlog := &types.ReceiptLog{ 573 Ty: types.TyLogTransfer, 574 Log: []byte(strdec), 575 } 576 577 logs := []*types.ReceiptLog{} 578 logs = append(logs, rlog) 579 580 var rdata = &types.ReceiptData{ 581 Ty: 5, 582 Logs: logs, 583 } 584 reply := types.TransactionDetail{ 585 Tx: tx, 586 Receipt: rdata, 587 Height: 10, 588 } 589 590 api := new(mocks.QueueProtocolAPI) 591 api.On("GetConfig", mock.Anything).Return(cfg) 592 api.On("QueryTx", &types.ReqHash{Hash: []byte("")}).Return(&reply, nil) 593 testTuringchain := newTestTuringchain(api) 594 var testResult interface{} 595 596 err := testTuringchain.QueryTransaction(data, &testResult) 597 t.Log(err) 598 assert.Nil(t, err) 599 assert.Equal(t, testResult.(*rpctypes.TransactionDetail).Height, reply.Height) 600 assert.Equal(t, testResult.(*rpctypes.TransactionDetail).Tx.Execer, string(tx.Execer)) 601 602 mock.AssertExpectationsForObjects(t, api) 603 } 604 605 func TestTuringchain_GetBlocks(t *testing.T) { 606 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 607 api := new(mocks.QueueProtocolAPI) 608 api.On("GetConfig", mock.Anything).Return(cfg) 609 api.On("GetBlocks", &types.ReqBlocks{Pid: []string{""}}).Return(&types.BlockDetails{Items: []*types.BlockDetail{{}}}, nil) 610 testTuringchain := newTestTuringchain(api) 611 var testResult interface{} 612 data := rpctypes.BlockParam{} 613 err := testTuringchain.GetBlocks(data, &testResult) 614 t.Log(err) 615 assert.NoError(t, err) 616 617 mock.AssertExpectationsForObjects(t, api) 618 } 619 620 func TestTuringchain_GetLastHeader(t *testing.T) { 621 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 622 api := new(mocks.QueueProtocolAPI) 623 api.On("GetConfig", mock.Anything).Return(cfg) 624 api.On("GetLastHeader", mock.Anything).Return(&types.Header{}, nil) 625 testTuringchain := newTestTuringchain(api) 626 var testResult interface{} 627 data := &types.ReqNil{} 628 err := testTuringchain.GetLastHeader(data, &testResult) 629 t.Log(err) 630 assert.NotNil(t, &testResult) 631 assert.NoError(t, err) 632 633 mock.AssertExpectationsForObjects(t, api) 634 } 635 636 func TestTuringchain_GetTxByAddr(t *testing.T) { 637 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 638 api := new(mocks.QueueProtocolAPI) 639 api.On("GetConfig", mock.Anything).Return(cfg) 640 testTuringchain := newTestTuringchain(api) 641 642 api.On("GetTransactionByAddr", mock.Anything).Return(&types.ReplyTxInfos{TxInfos: []*types.ReplyTxInfo{{}}}, nil) 643 var testResult interface{} 644 data := types.ReqAddr{} 645 err := testTuringchain.GetTxByAddr(data, &testResult) 646 t.Log(err) 647 assert.NotNil(t, testResult) 648 assert.NoError(t, err) 649 650 mock.AssertExpectationsForObjects(t, api) 651 } 652 653 func TestTuringchain_GetTxByHashes(t *testing.T) { 654 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 655 api := new(mocks.QueueProtocolAPI) 656 api.On("GetConfig", mock.Anything).Return(cfg) 657 testTuringchain := newTestTuringchain(api) 658 659 api.On("GetTransactionByHash", mock.Anything).Return(&types.TransactionDetails{}, nil) 660 var testResult interface{} 661 data := rpctypes.ReqHashes{} 662 data.Hashes = append(data.Hashes, "0xdcf13a93e3bf58534c773e13d339894c18dafbd3ff273a9d1caa0c2bec8e8cd6") 663 err := testTuringchain.GetTxByHashes(data, &testResult) 664 t.Log(err) 665 assert.NotNil(t, testResult) 666 assert.NoError(t, err) 667 668 mock.AssertExpectationsForObjects(t, api) 669 } 670 671 func TestTuringchain_GetMempool(t *testing.T) { 672 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 673 api := new(mocks.QueueProtocolAPI) 674 api.On("GetConfig", mock.Anything).Return(cfg) 675 testTuringchain := newTestTuringchain(api) 676 677 api.On("GetMempool", &types.ReqGetMempool{}).Return(&types.ReplyTxList{Txs: []*types.Transaction{{}}}, nil) 678 var testResult interface{} 679 data := &types.ReqGetMempool{IsAll: false} 680 err := testTuringchain.GetMempool(data, &testResult) 681 t.Log(err) 682 assert.NotNil(t, testResult) 683 assert.NoError(t, err) 684 685 mock.AssertExpectationsForObjects(t, api) 686 } 687 688 func TestTuringchain_GetAccountsV2(t *testing.T) { 689 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 690 api := new(mocks.QueueProtocolAPI) 691 api.On("GetConfig", mock.Anything).Return(cfg) 692 testTuringchain := newTestTuringchain(api) 693 694 api.On("ExecWalletFunc", "wallet", "WalletGetAccountList", mock.Anything).Return(&types.WalletAccounts{Wallets: []*types.WalletAccount{{}}}, nil) 695 var testResult interface{} 696 err := testTuringchain.GetAccountsV2(nil, &testResult) 697 t.Log(err) 698 assert.NotNil(t, testResult) 699 assert.NoError(t, err) 700 } 701 702 func TestTuringchain_GetAccounts(t *testing.T) { 703 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 704 api := new(mocks.QueueProtocolAPI) 705 api.On("GetConfig", mock.Anything).Return(cfg) 706 testTuringchain := newTestTuringchain(api) 707 708 api.On("ExecWalletFunc", "wallet", "WalletGetAccountList", mock.Anything).Return(nil, errors.New("error value")) 709 var testResult interface{} 710 data := &types.ReqAccountList{} 711 err := testTuringchain.GetAccounts(data, &testResult) 712 t.Log(err) 713 assert.Equal(t, nil, testResult) 714 assert.NotNil(t, err) 715 716 mock.AssertExpectationsForObjects(t, api) 717 } 718 719 func TestTuringchain_NewAccount(t *testing.T) { 720 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 721 api := new(mocks.QueueProtocolAPI) 722 api.On("GetConfig", mock.Anything).Return(cfg) 723 testTuringchain := newTestTuringchain(api) 724 725 api.On("ExecWalletFunc", "wallet", "NewAccount", &types.ReqNewAccount{}).Return(nil, errors.New("error value")) 726 727 var testResult interface{} 728 err := testTuringchain.NewAccount(types.ReqNewAccount{}, &testResult) 729 t.Log(err) 730 assert.Equal(t, nil, testResult) 731 assert.NotNil(t, err) 732 mock.AssertExpectationsForObjects(t, api) 733 } 734 735 func TestTuringchain_GetAccount(t *testing.T) { 736 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 737 api := new(mocks.QueueProtocolAPI) 738 api.On("GetConfig", mock.Anything).Return(cfg) 739 testTuringchain := newTestTuringchain(api) 740 741 api.On("ExecWalletFunc", "wallet", "WalletGetAccount", &types.ReqGetAccount{}).Return(nil, errors.New("error value")) 742 743 var testResult interface{} 744 err := testTuringchain.GetAccount(types.ReqGetAccount{}, &testResult) 745 t.Log(err) 746 assert.Equal(t, nil, testResult) 747 assert.NotNil(t, err) 748 mock.AssertExpectationsForObjects(t, api) 749 } 750 func TestTuringchain_WalletTxList(t *testing.T) { 751 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 752 api := new(mocks.QueueProtocolAPI) 753 api.On("GetConfig", mock.Anything).Return(cfg) 754 testTuringchain := newTestTuringchain(api) 755 756 expected := &types.ReqWalletTransactionList{FromTx: []byte("")} 757 api.On("ExecWalletFunc", "wallet", "WalletTransactionList", expected).Return(nil, errors.New("error value")) 758 759 var testResult interface{} 760 actual := rpctypes.ReqWalletTransactionList{} 761 err := testTuringchain.WalletTxList(actual, &testResult) 762 t.Log(err) 763 assert.Equal(t, nil, testResult) 764 assert.NotNil(t, err) 765 766 mock.AssertExpectationsForObjects(t, api) 767 } 768 769 func TestTuringchain_ImportPrivkey(t *testing.T) { 770 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 771 api := new(mocks.QueueProtocolAPI) 772 api.On("GetConfig", mock.Anything).Return(cfg) 773 testTuringchain := newTestTuringchain(api) 774 775 expected := &types.ReqWalletImportPrivkey{} 776 api.On("ExecWalletFunc", "wallet", "WalletImportPrivkey", expected).Return(nil, errors.New("error value")) 777 778 var testResult interface{} 779 actual := types.ReqWalletImportPrivkey{} 780 err := testTuringchain.ImportPrivkey(actual, &testResult) 781 t.Log(err) 782 assert.Equal(t, nil, testResult) 783 assert.NotNil(t, err) 784 785 mock.AssertExpectationsForObjects(t, api) 786 } 787 788 func TestTuringchain_SendToAddress(t *testing.T) { 789 //if types.IsPara() { 790 // t.Skip() 791 // return 792 //} 793 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 794 api := new(mocks.QueueProtocolAPI) 795 api.On("GetConfig", mock.Anything).Return(cfg) 796 testTuringchain := newTestTuringchain(api) 797 798 expected := &types.ReqWalletSendToAddress{} 799 api.On("ExecWalletFunc", "wallet", "WalletSendToAddress", expected).Return(nil, errors.New("error value")) 800 801 var testResult interface{} 802 actual := types.ReqWalletSendToAddress{} 803 err := testTuringchain.SendToAddress(actual, &testResult) 804 t.Log(err) 805 assert.Equal(t, nil, testResult) 806 assert.NotNil(t, err) 807 808 mock.AssertExpectationsForObjects(t, api) 809 } 810 811 func TestTuringchain_SetTxFee(t *testing.T) { 812 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 813 api := new(mocks.QueueProtocolAPI) 814 api.On("GetConfig", mock.Anything).Return(cfg) 815 testTuringchain := newTestTuringchain(api) 816 817 expected := &types.ReqWalletSetFee{} 818 api.On("ExecWalletFunc", "wallet", "WalletSetFee", expected).Return(nil, errors.New("error value")) 819 820 var testResult interface{} 821 actual := types.ReqWalletSetFee{} 822 err := testTuringchain.SetTxFee(actual, &testResult) 823 t.Log(err) 824 assert.Equal(t, nil, testResult) 825 assert.NotNil(t, err) 826 827 mock.AssertExpectationsForObjects(t, api) 828 } 829 830 func TestTuringchain_SetLabl(t *testing.T) { 831 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 832 api := new(mocks.QueueProtocolAPI) 833 api.On("GetConfig", mock.Anything).Return(cfg) 834 testTuringchain := newTestTuringchain(api) 835 836 expected := &types.ReqWalletSetLabel{} 837 api.On("ExecWalletFunc", "wallet", "WalletSetLabel", expected).Return(nil, errors.New("error value")) 838 839 var testResult interface{} 840 actual := types.ReqWalletSetLabel{} 841 err := testTuringchain.SetLabl(actual, &testResult) 842 t.Log(err) 843 assert.Equal(t, nil, testResult) 844 assert.NotNil(t, err) 845 846 mock.AssertExpectationsForObjects(t, api) 847 } 848 849 func TestTuringchain_MergeBalance(t *testing.T) { 850 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 851 api := new(mocks.QueueProtocolAPI) 852 api.On("GetConfig", mock.Anything).Return(cfg) 853 testTuringchain := newTestTuringchain(api) 854 855 expected := &types.ReqWalletMergeBalance{} 856 api.On("ExecWalletFunc", "wallet", "WalletMergeBalance", expected).Return(nil, errors.New("error value")) 857 858 var testResult interface{} 859 actual := types.ReqWalletMergeBalance{} 860 err := testTuringchain.MergeBalance(actual, &testResult) 861 t.Log(err) 862 assert.Equal(t, nil, testResult) 863 assert.NotNil(t, err) 864 865 mock.AssertExpectationsForObjects(t, api) 866 } 867 868 func TestTuringchain_SetPasswd(t *testing.T) { 869 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 870 api := new(mocks.QueueProtocolAPI) 871 api.On("GetConfig", mock.Anything).Return(cfg) 872 testTuringchain := newTestTuringchain(api) 873 874 expected := &types.ReqWalletSetPasswd{} 875 api.On("ExecWalletFunc", "wallet", "WalletSetPasswd", expected).Return(nil, errors.New("error value")) 876 877 var testResult interface{} 878 actual := types.ReqWalletSetPasswd{} 879 err := testTuringchain.SetPasswd(actual, &testResult) 880 t.Log(err) 881 assert.Equal(t, nil, testResult) 882 assert.NotNil(t, err) 883 884 mock.AssertExpectationsForObjects(t, api) 885 } 886 887 func TestTuringchain_Lock(t *testing.T) { 888 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 889 api := new(mocks.QueueProtocolAPI) 890 api.On("GetConfig", mock.Anything).Return(cfg) 891 testTuringchain := newTestTuringchain(api) 892 893 expected := types.ReqNil{} 894 api.On("ExecWalletFunc", "wallet", "WalletLock", &expected).Return(nil, errors.New("error value")) 895 896 var testResult interface{} 897 actual := types.ReqNil{} 898 err := testTuringchain.Lock(actual, &testResult) 899 t.Log(err) 900 assert.Equal(t, nil, testResult) 901 assert.NotNil(t, err) 902 903 mock.AssertExpectationsForObjects(t, api) 904 } 905 906 func TestTuringchain_UnLock(t *testing.T) { 907 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 908 api := new(mocks.QueueProtocolAPI) 909 api.On("GetConfig", mock.Anything).Return(cfg) 910 testTuringchain := newTestTuringchain(api) 911 912 expected := &types.WalletUnLock{} 913 api.On("ExecWalletFunc", "wallet", "WalletUnLock", expected).Return(nil, errors.New("error value")) 914 915 var testResult interface{} 916 actual := types.WalletUnLock{} 917 err := testTuringchain.UnLock(actual, &testResult) 918 t.Log(err) 919 assert.Equal(t, nil, testResult) 920 assert.NotNil(t, err) 921 922 mock.AssertExpectationsForObjects(t, api) 923 } 924 925 func TestTuringchain_GetPeerInfo(t *testing.T) { 926 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 927 api := new(mocks.QueueProtocolAPI) 928 api.On("GetConfig", mock.Anything).Return(cfg) 929 testTuringchain := newTestTuringchain(api) 930 931 api.On("PeerInfo", mock.Anything).Return(nil, errors.New("error value")) 932 933 var testResult interface{} 934 actual := types.P2PGetPeerReq{} 935 err := testTuringchain.GetPeerInfo(actual, &testResult) 936 t.Log(err) 937 assert.Equal(t, nil, testResult) 938 assert.NotNil(t, err) 939 940 mock.AssertExpectationsForObjects(t, api) 941 } 942 943 func TestTuringchain_GetPeerInfoOk(t *testing.T) { 944 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 945 api := new(mocks.QueueProtocolAPI) 946 api.On("GetConfig", mock.Anything).Return(cfg) 947 testTuringchain := newTestTuringchain(api) 948 949 var peerlist types.PeerList 950 var pr = &types.Peer{ 951 Addr: "abcdsd", 952 } 953 peerlist.Peers = append(peerlist.Peers, pr) 954 955 api.On("PeerInfo", mock.Anything).Return(&peerlist, nil) 956 var testResult interface{} 957 var in types.P2PGetPeerReq 958 _ = testTuringchain.GetPeerInfo(in, &testResult) 959 assert.Equal(t, testResult.(*rpctypes.PeerList).Peers[0].Addr, peerlist.Peers[0].Addr) 960 } 961 962 func TestTuringchain_GetHeaders(t *testing.T) { 963 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 964 api := new(mocks.QueueProtocolAPI) 965 api.On("GetConfig", mock.Anything).Return(cfg) 966 testTuringchain := newTestTuringchain(api) 967 968 expected := &types.ReqBlocks{} 969 api.On("GetHeaders", expected).Return(nil, errors.New("error value")) 970 971 var testResult interface{} 972 actual := types.ReqBlocks{} 973 err := testTuringchain.GetHeaders(actual, &testResult) 974 t.Log(err) 975 assert.Equal(t, nil, testResult) 976 assert.NotNil(t, err) 977 978 mock.AssertExpectationsForObjects(t, api) 979 } 980 981 func TestTuringchain_GetHeadersOk(t *testing.T) { 982 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 983 api := new(mocks.QueueProtocolAPI) 984 api.On("GetConfig", mock.Anything).Return(cfg) 985 testTuringchain := newTestTuringchain(api) 986 987 var headers types.Headers 988 var header = &types.Header{ 989 TxCount: 10, 990 } 991 headers.Items = append(headers.Items, header) 992 993 expected := &types.ReqBlocks{} 994 api.On("GetHeaders", expected).Return(&headers, nil) 995 996 var testResult interface{} 997 actual := types.ReqBlocks{} 998 err := testTuringchain.GetHeaders(actual, &testResult) 999 assert.Nil(t, err) 1000 assert.Equal(t, testResult.(*rpctypes.Headers).Items[0].TxCount, header.TxCount) 1001 1002 } 1003 1004 func TestTuringchain_GetLastMemPool(t *testing.T) { 1005 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1006 api := new(mocks.QueueProtocolAPI) 1007 api.On("GetConfig", mock.Anything).Return(cfg) 1008 testTuringchain := newTestTuringchain(api) 1009 1010 // expected := &types.ReqBlocks{} 1011 api.On("GetLastMempool").Return(nil, errors.New("error value")) 1012 1013 var testResult interface{} 1014 actual := types.ReqNil{} 1015 err := testTuringchain.GetLastMemPool(actual, &testResult) 1016 t.Log(err) 1017 assert.Equal(t, nil, testResult) 1018 assert.NotNil(t, err) 1019 1020 mock.AssertExpectationsForObjects(t, api) 1021 } 1022 1023 func TestTuringchain_GetProperFee(t *testing.T) { 1024 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1025 api := new(mocks.QueueProtocolAPI) 1026 api.On("GetConfig", mock.Anything).Return(cfg) 1027 testTuringchain := newTestTuringchain(api) 1028 1029 expected := types.ReqProperFee{} 1030 api.On("GetProperFee", &expected).Return(nil, errors.New("error value")) 1031 1032 var testResult interface{} 1033 err := testTuringchain.GetProperFee(expected, &testResult) 1034 t.Log(err) 1035 assert.Equal(t, nil, testResult) 1036 assert.NotNil(t, err) 1037 1038 mock.AssertExpectationsForObjects(t, api) 1039 } 1040 1041 func TestTuringchain_GetBlockOverview(t *testing.T) { 1042 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1043 api := new(mocks.QueueProtocolAPI) 1044 api.On("GetConfig", mock.Anything).Return(cfg) 1045 testTuringchain := newTestTuringchain(api) 1046 1047 expected := &types.ReqHash{Hash: []byte{}} 1048 api.On("GetBlockOverview", expected).Return(nil, errors.New("error value")) 1049 1050 var testResult interface{} 1051 actual := rpctypes.QueryParm{} 1052 err := testTuringchain.GetBlockOverview(actual, &testResult) 1053 t.Log(err) 1054 assert.Equal(t, nil, testResult) 1055 assert.NotNil(t, err) 1056 1057 mock.AssertExpectationsForObjects(t, api) 1058 } 1059 1060 func TestTuringchain_GetBlockOverviewOk(t *testing.T) { 1061 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1062 api := new(mocks.QueueProtocolAPI) 1063 api.On("GetConfig", mock.Anything).Return(cfg) 1064 testTuringchain := newTestTuringchain(api) 1065 var head = &types.Header{ 1066 Hash: []byte("123456"), 1067 } 1068 var replyblock = &types.BlockOverview{ 1069 Head: head, 1070 TxCount: 1, 1071 } 1072 1073 expected := &types.ReqHash{Hash: []byte{0x12, 0x34, 0x56}} 1074 api.On("GetBlockOverview", expected).Return(replyblock, nil) 1075 1076 var testResult interface{} 1077 actual := rpctypes.QueryParm{Hash: "123456"} 1078 1079 err := testTuringchain.GetBlockOverview(actual, &testResult) 1080 t.Log(err) 1081 assert.Nil(t, err) 1082 assert.Equal(t, testResult.(*rpctypes.BlockOverview).TxCount, replyblock.TxCount) 1083 assert.Equal(t, testResult.(*rpctypes.BlockOverview).Head.Hash, common.ToHex(replyblock.Head.Hash)) 1084 mock.AssertExpectationsForObjects(t, api) 1085 } 1086 1087 func TestTuringchain_GetAddrOverview(t *testing.T) { 1088 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1089 api := new(mocks.QueueProtocolAPI) 1090 api.On("GetConfig", mock.Anything).Return(cfg) 1091 testTuringchain := newTestTuringchain(api) 1092 1093 expected := &types.ReqAddr{} 1094 api.On("GetAddrOverview", expected).Return(nil, errors.New("error value")) 1095 1096 var testResult interface{} 1097 actual := types.ReqAddr{} 1098 err := testTuringchain.GetAddrOverview(actual, &testResult) 1099 t.Log(err) 1100 assert.Equal(t, nil, testResult) 1101 assert.NotNil(t, err) 1102 1103 // mock.AssertExpectationsForObjects(t, api) 1104 } 1105 1106 func TestTuringchain_GetBlockHash(t *testing.T) { 1107 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1108 api := new(mocks.QueueProtocolAPI) 1109 api.On("GetConfig", mock.Anything).Return(cfg) 1110 testTuringchain := newTestTuringchain(api) 1111 1112 expected := &types.ReqInt{} 1113 api.On("GetBlockHash", expected).Return(nil, errors.New("error value")) 1114 1115 var testResult interface{} 1116 actual := types.ReqInt{} 1117 err := testTuringchain.GetBlockHash(actual, &testResult) 1118 t.Log(err) 1119 assert.Equal(t, nil, testResult) 1120 assert.NotNil(t, err) 1121 1122 mock.AssertExpectationsForObjects(t, api) 1123 } 1124 1125 func TestTuringchain_GenSeed(t *testing.T) { 1126 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1127 api := new(mocks.QueueProtocolAPI) 1128 api.On("GetConfig", mock.Anything).Return(cfg) 1129 testTuringchain := newTestTuringchain(api) 1130 1131 expected := &types.GenSeedLang{} 1132 api.On("ExecWalletFunc", "wallet", "GenSeed", expected).Return(nil, errors.New("error value")) 1133 1134 var testResult interface{} 1135 actual := types.GenSeedLang{} 1136 err := testTuringchain.GenSeed(actual, &testResult) 1137 t.Log(err) 1138 assert.Equal(t, nil, testResult) 1139 assert.NotNil(t, err) 1140 1141 mock.AssertExpectationsForObjects(t, api) 1142 } 1143 1144 func TestTuringchain_SaveSeed(t *testing.T) { 1145 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1146 api := new(mocks.QueueProtocolAPI) 1147 api.On("GetConfig", mock.Anything).Return(cfg) 1148 testTuringchain := newTestTuringchain(api) 1149 1150 expected := &types.SaveSeedByPw{} 1151 api.On("ExecWalletFunc", "wallet", "SaveSeed", expected).Return(nil, errors.New("error value")) 1152 1153 var testResult interface{} 1154 actual := types.SaveSeedByPw{} 1155 err := testTuringchain.SaveSeed(actual, &testResult) 1156 t.Log(err) 1157 assert.Equal(t, nil, testResult) 1158 assert.NotNil(t, err) 1159 1160 mock.AssertExpectationsForObjects(t, api) 1161 } 1162 1163 func TestTuringchain_GetSeed(t *testing.T) { 1164 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1165 api := new(mocks.QueueProtocolAPI) 1166 api.On("GetConfig", mock.Anything).Return(cfg) 1167 testTuringchain := newTestTuringchain(api) 1168 1169 expected := &types.GetSeedByPw{} 1170 api.On("ExecWalletFunc", "wallet", "GetSeed", expected).Return(nil, errors.New("error value")) 1171 1172 var testResult interface{} 1173 actual := types.GetSeedByPw{} 1174 err := testTuringchain.GetSeed(actual, &testResult) 1175 t.Log(err) 1176 assert.Equal(t, nil, testResult) 1177 assert.NotNil(t, err) 1178 1179 mock.AssertExpectationsForObjects(t, api) 1180 } 1181 1182 func TestTuringchain_GetWalletStatus(t *testing.T) { 1183 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1184 api := new(mocks.QueueProtocolAPI) 1185 api.On("GetConfig", mock.Anything).Return(cfg) 1186 testTuringchain := newTestTuringchain(api) 1187 1188 expected := types.ReqNil{} 1189 api.On("ExecWalletFunc", "wallet", "GetWalletStatus", &expected).Return(nil, errors.New("error value")).Once() 1190 1191 var testResult interface{} 1192 actual := types.ReqNil{} 1193 err := testTuringchain.GetWalletStatus(actual, &testResult) 1194 t.Log(err) 1195 assert.Equal(t, nil, testResult) 1196 assert.NotNil(t, err) 1197 1198 expect := types.WalletStatus{ 1199 IsWalletLock: true, 1200 IsAutoMining: true, 1201 IsHasSeed: false, 1202 IsTicketLock: false, 1203 } 1204 1205 api.On("ExecWalletFunc", "wallet", "GetWalletStatus", &expected).Return(&expect, nil).Once() 1206 err = testTuringchain.GetWalletStatus(actual, &testResult) 1207 t.Log(err) 1208 assert.Nil(t, err) 1209 status, ok := testResult.(*rpctypes.WalletStatus) 1210 if !ok { 1211 t.Error("GetWalletStatus type error") 1212 } 1213 assert.Equal(t, expect.IsWalletLock, status.IsWalletLock) 1214 assert.Equal(t, expect.IsAutoMining, status.IsAutoMining) 1215 assert.Equal(t, expect.IsHasSeed, status.IsHasSeed) 1216 assert.Equal(t, expect.IsTicketLock, status.IsTicketLock) 1217 1218 mock.AssertExpectationsForObjects(t, api) 1219 } 1220 1221 // ---------------------------- 1222 1223 func TestTuringchain_Version(t *testing.T) { 1224 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1225 api := new(mocks.QueueProtocolAPI) 1226 api.On("GetConfig", mock.Anything).Return(cfg) 1227 testTuringchain := newTestTuringchain(api) 1228 var testResult interface{} 1229 in := &types.ReqNil{} 1230 ver := &types.VersionInfo{Turingchain: "6.0.2"} 1231 api.On("Version", mock.Anything).Return(ver, nil) 1232 err := testTuringchain.Version(in, &testResult) 1233 t.Log(err) 1234 t.Log(testResult) 1235 assert.Equal(t, nil, err) 1236 assert.NotNil(t, testResult) 1237 } 1238 1239 func TestTuringchain_GetTimeStatus(t *testing.T) { 1240 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1241 api := new(mocks.QueueProtocolAPI) 1242 api.On("GetConfig", mock.Anything).Return(cfg) 1243 client := newTestTuringchain(api) 1244 var result interface{} 1245 err := client.GetTimeStatus(&types.ReqNil{}, &result) 1246 assert.Nil(t, err) 1247 } 1248 1249 func TestTuringchain_GetServerTime(t *testing.T) { 1250 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1251 api := new(mocks.QueueProtocolAPI) 1252 api.On("GetConfig", mock.Anything).Return(cfg) 1253 client := newTestTuringchain(api) 1254 var result interface{} 1255 err := client.GetServerTime(&types.ReqNil{}, &result) 1256 assert.Nil(t, err) 1257 } 1258 1259 func TestTuringchain_GetLastBlockSequence(t *testing.T) { 1260 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1261 api := new(mocks.QueueProtocolAPI) 1262 api.On("GetConfig", mock.Anything).Return(cfg) 1263 client := newTestTuringchain(api) 1264 var result interface{} 1265 api.On("GetLastBlockSequence", mock.Anything).Return(nil, types.ErrInvalidParam) 1266 err := client.GetLastBlockSequence(&types.ReqNil{}, &result) 1267 assert.NotNil(t, err) 1268 1269 api = new(mocks.QueueProtocolAPI) 1270 api.On("GetConfig", mock.Anything).Return(cfg) 1271 client = newTestTuringchain(api) 1272 var result2 interface{} 1273 lastSeq := types.Int64{Data: 1} 1274 api.On("GetLastBlockSequence", mock.Anything).Return(&lastSeq, nil) 1275 err = client.GetLastBlockSequence(&types.ReqNil{}, &result2) 1276 assert.Nil(t, err) 1277 assert.Equal(t, int64(1), result2) 1278 } 1279 1280 func TestTuringchain_GetBlockSequences(t *testing.T) { 1281 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1282 api := new(mocks.QueueProtocolAPI) 1283 api.On("GetConfig", mock.Anything).Return(cfg) 1284 client := newTestTuringchain(api) 1285 var result interface{} 1286 api.On("GetBlockSequences", mock.Anything).Return(nil, types.ErrInvalidParam) 1287 err := client.GetBlockSequences(rpctypes.BlockParam{}, &result) 1288 assert.NotNil(t, err) 1289 1290 api = new(mocks.QueueProtocolAPI) 1291 api.On("GetConfig", mock.Anything).Return(cfg) 1292 client = newTestTuringchain(api) 1293 var result2 interface{} 1294 blocks := types.BlockSequences{} 1295 blocks.Items = make([]*types.BlockSequence, 0) 1296 blocks.Items = append(blocks.Items, &types.BlockSequence{Hash: []byte("h1"), Type: 1}) 1297 api.On("GetBlockSequences", mock.Anything).Return(&blocks, nil) 1298 err = client.GetBlockSequences(rpctypes.BlockParam{}, &result2) 1299 assert.Nil(t, err) 1300 assert.Equal(t, 1, len(result2.(*rpctypes.ReplyBlkSeqs).BlkSeqInfos)) 1301 } 1302 1303 func TestTuringchain_GetBlockByHashes(t *testing.T) { 1304 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1305 api := new(mocks.QueueProtocolAPI) 1306 api.On("GetConfig", mock.Anything).Return(cfg) 1307 client := newTestTuringchain(api) 1308 var testResult interface{} 1309 in := rpctypes.ReqHashes{Hashes: []string{}} 1310 in.Hashes = append(in.Hashes, common.ToHex([]byte("h1"))) 1311 api.On("GetBlockByHashes", mock.Anything).Return(&types.BlockDetails{}, nil) 1312 err := client.GetBlockByHashes(in, &testResult) 1313 assert.Nil(t, err) 1314 1315 api = new(mocks.QueueProtocolAPI) 1316 api.On("GetConfig", mock.Anything).Return(cfg) 1317 client = newTestTuringchain(api) 1318 var testResult2 interface{} 1319 api.On("GetBlockByHashes", mock.Anything).Return(nil, types.ErrInvalidParam) 1320 err = client.GetBlockByHashes(in, &testResult2) 1321 assert.NotNil(t, err) 1322 } 1323 1324 func TestTuringchain_CreateTransaction(t *testing.T) { 1325 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1326 api := new(mocks.QueueProtocolAPI) 1327 api.On("GetConfig", mock.Anything).Return(cfg) 1328 client := newTestTuringchain(api) 1329 1330 var result interface{} 1331 err := client.CreateTransaction(nil, &result) 1332 assert.NotNil(t, err) 1333 1334 in := &rpctypes.CreateTxIn{Execer: "notExist", ActionName: "x", Payload: []byte("x")} 1335 err = client.CreateTransaction(in, &result) 1336 assert.Equal(t, types.ErrExecNotFound, err) 1337 1338 in = &rpctypes.CreateTxIn{Execer: cfg.ExecName("coins"), ActionName: "notExist", Payload: []byte("x")} 1339 err = client.CreateTransaction(in, &result) 1340 assert.Equal(t, types.ErrActionNotSupport, err) 1341 1342 in = &rpctypes.CreateTxIn{ 1343 Execer: cfg.ExecName("coins"), 1344 ActionName: "Transfer", 1345 Payload: []byte("{\"to\": \"addr\", \"amount\":\"10\"}"), 1346 } 1347 err = client.CreateTransaction(in, &result) 1348 assert.Nil(t, err) 1349 } 1350 1351 func TestTuringchain_GetExecBalance(t *testing.T) { 1352 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1353 api := new(mocks.QueueProtocolAPI) 1354 api.On("GetConfig", mock.Anything).Return(cfg) 1355 client := newTestTuringchain(api) 1356 var testResult interface{} 1357 in := &types.ReqGetExecBalance{} 1358 api.On("StoreList", mock.Anything).Return(&types.StoreListReply{}, nil) 1359 err := client.GetExecBalance(in, &testResult) 1360 assert.Nil(t, err) 1361 1362 api = new(mocks.QueueProtocolAPI) 1363 api.On("GetConfig", mock.Anything).Return(cfg) 1364 client = newTestTuringchain(api) 1365 var testResult2 interface{} 1366 api.On("StoreList", mock.Anything).Return(nil, types.ErrInvalidParam) 1367 err = client.GetExecBalance(in, &testResult2) 1368 assert.NotNil(t, err) 1369 } 1370 1371 func TestTuringchain_GetBalance(t *testing.T) { 1372 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1373 api := new(mocks.QueueProtocolAPI) 1374 api.On("GetConfig", mock.Anything).Return(cfg) 1375 client := newTestTuringchain(api) 1376 1377 var addrs = []string{"1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt"} 1378 cases := []struct { 1379 In types.ReqBalance 1380 }{ 1381 {In: types.ReqBalance{ 1382 Execer: cfg.ExecName("coins"), 1383 Addresses: addrs, 1384 }}, 1385 {In: types.ReqBalance{ 1386 Execer: cfg.ExecName("ticket"), 1387 Addresses: addrs, 1388 }}, 1389 1390 {In: types.ReqBalance{ 1391 AssetSymbol: "trc", 1392 AssetExec: "coins", 1393 Execer: cfg.ExecName("ticket"), 1394 Addresses: addrs, 1395 }}, 1396 {In: types.ReqBalance{ 1397 AssetSymbol: "trc", 1398 AssetExec: "coins", 1399 Execer: cfg.ExecName("coins"), 1400 Addresses: addrs, 1401 }}, 1402 } 1403 1404 for _, c := range cases { 1405 c := c 1406 t.Run("test GetBalance", func(t *testing.T) { 1407 head := &types.Header{StateHash: []byte("sdfadasds")} 1408 api.On("GetLastHeader").Return(head, nil) 1409 1410 var acc = &types.Account{Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", Balance: 100} 1411 accv := types.Encode(acc) 1412 storevalue := &types.StoreReplyValue{} 1413 storevalue.Values = append(storevalue.Values, accv) 1414 api.On("StoreGet", mock.Anything).Return(storevalue, nil) 1415 1416 var data interface{} 1417 err := client.GetBalance(c.In, &data) 1418 assert.Nil(t, err) 1419 result := data.([]*rpctypes.Account) 1420 assert.Equal(t, 1, len(result)) 1421 //t.Error("result", "x", result) 1422 assert.Equal(t, acc.Addr, result[0].Addr) 1423 assert.Equal(t, int64(100), result[0].Balance) 1424 }) 1425 } 1426 1427 //测试地址不合法返回ErrInvalidAddress 1428 var data1 interface{} 1429 var addrs1 = []string{"17n2qu84Z1SUUosWjySggBS9pKWdAP3tZt"} 1430 input := types.ReqBalance{ 1431 Execer: cfg.ExecName("coins"), 1432 Addresses: addrs1, 1433 } 1434 1435 err := client.GetBalance(input, &data1) 1436 assert.Equal(t, err, types.ErrInvalidAddress) 1437 1438 //测试多重签名地址不合法返回ErrInvalidAddress 1439 var addrs2 = []string{"3BJqXn4v741wDJY6Fzb4YbLSftXwgDzFE8"} 1440 input = types.ReqBalance{ 1441 Execer: cfg.ExecName("coins"), 1442 Addresses: addrs2, 1443 } 1444 1445 err = client.GetBalance(input, &data1) 1446 assert.Equal(t, err, types.ErrInvalidAddress) 1447 1448 //测试多重签名地址合法 1449 var addrs3 = []string{"3BJqXn4v741wDJY6Fzb4YbLSftXwgDzFE7"} 1450 input = types.ReqBalance{ 1451 Execer: cfg.ExecName("coins"), 1452 Addresses: addrs3, 1453 } 1454 1455 err = client.GetBalance(input, &data1) 1456 assert.Nil(t, err) 1457 1458 } 1459 1460 func TestTuringchain_CreateNoBalanceTransaction(t *testing.T) { 1461 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1462 api := new(mocks.QueueProtocolAPI) 1463 api.On("GetConfig", mock.Anything).Return(cfg) 1464 turingchain := newTestTuringchain(api) 1465 api.On("GetProperFee", mock.Anything).Return(&types.ReplyProperFee{ProperFee: 1000000}, nil) 1466 var result string 1467 err := turingchain.CreateNoBalanceTransaction(&types.NoBalanceTx{TxHex: "0a05636f696e73122c18010a281080c2d72f222131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b7120a08d0630a696c0b3f78dd9ec083a2131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b71"}, &result) 1468 assert.NoError(t, err) 1469 } 1470 1471 func TestTuringchain_CreateNoBalanceTxs(t *testing.T) { 1472 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1473 api := new(mocks.QueueProtocolAPI) 1474 api.On("GetConfig", mock.Anything).Return(cfg) 1475 turingchain := newTestTuringchain(api) 1476 api.On("GetProperFee", mock.Anything).Return(&types.ReplyProperFee{ProperFee: 1000000}, nil) 1477 var result string 1478 err := turingchain.CreateNoBlanaceTxs(&types.NoBalanceTxs{TxHexs: []string{"0a05746f6b656e12413804223d0a0443434e5910a09c011a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d0630969a9fe6c4b9c7ba5d3a2231333559774e715367694551787577586650626d526d483259353345646738643438", "0a05746f6b656e12413804223d0a0443434e5910b0ea011a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d0630bca0a2dbc0f182e06f3a2231333559774e715367694551787577586650626d526d483259353345646738643438"}}, &result) 1479 assert.NoError(t, err) 1480 } 1481 1482 func TestTuringchain_ExecWallet(t *testing.T) { 1483 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1484 api := new(mocks.QueueProtocolAPI) 1485 api.On("GetConfig", mock.Anything).Return(cfg) 1486 client := newTestTuringchain(api) 1487 var testResult interface{} 1488 in := &rpctypes.ChainExecutor{} 1489 api.On("ExecWallet", mock.Anything).Return(nil, nil) 1490 err := client.ExecWallet(in, &testResult) 1491 assert.NotNil(t, err) 1492 } 1493 1494 func TestTuringchain_Query(t *testing.T) { 1495 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1496 api := new(mocks.QueueProtocolAPI) 1497 api.On("GetConfig", mock.Anything).Return(cfg) 1498 client := newTestTuringchain(api) 1499 var testResult interface{} 1500 in := rpctypes.Query4Jrpc{Execer: "coins"} 1501 api.On("Query", mock.Anything).Return(nil, nil) 1502 err := client.Query(in, &testResult) 1503 assert.NotNil(t, err) 1504 } 1505 1506 func TestTuringchain_DumpPrivkey(t *testing.T) { 1507 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1508 api := new(mocks.QueueProtocolAPI) 1509 api.On("GetConfig", mock.Anything).Return(cfg) 1510 client := newTestTuringchain(api) 1511 var testResult interface{} 1512 api.On("ExecWalletFunc", "wallet", "DumpPrivkey", mock.Anything).Return(nil, nil) 1513 err := client.DumpPrivkey(types.ReqString{}, &testResult) 1514 assert.NoError(t, err) 1515 } 1516 1517 func TestTuringchain_DumpPrivkeysFile(t *testing.T) { 1518 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1519 api := new(mocks.QueueProtocolAPI) 1520 api.On("GetConfig", mock.Anything).Return(cfg) 1521 client := newTestTuringchain(api) 1522 var testResult interface{} 1523 api.On("ExecWalletFunc", "wallet", "DumpPrivkeysFile", mock.Anything).Return(&types.Reply{}, nil) 1524 err := client.DumpPrivkeysFile(types.ReqPrivkeysFile{}, &testResult) 1525 assert.NoError(t, err) 1526 } 1527 1528 func TestTuringchain_ImportPrivkeysFile(t *testing.T) { 1529 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1530 api := new(mocks.QueueProtocolAPI) 1531 api.On("GetConfig", mock.Anything).Return(cfg) 1532 client := newTestTuringchain(api) 1533 var testResult interface{} 1534 api.On("ExecWalletFunc", "wallet", "ImportPrivkeysFile", mock.Anything).Return(&types.Reply{}, nil) 1535 err := client.ImportPrivkeysFile(types.ReqPrivkeysFile{}, &testResult) 1536 assert.NoError(t, err) 1537 } 1538 1539 func TestTuringchain_GetTotalCoins(t *testing.T) { 1540 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1541 api := new(mocks.QueueProtocolAPI) 1542 api.On("GetConfig", mock.Anything).Return(cfg) 1543 client := newTestTuringchain(api) 1544 var testResult interface{} 1545 api.On("StoreGetTotalCoins", mock.Anything).Return(nil, nil) 1546 err := client.GetTotalCoins(&types.ReqGetTotalCoins{}, &testResult) 1547 assert.NoError(t, err) 1548 } 1549 1550 func TestTuringchain_GetFatalFailure(t *testing.T) { 1551 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1552 api := new(mocks.QueueProtocolAPI) 1553 api.On("GetConfig", mock.Anything).Return(cfg) 1554 client := newTestTuringchain(api) 1555 var testResult interface{} 1556 1557 expected := types.ReqNil{} 1558 api.On("ExecWalletFunc", "wallet", "FatalFailure", &expected).Return(&types.Int32{}, nil) 1559 err := client.GetFatalFailure(&expected, &testResult) 1560 assert.NoError(t, err) 1561 } 1562 1563 func TestTuringchain_DecodeRawTransaction(t *testing.T) { 1564 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1565 api := new(mocks.QueueProtocolAPI) 1566 api.On("GetConfig", mock.Anything).Return(cfg) 1567 client := newTestTuringchain(api) 1568 var testResult interface{} 1569 err := client.DecodeRawTransaction(&types.ReqDecodeRawTransaction{TxHex: "0a05636f696e73122c18010a281080c2d72f222131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b7120a08d0630a696c0b3f78dd9ec083a2131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b71"}, &testResult) 1570 assert.NoError(t, err) 1571 } 1572 1573 func TestTuringchain_CloseQueue(t *testing.T) { 1574 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1575 api := new(mocks.QueueProtocolAPI) 1576 api.On("GetConfig", mock.Anything).Return(cfg) 1577 client := newTestTuringchain(api) 1578 var testResult interface{} 1579 api.On("CloseQueue", mock.Anything).Return(nil, nil) 1580 err := client.CloseQueue(nil, &testResult) 1581 assert.True(t, testResult.(*types.Reply).IsOk) 1582 assert.NoError(t, err) 1583 } 1584 1585 func TestTuringchain_AddSeqCallBack(t *testing.T) { 1586 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1587 api := new(mocks.QueueProtocolAPI) 1588 api.On("GetConfig", mock.Anything).Return(cfg) 1589 client := newTestTuringchain(api) 1590 var testResult interface{} 1591 api.On("AddPushSubscribe", mock.Anything).Return(&types.ReplySubscribePush{}, nil) 1592 err := client.AddPushSubscribe(nil, &testResult) 1593 assert.NoError(t, err) 1594 } 1595 1596 func TestTuringchain_ListSeqCallBack(t *testing.T) { 1597 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1598 api := new(mocks.QueueProtocolAPI) 1599 api.On("GetConfig", mock.Anything).Return(cfg) 1600 client := newTestTuringchain(api) 1601 var testResult interface{} 1602 api.On("ListPushes", mock.Anything).Return(&types.PushSubscribes{}, nil) 1603 err := client.ListPushes(nil, &testResult) 1604 assert.NoError(t, err) 1605 } 1606 1607 func TestTuringchain_GetSeqCallBackLastNum(t *testing.T) { 1608 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1609 api := new(mocks.QueueProtocolAPI) 1610 api.On("GetConfig", mock.Anything).Return(cfg) 1611 client := newTestTuringchain(api) 1612 var testResult interface{} 1613 api.On("GetPushSeqLastNum", mock.Anything).Return(&types.Int64{}, nil) 1614 err := client.GetPushSeqLastNum(nil, &testResult) 1615 assert.NoError(t, err) 1616 } 1617 1618 func TestTuringchain_ConvertExectoAddr(t *testing.T) { 1619 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1620 api := new(mocks.QueueProtocolAPI) 1621 api.On("GetConfig", mock.Anything).Return(cfg) 1622 client := newTestTuringchain(api) 1623 var testResult string 1624 err := client.ConvertExectoAddr(rpctypes.ExecNameParm{ExecName: "coins"}, &testResult) 1625 assert.NoError(t, err) 1626 } 1627 1628 func Test_fmtTxDetail(t *testing.T) { 1629 1630 tx := &types.Transaction{Execer: []byte("coins")} 1631 log := &types.ReceiptLog{Ty: 0, Log: []byte("test")} 1632 receipt := &types.ReceiptData{Ty: 0, Logs: []*types.ReceiptLog{log}} 1633 detail := &types.TransactionDetail{Tx: tx, Receipt: receipt} 1634 var err error 1635 //test withdraw swap from to 1636 detail.Fromaddr = "from" 1637 detail.Tx.Payload, err = common.FromHex("0x180322301080c2d72f2205636f696e732a22314761485970576d71414a7371527772706f4e6342385676674b7453776a63487174") 1638 assert.NoError(t, err) 1639 tx.To = "to" 1640 tran, err := fmtTxDetail(detail, false) 1641 assert.NoError(t, err) 1642 assert.Equal(t, "to", tran.Fromaddr) 1643 assert.Equal(t, "from", tx.To) 1644 } 1645 1646 func queryTotalFee(client *Turingchain, req *types.LocalDBGet, t *testing.T) int64 { 1647 var testResult interface{} 1648 err := client.QueryTotalFee(req, &testResult) 1649 assert.NoError(t, err) 1650 fee, _ := testResult.(types.TotalFee) 1651 return fee.Fee 1652 } 1653 1654 func TestTuringchain_QueryTotalFee(t *testing.T) { 1655 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1656 api := new(mocks.QueueProtocolAPI) 1657 api.On("GetConfig", mock.Anything).Return(cfg) 1658 client := newTestTuringchain(api) 1659 1660 total := &types.TotalFee{TxCount: 1, Fee: 10000} 1661 api.On("LocalGet", mock.Anything).Return(&types.LocalReplyValue{Values: [][]byte{types.Encode(total)}}, nil) 1662 req := &types.LocalDBGet{Keys: [][]byte{types.TotalFeeKey([]byte("testHash"))}} 1663 req1 := &types.LocalDBGet{Keys: [][]byte{[]byte("testHash")}} 1664 1665 assert.Equal(t, total.Fee, queryTotalFee(client, req, t)) 1666 assert.Equal(t, total.Fee, queryTotalFee(client, req1, t)) 1667 assert.True(t, bytes.Equal(req.Keys[0], req1.Keys[0])) 1668 } 1669 1670 func TestTuringchain_GetSequenceByHash(t *testing.T) { 1671 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1672 api := new(mocks.QueueProtocolAPI) 1673 api.On("GetConfig", mock.Anything).Return(cfg) 1674 client := newTestTuringchain(api) 1675 api.On("GetSequenceByHash", mock.Anything).Return(&types.Int64{}, nil) 1676 var testResult interface{} 1677 err := client.GetSequenceByHash(rpctypes.ReqHashes{Hashes: []string{"testhash"}}, &testResult) 1678 assert.Error(t, err) 1679 hash := "0x06a9f4ae07dd8a9b5f7f01ed23084967209d5ddff7195a8515ce43da218e8aa7" 1680 err = client.GetSequenceByHash(rpctypes.ReqHashes{Hashes: []string{hash}}, &testResult) 1681 assert.Nil(t, err) 1682 1683 } 1684 1685 func TestTuringchain_GetBlockBySeq(t *testing.T) { 1686 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1687 api := new(mocks.QueueProtocolAPI) 1688 api.On("GetConfig", mock.Anything).Return(cfg) 1689 client := newTestTuringchain(api) 1690 api.On("GetBlockBySeq", mock.Anything).Return(&types.BlockSeq{Num: 12345, Seq: &types.BlockSequence{Type: 1, Hash: []byte("1111")}}, nil) 1691 var testResult interface{} 1692 err := client.GetBlockBySeq(types.Int64{Data: 11}, &testResult) 1693 assert.Nil(t, err) 1694 1695 } 1696 1697 func TestTuringchain_GetParaTxByTitle(t *testing.T) { 1698 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1699 api := new(mocks.QueueProtocolAPI) 1700 api.On("GetConfig", mock.Anything).Return(cfg) 1701 client := newTestTuringchain(api) 1702 api.On("GetParaTxByTitle", mock.Anything).Return(&types.ParaTxDetails{Items: []*types.ParaTxDetail{}}, nil) 1703 var testResult interface{} 1704 err := client.GetParaTxByTitle(types.ReqParaTxByTitle{Start: 11, End: 11, Title: "2323"}, &testResult) 1705 assert.Nil(t, err) 1706 } 1707 1708 func TestTuringchain_LoadParaTxByTitle(t *testing.T) { 1709 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1710 api := new(mocks.QueueProtocolAPI) 1711 api.On("GetConfig", mock.Anything).Return(cfg) 1712 client := newTestTuringchain(api) 1713 api.On("LoadParaTxByTitle", mock.Anything).Return(&types.ReplyHeightByTitle{}, nil) 1714 var testResult interface{} 1715 err := client.LoadParaTxByTitle(types.ReqHeightByTitle{}, &testResult) 1716 assert.Nil(t, err) 1717 } 1718 1719 func TestTuringchain_GetParaTxByHeight(t *testing.T) { 1720 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1721 api := new(mocks.QueueProtocolAPI) 1722 api.On("GetConfig", mock.Anything).Return(cfg) 1723 client := newTestTuringchain(api) 1724 api.On("GetParaTxByHeight", mock.Anything).Return(&types.ParaTxDetails{}, nil) 1725 var testResult interface{} 1726 err := client.GetParaTxByHeight(types.ReqParaTxByHeight{}, &testResult) 1727 assert.Nil(t, err) 1728 } 1729 1730 func TestTuringchain_QueryChain(t *testing.T) { 1731 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 1732 api := new(mocks.QueueProtocolAPI) 1733 api.On("GetConfig", mock.Anything).Return(cfg) 1734 client := newTestTuringchain(api) 1735 api.On("QueryChain", mock.Anything).Return(nil, types.ErrInvalidParam) 1736 var testResult interface{} 1737 err := client.QueryChain(rpctypes.ChainExecutor{}, &testResult) 1738 assert.NotNil(t, err) 1739 } 1740 1741 func TestTuringchain_convertParaTxDetails(t *testing.T) { 1742 1743 var details types.ParaTxDetails 1744 var detail types.ParaTxDetail 1745 details.Items = append(details.Items, &detail) 1746 detail.Type = 123 1747 txhash := "0x7feb86911f2143b992c5d543cc7314f24c3f94535f1beb38f781f2a0d72ae918" 1748 hashBs, err := common.FromHex(txhash) 1749 assert.Nil(t, err) 1750 detail.Header = &types.Header{Height: 555, BlockTime: 39169, TxHash: hashBs} 1751 var rmsg rpctypes.ParaTxDetails 1752 convertParaTxDetails(&details, &rmsg) 1753 assert.Equal(t, 555, int(rmsg.Items[0].Header.Height)) 1754 assert.Equal(t, 39169, int(rmsg.Items[0].Header.BlockTime)) 1755 assert.Equal(t, txhash, rmsg.Items[0].Header.TxHash) 1756 } 1757 1758 func TestTuringchain_convertHeader(t *testing.T) { 1759 var header types.Header 1760 var reheader rpctypes.Header 1761 header.TxHash, _ = hex.DecodeString("7feb86911f2143b992c5d543cc7314f24c3f94535f1beb38f781f2a0d72ae918") 1762 header.Height = 666 1763 header.BlockTime = 1234567 1764 header.Signature = nil 1765 header.TxCount = 9 1766 convertHeader(&header, &reheader) 1767 assert.Equal(t, "0x7feb86911f2143b992c5d543cc7314f24c3f94535f1beb38f781f2a0d72ae918", reheader.TxHash) 1768 assert.Equal(t, header.GetTxCount(), reheader.TxCount) 1769 assert.Equal(t, header.GetBlockTime(), reheader.BlockTime) 1770 assert.Equal(t, header.GetHeight(), reheader.Height) 1771 1772 }