MCPcopy Create free account
hub / github.com/tendermint/tendermint / ExampleHTTP_simple

Function ExampleHTTP_simple

rpc/client/examples_test.go:15–77  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13)
14
15func 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, err := rpctest.CreateConfig("ExampleHTTP_simple")
22 if err != nil {
23 log.Fatal(err)
24 }
25
26 _, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
27 if err != nil {
28 log.Fatal(err)
29 }
30 defer func() { _ = closer(ctx) }()
31
32 // Create our RPC client
33 rpcAddr := conf.RPC.ListenAddress
34 c, err := rpchttp.New(rpcAddr)
35 if err != nil {
36 log.Fatal(err)
37 }
38
39 // Create a transaction
40 k := []byte("name")
41 v := []byte("satoshi")
42 tx := append(k, append([]byte("="), v...)...)
43
44 // Broadcast the transaction and wait for it to commit (rather use
45 // c.BroadcastTxSync though in production).
46 bres, err := c.BroadcastTxCommit(context.Background(), tx)
47 if err != nil {
48 log.Fatal(err)
49 }
50 if bres.CheckTx.IsErr() || bres.DeliverTx.IsErr() {
51 log.Fatal("BroadcastTxCommit transaction failed")
52 }
53
54 // Now try to fetch the value for the key
55 qres, err := c.ABCIQuery(context.Background(), "/key", k)
56 if err != nil {
57 log.Fatal(err)
58 }
59 if qres.Response.IsErr() {
60 log.Fatal("ABCIQuery failed")
61 }
62 if !bytes.Equal(qres.Response.Key, k) {
63 log.Fatal("returned key does not match queried key")
64 }
65 if !bytes.Equal(qres.Response.Value, v) {
66 log.Fatal("returned value does not match sent value")
67 }
68
69 fmt.Println("Sent tx :", string(tx))
70 fmt.Println("Queried for :", string(qres.Response.Key))
71 fmt.Println("Got value :", string(qres.Response.Value))
72

Callers

nothing calls this directly

Calls 6

NewApplicationFunction · 0.92
BroadcastTxCommitMethod · 0.65
ABCIQueryMethod · 0.65
EqualMethod · 0.65
closerFuncType · 0.50
IsErrMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…