github.com/turingchain2020/turingchain@v1.1.21/rpc/client_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 "encoding/hex" 9 "fmt" 10 "testing" 11 12 "github.com/turingchain2020/turingchain/account" 13 "github.com/turingchain2020/turingchain/client/mocks" 14 "github.com/turingchain2020/turingchain/common/address" 15 slog "github.com/turingchain2020/turingchain/common/log" 16 "github.com/turingchain2020/turingchain/pluginmgr" 17 qmock "github.com/turingchain2020/turingchain/queue/mocks" 18 cty "github.com/turingchain2020/turingchain/system/dapp/coins/types" 19 "github.com/turingchain2020/turingchain/types" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/mock" 22 ) 23 24 func Init(cfg *types.TuringchainConfig) { 25 slog.SetLogLevel("error") 26 pluginmgr.InitExec(cfg) 27 } 28 29 func newTestChannelClient() *channelClient { 30 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 31 api := &mocks.QueueProtocolAPI{} 32 api.On("GetConfig", mock.Anything).Return(cfg) 33 return &channelClient{ 34 QueueProtocolAPI: api, 35 } 36 } 37 38 // TODO 39 func TestInit(t *testing.T) { 40 client := newTestChannelClient() 41 qm := &qmock.Client{} 42 cfg := client.GetConfig() 43 qm.On("GetConfig", mock.Anything).Return(cfg) 44 client.Init(qm, nil) 45 } 46 47 func testCreateRawTransactionNil(t *testing.T) { 48 client := newTestChannelClient() 49 _, err := client.CreateRawTransaction(nil) 50 assert.Equal(t, types.ErrInvalidParam, err) 51 } 52 53 func testCreateRawTransactionExecNameErr(t *testing.T) { 54 tx := types.CreateTx{ExecName: "aaa", To: "1MY4pMgjpS2vWiaSDZasRhN47pcwEire32"} 55 56 client := newTestChannelClient() 57 cfg := client.GetConfig() 58 Init(cfg) 59 _, err := client.CreateRawTransaction(&tx) 60 assert.Equal(t, types.ErrExecNameNotMatch, err) 61 } 62 63 func testCreateRawTransactionAmoutErr(t *testing.T) { 64 client := newTestChannelClient() 65 cfg := client.GetConfig() 66 tx := types.CreateTx{ExecName: cfg.ExecName(cty.CoinsX), Amount: -1, To: "1MY4pMgjpS2vWiaSDZasRhN47pcwEire32"} 67 68 _, err := client.CreateRawTransaction(&tx) 69 assert.Equal(t, types.ErrAmount, err) 70 } 71 72 func testCreateRawTransactionTo(t *testing.T) { 73 client := newTestChannelClient() 74 cfg := client.GetConfig() 75 name := cfg.ExecName(cty.CoinsX) 76 tx := types.CreateTx{ExecName: name, Amount: 1, To: "1MY4pMgjpS2vWiaSDZasRhN47pcwEire32", Fee: 1} 77 78 rawtx, err := client.CreateRawTransaction(&tx) 79 assert.NoError(t, err) 80 81 reqDecode := &types.ReqDecodeRawTransaction{TxHex: hex.EncodeToString(rawtx)} 82 _, err = client.DecodeRawTransaction(reqDecode) 83 assert.NoError(t, err) 84 85 assert.Nil(t, err) 86 var mytx types.Transaction 87 err = types.Decode(rawtx, &mytx) 88 assert.Nil(t, err) 89 if cfg.IsPara() { 90 assert.Equal(t, address.ExecAddress(name), mytx.To) 91 } else { 92 assert.Equal(t, tx.To, mytx.To) 93 } 94 } 95 96 func testCreateRawTransactionCoinTransfer(t *testing.T) { 97 ctx := types.CreateTx{ 98 ExecName: "", 99 Amount: 10, 100 IsToken: false, 101 IsWithdraw: false, 102 To: "1JkbMq5yNMZHtokjg5XxkC3RZbqjoPJm84", 103 Note: []byte("note"), 104 Fee: 1, 105 } 106 107 client := newTestChannelClient() 108 cfg := client.GetConfig() 109 txHex, err := client.CreateRawTransaction(&ctx) 110 assert.Nil(t, err) 111 var tx types.Transaction 112 types.Decode(txHex, &tx) 113 assert.Equal(t, []byte(cfg.ExecName(cty.CoinsX)), tx.Execer) 114 115 var transfer cty.CoinsAction 116 types.Decode(tx.Payload, &transfer) 117 assert.Equal(t, int32(cty.CoinsActionTransfer), transfer.Ty) 118 } 119 120 func testCreateRawTransactionCoinTransferExec(t *testing.T) { 121 client := newTestChannelClient() 122 cfg := client.GetConfig() 123 name := cfg.ExecName("coins") 124 ctx := types.CreateTx{ 125 ExecName: name, 126 Amount: 10, 127 IsToken: false, 128 IsWithdraw: false, 129 To: "1JkbMq5yNMZHtokjg5XxkC3RZbqjoPJm84", 130 Note: []byte("note"), 131 Fee: 1, 132 } 133 txHex, err := client.CreateRawTransaction(&ctx) 134 assert.Nil(t, err) 135 var tx types.Transaction 136 types.Decode(txHex, &tx) 137 assert.Equal(t, []byte(cfg.ExecName(cty.CoinsX)), tx.Execer) 138 139 var transfer cty.CoinsAction 140 types.Decode(tx.Payload, &transfer) 141 assert.Equal(t, int32(cty.CoinsActionTransferToExec), transfer.Ty) 142 if cfg.IsPara() { 143 assert.Equal(t, address.ExecAddress(cfg.ExecName(cty.CoinsX)), tx.To) 144 } else { 145 assert.Equal(t, ctx.To, tx.To) 146 } 147 } 148 149 func testCreateRawTransactionCoinWithdraw(t *testing.T) { 150 client := newTestChannelClient() 151 cfg := client.GetConfig() 152 ctx := types.CreateTx{ 153 ExecName: cfg.ExecName("coins"), 154 Amount: 10, 155 IsToken: false, 156 IsWithdraw: true, 157 To: "1JkbMq5yNMZHtokjg5XxkC3RZbqjoPJm84", 158 Note: []byte("note"), 159 Fee: 1, 160 } 161 162 txHex, err := client.CreateRawTransaction(&ctx) 163 assert.Nil(t, err) 164 var tx types.Transaction 165 types.Decode(txHex, &tx) 166 assert.Equal(t, []byte(cfg.ExecName(cty.CoinsX)), tx.Execer) 167 168 var transfer cty.CoinsAction 169 types.Decode(tx.Payload, &transfer) 170 assert.Equal(t, int32(cty.CoinsActionWithdraw), transfer.Ty) 171 172 if cfg.IsPara() { 173 assert.Equal(t, address.ExecAddress(cfg.ExecName(cty.CoinsX)), tx.To) 174 } else { 175 assert.Equal(t, ctx.To, tx.To) 176 } 177 } 178 179 func TestChannelClient_CreateRawTransaction(t *testing.T) { 180 testCreateRawTransactionNil(t) 181 testCreateRawTransactionExecNameErr(t) 182 testCreateRawTransactionAmoutErr(t) 183 testCreateRawTransactionTo(t) 184 testCreateRawTransactionCoinTransfer(t) 185 testCreateRawTransactionCoinTransferExec(t) 186 testCreateRawTransactionCoinWithdraw(t) 187 } 188 189 func testChannelClientGetAddrOverviewNil(t *testing.T) { 190 parm := &types.ReqAddr{ 191 Addr: "abcde", 192 } 193 client := newTestChannelClient() 194 _, err := client.GetAddrOverview(parm) 195 assert.Equal(t, types.ErrInvalidAddress, err) 196 } 197 198 func testChannelClientGetAddrOverviewErr(t *testing.T) { 199 parm := &types.ReqAddr{ 200 Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", 201 } 202 api := new(mocks.QueueProtocolAPI) 203 client := &channelClient{ 204 QueueProtocolAPI: api, 205 } 206 207 api.On("GetAddrOverview", mock.Anything).Return(nil, fmt.Errorf("error")) 208 _, err := client.GetAddrOverview(parm) 209 assert.EqualError(t, err, "error") 210 211 } 212 213 func testChannelClientGetAddrOverviewOK(t *testing.T) { 214 api := new(mocks.QueueProtocolAPI) 215 db := new(account.DB) 216 client := &channelClient{ 217 QueueProtocolAPI: api, 218 accountdb: db, 219 } 220 221 addr := &types.AddrOverview{} 222 api.On("GetAddrOverview", mock.Anything).Return(addr, nil) 223 224 head := &types.Header{StateHash: []byte("sdfadasds")} 225 api.On("GetLastHeader").Return(head, nil) 226 227 var acc = &types.Account{Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", Balance: 100} 228 accv := types.Encode(acc) 229 storevalue := &types.StoreReplyValue{} 230 storevalue.Values = append(storevalue.Values, accv) 231 api.On("StoreGet", mock.Anything).Return(storevalue, nil) 232 233 //reply := types.AddrOverview{} 234 parm := &types.ReqAddr{ 235 Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", 236 } 237 data, err := client.GetAddrOverview(parm) 238 assert.Nil(t, err, "error") 239 assert.Equal(t, acc.Balance, data.Balance) 240 241 } 242 243 func TestChannelClient_GetAddrOverview(t *testing.T) { 244 testChannelClientGetAddrOverviewNil(t) 245 testChannelClientGetAddrOverviewOK(t) 246 testChannelClientGetAddrOverviewErr(t) 247 } 248 249 func testChannelClientGetBalanceCoin(t *testing.T) { 250 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 251 api := &mocks.QueueProtocolAPI{} 252 api.On("GetConfig", mock.Anything).Return(cfg) 253 db := new(account.DB) 254 client := &channelClient{ 255 QueueProtocolAPI: api, 256 accountdb: db, 257 } 258 259 //addr := &types.AddrOverview{} 260 //api.On("GetAddrOverview", mock.Anything).Return(addr, nil) 261 262 head := &types.Header{StateHash: []byte("sdfadasds")} 263 api.On("GetLastHeader").Return(head, nil) 264 265 var acc = &types.Account{Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", Balance: 100} 266 accv := types.Encode(acc) 267 storevalue := &types.StoreReplyValue{} 268 storevalue.Values = append(storevalue.Values, accv) 269 api.On("StoreGet", mock.Anything).Return(storevalue, nil) 270 271 var addrs = make([]string, 1) 272 addrs = append(addrs, "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt") 273 var in = &types.ReqBalance{ 274 Execer: "coins", 275 Addresses: addrs, 276 } 277 data, err := client.GetBalance(in) 278 assert.Nil(t, err) 279 assert.Equal(t, acc.Addr, data[0].Addr) 280 281 } 282 283 func testChannelClientGetBalanceOther(t *testing.T) { 284 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 285 api := &mocks.QueueProtocolAPI{} 286 api.On("GetConfig", mock.Anything).Return(cfg) 287 db := new(account.DB) 288 client := &channelClient{ 289 QueueProtocolAPI: api, 290 accountdb: db, 291 } 292 293 //addr := &types.AddrOverview{} 294 //api.On("GetAddrOverview", mock.Anything).Return(addr, nil) 295 296 head := &types.Header{StateHash: []byte("sdfadasds")} 297 api.On("GetLastHeader").Return(head, nil) 298 299 var acc = &types.Account{Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", Balance: 100} 300 accv := types.Encode(acc) 301 storevalue := &types.StoreReplyValue{} 302 storevalue.Values = append(storevalue.Values, accv) 303 api.On("StoreGet", mock.Anything).Return(storevalue, nil) 304 305 var addrs = make([]string, 1) 306 addrs = append(addrs, "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt") 307 var in = &types.ReqBalance{ 308 Execer: cfg.ExecName("ticket"), 309 Addresses: addrs, 310 } 311 data, err := client.GetBalance(in) 312 assert.Nil(t, err) 313 assert.Equal(t, acc.Addr, data[0].Addr) 314 315 } 316 317 func TestChannelClient_GetBalance(t *testing.T) { 318 testChannelClientGetBalanceCoin(t) 319 testChannelClientGetBalanceOther(t) 320 } 321 322 func TestChannelClient_GetTotalCoins(t *testing.T) { 323 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 324 client := new(channelClient) 325 api := new(mocks.QueueProtocolAPI) 326 api.On("GetConfig", mock.Anything).Return(cfg) 327 qm := &qmock.Client{} 328 qm.On("GetConfig", mock.Anything).Return(cfg) 329 client.Init(qm, api) 330 api.On("StoreGetTotalCoins", mock.Anything).Return(&types.ReplyGetTotalCoins{}, nil) 331 _, err := client.GetTotalCoins(&types.ReqGetTotalCoins{}) 332 assert.NoError(t, err) 333 334 // accountdb = 335 //token := &types.ReqGetTotalCoins{ 336 // Symbol: "CNY", 337 // StateHash: []byte("1234"), 338 // StartKey: []byte("sad"), 339 // Count: 1, 340 // Execer: "coin", 341 //} 342 //data, err = client.GetTotalCoins(token) 343 //assert.NotNil(t, data) 344 //assert.Nil(t, err) 345 } 346 347 func TestChannelClient_CreateNoBalanceTransaction(t *testing.T) { 348 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 349 client := new(channelClient) 350 api := new(mocks.QueueProtocolAPI) 351 api.On("GetConfig", mock.Anything).Return(cfg) 352 qm := &qmock.Client{} 353 qm.On("GetConfig", mock.Anything).Return(cfg) 354 client.Init(qm, api) 355 fee := cfg.GetMinTxFeeRate() * 2 356 api.On("GetProperFee", mock.Anything).Return(&types.ReplyProperFee{ProperFee: fee}, nil) 357 in := &types.NoBalanceTx{} 358 params := &types.NoBalanceTxs{ 359 TxHexs: []string{in.GetTxHex()}, 360 PayAddr: in.GetPayAddr(), 361 Privkey: in.GetPrivkey(), 362 Expire: in.GetExpire(), 363 } 364 tx, err := client.CreateNoBalanceTxs(params) 365 assert.NoError(t, err) 366 gtx, _ := tx.GetTxGroup() 367 assert.NoError(t, gtx.Check(cfg, 0, fee, cfg.GetMaxTxFee())) 368 assert.NoError(t, err) 369 params.Expire = "300s" 370 tx, err = client.CreateNoBalanceTxs(params) 371 assert.NoError(t, err) 372 assert.Equal(t, true, (tx.GetExpire() > types.Now().Unix()) && (tx.GetExpire() <= types.Now().Unix()+300)) 373 params.TxHexs[0] = hex.EncodeToString(types.Encode(&types.Transaction{Execer: []byte("user.p.para.coins")})) 374 params.Expire = "100" 375 tx, err = client.CreateNoBalanceTxs(params) 376 assert.Equal(t, types.ErrInvalidExpire, err) 377 params.Expire = "0" 378 _, err = client.CreateNoBalanceTxs(params) 379 assert.NotEqual(t, types.ErrInvalidExpire, err) 380 params.Expire = fmt.Sprintf("%d", types.ExpireBound+1) 381 _, err = client.CreateNoBalanceTxs(params) 382 assert.NotEqual(t, types.ErrInvalidExpire, err) 383 } 384 385 func TestClientReWriteRawTx(t *testing.T) { 386 //交易组原始交易的修改测试 387 txHex1 := "0a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6720c0843d30aab4d59684b5cce7143a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4ab50c0aa3010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6720c0843d30aab4d59684b5cce7143a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522008217c413b035fddd8f34a303e90a29e661746ed9b23a97768c1f25817c2c3450a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a673094fbcabe96c99ea7163a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f552203c6a2b11cce466891f084b49450472b1d4c39213f63117d3d4ce2a3851304ebc0a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730c187fb80fe88ce9e3c3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522066419d70492f757d7285fd226dff62da8d803c8121ded95242d222dbb10f2d9b0a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a673098aa929ab292b3f0023a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f552202bab08051d24fe923f66c8aeea4ce3f425d47a72f7c5c230a2b1427e04e2eb510a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730bfe9abb3edc6d9cb163a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f55220e1ba0493aa431ea3071026bd8dfa8280efab53ce86441fc474a1c19550a554ba0a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730d2e196a8ecada9d53e3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522016600fbfa23b3f0e8f9a14b716ce8f4064c091fbf6fa94489bc9d14b5b6049a60a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730a0b7b1b1dda2f4c5743a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522089d0442d76713369022499d054db65ccacbf5c627a525bd5454e0a30d23fa2990a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730c5838f94e2f49acb4b3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522018f208938606b390d752898332a84a9fbb900c2ed55ec33cd54d09b1970043b90a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a67308dfddb82faf7dfc4113a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522013002bab7a9c65881bd937a6fded4c3959bb631fa84434572970c1ec3e6fccf90a7d0a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730b8b082d799a4ddc93a3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522008217c413b035fddd8f34a303e90a29e661746ed9b23a97768c1f25817c2c345" 388 //修改交易组的所有交易 389 ctx := types.ReWriteRawTx{ 390 Tx: txHex1, 391 Fee: 29977777777, 392 Expire: "130s", 393 To: "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt", 394 Index: 0, 395 } 396 397 client := newTestChannelClient() 398 399 txHex, err := client.ReWriteRawTx(&ctx) 400 assert.Nil(t, err) 401 rtTx := hex.EncodeToString(txHex) 402 txData, err := hex.DecodeString(rtTx) 403 assert.Nil(t, err) 404 tx := &types.Transaction{} 405 err = types.Decode(txData, tx) 406 assert.Nil(t, err) 407 assert.Equal(t, ctx.Fee, tx.Fee) 408 409 //只修改交易组中指定的交易 410 ctx2 := types.ReWriteRawTx{ 411 Tx: txHex1, 412 Fee: 29977777777, 413 Expire: "130s", 414 To: "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt", 415 Index: 2, 416 } 417 txHex22, err := client.ReWriteRawTx(&ctx2) 418 assert.Nil(t, err) 419 rtTx22 := hex.EncodeToString(txHex22) 420 txData22, err := hex.DecodeString(rtTx22) 421 assert.Nil(t, err) 422 tx22 := &types.Transaction{} 423 err = types.Decode(txData22, tx22) 424 assert.Nil(t, err) 425 group22, err := tx22.GetTxGroup() 426 assert.Nil(t, err) 427 428 for index, tmptx := range group22.GetTxs() { 429 if tmptx.GetExpire() != 0 && index != 1 { 430 t.Error("TestClientReWriteRawTx Expire !=0 index != 1 ") 431 } 432 if tmptx.GetFee() != 0 && index != 0 { 433 t.Error("TestClientReWriteRawTx Fee !=0") 434 } 435 } 436 }