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

Migrate wasm to proto #110

Merged
merged 7 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func NewWasmApp(
}
wasmConfig := wasmWrap.Wasm

app.wasmKeeper = wasm.NewKeeper(app.cdc, keys[wasm.StoreKey], app.accountKeeper, app.bankKeeper, wasmRouter, wasmDir, wasmConfig)
app.wasmKeeper = wasm.NewKeeper(appCodec, keys[wasm.StoreKey], app.accountKeeper, app.bankKeeper, wasmRouter, wasmDir, wasmConfig)

// register the proposal types
govRouter := gov.NewRouter()
Expand Down
6 changes: 3 additions & 3 deletions app/integration/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
// Setup initializes a new wasmd.WasmApp. A Nop logger is set in WasmApp.
func Setup(isCheckTx bool) *wasmd.WasmApp {
db := dbm.NewMemDB()
app := wasmd.NewWasmApp(log.NewNopLogger(), db, nil, true, 0, nil)
app := wasmd.NewWasmApp(log.NewNopLogger(), db, nil, true, 0, nil, "")
// app := wasmd.NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, 0)
if !isCheckTx {
// init chain must be called to stop deliverState from being nil
Expand All @@ -57,7 +57,7 @@ func Setup(isCheckTx bool) *wasmd.WasmApp {
// genesis accounts.
func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount) *wasmd.WasmApp {
db := dbm.NewMemDB()
app := wasmd.NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0, nil)
app := wasmd.NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0, nil, "")
// app := wasmd.NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, 0)

// initialize the chain with the passed in genesis accounts
Expand Down Expand Up @@ -142,7 +142,7 @@ func GenTx(msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accnums
}

sigs[i] = auth.StdSignature{
PubKey: p.PubKey(),
PubKey: p.PubKey().Bytes(),
Signature: sig,
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/client/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func GetTestData() ([]byte, []byte, []byte, error) {
wasmCode, err := ioutil.ReadFile("../../internal/keeper/testdata/contract.wasm")
wasmCode, err := ioutil.ReadFile("../../keeper/testdata/contract.wasm")

if err != nil {
return nil, nil, nil, err
Expand Down
12 changes: 6 additions & 6 deletions x/wasm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func TestInitGenesis(t *testing.T) {

deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000))
creator := createFakeFundedAccount(data.ctx, data.acctKeeper, deposit.Add(deposit...))
fred := createFakeFundedAccount(data.ctx, data.acctKeeper, topUp)
creator := createFakeFundedAccount(t, data.ctx, data.acctKeeper, data.bankKeeper, deposit.Add(deposit...))
fred := createFakeFundedAccount(t, data.ctx, data.acctKeeper, data.bankKeeper, topUp)

h := data.module.NewHandler()
q := data.module.NewQuerierHandler()

t.Log("fail with invalid source url")
msg := MsgStoreCode{
Sender: creator,
WASMByteCode: testContract,
WasmByteCode: testContract,
Source: "someinvalidurl",
Builder: "",
}
Expand All @@ -41,7 +41,7 @@ func TestInitGenesis(t *testing.T) {
t.Log("fail with relative source url")
msg = MsgStoreCode{
Sender: creator,
WASMByteCode: testContract,
WasmByteCode: testContract,
Source: "./testdata/escrow.wasm",
Builder: "",
}
Expand All @@ -55,7 +55,7 @@ func TestInitGenesis(t *testing.T) {
t.Log("fail with invalid build tag")
msg = MsgStoreCode{
Sender: creator,
WASMByteCode: testContract,
WasmByteCode: testContract,
Source: "",
Builder: "somerandombuildtag-0.6.2",
}
Expand All @@ -69,7 +69,7 @@ func TestInitGenesis(t *testing.T) {
t.Log("no error with valid source and build tag")
msg = MsgStoreCode{
Sender: creator,
WASMByteCode: testContract,
WasmByteCode: testContract,
Source: "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm",
Builder: "confio/cosmwasm-opt:0.7.0",
}
Expand Down
45 changes: 25 additions & 20 deletions x/wasm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
)

const (
Expand Down Expand Up @@ -43,9 +44,9 @@ func NewHandler(k Keeper) sdk.Handler {

// filterMessageEvents returns the same events with all of type == EventTypeMessage removed.
// this is so only our top-level message event comes through
func filterMessageEvents(manager *sdk.EventManager) sdk.Events {
events := manager.Events()
res := make([]sdk.Event, 0, len(events)+1)
func filteredMessageEvents(manager *sdk.EventManager) []abci.Event {
events := manager.ABCIEvents()
res := make([]abci.Event, 0, len(events))
for _, e := range events {
if e.Type != sdk.EventTypeMessage {
res = append(res, e)
Expand All @@ -60,22 +61,23 @@ func handleStoreCode(ctx sdk.Context, k Keeper, msg *MsgStoreCode) (*sdk.Result,
return nil, err
}

codeID, err := k.Create(ctx, msg.Sender, msg.WASMByteCode, msg.Source, msg.Builder)
codeID, err := k.Create(ctx, msg.Sender, msg.WasmByteCode, msg.Source, msg.Builder)
if err != nil {
return nil, err
}

events := filterMessageEvents(ctx.EventManager())
Copy link
Member

Choose a reason for hiding this comment

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

It seems you are missing the filterMessageEvents, which may be causing this other error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this maybe the reason. Will modify this and see

Copy link
Member

Choose a reason for hiding this comment

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

fixed in my last commits

ourEvent := sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, ModuleName),
sdk.NewAttribute(AttributeSigner, msg.Sender.String()),
sdk.NewAttribute(AttributeKeyCodeID, fmt.Sprintf("%d", codeID)),
)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, ModuleName),
sdk.NewAttribute(AttributeSigner, msg.Sender.String()),
sdk.NewAttribute(AttributeKeyCodeID, fmt.Sprintf("%d", codeID)),
),
})

return &sdk.Result{
Data: []byte(fmt.Sprintf("%d", codeID)),
Events: append(events, ourEvent),
Events: ctx.EventManager().ABCIEvents(),
}, nil
}

Expand All @@ -85,18 +87,19 @@ func handleInstantiate(ctx sdk.Context, k Keeper, msg *MsgInstantiateContract) (
return nil, err
}

events := filterMessageEvents(ctx.EventManager())
ourEvent := sdk.NewEvent(
events := filteredMessageEvents(ctx.EventManager())
custom := sdk.Events{sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, ModuleName),
sdk.NewAttribute(AttributeSigner, msg.Sender.String()),
sdk.NewAttribute(AttributeKeyCodeID, fmt.Sprintf("%d", msg.Code)),
sdk.NewAttribute(AttributeKeyContract, contractAddr.String()),
)
)}
events = append(events, custom.ToABCIEvents()...)

return &sdk.Result{
Data: contractAddr,
Events: append(events, ourEvent),
Events: events,
}, nil
}

Expand All @@ -106,14 +109,16 @@ func handleExecute(ctx sdk.Context, k Keeper, msg *MsgExecuteContract) (*sdk.Res
return nil, err
}

events := filterMessageEvents(ctx.EventManager())
ourEvent := sdk.NewEvent(
events := filteredMessageEvents(ctx.EventManager())
custom := sdk.Events{sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, ModuleName),
sdk.NewAttribute(AttributeSigner, msg.Sender.String()),
sdk.NewAttribute(AttributeKeyContract, msg.Contract.String()),
)
),
}
events = append(events, custom.ToABCIEvents()...)

res.Events = append(events, ourEvent)
res.Events = events
return &res, nil
}
2 changes: 1 addition & 1 deletion x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {

genState.Contracts = append(genState.Contracts, types.Contract{
ContractAddress: addr,
ContractInfo: contract,
ContractInfo: &contract,
ContractState: state,
})

Expand Down
8 changes: 4 additions & 4 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const MaxGas = 900_000_000
// Keeper will have a reference to Wasmer with it's own data directory.
type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
cdc codec.Marshaler
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper

Expand All @@ -43,7 +43,7 @@ type Keeper struct {
}

// NewKeeper creates a new contract Keeper instance
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, accountKeeper auth.AccountKeeper, bankKeeper bank.Keeper,
func NewKeeper(cdc codec.Marshaler, storeKey sdk.StoreKey, accountKeeper auth.AccountKeeper, bankKeeper bank.Keeper,
router sdk.Router, homeDir string, wasmConfig types.WasmConfig) Keeper {
wasmer, err := wasm.NewWasmer(filepath.Join(homeDir, "wasm"), wasmConfig.CacheSize)
if err != nil {
Expand Down Expand Up @@ -237,7 +237,7 @@ func (k Keeper) contractInstance(ctx sdk.Context, contractAddress sdk.AccAddress
var contract types.ContractInfo
k.cdc.MustUnmarshalBinaryBare(contractBz, &contract)

contractInfoBz := store.Get(types.GetCodeKey(contract.CodeID))
contractInfoBz := store.Get(types.GetCodeKey(contract.CodeId))
if contractInfoBz == nil {
return types.CodeInfo{}, prefix.Store{}, sdkerrors.Wrap(types.ErrNotFound, "contract info")
}
Expand All @@ -259,7 +259,7 @@ func (k Keeper) GetContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress)
return &contract
}

func (k Keeper) setContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress, contract types.ContractInfo) {
func (k Keeper) setContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress, contract *types.ContractInfo) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetContractAddressKey(contractAddress), k.cdc.MustMarshalBinaryBare(contract))
}
Expand Down
Loading