Files
tendermint/rpc/client/examples_test.go
William Banfield 0b8a62c87b abci: Synchronize FinalizeBlock with the updated specification (#7983)
This change set implements the most recent version of `FinalizeBlock`. 

# What does this change actually contain?

* This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name.
* The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock`
* The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous.
* Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across.
* Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created.

# Questions for reviewers
* We store this [ABCIResponses](5721a13ab1/proto/tendermint/state/types.pb.go (L37)) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable?
* Similarly, this change is exposed via the RPC through [ResultBlockResults](5721a13ab1/rpc/coretypes/responses.go (L69)) changing. Should we somehow shim or notify for this change? 


closes: #7658
2022-03-04 22:32:37 +00:00

155 lines
4.2 KiB
Go

package client_test
import (
"bytes"
"context"
"log"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/abci/example/kvstore"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/tendermint/tendermint/rpc/coretypes"
rpctest "github.com/tendermint/tendermint/rpc/test"
)
func TestHTTPSimple(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start a tendermint node (and kvstore) in the background to test against
app := kvstore.NewApplication()
conf, err := rpctest.CreateConfig(t, "ExampleHTTP_simple")
require.NoError(t, err)
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
if err != nil {
log.Fatal(err) //nolint:gocritic
}
defer func() { _ = closer(ctx) }()
// Create our RPC client
rpcAddr := conf.RPC.ListenAddress
c, err := rpchttp.New(rpcAddr)
require.NoError(t, err)
// Create a transaction
k := []byte("name")
v := []byte("satoshi")
tx := append(k, append([]byte("="), v...)...)
// Broadcast the transaction and wait for it to commit (rather use
// c.BroadcastTxSync though in production).
bres, err := c.BroadcastTxCommit(ctx, tx)
require.NoError(t, err)
if err != nil {
log.Fatal(err)
}
if bres.CheckTx.IsErr() || bres.TxResult.IsErr() {
log.Fatal("BroadcastTxCommit transaction failed")
}
// Now try to fetch the value for the key
qres, err := c.ABCIQuery(ctx, "/key", k)
require.NoError(t, err)
require.False(t, qres.Response.IsErr(), "ABCIQuery failed")
require.True(t, bytes.Equal(qres.Response.Key, k),
"returned key does not match queried key")
require.True(t, bytes.Equal(qres.Response.Value, v),
"returned value does not match sent value [%s]", string(v))
assert.Equal(t, "name=satoshi", string(tx), "sent tx")
assert.Equal(t, "name", string(qres.Response.Key), "queried for")
assert.Equal(t, "satoshi", string(qres.Response.Value), "got value")
}
func TestHTTPBatching(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start a tendermint node (and kvstore) in the background to test against
app := kvstore.NewApplication()
conf, err := rpctest.CreateConfig(t, "ExampleHTTP_batching")
require.NoError(t, err)
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
if err != nil {
log.Fatal(err) //nolint:gocritic
}
defer func() { _ = closer(ctx) }()
rpcAddr := conf.RPC.ListenAddress
c, err := rpchttp.NewWithClient(rpcAddr, http.DefaultClient)
require.NoError(t, err)
// Create our two transactions
k1 := []byte("firstName")
v1 := []byte("satoshi")
tx1 := append(k1, append([]byte("="), v1...)...)
k2 := []byte("lastName")
v2 := []byte("nakamoto")
tx2 := append(k2, append([]byte("="), v2...)...)
txs := [][]byte{tx1, tx2}
// Create a new batch
batch := c.NewBatch()
// Queue up our transactions
for _, tx := range txs {
// Broadcast the transaction and wait for it to commit (rather use
// c.BroadcastTxSync though in production).
_, err := batch.BroadcastTxSync(ctx, tx)
require.NoError(t, err)
}
// Send the batch of 2 transactions
_, err = batch.Send(ctx)
require.NoError(t, err)
// wait for the transaction to land, we could poll more for
// the transactions to land definitively.
require.Eventually(t,
func() bool {
// Now let's query for the original results as a batch
exists := 0
for _, key := range [][]byte{k1, k2} {
_, err := batch.ABCIQuery(ctx, "/key", key)
if err == nil {
exists++
}
}
return exists == 2
},
10*time.Second,
time.Second,
)
// Send the 2 queries and keep the results
results, err := batch.Send(ctx)
require.NoError(t, err)
require.Len(t, results, 2)
// Each result in the returned list is the deserialized result of each
// respective ABCIQuery response
for _, result := range results {
qr, ok := result.(*coretypes.ResultABCIQuery)
require.True(t, ok, "invalid result type from ABCIQuery request")
switch string(qr.Response.Key) {
case "firstName":
require.Equal(t, "satoshi", string(qr.Response.Value))
case "lastName":
require.Equal(t, "nakamoto", string(qr.Response.Value))
default:
t.Fatalf("encountered unknown key %q", string(qr.Response.Key))
}
}
}