|
| 1 | +# Custom Endorser Example |
| 2 | + |
| 3 | +This example shows how to build a custom endorser application using the |
| 4 | +[fabric-x-sdk](https://github.com/hyperledger/fabric-x-sdk/). An endorser is like a peer executing chaincode in |
| 5 | +classic Fabric: it receives transaction proposals, reads and writes world state through a |
| 6 | +`SimulationStore`, and returns signed read/write sets. Unlike chaincode, it runs as a standalone |
| 7 | +gRPC service — outside the peer, in your own process. |
| 8 | + |
| 9 | +You will build and run two endorser instances, submit transactions through the included client CLI, |
| 10 | +and see how to wire in your own `Executor` — the single interface you implement to add application |
| 11 | +logic. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- Go 1.26+ |
| 16 | +- Docker (for the test network committer and orderer) |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +### 1. Build |
| 21 | + |
| 22 | +```shell |
| 23 | +make build # compiles bin/endorser and bin/client |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Generate crypto material and start the test network |
| 27 | + |
| 28 | +```shell |
| 29 | +make init-network # generate TLS certs and MSP material (only once) |
| 30 | +make start-network # start committer + orderer in Docker |
| 31 | +``` |
| 32 | + |
| 33 | +### 3. Start the endorsers |
| 34 | + |
| 35 | +Run each in a **separate terminal** — logs stream to stdout so you can see what's happening: |
| 36 | + |
| 37 | +**Terminal 1** |
| 38 | +```shell |
| 39 | +./bin/endorser -c sampleconfig/endorser-org1.yaml |
| 40 | +``` |
| 41 | + |
| 42 | +**Terminal 2** |
| 43 | +```shell |
| 44 | +./bin/endorser -c sampleconfig/endorser-org2.yaml |
| 45 | +``` |
| 46 | + |
| 47 | +Each endorser logs `starting endorser` and then listens for proposals (endorser1 on `:9001`, |
| 48 | +endorser2 on `:9002`). |
| 49 | + |
| 50 | +> [!NOTE] |
| 51 | +> Our "network" consists of two organizations, as defined in [testdata/crypto-config.yaml](./testdata/crypto-config.yaml). |
| 52 | +> The fact that we need two endorsers is defined by the endorsement policy in |
| 53 | +> fxconfig-init container: `--policy=AND('Org1MSP.member', 'Org2MSP.member')`. It means both |
| 54 | +> organizations have to sign the read/write set for it to be accepted on the ledger. |
| 55 | +
|
| 56 | +### 4. Send a transaction |
| 57 | + |
| 58 | +```shell |
| 59 | +# write a value |
| 60 | +./bin/client -c sampleconfig/client.yaml invoke '{"Args":["set", "greeting", "hello world"]}' |
| 61 | + |
| 62 | +# read it back |
| 63 | +./bin/client -c sampleconfig/client.yaml query '{"Args":["get", "greeting"]}' |
| 64 | +``` |
| 65 | + |
| 66 | +`invoke` collects endorsements from both endorsers and submits to the orderer. |
| 67 | +`query` collects endorsements and prints the response payload — it does not submit. |
| 68 | + |
| 69 | +The transaction argument follows the Fabric peer CLI convention: a JSON `Args` array with the |
| 70 | +function name as the first element, or a `{"function": "...", "Args": [...]}` object. |
| 71 | + |
| 72 | +### 5. Tear down |
| 73 | + |
| 74 | +Stop the running endorsers by pressing CTRL+C. Then stop the test container. |
| 75 | + |
| 76 | +```shell |
| 77 | +make stop-network # stop the Docker test network |
| 78 | +``` |
| 79 | + |
| 80 | +## Writing Your Own Executor |
| 81 | + |
| 82 | +The `Executor` interface is the only thing you need to implement to add your own logic: |
| 83 | + |
| 84 | +```go |
| 85 | +type Executor interface { |
| 86 | + Execute(ctx context.Context, newStore StoreFactory, inv endorsement.Invocation) (endorsement.ExecutionResult, error) |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +The included [`SampleExecutor`](./cmd/endorser/executor.go) is a simple key/value getter and setter |
| 91 | +— about 40 lines. It reads from and writes to the `SimulationStore`, which captures the read/write |
| 92 | +set that the endorser will sign. |
| 93 | + |
| 94 | +To plug in your own executor, edit [`cmd/endorser/main.go`](./cmd/endorser/main.go) and add it to |
| 95 | +the `executors` map: |
| 96 | + |
| 97 | +```go |
| 98 | +executors := map[string]service.Executor{ |
| 99 | + "my-namespace": MyExecutor{}, |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Each key is a namespace. The namespace must be registered in the network configuration via |
| 104 | +`fxconfig` (see `testdata/fxconfig-docker.yaml` for the namespace used by this sample). |
| 105 | + |
| 106 | +If the `SimulationStore` does not give you enough control, you can call `store.Result()` early and |
| 107 | +modify the resulting read/write set directly before returning `endorsement.Success(...)`. |
| 108 | + |
| 109 | +## Project Structure |
| 110 | + |
| 111 | +``` |
| 112 | +├── cmd/ |
| 113 | +│ ├── endorser/ # Endorser service entry point + SampleExecutor |
| 114 | +│ └── client/ # Developer client CLI |
| 115 | +├── config/ # Configuration structures |
| 116 | +├── sampleconfig/ # Sample config files (endorser1/2, client) |
| 117 | +├── service/ # Service implementation and integration tests |
| 118 | +├── testdata/ # Network config, crypto-config, and generated crypto material |
| 119 | +├── compose.yml # Docker Compose for committer + orderer |
| 120 | +├── Makefile |
| 121 | +├── go.mod |
| 122 | +└── README.md |
| 123 | +``` |
| 124 | + |
| 125 | +## Configuration |
| 126 | + |
| 127 | +### Files |
| 128 | + |
| 129 | +See the `sampleconfig/` folder for endorser and client configs, and `testdata/` for the network |
| 130 | +and crypto material used by the test environment. |
| 131 | + |
| 132 | +### TLS Modes |
| 133 | + |
| 134 | +| Mode | Description | |
| 135 | +| ------ | ------------------------------------------------------ | |
| 136 | +| `none` | No TLS — for local development without crypto material | |
| 137 | +| `tls` | One-sided TLS — server certificate only | |
| 138 | +| `mtls` | Mutual TLS — both sides present certificates | |
| 139 | + |
| 140 | +### Environment Variables |
| 141 | + |
| 142 | +Any config field can be overridden with the `ENDORSER_` prefix: |
| 143 | + |
| 144 | +```shell |
| 145 | +ENDORSER_SERVER_ENDPOINT_PORT=8080 ./bin/endorser -c sampleconfig/endorser-org1.yaml |
| 146 | +``` |
| 147 | + |
| 148 | +## Core Dependencies |
| 149 | + |
| 150 | +- [`fabric-x-sdk`](https://github.com/hyperledger/fabric-x-sdk) — endorsement building, block |
| 151 | + delivery, identity, versioned state |
| 152 | +- [`fabric-x-committer`](https://github.com/hyperledger/fabric-x-committer) — `utils/connection` |
| 153 | + for gRPC server setup |
| 154 | +- [`fabric-x-common`](https://github.com/hyperledger/fabric-x-common) — config parsing |
0 commit comments