Expand description
Hyperledger Fabric chaincode shim for Rust — chaincode-as-a-service (CCaaS) only, targeting Fabric 3.x peers.
The chaincode runs as a gRPC server; the peer dials in and drives the
shim protocol over a bidirectional ChaincodeMessage stream. This is the
deployment model ChainLaunch uses for all Fabric chaincode.
use fabric_shim::{Chaincode, ChaincodeStub, Response, Server};
#[derive(Default)]
struct MyContract;
#[fabric_shim::async_trait]
impl Chaincode for MyContract {
async fn invoke(&self, stub: ChaincodeStub) -> Response {
let (function, _args) = stub.get_function_and_args();
match function.as_str() {
"Ping" => Response::success(b"pong".to_vec()),
other => Response::error(format!("unknown function {other}")),
}
}
}
#[tokio::main]
async fn main() -> Result<(), fabric_shim::Error> {
Server::from_env()?.start(MyContract).await
}Re-exports§
pub use serde_json;pub use fabric_shim_protos as protos;
Modules§
- contract
- High-level contract API — the Rust equivalent of
fabric-contract-api-goand fabric-chaincode-node’s@Transaction/@Objectdecorators. - metadata
- Contract metadata, served for the
org.hyperledger.fabric:GetMetadatasystem function — the same mechanismfabric-contract-api-goandfabric-chaincode-nodeprovide.
Structs§
- Chaincode
Stub - Per-transaction API handed to
crate::Chaincodeimplementations. - History
Query Iterator - Iterator over
queryresult::KeyModificationrecords fromcrate::ChaincodeStub::get_history_for_key. - Response
- The result of a chaincode transaction, serialized into the peer’s
Responseprotobuf on COMPLETED. - Server
- CCaaS chaincode server: listens for peer connections and runs the shim protocol for each.
- State
Query Iterator - Iterator over
queryresult::Kvrecords from range, partial-composite-key, and rich queries.
Enums§
- Error
- Errors surfaced by the shim to user chaincode and server callers.
Constants§
- DEFAULT_
MAX_ MESSAGE_ SIZE - Default max gRPC message size, matching the Fabric peer (100 MiB).
- ERROR
- Status code for a failed transaction (mirrors Go shim
shim.ERROR). - ERROR_
THRESHOLD - Status codes >= this threshold are treated as errors by the peer.
- OK
- Status code for a successful transaction (mirrors Go shim
shim.OK).
Traits§
- Chaincode
- A chaincode implementation.
invokehandles every transaction;initdefaults to delegating toinvokeand only matters for legacy--init-requireddeployments.
Functions§
- create_
composite_ key - Build a composite key:
\x00{object_type}\x00{attr1}\x00{attr2}\x00... - split_
composite_ key - Split a composite key back into
(object_type, attributes).
Type Aliases§
Attribute Macros§
- async_
trait - contract
- Contract API macros:
#[contract]on an impl block,#[derive(DataType)]on parameter/return structs. Seecontractfor a full example. Turns an impl block into a Fabric contract: routes transactions by name, parses typed arguments, and generatesChaincode::{invoke, metadata}.
Derive Macros§
- Data
Type - Contract API macros:
#[contract]on an impl block,#[derive(DataType)]on parameter/return structs. Seecontractfor a full example. DerivesContractSchema/ContractArg/ContractReturnfor a struct so it can be used as a typed transaction parameter or return value. Field names in the generated JSON schema honor#[serde(rename)]/#[serde(rename_all)], so the schema always matches the serialized JSON.