Skip to content

Commit

Permalink
Change genesis preserving contract history (CosmWasm#1076)
Browse files Browse the repository at this point in the history
* preserve contract created date on genesis import and add query contract created date

* add validate created

* fix sims test app import export

* add preserve contract history

* Make proto-all only

* Remove ResetFromGenesis

* Add validation

Co-authored-by: Alex Peters <alpe@users.noreply.github.com>
  • Loading branch information
2 people authored and conorpp committed Feb 1, 2023
1 parent 1c5ad5d commit 5b27260
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 172 deletions.
34 changes: 0 additions & 34 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/prefix"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
Expand Down Expand Up @@ -196,37 +193,6 @@ func TestAppImportExport(t *testing.T) {
// delete persistent tx counter value
ctxA.KVStore(app.keys[wasm.StoreKey]).Delete(wasmtypes.TXCounterPrefix)

// reset contract code index in source DB for comparison with dest DB
dropContractHistory := func(s store.KVStore, keys ...[]byte) {
for _, key := range keys {
prefixStore := prefix.NewStore(s, key)
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
prefixStore.Delete(iter.Key())
}
iter.Close()
}
}
prefixes := [][]byte{wasmtypes.ContractCodeHistoryElementPrefix, wasmtypes.ContractByCodeIDAndCreatedSecondaryIndexPrefix}
dropContractHistory(ctxA.KVStore(app.keys[wasm.StoreKey]), prefixes...)
dropContractHistory(ctxB.KVStore(newApp.keys[wasm.StoreKey]), prefixes...)

normalizeContractInfo := func(ctx sdk.Context, app *WasmApp) {
var index uint64
app.WasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info wasmtypes.ContractInfo) bool {
created := &wasmtypes.AbsoluteTxPosition{
BlockHeight: uint64(0),
TxIndex: index,
}
info.Created = created
store := ctx.KVStore(app.keys[wasm.StoreKey])
store.Set(wasmtypes.GetContractAddressKey(address), app.appCodec.MustMarshal(&info))
index++
return false
})
}
normalizeContractInfo(ctxA, app)
normalizeContractInfo(ctxB, newApp)
// diff both stores
for _, skp := range storeKeysPrefixes {
storeA := ctxA.KVStore(skp.A)
Expand Down
3 changes: 2 additions & 1 deletion docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ ContractInfo stores a WASM contract instance
| `creator` | [string](#string) | | Creator address who initially instantiated the contract |
| `admin` | [string](#string) | | Admin is an optional address that can execute migrations |
| `label` | [string](#string) | | Label is optional metadata to be stored with a contract instance. |
| `created` | [AbsoluteTxPosition](#cosmwasm.wasm.v1.AbsoluteTxPosition) | | Created Tx position when the contract was instantiated. This data should kept internal and not be exposed via query results. Just use for sorting |
| `created` | [AbsoluteTxPosition](#cosmwasm.wasm.v1.AbsoluteTxPosition) | | Created Tx position when the contract was instantiated. |
| `ibc_port_id` | [string](#string) | | |
| `extension` | [google.protobuf.Any](#google.protobuf.Any) | | Extension is an extension point to store custom metadata within the persistence model. |

Expand Down Expand Up @@ -568,6 +568,7 @@ Contract struct encompasses ContractAddress, ContractInfo, and ContractState
| `contract_address` | [string](#string) | | |
| `contract_info` | [ContractInfo](#cosmwasm.wasm.v1.ContractInfo) | | |
| `contract_state` | [Model](#cosmwasm.wasm.v1.Model) | repeated | |
| `contract_code_history` | [ContractCodeHistoryEntry](#cosmwasm.wasm.v1.ContractCodeHistoryEntry) | repeated | |



Expand Down
2 changes: 2 additions & 0 deletions proto/cosmwasm/wasm/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ message Contract {
string contract_address = 1;
ContractInfo contract_info = 2 [ (gogoproto.nullable) = false ];
repeated Model contract_state = 3 [ (gogoproto.nullable) = false ];
repeated ContractCodeHistoryEntry contract_code_history = 4
[ (gogoproto.nullable) = false ];
}

// Sequence key and value of an id generation counter
Expand Down
2 changes: 0 additions & 2 deletions proto/cosmwasm/wasm/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ message ContractInfo {
// Label is optional metadata to be stored with a contract instance.
string label = 4;
// Created Tx position when the contract was instantiated.
// This data should kept internal and not be exposed via query results. Just
// use for sorting
AbsoluteTxPosition created = 5;
string ibc_port_id = 6 [ (gogoproto.customname) = "IBCPortID" ];

Expand Down
36 changes: 20 additions & 16 deletions x/wasm/client/cli/genesis_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,11 @@ func TestExecuteContractCmd(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: firstContractAddress,
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil
}),
ContractState: []types.Model{},
ContractInfo: types.ContractInfoFixture(),
ContractState: []types.Model{},
ContractCodeHistory: []types.ContractCodeHistoryEntry{
types.ContractCodeHistoryEntryFixture(),
},
},
},
},
Expand Down Expand Up @@ -473,10 +474,11 @@ func TestExecuteContractCmd(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: firstContractAddress,
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil
}),
ContractState: []types.Model{},
ContractInfo: types.ContractInfoFixture(),
ContractState: []types.Model{},
ContractCodeHistory: []types.ContractCodeHistoryEntry{
types.ContractCodeHistoryEntryFixture(),
},
},
},
},
Expand All @@ -500,10 +502,11 @@ func TestExecuteContractCmd(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: firstContractAddress,
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil
}),
ContractState: []types.Model{},
ContractInfo: types.ContractInfoFixture(),
ContractState: []types.Model{},
ContractCodeHistory: []types.ContractCodeHistoryEntry{
types.ContractCodeHistoryEntryFixture(),
},
},
},
},
Expand All @@ -528,10 +531,11 @@ func TestExecuteContractCmd(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: firstContractAddress,
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil
}),
ContractState: []types.Model{},
ContractInfo: types.ContractInfoFixture(),
ContractState: []types.Model{},
ContractCodeHistory: []types.ContractCodeHistoryEntry{
types.ContractCodeHistoryEntryFixture(),
},
},
},
},
Expand Down
14 changes: 8 additions & 6 deletions x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState, staki
if err != nil {
return nil, sdkerrors.Wrapf(err, "address in contract number %d", i)
}
err = keeper.importContract(ctx, contractAddr, &contract.ContractInfo, contract.ContractState)
err = keeper.importContract(ctx, contractAddr, &contract.ContractInfo, contract.ContractState, contract.ContractCodeHistory)
if err != nil {
return nil, sdkerrors.Wrapf(err, "contract number %d", i)
}
Expand Down Expand Up @@ -107,12 +107,14 @@ func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState {
state = append(state, types.Model{Key: key, Value: value})
return false
})
// redact contract info
contract.Created = nil

contractCodeHistory := keeper.GetContractHistory(ctx, addr)

genState.Contracts = append(genState.Contracts, types.Contract{
ContractAddress: addr.String(),
ContractInfo: contract,
ContractState: state,
ContractAddress: addr.String(),
ContractInfo: contract,
ContractState: state,
ContractCodeHistory: contractCodeHistory,
})
return false
})
Expand Down
Loading

0 comments on commit 5b27260

Please sign in to comment.