This document explains how to run trac-peer, connect it to an existing MSB network, create/join a subnet, and test the built‑in demo app (“contract”) (ping + set), plus the HTTP RPC API used by wallets/apps.
If you’re building your own app (Protocol + Contract) and want wallet/dapp integration details, see APP_DEV.md.
It’s written to be usable even if you’re not deeply familiar with P2P/blockchain systems.
If something doesn’t work, jump to Troubleshooting at the end.
- MSB (Main Settlement Bus) is the settlement layer. A transaction becomes “real” when MSB accepts it (validators confirm it).
- trac-peer is a subnet runner. A subnet is a smaller P2P network with:
- a shared ordered log (who wrote what, in what order)
- a deterministic app/contract state derived from that log
- Subnet nodes only execute a transaction locally once they can prove it exists in MSB (confirmed).
In practice:
- You run an MSB network (you already have this).
- You run one or more
trac-peernodes that join that MSB network (each trac-peer starts a small “MSB client node” in-process so it can talk to MSB). - Those peers form (or join) a subnet and replicate state P2P.
- When you call
/tx, trac-peer broadcasts the tx to MSB. - When MSB confirms it, the subnet appends an op referencing that MSB tx and all peers execute the same contract logic and derive the same state.
- Node.js + npm (recommended: a modern Node LTS).
- The Pear CLI is optional.
peer:pearautomatically uses legacypear runwhen it detects Pear v2, and uses the embeddedpear-runtimeworker for Pear v3 (or when Pear is not installed). - You need your MSB bootstrap (32‑byte hex / 64 hex characters) and MSB channel (string).
From the trac-peer repo folder:
npm install- Store: a local folder where a node saves its data (keys, logs, derived state). In this repo it defaults to
stores/<name>/.... - MSB bootstrap: the unique ID of the MSB network you are joining (32 bytes, hex string length 64).
- MSB channel: a discovery topic used to find the MSB network peers (must match your MSB network).
- Subnet bootstrap: the unique ID of a subnet (32 bytes, hex string length 64). Everyone in the same subnet must use the same subnet bootstrap.
- Subnet channel: a discovery topic used to find other subnet peers. Everyone in the same subnet must use the same subnet channel.
- Peer pubkey (hex): your trac-peer identity key (public). Used for subnet permissions (admin).
- Peer writer key (hex): the key that identifies a subnet writer (used for add/remove writer/indexer).
- Peer MSB address: the MSB “account/address” derived from your peer keypair. MSB must fund it so MSB fee checks pass.
Important safety note:
- Do not share your
Secret Keyprinted by/get_keys.
- Start peer:
npm run peer:run -- --msb-bootstrap=<hex32> --msb-channel=<channel>- Copy the printed
Peer MSB address: trac1...and fund it on your MSB admin node. - In the peer console:
/deploy_subnet
/add_admin --address <your-peer-publicKey-hex>
/tx --command "ping hello"
/get --key app/ping/<tx-hash> --confirmed falseThe Node runner starts:
- a local MSB client node (in-process) that joins your MSB network (new store), and
- the
trac-peersubnet node on top of it.
Basic:
npm run peer:run -- \
--msb-bootstrap=<32-byte-hex> \
--msb-channel=<channel-string>If you want separate store names (recommended when running multiple peers on one machine):
npm run peer:run -- \
--msb-bootstrap=<32-byte-hex> \
--msb-channel=<channel-string> \
--msb-store-name=peer-msb-1 \
--peer-store-name=peer1Notes:
- In
zsh, always put all flags on the same command or use\line continuations. If you start a new line without\, your shell will treat it as a new command.
The Pear compatibility runner uses the same command on Pear v2 and v3. It delegates to legacy
pear run on v2; on v3, where that command no longer exists, it launches the peer as a Bare worker
through the embedded pear-runtime module. If no Pear platform version can be detected, it safely
uses the module path.
Recommended: set store names explicitly (this avoids confusion when running multiple nodes on one machine).
npm run peer:pear -- \
--msb-bootstrap=<32-byte-hex> \
--msb-channel=<channel-string> \
--msb-store-name=peer-msb-1 \
--peer-store-name=peer1Notes:
- If
--peer-store-nameis omitted, you can pass an optional first positional arg (Pear “store label”) and it will be used as the peer store name. - To start a fresh subnet, delete
stores/<peer-store-name>/subnet-bootstrap.hex(and optionally the whole store folder).
After a peer starts, it prints something like:
Peer MSB address: trac1...
On your already-running MSB admin node, fund that address.
Why?
- MSB validators reject txs from addresses that don’t exist in MSB state or that have insufficient balance to pay fees.
Until the address is funded, you’ll see errors like:
Requester address not found in state
In the peer console:
/deploy_subnetThis registers the subnet bootstrap + subnet channel with MSB so TX settlement is allowed.
Admin is stored in subnet state (admin key). Admin can manage writers/indexers and chat moderation.
In the peer console:
/add_admin --address <your-peer-publicKey-hex>You can find your public key:
- printed on startup as
Peer pubkey (hex): ..., or - via
/get_keys(public key only; never share secret key).
Verify:
/get --key adminNotes:
/add_adminis allowed only once, and only from the bootstrap node.
The default runner includes a demo protocol + demo contract:
pingwritesapp/ping/<txHash>setwritesapp/kv/<key>
/tx --command "ping hello"You should see MSB TX broadcasted: <hash> and then after confirmation an appended... message.
Read what the contract wrote:
/get --key app/ping/<tx-hash>/tx --command "set foo bar"
/get --key app/kv/fooYou should see value: "bar" plus metadata (tx hash, from).
You’ll run three terminals. The important rule is: each node must use a different store, but all nodes must point to the same MSB bootstrap/channel and the same subnet bootstrap/channel.
npm run peer:run -- \
--msb-bootstrap=<msb-bootstrap-hex32> \
--msb-channel=<msb-channel> \
--msb-store-name=peer-msb-1 \
--peer-store-name=peer1Then:
- Fund the printed
Peer MSB address: trac1...on your MSB admin node. - In Peer 1 console:
/deploy_subnet
/add_admin --address <peer1-publicKey-hex>
/set_chat_status --enabled 1
/set_nick --nick "admin"npm run peer:run -- \
--msb-bootstrap=<msb-bootstrap-hex32> \
--msb-channel=<msb-channel> \
--msb-store-name=peer-msb-2 \
--peer-store-name=peer2 \
--subnet-bootstrap=<subnet-bootstrap-hex32> \
--subnet-channel=trac-peer-subnetPeer 2 will print:
Peer Writer: <peer2-writerKey-hex>Peer pubkey (hex): <peer2-publicKey-hex>Peer MSB address: trac1...(fund it on MSB too, otherwise Peer 2 cannot settle/tx)
On Peer 1 (admin), add Peer 2 as writer (and optionally indexer):
/add_writer --key <peer2-writerKey-hex>
/add_indexer --key <peer2-writerKey-hex>Start Peer 3 exactly like Peer 2 but do not add its writer key as a writer:
npm run peer:run -- \
--msb-bootstrap=<msb-bootstrap-hex32> \
--msb-channel=<msb-channel> \
--msb-store-name=peer-msb-3 \
--peer-store-name=peer3 \
--subnet-bootstrap=<subnet-bootstrap-hex32> \
--subnet-channel=trac-peer-subnetPeer 3 should replicate state (you’ll see connections in /stats) but it won’t be able to append /tx unless an admin adds its writer key.
- On Peer 2:
/set_nick --nick "writer2"
/post --message "hello from peer2"- On Peer 1 (admin), send a tx:
/tx --command "set shared hello-world"- On Peer 3 (reader), read the state:
/get --key app/kv/sharedTo simulate multiple nodes, run multiple peers with different stores.
Peer 1 prints the subnet bootstrap (or stores it to stores/<peer-store>/subnet-bootstrap.hex).
Use the same subnet bootstrap and channel:
npm run peer:run -- \
--msb-bootstrap=<msb-bootstrap-hex32> \
--msb-channel=<msb-channel> \
--msb-store-name=peer-msb-2 \
--peer-store-name=peer2 \
--subnet-bootstrap=<subnet-bootstrap-hex32> \
--subnet-channel=trac-peer-subnetIf you started Peer 1 with the Pear compatibility runner, use the same runner for Peer 2:
npm run peer:pear -- \
--msb-bootstrap=<msb-bootstrap-hex32> \
--msb-channel=<msb-channel> \
--msb-store-name=peer-msb-2 \
--peer-store-name=peer2 \
--subnet-bootstrap=<subnet-bootstrap-hex32> \
--subnet-channel=trac-peer-subnet- A peer is writer when it has permission to append to the subnet log.
- A peer is reader when it can replicate but not append.
Admin can add a writer by writer key:
/add_writer --key <peer2-writerKey-hex>You can see a peer’s writer key in its startup banner: Peer Writer: ...
Chat is disabled by default.
- Enable chat (admin-only):
/set_chat_status --enabled 1- Set a nickname:
/set_nick --nick "alice"- Post a message:
/post --message "Hello from peer1"Messages are replicated like any other subnet op.
RPC is an HTTP server that runs alongside your peer and lets a wallet/dApp connect via URL (Ethereum-style).
Important: operator/admin controls (deploy subnet, writer/indexer management, chat moderation) are CLI-only and are not exposed via RPC.
npm run peer:run-rpc -- \
--msb-bootstrap=<hex32> \
--msb-channel=<channel> \
--api-tx-exposed \
--rpc-host=127.0.0.1 \
--rpc-port=5001This command uses the same automatic Pear v2/v3 selection described above.
npm run peer:pear-rpc -- \
--msb-bootstrap=<hex32> \
--msb-channel=<channel> \
--msb-store-name=peer-msb-rpc \
--peer-store-name=peer-rpc \
--api-tx-exposed \
--rpc-host=127.0.0.1 \
--rpc-port=5001- ✅
GET http://127.0.0.1:5001/v1/health - ❌
GET http://127.0.0.1:5001/health(returns 404)
- Status:
GET /v1/status
- Contract schema (JSON Schema; contract tx types + Protocol API methods):
GET /v1/contract/schema
- Read state:
GET /v1/state?key=app%2Fkv%2Ffoo&confirmed=true
- Client tx flow (dapp constructs, wallet signs):
GET /v1/contract/nonceGET /v1/contract/tx/context(returns MSB tx context for client-side tx derivation)POST /v1/contract/txbody:{ "tx": "<hex32>", "prepared_command": { ... }, "address": "<pubkey-hex32>", "signature": "<hex64>", "nonce": "<hex32>", "sim": true|false }
Notes:
- To allow wallet tx submission, start the peer with
--rpcand--api-tx-exposed(or envPEER_API_TX_EXPOSED=1+PEER_RPC=1). - The peer must be subnet-writable (writer) to broadcast a tx.
The runner uses demo protocol/contract files under src/dev/ (wired in scripts/run-peer.mjs) so you can test quickly.
For a real app, you typically:
-
Create a custom Protocol that maps user commands to typed operations:
- implement
mapTxCommand(command) -> { type, value }
- implement
-
Create a custom Contract that deterministically handles those ops and writes to state:
addFunction("yourOp")or schemas- implement
async yourOp() { await this.put("app/...", ...) }
All nodes in the subnet must run the same Protocol/Contract logic for deterministic results.
When you run /tx --command "..." in the CLI (or a client uses the RPC tx flow), the flow is:
- The command string is mapped into an operation object:
{ type, value }. - trac-peer hashes and signs the operation and broadcasts a settlement tx to MSB.
- trac-peer waits until its local MSB view confirms that tx.
- trac-peer appends a subnet op that references the confirmed MSB tx (so every subnet node can verify it).
- Every subnet node applies the subnet op and runs contract logic locally, deriving the same results from the same ordered log.
Where does step (1) happen?
- In the demo runner (
scripts/run-peer.mjs) it’s in the protocol class’smapTxCommand(...)(example:src/dev/tuxemonProtocol.js). - The base protocol method is
Protocol.mapTxCommand(...)insrc/protocol.js. For your own app you override that function.
Client tx flow specifics:
- The client fetches MSB tx context from
GET /v1/contract/tx/context. - The client computes
command_hash = blake3(utf8Bytes(JSON.stringify(prepared_command))), then computestxfrom the MSB preimage fields +nonce. Hash the UTF-8 bytes of the JSON string, not the JS string itself. - Peers accept that digest and, for transactions from clients before 0.4.10, the previous digest of the string (
txContentHashpeer option:utf8default,legacyto keep producing the previous digest while a network upgrades,utf8-strictto accept UTF-8 digests only). - The wallet signs
tx. - The client submits the signed payload to
POST /v1/contract/txwithsim: trueto simulate (recommended), thensim: falseto broadcast.
If you want to “start over”, stop the peer and delete its store folder(s):
rm -rf stores/peer1 stores/peer2 stores/peer3If you only want to change subnet bootstrap generation for one peer, delete just:
stores/<peer-store-name>/subnet-bootstrap.hex
MSB doesn’t know your Peer MSB address yet. Fund it on the MSB admin node.
One of your bootstraps is the wrong length. It must be exactly 64 hex characters (32 bytes). Common causes:
- copy/paste includes whitespace
- you accidentally repeated the bootstrap twice in the command
- you passed a non-hex string
Usually:
- peer MSB address not funded, or
- MSB validator connectivity issue.
This happens when a new store needs to generate a new keypair and Pear’s runtime provides a broken TextEncoder.
trac-peer includes a runtime workaround so you should just re-run after updating; if it persists, confirm you are on the latest workspace state.
Use versioned routes: /v1/health.