github.com/turingchain2020/turingchain@v1.1.21/rpc/rpc_real_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_test 6 7 import ( 8 "encoding/hex" 9 "fmt" 10 "testing" 11 12 "github.com/turingchain2020/turingchain/common" 13 "github.com/turingchain2020/turingchain/common/address" 14 "github.com/turingchain2020/turingchain/rpc/jsonclient" 15 rpctypes "github.com/turingchain2020/turingchain/rpc/types" 16 "github.com/turingchain2020/turingchain/types" 17 "github.com/turingchain2020/turingchain/util" 18 "github.com/turingchain2020/turingchain/util/testnode" 19 "github.com/stretchr/testify/assert" 20 21 _ "github.com/turingchain2020/turingchain/system" 22 ) 23 24 func getRPCClient(t *testing.T, mocker *testnode.TuringchainMock) *jsonclient.JSONClient { 25 jrpcClient := mocker.GetJSONC() 26 assert.NotNil(t, jrpcClient) 27 return jrpcClient 28 } 29 30 func TestErrLog(t *testing.T) { 31 // 启动RPCmocker 32 mocker := testnode.New("--free--", nil) 33 defer mocker.Close() 34 cfg := mocker.GetClient().GetConfig() 35 mocker.Listen() 36 jrpcClient := getRPCClient(t, mocker) 37 gen := mocker.GetGenesisKey() 38 //发送交易到区块链 39 addr1, key1 := util.Genaddress() 40 addr2, _ := util.Genaddress() 41 tx1 := util.CreateCoinsTx(cfg, gen, addr1, 1*types.Coin) 42 mocker.GetAPI().SendTx(tx1) 43 mocker.WaitHeight(1) 44 45 tx11 := util.CreateCoinsTx(cfg, key1, addr2, 6*int64(1e7)) 46 reply, err := mocker.GetAPI().SendTx(tx11) 47 assert.Nil(t, err) 48 assert.Equal(t, reply.GetMsg(), tx11.Hash()) 49 tx12 := util.CreateCoinsTx(cfg, key1, addr2, 6*int64(1e7)) 50 reply, err = mocker.GetAPI().SendTx(tx12) 51 assert.Nil(t, err) 52 assert.Equal(t, reply.GetMsg(), tx12.Hash()) 53 mocker.WaitTx(reply.GetMsg()) 54 var testResult rpctypes.TransactionDetail 55 req := rpctypes.QueryParm{ 56 Hash: common.ToHex(tx12.Hash()), 57 } 58 //query transaction 59 err = jrpcClient.Call("Turingchain.QueryTransaction", req, &testResult) 60 assert.Nil(t, err) 61 assert.Equal(t, string(testResult.Receipt.Logs[0].Log), `"ErrNoBalance"`) 62 assert.Equal(t, "0.6000", testResult.Tx.AmountFmt) 63 assert.Equal(t, int64(6e7), testResult.Tx.Amount) 64 } 65 66 func getTx(t *testing.T, hex string) *types.Transaction { 67 data, err := common.FromHex(hex) 68 assert.Nil(t, err) 69 var tx types.Transaction 70 err = types.Decode(data, &tx) 71 assert.Nil(t, err) 72 return &tx 73 } 74 75 func TestSendToExec(t *testing.T) { 76 mocker := testnode.New("--free--", nil) 77 defer mocker.Close() 78 mocker.Listen() 79 jrpcClient := getRPCClient(t, mocker) 80 //1. 调用createrawtransaction 创建交易 81 req := &rpctypes.CreateTx{ 82 To: address.ExecAddress("user.f3d"), 83 Amount: 10, 84 Fee: 1, 85 Note: "12312", 86 IsWithdraw: false, 87 IsToken: false, 88 TokenSymbol: "", 89 ExecName: "user.f3d", 90 } 91 var res string 92 err := jrpcClient.Call("Turingchain.CreateRawTransaction", req, &res) 93 assert.Nil(t, err) 94 gen := mocker.GetGenesisKey() 95 tx := getTx(t, res) 96 tx.Sign(types.SECP256K1, gen) 97 reply, err := mocker.GetAPI().SendTx(tx) 98 assert.Nil(t, err) 99 _, err = mocker.WaitTx(reply.GetMsg()) 100 assert.Nil(t, err) 101 block := mocker.GetLastBlock() 102 balance := mocker.GetExecAccount(block.StateHash, "user.f3d", mocker.GetGenesisAddress()).Balance 103 assert.Equal(t, int64(10), balance) 104 } 105 106 func TestGetAllExecBalance(t *testing.T) { 107 mocker := testnode.New("--free--", nil) 108 defer mocker.Close() 109 mocker.Listen() 110 jrpcClient := getRPCClient(t, mocker) 111 112 addr := "38BRY193Wvy9MkdqMjmuaYeUHnJaFjUxMP" 113 req := types.ReqAddr{Addr: addr} 114 var res rpctypes.AllExecBalance 115 err := jrpcClient.Call("Turingchain.GetAllExecBalance", req, &res) 116 assert.Nil(t, err) 117 assert.Equal(t, addr, res.Addr) 118 assert.Nil(t, res.ExecAccount) 119 assert.Equal(t, 0, len(res.ExecAccount)) 120 } 121 122 func TestCreateTransactionUserWrite(t *testing.T) { 123 mocker := testnode.New("--free--", nil) 124 defer mocker.Close() 125 mocker.Listen() 126 jrpcClient := getRPCClient(t, mocker) 127 req := &rpctypes.CreateTxIn{ 128 Execer: "user.write", 129 ActionName: "write", 130 Payload: []byte(`{"key":"value"}`), 131 } 132 var res string 133 err := jrpcClient.Call("Turingchain.CreateTransaction", req, &res) 134 assert.Nil(t, err) 135 tx := getTx(t, res) 136 assert.NotNil(t, tx) 137 fmt.Println(string(tx.Payload)) 138 assert.Nil(t, err) 139 assert.Equal(t, `{"key":"value"}`, string(tx.Payload)) 140 } 141 142 func TestExprieCreateNoBalanceTransaction(t *testing.T) { 143 mocker := testnode.New("--free--", nil) 144 defer mocker.Close() 145 mocker.Listen() 146 jrpcClient := getRPCClient(t, mocker) 147 req := &rpctypes.CreateTxIn{ 148 Execer: "user.write", 149 ActionName: "write", 150 Payload: []byte(`{"key":"value"}`), 151 } 152 var res string 153 err := jrpcClient.Call("Turingchain.CreateTransaction", req, &res) 154 assert.Nil(t, err) 155 gen := mocker.GetGenesisKey().Bytes() 156 req2 := &types.NoBalanceTx{ 157 TxHex: res, 158 Privkey: common.ToHex(gen), 159 Expire: "300s", 160 } 161 var groupres string 162 err = jrpcClient.Call("Turingchain.CreateNoBalanceTransaction", req2, &groupres) 163 assert.Nil(t, err) 164 165 txByteData, err := common.FromHex(groupres) 166 assert.Nil(t, err) 167 var tx types.Transaction 168 err = types.Decode(txByteData, &tx) 169 assert.Nil(t, err) 170 txgroup, err := tx.GetTxGroup() 171 assert.Nil(t, err) 172 assert.True(t, txgroup.GetTxs()[0].GetExpire() > 0) 173 } 174 175 func TestExprieSignRawTx(t *testing.T) { 176 mocker := testnode.New("--free--", nil) 177 cfg := mocker.GetClient().GetConfig() 178 defer mocker.Close() 179 mocker.Listen() 180 jrpcClient := getRPCClient(t, mocker) 181 req := &rpctypes.CreateTxIn{ 182 Execer: "user.write", 183 ActionName: "write", 184 Payload: []byte(`{"key":"value"}`), 185 } 186 var res string 187 err := jrpcClient.Call("Turingchain.CreateTransaction", req, &res) 188 assert.Nil(t, err) 189 190 txNone := &types.Transaction{Execer: []byte(cfg.ExecName(types.NoneX)), Payload: []byte("no-fee-transaction")} 191 txNone.To = address.ExecAddress(string(txNone.Execer)) 192 txNone, err = types.FormatTx(cfg, cfg.ExecName(types.NoneX), txNone) 193 assert.NoError(t, err) 194 assert.Nil(t, err) 195 gen := mocker.GetGenesisKey().Bytes() 196 req2 := &types.CreateTransactionGroup{ 197 Txs: []string{hex.EncodeToString(types.Encode(txNone)), res}, 198 } 199 var groupres string 200 err = jrpcClient.Call("Turingchain.CreateRawTxGroup", req2, &groupres) 201 assert.Nil(t, err) 202 203 txByteData, err := common.FromHex(groupres) 204 assert.Nil(t, err) 205 var tx types.Transaction 206 err = types.Decode(txByteData, &tx) 207 assert.Nil(t, err) 208 req3 := &types.ReqSignRawTx{ 209 TxHex: common.ToHex(types.Encode(&tx)), 210 Privkey: common.ToHex(gen), 211 Expire: "300s", 212 } 213 var signgrouptx string 214 err = jrpcClient.Call("Turingchain.SignRawTx", req3, &signgrouptx) 215 assert.Nil(t, err) 216 217 txByteData, err = common.FromHex(signgrouptx) 218 assert.Nil(t, err) 219 var tx2 types.Transaction 220 err = types.Decode(txByteData, &tx2) 221 assert.Nil(t, err) 222 txgroup2, err := tx2.GetTxGroup() 223 assert.Nil(t, err) 224 assert.True(t, txgroup2.GetTxs()[0].GetExpire() > 0) 225 }