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