Skip to main content

fabric_shim/
lib.rs

1//! Hyperledger Fabric chaincode shim for Rust — chaincode-as-a-service
2//! (CCaaS) only, targeting Fabric 3.x peers.
3//!
4//! The chaincode runs as a gRPC *server*; the peer dials in and drives the
5//! shim protocol over a bidirectional `ChaincodeMessage` stream. This is the
6//! deployment model ChainLaunch uses for all Fabric chaincode.
7//!
8//! ```no_run
9//! use fabric_shim::{Chaincode, ChaincodeStub, Response, Server};
10//!
11//! #[derive(Default)]
12//! struct MyContract;
13//!
14//! #[fabric_shim::async_trait]
15//! impl Chaincode for MyContract {
16//!     async fn invoke(&self, stub: ChaincodeStub) -> Response {
17//!         let (function, _args) = stub.get_function_and_args();
18//!         match function.as_str() {
19//!             "Ping" => Response::success(b"pong".to_vec()),
20//!             other => Response::error(format!("unknown function {other}")),
21//!         }
22//!     }
23//! }
24//!
25//! #[tokio::main]
26//! async fn main() -> Result<(), fabric_shim::Error> {
27//!     Server::from_env()?.start(MyContract).await
28//! }
29//! ```
30
31// Lets macro-generated `fabric_shim::` paths resolve inside this crate too.
32extern crate self as fabric_shim;
33
34pub mod contract;
35mod error;
36mod handler;
37mod iterators;
38pub mod metadata;
39mod response;
40mod server;
41mod stub;
42
43pub use async_trait::async_trait;
44pub use error::{Error, Result};
45/// Contract API macros: `#[contract]` on an impl block, `#[derive(DataType)]`
46/// on parameter/return structs. See [`mod@contract`] for a full example.
47pub use fabric_shim_macros::{contract, DataType};
48pub use iterators::{HistoryQueryIterator, StateQueryIterator};
49pub use response::{Response, ERROR, ERROR_THRESHOLD, OK};
50/// Re-exported for macro-generated code; also handy for chaincode authors.
51pub use serde_json;
52pub use server::{Server, DEFAULT_MAX_MESSAGE_SIZE};
53pub use stub::{create_composite_key, split_composite_key, ChaincodeStub};
54
55/// Re-exported generated protos for advanced use (decoding creator
56/// identities, query results, proposals).
57pub use fabric_shim_protos as protos;
58
59/// A chaincode implementation. `invoke` handles every transaction; `init`
60/// defaults to delegating to `invoke` and only matters for legacy
61/// `--init-required` deployments.
62#[async_trait]
63pub trait Chaincode: Send + Sync + 'static {
64    /// Contract metadata served for `org.hyperledger.fabric:GetMetadata`
65    /// (the system function `fabric-contract-api-go`/`-node` expose, used by
66    /// ChainLaunch's metadata endpoint and UI explorer). Return `Some` and
67    /// the shim answers that function itself, before `invoke` is reached;
68    /// with the default `None` the call falls through to `invoke`.
69    fn metadata(&self) -> Option<metadata::Metadata> {
70        None
71    }
72
73    async fn init(&self, stub: ChaincodeStub) -> Response {
74        self.invoke(stub).await
75    }
76
77    async fn invoke(&self, stub: ChaincodeStub) -> Response;
78}