From 52bf24c3ba98a1058a2da86f76908183ae08b014 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 30 Oct 2018 09:50:52 -0700 Subject: [PATCH 1/2] first commit...; tests compile --- Gopkg.lock | 66 ++++------ Gopkg.toml | 8 +- baseapp/baseapp.go | 2 +- baseapp/baseapp_test.go | 16 +-- client/context/context.go | 3 +- client/context/query.go | 59 +++++---- client/lcd/lcd_test.go | 4 +- client/tx/query.go | 2 +- client/utils/utils.go | 2 +- client/utils/utils_test.go | 4 +- cmd/gaia/app/app.go | 2 +- cmd/gaia/cmd/gaiadebug/main.go | 2 +- crypto/keys/keybase.go | 2 +- crypto/keys/types.go | 4 +- docs/sdk/core/app2.md | 2 +- docs/sdk/core/examples/app2.go | 2 +- docs/sdk/core/examples/app2_test.go | 4 +- examples/democoin/x/oracle/keeper.go | 4 +- examples/democoin/x/oracle/keeper_keys.go | 4 +- examples/democoin/x/oracle/oracle_test.go | 4 +- examples/democoin/x/simplestake/keeper.go | 4 +- server/start.go | 21 ++- store/iavlstore.go | 14 +- store/iavlstore_test.go | 8 +- store/list.go | 10 +- store/multistoreproof.go | 145 ++++++++++++--------- store/multistoreproof_test.go | 149 ++++++---------------- store/queue.go | 4 +- store/queue_test.go | 6 +- store/rootmultistore.go | 19 +-- store/rootmultistore_test.go | 4 +- types/decimal_test.go | 8 +- x/auth/account_test.go | 6 +- x/auth/client/txbuilder/txbuilder.go | 4 +- x/auth/feekeeper.go | 4 +- x/auth/mapper.go | 4 +- x/auth/stdtx.go | 2 +- x/bank/client/cli/broadcast.go | 2 +- x/bank/client/rest/broadcast.go | 2 +- x/distribution/keeper/delegation.go | 4 +- x/distribution/keeper/genesis.go | 4 +- x/distribution/keeper/keeper.go | 8 +- x/distribution/keeper/test_common.go | 2 +- x/distribution/keeper/validator.go | 4 +- x/gov/client/rest/rest.go | 2 +- x/gov/keeper.go | 30 ++--- x/gov/keeper_test.go | 8 +- x/gov/queryable.go | 4 +- x/gov/tally.go | 2 +- x/ibc/client/cli/relay.go | 6 +- x/ibc/mapper.go | 8 +- x/mint/keeper.go | 4 +- x/slashing/client/cli/query.go | 2 +- x/slashing/client/rest/query.go | 2 +- x/slashing/keeper.go | 4 +- x/slashing/signing_info.go | 8 +- x/slashing/slashing_period.go | 4 +- x/stake/handler.go | 4 +- x/stake/handler_test.go | 18 +-- x/stake/keeper/delegation.go | 12 +- x/stake/keeper/keeper.go | 16 +-- x/stake/keeper/val_state_change.go | 2 +- x/stake/keeper/validator.go | 8 +- x/stake/types/delegation.go | 20 +-- x/stake/types/params.go | 6 +- x/stake/types/pool.go | 6 +- x/stake/types/validator.go | 4 +- 67 files changed, 400 insertions(+), 414 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 68350442d84d..fa6648168313 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -10,6 +10,7 @@ revision = "48b08affede2cea076a3cf13b2e3f72ed262b743" [[projects]] + branch = "master" digest = "1:09a7f74eb6bb3c0f14d8926610c87f569c5cff68e978d30e9a3540aeb626fdf0" name = "github.com/bartekn/go-bip39" packages = ["."] @@ -38,7 +39,7 @@ name = "github.com/btcsuite/btcd" packages = ["btcec"] pruneopts = "UT" - revision = "2a560b2036bee5e3679ec2133eb6520b2f195213" + revision = "67e573d211ace594f1366b4ce9d39726c4b19bd0" [[projects]] digest = "1:386de157f7d19259a7f9c81f26ce011223ce0f090353c1152ffdf730d7d10ac2" @@ -245,12 +246,12 @@ version = "v1.0.0" [[projects]] - digest = "1:e32dfc6abff6a3633ef4d9a1022fd707c8ef26f1e1e8f855dc58dc415ce7c8f3" + digest = "1:53bc4cd4914cd7cd52139990d5170d6dc99067ae31c56530621b18b35fc30318" name = "github.com/mitchellh/mapstructure" packages = ["."] pruneopts = "UT" - revision = "fe40af7a9c397fa3ddba203c38a5042c5d0475ad" - version = "v1.1.1" + revision = "3536a929edddb9a5b34bd6861dc4a9647cb459fe" + version = "v1.1.2" [[projects]] digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e" @@ -296,7 +297,7 @@ [[projects]] branch = "master" - digest = "1:63b68062b8968092eb86bedc4e68894bd096ea6b24920faca8b9dcf451f54bb5" + digest = "1:db712fde5d12d6cdbdf14b777f0c230f4ff5ab0be8e35b239fc319953ed577a4" name = "github.com/prometheus/common" packages = [ "expfmt", @@ -304,11 +305,11 @@ "model", ] pruneopts = "UT" - revision = "c7de2306084e37d54b8be01f3541a8464345e9a5" + revision = "7e9e6cabbd393fc208072eedef99188d0ce788b6" [[projects]] branch = "master" - digest = "1:ef1dd9945e58ee9b635273d28c0ef3fa3742a7dedc038ebe207fd63e6ce000ef" + digest = "1:ef74914912f99c79434d9c09658274678bc85080ebe3ab32bec3940ebce5e1fc" name = "github.com/prometheus/procfs" packages = [ ".", @@ -317,7 +318,7 @@ "xfs", ] pruneopts = "UT" - revision = "418d78d0b9a7b7de3a6bbc8a23def624cc977bb2" + revision = "185b4288413d2a0dd0806f78c90dde719829e5ae" [[projects]] digest = "1:ea0700160aca4ef099f4e06686a665a87691f4248dddd40796925eda2e46bd64" @@ -346,12 +347,12 @@ version = "v1.1.2" [[projects]] - digest = "1:516e71bed754268937f57d4ecb190e01958452336fa73dbac880894164e91c1f" + digest = "1:08d65904057412fc0270fc4812a1c90c594186819243160dc779a402d4b6d0bc" name = "github.com/spf13/cast" packages = ["."] pruneopts = "UT" - revision = "8965335b8c7107321228e3e3702cab9832751bac" - version = "v1.2.0" + revision = "8c9545af88b134710ab1cd196795e7f2388358d7" + version = "v1.3.0" [[projects]] digest = "1:7ffc0983035bc7e297da3688d9fe19d60a420e9c38bef23f845c53788ed6a05e" @@ -370,12 +371,12 @@ version = "v1.0.0" [[projects]] - digest = "1:dab83a1bbc7ad3d7a6ba1a1cc1760f25ac38cdf7d96a5cdd55cd915a4f5ceaf9" + digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2" name = "github.com/spf13/pflag" packages = ["."] pruneopts = "UT" - revision = "9a97c102cda95a86cec2345a6f09f55a939babf5" - version = "v1.0.2" + revision = "298182f68c66c05229eb03ac171abe6e309ee79a" + version = "v1.0.3" [[projects]] digest = "1:f8e1a678a2571e265f4bf91a3e5e32aa6b1474a55cb0ea849750cc177b664d96" @@ -424,35 +425,23 @@ revision = "e5840949ff4fff0c56f9b6a541e22b63581ea9df" [[projects]] - branch = "master" - digest = "1:087aaa7920e5d0bf79586feb57ce01c35c830396ab4392798112e8aae8c47722" - name = "github.com/tendermint/ed25519" - packages = [ - ".", - "edwards25519", - "extra25519", - ] - pruneopts = "UT" - revision = "d8387025d2b9d158cf4efb07e7ebf814bcce2057" - -[[projects]] - digest = "1:2c971a45c89ca2ccc735af50919cdee05fbdc54d4bf50625073693300e31ead8" + digest = "1:10b3a599325740c84a7c81f3f3cb2e1fdb70b3ea01b7fa28495567a2519df431" name = "github.com/tendermint/go-amino" packages = ["."] pruneopts = "UT" - revision = "faa6e731944e2b7b6a46ad202902851e8ce85bee" - version = "v0.12.0" + revision = "6dcc6ddc143e116455c94b25c1004c99e0d0ca12" + version = "v0.14.0" [[projects]] - digest = "1:53397098d6acb7613358683cc84ae59281a60c6033f0bff62fa8d3f279c6c430" + digest = "1:9f8c4c93658315a795ffd3e0c943d39f78067dd8382b8d7bcfaf6686b92f3978" name = "github.com/tendermint/iavl" packages = ["."] pruneopts = "UT" - revision = "3acc91fb8811db2c5409a855ae1f8e441fe98e2d" - version = "v0.11.0" + revision = "fa74114f764f9827c4ad5573f990ed25bf8c4bac" + version = "v0.11.1" [[projects]] - digest = "1:f9c7a1f3ee087476f4883c33cc7c1bdbe56b9670b2fb27855ea2f386393272f5" + digest = "1:6ebe52fa977e5be2afdb8c8303e343b559bec3426b185bacf6d9b371cc1bcc2b" name = "github.com/tendermint/tendermint" packages = [ "abci/client", @@ -505,7 +494,6 @@ "rpc/core", "rpc/core/types", "rpc/grpc", - "rpc/lib", "rpc/lib/client", "rpc/lib/server", "rpc/lib/types", @@ -518,8 +506,8 @@ "version", ] pruneopts = "UT" - revision = "90eda9bfb6e6daeed1c8015df41cb36772d91778" - version = "v0.25.1-rc0" + revision = "bc0d672b26dbbd6858e97a570c87e3c20cbf199e" + version = "v0.26.0-rc0" [[projects]] digest = "1:7886f86064faff6f8d08a3eb0e8c773648ff5a2e27730831e2bfbf07467f6666" @@ -530,13 +518,15 @@ version = "v0.1.0" [[projects]] - digest = "1:aaff04fa01d9b824fde6799759cc597b3ac3671b9ad31924c28b6557d0ee5284" + digest = "1:6f6dc6060c4e9ba73cf28aa88f12a69a030d3d19d518ef8e931879eaa099628d" name = "golang.org/x/crypto" packages = [ "bcrypt", "blowfish", "chacha20poly1305", "curve25519", + "ed25519", + "ed25519/internal/edwards25519", "hkdf", "internal/chacha20", "internal/subtle", @@ -551,7 +541,7 @@ ] pruneopts = "UT" revision = "3764759f34a542a3aef74d6b02e35be7ab893bba" - source = "https://github.com/tendermint/crypto" + source = "github.com/tendermint/crypto" [[projects]] digest = "1:d36f55a999540d29b6ea3c2ea29d71c76b1d9853fdcd3e5c5cb4836f2ba118f1" diff --git a/Gopkg.toml b/Gopkg.toml index 0902bc412607..5ba48e2793b8 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -49,21 +49,21 @@ [[override]] name = "github.com/tendermint/go-amino" - version = "=v0.12.0" + version = "v0.14.0" [[override]] name = "github.com/tendermint/iavl" - version = "=v0.11.0" + version = "=v0.11.1" [[override]] name = "github.com/tendermint/tendermint" - version = "=0.25.1-rc0" + version = "=v0.26.0-rc0" ## deps without releases: [[override]] name = "golang.org/x/crypto" - source = "https://github.com/tendermint/crypto" + source = "github.com/tendermint/crypto" revision = "3764759f34a542a3aef74d6b02e35be7ab893bba" [[constraint]] diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 17942b976aa9..a2b2f4166757 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -337,7 +337,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc } // Encode with json - value := codec.Cdc.MustMarshalBinary(result) + value := codec.Cdc.MustMarshalBinaryLengthPrefixed(result) return abci.ResponseQuery{ Code: uint32(sdk.ABCICodeOK), Value: value, diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index ff6cbb83418f..9f6414214bbb 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -358,7 +358,7 @@ func testTxDecoder(cdc *codec.Codec) sdk.TxDecoder { if len(txBytes) == 0 { return nil, sdk.ErrTxDecode("txBytes are empty") } - err := cdc.UnmarshalBinary(txBytes, &tx) + err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, sdk.ErrTxDecode("").TraceSDK(err.Error()) } @@ -455,7 +455,7 @@ func TestCheckTx(t *testing.T) { for i := int64(0); i < nTxs; i++ { tx := newTxCounter(i, 0) - txBytes, err := codec.MarshalBinary(tx) + txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) r := app.CheckTx(txBytes) assert.True(t, r.IsOK(), fmt.Sprintf("%v", r)) @@ -503,7 +503,7 @@ func TestDeliverTx(t *testing.T) { for i := 0; i < txPerHeight; i++ { counter := int64(blockN*txPerHeight + i) tx := newTxCounter(counter, counter) - txBytes, err := codec.MarshalBinary(tx) + txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) res := app.DeliverTx(txBytes) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) @@ -544,7 +544,7 @@ func TestMultiMsgDeliverTx(t *testing.T) { { app.BeginBlock(abci.RequestBeginBlock{}) tx := newTxCounter(0, 0, 1, 2) - txBytes, err := codec.MarshalBinary(tx) + txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) res := app.DeliverTx(txBytes) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) @@ -565,7 +565,7 @@ func TestMultiMsgDeliverTx(t *testing.T) { tx := newTxCounter(1, 3) tx.Msgs = append(tx.Msgs, msgCounter2{0}) tx.Msgs = append(tx.Msgs, msgCounter2{1}) - txBytes, err := codec.MarshalBinary(tx) + txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) res := app.DeliverTx(txBytes) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) @@ -638,7 +638,7 @@ func TestSimulateTx(t *testing.T) { require.Equal(t, gasConsumed, result.GasUsed) // simulate by calling Query with encoded tx - txBytes, err := cdc.MarshalBinary(tx) + txBytes, err := cdc.MarshalBinaryLengthPrefixed(tx) require.Nil(t, err) query := abci.RequestQuery{ Path: "/app/simulate", @@ -648,7 +648,7 @@ func TestSimulateTx(t *testing.T) { require.True(t, queryResult.IsOK(), queryResult.Log) var res sdk.Result - codec.Cdc.MustUnmarshalBinary(queryResult.Value, &res) + codec.Cdc.MustUnmarshalBinaryLengthPrefixed(queryResult.Value, &res) require.Nil(t, err, "Result unmarshalling failed") require.True(t, res.IsOK(), res.Log) require.Equal(t, gasConsumed, res.GasUsed, res.Log) @@ -729,7 +729,7 @@ func TestRunInvalidTransaction(t *testing.T) { registerTestCodec(newCdc) newCdc.RegisterConcrete(&msgNoDecode{}, "cosmos-sdk/baseapp/msgNoDecode", nil) - txBytes, err := newCdc.MarshalBinary(tx) + txBytes, err := newCdc.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) res := app.DeliverTx(txBytes) require.EqualValues(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeTxDecode), res.Code) diff --git a/client/context/context.go b/client/context/context.go index ce36c37a8a9d..5c1142218f84 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -122,7 +122,8 @@ func createVerifier() tmlite.Verifier { os.Exit(1) } node := rpcclient.NewHTTP(nodeURI, "/websocket") - verifier, err := tmliteProxy.NewVerifier(chainID, filepath.Join(home, ".gaialite"), node, log.NewNopLogger()) + cacheSize := 10 // TODO What's a good value? + verifier, err := tmliteProxy.NewVerifier(chainID, filepath.Join(home, ".gaialite"), node, log.NewNopLogger(), cacheSize) if err != nil { fmt.Printf("Create verifier failed: %s\n", err.Error()) diff --git a/client/context/query.go b/client/context/query.go index 8fca9becff07..f1f9dd26ed77 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -10,9 +10,9 @@ import ( "strings" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" abci "github.com/tendermint/tendermint/abci/types" + merkle "github.com/tendermint/tendermint/crypto/merkle" cmn "github.com/tendermint/tendermint/libs/common" tmliteErr "github.com/tendermint/tendermint/lite/errors" tmliteProxy "github.com/tendermint/tendermint/lite/proxy" @@ -54,7 +54,7 @@ func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sd return res, err } - ctx.Codec.MustUnmarshalBinary(resRaw, &res) + ctx.Codec.MustUnmarshalBinaryLengthPrefixed(resRaw, &res) return } @@ -157,8 +157,8 @@ func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, err erro } opts := rpcclient.ABCIQueryOptions{ - Height: ctx.Height, - Trusted: ctx.TrustNode, + Height: ctx.Height, + Prove: !ctx.TrustNode, } result, err := node.ABCIQueryWithOptions(path, key, opts) @@ -198,7 +198,7 @@ func (ctx CLIContext) Verify(height int64) (tmtypes.SignedHeader, error) { } // verifyProof perform response proof verification. -func (ctx CLIContext) verifyProof(_ string, resp abci.ResponseQuery) error { +func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) error { if ctx.Verifier == nil { return fmt.Errorf("missing valid certifier to verify data from distrusted node") } @@ -209,25 +209,20 @@ func (ctx CLIContext) verifyProof(_ string, resp abci.ResponseQuery) error { return err } - var multiStoreProof store.MultiStoreProof - cdc := codec.New() - - err = cdc.UnmarshalBinary(resp.Proof, &multiStoreProof) + // TODO Instead of reconstructing, stash on CLIContext field? + prt := store.DefaultProofRuntime() + // TODO Better convention for path? + storeName, err := parseQueryStorePath(queryPath) if err != nil { - return errors.Wrap(err, "failed to unmarshalBinary rangeProof") - } - - // verify the substore commit hash against trusted appHash - substoreCommitHash, err := store.VerifyMultiStoreCommitInfo( - multiStoreProof.StoreName, multiStoreProof.StoreInfos, commit.Header.AppHash, - ) - if err != nil { - return errors.Wrap(err, "failed in verifying the proof against appHash") + return err } + kp := new(merkle.KeyPath) + kp.AppendKey([]byte(storeName), merkle.KeyEncodingURL) + kp.AppendKey([]byte(resp.Key), merkle.KeyEncodingURL) - err = store.VerifyRangeProof(resp.Key, resp.Value, substoreCommitHash, &multiStoreProof.RangeProof) + err = prt.VerifyValue(resp.Proof, commit.Header.AppHash, kp.String(), resp.Value) if err != nil { - return errors.Wrap(err, "failed in the range proof verification") + return errors.Wrap(err, "failed to prove merkle.Proof") } return nil @@ -241,7 +236,7 @@ func (ctx CLIContext) queryStore(key cmn.HexBytes, storeName, endPath string) ([ } // isQueryStoreWithProof expects a format like /// -// queryType can be app or store. +// queryType must be "store" and subpath must be "key" to require a proof. func isQueryStoreWithProof(path string) bool { if !strings.HasPrefix(path, "/") { return false @@ -251,10 +246,30 @@ func isQueryStoreWithProof(path string) bool { if len(paths) != 3 { return false } - + if paths[0] != "store" { + return false + } if store.RequireProof("/" + paths[2]) { return true } return false } + +// parseQueryStorePath expects a format like /store//key. +func parseQueryStorePath(path string) (storeName string, err error) { + if !strings.HasPrefix(path, "/") { + return "", errors.New("expected path to start with /") + } + paths := strings.SplitN(path[1:], "/", 3) + if len(paths) != 3 { + return "", errors.New("expected format like /store//key") + } + if paths[0] != "store" { + return "", errors.New("expected format like /store//key") + } + if paths[2] != "key" { + return "", errors.New("expected format like /store//key") + } + return paths[1], nil +} diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index cb25721e891d..d3140e5b8d71 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -158,11 +158,11 @@ func TestNodeStatus(t *testing.T) { res, body := Request(t, port, "GET", "/node_info", nil) require.Equal(t, http.StatusOK, res.StatusCode, body) - var nodeInfo p2p.NodeInfo + var nodeInfo p2p.DefaultNodeInfo err := cdc.UnmarshalJSON([]byte(body), &nodeInfo) require.Nil(t, err, "Couldn't parse node info") - require.NotEqual(t, p2p.NodeInfo{}, nodeInfo, "res: %v", res) + require.NotEqual(t, p2p.DefaultNodeInfo{}, nodeInfo, "res: %v", res) // syncing res, body = Request(t, port, "GET", "/syncing", nil) diff --git a/client/tx/query.go b/client/tx/query.go index da94404f2d24..3bbbf9a13223 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -125,7 +125,7 @@ type Info struct { func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) { var tx auth.StdTx - err := cdc.UnmarshalBinary(txBytes, &tx) + err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, err } diff --git a/client/utils/utils.go b/client/utils/utils.go index f5cf04168c48..f6aae8ccb04f 100644 --- a/client/utils/utils.go +++ b/client/utils/utils.go @@ -166,7 +166,7 @@ func adjustGasEstimate(estimate int64, adjustment float64) int64 { func parseQueryResponse(cdc *amino.Codec, rawRes []byte) (int64, error) { var simulationResult sdk.Result - if err := cdc.UnmarshalBinary(rawRes, &simulationResult); err != nil { + if err := cdc.UnmarshalBinaryLengthPrefixed(rawRes, &simulationResult); err != nil { return 0, err } return simulationResult.GasUsed, nil diff --git a/client/utils/utils_test.go b/client/utils/utils_test.go index 731ded903ff0..c3bf315699ac 100644 --- a/client/utils/utils_test.go +++ b/client/utils/utils_test.go @@ -12,7 +12,7 @@ import ( func TestParseQueryResponse(t *testing.T) { cdc := app.MakeCodec() - sdkResBytes := cdc.MustMarshalBinary(sdk.Result{GasUsed: 10}) + sdkResBytes := cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: 10}) gas, err := parseQueryResponse(cdc, sdkResBytes) assert.Equal(t, gas, int64(10)) assert.Nil(t, err) @@ -28,7 +28,7 @@ func TestCalculateGas(t *testing.T) { if wantErr { return nil, errors.New("") } - return cdc.MustMarshalBinary(sdk.Result{GasUsed: gasUsed}), nil + return cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: gasUsed}), nil } } type args struct { diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 21ef855f026a..87972dc37cb3 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -259,7 +259,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci if err != nil { panic(err) } - bz := app.cdc.MustMarshalBinary(tx) + bz := app.cdc.MustMarshalBinaryLengthPrefixed(tx) res := app.BaseApp.DeliverTx(bz) if !res.IsOK() { panic(res.Log) diff --git a/cmd/gaia/cmd/gaiadebug/main.go b/cmd/gaia/cmd/gaiadebug/main.go index 73840537b2bc..93cca20d4139 100644 --- a/cmd/gaia/cmd/gaiadebug/main.go +++ b/cmd/gaia/cmd/gaiadebug/main.go @@ -213,7 +213,7 @@ func runTxCmd(cmd *cobra.Command, args []string) error { var tx = auth.StdTx{} cdc := gaia.MakeCodec() - err = cdc.UnmarshalBinary(txBytes, &tx) + err = cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return err } diff --git a/crypto/keys/keybase.go b/crypto/keys/keybase.go index 86ae5c157165..0bd500feef5a 100644 --- a/crypto/keys/keybase.go +++ b/crypto/keys/keybase.go @@ -273,7 +273,7 @@ func (kb dbKeybase) Sign(name, passphrase string, msg []byte) (sig []byte, pub t if err != nil { return nil, nil, err } - cdc.MustUnmarshalBinary([]byte(signed), sig) + cdc.MustUnmarshalBinaryLengthPrefixed([]byte(signed), sig) return sig, linfo.GetPubKey(), nil } sig, err = priv.Sign(msg) diff --git a/crypto/keys/types.go b/crypto/keys/types.go index ff90c3205dca..eeb4fdfcf0ce 100644 --- a/crypto/keys/types.go +++ b/crypto/keys/types.go @@ -182,11 +182,11 @@ func (i offlineInfo) GetAddress() types.AccAddress { // encoding info func writeInfo(i Info) []byte { - return cdc.MustMarshalBinary(i) + return cdc.MustMarshalBinaryLengthPrefixed(i) } // decoding info func readInfo(bz []byte) (info Info, err error) { - err = cdc.UnmarshalBinary(bz, &info) + err = cdc.UnmarshalBinaryLengthPrefixed(bz, &info) return } diff --git a/docs/sdk/core/app2.md b/docs/sdk/core/app2.md index d8040a850014..b0e42fd10df7 100644 --- a/docs/sdk/core/app2.md +++ b/docs/sdk/core/app2.md @@ -178,7 +178,7 @@ func (tx app2Tx) GetMsgs() []sdk.Msg { func tx2Decoder(cdc *codec.Codec) sdk.TxDecoder { return func(txBytes []byte) (sdk.Tx, sdk.Error) { var tx app2Tx - err := cdc.UnmarshalBinary(txBytes, &tx) + err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, sdk.ErrTxDecode(err.Error()) } diff --git a/docs/sdk/core/examples/app2.go b/docs/sdk/core/examples/app2.go index c1ae43666824..24458384c0d3 100644 --- a/docs/sdk/core/examples/app2.go +++ b/docs/sdk/core/examples/app2.go @@ -201,7 +201,7 @@ func (tx app2Tx) GetSignature() []byte { func tx2Decoder(cdc *codec.Codec) sdk.TxDecoder { return func(txBytes []byte) (sdk.Tx, sdk.Error) { var tx app2Tx - err := cdc.UnmarshalBinary(txBytes, &tx) + err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, sdk.ErrTxDecode(err.Error()) } diff --git a/docs/sdk/core/examples/app2_test.go b/docs/sdk/core/examples/app2_test.go index 9139034880cd..c59dec26477f 100644 --- a/docs/sdk/core/examples/app2_test.go +++ b/docs/sdk/core/examples/app2_test.go @@ -39,7 +39,7 @@ func TestEncoding(t *testing.T) { cdc := NewCodec() testTxDecoder := tx2Decoder(cdc) - encodedSendTx, err := cdc.MarshalBinary(sendTxBefore) + encodedSendTx, err := cdc.MarshalBinaryLengthPrefixed(sendTxBefore) require.Nil(t, err, "Error encoding sendTx") @@ -69,7 +69,7 @@ func TestEncoding(t *testing.T) { Signature: sig, } - encodedIssueTx, err2 := cdc.MarshalBinary(issueTxBefore) + encodedIssueTx, err2 := cdc.MarshalBinaryLengthPrefixed(issueTxBefore) require.Nil(t, err2, "Error encoding issueTx") diff --git a/examples/democoin/x/oracle/keeper.go b/examples/democoin/x/oracle/keeper.go index e55cd7083c0d..d061d2f8ed4f 100644 --- a/examples/democoin/x/oracle/keeper.go +++ b/examples/democoin/x/oracle/keeper.go @@ -71,7 +71,7 @@ func (keeper Keeper) Info(ctx sdk.Context, p Payload) (res Info) { if bz == nil { return EmptyInfo(ctx) } - keeper.cdc.MustUnmarshalBinary(bz, &res) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res) return } @@ -80,7 +80,7 @@ func (keeper Keeper) setInfo(ctx sdk.Context, p Payload, info Info) { store := ctx.KVStore(keeper.key) key := GetInfoKey(p, keeper.cdc) - bz := keeper.cdc.MustMarshalBinary(info) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(info) store.Set(key, bz) } diff --git a/examples/democoin/x/oracle/keeper_keys.go b/examples/democoin/x/oracle/keeper_keys.go index 9b71aeaa1815..d678692be9fa 100644 --- a/examples/democoin/x/oracle/keeper_keys.go +++ b/examples/democoin/x/oracle/keeper_keys.go @@ -7,13 +7,13 @@ import ( // GetInfoKey returns the key for OracleInfo func GetInfoKey(p Payload, cdc *codec.Codec) []byte { - bz := cdc.MustMarshalBinary(p) + bz := cdc.MustMarshalBinaryLengthPrefixed(p) return append([]byte{0x00}, bz...) } // GetSignPrefix returns the prefix for signs func GetSignPrefix(p Payload, cdc *codec.Codec) []byte { - bz := cdc.MustMarshalBinary(p) + bz := cdc.MustMarshalBinaryLengthPrefixed(p) return append([]byte{0x01}, bz...) } diff --git a/examples/democoin/x/oracle/oracle_test.go b/examples/democoin/x/oracle/oracle_test.go index 7621ea86090f..1284f75b6dec 100644 --- a/examples/democoin/x/oracle/oracle_test.go +++ b/examples/democoin/x/oracle/oracle_test.go @@ -82,7 +82,7 @@ func getSequence(ctx sdk.Context, key sdk.StoreKey) int { if seqbz == nil { seq = 0 } else { - codec.New().MustUnmarshalBinary(seqbz, &seq) + codec.New().MustUnmarshalBinaryLengthPrefixed(seqbz, &seq) } return seq @@ -96,7 +96,7 @@ func handleSeqOracle(ctx sdk.Context, key sdk.StoreKey, o seqOracle) sdk.Error { return sdk.NewError(sdk.CodespaceRoot, 1, "") } - bz := codec.New().MustMarshalBinary(seq + 1) + bz := codec.New().MustMarshalBinaryLengthPrefixed(seq + 1) store.Set([]byte("seq"), bz) return nil diff --git a/examples/democoin/x/simplestake/keeper.go b/examples/democoin/x/simplestake/keeper.go index 7bdc17937b55..607f61d83c5c 100644 --- a/examples/democoin/x/simplestake/keeper.go +++ b/examples/democoin/x/simplestake/keeper.go @@ -39,7 +39,7 @@ func (k Keeper) getBondInfo(ctx sdk.Context, addr sdk.AccAddress) bondInfo { return bondInfo{} } var bi bondInfo - err := k.cdc.UnmarshalBinary(bz, &bi) + err := k.cdc.UnmarshalBinaryLengthPrefixed(bz, &bi) if err != nil { panic(err) } @@ -48,7 +48,7 @@ func (k Keeper) getBondInfo(ctx sdk.Context, addr sdk.AccAddress) bondInfo { func (k Keeper) setBondInfo(ctx sdk.Context, addr sdk.AccAddress, bi bondInfo) { store := ctx.KVStore(k.key) - bz, err := k.cdc.MarshalBinary(bi) + bz, err := k.cdc.MarshalBinaryLengthPrefixed(bi) if err != nil { panic(err) } diff --git a/server/start.go b/server/start.go index 82fbbbbebadd..56a562d10c1d 100644 --- a/server/start.go +++ b/server/start.go @@ -1,6 +1,10 @@ package server import ( + "os" + "os/signal" + "syscall" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -136,6 +140,21 @@ func startInProcess(ctx *Context, appCreator AppCreator) (*node.Node, error) { } // trap signal (run forever) - tmNode.RunForever() + // Stop upon receiving SIGTERM or CTRL-C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + for _ = range c { + // logger.Error(fmt.Sprintf("captured %v, exiting...", sig)) + if tmNode.IsRunning() { + tmNode.Stop() + } + os.Exit(1) + } + }() + // Run forever + select {} + + // This won't get called. return tmNode, nil } diff --git a/store/iavlstore.go b/store/iavlstore.go index d535fd436c5b..471523b758b7 100644 --- a/store/iavlstore.go +++ b/store/iavlstore.go @@ -5,9 +5,9 @@ import ( "io" "sync" - "github.com/tendermint/go-amino" "github.com/tendermint/iavl" abci "github.com/tendermint/tendermint/abci/types" + merkle "github.com/tendermint/tendermint/crypto/merkle" cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" @@ -210,7 +210,7 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { res.Height = getHeight(tree, req) switch req.Path { - case "/store", "/key": // Get by key + case "/key": // Get by key key := req.Data // Data holds the key bytes res.Key = key if !st.VersionExists(res.Height) { @@ -224,13 +224,7 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { break } res.Value = value - cdc := amino.NewCodec() - p, err := cdc.MarshalBinary(proof) - if err != nil { - res.Log = err.Error() - break - } - res.Proof = p + res.Proof = &merkle.Proof{Ops: []merkle.ProofOp{iavl.NewIAVLValueOp(key, proof).ProofOp()}} } else { _, res.Value = tree.GetVersioned(key, res.Height) } @@ -243,7 +237,7 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { KVs = append(KVs, KVPair{Key: iterator.Key(), Value: iterator.Value()}) } iterator.Close() - res.Value = cdc.MustMarshalBinary(KVs) + res.Value = cdc.MustMarshalBinaryLengthPrefixed(KVs) default: msg := fmt.Sprintf("Unexpected Query path: %v", req.Path) return sdk.ErrUnknownRequest(msg).QueryResult() diff --git a/store/iavlstore_test.go b/store/iavlstore_test.go index 1e9263b7bbcf..d26cee055f60 100644 --- a/store/iavlstore_test.go +++ b/store/iavlstore_test.go @@ -394,9 +394,9 @@ func TestIAVLStoreQuery(t *testing.T) { {Key: k1, Value: v3}, {Key: k2, Value: v2}, } - valExpSubEmpty := cdc.MustMarshalBinary(KVs0) - valExpSub1 := cdc.MustMarshalBinary(KVs1) - valExpSub2 := cdc.MustMarshalBinary(KVs2) + valExpSubEmpty := cdc.MustMarshalBinaryLengthPrefixed(KVs0) + valExpSub1 := cdc.MustMarshalBinaryLengthPrefixed(KVs1) + valExpSub2 := cdc.MustMarshalBinaryLengthPrefixed(KVs2) cid := iavlStore.Commit() ver := cid.Version @@ -459,7 +459,7 @@ func TestIAVLStoreQuery(t *testing.T) { require.Equal(t, valExpSub2, qres.Value) // default (height 0) will show latest -1 - query0 := abci.RequestQuery{Path: "/store", Data: k1} + query0 := abci.RequestQuery{Path: "/key", Data: k1} qres = iavlStore.Query(query0) require.Equal(t, uint32(sdk.CodeOK), qres.Code) require.Equal(t, v1, qres.Value) diff --git a/store/list.go b/store/list.go index 9905b0d6f5be..2c22afb06169 100644 --- a/store/list.go +++ b/store/list.go @@ -42,21 +42,21 @@ func (m List) Len() (res uint64) { if bz == nil { return 0 } - m.cdc.MustUnmarshalBinary(bz, &res) + m.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res) return } // Get() returns the element by its index func (m List) Get(index uint64, ptr interface{}) error { bz := m.store.Get(ElemKey(index)) - return m.cdc.UnmarshalBinary(bz, ptr) + return m.cdc.UnmarshalBinaryLengthPrefixed(bz, ptr) } // Set() stores the element to the given position // Setting element out of range will break length counting // Use Push() instead of Set() to append a new element func (m List) Set(index uint64, value interface{}) { - bz := m.cdc.MustMarshalBinary(value) + bz := m.cdc.MustMarshalBinaryLengthPrefixed(value) m.store.Set(ElemKey(index), bz) } @@ -72,7 +72,7 @@ func (m List) Delete(index uint64) { func (m List) Push(value interface{}) { length := m.Len() m.Set(length, value) - m.store.Set(LengthKey(), m.cdc.MustMarshalBinary(length+1)) + m.store.Set(LengthKey(), m.cdc.MustMarshalBinaryLengthPrefixed(length+1)) } // Iterate() is used to iterate over all existing elements in the list @@ -85,7 +85,7 @@ func (m List) Iterate(ptr interface{}, fn func(uint64) bool) { iter := sdk.KVStorePrefixIterator(m.store, []byte{0x01}) for ; iter.Valid(); iter.Next() { v := iter.Value() - m.cdc.MustUnmarshalBinary(v, ptr) + m.cdc.MustUnmarshalBinaryLengthPrefixed(v, ptr) k := iter.Key() s := string(k[len(k)-20:]) index, err := strconv.ParseUint(s, 10, 64) diff --git a/store/multistoreproof.go b/store/multistoreproof.go index d62bc4aca509..ef030c092fae 100644 --- a/store/multistoreproof.go +++ b/store/multistoreproof.go @@ -2,90 +2,121 @@ package store import ( "bytes" + "fmt" - "github.com/pkg/errors" "github.com/tendermint/iavl" + "github.com/tendermint/tendermint/crypto/merkle" cmn "github.com/tendermint/tendermint/libs/common" ) // MultiStoreProof defines a collection of store proofs in a multi-store type MultiStoreProof struct { StoreInfos []storeInfo - StoreName string - RangeProof iavl.RangeProof } -// buildMultiStoreProof build MultiStoreProof based on iavl proof and storeInfos -func buildMultiStoreProof(iavlProof []byte, storeName string, storeInfos []storeInfo) []byte { - var rangeProof iavl.RangeProof - cdc.MustUnmarshalBinary(iavlProof, &rangeProof) +func NewMultiStoreProof(storeInfos []storeInfo) *MultiStoreProof { + return &MultiStoreProof{StoreInfos: storeInfos} +} - msp := MultiStoreProof{ - StoreInfos: storeInfos, - StoreName: storeName, - RangeProof: rangeProof, +func (proof *MultiStoreProof) ComputeRootHash() []byte { + ci := commitInfo{ + Version: -1, // Not needed. TODO: improve code + StoreInfos: proof.StoreInfos, } - - proof := cdc.MustMarshalBinary(msp) - return proof + return ci.Hash() } -// VerifyMultiStoreCommitInfo verify multiStoreCommitInfo against appHash -func VerifyMultiStoreCommitInfo(storeName string, storeInfos []storeInfo, appHash []byte) ([]byte, error) { - var substoreCommitHash []byte - var height int64 - for _, storeInfo := range storeInfos { - if storeInfo.Name == storeName { - substoreCommitHash = storeInfo.Core.CommitID.Hash - height = storeInfo.Core.CommitID.Version - } +// RequireProof return whether proof is require for the subpath +func RequireProof(subpath string) bool { + // XXX create a better convention. + // Currently, only when query subpath is "/key", will proof be included in response. + // If there are some changes about proof building in iavlstore.go, we must change code here to keep consistency with iavlstore.go:212 + if subpath == "/key" { + return true } - if len(substoreCommitHash) == 0 { - return nil, cmn.NewError("failed to get substore root commit hash by store name") + return false +} + +//---------------------------------------- + +// Implements +type MultiStoreProofOp struct { + // Encoded in ProofOp.Key + key []byte + + // To encode in ProofOp.Data. + Proof *MultiStoreProof `json:"proof"` +} + +var _ merkle.ProofOperator = MultiStoreProofOp{} + +const ProofOpMultiStoreProof = "multistore" + +func NewMultiStoreProofOp(key []byte, proof *MultiStoreProof) MultiStoreProofOp { + return MultiStoreProofOp{ + key: key, + Proof: proof, } +} - ci := commitInfo{ - Version: height, - StoreInfos: storeInfos, +func MultiStoreProofOpDecoder(pop merkle.ProofOp) (merkle.ProofOperator, error) { + if pop.Type != ProofOpMultiStoreProof { + return nil, cmn.NewError("unexpected ProofOp.Type; got %v, want %v", pop.Type, ProofOpMultiStoreProof) } - if !bytes.Equal(appHash, ci.Hash()) { - return nil, cmn.NewError("the merkle root of multiStoreCommitInfo doesn't equal to appHash") + var op MultiStoreProofOp // a bit strange as we'll discard this, but it works. + err := cdc.UnmarshalBinaryLengthPrefixed(pop.Data, &op) + if err != nil { + return nil, cmn.ErrorWrap(err, "decoding ProofOp.Data into MultiStoreProofOp") } - return substoreCommitHash, nil + return NewMultiStoreProofOp(pop.Key, op.Proof), nil } -// VerifyRangeProof verify iavl RangeProof -func VerifyRangeProof(key, value []byte, substoreCommitHash []byte, rangeProof *iavl.RangeProof) error { +func (op MultiStoreProofOp) ProofOp() merkle.ProofOp { + bz := cdc.MustMarshalBinaryLengthPrefixed(op) + return merkle.ProofOp{ + Type: ProofOpMultiStoreProof, + Key: op.key, + Data: bz, + } +} - // verify the proof to ensure data integrity. - err := rangeProof.Verify(substoreCommitHash) - if err != nil { - return errors.Wrap(err, "proof root hash doesn't equal to substore commit root hash") +func (op MultiStoreProofOp) String() string { + return fmt.Sprintf("MultiStoreProofOp{%v}", op.GetKey()) +} + +func (op MultiStoreProofOp) GetKey() []byte { + return op.key +} + +func (op MultiStoreProofOp) Run(args [][]byte) ([][]byte, error) { + if len(args) != 1 { + return nil, cmn.NewError("Value size is not 1") } + value := args[0] + root := op.Proof.ComputeRootHash() - if len(value) != 0 { - // verify existence proof - err = rangeProof.VerifyItem(key, value) - if err != nil { - return errors.Wrap(err, "failed in existence verification") - } - } else { - // verify absence proof - err = rangeProof.VerifyAbsence(key) - if err != nil { - return errors.Wrap(err, "failed in absence verification") + for _, si := range op.Proof.StoreInfos { + if si.Name == string(op.key) { + + if bytes.Equal(value, si.Core.CommitID.Hash) { + return [][]byte{root}, nil + } else { + return nil, cmn.NewError("hash mismatch for substore %v: %X vs %X", si.Name, si.Core.CommitID.Hash, value) + } } } - return nil + return nil, cmn.NewError("key %v not found in multistore proof", op.key) } -// RequireProof return whether proof is require for the subpath -func RequireProof(subpath string) bool { - // Currently, only when query subpath is "/store" or "/key", will proof be included in response. - // If there are some changes about proof building in iavlstore.go, we must change code here to keep consistency with iavlstore.go:212 - if subpath == "/store" || subpath == "/key" { - return true - } - return false +//---------------------------------------- + +// XXX This should be managed by the rootMultiStore +// which may want to register more proof ops? +func DefaultProofRuntime() (prt *merkle.ProofRuntime) { + prt = merkle.NewProofRuntime() + prt.RegisterOpDecoder(merkle.ProofOpSimpleValue, merkle.SimpleValueOpDecoder) + prt.RegisterOpDecoder(iavl.ProofOpIAVLValue, iavl.IAVLValueOpDecoder) + prt.RegisterOpDecoder(iavl.ProofOpIAVLAbsence, iavl.IAVLAbsenceOpDecoder) + return } diff --git a/store/multistoreproof_test.go b/store/multistoreproof_test.go index 45a102cd3ba4..ed0a3af3991e 100644 --- a/store/multistoreproof_test.go +++ b/store/multistoreproof_test.go @@ -1,123 +1,56 @@ package store import ( - "encoding/hex" + "fmt" "testing" - "github.com/stretchr/testify/assert" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/require" - "github.com/tendermint/iavl" - cmn "github.com/tendermint/tendermint/libs/common" - "github.com/tendermint/tendermint/libs/db" + abci "github.com/tendermint/tendermint/abci/types" + dbm "github.com/tendermint/tendermint/libs/db" ) -func TestVerifyMultiStoreCommitInfo(t *testing.T) { - appHash, _ := hex.DecodeString("69959B1B4E68E0F7BD3551A50C8F849B81801AF2") +func TestVerifyMultiStoreQueryProof(t *testing.T) { - substoreRootHash, _ := hex.DecodeString("ea5d468431015c2cd6295e9a0bb1fc0e49033828") - storeName := "acc" - - var storeInfos []storeInfo - - gocRootHash, _ := hex.DecodeString("62c171bb022e47d1f745608ff749e676dbd25f78") - storeInfos = append(storeInfos, storeInfo{ - Name: "gov", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: gocRootHash, - }, - }, - }) - - storeInfos = append(storeInfos, storeInfo{ - Name: "main", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: nil, - }, - }, - }) - - accRootHash, _ := hex.DecodeString("ea5d468431015c2cd6295e9a0bb1fc0e49033828") - storeInfos = append(storeInfos, storeInfo{ - Name: "acc", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: accRootHash, - }, - }, - }) - - storeInfos = append(storeInfos, storeInfo{ - Name: "ibc", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: nil, - }, - }, - }) - - stakeRootHash, _ := hex.DecodeString("987d1d27b8771d93aa3691262f661d2c85af7ca4") - storeInfos = append(storeInfos, storeInfo{ - Name: "stake", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: stakeRootHash, + // Create main tree for testing. + db := dbm.NewMemDB() + store_i, err := LoadIAVLStore(db, CommitID{}, sdk.PruneNothing) + store := store_i.(*iavlStore) + require.Nil(t, err) + store.Set([]byte("MYKEY"), []byte("MYVALUE")) + cid := store.Commit() + + /* + var storeInfos = []storeInfo{ + storeInfo{ + Name: "tree", + Core: storeCore{ + CommitID: cid, + }, }, - }, - }) - - slashingRootHash, _ := hex.DecodeString("388ee6e5b11f367069beb1eefd553491afe9d73e") - storeInfos = append(storeInfos, storeInfo{ - Name: "slashing", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: slashingRootHash, + storeInfo{ + Name: "otherTree", + Core: storeCore{ + CommitID: CommitID{ + Version: 689, + Hash: []byte("otherHash"), + }, + }, }, - }, + } + */ + + // Get Proof + res := store.Query(abci.RequestQuery{ + Path: "/key", // required path to get key/value+proof + Data: []byte("MYKEY"), + Prove: true, }) + fmt.Println("result", spew.Sdump(res)) + require.NotNil(t, res.Proof) - commitHash, err := VerifyMultiStoreCommitInfo(storeName, storeInfos, appHash) + prt := DefaultProofRuntime() + err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE")) require.Nil(t, err) - require.Equal(t, commitHash, substoreRootHash) - - appHash, _ = hex.DecodeString("29de216bf5e2531c688de36caaf024cd3bb09ee3") - - _, err = VerifyMultiStoreCommitInfo(storeName, storeInfos, appHash) - require.Error(t, err, "appHash doesn't match to the merkle root of multiStoreCommitInfo") -} - -func TestVerifyRangeProof(t *testing.T) { - tree := iavl.NewMutableTree(db.NewMemDB(), 0) - - rand := cmn.NewRand() - rand.Seed(0) // for determinism - for _, ikey := range []byte{0x11, 0x32, 0x50, 0x72, 0x99} { - key := []byte{ikey} - tree.Set(key, []byte(rand.Str(8))) - } - - root := tree.WorkingHash() - - key := []byte{0x32} - val, proof, err := tree.GetWithProof(key) - assert.Nil(t, err) - assert.NotEmpty(t, val) - assert.NotEmpty(t, proof) - err = VerifyRangeProof(key, val, root, proof) - assert.Nil(t, err) - - key = []byte{0x40} - val, proof, err = tree.GetWithProof(key) - assert.Nil(t, err) - assert.Empty(t, val) - assert.NotEmpty(t, proof) - err = VerifyRangeProof(key, val, root, proof) - assert.Nil(t, err) } diff --git a/store/queue.go b/store/queue.go index 4081f63b3c6b..f41ecb7d7a19 100644 --- a/store/queue.go +++ b/store/queue.go @@ -27,12 +27,12 @@ func (m Queue) getTop() (res uint64) { return 0 } - m.List.cdc.MustUnmarshalBinary(bz, &res) + m.List.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res) return } func (m Queue) setTop(top uint64) { - bz := m.List.cdc.MustMarshalBinary(top) + bz := m.List.cdc.MustMarshalBinaryLengthPrefixed(top) m.List.store.Set(TopKey(), bz) } diff --git a/store/queue_test.go b/store/queue_test.go index 5ea6d906a4e1..58e96f56a19e 100644 --- a/store/queue_test.go +++ b/store/queue_test.go @@ -81,7 +81,7 @@ func TestKeys(t *testing.T) { var actual int // Checking keys.LengthKey - err := cdc.UnmarshalBinary(store.Get(LengthKey()), &len) + err := cdc.UnmarshalBinaryLengthPrefixed(store.Get(LengthKey()), &len) require.Nil(t, err) require.Equal(t, len, queue.List.Len()) @@ -89,14 +89,14 @@ func TestKeys(t *testing.T) { for i := 0; i < 10; i++ { queue.List.Get(uint64(i), &expected) bz := store.Get(ElemKey(uint64(i))) - err = cdc.UnmarshalBinary(bz, &actual) + err = cdc.UnmarshalBinaryLengthPrefixed(bz, &actual) require.Nil(t, err) require.Equal(t, expected, actual) } queue.Pop() - err = cdc.UnmarshalBinary(store.Get(TopKey()), &top) + err = cdc.UnmarshalBinaryLengthPrefixed(store.Get(TopKey()), &top) require.Nil(t, err) require.Equal(t, top, queue.getTop()) } diff --git a/store/rootmultistore.go b/store/rootmultistore.go index 7de465f7703e..27b983c19fa2 100644 --- a/store/rootmultistore.go +++ b/store/rootmultistore.go @@ -300,7 +300,10 @@ func (rs *rootMultiStore) Query(req abci.RequestQuery) abci.ResponseQuery { return sdk.ErrInternal(errMsg.Error()).QueryResult() } - res.Proof = buildMultiStoreProof(res.Proof, storeName, commitInfo.StoreInfos) + res.Proof.Ops = append(res.Proof.Ops, NewMultiStoreProofOp( + []byte(storeName), + NewMultiStoreProof(commitInfo.StoreInfos), + ).ProofOp()) return res } @@ -387,9 +390,9 @@ type commitInfo struct { // Hash returns the simple merkle root hash of the stores sorted by name. func (ci commitInfo) Hash() []byte { // TODO cache to ci.hash []byte - m := make(map[string]merkle.Hasher, len(ci.StoreInfos)) + m := make(map[string][]byte, len(ci.StoreInfos)) for _, storeInfo := range ci.StoreInfos { - m[storeInfo.Name] = storeInfo + m[storeInfo.Name] = storeInfo.Hash() } return merkle.SimpleHashFromMap(m) } @@ -422,7 +425,7 @@ type storeCore struct { func (si storeInfo) Hash() []byte { // Doesn't write Name, since merkle.SimpleHashFromMap() will // include them via the keys. - bz, _ := cdc.MarshalBinary(si.Core) // Does not error + bz, _ := cdc.MarshalBinaryLengthPrefixed(si.Core) // Does not error hasher := tmhash.New() _, err := hasher.Write(bz) if err != nil { @@ -441,7 +444,7 @@ func getLatestVersion(db dbm.DB) int64 { if latestBytes == nil { return 0 } - err := cdc.UnmarshalBinary(latestBytes, &latest) + err := cdc.UnmarshalBinaryLengthPrefixed(latestBytes, &latest) if err != nil { panic(err) } @@ -450,7 +453,7 @@ func getLatestVersion(db dbm.DB) int64 { // Set the latest version. func setLatestVersion(batch dbm.Batch, version int64) { - latestBytes, _ := cdc.MarshalBinary(version) // Does not error + latestBytes, _ := cdc.MarshalBinaryLengthPrefixed(version) // Does not error batch.Set([]byte(latestVersionKey), latestBytes) } @@ -493,7 +496,7 @@ func getCommitInfo(db dbm.DB, ver int64) (commitInfo, error) { // Parse bytes. var cInfo commitInfo - err := cdc.UnmarshalBinary(cInfoBytes, &cInfo) + err := cdc.UnmarshalBinaryLengthPrefixed(cInfoBytes, &cInfo) if err != nil { return commitInfo{}, fmt.Errorf("failed to get rootMultiStore: %v", err) } @@ -502,7 +505,7 @@ func getCommitInfo(db dbm.DB, ver int64) (commitInfo, error) { // Set a commitInfo for given version. func setCommitInfo(batch dbm.Batch, version int64, cInfo commitInfo) { - cInfoBytes, err := cdc.MarshalBinary(cInfo) + cInfoBytes, err := cdc.MarshalBinaryLengthPrefixed(cInfo) if err != nil { panic(err) } diff --git a/store/rootmultistore_test.go b/store/rootmultistore_test.go index d6a714c66efa..501c5b730c25 100644 --- a/store/rootmultistore_test.go +++ b/store/rootmultistore_test.go @@ -215,7 +215,7 @@ func getExpectedCommitID(store *rootMultiStore, ver int64) CommitID { } func hashStores(stores map[StoreKey]CommitStore) []byte { - m := make(map[string]merkle.Hasher, len(stores)) + m := make(map[string][]byte, len(stores)) for key, store := range stores { name := key.Name() m[name] = storeInfo{ @@ -224,7 +224,7 @@ func hashStores(stores map[StoreKey]CommitStore) []byte { CommitID: store.LastCommitID(), // StoreType: store.GetStoreType(), }, - } + }.Hash() } return merkle.SimpleHashFromMap(m) } diff --git a/types/decimal_test.go b/types/decimal_test.go index 07329c7dc7da..b3589477134f 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -305,11 +305,11 @@ func TestSerializationGocodecJSON(t *testing.T) { func TestSerializationGocodecBinary(t *testing.T) { d := mustNewDecFromStr(t, "0.333") - bz, err := cdc.MarshalBinary(d) + bz, err := cdc.MarshalBinaryLengthPrefixed(d) require.NoError(t, err) var d2 Dec - err = cdc.UnmarshalBinary(bz, &d2) + err = cdc.UnmarshalBinaryLengthPrefixed(bz, &d2) require.NoError(t, err) require.True(t, d.Equal(d2), "original: %v, unmarshalled: %v", d, d2) } @@ -323,11 +323,11 @@ type testDEmbedStruct struct { // TODO make work for UnmarshalJSON func TestEmbeddedStructSerializationGocodec(t *testing.T) { obj := testDEmbedStruct{"foo", 10, NewDecWithPrec(1, 3)} - bz, err := cdc.MarshalBinary(obj) + bz, err := cdc.MarshalBinaryLengthPrefixed(obj) require.Nil(t, err) var obj2 testDEmbedStruct - err = cdc.UnmarshalBinary(bz, &obj2) + err = cdc.UnmarshalBinaryLengthPrefixed(bz, &obj2) require.Nil(t, err) require.Equal(t, obj.Field1, obj2.Field1) diff --git a/x/auth/account_test.go b/x/auth/account_test.go index b7a78e2d2220..e48060fbefc9 100644 --- a/x/auth/account_test.go +++ b/x/auth/account_test.go @@ -93,16 +93,16 @@ func TestBaseAccountMarshal(t *testing.T) { cdc := codec.New() codec.RegisterCrypto(cdc) - b, err := cdc.MarshalBinary(acc) + b, err := cdc.MarshalBinaryLengthPrefixed(acc) require.Nil(t, err) acc2 := BaseAccount{} - err = cdc.UnmarshalBinary(b, &acc2) + err = cdc.UnmarshalBinaryLengthPrefixed(b, &acc2) require.Nil(t, err) require.Equal(t, acc, acc2) // error on bad bytes acc2 = BaseAccount{} - err = cdc.UnmarshalBinary(b[:len(b)/2], &acc2) + err = cdc.UnmarshalBinaryLengthPrefixed(b[:len(b)/2], &acc2) require.NotNil(t, err) } diff --git a/x/auth/client/txbuilder/txbuilder.go b/x/auth/client/txbuilder/txbuilder.go index b488ef359f7b..72022d7dc036 100644 --- a/x/auth/client/txbuilder/txbuilder.go +++ b/x/auth/client/txbuilder/txbuilder.go @@ -125,7 +125,7 @@ func (bldr TxBuilder) Sign(name, passphrase string, msg StdSignMsg) ([]byte, err if err != nil { return nil, err } - return bldr.Codec.MarshalBinary(auth.NewStdTx(msg.Msgs, msg.Fee, []auth.StdSignature{sig}, msg.Memo)) + return bldr.Codec.MarshalBinaryLengthPrefixed(auth.NewStdTx(msg.Msgs, msg.Fee, []auth.StdSignature{sig}, msg.Memo)) } // BuildAndSign builds a single message to be signed, and signs a transaction @@ -166,7 +166,7 @@ func (bldr TxBuilder) BuildWithPubKey(name string, msgs []sdk.Msg) ([]byte, erro PubKey: info.GetPubKey(), }} - return bldr.Codec.MarshalBinary(auth.NewStdTx(msg.Msgs, msg.Fee, sigs, msg.Memo)) + return bldr.Codec.MarshalBinaryLengthPrefixed(auth.NewStdTx(msg.Msgs, msg.Fee, sigs, msg.Memo)) } // SignStdTx appends a signature to a StdTx and returns a copy of a it. If append diff --git a/x/auth/feekeeper.go b/x/auth/feekeeper.go index a6be2e12def0..0006a918529c 100644 --- a/x/auth/feekeeper.go +++ b/x/auth/feekeeper.go @@ -36,12 +36,12 @@ func (fck FeeCollectionKeeper) GetCollectedFees(ctx sdk.Context) sdk.Coins { } feePool := &(sdk.Coins{}) - fck.cdc.MustUnmarshalBinary(bz, feePool) + fck.cdc.MustUnmarshalBinaryLengthPrefixed(bz, feePool) return *feePool } func (fck FeeCollectionKeeper) setCollectedFees(ctx sdk.Context, coins sdk.Coins) { - bz := fck.cdc.MustMarshalBinary(coins) + bz := fck.cdc.MustMarshalBinaryLengthPrefixed(coins) store := ctx.KVStore(fck.key) store.Set(collectedFeesKey, bz) } diff --git a/x/auth/mapper.go b/x/auth/mapper.go index dd127c7312e3..da5481749fa7 100644 --- a/x/auth/mapper.go +++ b/x/auth/mapper.go @@ -148,13 +148,13 @@ func (am AccountKeeper) GetNextAccountNumber(ctx sdk.Context) int64 { if bz == nil { accNumber = 0 } else { - err := am.cdc.UnmarshalBinary(bz, &accNumber) + err := am.cdc.UnmarshalBinaryLengthPrefixed(bz, &accNumber) if err != nil { panic(err) } } - bz = am.cdc.MustMarshalBinary(accNumber + 1) + bz = am.cdc.MustMarshalBinaryLengthPrefixed(accNumber + 1) store.Set(globalAccountNumberKey, bz) return accNumber diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go index ca6eb7b422ff..b77b8b27ffb7 100644 --- a/x/auth/stdtx.go +++ b/x/auth/stdtx.go @@ -151,7 +151,7 @@ func DefaultTxDecoder(cdc *codec.Codec) sdk.TxDecoder { // StdTx.Msg is an interface. The concrete types // are registered by MakeTxCodec - err := cdc.UnmarshalBinary(txBytes, &tx) + err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, sdk.ErrTxDecode("").TraceSDK(err.Error()) } diff --git a/x/bank/client/cli/broadcast.go b/x/bank/client/cli/broadcast.go index e7e6bc16d135..9371062fd4a4 100644 --- a/x/bank/client/cli/broadcast.go +++ b/x/bank/client/cli/broadcast.go @@ -25,7 +25,7 @@ in place of an input filename, the command reads from standard input.`, if err != nil { return } - txBytes, err := cliCtx.Codec.MarshalBinary(stdTx) + txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(stdTx) if err != nil { return } diff --git a/x/bank/client/rest/broadcast.go b/x/bank/client/rest/broadcast.go index c52961caf5fe..5c557a506c62 100644 --- a/x/bank/client/rest/broadcast.go +++ b/x/bank/client/rest/broadcast.go @@ -22,7 +22,7 @@ func BroadcastTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) ht return } - txBytes, err := cliCtx.Codec.MarshalBinary(m.Tx) + txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(m.Tx) if err != nil { utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 4dcc31a51280..ac519107daea 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -23,14 +23,14 @@ func (k Keeper) GetDelegationDistInfo(ctx sdk.Context, delAddr sdk.AccAddress, panic("Stored delegation-distribution info should not have been nil") } - k.cdc.MustUnmarshalBinary(b, &ddi) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &ddi) return } // set the delegator distribution info func (k Keeper) SetDelegationDistInfo(ctx sdk.Context, ddi types.DelegationDistInfo) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(ddi) + b := k.cdc.MustMarshalBinaryLengthPrefixed(ddi) store.Set(GetDelegationDistInfoKey(ddi.DelegatorAddr, ddi.ValOperatorAddr), b) } diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index 06b153a51de0..804ea5242220 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -13,7 +13,7 @@ func (k Keeper) GetAllValidatorDistInfos(ctx sdk.Context) (vdis []types.Validato for ; iterator.Valid(); iterator.Next() { var vdi types.ValidatorDistInfo - k.cdc.MustUnmarshalBinary(iterator.Value(), &vdi) + k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &vdi) vdis = append(vdis, vdi) } return vdis @@ -27,7 +27,7 @@ func (k Keeper) GetAllDelegationDistInfos(ctx sdk.Context) (ddis []types.Delegat for ; iterator.Valid(); iterator.Next() { var ddi types.DelegationDistInfo - k.cdc.MustUnmarshalBinary(iterator.Value(), &ddi) + k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &ddi) ddis = append(ddis, ddi) } return ddis diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index d167e0efe275..a6fed963586a 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -44,14 +44,14 @@ func (k Keeper) GetFeePool(ctx sdk.Context) (feePool types.FeePool) { if b == nil { panic("Stored fee pool should not have been nil") } - k.cdc.MustUnmarshalBinary(b, &feePool) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &feePool) return } // set the global fee pool distribution info func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(feePool) + b := k.cdc.MustMarshalBinaryLengthPrefixed(feePool) store.Set(FeePoolKey, b) } @@ -77,14 +77,14 @@ func (k Keeper) GetPreviousProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsA panic("Previous proposer not set") } - k.cdc.MustUnmarshalBinary(b, &consAddr) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &consAddr) return } // get the proposer public key for this block func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(consAddr) + b := k.cdc.MustMarshalBinaryLengthPrefixed(consAddr) store.Set(ProposerKey, b) } diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index b1bd4ae4e05c..0a02fb245f07 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -172,7 +172,7 @@ func (k Keeper) IterateValidatorDistInfos(ctx sdk.Context, index := int64(0) for ; iter.Valid(); iter.Next() { var vdi types.ValidatorDistInfo - k.cdc.MustUnmarshalBinary(iter.Value(), &vdi) + k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &vdi) if fn(index, vdi) { return } diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index d267d66b2247..d2c755cfa6b5 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -23,14 +23,14 @@ func (k Keeper) GetValidatorDistInfo(ctx sdk.Context, panic("Stored validator-distribution info should not have been nil") } - k.cdc.MustUnmarshalBinary(b, &vdi) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &vdi) return } // set the validator distribution info func (k Keeper) SetValidatorDistInfo(ctx sdk.Context, vdi types.ValidatorDistInfo) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(vdi) + b := k.cdc.MustMarshalBinaryLengthPrefixed(vdi) store.Set(GetValidatorDistInfoKey(vdi.OperatorAddr), b) } diff --git a/x/gov/client/rest/rest.go b/x/gov/client/rest/rest.go index 374f5cc2ac56..842edfdacb45 100644 --- a/x/gov/client/rest/rest.go +++ b/x/gov/client/rest/rest.go @@ -292,7 +292,7 @@ func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han var deposit gov.Deposit cdc.UnmarshalJSON(res, &deposit) if deposit.Empty() { - res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(gov.QueryProposalParams{params.ProposalID})) + res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinaryLengthPrefixed(gov.QueryProposalParams{params.ProposalID})) if err != nil || len(res) == 0 { err := errors.Errorf("proposalID [%d] does not exist", proposalID) utils.WriteErrorResponse(w, http.StatusNotFound, err.Error()) diff --git a/x/gov/keeper.go b/x/gov/keeper.go index d061f1206232..a3e3cda2534c 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -106,7 +106,7 @@ func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID int64) Proposal { } var proposal Proposal - keeper.cdc.MustUnmarshalBinary(bz, &proposal) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposal) return proposal } @@ -114,7 +114,7 @@ func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID int64) Proposal { // Implements sdk.AccountKeeper. func (keeper Keeper) SetProposal(ctx sdk.Context, proposal Proposal) { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinary(proposal) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposal) store.Set(KeyProposal(proposal.GetProposalID()), bz) } @@ -175,7 +175,7 @@ func (keeper Keeper) setInitialProposalID(ctx sdk.Context, proposalID int64) sdk if bz != nil { return ErrInvalidGenesis(keeper.codespace, "Initial ProposalID already set") } - bz = keeper.cdc.MustMarshalBinary(proposalID) + bz = keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) store.Set(KeyNextProposalID, bz) return nil } @@ -197,8 +197,8 @@ func (keeper Keeper) getNewProposalID(ctx sdk.Context) (proposalID int64, err sd if bz == nil { return -1, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set") } - keeper.cdc.MustUnmarshalBinary(bz, &proposalID) - bz = keeper.cdc.MustMarshalBinary(proposalID + 1) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID) + bz = keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID + 1) store.Set(KeyNextProposalID, bz) return proposalID, nil } @@ -210,7 +210,7 @@ func (keeper Keeper) peekCurrentProposalID(ctx sdk.Context) (proposalID int64, e if bz == nil { return -1, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set") } - keeper.cdc.MustUnmarshalBinary(bz, &proposalID) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID) return proposalID, nil } @@ -298,13 +298,13 @@ func (keeper Keeper) GetVote(ctx sdk.Context, proposalID int64, voterAddr sdk.Ac return Vote{}, false } var vote Vote - keeper.cdc.MustUnmarshalBinary(bz, &vote) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &vote) return vote, true } func (keeper Keeper) setVote(ctx sdk.Context, proposalID int64, voterAddr sdk.AccAddress, vote Vote) { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinary(vote) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(vote) store.Set(KeyVote(proposalID, voterAddr), bz) } @@ -330,13 +330,13 @@ func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID int64, depositerAddr return Deposit{}, false } var deposit Deposit - keeper.cdc.MustUnmarshalBinary(bz, &deposit) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &deposit) return deposit, true } func (keeper Keeper) setDeposit(ctx sdk.Context, proposalID int64, depositerAddr sdk.AccAddress, deposit Deposit) { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinary(deposit) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(deposit) store.Set(KeyDeposit(proposalID, depositerAddr), bz) } @@ -398,7 +398,7 @@ func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID int64) { for ; depositsIterator.Valid(); depositsIterator.Next() { deposit := &Deposit{} - keeper.cdc.MustUnmarshalBinary(depositsIterator.Value(), deposit) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), deposit) _, _, err := keeper.ck.AddCoins(ctx, deposit.Depositer, deposit.Amount) if err != nil { @@ -434,14 +434,14 @@ func (keeper Keeper) getActiveProposalQueue(ctx sdk.Context) ProposalQueue { } var proposalQueue ProposalQueue - keeper.cdc.MustUnmarshalBinary(bz, &proposalQueue) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalQueue) return proposalQueue } func (keeper Keeper) setActiveProposalQueue(ctx sdk.Context, proposalQueue ProposalQueue) { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinary(proposalQueue) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalQueue) store.Set(KeyActiveProposalQueue, bz) } @@ -480,14 +480,14 @@ func (keeper Keeper) getInactiveProposalQueue(ctx sdk.Context) ProposalQueue { var proposalQueue ProposalQueue - keeper.cdc.MustUnmarshalBinary(bz, &proposalQueue) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalQueue) return proposalQueue } func (keeper Keeper) setInactiveProposalQueue(ctx sdk.Context, proposalQueue ProposalQueue) { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinary(proposalQueue) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalQueue) store.Set(KeyInactiveProposalQueue, bz) } diff --git a/x/gov/keeper_test.go b/x/gov/keeper_test.go index 91c41d7d7dde..0152c50bbd4c 100644 --- a/x/gov/keeper_test.go +++ b/x/gov/keeper_test.go @@ -122,11 +122,11 @@ func TestDeposits(t *testing.T) { // Test deposit iterator depositsIterator := keeper.GetDeposits(ctx, proposalID) require.True(t, depositsIterator.Valid()) - keeper.cdc.MustUnmarshalBinary(depositsIterator.Value(), &deposit) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit) require.Equal(t, addrs[0], deposit.Depositer) require.Equal(t, fourSteak.Plus(fiveSteak), deposit.Amount) depositsIterator.Next() - keeper.cdc.MustUnmarshalBinary(depositsIterator.Value(), &deposit) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit) require.Equal(t, addrs[1], deposit.Depositer) require.Equal(t, fourSteak, deposit.Amount) depositsIterator.Next() @@ -184,14 +184,14 @@ func TestVotes(t *testing.T) { // Test vote iterator votesIterator := keeper.GetVotes(ctx, proposalID) require.True(t, votesIterator.Valid()) - keeper.cdc.MustUnmarshalBinary(votesIterator.Value(), &vote) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote) require.True(t, votesIterator.Valid()) require.Equal(t, addrs[0], vote.Voter) require.Equal(t, proposalID, vote.ProposalID) require.Equal(t, OptionYes, vote.Option) votesIterator.Next() require.True(t, votesIterator.Valid()) - keeper.cdc.MustUnmarshalBinary(votesIterator.Value(), &vote) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote) require.True(t, votesIterator.Valid()) require.Equal(t, addrs[1], vote.Voter) require.Equal(t, proposalID, vote.ProposalID) diff --git a/x/gov/queryable.go b/x/gov/queryable.go index 93469a5ac299..ae7c6d9c55a9 100644 --- a/x/gov/queryable.go +++ b/x/gov/queryable.go @@ -126,7 +126,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper depositsIterator := keeper.GetDeposits(ctx, params.ProposalID) for ; depositsIterator.Valid(); depositsIterator.Next() { deposit := Deposit{} - keeper.cdc.MustUnmarshalBinary(depositsIterator.Value(), &deposit) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit) deposits = append(deposits, deposit) } @@ -155,7 +155,7 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke votesIterator := keeper.GetVotes(ctx, params.ProposalID) for ; votesIterator.Valid(); votesIterator.Next() { vote := Vote{} - keeper.cdc.MustUnmarshalBinary(votesIterator.Value(), &vote) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote) votes = append(votes, vote) } diff --git a/x/gov/tally.go b/x/gov/tally.go index b6e42c4b5a02..2bfaa7dd550c 100644 --- a/x/gov/tally.go +++ b/x/gov/tally.go @@ -39,7 +39,7 @@ func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, tall defer votesIterator.Close() for ; votesIterator.Valid(); votesIterator.Next() { vote := &Vote{} - keeper.cdc.MustUnmarshalBinary(votesIterator.Value(), vote) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), vote) // if validator, just record it in the map // if delegator tally voting power diff --git a/x/ibc/client/cli/relay.go b/x/ibc/client/cli/relay.go index ab7168aca3ff..43ad783f4935 100644 --- a/x/ibc/client/cli/relay.go +++ b/x/ibc/client/cli/relay.go @@ -117,7 +117,7 @@ OUTER: var processed int64 if processedbz == nil { processed = 0 - } else if err = c.cdc.UnmarshalBinary(processedbz, &processed); err != nil { + } else if err = c.cdc.UnmarshalBinaryLengthPrefixed(processedbz, &processed); err != nil { panic(err) } @@ -131,7 +131,7 @@ OUTER: var egressLength int64 if egressLengthbz == nil { egressLength = 0 - } else if err = c.cdc.UnmarshalBinary(egressLengthbz, &egressLength); err != nil { + } else if err = c.cdc.UnmarshalBinaryLengthPrefixed(egressLengthbz, &egressLength); err != nil { panic(err) } @@ -192,7 +192,7 @@ func (c relayCommander) getSequence(node string) int64 { func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []byte { var packet ibc.IBCPacket - if err := c.cdc.UnmarshalBinary(bz, &packet); err != nil { + if err := c.cdc.UnmarshalBinaryLengthPrefixed(bz, &packet); err != nil { panic(err) } diff --git a/x/ibc/mapper.go b/x/ibc/mapper.go index 95c88b62fbc1..957ab191a8a3 100644 --- a/x/ibc/mapper.go +++ b/x/ibc/mapper.go @@ -33,13 +33,13 @@ func (ibcm Mapper) PostIBCPacket(ctx sdk.Context, packet IBCPacket) sdk.Error { // write everything into the state store := ctx.KVStore(ibcm.key) index := ibcm.getEgressLength(store, packet.DestChain) - bz, err := ibcm.cdc.MarshalBinary(packet) + bz, err := ibcm.cdc.MarshalBinaryLengthPrefixed(packet) if err != nil { panic(err) } store.Set(EgressKey(packet.DestChain, index), bz) - bz, err = ibcm.cdc.MarshalBinary(index + 1) + bz, err = ibcm.cdc.MarshalBinaryLengthPrefixed(index + 1) if err != nil { panic(err) } @@ -61,7 +61,7 @@ func (ibcm Mapper) ReceiveIBCPacket(ctx sdk.Context, packet IBCPacket) sdk.Error // Functions for accessing the underlying KVStore. func marshalBinaryPanic(cdc *codec.Codec, value interface{}) []byte { - res, err := cdc.MarshalBinary(value) + res, err := cdc.MarshalBinaryLengthPrefixed(value) if err != nil { panic(err) } @@ -69,7 +69,7 @@ func marshalBinaryPanic(cdc *codec.Codec, value interface{}) []byte { } func unmarshalBinaryPanic(cdc *codec.Codec, bz []byte, ptr interface{}) { - err := cdc.UnmarshalBinary(bz, ptr) + err := cdc.UnmarshalBinaryLengthPrefixed(bz, ptr) if err != nil { panic(err) } diff --git a/x/mint/keeper.go b/x/mint/keeper.go index 6e6a642f48c3..eba4c3fc1875 100644 --- a/x/mint/keeper.go +++ b/x/mint/keeper.go @@ -59,14 +59,14 @@ func (k Keeper) GetMinter(ctx sdk.Context) (minter Minter) { if b == nil { panic("Stored fee pool should not have been nil") } - k.cdc.MustUnmarshalBinary(b, &minter) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &minter) return } // set the minter func (k Keeper) SetMinter(ctx sdk.Context, minter Minter) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(minter) + b := k.cdc.MustMarshalBinaryLengthPrefixed(minter) store.Set(minterKey, b) } diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index 50ccc6c0eaeb..de4fc5d57d9a 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -34,7 +34,7 @@ func GetCmdQuerySigningInfo(storeName string, cdc *codec.Codec) *cobra.Command { } signingInfo := new(slashing.ValidatorSigningInfo) - cdc.MustUnmarshalBinary(res, signingInfo) + cdc.MustUnmarshalBinaryLengthPrefixed(res, signingInfo) switch viper.Get(cli.OutputFlag) { diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index 99db0df63b54..e55b6b9be797 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -45,7 +45,7 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *code var signingInfo slashing.ValidatorSigningInfo - err = cdc.UnmarshalBinary(res, &signingInfo) + err = cdc.UnmarshalBinaryLengthPrefixed(res, &signingInfo) if err != nil { utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return diff --git a/x/slashing/keeper.go b/x/slashing/keeper.go index 647a9df09863..718a024350ee 100644 --- a/x/slashing/keeper.go +++ b/x/slashing/keeper.go @@ -187,7 +187,7 @@ func (k Keeper) addPubkey(ctx sdk.Context, pubkey crypto.PubKey) { func (k Keeper) getPubkey(ctx sdk.Context, address crypto.Address) (crypto.PubKey, error) { store := ctx.KVStore(k.storeKey) var pubkey crypto.PubKey - err := k.cdc.UnmarshalBinary(store.Get(getAddrPubkeyRelationKey(address)), &pubkey) + err := k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(getAddrPubkeyRelationKey(address)), &pubkey) if err != nil { return nil, fmt.Errorf("address %v not found", address) } @@ -196,7 +196,7 @@ func (k Keeper) getPubkey(ctx sdk.Context, address crypto.Address) (crypto.PubKe func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address, pubkey crypto.PubKey) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(pubkey) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(pubkey) store.Set(getAddrPubkeyRelationKey(addr), bz) } diff --git a/x/slashing/signing_info.go b/x/slashing/signing_info.go index 8c35693f1c96..6479be928544 100644 --- a/x/slashing/signing_info.go +++ b/x/slashing/signing_info.go @@ -15,7 +15,7 @@ func (k Keeper) getValidatorSigningInfo(ctx sdk.Context, address sdk.ConsAddress found = false return } - k.cdc.MustUnmarshalBinary(bz, &info) + k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &info) found = true return } @@ -23,7 +23,7 @@ func (k Keeper) getValidatorSigningInfo(ctx sdk.Context, address sdk.ConsAddress // Stored by *validator* address (not operator address) func (k Keeper) setValidatorSigningInfo(ctx sdk.Context, address sdk.ConsAddress, info ValidatorSigningInfo) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(info) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(info) store.Set(GetValidatorSigningInfoKey(address), bz) } @@ -36,14 +36,14 @@ func (k Keeper) getValidatorMissedBlockBitArray(ctx sdk.Context, address sdk.Con missed = false return } - k.cdc.MustUnmarshalBinary(bz, &missed) + k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &missed) return } // Stored by *validator* address (not operator address) func (k Keeper) setValidatorMissedBlockBitArray(ctx sdk.Context, address sdk.ConsAddress, index int64, missed bool) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(missed) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(missed) store.Set(GetValidatorMissedBlockBitArrayKey(address, index), bz) } diff --git a/x/slashing/slashing_period.go b/x/slashing/slashing_period.go index 4b1328858882..026d141a2b7c 100644 --- a/x/slashing/slashing_period.go +++ b/x/slashing/slashing_period.go @@ -61,14 +61,14 @@ func (k Keeper) addOrUpdateValidatorSlashingPeriod(ctx sdk.Context, slashingPeri SlashedSoFar: slashingPeriod.SlashedSoFar, } store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(slashingPeriodValue) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(slashingPeriodValue) store.Set(GetValidatorSlashingPeriodKey(slashingPeriod.ValidatorAddr, slashingPeriod.StartHeight), bz) } // Unmarshal key/value into a ValidatorSlashingPeriod func (k Keeper) unmarshalSlashingPeriodKeyValue(key []byte, value []byte) ValidatorSlashingPeriod { var slashingPeriodValue ValidatorSlashingPeriodValue - k.cdc.MustUnmarshalBinary(value, &slashingPeriodValue) + k.cdc.MustUnmarshalBinaryLengthPrefixed(value, &slashingPeriodValue) address := sdk.ConsAddress(key[1 : 1+sdk.AddrLen]) startHeight := int64(binary.BigEndian.Uint64(key[1+sdk.AddrLen:1+sdk.AddrLen+8]) - uint64(stake.ValidatorUpdateDelay)) return ValidatorSlashingPeriod{ diff --git a/x/stake/handler.go b/x/stake/handler.go index 1d095f3f4d80..328a90133024 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -201,7 +201,7 @@ func handleMsgBeginUnbonding(ctx sdk.Context, msg types.MsgBeginUnbonding, k kee return err.Result() } - finishTime := types.MsgCdc.MustMarshalBinary(ubd.MinTime) + finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(ubd.MinTime) tags := sdk.NewTags( tags.Action, tags.ActionBeginUnbonding, @@ -219,7 +219,7 @@ func handleMsgBeginRedelegate(ctx sdk.Context, msg types.MsgBeginRedelegate, k k return err.Result() } - finishTime := types.MsgCdc.MustMarshalBinary(red.MinTime) + finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(red.MinTime) tags := sdk.NewTags( tags.Action, tags.ActionBeginRedelegation, diff --git a/x/stake/handler_test.go b/x/stake/handler_test.go index c4a558b91609..e5c78bcf6fe6 100644 --- a/x/stake/handler_test.go +++ b/x/stake/handler_test.go @@ -94,7 +94,7 @@ func TestValidatorByPowerIndex(t *testing.T) { got = handleMsgBeginUnbonding(ctx, msgBeginUnbonding, keeper) require.True(t, got.IsOK(), "expected msg to be ok, got %v", got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -232,7 +232,7 @@ func TestLegacyValidatorDelegations(t *testing.T) { require.True(t, got.IsOK(), "expected begin unbonding validator msg to be ok, got %v", got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -400,7 +400,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { got := handleMsgBeginUnbonding(ctx, msgBeginUnbonding, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -494,7 +494,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) { got := handleMsgBeginUnbonding(ctx, msgBeginUnbonding, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -540,7 +540,7 @@ func TestMultipleMsgDelegate(t *testing.T) { got := handleMsgBeginUnbonding(ctx, msgBeginUnbonding, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -570,7 +570,7 @@ func TestJailValidator(t *testing.T) { got = handleMsgBeginUnbonding(ctx, msgBeginUnbondingValidator, keeper) require.True(t, got.IsOK(), "expected no error: %v", got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -586,7 +586,7 @@ func TestJailValidator(t *testing.T) { msgBeginUnbondingDelegator := NewMsgBeginUnbonding(delegatorAddr, validatorAddr, sdk.NewDec(10)) got = handleMsgBeginUnbonding(ctx, msgBeginUnbondingDelegator, keeper) require.True(t, got.IsOK(), "expected no error") - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) @@ -621,7 +621,7 @@ func TestValidatorQueue(t *testing.T) { got = handleMsgBeginUnbonding(ctx, msgBeginUnbondingValidator, keeper) require.True(t, got.IsOK(), "expected no error: %v", got) var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime) EndBlocker(ctx, keeper) origHeader := ctx.BlockHeader() @@ -709,7 +709,7 @@ func TestUnbondingFromUnbondingValidator(t *testing.T) { // change the ctx to Block Time one second before the validator would have unbonded var finishTime time.Time - types.MsgCdc.MustUnmarshalBinary(got.Data, &finishTime) + types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime) ctx = ctx.WithBlockTime(finishTime.Add(time.Second * -1)) // unbond the delegator from the validator diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index a5d08d4892a9..2791dff7a4e2 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -163,14 +163,14 @@ func (k Keeper) GetUnbondingQueueTimeSlice(ctx sdk.Context, timestamp time.Time) if bz == nil { return []types.DVPair{} } - k.cdc.MustUnmarshalBinary(bz, &dvPairs) + k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &dvPairs) return dvPairs } // Sets a specific unbonding queue timeslice. func (k Keeper) SetUnbondingQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []types.DVPair) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(keys) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(keys) store.Set(GetUnbondingDelegationTimeKey(timestamp), bz) } @@ -199,7 +199,7 @@ func (k Keeper) DequeueAllMatureUnbondingQueue(ctx sdk.Context, currTime time.Ti unbondingTimesliceIterator := k.UnbondingQueueIterator(ctx, ctx.BlockHeader().Time) for ; unbondingTimesliceIterator.Valid(); unbondingTimesliceIterator.Next() { timeslice := []types.DVPair{} - k.cdc.MustUnmarshalBinary(unbondingTimesliceIterator.Value(), ×lice) + k.cdc.MustUnmarshalBinaryLengthPrefixed(unbondingTimesliceIterator.Value(), ×lice) matureUnbonds = append(matureUnbonds, timeslice...) store.Delete(unbondingTimesliceIterator.Key()) } @@ -300,14 +300,14 @@ func (k Keeper) GetRedelegationQueueTimeSlice(ctx sdk.Context, timestamp time.Ti if bz == nil { return []types.DVVTriplet{} } - k.cdc.MustUnmarshalBinary(bz, &dvvTriplets) + k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &dvvTriplets) return dvvTriplets } // Sets a specific redelegation queue timeslice. func (k Keeper) SetRedelegationQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []types.DVVTriplet) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(keys) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(keys) store.Set(GetRedelegationTimeKey(timestamp), bz) } @@ -336,7 +336,7 @@ func (k Keeper) DequeueAllMatureRedelegationQueue(ctx sdk.Context, currTime time redelegationTimesliceIterator := k.RedelegationQueueIterator(ctx, ctx.BlockHeader().Time) for ; redelegationTimesliceIterator.Valid(); redelegationTimesliceIterator.Next() { timeslice := []types.DVVTriplet{} - k.cdc.MustUnmarshalBinary(redelegationTimesliceIterator.Value(), ×lice) + k.cdc.MustUnmarshalBinaryLengthPrefixed(redelegationTimesliceIterator.Value(), ×lice) matureRedelegations = append(matureRedelegations, timeslice...) store.Delete(redelegationTimesliceIterator.Key()) } diff --git a/x/stake/keeper/keeper.go b/x/stake/keeper/keeper.go index 17ee31268cff..b61ead54c985 100644 --- a/x/stake/keeper/keeper.go +++ b/x/stake/keeper/keeper.go @@ -60,14 +60,14 @@ func (k Keeper) GetPool(ctx sdk.Context) (pool types.Pool) { if b == nil { panic("stored pool should not have been nil") } - k.cdc.MustUnmarshalBinary(b, &pool) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &pool) return } // set the pool func (k Keeper) SetPool(ctx sdk.Context, pool types.Pool) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(pool) + b := k.cdc.MustMarshalBinaryLengthPrefixed(pool) store.Set(PoolKey, b) } @@ -80,14 +80,14 @@ func (k Keeper) GetLastTotalPower(ctx sdk.Context) (power sdk.Int) { if b == nil { return sdk.ZeroInt() } - k.cdc.MustUnmarshalBinary(b, &power) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &power) return } // Set the last total validator power. func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(power) + b := k.cdc.MustMarshalBinaryLengthPrefixed(power) store.Set(LastTotalPowerKey, b) } @@ -101,14 +101,14 @@ func (k Keeper) GetLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress) if bz == nil { return sdk.ZeroInt() } - k.cdc.MustUnmarshalBinary(bz, &power) + k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &power) return } // Set the last validator power. func (k Keeper) SetLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress, power sdk.Int) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(power) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(power) store.Set(GetLastValidatorPowerKey(operator), bz) } @@ -128,13 +128,13 @@ func (k Keeper) GetIntraTxCounter(ctx sdk.Context) int16 { return 0 } var counter int16 - k.cdc.MustUnmarshalBinary(b, &counter) + k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &counter) return counter } // set the current in-block validator operation counter func (k Keeper) SetIntraTxCounter(ctx sdk.Context, counter int16) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(counter) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(counter) store.Set(IntraTxCounterKey, bz) } diff --git a/x/stake/keeper/val_state_change.go b/x/stake/keeper/val_state_change.go index ea057c0fddcf..c259fe0254f0 100644 --- a/x/stake/keeper/val_state_change.go +++ b/x/stake/keeper/val_state_change.go @@ -73,7 +73,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab // calculate the new power bytes newPower := validator.BondedTokens().RoundInt64() - newPowerBytes := k.cdc.MustMarshalBinary(sdk.NewInt(newPower)) + newPowerBytes := k.cdc.MustMarshalBinaryLengthPrefixed(sdk.NewInt(newPower)) // update the validator set if power has changed if !found || !bytes.Equal(oldPowerBytes, newPowerBytes) { updates = append(updates, validator.ABCIValidatorUpdate()) diff --git a/x/stake/keeper/validator.go b/x/stake/keeper/validator.go index 9fd7434d313a..5770ada2ff8a 100644 --- a/x/stake/keeper/validator.go +++ b/x/stake/keeper/validator.go @@ -291,14 +291,14 @@ func (k Keeper) GetValidatorQueueTimeSlice(ctx sdk.Context, timestamp time.Time) if bz == nil { return []sdk.ValAddress{} } - k.cdc.MustUnmarshalBinary(bz, &valAddrs) + k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &valAddrs) return valAddrs } // Sets a specific validator queue timeslice. func (k Keeper) SetValidatorQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []sdk.ValAddress) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(keys) + bz := k.cdc.MustMarshalBinaryLengthPrefixed(keys) store.Set(GetValidatorQueueTimeKey(timestamp), bz) } @@ -325,7 +325,7 @@ func (k Keeper) GetAllMatureValidatorQueue(ctx sdk.Context, currTime time.Time) validatorTimesliceIterator := k.ValidatorQueueIterator(ctx, ctx.BlockHeader().Time) for ; validatorTimesliceIterator.Valid(); validatorTimesliceIterator.Next() { timeslice := []sdk.ValAddress{} - k.cdc.MustUnmarshalBinary(validatorTimesliceIterator.Value(), ×lice) + k.cdc.MustUnmarshalBinaryLengthPrefixed(validatorTimesliceIterator.Value(), ×lice) matureValsAddrs = append(matureValsAddrs, timeslice...) } return matureValsAddrs @@ -337,7 +337,7 @@ func (k Keeper) UnbondAllMatureValidatorQueue(ctx sdk.Context) { validatorTimesliceIterator := k.ValidatorQueueIterator(ctx, ctx.BlockHeader().Time) for ; validatorTimesliceIterator.Valid(); validatorTimesliceIterator.Next() { timeslice := []sdk.ValAddress{} - k.cdc.MustUnmarshalBinary(validatorTimesliceIterator.Value(), ×lice) + k.cdc.MustUnmarshalBinaryLengthPrefixed(validatorTimesliceIterator.Value(), ×lice) for _, valAddr := range timeslice { val, found := k.GetValidator(ctx, valAddr) if !found || val.GetStatus() != sdk.Unbonding { diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 88b67018d828..e7315542741d 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -47,7 +47,7 @@ func MustMarshalDelegation(cdc *codec.Codec, delegation Delegation) []byte { delegation.Shares, delegation.Height, } - return cdc.MustMarshalBinary(val) + return cdc.MustMarshalBinaryLengthPrefixed(val) } // return the delegation without fields contained within the key for the store @@ -62,7 +62,7 @@ func MustUnmarshalDelegation(cdc *codec.Codec, key, value []byte) Delegation { // return the delegation without fields contained within the key for the store func UnmarshalDelegation(cdc *codec.Codec, key, value []byte) (delegation Delegation, err error) { var storeValue delegationValue - err = cdc.UnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue) if err != nil { err = fmt.Errorf("%v: %v", ErrNoDelegation(DefaultCodespace).Data(), err) return @@ -139,7 +139,7 @@ func MustMarshalUBD(cdc *codec.Codec, ubd UnbondingDelegation) []byte { ubd.InitialBalance, ubd.Balance, } - return cdc.MustMarshalBinary(val) + return cdc.MustMarshalBinaryLengthPrefixed(val) } // unmarshal a unbonding delegation from a store key and value @@ -154,7 +154,7 @@ func MustUnmarshalUBD(cdc *codec.Codec, key, value []byte) UnbondingDelegation { // unmarshal a unbonding delegation from a store key and value func UnmarshalUBD(cdc *codec.Codec, key, value []byte) (ubd UnbondingDelegation, err error) { var storeValue ubdValue - err = cdc.UnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue) if err != nil { return } @@ -179,8 +179,8 @@ func UnmarshalUBD(cdc *codec.Codec, key, value []byte) (ubd UnbondingDelegation, // nolint func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool { - bz1 := MsgCdc.MustMarshalBinary(&d) - bz2 := MsgCdc.MustMarshalBinary(&d2) + bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d) + bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d2) return bytes.Equal(bz1, bz2) } @@ -231,7 +231,7 @@ func MustMarshalRED(cdc *codec.Codec, red Redelegation) []byte { red.SharesSrc, red.SharesDst, } - return cdc.MustMarshalBinary(val) + return cdc.MustMarshalBinaryLengthPrefixed(val) } // unmarshal a redelegation from a store key and value @@ -246,7 +246,7 @@ func MustUnmarshalRED(cdc *codec.Codec, key, value []byte) Redelegation { // unmarshal a redelegation from a store key and value func UnmarshalRED(cdc *codec.Codec, key, value []byte) (red Redelegation, err error) { var storeValue redValue - err = cdc.UnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue) if err != nil { return } @@ -275,8 +275,8 @@ func UnmarshalRED(cdc *codec.Codec, key, value []byte) (red Redelegation, err er // nolint func (d Redelegation) Equal(d2 Redelegation) bool { - bz1 := MsgCdc.MustMarshalBinary(&d) - bz2 := MsgCdc.MustMarshalBinary(&d2) + bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d) + bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d2) return bytes.Equal(bz1, bz2) } diff --git a/x/stake/types/params.go b/x/stake/types/params.go index 5915570c12ab..699758ace439 100644 --- a/x/stake/types/params.go +++ b/x/stake/types/params.go @@ -49,8 +49,8 @@ func (p *Params) KeyValuePairs() params.KeyValuePairs { // Equal returns a boolean determining if two Param types are identical. func (p Params) Equal(p2 Params) bool { - bz1 := MsgCdc.MustMarshalBinary(&p) - bz2 := MsgCdc.MustMarshalBinary(&p2) + bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p) + bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p2) return bytes.Equal(bz1, bz2) } @@ -85,7 +85,7 @@ func MustUnmarshalParams(cdc *codec.Codec, value []byte) Params { // unmarshal the current staking params value from store key func UnmarshalParams(cdc *codec.Codec, value []byte) (params Params, err error) { - err = cdc.UnmarshalBinary(value, ¶ms) + err = cdc.UnmarshalBinaryLengthPrefixed(value, ¶ms) if err != nil { return } diff --git a/x/stake/types/pool.go b/x/stake/types/pool.go index e2015dcafc7c..4b227aa8051e 100644 --- a/x/stake/types/pool.go +++ b/x/stake/types/pool.go @@ -16,8 +16,8 @@ type Pool struct { // nolint func (p Pool) Equal(p2 Pool) bool { - bz1 := MsgCdc.MustMarshalBinary(&p) - bz2 := MsgCdc.MustMarshalBinary(&p2) + bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p) + bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p2) return bytes.Equal(bz1, bz2) } @@ -90,7 +90,7 @@ func MustUnmarshalPool(cdc *codec.Codec, value []byte) Pool { // unmarshal the current pool value from store key func UnmarshalPool(cdc *codec.Codec, value []byte) (pool Pool, err error) { - err = cdc.UnmarshalBinary(value, &pool) + err = cdc.UnmarshalBinaryLengthPrefixed(value, &pool) if err != nil { return } diff --git a/x/stake/types/validator.go b/x/stake/types/validator.go index 52d30f0a313b..9301da3ecef5 100644 --- a/x/stake/types/validator.go +++ b/x/stake/types/validator.go @@ -87,7 +87,7 @@ func MustMarshalValidator(cdc *codec.Codec, validator Validator) []byte { UnbondingMinTime: validator.UnbondingMinTime, Commission: validator.Commission, } - return cdc.MustMarshalBinary(val) + return cdc.MustMarshalBinaryLengthPrefixed(val) } // unmarshal a redelegation from a store key and value @@ -106,7 +106,7 @@ func UnmarshalValidator(cdc *codec.Codec, operatorAddr, value []byte) (validator return } var storeValue validatorValue - err = cdc.UnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue) if err != nil { return } From 6e13a7278124d08ddc601b0ffa94ee7d52e6786f Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 30 Oct 2018 23:46:32 -0700 Subject: [PATCH 2/2] Add rootmultistore proof tests; Fix general merkle keypath element ordering --- Gopkg.lock | 14 ++--- Gopkg.toml | 2 +- store/multistoreproof.go | 1 + store/multistoreproof_test.go | 100 ++++++++++++++++++++++++++-------- store/rootmultistore.go | 1 + 5 files changed, 83 insertions(+), 35 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index fa6648168313..637b6b207bf2 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -63,13 +63,6 @@ revision = "346938d642f2ec3594ed81d874461961cd0faa76" version = "v1.1.0" -[[projects]] - digest = "1:c7644c73a3d23741fdba8a99b1464e021a224b7e205be497271a8003a15ca41b" - name = "github.com/ebuchman/fail-test" - packages = ["."] - pruneopts = "UT" - revision = "95f809107225be108efcf10a3509e4ea6ceef3c4" - [[projects]] digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd" name = "github.com/fsnotify/fsnotify" @@ -441,7 +434,8 @@ version = "v0.11.1" [[projects]] - digest = "1:6ebe52fa977e5be2afdb8c8303e343b559bec3426b185bacf6d9b371cc1bcc2b" + branch = "jae/generalmerklefix" + digest = "1:fbc7b3a2fbd5ed000fd0cef6c9a0af9ef2cc1fe8149af5075047c637e328f7d1" name = "github.com/tendermint/tendermint" packages = [ "abci/client", @@ -474,6 +468,7 @@ "libs/db", "libs/errors", "libs/events", + "libs/fail", "libs/flowrate", "libs/log", "libs/pubsub", @@ -506,8 +501,7 @@ "version", ] pruneopts = "UT" - revision = "bc0d672b26dbbd6858e97a570c87e3c20cbf199e" - version = "v0.26.0-rc0" + revision = "8e959589a1b05d620341adab0c571c652421506f" [[projects]] digest = "1:7886f86064faff6f8d08a3eb0e8c773648ff5a2e27730831e2bfbf07467f6666" diff --git a/Gopkg.toml b/Gopkg.toml index 5ba48e2793b8..dca469914ebd 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -57,7 +57,7 @@ [[override]] name = "github.com/tendermint/tendermint" - version = "=v0.26.0-rc0" + branch = "jae/generalmerklefix" ## deps without releases: diff --git a/store/multistoreproof.go b/store/multistoreproof.go index ef030c092fae..f8654aea50ff 100644 --- a/store/multistoreproof.go +++ b/store/multistoreproof.go @@ -118,5 +118,6 @@ func DefaultProofRuntime() (prt *merkle.ProofRuntime) { prt.RegisterOpDecoder(merkle.ProofOpSimpleValue, merkle.SimpleValueOpDecoder) prt.RegisterOpDecoder(iavl.ProofOpIAVLValue, iavl.IAVLValueOpDecoder) prt.RegisterOpDecoder(iavl.ProofOpIAVLAbsence, iavl.IAVLAbsenceOpDecoder) + prt.RegisterOpDecoder(ProofOpMultiStoreProof, MultiStoreProofOpDecoder) return } diff --git a/store/multistoreproof_test.go b/store/multistoreproof_test.go index ed0a3af3991e..f5f5423181f4 100644 --- a/store/multistoreproof_test.go +++ b/store/multistoreproof_test.go @@ -1,17 +1,15 @@ package store import ( - "fmt" "testing" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" dbm "github.com/tendermint/tendermint/libs/db" ) -func TestVerifyMultiStoreQueryProof(t *testing.T) { +func TestVerifyIAVLStoreQueryProof(t *testing.T) { // Create main tree for testing. db := dbm.NewMemDB() @@ -21,36 +19,90 @@ func TestVerifyMultiStoreQueryProof(t *testing.T) { store.Set([]byte("MYKEY"), []byte("MYVALUE")) cid := store.Commit() - /* - var storeInfos = []storeInfo{ - storeInfo{ - Name: "tree", - Core: storeCore{ - CommitID: cid, - }, - }, - storeInfo{ - Name: "otherTree", - Core: storeCore{ - CommitID: CommitID{ - Version: 689, - Hash: []byte("otherHash"), - }, - }, - }, - } - */ - // Get Proof res := store.Query(abci.RequestQuery{ Path: "/key", // required path to get key/value+proof Data: []byte("MYKEY"), Prove: true, }) - fmt.Println("result", spew.Sdump(res)) require.NotNil(t, res.Proof) + // Verify proof. prt := DefaultProofRuntime() err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE")) require.Nil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY_NOT", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY/MYKEY", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "MYKEY", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE_NOT")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte(nil)) + require.NotNil(t, err) +} + +func TestVerifyMultiStoreQueryProof(t *testing.T) { + + // Create main tree for testing. + db := dbm.NewMemDB() + store := NewCommitMultiStore(db) + iavlStoreKey := sdk.NewKVStoreKey("iavlStoreKey") + store.MountStoreWithDB(iavlStoreKey, sdk.StoreTypeIAVL, nil) + store.LoadVersion(0) + iavlStore := store.GetCommitStore(iavlStoreKey).(*iavlStore) + iavlStore.Set([]byte("MYKEY"), []byte("MYVALUE")) + cid := store.Commit() + + // Get Proof + res := store.Query(abci.RequestQuery{ + Path: "/iavlStoreKey/key", // required path to get key/value+proof + Data: []byte("MYKEY"), + Prove: true, + }) + require.NotNil(t, res.Proof) + + // Verify proof. + prt := DefaultProofRuntime() + err := prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE")) + require.Nil(t, err) + + // Verify proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE")) + require.Nil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY_NOT", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY/MYKEY", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "iavlStoreKey/MYKEY", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE_NOT")) + require.NotNil(t, err) + + // Verify (bad) proof. + err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte(nil)) + require.NotNil(t, err) } diff --git a/store/rootmultistore.go b/store/rootmultistore.go index 27b983c19fa2..fd3736a9e211 100644 --- a/store/rootmultistore.go +++ b/store/rootmultistore.go @@ -300,6 +300,7 @@ func (rs *rootMultiStore) Query(req abci.RequestQuery) abci.ResponseQuery { return sdk.ErrInternal(errMsg.Error()).QueryResult() } + // Restore origin path and append proof op. res.Proof.Ops = append(res.Proof.Ops, NewMultiStoreProofOp( []byte(storeName), NewMultiStoreProof(commitInfo.StoreInfos),