github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/tendermint/lite/proxy/query_test.go (about) 1 package proxy 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/fibonacci-chain/fbc/libs/tendermint/abci/example/kvstore" 13 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/merkle" 14 "github.com/fibonacci-chain/fbc/libs/tendermint/lite" 15 certclient "github.com/fibonacci-chain/fbc/libs/tendermint/lite/client" 16 nm "github.com/fibonacci-chain/fbc/libs/tendermint/node" 17 "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/client" 18 rpclocal "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/client/local" 19 rpctest "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/test" 20 "github.com/fibonacci-chain/fbc/libs/tendermint/types" 21 ) 22 23 var node *nm.Node 24 var chainID = "tendermint_test" // TODO use from config. 25 //nolint:unused 26 var waitForEventTimeout = 5 * time.Second 27 28 // TODO fix tests!! 29 30 func TestMain(m *testing.M) { 31 app := kvstore.NewApplication() 32 node = rpctest.StartTendermint(app) 33 34 code := m.Run() 35 36 rpctest.StopTendermint(node) 37 os.Exit(code) 38 } 39 40 func kvstoreTx(k, v []byte) []byte { 41 return []byte(fmt.Sprintf("%s=%s", k, v)) 42 } 43 44 // TODO: enable it after general proof format has been adapted 45 // in abci/examples/kvstore.go 46 // 47 //nolint:unused,deadcode 48 func _TestAppProofs(t *testing.T) { 49 assert, require := assert.New(t), require.New(t) 50 51 prt := defaultProofRuntime() 52 cl := rpclocal.New(node) 53 client.WaitForHeight(cl, 1, nil) 54 55 // This sets up our trust on the node based on some past point. 56 source := certclient.NewProvider(chainID, cl) 57 seed, err := source.LatestFullCommit(chainID, 1, 1) 58 require.NoError(err, "%#v", err) 59 cert := lite.NewBaseVerifier(chainID, seed.Height(), seed.Validators) 60 61 // Wait for tx confirmation. 62 done := make(chan int64) 63 go func() { 64 evtTyp := types.EventTx 65 _, err = client.WaitForOneEvent(cl, evtTyp, waitForEventTimeout) 66 require.Nil(err, "%#v", err) 67 close(done) 68 }() 69 70 // Submit a transaction. 71 k := []byte("my-key") 72 v := []byte("my-value") 73 tx := kvstoreTx(k, v) 74 br, err := cl.BroadcastTxCommit(tx) 75 require.NoError(err, "%#v", err) 76 require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx) 77 require.EqualValues(0, br.DeliverTx.Code) 78 brh := br.Height 79 80 // Fetch latest after tx commit. 81 <-done 82 latest, err := source.LatestFullCommit(chainID, 1, 1<<63-1) 83 require.NoError(err, "%#v", err) 84 rootHash := latest.SignedHeader.AppHash 85 if rootHash == nil { 86 // Fetch one block later, AppHash hasn't been committed yet. 87 // TODO find a way to avoid doing this. 88 client.WaitForHeight(cl, latest.SignedHeader.Height+1, nil) 89 latest, err = source.LatestFullCommit(chainID, latest.SignedHeader.Height+1, 1<<63-1) 90 require.NoError(err, "%#v", err) 91 rootHash = latest.SignedHeader.AppHash 92 } 93 require.NotNil(rootHash) 94 95 // verify a query before the tx block has no data (and valid non-exist proof) 96 bs, height, proof, err := GetWithProof(prt, k, brh-1, cl, cert) 97 require.NoError(err, "%#v", err) 98 require.NotNil(proof) 99 require.Equal(height, brh-1) 100 // require.NotNil(proof) 101 // TODO: Ensure that *some* keys will be there, ensuring that proof is nil, 102 // (currently there's a race condition) 103 // and ensure that proof proves absence of k. 104 require.Nil(bs) 105 106 // but given that block it is good 107 bs, height, proof, err = GetWithProof(prt, k, brh, cl, cert) 108 require.NoError(err, "%#v", err) 109 require.NotNil(proof) 110 require.Equal(height, brh) 111 112 assert.EqualValues(v, bs) 113 err = prt.VerifyValue(proof, rootHash, string(k), bs) // XXX key encoding 114 assert.NoError(err, "%#v", err) 115 116 // Test non-existing key. 117 missing := []byte("my-missing-key") 118 bs, _, proof, err = GetWithProof(prt, missing, 0, cl, cert) 119 require.NoError(err) 120 require.Nil(bs) 121 require.NotNil(proof) 122 err = prt.VerifyAbsence(proof, rootHash, string(missing)) // XXX VerifyAbsence(), keyencoding 123 assert.NoError(err, "%#v", err) 124 err = prt.VerifyAbsence(proof, rootHash, string(k)) // XXX VerifyAbsence(), keyencoding 125 assert.Error(err, "%#v", err) 126 } 127 128 func TestTxProofs(t *testing.T) { 129 assert, require := assert.New(t), require.New(t) 130 131 cl := rpclocal.New(node) 132 client.WaitForHeight(cl, 1, nil) 133 134 tx := kvstoreTx([]byte("key-a"), []byte("value-a")) 135 br, err := cl.BroadcastTxCommit(tx) 136 require.NoError(err, "%#v", err) 137 require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx) 138 require.EqualValues(0, br.DeliverTx.Code) 139 brh := br.Height 140 141 source := certclient.NewProvider(chainID, cl) 142 seed, err := source.LatestFullCommit(chainID, brh-2, brh-2) 143 require.NoError(err, "%#v", err) 144 cert := lite.NewBaseVerifier(chainID, seed.Height(), seed.Validators) 145 146 time.Sleep(1 * time.Second) 147 // First let's make sure a bogus transaction hash returns a valid non-existence proof. 148 key := types.Tx([]byte("bogus")).Hash(brh) 149 _, err = cl.Tx(key, true) 150 require.NotNil(err) 151 require.Contains(err.Error(), "not found") 152 153 // Now let's check with the real tx root hash. 154 key = types.Tx(tx).Hash(brh) 155 res, err := cl.Tx(key, true) 156 require.NoError(err, "%#v", err) 157 require.NotNil(res) 158 keyHash := merkle.SimpleHashFromByteSlices([][]byte{key}) 159 err = res.Proof.Validate(keyHash, brh) 160 assert.NoError(err, "%#v", err) 161 162 commit, err := GetCertifiedCommit(br.Height, cl, cert) 163 require.Nil(err, "%#v", err) 164 require.Equal(res.Proof.RootHash, commit.Header.DataHash) 165 }