Change genesis preserving contract history (#1076)

* 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>
This commit is contained in:
GNaD13
2022-11-11 15:45:40 +07:00
committed by GitHub
parent 5caaab9c0b
commit 4c6afb38eb
19 changed files with 390 additions and 172 deletions

View File

@@ -12,7 +12,6 @@ import (
"time"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
@@ -111,21 +110,10 @@ func TestGenesisExportImport(t *testing.T) {
// reset contract code index in source DB for comparison with dest DB
wasmKeeper.IterateContractInfo(srcCtx, func(address sdk.AccAddress, info wasmTypes.ContractInfo) bool {
creatorAddress := sdk.MustAccAddressFromBech32(info.Creator)
wasmKeeper.removeFromContractCodeSecondaryIndex(srcCtx, address, wasmKeeper.getLastContractHistoryEntry(srcCtx, address))
history := wasmKeeper.GetContractHistory(srcCtx, address)
prefixStore := prefix.NewStore(srcCtx.KVStore(wasmKeeper.storeKey), types.GetContractCodeHistoryElementPrefix(address))
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
prefixStore.Delete(iter.Key())
}
x := &info
newHistory := x.ResetFromGenesis(dstCtx)
wasmKeeper.storeContractInfo(srcCtx, address, x)
wasmKeeper.addToContractCodeSecondaryIndex(srcCtx, address, newHistory)
wasmKeeper.addToContractCreatorSecondaryIndex(srcCtx, creatorAddress, newHistory.Updated, address)
wasmKeeper.appendToContractHistory(srcCtx, address, newHistory)
iter.Close()
wasmKeeper.addToContractCodeSecondaryIndex(srcCtx, address, history[len(history)-1])
wasmKeeper.addToContractCreatorSecondaryIndex(srcCtx, creatorAddress, history[0].Updated, address)
return false
})
@@ -272,7 +260,15 @@ 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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{}`),
},
},
},
},
Sequences: []types.Sequence{
@@ -293,10 +289,26 @@ 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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{}`),
},
},
}, {
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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{"foo":"bar"}`),
},
},
},
},
Sequences: []types.Sequence{
@@ -312,7 +324,15 @@ 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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{"foo":"bar"}`),
},
},
},
},
Params: types.DefaultParams(),
@@ -328,10 +348,26 @@ 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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{"foo":"bar"}`),
},
},
}, {
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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{"other":"value"}`),
},
},
},
},
Params: types.DefaultParams(),
@@ -347,7 +383,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},
@@ -358,6 +394,14 @@ func TestGenesisInit(t *testing.T) {
Value: []byte("bar"),
},
},
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{"foo":"bar"}`),
},
},
},
},
Params: types.DefaultParams(),
@@ -395,7 +439,15 @@ 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),
ContractCodeHistory: []types.ContractCodeHistoryEntry{
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: 1,
Updated: &types.AbsoluteTxPosition{BlockHeight: rand.Uint64(), TxIndex: rand.Uint64()},
Msg: []byte(`{}`),
},
},
},
},
Sequences: []types.Sequence{
@@ -459,7 +511,7 @@ func TestGenesisInit(t *testing.T) {
}
}
func TestImportContractWithCodeHistoryReset(t *testing.T) {
func TestImportContractWithCodeHistoryPreserved(t *testing.T) {
genesisTemplate := `
{
"params":{
@@ -489,8 +541,32 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
"code_id": "1",
"creator": "cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x",
"admin": "cosmos1h5t8zxmjr30e9dqghtlpl40f2zz5cgey6esxtn",
"label": "ȀĴnZV芢毤"
}
"label": "ȀĴnZV芢毤",
"created": {
"block_height" : "100",
"tx_index" : "10"
}
},
"contract_code_history": [
{
"operation": "CONTRACT_CODE_HISTORY_OPERATION_TYPE_INIT",
"code_id": "1",
"updated": {
"block_height" : "100",
"tx_index" : "10"
},
"msg": {"foo": "bar"}
},
{
"operation": "CONTRACT_CODE_HISTORY_OPERATION_TYPE_MIGRATE",
"code_id": "1",
"updated": {
"block_height" : "200",
"tx_index" : "10"
},
"msg": {"other": "msg"}
}
]
}
],
"sequences": [
@@ -550,15 +626,28 @@ 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,
Operation: types.ContractCodeHistoryOperationTypeInit,
CodeID: firstCodeID,
Updated: types.NewAbsoluteTxPosition(ctx),
Updated: &types.AbsoluteTxPosition{
BlockHeight: 100,
TxIndex: 10,
},
Msg: []byte(`{"foo": "bar"}`),
},
{
Operation: types.ContractCodeHistoryOperationTypeMigrate,
CodeID: firstCodeID,
Updated: &types.AbsoluteTxPosition{
BlockHeight: 200,
TxIndex: 10,
},
Msg: []byte(`{"other": "msg"}`),
},
}
assert.Equal(t, expHistory, keeper.GetContractHistory(ctx, contractAddr))