Skip to main content

Crate fabric_shim

Crate fabric_shim 

Source
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-go and fabric-chaincode-node’s @Transaction/@Object decorators.
metadata
Contract metadata, served for the org.hyperledger.fabric:GetMetadata system function — the same mechanism fabric-contract-api-go and fabric-chaincode-node provide.

Structs§

ChaincodeStub
Per-transaction API handed to crate::Chaincode implementations.
HistoryQueryIterator
Iterator over queryresult::KeyModification records from crate::ChaincodeStub::get_history_for_key.
Response
The result of a chaincode transaction, serialized into the peer’s Response protobuf on COMPLETED.
Server
CCaaS chaincode server: listens for peer connections and runs the shim protocol for each.
StateQueryIterator
Iterator over queryresult::Kv records 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. invoke handles every transaction; init defaults to delegating to invoke and only matters for legacy --init-required deployments.

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§

Result

Attribute Macros§

async_trait
contract
Contract API macros: #[contract] on an impl block, #[derive(DataType)] on parameter/return structs. See contract for a full example. Turns an impl block into a Fabric contract: routes transactions by name, parses typed arguments, and generates Chaincode::{invoke, metadata}.

Derive Macros§

DataType
Contract API macros: #[contract] on an impl block, #[derive(DataType)] on parameter/return structs. See contract for a full example. Derives ContractSchema/ContractArg/ContractReturn for 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.