github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/types/wasmer_engine.go (about)

     1  package types
     2  
     3  import (
     4  	wasmvm "github.com/CosmWasm/wasmvm"
     5  	wasmvmtypes "github.com/CosmWasm/wasmvm/types"
     6  )
     7  
     8  // DefaultMaxQueryStackSize maximum size of the stack of contract instances doing queries
     9  const DefaultMaxQueryStackSize uint32 = 10
    10  
    11  // WasmerEngine defines the WASM contract runtime engine.
    12  type WasmerEngine interface {
    13  	// Create will compile the wasm code, and store the resulting pre-compile
    14  	// as well as the original code. Both can be referenced later via CodeID
    15  	// This must be done one time for given code, after which it can be
    16  	// instatitated many times, and each instance called many times.
    17  	//
    18  	// For example, the code for all ERC-20 contracts should be the same.
    19  	// This function stores the code for that contract only once, but it can
    20  	// be instantiated with custom inputs in the future.
    21  	Create(code wasmvm.WasmCode) (wasmvm.Checksum, error)
    22  
    23  	// AnalyzeCode will statically analyze the code.
    24  	// Currently just reports if it exposes all IBC entry points.
    25  	AnalyzeCode(checksum wasmvm.Checksum) (*wasmvmtypes.AnalysisReport, error)
    26  
    27  	// Instantiate will create a new contract based on the given codeID.
    28  	// We can set the initMsg (contract "genesis") here, and it then receives
    29  	// an account and address and can be invoked (Execute) many times.
    30  	//
    31  	// Storage should be set with a PrefixedKVStore that this code can safely access.
    32  	//
    33  	// Under the hood, we may recompile the wasm, use a cached native compile, or even use a cached instance
    34  	// for performance.
    35  	Instantiate(
    36  		checksum wasmvm.Checksum,
    37  		env wasmvmtypes.Env,
    38  		info wasmvmtypes.MessageInfo,
    39  		initMsg []byte,
    40  		store wasmvm.KVStore,
    41  		goapi wasmvm.GoAPI,
    42  		querier wasmvm.Querier,
    43  		gasMeter wasmvm.GasMeter,
    44  		gasLimit uint64,
    45  		deserCost wasmvmtypes.UFraction,
    46  	) (*wasmvmtypes.Response, uint64, error)
    47  
    48  	// Execute calls a given contract. Since the only difference between contracts with the same CodeID is the
    49  	// data in their local storage, and their address in the outside world, we need no ContractID here.
    50  	// (That is a detail for the external, sdk-facing, side).
    51  	//
    52  	// The caller is responsible for passing the correct `store` (which must have been initialized exactly once),
    53  	// and setting the env with relevant info on this instance (address, balance, etc)
    54  	Execute(
    55  		code wasmvm.Checksum,
    56  		env wasmvmtypes.Env,
    57  		info wasmvmtypes.MessageInfo,
    58  		executeMsg []byte,
    59  		store wasmvm.KVStore,
    60  		goapi wasmvm.GoAPI,
    61  		querier wasmvm.Querier,
    62  		gasMeter wasmvm.GasMeter,
    63  		gasLimit uint64,
    64  		deserCost wasmvmtypes.UFraction,
    65  	) (*wasmvmtypes.Response, uint64, error)
    66  
    67  	// Query allows a client to execute a contract-specific query. If the result is not empty, it should be
    68  	// valid json-encoded data to return to the client.
    69  	// The meaning of path and data can be determined by the code. Path is the suffix of the abci.QueryRequest.Path
    70  	Query(
    71  		code wasmvm.Checksum,
    72  		env wasmvmtypes.Env,
    73  		queryMsg []byte,
    74  		store wasmvm.KVStore,
    75  		goapi wasmvm.GoAPI,
    76  		querier wasmvm.Querier,
    77  		gasMeter wasmvm.GasMeter,
    78  		gasLimit uint64,
    79  		deserCost wasmvmtypes.UFraction,
    80  	) ([]byte, uint64, error)
    81  
    82  	// Migrate will migrate an existing contract to a new code binary.
    83  	// This takes storage of the data from the original contract and the CodeID of the new contract that should
    84  	// replace it. This allows it to run a migration step if needed, or return an error if unable to migrate
    85  	// the given data.
    86  	//
    87  	// MigrateMsg has some data on how to perform the migration.
    88  	Migrate(
    89  		checksum wasmvm.Checksum,
    90  		env wasmvmtypes.Env,
    91  		migrateMsg []byte,
    92  		store wasmvm.KVStore,
    93  		goapi wasmvm.GoAPI,
    94  		querier wasmvm.Querier,
    95  		gasMeter wasmvm.GasMeter,
    96  		gasLimit uint64,
    97  		deserCost wasmvmtypes.UFraction,
    98  	) (*wasmvmtypes.Response, uint64, error)
    99  
   100  	// Sudo runs an existing contract in read/write mode (like Execute), but is never exposed to external callers
   101  	// (either transactions or government proposals), but can only be called by other native Go modules directly.
   102  	//
   103  	// This allows a contract to expose custom "super user" functions or priviledged operations that can be
   104  	// deeply integrated with native modules.
   105  	Sudo(
   106  		checksum wasmvm.Checksum,
   107  		env wasmvmtypes.Env,
   108  		sudoMsg []byte,
   109  		store wasmvm.KVStore,
   110  		goapi wasmvm.GoAPI,
   111  		querier wasmvm.Querier,
   112  		gasMeter wasmvm.GasMeter,
   113  		gasLimit uint64,
   114  		deserCost wasmvmtypes.UFraction,
   115  	) (*wasmvmtypes.Response, uint64, error)
   116  
   117  	// Reply is called on the original dispatching contract after running a submessage
   118  	Reply(
   119  		checksum wasmvm.Checksum,
   120  		env wasmvmtypes.Env,
   121  		reply wasmvmtypes.Reply,
   122  		store wasmvm.KVStore,
   123  		goapi wasmvm.GoAPI,
   124  		querier wasmvm.Querier,
   125  		gasMeter wasmvm.GasMeter,
   126  		gasLimit uint64,
   127  		deserCost wasmvmtypes.UFraction,
   128  	) (*wasmvmtypes.Response, uint64, error)
   129  
   130  	// GetCode will load the original wasm code for the given code id.
   131  	// This will only succeed if that code id was previously returned from
   132  	// a call to Create.
   133  	//
   134  	// This can be used so that the (short) code id (hash) is stored in the iavl tree
   135  	// and the larger binary blobs (wasm and pre-compiles) are all managed by the
   136  	// rust library
   137  	GetCode(code wasmvm.Checksum) (wasmvm.WasmCode, error)
   138  
   139  	// Cleanup should be called when no longer using this to free resources on the rust-side
   140  	Cleanup()
   141  
   142  	// IBCChannelOpen is available on IBC-enabled contracts and is a hook to call into
   143  	// during the handshake pahse
   144  	IBCChannelOpen(
   145  		checksum wasmvm.Checksum,
   146  		env wasmvmtypes.Env,
   147  		channel wasmvmtypes.IBCChannelOpenMsg,
   148  		store wasmvm.KVStore,
   149  		goapi wasmvm.GoAPI,
   150  		querier wasmvm.Querier,
   151  		gasMeter wasmvm.GasMeter,
   152  		gasLimit uint64,
   153  		deserCost wasmvmtypes.UFraction,
   154  	) (*wasmvmtypes.IBC3ChannelOpenResponse, uint64, error)
   155  
   156  	// IBCChannelConnect is available on IBC-enabled contracts and is a hook to call into
   157  	// during the handshake pahse
   158  	IBCChannelConnect(
   159  		checksum wasmvm.Checksum,
   160  		env wasmvmtypes.Env,
   161  		channel wasmvmtypes.IBCChannelConnectMsg,
   162  		store wasmvm.KVStore,
   163  		goapi wasmvm.GoAPI,
   164  		querier wasmvm.Querier,
   165  		gasMeter wasmvm.GasMeter,
   166  		gasLimit uint64,
   167  		deserCost wasmvmtypes.UFraction,
   168  	) (*wasmvmtypes.IBCBasicResponse, uint64, error)
   169  
   170  	// IBCChannelClose is available on IBC-enabled contracts and is a hook to call into
   171  	// at the end of the channel lifetime
   172  	IBCChannelClose(
   173  		checksum wasmvm.Checksum,
   174  		env wasmvmtypes.Env,
   175  		channel wasmvmtypes.IBCChannelCloseMsg,
   176  		store wasmvm.KVStore,
   177  		goapi wasmvm.GoAPI,
   178  		querier wasmvm.Querier,
   179  		gasMeter wasmvm.GasMeter,
   180  		gasLimit uint64,
   181  		deserCost wasmvmtypes.UFraction,
   182  	) (*wasmvmtypes.IBCBasicResponse, uint64, error)
   183  
   184  	// IBCPacketReceive is available on IBC-enabled contracts and is called when an incoming
   185  	// packet is received on a channel belonging to this contract
   186  	IBCPacketReceive(
   187  		checksum wasmvm.Checksum,
   188  		env wasmvmtypes.Env,
   189  		packet wasmvmtypes.IBCPacketReceiveMsg,
   190  		store wasmvm.KVStore,
   191  		goapi wasmvm.GoAPI,
   192  		querier wasmvm.Querier,
   193  		gasMeter wasmvm.GasMeter,
   194  		gasLimit uint64,
   195  		deserCost wasmvmtypes.UFraction,
   196  	) (*wasmvmtypes.IBCReceiveResult, uint64, error)
   197  
   198  	// IBCPacketAck is available on IBC-enabled contracts and is called when an
   199  	// the response for an outgoing packet (previously sent by this contract)
   200  	// is received
   201  	IBCPacketAck(
   202  		checksum wasmvm.Checksum,
   203  		env wasmvmtypes.Env,
   204  		ack wasmvmtypes.IBCPacketAckMsg,
   205  		store wasmvm.KVStore,
   206  		goapi wasmvm.GoAPI,
   207  		querier wasmvm.Querier,
   208  		gasMeter wasmvm.GasMeter,
   209  		gasLimit uint64,
   210  		deserCost wasmvmtypes.UFraction,
   211  	) (*wasmvmtypes.IBCBasicResponse, uint64, error)
   212  
   213  	// IBCPacketTimeout is available on IBC-enabled contracts and is called when an
   214  	// outgoing packet (previously sent by this contract) will provably never be executed.
   215  	// Usually handled like ack returning an error
   216  	IBCPacketTimeout(
   217  		checksum wasmvm.Checksum,
   218  		env wasmvmtypes.Env,
   219  		packet wasmvmtypes.IBCPacketTimeoutMsg,
   220  		store wasmvm.KVStore,
   221  		goapi wasmvm.GoAPI,
   222  		querier wasmvm.Querier,
   223  		gasMeter wasmvm.GasMeter,
   224  		gasLimit uint64,
   225  		deserCost wasmvmtypes.UFraction,
   226  	) (*wasmvmtypes.IBCBasicResponse, uint64, error)
   227  
   228  	// Pin pins a code to an in-memory cache, such that is
   229  	// always loaded quickly when executed.
   230  	// Pin is idempotent.
   231  	Pin(checksum wasmvm.Checksum) error
   232  
   233  	// Unpin removes the guarantee of a contract to be pinned (see Pin).
   234  	// After calling this, the code may or may not remain in memory depending on
   235  	// the implementor's choice.
   236  	// Unpin is idempotent.
   237  	Unpin(checksum wasmvm.Checksum) error
   238  
   239  	// GetMetrics some internal metrics for monitoring purposes.
   240  	GetMetrics() (*wasmvmtypes.Metrics, error)
   241  }