Skip to content

add grpc vm interface #2270

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

add grpc vm interface #2270

wants to merge 17 commits into from

Conversation

faddat
Copy link
Contributor

@faddat faddat commented May 25, 2025

add grpc vm interface. Can be used with the grpc virtual machine found here:

to start the vm you do:

git clone https://github.com/CosmWasm/wasmvm
cd wasmvm
gh pr checkout 676
cd rpc-serve
cargo run

the grpc vm will serve on port 50051

wasmd is set to connect to wasmvm using the .proto file's interface. This allows us to secure the boundary between the contract execution environment and our chain binary.

syntax = "proto3";

package cosmwasm;

option go_package = "github.com/CosmWasm/wasmd/proto";

// Context message for blockchain-related information
message Context {
  uint64 block_height = 1;
  string sender = 2;
  string chain_id = 3;
}

// WasmVMService: RPC interface for wasmvm
service WasmVMService {
  // Module lifecycle management
  rpc LoadModule(LoadModuleRequest) returns (LoadModuleResponse);
  rpc RemoveModule(RemoveModuleRequest) returns (RemoveModuleResponse);
  rpc PinModule(PinModuleRequest) returns (PinModuleResponse);
  rpc UnpinModule(UnpinModuleRequest) returns (UnpinModuleResponse);
  rpc GetCode(GetCodeRequest) returns (GetCodeResponse); // Retrieve raw WASM bytes

  // Contract execution calls
  rpc Instantiate(InstantiateRequest) returns (InstantiateResponse);
  rpc Execute(ExecuteRequest) returns (ExecuteResponse);
  rpc Query(QueryRequest) returns (QueryResponse);
  rpc Migrate(MigrateRequest) returns (MigrateResponse);
  rpc Sudo(SudoRequest) returns (SudoResponse);
  rpc Reply(ReplyRequest) returns (ReplyResponse);

  // Code analysis
  rpc AnalyzeCode(AnalyzeCodeRequest) returns (AnalyzeCodeResponse);

  // Metrics
  rpc GetMetrics(GetMetricsRequest) returns (GetMetricsResponse);
  rpc GetPinnedMetrics(GetPinnedMetricsRequest) returns (GetPinnedMetricsResponse);

  // IBC Entry Points
  // All IBC calls typically share a similar request/response structure
  // with checksum, context, message, gas limit, and request ID.
  // Their responses usually contain data, gas used, and an error.
  rpc IbcChannelOpen(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcChannelConnect(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcChannelClose(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcPacketReceive(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcPacketAck(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcPacketTimeout(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcSourceCallback(IbcMsgRequest) returns (IbcMsgResponse);
  rpc IbcDestinationCallback(IbcMsgRequest) returns (IbcMsgResponse);
  rpc Ibc2PacketReceive(IbcMsgRequest) returns (IbcMsgResponse);
  rpc Ibc2PacketAck(IbcMsgRequest) returns (IbcMsgResponse);
  rpc Ibc2PacketTimeout(IbcMsgRequest) returns (IbcMsgResponse);
  rpc Ibc2PacketSend(IbcMsgRequest) returns (IbcMsgResponse);
}

// --- Common Message Types ---

message LoadModuleRequest {
  bytes module_bytes = 1;
}

message LoadModuleResponse {
  string checksum = 1; // SHA256 checksum of the module (hex encoded)
  string error = 2;
}

message InstantiateRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module
  Context context = 2;
  bytes init_msg = 3;
  uint64 gas_limit = 4;
  string request_id = 5;
}

message InstantiateResponse {
  string contract_id = 1; // Identifier for the instantiated contract, typically derived from request_id or a unique hash
  bytes data = 2; // Binary response data from the contract
  uint64 gas_used = 3;
  string error = 4;
}

message ExecuteRequest {
  string contract_id = 1; // Hex encoded checksum of the WASM module
  Context context = 2;
  bytes msg = 3;
  uint64 gas_limit = 4;
  string request_id = 5;
}

message ExecuteResponse {
  bytes data = 1;
  uint64 gas_used = 2;
  string error = 3;
}

message QueryRequest {
  string contract_id = 1; // Hex encoded checksum of the WASM module
  Context context = 2;
  bytes query_msg = 3;
  string request_id = 4;
}

message QueryResponse {
  bytes result = 1; // Binary query response data
  string error = 2;
}
 
message MigrateRequest {
  string contract_id = 1; // Hex encoded checksum of the existing contract
  string checksum = 2; // Hex encoded checksum of the new WASM module for migration
  Context context = 3;
  bytes migrate_msg = 4;
  uint64 gas_limit = 5;
  string request_id = 6;
}

message MigrateResponse {
  bytes data = 1;
  uint64 gas_used = 2;
  string error = 3;
}

message SudoRequest {
  string contract_id = 1; // Hex encoded checksum of the WASM module
  Context context = 2;
  bytes msg = 3;
  uint64 gas_limit = 4;
  string request_id = 5;
}

message SudoResponse {
  bytes data = 1;
  uint64 gas_used = 2;
  string error = 3;
}

message ReplyRequest {
  string contract_id = 1; // Hex encoded checksum of the WASM module
  Context context = 2;
  bytes reply_msg = 3;
  uint64 gas_limit = 4;
  string request_id = 5;
}

message ReplyResponse {
  bytes data = 1;
  uint64 gas_used = 2;
  string error = 3;
}

message AnalyzeCodeRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module
}

message AnalyzeCodeResponse {
  repeated string required_capabilities = 1; // Comma-separated list of required capabilities
  bool has_ibc_entry_points = 2; // True if IBC entry points are detected
  string error = 3;
}

// HostService: RPC interface for host function callbacks (used by the VM to call back into the host)
service HostService {
  rpc CallHostFunction(CallHostFunctionRequest) returns (CallHostFunctionResponse);
}

message CallHostFunctionRequest {
  string function_name = 1;
  bytes args = 2; // Binary arguments specific to the host function
  Context context = 3;
  string request_id = 4;
}

message CallHostFunctionResponse {
  bytes result = 1;
  string error = 2;
}

// --- New Message Types for Extended Functionality ---

message RemoveModuleRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module to remove
}

message RemoveModuleResponse {
  string error = 1; // Error message if removal failed
}

message PinModuleRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module to pin
}

message PinModuleResponse {
  string error = 1; // Error message if pinning failed
}

message UnpinModuleRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module to unpin
}

message UnpinModuleResponse {
  string error = 1; // Error message if unpinning failed
}

message GetCodeRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module to retrieve
}

message GetCodeResponse {
  bytes module_bytes = 1; // Raw WASM bytes
  string error = 2;
}

message Metrics {
  uint32 hits_pinned_memory_cache = 1;
  uint32 hits_memory_cache = 2;
  uint32 hits_fs_cache = 3;
  uint32 misses = 4;
  uint64 elements_pinned_memory_cache = 5;
  uint64 elements_memory_cache = 6;
  uint64 size_pinned_memory_cache = 7;
  uint64 size_memory_cache = 8;
}

message GetMetricsRequest {}

message GetMetricsResponse {
  Metrics metrics = 1;
  string error = 2;
}

message PerModuleMetrics {
  uint32 hits = 1;
  uint64 size = 2; // Size of the module in bytes
}

message PinnedMetrics {
  // Map from hex-encoded checksum to its metrics
  map<string, PerModuleMetrics> per_module = 1;
}

message GetPinnedMetricsRequest {}

message GetPinnedMetricsResponse {
  PinnedMetrics pinned_metrics = 1;
  string error = 2;
}

// Generalized IBC Message Request/Response for various IBC entry points
// This structure is reused across all IBC-related RPC calls in WasmVMService
message IbcMsgRequest {
  string checksum = 1; // Hex encoded checksum of the WASM module
  Context context = 2;
  bytes msg = 3; // Binary message for the IBC call
  uint64 gas_limit = 4;
  string request_id = 5;
}

message IbcMsgResponse {
  bytes data = 1; // Binary response data from the contract
  uint64 gas_used = 2;
  string error = 3;
}

Comment on lines +324 to +332
for checksum, metrics := range resp.PinnedMetrics.PerModule {
perModule = append(perModule, wasmvmtypes.PerModuleEntry{
Checksum: wasmvmtypes.Checksum(checksum),
Metrics: wasmvmtypes.PerModuleMetrics{
Hits: metrics.Hits,
Size: metrics.Size,
},
})
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
Comment on lines +477 to +485
for checksum, metrics := range resp.PinnedMetrics.PerModule {
perModule = append(perModule, wasmvmtypes.PerModuleEntry{
Checksum: wasmvmtypes.Checksum(checksum),
Metrics: wasmvmtypes.PerModuleMetrics{
Hits: metrics.Hits,
Size: metrics.Size,
},
})
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
pinosu and others added 8 commits May 26, 2025 15:52
* Bump cosmos-sdk v0.53.0

* remove crisis module

* fix simulations

* Suppress lint errors

* Fix golangci-lint warnings

* Remove unnecessary loop copy

* Updates

* [autofix.ci] apply automated fixes

* fix: Disable IAVL prunning to prevent dangling goroutines in tests

* fix: Try to use newer golangci

* Increase resources for lint

* Update golangci-lint (CosmWasm#2269)

* Remove deprecated golangci-lint configs

* Update golangci-lint

* Apply linter fixes

---------

Co-authored-by: Christoph Otter <chris@confio.gmbh>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Tomasz Kulik <tomek.kulik2@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants