Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change genesis preserving contract history #1076

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func TestAppImportExport(t *testing.T) {
// delete persistent tx counter value
ctxA.KVStore(app.keys[wasm.StoreKey]).Delete(wasmtypes.TXCounterPrefix)

// still need to drop contract history. We not recovery the ContractCodeHistoryEntry.Msg
// reset contract code index in source DB for comparison with dest DB
dropContractHistory := func(s store.KVStore, keys ...[]byte) {
for _, key := range keys {
Expand All @@ -211,22 +212,6 @@ func TestAppImportExport(t *testing.T) {
dropContractHistory(ctxA.KVStore(app.keys[wasm.StoreKey]), prefixes...)
dropContractHistory(ctxB.KVStore(newApp.keys[wasm.StoreKey]), prefixes...)

normalizeContractInfo := func(ctx sdk.Context, app *WasmApp) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
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
24 changes: 8 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,8 @@ 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{},
},
},
},
Expand Down Expand Up @@ -473,10 +471,8 @@ 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{},
},
},
},
Expand All @@ -500,10 +496,8 @@ 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{},
},
},
},
Expand All @@ -528,10 +522,8 @@ 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{},
},
},
},
Expand Down
2 changes: 0 additions & 2 deletions x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

genState.Contracts = append(genState.Contracts, types.Contract{
ContractAddress: addr.String(),
ContractInfo: contract,
Expand Down
29 changes: 18 additions & 11 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestGenesisInit(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
},
},
Sequences: []types.Sequence{
Expand All @@ -293,10 +293,10 @@ func TestGenesisInit(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
}, {
ContractAddress: BuildContractAddressClassic(1, 2).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
},
},
Sequences: []types.Sequence{
Expand All @@ -312,7 +312,7 @@ func TestGenesisInit(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
},
},
Params: types.DefaultParams(),
Expand All @@ -328,10 +328,10 @@ func TestGenesisInit(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
}, {
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
},
},
Params: types.DefaultParams(),
Expand All @@ -347,7 +347,7 @@ func TestGenesisInit(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
ContractState: []types.Model{
{
Key: []byte{0x1},
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestGenesisInit(t *testing.T) {
Contracts: []types.Contract{
{
ContractAddress: BuildContractAddressClassic(1, 1).String(),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.OnlyGenesisFields),
ContractInfo: types.ContractInfoFixture(func(c *wasmTypes.ContractInfo) { c.CodeID = 1 }, types.RandCreatedFields),
},
},
Sequences: []types.Sequence{
Expand Down Expand Up @@ -489,7 +489,11 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
"code_id": "1",
"creator": "cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x",
"admin": "cosmos1h5t8zxmjr30e9dqghtlpl40f2zz5cgey6esxtn",
"label": "ȀĴnZV芢毤"
"label": "ȀĴnZV芢毤",
"created": {
"block_height" : "100",
"tx_index" : "10"
}
}
}
],
Expand Down Expand Up @@ -550,15 +554,18 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
Creator: contractCreatorAddr,
Admin: adminAddr,
Label: "ȀĴnZV芢毤",
Created: &types.AbsoluteTxPosition{BlockHeight: 0, TxIndex: 0},
Created: &types.AbsoluteTxPosition{BlockHeight: 100, TxIndex: 10},
}
assert.Equal(t, expContractInfo, *gotContractInfo)

expHistory := []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeGenesis,
CodeID: firstCodeID,
Updated: types.NewAbsoluteTxPosition(ctx),
Updated: &types.AbsoluteTxPosition{
BlockHeight: 100,
TxIndex: 10,
},
},
}
assert.Equal(t, expHistory, keeper.GetContractHistory(ctx, contractAddr))
Expand Down
1 change: 1 addition & 0 deletions x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func TestMigrateProposal(t *testing.T) {
contractInfoFixture := types.ContractInfoFixture(func(c *types.ContractInfo) {
c.Label = "testing"
c.Admin = anyAddress.String()
c.Created = types.NewAbsoluteTxPosition(ctx)
})
key, err := hex.DecodeString("636F6E666967")
require.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions x/wasm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ func queryContractInfo(ctx sdk.Context, addr sdk.AccAddress, keeper types.ViewKe
if info == nil {
return nil, types.ErrNotFound
}
// redact the Created field (just used for sorting, not part of public API)
info.Created = nil
return &types.QueryContractInfoResponse{
Address: addr.String(),
ContractInfo: *info,
Expand Down
12 changes: 4 additions & 8 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,20 +560,16 @@ func TestQueryContractInfo(t *testing.T) {
src: &types.QueryContractInfoRequest{Address: contractAddr.String()},
stored: types.ContractInfoFixture(),
expRsp: &types.QueryContractInfoResponse{
Address: contractAddr.String(),
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil // not returned on queries
}),
Address: contractAddr.String(),
ContractInfo: types.ContractInfoFixture(),
},
},
"with extension": {
src: &types.QueryContractInfoRequest{Address: contractAddr.String()},
stored: types.ContractInfoFixture(myExtension),
expRsp: &types.QueryContractInfoResponse{
Address: contractAddr.String(),
ContractInfo: types.ContractInfoFixture(myExtension, func(info *types.ContractInfo) {
info.Created = nil // not returned on queries
}),
Address: contractAddr.String(),
ContractInfo: types.ContractInfoFixture(myExtension),
},
},
"not found": {
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (c Contract) ValidateBasic() error {
return sdkerrors.Wrap(err, "contract info")
}

if c.ContractInfo.Created != nil {
return sdkerrors.Wrap(ErrInvalid, "created must be empty")
if c.ContractInfo.Created == nil {
return sdkerrors.Wrap(ErrInvalid, "created must not be empty")
}
for i := range c.ContractState {
if err := c.ContractState[i].ValidateBasic(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestContractValidateBasic(t *testing.T) {
srcMutator: func(c *Contract) {
c.ContractInfo.Created = &AbsoluteTxPosition{}
},
expError: true,
expError: false,
},
"contract state invalid": {
srcMutator: func(c *Contract) {
Expand Down
6 changes: 5 additions & 1 deletion x/wasm/types/test_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ContractFixture(mutators ...func(*Contract)) Contract {

fixture := Contract{
ContractAddress: anyAddress,
ContractInfo: ContractInfoFixture(OnlyGenesisFields),
ContractInfo: ContractInfoFixture(RandCreatedFields),
ContractState: []Model{{Key: []byte("anyKey"), Value: []byte("anyValue")}},
}

Expand All @@ -101,6 +101,10 @@ func OnlyGenesisFields(info *ContractInfo) {
info.Created = nil
}

func RandCreatedFields(info *ContractInfo) {
info.Created = &AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()}
}

func ContractInfoFixture(mutators ...func(*ContractInfo)) ContractInfo {
const anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs2m6sx4"

Expand Down
1 change: 0 additions & 1 deletion x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func (c *ContractInfo) AddMigration(ctx sdk.Context, codeID uint64, msg []byte)

// ResetFromGenesis resets contracts timestamp and history.
func (c *ContractInfo) ResetFromGenesis(ctx sdk.Context) ContractCodeHistoryEntry {
c.Created = NewAbsoluteTxPosition(ctx)
return ContractCodeHistoryEntry{
Operation: ContractCodeHistoryOperationTypeGenesis,
CodeID: c.CodeID,
Expand Down