github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/rpc/client/examples_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"log"
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/ari-anchor/sei-tendermint/abci/example/kvstore"
    15  	rpchttp "github.com/ari-anchor/sei-tendermint/rpc/client/http"
    16  	"github.com/ari-anchor/sei-tendermint/rpc/coretypes"
    17  	rpctest "github.com/ari-anchor/sei-tendermint/rpc/test"
    18  )
    19  
    20  func TestHTTPSimple(t *testing.T) {
    21  	ctx, cancel := context.WithCancel(context.Background())
    22  	defer cancel()
    23  
    24  	// Start a tendermint node (and kvstore) in the background to test against
    25  	app := kvstore.NewApplication()
    26  	conf, err := rpctest.CreateConfig(t, "ExampleHTTP_simple")
    27  	require.NoError(t, err)
    28  
    29  	_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
    30  	if err != nil {
    31  		log.Fatal(err) //nolint:gocritic
    32  	}
    33  	defer func() { _ = closer(ctx) }()
    34  
    35  	// Create our RPC client
    36  	rpcAddr := conf.RPC.ListenAddress
    37  	c, err := rpchttp.New(rpcAddr)
    38  	require.NoError(t, err)
    39  
    40  	// Create a transaction
    41  	k := []byte("name")
    42  	v := []byte("satoshi")
    43  	tx := append(k, append([]byte("="), v...)...)
    44  
    45  	// Broadcast the transaction and wait for it to commit (rather use
    46  	// c.BroadcastTxSync though in production).
    47  	bres, err := c.BroadcastTxCommit(ctx, tx)
    48  	require.NoError(t, err)
    49  	if err != nil {
    50  		log.Fatal(err)
    51  	}
    52  	if bres.CheckTx.IsErr() || bres.TxResult.IsErr() {
    53  		log.Fatal("BroadcastTxCommit transaction failed")
    54  	}
    55  
    56  	// Now try to fetch the value for the key
    57  	qres, err := c.ABCIQuery(ctx, "/key", k)
    58  	require.NoError(t, err)
    59  	require.False(t, qres.Response.IsErr(), "ABCIQuery failed")
    60  	require.True(t, bytes.Equal(qres.Response.Key, k),
    61  		"returned key does not match queried key")
    62  	require.True(t, bytes.Equal(qres.Response.Value, v),
    63  		"returned value does not match sent value [%s]", string(v))
    64  
    65  	assert.Equal(t, "name=satoshi", string(tx), "sent tx")
    66  	assert.Equal(t, "name", string(qres.Response.Key), "queried for")
    67  	assert.Equal(t, "satoshi", string(qres.Response.Value), "got value")
    68  }
    69  
    70  func TestHTTPBatching(t *testing.T) {
    71  	ctx, cancel := context.WithCancel(context.Background())
    72  	defer cancel()
    73  
    74  	// Start a tendermint node (and kvstore) in the background to test against
    75  	app := kvstore.NewApplication()
    76  	conf, err := rpctest.CreateConfig(t, "ExampleHTTP_batching")
    77  	require.NoError(t, err)
    78  
    79  	_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
    80  	if err != nil {
    81  		log.Fatal(err) //nolint:gocritic
    82  	}
    83  	defer func() { _ = closer(ctx) }()
    84  
    85  	rpcAddr := conf.RPC.ListenAddress
    86  	c, err := rpchttp.NewWithClient(rpcAddr, http.DefaultClient)
    87  	require.NoError(t, err)
    88  
    89  	// Create our two transactions
    90  	k1 := []byte("firstName")
    91  	v1 := []byte("satoshi")
    92  	tx1 := append(k1, append([]byte("="), v1...)...)
    93  
    94  	k2 := []byte("lastName")
    95  	v2 := []byte("nakamoto")
    96  	tx2 := append(k2, append([]byte("="), v2...)...)
    97  
    98  	txs := [][]byte{tx1, tx2}
    99  
   100  	// Create a new batch
   101  	batch := c.NewBatch()
   102  
   103  	// Queue up our transactions
   104  	for _, tx := range txs {
   105  		// Broadcast the transaction and wait for it to commit (rather use
   106  		// c.BroadcastTxSync though in production).
   107  		_, err := batch.BroadcastTxSync(ctx, tx)
   108  		require.NoError(t, err)
   109  	}
   110  
   111  	// Send the batch of 2 transactions
   112  	_, err = batch.Send(ctx)
   113  	require.NoError(t, err)
   114  
   115  	// wait for the transaction to land, we could poll more for
   116  	// the transactions to land definitively.
   117  	require.Eventually(t,
   118  		func() bool {
   119  			// Now let's query for the original results as a batch
   120  			exists := 0
   121  			for _, key := range [][]byte{k1, k2} {
   122  				_, err := batch.ABCIQuery(ctx, "/key", key)
   123  				if err == nil {
   124  					exists++
   125  
   126  				}
   127  			}
   128  			return exists == 2
   129  		},
   130  		10*time.Second,
   131  		time.Second,
   132  	)
   133  
   134  	// Send the 2 queries and keep the results
   135  	results, err := batch.Send(ctx)
   136  	require.NoError(t, err)
   137  
   138  	require.Len(t, results, 2)
   139  	// Each result in the returned list is the deserialized result of each
   140  	// respective ABCIQuery response
   141  	for _, result := range results {
   142  		qr, ok := result.(*coretypes.ResultABCIQuery)
   143  		require.True(t, ok, "invalid result type from ABCIQuery request")
   144  
   145  		switch string(qr.Response.Key) {
   146  		case "firstName":
   147  			require.Equal(t, "satoshi", string(qr.Response.Value))
   148  		case "lastName":
   149  			require.Equal(t, "nakamoto", string(qr.Response.Value))
   150  		default:
   151  			t.Fatalf("encountered unknown key %q", string(qr.Response.Key))
   152  		}
   153  	}
   154  }