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