github.com/Oyster-zx/tendermint@v0.34.24-fork/test/e2e/tests/app_test.go (about)

     1  package e2e_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"math/rand"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	e2e "github.com/tendermint/tendermint/test/e2e/pkg"
    14  	"github.com/tendermint/tendermint/types"
    15  )
    16  
    17  // Tests that any initial state given in genesis has made it into the app.
    18  func TestApp_InitialState(t *testing.T) {
    19  	testNode(t, func(t *testing.T, node e2e.Node) {
    20  		if len(node.Testnet.InitialState) == 0 {
    21  			return
    22  		}
    23  
    24  		client, err := node.Client()
    25  		require.NoError(t, err)
    26  		for k, v := range node.Testnet.InitialState {
    27  			resp, err := client.ABCIQuery(ctx, "", []byte(k))
    28  			require.NoError(t, err)
    29  			assert.Equal(t, k, string(resp.Response.Key))
    30  			assert.Equal(t, v, string(resp.Response.Value))
    31  		}
    32  	})
    33  }
    34  
    35  // Tests that the app hash (as reported by the app) matches the last
    36  // block and the node sync status.
    37  func TestApp_Hash(t *testing.T) {
    38  	testNode(t, func(t *testing.T, node e2e.Node) {
    39  		client, err := node.Client()
    40  		require.NoError(t, err)
    41  		info, err := client.ABCIInfo(ctx)
    42  		require.NoError(t, err)
    43  		require.NotEmpty(t, info.Response.LastBlockAppHash, "expected app to return app hash")
    44  
    45  		block, err := client.Block(ctx, nil)
    46  		require.NoError(t, err)
    47  		require.EqualValues(t, info.Response.LastBlockAppHash, block.Block.AppHash,
    48  			"app hash does not match last block's app hash")
    49  
    50  		status, err := client.Status(ctx)
    51  		require.NoError(t, err)
    52  		require.EqualValues(t, info.Response.LastBlockAppHash, status.SyncInfo.LatestAppHash,
    53  			"app hash does not match node status")
    54  	})
    55  }
    56  
    57  // Tests that we can set a value and retrieve it.
    58  func TestApp_Tx(t *testing.T) {
    59  	testNode(t, func(t *testing.T, node e2e.Node) {
    60  		client, err := node.Client()
    61  		require.NoError(t, err)
    62  
    63  		// Generate a random value, to prevent duplicate tx errors when
    64  		// manually running the test multiple times for a testnet.
    65  		r := rand.New(rand.NewSource(time.Now().UnixNano()))
    66  		bz := make([]byte, 32)
    67  		_, err = r.Read(bz)
    68  		require.NoError(t, err)
    69  
    70  		key := fmt.Sprintf("testapp-tx-%v", node.Name)
    71  		value := fmt.Sprintf("%x", bz)
    72  		tx := types.Tx(fmt.Sprintf("%v=%v", key, value))
    73  
    74  		_, err = client.BroadcastTxSync(ctx, tx)
    75  		require.NoError(t, err)
    76  
    77  		hash := tx.Hash()
    78  		waitTime := 30 * time.Second
    79  		require.Eventuallyf(t, func() bool {
    80  			txResp, err := client.Tx(ctx, hash, false)
    81  			return err == nil && bytes.Equal(txResp.Tx, tx)
    82  		}, waitTime, time.Second,
    83  			"submitted tx wasn't committed after %v", waitTime,
    84  		)
    85  
    86  		// NOTE: we don't test abci query of the light client
    87  		if node.Mode == e2e.ModeLight {
    88  			return
    89  		}
    90  
    91  		abciResp, err := client.ABCIQuery(ctx, "", []byte(key))
    92  		require.NoError(t, err)
    93  		assert.Equal(t, key, string(abciResp.Response.Key))
    94  		assert.Equal(t, value, string(abciResp.Response.Value))
    95  
    96  	})
    97  }