github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/vent/test/events.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/hyperledger/burrow/encoding"
     8  	"github.com/hyperledger/burrow/rpc/web3/ethclient"
     9  	"google.golang.org/grpc"
    10  
    11  	"github.com/hyperledger/burrow/crypto"
    12  	"github.com/hyperledger/burrow/execution/evm/abi"
    13  	"github.com/hyperledger/burrow/execution/exec"
    14  	"github.com/hyperledger/burrow/rpc/rpctransact"
    15  	"github.com/hyperledger/burrow/txs/payload"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  const gasLimit = ethclient.BasicGasLimit * 100
    20  
    21  type TransactClient interface {
    22  	CallTxSync(ctx context.Context, in *payload.CallTx, opts ...grpc.CallOption) (*exec.TxExecution, error)
    23  }
    24  
    25  func NewBurrowTransactClient(t testing.TB, listenAddress string) rpctransact.TransactClient {
    26  	t.Helper()
    27  
    28  	conn, err := encoding.GRPCDial(listenAddress)
    29  	require.NoError(t, err)
    30  	return rpctransact.NewTransactClient(conn)
    31  }
    32  
    33  func CreateContract(t testing.TB, cli TransactClient, inputAddress crypto.Address) *exec.TxExecution {
    34  	t.Helper()
    35  
    36  	txe, err := cli.CallTxSync(context.Background(), &payload.CallTx{
    37  		Input: &payload.TxInput{
    38  			Address: inputAddress,
    39  		},
    40  		Address:  nil,
    41  		Data:     Bytecode_EventsTest,
    42  		GasLimit: gasLimit,
    43  	})
    44  	require.NoError(t, err)
    45  
    46  	if txe.Exception != nil {
    47  		t.Fatalf("call should not generate exception but returned: %v", txe.Exception.Error())
    48  	}
    49  
    50  	return txe
    51  }
    52  
    53  func CallRemoveEvent(t testing.TB, cli TransactClient, inputAddress, contractAddress crypto.Address,
    54  	name string) *exec.TxExecution {
    55  	return Call(t, cli, inputAddress, contractAddress, "removeThing", name)
    56  }
    57  
    58  func CallRemoveEvents(t testing.TB, cli TransactClient, inputAddress, contractAddress crypto.Address,
    59  	name string) *exec.TxExecution {
    60  	return Call(t, cli, inputAddress, contractAddress, "removeThings", name)
    61  }
    62  
    63  func CallAddEvent(t testing.TB, cli TransactClient, inputAddress, contractAddress crypto.Address,
    64  	name, description string) *exec.TxExecution {
    65  	return Call(t, cli, inputAddress, contractAddress, "addThing", name, description)
    66  }
    67  
    68  func CallAddEvents(t testing.TB, cli TransactClient, inputAddress, contractAddress crypto.Address,
    69  	name, description string) *exec.TxExecution {
    70  	return Call(t, cli, inputAddress, contractAddress, "addThings", name, description)
    71  }
    72  
    73  func Call(t testing.TB, cli TransactClient, inputAddress, contractAddress crypto.Address,
    74  	functionName string, args ...interface{}) *exec.TxExecution {
    75  	t.Helper()
    76  
    77  	spec, err := abi.ReadSpec(Abi_EventsTest)
    78  	require.NoError(t, err)
    79  
    80  	data, _, err := spec.Pack(functionName, args...)
    81  	require.NoError(t, err)
    82  
    83  	txe, err := cli.CallTxSync(context.Background(), &payload.CallTx{
    84  		Input: &payload.TxInput{
    85  			Address: inputAddress,
    86  		},
    87  		Address:  &contractAddress,
    88  		Data:     data,
    89  		GasLimit: gasLimit,
    90  	})
    91  	require.NoError(t, err)
    92  
    93  	if txe.Exception != nil {
    94  		t.Fatalf("call should not generate exception but returned: %v", txe.Exception.Error())
    95  	}
    96  
    97  	return txe
    98  }