From 235fb59d051e226fbd3f84a51e52e38d399e6607 Mon Sep 17 00:00:00 2001 From: Nikhil Suri Date: Wed, 7 Dec 2022 09:20:11 -0800 Subject: [PATCH] wormchain: move governance consts to sdk (#2086) --- sdk/vaa/payloads.go | 26 +++++++++++++++---- wormchain/x/wormhole/client/cli/genesis.go | 3 +-- .../msg_server_execute_governance_vaa.go | 11 ++------ .../msg_server_execute_governance_vaa_test.go | 2 +- .../x/wormhole/keeper/msg_server_wasmd.go | 16 +++--------- .../wormhole/keeper/msg_server_wasmd_test.go | 6 ++--- wormchain/x/wormhole/types/tx.pb.go | 4 +-- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/sdk/vaa/payloads.go b/sdk/vaa/payloads.go index 8a6527247..3c5bc8481 100644 --- a/sdk/vaa/payloads.go +++ b/sdk/vaa/payloads.go @@ -10,6 +10,22 @@ import ( // CoreModule is the identifier of the Core module (which is used for governance messages) var CoreModule = []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x43, 0x6f, 0x72, 0x65} +// WasmdModule is the identifier of the Wormchain Wasmd module (which is used for governance messages) +var WasmdModule = [32]byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x57, 0x61, 0x73, 0x6D, 0x64, 0x4D, 0x6F, 0x64, 0x75, 0x6C, 0x65} +var WasmdModuleStr = string(WasmdModule[:]) + +type GovernanceAction uint8 + +var ( + // Wormhole governance actions + ActionContractUpgrade GovernanceAction = 1 + ActionGuardianSetUpdate GovernanceAction = 2 + + // Wormchain cosmwasm governance actions + ActionStoreCode GovernanceAction = 1 + ActionInstantiateContract GovernanceAction = 2 +) + type ( // BodyContractUpgrade is a governance message to perform a contract upgrade of the core module BodyContractUpgrade struct { @@ -54,7 +70,7 @@ func (b BodyContractUpgrade) Serialize() []byte { // Module buf.Write(CoreModule) // Action - MustWrite(buf, binary.BigEndian, uint8(1)) + MustWrite(buf, binary.BigEndian, ActionContractUpgrade) // ChainID MustWrite(buf, binary.BigEndian, uint16(b.ChainID)) @@ -69,7 +85,7 @@ func (b BodyGuardianSetUpdate) Serialize() []byte { // Module buf.Write(CoreModule) // Action - MustWrite(buf, binary.BigEndian, uint8(2)) + MustWrite(buf, binary.BigEndian, ActionGuardianSetUpdate) // ChainID - 0 for universal MustWrite(buf, binary.BigEndian, uint16(0)) @@ -91,14 +107,14 @@ func (r BodyTokenBridgeUpgradeContract) Serialize() []byte { } func (r BodyWormchainStoreCode) Serialize() []byte { - return serializeBridgeGovernanceVaa("WasmdModule", 1, ChainIDWormchain, r.WasmHash) + return serializeBridgeGovernanceVaa(WasmdModuleStr, ActionStoreCode, ChainIDWormchain, r.WasmHash) } func (r BodyWormchainInstantiateContract) Serialize() []byte { - return serializeBridgeGovernanceVaa("WasmdModule", 2, ChainIDWormchain, r.InstantiationParamsHash) + return serializeBridgeGovernanceVaa(WasmdModuleStr, ActionInstantiateContract, ChainIDWormchain, r.InstantiationParamsHash) } -func serializeBridgeGovernanceVaa(module string, actionId uint8, chainId ChainID, payload [32]byte) []byte { +func serializeBridgeGovernanceVaa(module string, actionId GovernanceAction, chainId ChainID, payload [32]byte) []byte { if len(module) > 32 { panic("module longer than 32 byte") } diff --git a/wormchain/x/wormhole/client/cli/genesis.go b/wormchain/x/wormhole/client/cli/genesis.go index 5e8a8a80c..56529b5e7 100644 --- a/wormchain/x/wormhole/client/cli/genesis.go +++ b/wormchain/x/wormhole/client/cli/genesis.go @@ -20,7 +20,6 @@ import ( // "github.com/cosmos/cosmos-sdk/client/flags" "github.com/ethereum/go-ethereum/crypto" - "github.com/wormhole-foundation/wormchain/x/wormhole/keeper" "github.com/wormhole-foundation/wormchain/x/wormhole/types" wormholesdk "github.com/wormhole-foundation/wormhole/sdk" "github.com/wormhole-foundation/wormhole/sdk/vaa" @@ -378,7 +377,7 @@ func CmdGenerateGuardianSetUpdatea() *cobra.Command { set_update = append(set_update, pubkey...) } - action := keeper.ActionGuardianSetUpdate + action := vaa.ActionGuardianSetUpdate chain := 3104 module := [32]byte{} copy(module[:], vaa.CoreModule) diff --git a/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa.go b/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa.go index 70d32ab98..d4d7e05a3 100644 --- a/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa.go +++ b/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa.go @@ -9,13 +9,6 @@ import ( "github.com/wormhole-foundation/wormhole/sdk/vaa" ) -type GovernanceAction uint8 - -var ( - ActionContractUpgrade GovernanceAction = 1 - ActionGuardianSetUpdate GovernanceAction = 2 -) - func (k msgServer) ExecuteGovernanceVAA(goCtx context.Context, msg *types.MsgExecuteGovernanceVAA) (*types.MsgExecuteGovernanceVAAResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) @@ -34,8 +27,8 @@ func (k msgServer) ExecuteGovernanceVAA(goCtx context.Context, msg *types.MsgExe } // Execute action - switch GovernanceAction(action) { - case ActionGuardianSetUpdate: + switch vaa.GovernanceAction(action) { + case vaa.ActionGuardianSetUpdate: if len(payload) < 5 { return nil, types.ErrInvalidGovernancePayloadLength } diff --git a/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa_test.go b/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa_test.go index e18ea97a8..462a6dd46 100644 --- a/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa_test.go +++ b/wormchain/x/wormhole/keeper/msg_server_execute_governance_vaa_test.go @@ -26,7 +26,7 @@ func createExecuteGovernanceVaaPayload(k *keeper.Keeper, ctx sdk.Context, num_gu // governance message with sha3 of wasmBytes as the payload module := [32]byte{} copy(module[:], vaa.CoreModule) - gov_msg := types.NewGovernanceMessage(module, byte(keeper.ActionGuardianSetUpdate), uint16(vaa.ChainIDWormchain), set_update) + gov_msg := types.NewGovernanceMessage(module, byte(vaa.ActionGuardianSetUpdate), uint16(vaa.ChainIDWormchain), set_update) return gov_msg.MarshalBinary(), privateKeys } diff --git a/wormchain/x/wormhole/keeper/msg_server_wasmd.go b/wormchain/x/wormhole/keeper/msg_server_wasmd.go index 4f34224ed..d3cfddbda 100644 --- a/wormchain/x/wormhole/keeper/msg_server_wasmd.go +++ b/wormchain/x/wormhole/keeper/msg_server_wasmd.go @@ -12,14 +12,6 @@ import ( "golang.org/x/crypto/sha3" ) -// WasmdModule is the identifier of the Wasmd module (which is used for governance messages) -var WasmdModule = [32]byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x57, 0x61, 0x73, 0x6D, 0x64, 0x4D, 0x6F, 0x64, 0x75, 0x6C, 0x65} - -var ( - ActionStoreCode GovernanceAction = 1 - ActionInstantiateContract GovernanceAction = 2 -) - // Simple wrapper of x/wasmd StoreCode that requires a VAA func (k msgServer) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*types.MsgStoreCodeResponse, error) { if !k.setWasmd { @@ -33,12 +25,12 @@ func (k msgServer) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*t return nil, err } // Verify VAA - action, payload, err := k.VerifyGovernanceVAA(ctx, v, WasmdModule) + action, payload, err := k.VerifyGovernanceVAA(ctx, v, vaa.WasmdModule) if err != nil { return nil, err } - if GovernanceAction(action) != ActionStoreCode { + if vaa.GovernanceAction(action) != vaa.ActionStoreCode { return nil, types.ErrUnknownGovernanceAction } @@ -83,12 +75,12 @@ func (k msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst } // Verify VAA - action, payload, err := k.VerifyGovernanceVAA(ctx, v, WasmdModule) + action, payload, err := k.VerifyGovernanceVAA(ctx, v, vaa.WasmdModule) if err != nil { return nil, err } - if GovernanceAction(action) != ActionInstantiateContract { + if vaa.GovernanceAction(action) != vaa.ActionInstantiateContract { return nil, types.ErrUnknownGovernanceAction } diff --git a/wormchain/x/wormhole/keeper/msg_server_wasmd_test.go b/wormchain/x/wormhole/keeper/msg_server_wasmd_test.go index 41aea5bcb..cb212d2a9 100644 --- a/wormchain/x/wormhole/keeper/msg_server_wasmd_test.go +++ b/wormchain/x/wormhole/keeper/msg_server_wasmd_test.go @@ -22,7 +22,7 @@ func createWasmStoreCodePayload(wasmBytes []byte) []byte { keccak.Write(wasmBytes) keccak.Sum(hashWasm[:0]) - gov_msg := types.NewGovernanceMessage(keeper.WasmdModule, byte(keeper.ActionStoreCode), uint16(vaa.ChainIDWormchain), hashWasm[:]) + gov_msg := types.NewGovernanceMessage(vaa.WasmdModule, byte(vaa.ActionStoreCode), uint16(vaa.ChainIDWormchain), hashWasm[:]) return gov_msg.MarshalBinary() } @@ -34,8 +34,8 @@ func createWasmInstantiatePayload(code_id uint64, label string, json_msg string) expected_hash := vaa.CreateInstatiateCosmwasmContractHash(code_id, label, []byte(json_msg)) var payload bytes.Buffer - payload.Write(keeper.WasmdModule[:]) - payload.Write([]byte{byte(keeper.ActionInstantiateContract)}) + payload.Write(vaa.WasmdModule[:]) + payload.Write([]byte{byte(vaa.ActionInstantiateContract)}) binary.Write(&payload, binary.BigEndian, uint16(vaa.ChainIDWormchain)) // custom payload payload.Write(expected_hash[:]) diff --git a/wormchain/x/wormhole/types/tx.pb.go b/wormchain/x/wormhole/types/tx.pb.go index 2e16cf8e3..91d5ae8bc 100644 --- a/wormchain/x/wormhole/types/tx.pb.go +++ b/wormchain/x/wormhole/types/tx.pb.go @@ -528,7 +528,7 @@ type MsgClient interface { RegisterAccountAsGuardian(ctx context.Context, in *MsgRegisterAccountAsGuardian, opts ...grpc.CallOption) (*MsgRegisterAccountAsGuardianResponse, error) // StoreCode to submit Wasm code to the system StoreCode(ctx context.Context, in *MsgStoreCode, opts ...grpc.CallOption) (*MsgStoreCodeResponse, error) - // Instantiate creates a new smart contract instance for the given code id. + // Instantiate creates a new smart contract instance for the given code id. InstantiateContract(ctx context.Context, in *MsgInstantiateContract, opts ...grpc.CallOption) (*MsgInstantiateContractResponse, error) } @@ -582,7 +582,7 @@ type MsgServer interface { RegisterAccountAsGuardian(context.Context, *MsgRegisterAccountAsGuardian) (*MsgRegisterAccountAsGuardianResponse, error) // StoreCode to submit Wasm code to the system StoreCode(context.Context, *MsgStoreCode) (*MsgStoreCodeResponse, error) - // Instantiate creates a new smart contract instance for the given code id. + // Instantiate creates a new smart contract instance for the given code id. InstantiateContract(context.Context, *MsgInstantiateContract) (*MsgInstantiateContractResponse, error) }