Store Label with new contracts

This commit is contained in:
Ethan Frey 2020-02-25 17:04:34 +01:00
parent b80fbdaa9e
commit ba53b8a9c9
6 changed files with 18 additions and 16 deletions

View File

@ -70,7 +70,7 @@ func handleStoreCode(ctx sdk.Context, k Keeper, msg *MsgStoreCode) (*sdk.Result,
}
func handleInstantiate(ctx sdk.Context, k Keeper, msg *MsgInstantiateContract) (*sdk.Result, error) {
contractAddr, err := k.Instantiate(ctx, msg.Code, msg.Sender, msg.InitMsg, msg.InitFunds)
contractAddr, err := k.Instantiate(ctx, msg.Code, msg.Sender, msg.InitMsg, msg.Label, msg.InitFunds)
if err != nil {
return nil, err
}

View File

@ -94,7 +94,7 @@ func isSimulationMode(ctx sdk.Context) bool {
}
// Instantiate creates an instance of a WASM contract
func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator sdk.AccAddress, initMsg []byte, deposit sdk.Coins) (sdk.AccAddress, error) {
func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator sdk.AccAddress, initMsg []byte, label string, deposit sdk.Coins) (sdk.AccAddress, error) {
// create contract address
contractAddress := k.generateContractAddress(ctx, codeID)
existingAcct := k.accountKeeper.GetAccount(ctx, contractAddress)
@ -147,7 +147,7 @@ func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator sdk.AccAddre
}
// persist instance
instance := types.NewContractInfo(codeID, creator, initMsg)
instance := types.NewContractInfo(codeID, creator, initMsg, label)
// 0x02 | contractAddress (sdk.AccAddress) -> Instance
store.Set(types.GetContractAddressKey(contractAddress), k.cdc.MustMarshalBinaryBare(instance))

View File

@ -146,12 +146,12 @@ func TestInstantiate(t *testing.T) {
gasBefore := ctx.GasMeter().GasConsumed()
// create with no balance is also legal
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, nil)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, "demo contract 1", nil)
require.NoError(t, err)
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String())
gasAfter := ctx.GasMeter().GasConsumed()
require.Equal(t, uint64(28426), gasAfter-gasBefore)
require.Equal(t, uint64(28936), gasAfter-gasBefore)
}
func TestInstantiateWithNonExistingCodeID(t *testing.T) {
@ -170,7 +170,7 @@ func TestInstantiateWithNonExistingCodeID(t *testing.T) {
require.NoError(t, err)
const nonExistingCodeID = 9999
addr, err := keeper.Instantiate(ctx, nonExistingCodeID, creator, initMsgBz, nil)
addr, err := keeper.Instantiate(ctx, nonExistingCodeID, creator, initMsgBz, "demo contract 2", nil)
require.True(t, types.ErrNotFound.Is(err), err)
require.Nil(t, addr)
}
@ -200,7 +200,7 @@ func TestExecute(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, deposit)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, "demo contract 3", deposit)
require.NoError(t, err)
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String())
@ -236,7 +236,7 @@ func TestExecute(t *testing.T) {
// make sure gas is properly deducted from ctx
gasAfter := ctx.GasMeter().GasConsumed()
require.Equal(t, uint64(31754), gasAfter-gasBefore)
require.Equal(t, uint64(31805), gasAfter-gasBefore)
// ensure bob now exists and got both payments released
bobAcct = accKeeper.GetAccount(ctx, bob)
@ -292,7 +292,7 @@ func TestExecuteWithPanic(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, deposit)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, "demo contract 4", deposit)
require.NoError(t, err)
// let's make sure we get a reasonable error, no panic/crash
@ -325,7 +325,7 @@ func TestExecuteWithCpuLoop(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, deposit)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, "demo contract 5", deposit)
require.NoError(t, err)
// make sure we set a limit before calling
@ -366,7 +366,7 @@ func TestExecuteWithStorageLoop(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, deposit)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, "demo contract 6", deposit)
require.NoError(t, err)
// make sure we set a limit before calling

View File

@ -50,7 +50,7 @@ func TestMaskReflectOpaque(t *testing.T) {
// creator instantiates a contract and gives it tokens
contractStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
contractAddr, err := keeper.Instantiate(ctx, codeID, creator, []byte("{}"), contractStart)
contractAddr, err := keeper.Instantiate(ctx, codeID, creator, []byte("{}"), "mask contract 1", contractStart)
require.NoError(t, err)
require.NotEmpty(t, contractAddr)
@ -151,7 +151,7 @@ func TestMaskReflectContractSend(t *testing.T) {
// creator instantiates a contract and gives it tokens
maskStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
maskAddr, err := keeper.Instantiate(ctx, maskID, creator, []byte("{}"), maskStart)
maskAddr, err := keeper.Instantiate(ctx, maskID, creator, []byte("{}"), "mask contract 2", maskStart)
require.NoError(t, err)
require.NotEmpty(t, maskAddr)
@ -163,7 +163,7 @@ func TestMaskReflectContractSend(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
escrowStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 25000))
escrowAddr, err := keeper.Instantiate(ctx, escrowID, creator, initMsgBz, escrowStart)
escrowAddr, err := keeper.Instantiate(ctx, escrowID, creator, initMsgBz, "escrow contract 2", escrowStart)
require.NoError(t, err)
require.NotEmpty(t, escrowAddr)

View File

@ -39,7 +39,7 @@ func TestQueryContractState(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, deposit)
addr, err := keeper.Instantiate(ctx, contractID, creator, initMsgBz, "demo contract to query", deposit)
require.NoError(t, err)
contractModel := []types.Model{

View File

@ -43,6 +43,7 @@ func NewCodeInfo(codeHash []byte, creator sdk.AccAddress, source string, builder
type ContractInfo struct {
CodeID uint64 `json:"code_id"`
Creator sdk.AccAddress `json:"creator"`
Label string `json:"label"`
InitMsg json.RawMessage `json:"init_msg"`
}
@ -78,11 +79,12 @@ func NewWasmCoins(cosmosCoins sdk.Coins) (wasmCoins []wasmTypes.Coin) {
}
// NewContractInfo creates a new instance of a given WASM contract info
func NewContractInfo(codeID uint64, creator sdk.AccAddress, initMsg []byte) ContractInfo {
func NewContractInfo(codeID uint64, creator sdk.AccAddress, initMsg []byte, label string) ContractInfo {
return ContractInfo{
CodeID: codeID,
Creator: creator,
InitMsg: initMsg,
Label: label,
}
}