Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ func TestInitState(t *testing.T) {
assert.Equal(unsortCoins, coins)

err = app.InitState("base", "dslfkgjdas", "")
require.NotNil(err)
require.Error(err)

err = app.InitState("", "dslfkgjdas", "")
require.NotNil(err)
require.Error(err)

err = app.InitState("dslfkgjdas", "szfdjzs", "")
require.NotNil(err)
require.Error(err)
}

// Test CheckTx and DeliverTx with insufficient and sufficient balance
Expand Down
32 changes: 15 additions & 17 deletions app/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
sm "github.com/cosmos/cosmos-sdk/state"
)

// DefaultHistorySize is how many blocks of history to store for ABCI queries
const DefaultHistorySize = 10

// StoreApp contains a data store and all info needed
// to perform queries and handshakes.
//
Expand All @@ -41,7 +44,7 @@ type StoreApp struct {

// NewStoreApp creates a data store to handle queries
func NewStoreApp(appName, dbName string, cacheSize int, logger log.Logger) (*StoreApp, error) {
state, err := loadState(dbName, cacheSize)
state, err := loadState(dbName, cacheSize, DefaultHistorySize)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,11 +146,12 @@ func (app *StoreApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQu
// we must retrun most recent, even if apphash
// is not yet in the blockchain

// if tree.Tree.VersionExists(app.height - 1) {
// height = app.height - 1
// } else {
height = app.CommittedHeight()
// }
withProof := app.CommittedHeight() - 1
if tree.Tree.VersionExists(withProof) {
height = withProof
} else {
height = app.CommittedHeight()
}
}
resQuery.Height = height

Expand Down Expand Up @@ -234,11 +238,11 @@ func pubKeyIndex(val *abci.Validator, list []*abci.Validator) int {
return -1
}

func loadState(dbName string, cacheSize int) (*sm.State, error) {
func loadState(dbName string, cacheSize int, historySize uint64) (*sm.State, error) {
// memory backed case, just for testing
if dbName == "" {
tree := iavl.NewVersionedTree(0, dbm.NewMemDB())
return sm.NewState(tree), nil
return sm.NewState(tree, historySize), nil
}

// Expand the path fully
Expand All @@ -254,18 +258,12 @@ func loadState(dbName string, cacheSize int) (*sm.State, error) {
dir := path.Dir(dbPath)
name := path.Base(dbPath)

// Make sure the path exists
empty, _ := cmn.IsDirEmpty(dbPath + ".db")

// Open database called "dir/name.db", if it doesn't exist it will be created
db := dbm.NewDB(name, dbm.LevelDBBackendStr, dir)
tree := iavl.NewVersionedTree(cacheSize, db)

if !empty {
if err = tree.Load(); err != nil {
return nil, errors.ErrInternal("Loading tree: " + err.Error())
}
if err = tree.Load(); err != nil {
return nil, errors.ErrInternal("Loading tree: " + err.Error())
}

return sm.NewState(tree), nil
return sm.NewState(tree, historySize), nil
}
20 changes: 13 additions & 7 deletions client/commands/query/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tendermint/go-wire/data"
"github.com/tendermint/iavl"
"github.com/tendermint/light-client/proofs"
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands"
Expand All @@ -24,8 +25,8 @@ import (
// It will try to get the proof for the given key. If it is successful,
// it will return the height and also unserialize proof.Data into the data
// argument (so pass in a pointer to the appropriate struct)
func GetParsed(key []byte, data interface{}, prove bool) (uint64, error) {
bs, h, err := Get(key, prove)
func GetParsed(key []byte, data interface{}, height int, prove bool) (uint64, error) {
bs, h, err := Get(key, height, prove)
if err != nil {
return 0, err
}
Expand All @@ -44,26 +45,31 @@ func GetParsed(key []byte, data interface{}, prove bool) (uint64, error) {
// we just repeat whatever any (potentially malicious) node gives us.
// Only use that if you are running the full node yourself,
// and it is localhost or you have a secure connection (not HTTP)
func Get(key []byte, prove bool) (data.Bytes, uint64, error) {
func Get(key []byte, height int, prove bool) (data.Bytes, uint64, error) {
if height < 0 {
return nil, 0, fmt.Errorf("Height cannot be negative")
}

if !prove {
node := commands.GetNode()
resp, err := node.ABCIQuery("/key", key, false)
resp, err := node.ABCIQueryWithOptions("/key", key,
rpcclient.ABCIQueryOptions{Trusted: true, Height: uint64(height)})
return data.Bytes(resp.Value), resp.Height, err
}
val, h, _, err := GetWithProof(key)
val, h, _, err := GetWithProof(key, height)
return val, h, err
}

// GetWithProof returns the values stored under a given key at the named
// height as in Get. Additionally, it will return a validated merkle
// proof for the key-value pair if it exists, and all checks pass.
func GetWithProof(key []byte) (data.Bytes, uint64, iavl.KeyProof, error) {
func GetWithProof(key []byte, height int) (data.Bytes, uint64, iavl.KeyProof, error) {
node := commands.GetNode()
cert, err := commands.GetCertifier()
if err != nil {
return nil, 0, nil, err
}
return client.GetWithProof(key, node, cert)
return client.GetWithProof(key, height, node, cert)
}

// ParseHexKey parses the key flag as hex and converts to bytes or returns error
Expand Down
2 changes: 1 addition & 1 deletion client/commands/query/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func keyQueryCmd(cmd *cobra.Command, args []string) error {
}
prove := !viper.GetBool(commands.FlagTrustNode)

val, h, err := Get(key, prove)
val, h, err := Get(key, GetHeight(), prove)
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ import (
// If there is any error in checking, returns an error.
// If val is non-empty, proof should be KeyExistsProof
// If val is empty, proof should be KeyMissingProof
func GetWithProof(key []byte, node client.Client, cert certifiers.Certifier) (
func GetWithProof(key []byte, reqHeight int, node client.Client,
cert certifiers.Certifier) (
val data.Bytes, height uint64, proof iavl.KeyProof, err error) {

resp, err := node.ABCIQuery("/key", key, true)
if reqHeight < 0 {
err = errors.Errorf("Height cannot be negative")
return
}

resp, err := node.ABCIQueryWithOptions("/key", key,
client.ABCIQueryOptions{Height: uint64(reqHeight)})
if err != nil {
return
}
Expand Down
12 changes: 10 additions & 2 deletions client/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestAppProofs(t *testing.T) {
require.NoError(err, "%+v", err)
require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
require.EqualValues(0, br.DeliverTx.Code)
brh := br.Height

// This sets up our trust on the node based on some past point.
source := certclient.New(cl)
Expand All @@ -71,7 +72,14 @@ func TestAppProofs(t *testing.T) {
// Test existing key.
var data eyes.Data

bs, height, proof, err := GetWithProof(k, cl, cert)
// verify a query before the tx block has no data (and valid non-exist proof)
bs, height, proof, err := GetWithProof(k, brh-1, cl, cert)
require.NotNil(err)
require.True(lc.IsNoDataErr(err))
require.Nil(bs)

// but given that block it is good
bs, height, proof, err = GetWithProof(k, brh, cl, cert)
require.NoError(err, "%+v", err)
require.NotNil(proof)
require.True(height >= uint64(latest.Header.Height))
Expand All @@ -89,7 +97,7 @@ func TestAppProofs(t *testing.T) {

// Test non-existing key.
missing := []byte("my-missing-key")
bs, _, proof, err = GetWithProof(missing, cl, cert)
bs, _, proof, err = GetWithProof(missing, 0, cl, cert)
require.True(lc.IsNoDataErr(err))
require.Nil(bs)
require.NotNil(proof)
Expand Down
22 changes: 11 additions & 11 deletions examples/basecoin/tests/cli/basictx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ test01SendTx() {
HASH=$(echo $TX | jq .hash | tr -d \")
TX_HEIGHT=$(echo $TX | jq .height)

checkAccount $SENDER "9007199254740000"
checkAccount $SENDER "9007199254740000" "$TX_HEIGHT"
# make sure 0x prefix also works
checkAccount "0x$SENDER" "9007199254740000"
checkAccount $RECV "992"
checkAccount "0x$SENDER" "9007199254740000" "$TX_HEIGHT"
checkAccount $RECV "992" "$TX_HEIGHT"

# Make sure tx is indexed
checkSendTx $HASH $TX_HEIGHT $SENDER "992"
Expand All @@ -60,8 +60,8 @@ test02SendTxWithFee() {
TX_HEIGHT=$(echo $TX | jq .height)

# deduct 100 from sender, add 90 to receiver... fees "vanish"
checkAccount $SENDER "9007199254739900"
checkAccount $RECV "1082"
checkAccount $SENDER "9007199254739900" "$TX_HEIGHT"
checkAccount $RECV "1082" "$TX_HEIGHT"

# Make sure tx is indexed
checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10"
Expand All @@ -71,8 +71,8 @@ test02SendTxWithFee() {
assertFalse "line=${LINENO}, replay: $TX" $?

# checking normally
checkAccount $SENDER "9007199254739900"
checkAccount $RECV "1082"
checkAccount $SENDER "9007199254739900" "$TX_HEIGHT"
checkAccount $RECV "1082" "$TX_HEIGHT"

# make sure we can query the proper nonce
NONCE=$(${CLIENT_EXE} query nonce $SENDER)
Expand All @@ -89,8 +89,8 @@ test02SendTxWithFee() {
export BC_TRUST_NODE=1
export BC_NODE=localhost:46657
checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10"
checkAccount $SENDER "9007199254739900"
checkAccount $RECV "1082"
checkAccount $SENDER "9007199254739900" "$TX_HEIGHT"
checkAccount $RECV "1082" "$TX_HEIGHT"
unset BC_TRUST_NODE
unset BC_NODE
export BC_HOME=$OLD_BC_HOME
Expand All @@ -109,8 +109,8 @@ test03CreditTx() {
TX_HEIGHT=$(echo $TX | jq .height)

# receiver got cash, sender didn't lose any (1000 more than last check)
checkAccount $RECV "2082"
checkAccount $SENDER "9007199254739900"
checkAccount $RECV "2082" "$TX_HEIGHT"
checkAccount $SENDER "9007199254739900" "$TX_HEIGHT"
}


Expand Down
Loading