github.com/vipernet-xyz/tm@v0.34.24/rpc/client/examples_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"log"
     8  
     9  	"github.com/vipernet-xyz/tm/abci/example/kvstore"
    10  	rpchttp "github.com/vipernet-xyz/tm/rpc/client/http"
    11  	ctypes "github.com/vipernet-xyz/tm/rpc/core/types"
    12  	rpctest "github.com/vipernet-xyz/tm/rpc/test"
    13  )
    14  
    15  func ExampleHTTP_simple() {
    16  	// Start a tendermint node (and kvstore) in the background to test against
    17  	app := kvstore.NewApplication()
    18  	node := rpctest.StartTendermint(app, rpctest.SuppressStdout, rpctest.RecreateConfig)
    19  	defer rpctest.StopTendermint(node)
    20  
    21  	// Create our RPC client
    22  	rpcAddr := rpctest.GetConfig().RPC.ListenAddress
    23  	c, err := rpchttp.New(rpcAddr, "/websocket")
    24  	if err != nil {
    25  		log.Fatal(err) //nolint:gocritic
    26  	}
    27  
    28  	// Create a transaction
    29  	k := []byte("name")
    30  	v := []byte("satoshi")
    31  	tx := append(k, append([]byte("="), v...)...)
    32  
    33  	// Broadcast the transaction and wait for it to commit (rather use
    34  	// c.BroadcastTxSync though in production).
    35  	bres, err := c.BroadcastTxCommit(context.Background(), tx)
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	if bres.CheckTx.IsErr() || bres.DeliverTx.IsErr() {
    40  		log.Fatal("BroadcastTxCommit transaction failed")
    41  	}
    42  
    43  	// Now try to fetch the value for the key
    44  	qres, err := c.ABCIQuery(context.Background(), "/key", k)
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  	if qres.Response.IsErr() {
    49  		log.Fatal("ABCIQuery failed")
    50  	}
    51  	if !bytes.Equal(qres.Response.Key, k) {
    52  		log.Fatal("returned key does not match queried key")
    53  	}
    54  	if !bytes.Equal(qres.Response.Value, v) {
    55  		log.Fatal("returned value does not match sent value")
    56  	}
    57  
    58  	fmt.Println("Sent tx     :", string(tx))
    59  	fmt.Println("Queried for :", string(qres.Response.Key))
    60  	fmt.Println("Got value   :", string(qres.Response.Value))
    61  
    62  	// Output:
    63  	// Sent tx     : name=satoshi
    64  	// Queried for : name
    65  	// Got value   : satoshi
    66  }
    67  
    68  func ExampleHTTP_batching() {
    69  	// Start a tendermint node (and kvstore) in the background to test against
    70  	app := kvstore.NewApplication()
    71  	node := rpctest.StartTendermint(app, rpctest.SuppressStdout, rpctest.RecreateConfig)
    72  
    73  	// Create our RPC client
    74  	rpcAddr := rpctest.GetConfig().RPC.ListenAddress
    75  	c, err := rpchttp.New(rpcAddr, "/websocket")
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  
    80  	defer rpctest.StopTendermint(node)
    81  
    82  	// Create our two transactions
    83  	k1 := []byte("firstName")
    84  	v1 := []byte("satoshi")
    85  	tx1 := append(k1, append([]byte("="), v1...)...)
    86  
    87  	k2 := []byte("lastName")
    88  	v2 := []byte("nakamoto")
    89  	tx2 := append(k2, append([]byte("="), v2...)...)
    90  
    91  	txs := [][]byte{tx1, tx2}
    92  
    93  	// Create a new batch
    94  	batch := c.NewBatch()
    95  
    96  	// Queue up our transactions
    97  	for _, tx := range txs {
    98  		// Broadcast the transaction and wait for it to commit (rather use
    99  		// c.BroadcastTxSync though in production).
   100  		if _, err := batch.BroadcastTxCommit(context.Background(), tx); err != nil {
   101  			log.Fatal(err) //nolint:gocritic
   102  		}
   103  	}
   104  
   105  	// Send the batch of 2 transactions
   106  	if _, err := batch.Send(context.Background()); err != nil {
   107  		log.Fatal(err)
   108  	}
   109  
   110  	// Now let's query for the original results as a batch
   111  	keys := [][]byte{k1, k2}
   112  	for _, key := range keys {
   113  		if _, err := batch.ABCIQuery(context.Background(), "/key", key); err != nil {
   114  			log.Fatal(err)
   115  		}
   116  	}
   117  
   118  	// Send the 2 queries and keep the results
   119  	results, err := batch.Send(context.Background())
   120  	if err != nil {
   121  		log.Fatal(err)
   122  	}
   123  
   124  	// Each result in the returned list is the deserialized result of each
   125  	// respective ABCIQuery response
   126  	for _, result := range results {
   127  		qr, ok := result.(*ctypes.ResultABCIQuery)
   128  		if !ok {
   129  			log.Fatal("invalid result type from ABCIQuery request")
   130  		}
   131  		fmt.Println(string(qr.Response.Key), "=", string(qr.Response.Value))
   132  	}
   133  
   134  	// Output:
   135  	// firstName = satoshi
   136  	// lastName = nakamoto
   137  }