From ad520ed1d35df386ed0009f7441cba64e5139215 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Mon, 20 Jul 2020 17:22:10 +0200 Subject: [PATCH] Return init/migration msg with contract history --- api_migration.md | 36 ++++++++++++++++++++++++++ x/wasm/internal/keeper/keeper.go | 7 +---- x/wasm/internal/keeper/querier.go | 1 - x/wasm/internal/keeper/querier_test.go | 4 +++ x/wasm/internal/types/types.go | 1 + 5 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 api_migration.md diff --git a/api_migration.md b/api_migration.md new file mode 100644 index 00000000..888dc10d --- /dev/null +++ b/api_migration.md @@ -0,0 +1,36 @@ +# Changes to the api + +## [\#196](https://github.com/CosmWasm/wasmd/issues/196) - Move history of contract code migrations to their own prefix store + +The `ContractDetails.initMsg` used in cosmJs was moved into a new entity `ContractCodeHistoryEntry`. They contain code updates to a contract. + +### Route +This data is available via a new route `/wasm/contract/{contractAddr}/history` + +### Response +A list of ContractCodeHistoryEntries with following fields: +* `operation` can be any of `"Init", "Migrate", "Genesis"` +* `code_id` uint64 +* `msg` as raw json + +### Errors +* 404 - for an unknown contract + +### CLI +`wasmcli query wasm contract-history [bech32_address] to print all the code changes.` +Example: +`wasmcli query wasm contract-history cosmos18r5szma8hm93pvx6lwpjwyxruw27e0k5uw835c` +```json +[ + { + "operation": "Init", + "code_id": 1, + "msg": "\"init-msg\"" + }, + { + "operation": "Migrate", + "code_id": 2, + "msg": "\"migrate-msg\"" + } +] +``` diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index 76184594..a84c86a0 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -378,13 +378,8 @@ func (k Keeper) setContractAdmin(ctx sdk.Context, contractAddress, caller, newAd } func (k Keeper) appendToContractHistory(ctx sdk.Context, contractAddr sdk.AccAddress, newEntries ...types.ContractCodeHistoryEntry) { + entries := append(k.GetContractHistory(ctx, contractAddr), newEntries...) prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.ContractHistoryStorePrefix) - var entries []types.ContractCodeHistoryEntry - bz := prefixStore.Get(contractAddr) - if bz != nil { - k.cdc.MustUnmarshalBinaryBare(bz, &entries) - } - entries = append(entries, newEntries...) prefixStore.Set(contractAddr, k.cdc.MustMarshalBinaryBare(&entries)) } diff --git a/x/wasm/internal/keeper/querier.go b/x/wasm/internal/keeper/querier.go index a3eef62a..bd880189 100644 --- a/x/wasm/internal/keeper/querier.go +++ b/x/wasm/internal/keeper/querier.go @@ -240,7 +240,6 @@ func queryContractHistory(ctx sdk.Context, bech string, keeper Keeper) ([]byte, // redact response for i := range entries { entries[i].Updated = nil - entries[i].Msg = nil } bz, err := json.MarshalIndent(entries, "", " ") diff --git a/x/wasm/internal/keeper/querier_test.go b/x/wasm/internal/keeper/querier_test.go index 7420aded..599bd39d 100644 --- a/x/wasm/internal/keeper/querier_test.go +++ b/x/wasm/internal/keeper/querier_test.go @@ -244,6 +244,7 @@ func TestQueryContractHistory(t *testing.T) { expContent: []types.ContractCodeHistoryEntry{{ Operation: types.GenesisContractCodeHistoryType, CodeID: 1, + Msg: []byte(`"init message"`), }}, }, "response with multiple entries": { @@ -266,12 +267,15 @@ func TestQueryContractHistory(t *testing.T) { expContent: []types.ContractCodeHistoryEntry{{ Operation: types.InitContractCodeHistoryType, CodeID: 1, + Msg: []byte(`"init message"`), }, { Operation: types.MigrateContractCodeHistoryType, CodeID: 2, + Msg: []byte(`"migrate message 1"`), }, { Operation: types.MigrateContractCodeHistoryType, CodeID: 3, + Msg: []byte(`"migrate message 2"`), }}, }, "unknown contract address": { diff --git a/x/wasm/internal/types/types.go b/x/wasm/internal/types/types.go index cb9469ea..50414b37 100644 --- a/x/wasm/internal/types/types.go +++ b/x/wasm/internal/types/types.go @@ -77,6 +77,7 @@ const ( var AllCodeHistoryTypes = []ContractCodeHistoryOperationType{InitContractCodeHistoryType, MigrateContractCodeHistoryType} +// ContractCodeHistoryEntry stores code updates to a contract. type ContractCodeHistoryEntry struct { Operation ContractCodeHistoryOperationType `json:"operation"` CodeID uint64 `json:"code_id"`