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