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