All pages
Powered by GitBook
1 of 6

WasmX

摘要

wasmx模块负责将 CosmWasm 智能合约集成到Injective Chain。其主要功能是在每个区块的begin blocker阶段提供合约执行方法。若合约因gas耗尽而自动停用,可由合约所有者重新激活。

此外,该模块还提供用于管理合约的辅助方法,例如批量代码存储提案。这些功能确保CosmWasm合约能够无缝集成到Injective Chain,并提供管理和维护合约的实用工具。

目录

  1. 概念

  2. 数据

  3. 提案

  4. 消息

  5. 参数

概念

开始区块执行

智能合约只能响应传入的消息,无法自主执行操作。Wasmx模块允许在每个区块的开始区块(begin blockers)部分注册和调用合约。为了符合此要求,每个注册的合约必须响应名为begin_blocker的sudo消息,该消息只能由链本身调用,而不能直接由任何用户或其他合约调用。这确保了begin_blocker消息的可信性。

注册

在注册合约时,用户必须声明一个 gas 价格,即他们愿意为合约执行支付的费用,以及一个 gas 限制,即合约执行过程中可以消耗的最大 gas 数量。 目前,合约注册只能通过治理提案进行。如果提案获得批准,将把该合约添加到每个“开始区块”(begin blockers)期间执行的合约列表中。 出于安全考虑,提案者必须为合约指定一个code_id,该code_id将在注册时以及每次合约执行时进行验证。这是为了防止攻击者先注册一个无害的合约,但后来将其升级为恶意合约。提案者可以请求在注册合约时豁免此检查,以避免新版本合约发布时的延迟,但这可能会根据提案者的可信度影响投票结果。 提案者还可以请求将合约“固定”在内存中,这意味着合约将被加载并保持在内存中,从而大大提高合约的性能。

注销(Deregistration)

合约可以通过治理提案进行注销,任何人都可以发起该提案,包括合约拥有者(如果他们不再需要该合约)或任何其他个人(如果合约被发现是恶意的)。 如果合约因 gas 不足而执行失败,它将被自动注销。 当合约被注销时,wasmx将调用合约中的特殊deregister{}回调(如果存在),作为一个sudo消息。

停用(Deactivation)

合约可以在耗尽 gas 时自动停用,也可以由合约拥有者手动停用。当合约被停用时,wasmx将调用合约中的特殊deactivate{}回调(如果存在),作为一个sudo消息。合约可以由合约拥有者重新激活。

Fee Grant

Wasmx模块允许其他地址(合约、外部拥有账户EOA)通过x/feegrant模块为其他合约的开始区块执行支付费用。 当合约第一次注册时,用户需要指定FundingMode,该模式指示合约执行的资金来源。支持三种模式:

  • SelfFunded - 合约将为其自身的执行支付费用(默认模式)

  • GrantOnly - 合约将在其关联的津贴覆盖的情况下执行(由ContractRegistrationRequest中的GranterAddress提供)

  • Dual - 合约将优先使用其津贴资金。如果津贴不足以支付执行费用,则会使用其自身资金

由于x/feegrant模块提供了三种津贴(Basic、Periodic和AllowedMsg),wasmx模块仅支持Basic和Periodic。不建议向合约授予AllowedMsgAllowance,因为任何尝试使用此类津贴的合约默认会报错。

暂停(Pausing)、参数更新(Params Update)

合约的拥有者可以随时停用或激活合约,无需治理投票。他们还可以随时更新合约执行的参数,如 gas 价格或 gas 限制。

批量方法(Batch Methods)

为了方便,Wasmx模块提供了一些先前提到的提案的批量版本,例如批量注册和注销,以及StoreCodeProposal的批量版本。这些批量版本允许同时处理多个提案,而不是逐个处理。

数据

RegisteredContract

关于每个合约存储的数据:

type RegisteredContract struct {
	// limit of gas per BB execution
	GasLimit uint64  json:"gas_limit,omitempty"
        // gas price that contract is willing to pay for execution in BeginBlocker
	GasPrice uint64 json:"gas_price,omitempty"
	// is contract currently active
	IsExecutable bool  json:"is_executable,omitempty"
	// code_id that is allowed to be executed (to prevent malicious updates) - if nil/0 any code_id can be executed
	CodeId uint64 json:"code_id,omitempty"ł
	// optional - admin addr that is allowed to update contract data
	AdminAddress string json:"admin_address,omitempty"
	// address of an account providing grant for execution 
	GranterAddress string 
	// enum indicating how contract's execution is funded
	FundMode FundingMode
}

type FundingMode int32

const (
    FundingMode_Unspecified FundingMode = 0
    FundingMode_SelfFunded  FundingMode = 1
    FundingMode_GrantOnly   FundingMode = 2
    FundingMode_Dual        FundingMode = 3
)

提案

ContractRegistrationRequest

ContractRegistrationRequest 是用于注册新合约的基础消息(不应直接使用,而应作为提案的一部分)。

type ContractRegistrationRequest struct {
	ContractAddress string 
	GasLimit uint64 
	GasPrice    uint64 
	PinContract bool   
	AllowUpdating bool
	CodeId uint64
    ContractAdmin string 
	GranterAddress string
	FundMode FundingMode
}

字段描述:

  • ContractAddress - 合约实例的唯一标识符,用于注册该合约。

  • GasLimit - 执行智能合约时可使用的最大 gas。

  • GasPrice - 执行智能合约时使用的 gas 价格。

  • PinContract - 是否应将合约固定在内存中。

  • AllowUpdating - 定义合约拥有者是否可以在不重新注册的情况下迁移合约(如果为false,则只能执行当前的code_id)。

  • CodeId - 正在注册的合约的code_id,在执行时会进行验证,以允许在投票后进行最后的修改。

  • AdminAddress - 可选的管理员账户地址(该地址可以暂停或更新合约参数)。

  • GranterAddress - 提供执行资金的账户地址。如果FundMode不是SelfFunded,则必须设置此字段(见下文解释)。

FundingMode 表示合约将如何为其执行提供资金。

enum FundingMode {
    Unspecified = 0;
    SelfFunded = 1;
    GrantOnly = 2; 
    Dual = 3;      
}
  • SelfFunded - 合约将使用自己的资金来执行。

  • GrantOnly - 合约仅使用由资助提供的资金。

  • Dual - 合约将首先消耗资助的资金,然后再使用自己的资金。

ContractRegistrationRequestProposal

ContractRegistrationRequestProposal 定义了一个 SDK 消息,用于在 wasmx 合约注册表中注册单个合约。

type ContractRegistrationRequestProposal struct {
    Title                       string                      
    Description                 string                      
    ContractRegistrationRequest ContractRegistrationRequest 
}

字段描述:

  • Title - 描述提案的标题。

  • Description - 描述提案的内容。

  • ContractRegistrationRequest - 包含合约注册请求(如上所述)。

BatchContractRegistrationRequestProposal

BatchContractRegistrationRequestProposal 定义了一个 SDK 消息,用于在 wasmx 合约注册表中注册一批合约。

type BatchContractRegistrationRequestProposal struct {
    Title                       string                      
    Description                 string
	ContractRegistrationRequests  []ContractRegistrationRequest 
}

字段描述:

  • Title - 描述提案的标题。

  • Description - 描述提案的内容。

  • ContractRegistrationRequests - 包含合约注册请求的列表(如上所述)。

BatchStoreCodeProposal

BatchStoreCodeProposal 定义了一个 SDK 消息,用于在 wasm 中存储一批合约。

type BatchStoreCodeProposal struct {
    Title                       string                      
    Description                 string
	Proposals   []types.StoreCodeProposal
}

字段描述:

  • Title - 描述提案的标题。

  • Description - 描述提案的内容。

  • Proposals - 包含存储代码提案的列表(由 Cosmos wasm 模块定义)。

BatchContractDeregistrationProposal

BatchContractDeregistrationProposal 定义了一个 SDK 消息,用于在 wasm 中注销一批合约。

type BatchContractDeregistrationProposal struct {
    Title                       string                      
    Description                 string
	Contracts   []string 
}

字段描述:

  • Title - 描述提案的标题。

  • Description - 描述提案的内容。

  • Contracts - 包含要注销的合约地址列表。

消息

MsgUpdateContract

更新已注册合约的执行参数(gas 价格、限制)。还可以定义新的管理员账户。 此操作只能由管理员(如果已定义)或合约本身调用。


type MsgUpdateContract struct {
    Sender string `json:"sender,omitempty"`
    // Unique Identifier for contract instance to be registered.
    ContractAddress string `json:"contract_address,omitempty"`
    // Maximum gas to be used for the smart contract execution.
    GasLimit uint64 `json:"gas_limit,omitempty"`
    // gas price to be used for the smart contract execution.
    GasPrice uint64 `json:"gas_price,omitempty"`
    // optional - admin account that will be allowed to perform any changes
    AdminAddress string `json:"admin_address,omitempty"`
}

MsgDeactivateContract

停用已注册的合约(该合约将不再在开始区块(begin blocker)中执行)。


type MsgDeactivateContract struct {
    Sender string `json:"sender,omitempty"`
    // Unique Identifier for contract instance to be activated.
    ContractAddress string `json:"contract_address,omitempty"`
}

MsgActivateContract

重新激活已注册的合约(从现在起,该合约将在开始区块(begin blocker)中再次执行)。


type MsgActivateContract struct {
    Sender string `json:"sender,omitempty"`
    // Unique Identifier for contract instance to be activated.
    ContractAddress string `json:"contract_address,omitempty"`
}

MsgExecuteContract

调用智能合约中定义的函数。函数和参数通过ExecuteMsg编码,ExecuteMsg是一个以 Base64 编码的 JSON 消息。

type MsgExecuteContract struct {
    Sender     sdk.AccAddress   `json:"sender" yaml:"sender"`
    Contract   sdk.AccAddress   `json:"contract" yaml:"contract"`
    ExecuteMsg core.Base64Bytes `json:"execute_msg" yaml:"execute_msg"`
    Coins      sdk.Coins        `json:"coins" yaml:"coins"`
}

MsgMigrateContract

可以由可迁移智能合约的拥有者发起,以将其code_id重置为另一个code_id。MigrateMsg是一个以 Base64 编码的 JSON 消息。

type MsgMigrateContract struct {
    Owner      sdk.AccAddress   `json:"owner" yaml:"owner"`
    Contract   sdk.AccAddress   `json:"contract" yaml:"contract"`
    NewCodeID  uint64           `json:"new_code_id" yaml:"new_code_id"`
    MigrateMsg core.Base64Bytes `json:"migrate_msg" yaml:"migrate_msg"`
}

MsgUpdateContractOwner

可以由智能合约的拥有者发起,以转移所有权。

type MsgUpdateContractOwner struct {
    Owner    sdk.AccAddress `json:"owner" yaml:"owner"`
    NewOwner sdk.AccAddress `json:"new_owner" yaml:"new_owner"`
    Contract sdk.AccAddress `json:"contract" yaml:"contract"`
}

参数

wasmx模块的子空间是wasmx。

type Params struct {
    // Set the status to active to indicate that contracts can be executed in begin blocker.
    IsExecutionEnabled bool ` json:"is_execution_enabled,omitempty"`
    // Maximum aggregate total gas to be used for the contract executions in the BeginBlocker.
    MaxBeginBlockTotalGas uint64 `json:"max_begin_block_total_gas,omitempty"`
    // the maximum gas limit each individual contract can consume in the BeginBlocker.
    MaxContractGasLimit uint64 `json:"max_contract_gas_limit,omitempty"`
    // min_gas_price defines the minimum gas price the contracts must pay to be executed in the BeginBlocker.
    MinGasPrice uint64 `json:"min_gas_price,omitempty"`
}