Skip to content

Commit

Permalink
Merge branch 'feat/wasmdir_flag' of github.com:axelarnetwork/axelar-c…
Browse files Browse the repository at this point in the history
…ore into feat/wasmdir_flag
  • Loading branch information
João Sousa authored and João Sousa committed Feb 28, 2024
2 parents 02dfddc + 06e63b8 commit aa811d4
Show file tree
Hide file tree
Showing 15 changed files with 1,470 additions and 1,328 deletions.
22 changes: 18 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func NewAxelarApp(
upgradeKeeper: *getKeeper[upgradekeeper.Keeper](keepers),
}

app.setUpgradeBehaviour(configurator)
app.setUpgradeBehaviour(configurator, keepers)

// initialize stores
app.MountKVStores(keys)
Expand Down Expand Up @@ -453,11 +453,25 @@ func initMessageRouter(keepers *KeeperCache) nexusTypes.MessageRouter {
return messageRouter
}

func (app *AxelarApp) setUpgradeBehaviour(configurator module.Configurator) {
func (app *AxelarApp) setUpgradeBehaviour(configurator module.Configurator, keepers *KeeperCache) {
app.upgradeKeeper.SetUpgradeHandler(
upgradeName(app.Version()),
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, configurator, fromVM)
updatedVM, err := app.mm.RunMigrations(ctx, configurator, fromVM)
if err != nil {
return updatedVM, err
}

// TODO: remove after v35 upgrade
// Override wasm module default params
if upgradeName(app.Version()) == "v0.35" && IsWasmEnabled() {
getKeeper[wasm.Keeper](keepers).SetParams(ctx, wasmtypes.Params{
CodeUploadAccess: wasmtypes.AllowNobody,
InstantiateDefaultPermission: wasmtypes.AccessTypeNobody,
})
}

return updatedVM, err
},
)

Expand Down Expand Up @@ -1041,7 +1055,7 @@ func GetModuleBasics() module.BasicManager {
}

if IsWasmEnabled() {
managers = append(managers, NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{}, authtypes.NewModuleAddress(govtypes.ModuleName)))
managers = append(managers, NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{}))
}

if IsIBCWasmHooksEnabled() {
Expand Down
8 changes: 3 additions & 5 deletions app/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ func isIBCSendPacketMsg(msg wasmvmtypes.CosmosMsg) bool {

type WasmAppModuleBasicOverride struct {
wasm.AppModuleBasic
uploader sdk.AccAddress
}

func NewWasmAppModuleBasicOverride(wasmModule wasm.AppModuleBasic, uploader sdk.AccAddress) WasmAppModuleBasicOverride {
func NewWasmAppModuleBasicOverride(wasmModule wasm.AppModuleBasic) WasmAppModuleBasicOverride {
return WasmAppModuleBasicOverride{
AppModuleBasic: wasmModule,
uploader: uploader,
}
}

Expand All @@ -134,8 +132,8 @@ func NewWasmAppModuleBasicOverride(wasmModule wasm.AppModuleBasic, uploader sdk.
func (m WasmAppModuleBasicOverride) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(&wasm.GenesisState{
Params: wasmtypes.Params{
CodeUploadAccess: wasmtypes.AccessTypeAnyOfAddresses.With(m.uploader),
InstantiateDefaultPermission: wasmtypes.AccessTypeAnyOfAddresses,
CodeUploadAccess: wasmtypes.AllowNobody,
InstantiateDefaultPermission: wasmtypes.AccessTypeNobody,
},
})
}
9 changes: 3 additions & 6 deletions app/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -242,8 +241,7 @@ func TestMsgTypeBlacklistMessenger_DispatchMsg(t *testing.T) {
}

func TestNewWasmAppModuleBasicOverride(t *testing.T) {
uploader := authtypes.NewModuleAddress("allowed to upload")
wasmModule := app.NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{}, uploader)
wasmModule := app.NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{})
cdc := app.MakeEncodingConfig().Codec

genesis := wasmModule.DefaultGenesis(cdc)
Expand All @@ -252,9 +250,8 @@ func TestNewWasmAppModuleBasicOverride(t *testing.T) {
var state wasm.GenesisState
assert.NoError(t, cdc.UnmarshalJSON(genesis, &state))

assert.Equal(t, state.Params.InstantiateDefaultPermission, wasmtypes.AccessTypeAnyOfAddresses)
assert.True(t, state.Params.CodeUploadAccess.Allowed(uploader))
assert.Len(t, state.Params.CodeUploadAccess.AllAuthorizedAddresses(), 1)
assert.Equal(t, state.Params.InstantiateDefaultPermission, wasmtypes.AccessTypeNobody)
assert.True(t, state.Params.CodeUploadAccess.Equals(wasmtypes.AllowNobody))
}

func TestICSMiddleWare(t *testing.T) {
Expand Down
174 changes: 174 additions & 0 deletions vald/evm/decoders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package evm

import (
"fmt"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
geth "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"

"github.com/axelarnetwork/axelar-core/x/evm/types"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
"github.com/axelarnetwork/utils/funcs"
"github.com/axelarnetwork/utils/slices"
)

// Smart contract event signatures
var (
ERC20TransferSig = crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)"))
ERC20TokenDeploymentSig = crypto.Keccak256Hash([]byte("TokenDeployed(string,address)"))
MultisigTransferOperatorshipSig = crypto.Keccak256Hash([]byte("OperatorshipTransferred(bytes)"))
ContractCallSig = crypto.Keccak256Hash([]byte("ContractCall(address,string,string,bytes32,bytes)"))
ContractCallWithTokenSig = crypto.Keccak256Hash([]byte("ContractCallWithToken(address,string,string,bytes32,bytes,string,uint256)"))
TokenSentSig = crypto.Keccak256Hash([]byte("TokenSent(address,string,string,string,uint256)"))
)

func DecodeERC20TransferEvent(log *geth.Log) (types.EventTransfer, error) {
if len(log.Topics) != 3 || log.Topics[0] != ERC20TransferSig {
return types.EventTransfer{}, fmt.Errorf("log is not an ERC20 transfer")
}

uint256Type := funcs.Must(abi.NewType("uint256", "uint256", nil))

to := common.BytesToAddress(log.Topics[2][:])

arguments := abi.Arguments{
{Type: uint256Type},
}

params, err := arguments.Unpack(log.Data)
if err != nil {
return types.EventTransfer{}, err
}

return types.EventTransfer{
To: types.Address(to),
Amount: sdk.NewUintFromBigInt(params[0].(*big.Int)),
}, nil
}

func DecodeERC20TokenDeploymentEvent(log *geth.Log) (types.EventTokenDeployed, error) {
if len(log.Topics) != 1 || log.Topics[0] != ERC20TokenDeploymentSig {
return types.EventTokenDeployed{}, fmt.Errorf("event is not for an ERC20 token deployment")
}

stringType := funcs.Must(abi.NewType("string", "string", nil))
addressType := funcs.Must(abi.NewType("address", "address", nil))

arguments := abi.Arguments{{Type: stringType}, {Type: addressType}}
params, err := types.StrictDecode(arguments, log.Data)
if err != nil {
return types.EventTokenDeployed{}, err
}

return types.EventTokenDeployed{
Symbol: params[0].(string),
TokenAddress: types.Address(params[1].(common.Address)),
}, nil
}

func DecodeMultisigOperatorshipTransferredEvent(log *geth.Log) (types.EventMultisigOperatorshipTransferred, error) {
if len(log.Topics) != 1 || log.Topics[0] != MultisigTransferOperatorshipSig {
return types.EventMultisigOperatorshipTransferred{}, fmt.Errorf("event is not OperatorshipTransferred")
}

bytesType := funcs.Must(abi.NewType("bytes", "bytes", nil))
newOperatorsData, err := types.StrictDecode(abi.Arguments{{Type: bytesType}}, log.Data)
if err != nil {
return types.EventMultisigOperatorshipTransferred{}, err
}

addressesType := funcs.Must(abi.NewType("address[]", "address[]", nil))
uint256ArrayType := funcs.Must(abi.NewType("uint256[]", "uint256[]", nil))
uint256Type := funcs.Must(abi.NewType("uint256", "uint256", nil))

arguments := abi.Arguments{{Type: addressesType}, {Type: uint256ArrayType}, {Type: uint256Type}}
params, err := types.StrictDecode(arguments, newOperatorsData[0].([]byte))
if err != nil {
return types.EventMultisigOperatorshipTransferred{}, err
}

event := types.EventMultisigOperatorshipTransferred{
NewOperators: slices.Map(params[0].([]common.Address), func(addr common.Address) types.Address { return types.Address(addr) }),
NewWeights: slices.Map(params[1].([]*big.Int), sdk.NewUintFromBigInt),
NewThreshold: sdk.NewUintFromBigInt(params[2].(*big.Int)),
}

return event, nil
}

func DecodeEventContractCallWithToken(log *geth.Log) (types.EventContractCallWithToken, error) {
stringType := funcs.Must(abi.NewType("string", "string", nil))
bytesType := funcs.Must(abi.NewType("bytes", "bytes", nil))
uint256Type := funcs.Must(abi.NewType("uint256", "uint256", nil))

arguments := abi.Arguments{
{Type: stringType},
{Type: stringType},
{Type: bytesType},
{Type: stringType},
{Type: uint256Type},
}
params, err := types.StrictDecode(arguments, log.Data)
if err != nil {
return types.EventContractCallWithToken{}, err
}

return types.EventContractCallWithToken{
Sender: types.Address(common.BytesToAddress(log.Topics[1].Bytes())),
DestinationChain: nexus.ChainName(params[0].(string)),
ContractAddress: params[1].(string),
PayloadHash: types.Hash(common.BytesToHash(log.Topics[2].Bytes())),
Symbol: params[3].(string),
Amount: sdk.NewUintFromBigInt(params[4].(*big.Int)),
}, nil
}

func DecodeEventTokenSent(log *geth.Log) (types.EventTokenSent, error) {
stringType := funcs.Must(abi.NewType("string", "string", nil))
uint256Type := funcs.Must(abi.NewType("uint256", "uint256", nil))

arguments := abi.Arguments{
{Type: stringType},
{Type: stringType},
{Type: stringType},
{Type: uint256Type},
}
params, err := types.StrictDecode(arguments, log.Data)
if err != nil {
return types.EventTokenSent{}, err
}

return types.EventTokenSent{
Sender: types.Address(common.BytesToAddress(log.Topics[1].Bytes())),
DestinationChain: nexus.ChainName(params[0].(string)),
DestinationAddress: params[1].(string),
Symbol: params[2].(string),
Amount: sdk.NewUintFromBigInt(params[3].(*big.Int)),
}, nil
}

func DecodeEventContractCall(log *geth.Log) (types.EventContractCall, error) {
stringType := funcs.Must(abi.NewType("string", "string", nil))
bytesType := funcs.Must(abi.NewType("bytes", "bytes", nil))

arguments := abi.Arguments{
{Type: stringType},
{Type: stringType},
{Type: bytesType},
}
params, err := types.StrictDecode(arguments, log.Data)
if err != nil {
return types.EventContractCall{}, err
}

return types.EventContractCall{
Sender: types.Address(common.BytesToAddress(log.Topics[1].Bytes())),
DestinationChain: nexus.ChainName(params[0].(string)),
ContractAddress: params[1].(string),
PayloadHash: types.Hash(common.BytesToHash(log.Topics[2].Bytes())),
}, nil
}
Loading

0 comments on commit aa811d4

Please sign in to comment.