RRT Heavy16: A Ledger Command Recorded Once
Chapters
- The same shape, more modules
- Validate, then write
- The ledger test
Transcript
The same shape, more modules
This is RRT Heavy16, a much larger application. Its layout is the one we know. Each feature has a folder with its service and a versioned memory folder. The root wires them together. Endpoints are in lib dot r s. The root lists eight children. Four belong to the application, and the ledger's is called work. The other four come from libraries. Rechain keeps a recorded history, its replay module remembers recent commands, and Reactor delivers messages between canisters. A library module is attached just like your own, and you use it through its service.
Validate, then write
The request is to count commands by kind and add up their units. The ledger's data has an owner, five counts, one for each kind, a total, and the last caller. A Rechain reducer has two halves. Prepare is written for any access, so it can only read. It checks the caller against the owner and the units against a range, and it uses checked arithmetic. Each failure returns a typed business error. If everything passes it returns a plan, which holds the new counts and the new total. Nothing has been written. Commit exists only under a write scope. It takes the plan and stores it. It makes no decisions and has nothing to refuse. Rechain calls it after the action has been accepted and recorded. The ledger's Commands service ties it together. It asks Rechain to dispatch the action on the business chain. The reducer it passes is the work view wrapped by the replay module. Replay runs first and recognizes a command it has already seen.
The ledger test
The test sends one command of each kind with five units. Each returns its block number, zero through four. It sends the same dated commands again within the replay window. Keep the original creation time on a retry. Each repeat returns the same block number, and the total stays twenty-five. Two more commands are refused. One comes from a caller who is not the owner. The other asks for zero units. Each returns its typed error from prepare. The chain's next index is still five, so no block was spent on either. Other code never reads the ledger's fields. It uses the ledger's selectors, small read-only methods such as total and owner. Endpoints call them through the root, just as the Shop's endpoints called tax.