github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/integration/rpctransact/transact_server_test.go (about) 1 // +build integration 2 3 // Space above here matters 4 // Copyright Monax Industries Limited 5 // SPDX-License-Identifier: Apache-2.0 6 7 package rpctransact 8 9 import ( 10 "context" 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/hyperledger/burrow/integration" 16 17 "github.com/hyperledger/burrow/execution/exec" 18 "github.com/hyperledger/burrow/execution/solidity" 19 "github.com/hyperledger/burrow/integration/rpctest" 20 "github.com/hyperledger/burrow/rpc/rpcevents" 21 "github.com/hyperledger/burrow/rpc/rpcquery" 22 "github.com/hyperledger/burrow/rpc/rpctransact" 23 "github.com/hyperledger/burrow/txs" 24 "github.com/hyperledger/burrow/txs/payload" 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/require" 27 ) 28 29 var inputAccount = rpctest.PrivateAccounts[0] 30 var inputAddress = inputAccount.GetAddress() 31 32 func TestTransactServer(t *testing.T) { 33 t.Parallel() 34 kern, shutdown := integration.RunNode(t, rpctest.GenesisDoc, rpctest.PrivateAccounts) 35 defer shutdown() 36 37 t.Run("InputAccountPublicKeySet", func(t *testing.T) { 38 input := rpctest.PrivateAccounts[9] 39 tcli := rpctest.NewTransactClient(t, kern.GRPCListenAddress().String()) 40 qcli := rpctest.NewQueryClient(t, kern.GRPCListenAddress().String()) 41 acc, err := qcli.GetAccount(context.Background(), &rpcquery.GetAccountParam{Address: input.GetAddress()}) 42 require.NoError(t, err) 43 44 // Account PublicKey should be initially unset 45 assert.False(t, acc.GetPublicKey().IsSet()) 46 47 // Sign with this account - should set public key 48 _, err = rpctest.CreateEVMContract(tcli, input.GetAddress(), solidity.Bytecode_StrangeLoop, nil) 49 require.NoError(t, err) 50 acc, err = qcli.GetAccount(context.Background(), &rpcquery.GetAccountParam{Address: input.GetAddress()}) 51 52 // Check public key set 53 require.NoError(t, err) 54 assert.True(t, acc.PublicKey.IsSet()) 55 assert.Equal(t, input.GetPublicKey(), acc.PublicKey) 56 }) 57 58 t.Run("BroadcastTxLocallySigned", func(t *testing.T) { 59 qcli := rpctest.NewQueryClient(t, kern.GRPCListenAddress().String()) 60 acc, err := qcli.GetAccount(context.Background(), &rpcquery.GetAccountParam{ 61 Address: inputAddress, 62 }) 63 require.NoError(t, err) 64 amount := uint64(2123) 65 txEnv := txs.Enclose(rpctest.GenesisDoc.GetChainID(), &payload.SendTx{ 66 Inputs: []*payload.TxInput{{ 67 Address: inputAddress, 68 Sequence: acc.Sequence + 1, 69 Amount: amount, 70 }}, 71 Outputs: []*payload.TxOutput{{ 72 Address: rpctest.PrivateAccounts[1].GetAddress(), 73 Amount: amount, 74 }}, 75 }) 76 require.NoError(t, txEnv.Sign(inputAccount)) 77 78 // Test subscribing to transaction before sending it 79 ch := make(chan *exec.TxExecution) 80 go func() { 81 ecli := rpctest.NewExecutionEventsClient(t, kern.GRPCListenAddress().String()) 82 txe, err := ecli.Tx(context.Background(), &rpcevents.TxRequest{ 83 TxHash: txEnv.Tx.Hash(), 84 Wait: true, 85 }) 86 require.NoError(t, err) 87 ch <- txe 88 }() 89 90 // Make it wait 91 time.Sleep(time.Second) 92 93 // No broadcast 94 cli := rpctest.NewTransactClient(t, kern.GRPCListenAddress().String()) 95 receipt, err := cli.BroadcastTxAsync(context.Background(), &rpctransact.TxEnvelopeParam{Envelope: txEnv}) 96 require.NoError(t, err) 97 assert.False(t, receipt.CreatesContract, "This tx should not create a contract") 98 require.NotEmpty(t, receipt.TxHash, "Failed to compute tx hash") 99 assert.Equal(t, txEnv.Tx.Hash(), receipt.TxHash) 100 101 txe := <-ch 102 require.True(t, len(txe.Events) > 0) 103 assert.NotNil(t, txe.Events[0].Input) 104 }) 105 106 t.Run("FormulateTx", func(t *testing.T) { 107 cli := rpctest.NewTransactClient(t, kern.GRPCListenAddress().String()) 108 txEnv, err := cli.FormulateTx(context.Background(), &payload.Any{ 109 CallTx: &payload.CallTx{ 110 Input: &payload.TxInput{ 111 Address: inputAddress, 112 Amount: 230, 113 }, 114 Data: []byte{2, 3, 6, 4, 3}, 115 }, 116 }) 117 require.NoError(t, err) 118 bs, err := txEnv.Marshal() 119 require.NoError(t, err) 120 // We should see the sign bytes embedded 121 if !assert.Contains(t, string(bs), fmt.Sprintf("{\"ChainID\":\"%s\",\"Type\":\"CallTx\","+ 122 "\"Payload\":{\"Input\":{\"Address\":\"E80BB91C2F0F4C3C39FC53E89BF8416B219BE6E4\",\"Amount\":230},"+ 123 "\"Data\":\"0203060403\"}}", rpctest.GenesisDoc.GetChainID())) { 124 fmt.Println(string(bs)) 125 } 126 }) 127 }