brainchain-verify 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/dist/abi.js +113 -0
- package/dist/canonical.js +39 -0
- package/dist/cli.js +172 -0
- package/dist/deployments.js +48 -0
- package/dist/verify.js +389 -0
- package/package.json +36 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BrainChain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,146 @@
|
|
|
1
|
-
#
|
|
1
|
+
# brainchain-verify
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Check any BrainChain decision against the chain. The tool reads the decision from the `DecisionLog`, fetches the record Jev's answer was published in, recomputes its hashes and confidence, and applies the task's thresholds the way the contracts do. It needs a public RPC and nothing else: no keys, no account.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npx brainchain-verify mainnet 1
|
|
7
|
+
npx brainchain-verify mainnet --task 11 --set v3-1
|
|
8
|
+
npx brainchain-verify testnet 100 --json
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Node.js 22 or later.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```text
|
|
16
|
+
brainchain-verify <mainnet|testnet|chainId> <decisionId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
17
|
+
brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
| Argument | Meaning |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| `mainnet`, `testnet` or a chain id | `mainnet` is chain 274663 at `https://rpc.usebrainchain.com`; `testnet` is chain 274630 at `https://rpc.testnet.usebrainchain.com`. |
|
|
23
|
+
| `<decisionId>` | The decision's number in its `DecisionLog`, from 1. |
|
|
24
|
+
| `--task <id>` | Check every decision about one task, oldest first. |
|
|
25
|
+
| `--set <TaskBoard>` | A retired contract set, by its TaskBoard address or by the key a decision page uses (`?set=v3-1`). Without it, the active set. Each set numbers its tasks and decisions from 1. |
|
|
26
|
+
| `--rpc <url>` | Another RPC for the same chain. |
|
|
27
|
+
| `--json` | One JSON object on stdout. |
|
|
28
|
+
|
|
29
|
+
The package carries the active and retired contract sets from [`deployments/`](../../deployments) as they stood when it was built. An unknown `--set` lists them.
|
|
30
|
+
|
|
31
|
+
## Output
|
|
32
|
+
|
|
33
|
+
```text
|
|
34
|
+
$ npx brainchain-verify mainnet 22 --set v3-1
|
|
35
|
+
Decision 22 on BrainChain mainnet, contract set v3-1 (DecisionLog 0xbe8E68EbDEaf2C65bc13D8AD070d17098Be6d335)
|
|
36
|
+
|
|
37
|
+
ok read from DecisionLog 0xbe8E…d335: grade, recorded 2026-11-01 00:40:23 UTC in block 419, confidence 8000 bps
|
|
38
|
+
ok fetched the record from https://brain.usebrainchain.com/records/0xc6570be10e86227b06d9ce78b739c83dac8d62e9f9cf33b67f25b7d148e8182a.json
|
|
39
|
+
ok record hash matches payloadHash 0xc657…182a
|
|
40
|
+
ok request hash matches inputHash 0x7f5f…55ca
|
|
41
|
+
ok kind: grade
|
|
42
|
+
ok subject: task 11 on board 0x6fBF…f6e0, chain 274663
|
|
43
|
+
ok probabilities cover exactly the options Jev was asked
|
|
44
|
+
ok answer: accept, recorded as 0x0000…0001
|
|
45
|
+
ok confidence: 0.8 is 8000 bps, as recorded
|
|
46
|
+
ok the question quotes task 11's brief, as committed on chain
|
|
47
|
+
ok the levels Jev scored against are the brief's, lowest first
|
|
48
|
+
ok confidence recomputed: paid levels 1, 2 hold 0.46 + 0.34 = 0.8000, so accept at 0.8000
|
|
49
|
+
ok threshold: accept at 80% is at or above the 70% grade threshold fixed when task 11 was posted, so the work is accepted, as the board recorded
|
|
50
|
+
|
|
51
|
+
Verdict: match. The published record is exactly what the chain recorded, and the chain applied it as its contracts require.
|
|
52
|
+
Not checked: that Jev produced the answer. See https://docs.usebrainchain.com/trust-model
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Each line is one check. `ok` passed and `FAIL` means the record and the chain disagree. `skip` is a check that could not run, with the reason on the line. `ERROR` means the record host did not answer after three tries, so the checks that need the record did not run.
|
|
56
|
+
|
|
57
|
+
## What it checks
|
|
58
|
+
|
|
59
|
+
| Check | What passes |
|
|
60
|
+
| --- | --- |
|
|
61
|
+
| `read` | `DecisionLog.get(id)` and the decision's `Recorded` event agree. |
|
|
62
|
+
| `fetch` | The record at the event's `payloadURI` (https, http or a base64 `data:` URI, with redirects refused) is a decision record. |
|
|
63
|
+
| `record-hash` | `keccak256` of the fetched bytes equals `payloadHash`. |
|
|
64
|
+
| `input-hash` | `keccak256` of the record's `request`, serialized canonically (keys sorted, no whitespace), equals `inputHash`. |
|
|
65
|
+
| `kind` | The record's kind is the one on chain. |
|
|
66
|
+
| `subject` | A task decision names `TaskBoard.subjectOf(task)` for this chain and board. A change names its exact call. |
|
|
67
|
+
| `options` | The probabilities cover exactly the options in the question. |
|
|
68
|
+
| `answer` | The record's answer encodes to the one on chain: the hired model's id or worker address, or 1 for accept, pay, adopt and allow and 0 for their opposites. |
|
|
69
|
+
| `confidence` | The record's confidence times 10,000, rounded, equals `confidenceBps`. |
|
|
70
|
+
| `top-option` | For a hire, ruling or change, the answer is Jev's most likely option. |
|
|
71
|
+
| `confidence-recomputed` | The confidence follows from the probabilities. A hire uses `(p - 1/n) / (1 - 1/n)`, within 0.02 because the probabilities are rounded to two decimals. A grade sums the probabilities on the paid levels. A ruling or change uses the chosen option's probability. |
|
|
72
|
+
| `brief` | The question quotes the task's brief, fetched from its `Posted` event and checked against `briefHash`. |
|
|
73
|
+
| `levels` | A grade or ruling used the brief's own quality levels and pass level. |
|
|
74
|
+
| `threshold` | The threshold fixed when the task was posted, applied as `TaskBoard.hire`, `grade` and `rule` do, gives the outcome the board emitted. A change is queued only on a yes. |
|
|
75
|
+
| `change-subject`, `change-call` | A queued change is the call the decision names and the call Jev was shown. |
|
|
76
|
+
|
|
77
|
+
Some checks skip by design:
|
|
78
|
+
|
|
79
|
+
- A record for work Jev did not accept is served without the work, so `record-hash` and `input-hash` cannot match it. The answer, confidence and threshold are still checked.
|
|
80
|
+
- A decision the board never applied, because a later decision or a closed window settled the task, skips `threshold`.
|
|
81
|
+
- A grade recorded before the chain gated on the paid levels skips `confidence-recomputed`: its confidence is Jev's score confidence, which the probabilities alone do not give.
|
|
82
|
+
|
|
83
|
+
## What it cannot check
|
|
84
|
+
|
|
85
|
+
That Jev produced the answer. BrainChain records Jev's answers, and the contracts bound what a recorded answer can do. A decision that passes every check has the question, answer and probabilities the chain acted on. The [trust model](https://docs.usebrainchain.com/trust-model) explains what the chain relies on and what bounds it.
|
|
86
|
+
|
|
87
|
+
## Exit status
|
|
88
|
+
|
|
89
|
+
| Code | Meaning |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| 0 | Every check that could run passed. Skips do not fail a decision. |
|
|
92
|
+
| 1 | A check failed, a record could not be fetched, or the decision could not be read: an unknown id, a task with no decisions, an RPC error. |
|
|
93
|
+
| 2 | Bad arguments, or an RPC that serves another chain. |
|
|
94
|
+
|
|
95
|
+
## JSON
|
|
96
|
+
|
|
97
|
+
`--json` prints one object. `decisions` holds one entry, or one per decision with `--task`. `verdict` is `match`, `mismatch` or `unverified` (a record could not be fetched). Each check keeps its `name` across releases, and its `status` is `ok`, `fail`, `skip` or `error`. When the tool cannot run, it prints `{ "error": "..." }` and exits 1. Trimmed to one check:
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"network": "BrainChain mainnet",
|
|
102
|
+
"chainId": 274663,
|
|
103
|
+
"set": {
|
|
104
|
+
"key": "v3-1",
|
|
105
|
+
"release": "3.1.0",
|
|
106
|
+
"fromBlock": 282,
|
|
107
|
+
"board": "0x6fBF25F036F18C7AE3b1B7343ED8d16Dc695f6e0",
|
|
108
|
+
"log": "0xbe8E68EbDEaf2C65bc13D8AD070d17098Be6d335",
|
|
109
|
+
"executor": "0xC5442fEFDa6B55f48a1FCfc195a8737D7EfB829B"
|
|
110
|
+
},
|
|
111
|
+
"verdict": "match",
|
|
112
|
+
"failed": 0,
|
|
113
|
+
"errors": 0,
|
|
114
|
+
"skipped": 0,
|
|
115
|
+
"decisions": [
|
|
116
|
+
{
|
|
117
|
+
"id": 22,
|
|
118
|
+
"kind": "grade",
|
|
119
|
+
"task": 11,
|
|
120
|
+
"block": 419,
|
|
121
|
+
"recordedAt": "2026-11-01 00:40:23 UTC",
|
|
122
|
+
"confidenceBps": 8000,
|
|
123
|
+
"payloadURI": "https://brain.usebrainchain.com/records/0xc6570be10e86227b06d9ce78b739c83dac8d62e9f9cf33b67f25b7d148e8182a.json",
|
|
124
|
+
"verdict": "match",
|
|
125
|
+
"checks": [
|
|
126
|
+
{ "name": "record-hash", "status": "ok", "message": "record hash matches payloadHash 0xc657…182a" }
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
],
|
|
130
|
+
"notChecked": "That Jev produced the answer. BrainChain records Jev's answers, and the contracts bound what a recorded answer can do.",
|
|
131
|
+
"trustModel": "https://docs.usebrainchain.com/trust-model"
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Building
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
npm install
|
|
139
|
+
npm run build
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`build` reads `deployments/*.json` at the repository root into `src/deployments.ts` and compiles to `dist/`. `npm run abi` regenerates `src/abi.ts` from a `forge build` at the root.
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT
|
package/dist/abi.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Generated by scripts/abi.mjs from usebrainchain/brainchain 3ce2602. Do not edit.
|
|
2
|
+
export const decisionLogAbi = [
|
|
3
|
+
{ "type": "function", "name": "acceptOwnership", "inputs": [], "outputs": [], "stateMutability": "nonpayable" },
|
|
4
|
+
{ "type": "function", "name": "brain", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
5
|
+
{ "type": "function", "name": "count", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" },
|
|
6
|
+
{ "type": "function", "name": "get", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "decision", "type": "tuple", "internalType": "struct DecisionLog.Decision", "components": [{ "name": "kind", "type": "uint8", "internalType": "enum DecisionLog.Kind" }, { "name": "subject", "type": "bytes32", "internalType": "bytes32" }, { "name": "inputHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "answer", "type": "bytes32", "internalType": "bytes32" }, { "name": "confidenceBps", "type": "uint16", "internalType": "uint16" }, { "name": "recordedAt", "type": "uint64", "internalType": "uint64" }] }], "stateMutability": "view" },
|
|
7
|
+
{ "type": "function", "name": "latestOf", "inputs": [{ "name": "subject", "type": "bytes32", "internalType": "bytes32" }, { "name": "kind", "type": "uint8", "internalType": "enum DecisionLog.Kind" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" },
|
|
8
|
+
{ "type": "function", "name": "owner", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
9
|
+
{ "type": "function", "name": "pendingOwner", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
10
|
+
{ "type": "function", "name": "record", "inputs": [{ "name": "kind", "type": "uint8", "internalType": "enum DecisionLog.Kind" }, { "name": "subject", "type": "bytes32", "internalType": "bytes32" }, { "name": "inputHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "answer", "type": "bytes32", "internalType": "bytes32" }, { "name": "confidenceBps", "type": "uint16", "internalType": "uint16" }, { "name": "payloadHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "payloadURI", "type": "string", "internalType": "string" }], "outputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" },
|
|
11
|
+
{ "type": "function", "name": "setBrain", "inputs": [{ "name": "brain_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
12
|
+
{ "type": "function", "name": "transferOwnership", "inputs": [{ "name": "pendingOwner_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
13
|
+
{ "type": "event", "name": "BrainSet", "inputs": [{ "name": "brain", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
14
|
+
{ "type": "event", "name": "OwnerSet", "inputs": [{ "name": "owner", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
15
|
+
{ "type": "event", "name": "OwnershipTransferStarted", "inputs": [{ "name": "owner", "type": "address", "indexed": true, "internalType": "address" }, { "name": "pendingOwner", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
16
|
+
{ "type": "event", "name": "Recorded", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "kind", "type": "uint8", "indexed": true, "internalType": "enum DecisionLog.Kind" }, { "name": "subject", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "inputHash", "type": "bytes32", "indexed": false, "internalType": "bytes32" }, { "name": "answer", "type": "bytes32", "indexed": false, "internalType": "bytes32" }, { "name": "confidenceBps", "type": "uint16", "indexed": false, "internalType": "uint16" }, { "name": "payloadHash", "type": "bytes32", "indexed": false, "internalType": "bytes32" }, { "name": "payloadURI", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false },
|
|
17
|
+
];
|
|
18
|
+
export const taskBoardAbi = [
|
|
19
|
+
{ "type": "function", "name": "MAX_DURATION", "inputs": [], "outputs": [{ "name": "", "type": "uint64", "internalType": "uint64" }], "stateMutability": "view" },
|
|
20
|
+
{ "type": "function", "name": "MAX_FEE_BPS", "inputs": [], "outputs": [{ "name": "", "type": "uint16", "internalType": "uint16" }], "stateMutability": "view" },
|
|
21
|
+
{ "type": "function", "name": "MAX_WINDOW", "inputs": [], "outputs": [{ "name": "", "type": "uint64", "internalType": "uint64" }], "stateMutability": "view" },
|
|
22
|
+
{ "type": "function", "name": "config", "inputs": [], "outputs": [{ "name": "hireThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "gradeThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "ruleThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "minDuration", "type": "uint64", "internalType": "uint64" }, { "name": "maxDuration", "type": "uint64", "internalType": "uint64" }, { "name": "gradeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "disputeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "ruleWindow", "type": "uint64", "internalType": "uint64" }, { "name": "minBounty", "type": "uint128", "internalType": "uint128" }], "stateMutability": "view" },
|
|
23
|
+
{ "type": "function", "name": "count", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" },
|
|
24
|
+
{ "type": "function", "name": "decisionUsed", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" },
|
|
25
|
+
{ "type": "function", "name": "decisions", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract DecisionLog" }], "stateMutability": "view" },
|
|
26
|
+
{ "type": "function", "name": "dispute", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
27
|
+
{ "type": "function", "name": "feeBps", "inputs": [], "outputs": [{ "name": "", "type": "uint16", "internalType": "uint16" }], "stateMutability": "view" },
|
|
28
|
+
{ "type": "function", "name": "feeRouter", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
29
|
+
{ "type": "function", "name": "finalize", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
30
|
+
{ "type": "function", "name": "get", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "tuple", "internalType": "struct TaskBoard.Task", "components": [{ "name": "poster", "type": "address", "internalType": "address" }, { "name": "bounty", "type": "uint128", "internalType": "uint128" }, { "name": "deadline", "type": "uint64", "internalType": "uint64" }, { "name": "status", "type": "uint8", "internalType": "enum TaskBoard.Status" }, { "name": "accepted", "type": "bool", "internalType": "bool" }, { "name": "disputed", "type": "bool", "internalType": "bool" }, { "name": "disputedByModel", "type": "bool", "internalType": "bool" }, { "name": "model", "type": "bytes32", "internalType": "bytes32" }, { "name": "briefHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "resultHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "submittedAt", "type": "uint64", "internalType": "uint64" }, { "name": "gradedAt", "type": "uint64", "internalType": "uint64" }, { "name": "disputedAt", "type": "uint64", "internalType": "uint64" }, { "name": "postedAt", "type": "uint64", "internalType": "uint64" }, { "name": "payee", "type": "address", "internalType": "address" }, { "name": "feeRecipient", "type": "address", "internalType": "address" }, { "name": "feeBps", "type": "uint16", "internalType": "uint16" }, { "name": "hireThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "gradeThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "ruleThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "gradeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "disputeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "ruleWindow", "type": "uint64", "internalType": "uint64" }] }], "stateMutability": "view" },
|
|
31
|
+
{ "type": "function", "name": "grade", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
32
|
+
{ "type": "function", "name": "guardian", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
33
|
+
{ "type": "function", "name": "hire", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
34
|
+
{ "type": "function", "name": "models", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract ModelRegistry" }], "stateMutability": "view" },
|
|
35
|
+
{ "type": "function", "name": "owner", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
36
|
+
{ "type": "function", "name": "paused", "inputs": [], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" },
|
|
37
|
+
{ "type": "function", "name": "post", "inputs": [{ "name": "bounty", "type": "uint128", "internalType": "uint128" }, { "name": "deadline", "type": "uint64", "internalType": "uint64" }, { "name": "briefHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "briefURI", "type": "string", "internalType": "string" }], "outputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" },
|
|
38
|
+
{ "type": "function", "name": "rule", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
39
|
+
{ "type": "function", "name": "setConfig", "inputs": [{ "name": "config_", "type": "tuple", "internalType": "struct TaskBoard.Config", "components": [{ "name": "hireThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "gradeThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "ruleThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "minDuration", "type": "uint64", "internalType": "uint64" }, { "name": "maxDuration", "type": "uint64", "internalType": "uint64" }, { "name": "gradeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "disputeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "ruleWindow", "type": "uint64", "internalType": "uint64" }, { "name": "minBounty", "type": "uint128", "internalType": "uint128" }] }], "outputs": [], "stateMutability": "nonpayable" },
|
|
40
|
+
{ "type": "function", "name": "setFee", "inputs": [{ "name": "feeBps_", "type": "uint16", "internalType": "uint16" }, { "name": "feeRouter_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
41
|
+
{ "type": "function", "name": "setGuardian", "inputs": [{ "name": "guardian_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
42
|
+
{ "type": "function", "name": "setOwner", "inputs": [{ "name": "owner_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
43
|
+
{ "type": "function", "name": "setPaused", "inputs": [{ "name": "paused_", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
44
|
+
{ "type": "function", "name": "setWorkerAllowed", "inputs": [{ "name": "account", "type": "address", "internalType": "address" }, { "name": "allowed", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
45
|
+
{ "type": "function", "name": "staking", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract WorkerStaking" }], "stateMutability": "view" },
|
|
46
|
+
{ "type": "function", "name": "subjectOf", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "bytes32", "internalType": "bytes32" }], "stateMutability": "view" },
|
|
47
|
+
{ "type": "function", "name": "submit", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }, { "name": "resultHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "resultURI", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
48
|
+
{ "type": "function", "name": "token", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract IERC20" }], "stateMutability": "view" },
|
|
49
|
+
{ "type": "function", "name": "workerAllowed", "inputs": [{ "name": "", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" },
|
|
50
|
+
{ "type": "event", "name": "BountyPaid", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "model", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "account", "type": "address", "indexed": true, "internalType": "address" }, { "name": "bounty", "type": "uint128", "indexed": false, "internalType": "uint128" }, { "name": "fee", "type": "uint128", "indexed": false, "internalType": "uint128" }], "anonymous": false },
|
|
51
|
+
{ "type": "event", "name": "BountyRefunded", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "poster", "type": "address", "indexed": true, "internalType": "address" }, { "name": "amount", "type": "uint128", "indexed": false, "internalType": "uint128" }, { "name": "reason", "type": "uint8", "indexed": false, "internalType": "enum TaskBoard.RefundReason" }], "anonymous": false },
|
|
52
|
+
{ "type": "event", "name": "ConfigSet", "inputs": [{ "name": "config", "type": "tuple", "indexed": false, "internalType": "struct TaskBoard.Config", "components": [{ "name": "hireThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "gradeThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "ruleThresholdBps", "type": "uint16", "internalType": "uint16" }, { "name": "minDuration", "type": "uint64", "internalType": "uint64" }, { "name": "maxDuration", "type": "uint64", "internalType": "uint64" }, { "name": "gradeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "disputeWindow", "type": "uint64", "internalType": "uint64" }, { "name": "ruleWindow", "type": "uint64", "internalType": "uint64" }, { "name": "minBounty", "type": "uint128", "internalType": "uint128" }] }], "anonymous": false },
|
|
53
|
+
{ "type": "event", "name": "FeeSet", "inputs": [{ "name": "feeBps", "type": "uint16", "indexed": false, "internalType": "uint16" }, { "name": "feeRouter", "type": "address", "indexed": false, "internalType": "address" }], "anonymous": false },
|
|
54
|
+
{ "type": "event", "name": "FeeTaken", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "recipient", "type": "address", "indexed": true, "internalType": "address" }, { "name": "fee", "type": "uint128", "indexed": false, "internalType": "uint128" }], "anonymous": false },
|
|
55
|
+
{ "type": "event", "name": "GradeDisputed", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "opener", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
56
|
+
{ "type": "event", "name": "GuardianSet", "inputs": [{ "name": "guardian", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
57
|
+
{ "type": "event", "name": "HireHeld", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "indexed": true, "internalType": "uint256" }], "anonymous": false },
|
|
58
|
+
{ "type": "event", "name": "OwnerSet", "inputs": [{ "name": "owner", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
59
|
+
{ "type": "event", "name": "PausedSet", "inputs": [{ "name": "paused", "type": "bool", "indexed": false, "internalType": "bool" }], "anonymous": false },
|
|
60
|
+
{ "type": "event", "name": "Posted", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "poster", "type": "address", "indexed": true, "internalType": "address" }, { "name": "bounty", "type": "uint128", "indexed": false, "internalType": "uint128" }, { "name": "deadline", "type": "uint64", "indexed": false, "internalType": "uint64" }, { "name": "briefHash", "type": "bytes32", "indexed": false, "internalType": "bytes32" }, { "name": "briefURI", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false },
|
|
61
|
+
{ "type": "event", "name": "Ruled", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "paid", "type": "bool", "indexed": false, "internalType": "bool" }, { "name": "confident", "type": "bool", "indexed": false, "internalType": "bool" }], "anonymous": false },
|
|
62
|
+
{ "type": "event", "name": "WorkGraded", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "accepted", "type": "bool", "indexed": false, "internalType": "bool" }], "anonymous": false },
|
|
63
|
+
{ "type": "event", "name": "WorkSubmitted", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "model", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "resultHash", "type": "bytes32", "indexed": false, "internalType": "bytes32" }, { "name": "resultURI", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false },
|
|
64
|
+
{ "type": "event", "name": "WorkerAllowedSet", "inputs": [{ "name": "account", "type": "address", "indexed": true, "internalType": "address" }, { "name": "allowed", "type": "bool", "indexed": false, "internalType": "bool" }], "anonymous": false },
|
|
65
|
+
{ "type": "event", "name": "WorkerHired", "inputs": [{ "name": "id", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "model", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "decisionId", "type": "uint256", "indexed": true, "internalType": "uint256" }], "anonymous": false },
|
|
66
|
+
];
|
|
67
|
+
export const modelRegistryAbi = [
|
|
68
|
+
{ "type": "function", "name": "get", "inputs": [{ "name": "id", "type": "bytes32", "internalType": "bytes32" }], "outputs": [{ "name": "model", "type": "tuple", "internalType": "struct ModelRegistry.Model", "components": [{ "name": "account", "type": "address", "internalType": "address" }, { "name": "price", "type": "uint128", "internalType": "uint128" }, { "name": "active", "type": "bool", "internalType": "bool" }] }], "stateMutability": "view" },
|
|
69
|
+
{ "type": "function", "name": "ids", "inputs": [], "outputs": [{ "name": "", "type": "bytes32[]", "internalType": "bytes32[]" }], "stateMutability": "view" },
|
|
70
|
+
{ "type": "function", "name": "owner", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
71
|
+
{ "type": "function", "name": "setModel", "inputs": [{ "name": "id", "type": "bytes32", "internalType": "bytes32" }, { "name": "account", "type": "address", "internalType": "address" }, { "name": "price", "type": "uint128", "internalType": "uint128" }, { "name": "active", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
72
|
+
{ "type": "function", "name": "setOwner", "inputs": [{ "name": "owner_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
73
|
+
{ "type": "event", "name": "ModelSet", "inputs": [{ "name": "id", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "account", "type": "address", "indexed": true, "internalType": "address" }, { "name": "price", "type": "uint128", "indexed": false, "internalType": "uint128" }, { "name": "active", "type": "bool", "indexed": false, "internalType": "bool" }], "anonymous": false },
|
|
74
|
+
{ "type": "event", "name": "OwnerSet", "inputs": [{ "name": "owner", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
75
|
+
];
|
|
76
|
+
export const decisionExecutorAbi = [
|
|
77
|
+
{ "type": "function", "name": "GRACE_PERIOD", "inputs": [], "outputs": [{ "name": "", "type": "uint64", "internalType": "uint64" }], "stateMutability": "view" },
|
|
78
|
+
{ "type": "function", "name": "MAX_TIMELOCK", "inputs": [], "outputs": [{ "name": "", "type": "uint64", "internalType": "uint64" }], "stateMutability": "view" },
|
|
79
|
+
{ "type": "function", "name": "MIN_TIMELOCK", "inputs": [], "outputs": [{ "name": "", "type": "uint64", "internalType": "uint64" }], "stateMutability": "view" },
|
|
80
|
+
{ "type": "function", "name": "bindBoard", "inputs": [{ "name": "board_", "type": "address", "internalType": "contract TaskBoard" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
81
|
+
{ "type": "function", "name": "board", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract TaskBoard" }], "stateMutability": "view" },
|
|
82
|
+
{ "type": "function", "name": "changeCount", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" },
|
|
83
|
+
{ "type": "function", "name": "changeSubject", "inputs": [{ "name": "target", "type": "address", "internalType": "address" }, { "name": "data", "type": "bytes", "internalType": "bytes" }], "outputs": [{ "name": "", "type": "bytes32", "internalType": "bytes32" }], "stateMutability": "view" },
|
|
84
|
+
{ "type": "function", "name": "changeThresholdBps", "inputs": [], "outputs": [{ "name": "", "type": "uint16", "internalType": "uint16" }], "stateMutability": "view" },
|
|
85
|
+
{ "type": "function", "name": "decisionUsed", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" },
|
|
86
|
+
{ "type": "function", "name": "decisions", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract DecisionLog" }], "stateMutability": "view" },
|
|
87
|
+
{ "type": "function", "name": "execute", "inputs": [{ "name": "changeId", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
88
|
+
{ "type": "function", "name": "getChange", "inputs": [{ "name": "changeId", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "tuple", "internalType": "struct DecisionExecutor.Change", "components": [{ "name": "target", "type": "address", "internalType": "address" }, { "name": "data", "type": "bytes", "internalType": "bytes" }, { "name": "eta", "type": "uint64", "internalType": "uint64" }, { "name": "executed", "type": "bool", "internalType": "bool" }, { "name": "vetoed", "type": "bool", "internalType": "bool" }] }], "stateMutability": "view" },
|
|
89
|
+
{ "type": "function", "name": "guardian", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" },
|
|
90
|
+
{ "type": "function", "name": "houseSpendPerDay", "inputs": [], "outputs": [{ "name": "", "type": "uint128", "internalType": "uint128" }], "stateMutability": "view" },
|
|
91
|
+
{ "type": "function", "name": "houseSpentOnDay", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint128", "internalType": "uint128" }], "stateMutability": "view" },
|
|
92
|
+
{ "type": "function", "name": "houseTasksOnDay", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint32", "internalType": "uint32" }], "stateMutability": "view" },
|
|
93
|
+
{ "type": "function", "name": "houseTasksPerDay", "inputs": [], "outputs": [{ "name": "", "type": "uint32", "internalType": "uint32" }], "stateMutability": "view" },
|
|
94
|
+
{ "type": "function", "name": "maxHouseBounty", "inputs": [], "outputs": [{ "name": "", "type": "uint128", "internalType": "uint128" }], "stateMutability": "view" },
|
|
95
|
+
{ "type": "function", "name": "models", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract ModelRegistry" }], "stateMutability": "view" },
|
|
96
|
+
{ "type": "function", "name": "postHouseTask", "inputs": [{ "name": "bounty", "type": "uint128", "internalType": "uint128" }, { "name": "deadline", "type": "uint64", "internalType": "uint64" }, { "name": "briefHash", "type": "bytes32", "internalType": "bytes32" }, { "name": "briefURI", "type": "string", "internalType": "string" }], "outputs": [{ "name": "taskId", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" },
|
|
97
|
+
{ "type": "function", "name": "queue", "inputs": [{ "name": "decisionId", "type": "uint256", "internalType": "uint256" }, { "name": "target", "type": "address", "internalType": "address" }, { "name": "data", "type": "bytes", "internalType": "bytes" }], "outputs": [{ "name": "changeId", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" },
|
|
98
|
+
{ "type": "function", "name": "setGuardian", "inputs": [{ "name": "guardian_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
99
|
+
{ "type": "function", "name": "setParameters", "inputs": [{ "name": "changeThresholdBps_", "type": "uint16", "internalType": "uint16" }, { "name": "timelock_", "type": "uint64", "internalType": "uint64" }, { "name": "houseTasksPerDay_", "type": "uint32", "internalType": "uint32" }, { "name": "maxHouseBounty_", "type": "uint128", "internalType": "uint128" }, { "name": "houseSpendPerDay_", "type": "uint128", "internalType": "uint128" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
100
|
+
{ "type": "function", "name": "timelock", "inputs": [], "outputs": [{ "name": "", "type": "uint64", "internalType": "uint64" }], "stateMutability": "view" },
|
|
101
|
+
{ "type": "function", "name": "token", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "contract IERC20" }], "stateMutability": "view" },
|
|
102
|
+
{ "type": "function", "name": "transfer", "inputs": [{ "name": "to", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
103
|
+
{ "type": "function", "name": "veto", "inputs": [{ "name": "changeId", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" },
|
|
104
|
+
{ "type": "function", "name": "vetoedCall", "inputs": [{ "name": "", "type": "bytes32", "internalType": "bytes32" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" },
|
|
105
|
+
{ "type": "event", "name": "BoardBound", "inputs": [{ "name": "board", "type": "address", "indexed": true, "internalType": "address" }, { "name": "models", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
106
|
+
{ "type": "event", "name": "Executed", "inputs": [{ "name": "changeId", "type": "uint256", "indexed": true, "internalType": "uint256" }], "anonymous": false },
|
|
107
|
+
{ "type": "event", "name": "GuardianSet", "inputs": [{ "name": "guardian", "type": "address", "indexed": true, "internalType": "address" }], "anonymous": false },
|
|
108
|
+
{ "type": "event", "name": "HouseTaskPosted", "inputs": [{ "name": "taskId", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "bounty", "type": "uint128", "indexed": false, "internalType": "uint128" }], "anonymous": false },
|
|
109
|
+
{ "type": "event", "name": "ParametersSet", "inputs": [{ "name": "changeThresholdBps", "type": "uint16", "indexed": false, "internalType": "uint16" }, { "name": "timelock", "type": "uint64", "indexed": false, "internalType": "uint64" }, { "name": "houseTasksPerDay", "type": "uint32", "indexed": false, "internalType": "uint32" }, { "name": "maxHouseBounty", "type": "uint128", "indexed": false, "internalType": "uint128" }, { "name": "houseSpendPerDay", "type": "uint128", "indexed": false, "internalType": "uint128" }], "anonymous": false },
|
|
110
|
+
{ "type": "event", "name": "Queued", "inputs": [{ "name": "changeId", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "decisionId", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "target", "type": "address", "indexed": true, "internalType": "address" }, { "name": "data", "type": "bytes", "indexed": false, "internalType": "bytes" }, { "name": "eta", "type": "uint64", "indexed": false, "internalType": "uint64" }], "anonymous": false },
|
|
111
|
+
{ "type": "event", "name": "Transferred", "inputs": [{ "name": "to", "type": "address", "indexed": true, "internalType": "address" }, { "name": "amount", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false },
|
|
112
|
+
{ "type": "event", "name": "Vetoed", "inputs": [{ "name": "changeId", "type": "uint256", "indexed": true, "internalType": "uint256" }], "anonymous": false },
|
|
113
|
+
];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The brain's canonical JSON: object keys sorted recursively, no whitespace.
|
|
2
|
+
// Hashes on chain commit to this exact text, so it must not drift.
|
|
3
|
+
function serialize(value, seen) {
|
|
4
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
5
|
+
return JSON.stringify(value);
|
|
6
|
+
if (typeof value === "number") {
|
|
7
|
+
if (!Number.isFinite(value))
|
|
8
|
+
throw new TypeError("Canonical JSON does not support non-finite numbers");
|
|
9
|
+
return JSON.stringify(value);
|
|
10
|
+
}
|
|
11
|
+
if (typeof value !== "object")
|
|
12
|
+
throw new TypeError(`Canonical JSON does not support ${typeof value}`);
|
|
13
|
+
if (seen.has(value))
|
|
14
|
+
throw new TypeError("Canonical JSON does not support cyclic values");
|
|
15
|
+
seen.add(value);
|
|
16
|
+
try {
|
|
17
|
+
if (Array.isArray(value)) {
|
|
18
|
+
for (let i = 0; i < value.length; i++) {
|
|
19
|
+
if (!Object.hasOwn(value, i))
|
|
20
|
+
throw new TypeError("Canonical JSON does not support sparse arrays");
|
|
21
|
+
}
|
|
22
|
+
return `[${value.map((item) => serialize(item, seen)).join(",")}]`;
|
|
23
|
+
}
|
|
24
|
+
const prototype = Object.getPrototypeOf(value);
|
|
25
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
26
|
+
throw new TypeError("Canonical JSON only supports plain objects");
|
|
27
|
+
const source = value;
|
|
28
|
+
const entries = Object.keys(source)
|
|
29
|
+
.sort()
|
|
30
|
+
.map((key) => `${JSON.stringify(key)}:${serialize(source[key], seen)}`);
|
|
31
|
+
return `{${entries.join(",")}}`;
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
seen.delete(value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function canonicalStringify(value) {
|
|
38
|
+
return serialize(value, new Set());
|
|
39
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createPublicClient, http, isAddress } from "viem";
|
|
3
|
+
import { decisionLogAbi, taskBoardAbi } from "./abi.js";
|
|
4
|
+
import { NETWORKS } from "./deployments.js";
|
|
5
|
+
import { decisionsOf, errorMessage, verifyDecision } from "./verify.js";
|
|
6
|
+
const USAGE = `Usage:
|
|
7
|
+
brainchain-verify <mainnet|testnet|chainId> <decisionId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
8
|
+
brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
9
|
+
|
|
10
|
+
Checks a BrainChain decision against the chain: reads it from the DecisionLog,
|
|
11
|
+
fetches the published record, recomputes its hashes and confidence, and applies
|
|
12
|
+
the task's thresholds as the contracts do. Needs no keys.
|
|
13
|
+
|
|
14
|
+
--task <id> check every decision about one task
|
|
15
|
+
--set <board> a retired contract set, by TaskBoard address or by its key on
|
|
16
|
+
the site (v3-1); without it, the active set
|
|
17
|
+
--rpc <url> another RPC for the same chain
|
|
18
|
+
--json one JSON object on stdout
|
|
19
|
+
|
|
20
|
+
Exit status: 0 when every check that could run matches, 1 on a mismatch or when
|
|
21
|
+
a decision or its record cannot be read, 2 on bad arguments.`;
|
|
22
|
+
const TRUST_MODEL = "https://docs.usebrainchain.com/trust-model";
|
|
23
|
+
function usage(message) {
|
|
24
|
+
console.error(`${message}\n\n${USAGE}`);
|
|
25
|
+
process.exit(2);
|
|
26
|
+
}
|
|
27
|
+
const args = process.argv.slice(2);
|
|
28
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
29
|
+
console.log(USAGE);
|
|
30
|
+
process.exit(0);
|
|
31
|
+
}
|
|
32
|
+
const flag = (name) => {
|
|
33
|
+
const at = args.indexOf(name);
|
|
34
|
+
if (at < 0)
|
|
35
|
+
return undefined;
|
|
36
|
+
const value = args[at + 1];
|
|
37
|
+
if (value === undefined || value.startsWith("--"))
|
|
38
|
+
usage(`${name} needs a value`);
|
|
39
|
+
args.splice(at, 2);
|
|
40
|
+
return value;
|
|
41
|
+
};
|
|
42
|
+
const json = args.includes("--json");
|
|
43
|
+
if (json)
|
|
44
|
+
args.splice(args.indexOf("--json"), 1);
|
|
45
|
+
const taskArg = flag("--task");
|
|
46
|
+
const setArg = flag("--set");
|
|
47
|
+
const rpcArg = flag("--rpc");
|
|
48
|
+
const unknown = args.find((arg) => arg.startsWith("-"));
|
|
49
|
+
if (unknown)
|
|
50
|
+
usage(`Unknown option ${unknown}`);
|
|
51
|
+
const [networkArg, idArg, ...extra] = args;
|
|
52
|
+
if (networkArg === undefined)
|
|
53
|
+
usage("Name a network: mainnet, testnet or a chain id");
|
|
54
|
+
if (extra.length > 0 || (taskArg !== undefined && idArg !== undefined))
|
|
55
|
+
usage("Too many arguments");
|
|
56
|
+
const target = taskArg ?? idArg;
|
|
57
|
+
if (target === undefined)
|
|
58
|
+
usage("Name a decision id, or a task with --task");
|
|
59
|
+
if (!/^[1-9]\d*$/.test(target))
|
|
60
|
+
usage(`${taskArg === undefined ? "A decision" : "A task"} id is a whole number from 1`);
|
|
61
|
+
const foundNetwork = NETWORKS[networkArg] ?? Object.values(NETWORKS).find((n) => String(n.chainId) === networkArg);
|
|
62
|
+
if (!foundNetwork)
|
|
63
|
+
usage(`Unknown network "${networkArg}": use mainnet, testnet, 274663 or 274630`);
|
|
64
|
+
const network = foundNetwork;
|
|
65
|
+
const foundSet = setArg === undefined
|
|
66
|
+
? network.sets[0]
|
|
67
|
+
: network.sets.find((s) => (isAddress(setArg) ? s.board.toLowerCase() === setArg.toLowerCase() : s.key === setArg));
|
|
68
|
+
if (!foundSet) {
|
|
69
|
+
const sets = network.sets.map((s, i) => `${s.key} (TaskBoard ${s.board}${i === 0 ? ", active" : ""})`);
|
|
70
|
+
usage(`No contract set ${setArg} on ${network.name}. Sets: ${sets.join(", ")}`);
|
|
71
|
+
}
|
|
72
|
+
const set = foundSet;
|
|
73
|
+
const ctx = {
|
|
74
|
+
client: createPublicClient({ transport: http(rpcArg ?? network.rpc) }),
|
|
75
|
+
network,
|
|
76
|
+
set,
|
|
77
|
+
briefs: new Map(),
|
|
78
|
+
};
|
|
79
|
+
function fail(message) {
|
|
80
|
+
if (json)
|
|
81
|
+
console.log(JSON.stringify({ error: message }, null, 2));
|
|
82
|
+
else
|
|
83
|
+
console.error(message);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
}
|
|
86
|
+
const TAGS = { ok: "ok", fail: "FAIL", skip: "skip", error: "ERROR" };
|
|
87
|
+
const tally = (checks, status) => checks.filter((c) => c.status === status).length;
|
|
88
|
+
const outcome = (checks) => tally(checks, "fail") > 0 ? "mismatch" : tally(checks, "error") > 0 ? "unverified" : "match";
|
|
89
|
+
function verdict(failed, errors, skipped, many) {
|
|
90
|
+
if (failed > 0)
|
|
91
|
+
return `Verdict: MISMATCH. ${failed} check${failed === 1 ? "" : "s"} failed; see the FAIL lines.`;
|
|
92
|
+
if (errors > 0)
|
|
93
|
+
return "Verdict: UNVERIFIED. A record could not be fetched, so its checks did not run; see the ERROR lines and try again.";
|
|
94
|
+
if (skipped > 0)
|
|
95
|
+
return "Verdict: match. Every check that could run passed; the skip lines say what could not be checked.";
|
|
96
|
+
return many
|
|
97
|
+
? "Verdict: match. The published records are exactly what the chain recorded, and the chain applied them as its contracts require."
|
|
98
|
+
: "Verdict: match. The published record is exactly what the chain recorded, and the chain applied it as its contracts require.";
|
|
99
|
+
}
|
|
100
|
+
function report(reports, header, task) {
|
|
101
|
+
const all = reports.flatMap((r) => r.checks);
|
|
102
|
+
const failed = tally(all, "fail");
|
|
103
|
+
const errors = tally(all, "error");
|
|
104
|
+
const skipped = tally(all, "skip");
|
|
105
|
+
if (json) {
|
|
106
|
+
const decisions = reports.map(({ checks, ...r }) => ({
|
|
107
|
+
id: r.id,
|
|
108
|
+
kind: r.kind,
|
|
109
|
+
task: r.task,
|
|
110
|
+
block: r.block,
|
|
111
|
+
recordedAt: r.recordedAt,
|
|
112
|
+
confidenceBps: r.confidenceBps,
|
|
113
|
+
payloadURI: r.payloadURI,
|
|
114
|
+
verdict: outcome(checks),
|
|
115
|
+
checks,
|
|
116
|
+
}));
|
|
117
|
+
const out = {
|
|
118
|
+
network: network.name,
|
|
119
|
+
chainId: network.chainId,
|
|
120
|
+
set,
|
|
121
|
+
...(task === undefined ? {} : { task }),
|
|
122
|
+
verdict: outcome(all),
|
|
123
|
+
failed,
|
|
124
|
+
errors,
|
|
125
|
+
skipped,
|
|
126
|
+
decisions,
|
|
127
|
+
notChecked: "That Jev produced the answer. BrainChain records Jev's answers, and the contracts bound what a recorded answer can do.",
|
|
128
|
+
trustModel: TRUST_MODEL,
|
|
129
|
+
};
|
|
130
|
+
console.log(JSON.stringify(out, null, 2));
|
|
131
|
+
process.exit(failed + errors > 0 ? 1 : 0);
|
|
132
|
+
}
|
|
133
|
+
console.log(header);
|
|
134
|
+
for (const r of reports) {
|
|
135
|
+
console.log(task === undefined ? "" : `\nDecision ${r.id}`);
|
|
136
|
+
for (const c of r.checks)
|
|
137
|
+
console.log(` ${TAGS[c.status].padEnd(5)} ${c.message}`);
|
|
138
|
+
}
|
|
139
|
+
console.log(`\n${verdict(failed, errors, skipped, reports.length > 1)}`);
|
|
140
|
+
console.log(`Not checked: that Jev produced the answer. See ${TRUST_MODEL}`);
|
|
141
|
+
process.exit(failed + errors > 0 ? 1 : 0);
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
const chainId = await ctx.client.getChainId();
|
|
145
|
+
if (chainId !== network.chainId)
|
|
146
|
+
usage(`The RPC serves chain ${chainId}, not ${network.name} (${network.chainId})`);
|
|
147
|
+
const where = `on ${network.name}, contract set ${set.key}`;
|
|
148
|
+
if (taskArg !== undefined) {
|
|
149
|
+
const task = BigInt(taskArg);
|
|
150
|
+
const tasks = await ctx.client.readContract({ address: set.board, abi: taskBoardAbi, functionName: "count" });
|
|
151
|
+
if (task > tasks)
|
|
152
|
+
fail(`TaskBoard ${set.board} (set ${set.key}) holds tasks 1 to ${tasks}`);
|
|
153
|
+
const ids = await decisionsOf(ctx, task);
|
|
154
|
+
if (ids.length === 0)
|
|
155
|
+
fail(`Task ${task} has no decisions ${where}`);
|
|
156
|
+
const reports = [];
|
|
157
|
+
for (const id of ids)
|
|
158
|
+
reports.push(await verifyDecision(ctx, id));
|
|
159
|
+
report(reports, `Task ${task} ${where} (TaskBoard ${set.board}): decision${ids.length === 1 ? "" : "s"} ${ids.join(", ")}`, Number(task));
|
|
160
|
+
}
|
|
161
|
+
const id = BigInt(target);
|
|
162
|
+
const decisions = await ctx.client.readContract({ address: set.log, abi: decisionLogAbi, functionName: "count" });
|
|
163
|
+
if (id > decisions) {
|
|
164
|
+
const others = network.sets.filter((s) => s !== set).map((s) => s.key);
|
|
165
|
+
const hint = others.length > 0 ? ` Each set numbers its own decisions; try --set ${others.join(" or ")}.` : "";
|
|
166
|
+
fail(`DecisionLog ${set.log} (set ${set.key}) holds decisions 1 to ${decisions}.${hint}`);
|
|
167
|
+
}
|
|
168
|
+
report([await verifyDecision(ctx, id)], `Decision ${id} ${where} (DecisionLog ${set.log})`);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
fail(errorMessage(error));
|
|
172
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const NETWORKS = {
|
|
2
|
+
"mainnet": {
|
|
3
|
+
"chainId": 274663,
|
|
4
|
+
"name": "BrainChain mainnet",
|
|
5
|
+
"rpc": "https://rpc.usebrainchain.com",
|
|
6
|
+
"sets": [
|
|
7
|
+
{
|
|
8
|
+
"key": "v3-3",
|
|
9
|
+
"release": "3.3.0",
|
|
10
|
+
"fromBlock": 425,
|
|
11
|
+
"board": "0x72C890AA996bF7efcA88e3F5063B626560215160",
|
|
12
|
+
"log": "0x5FfED27750E5Ca990aB60751Fc4B25E39358FcD7",
|
|
13
|
+
"executor": "0xA03614B09Dcc3813eD665a1693123f2a4f2C2483"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"key": "v3-1",
|
|
17
|
+
"release": "3.1.0",
|
|
18
|
+
"fromBlock": 282,
|
|
19
|
+
"board": "0x6fBF25F036F18C7AE3b1B7343ED8d16Dc695f6e0",
|
|
20
|
+
"log": "0xbe8E68EbDEaf2C65bc13D8AD070d17098Be6d335",
|
|
21
|
+
"executor": "0xC5442fEFDa6B55f48a1FCfc195a8737D7EfB829B"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"key": "v3",
|
|
25
|
+
"release": "3.0.0",
|
|
26
|
+
"fromBlock": 101,
|
|
27
|
+
"board": "0x33825Bc40E43cC7f8A301D6b7aC79eDCDb92ACa7",
|
|
28
|
+
"log": "0x78314f2d6f29E79ADAdbA9ebcd10cAD7Aa3f205f",
|
|
29
|
+
"executor": "0xdE2e18fB117832EEefD5895646550E0BDC166d0b"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"testnet": {
|
|
34
|
+
"chainId": 274630,
|
|
35
|
+
"name": "BrainChain testnet",
|
|
36
|
+
"rpc": "https://rpc.testnet.usebrainchain.com",
|
|
37
|
+
"sets": [
|
|
38
|
+
{
|
|
39
|
+
"key": "v3-1",
|
|
40
|
+
"release": "3.1.0",
|
|
41
|
+
"fromBlock": 898,
|
|
42
|
+
"board": "0xC74564F100e70F95346A6e399ebf1Ce14309C8B6",
|
|
43
|
+
"log": "0xc7C5663c1cBC231596F770acafb88326f248492D",
|
|
44
|
+
"executor": "0x420484B139001ED5B1DA330e689Cc2f4814001Fb"
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
};
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { BaseError, decodeFunctionData, encodeAbiParameters, keccak256, pad, toBytes, } from "viem";
|
|
2
|
+
import { decisionExecutorAbi, decisionLogAbi, modelRegistryAbi, taskBoardAbi } from "./abi.js";
|
|
3
|
+
import { canonicalStringify } from "./canonical.js";
|
|
4
|
+
const Kind = { Hire: 1, Grade: 2, Ruling: 3, Change: 4 };
|
|
5
|
+
const KINDS = { 1: "hire", 2: "grade", 3: "ruling", 4: "change" };
|
|
6
|
+
const YES = `0x${"0".repeat(63)}1`;
|
|
7
|
+
const NO = `0x${"0".repeat(64)}`;
|
|
8
|
+
const YES_ANSWERS = new Set(["accept", "pay", "adopt", "allow"]);
|
|
9
|
+
const NO_ANSWERS = new Set(["reject", "refund", "keep", "refuse"]);
|
|
10
|
+
// TypeSafe rounds probabilities and its own choice confidence to two decimals,
|
|
11
|
+
// so a hire's confidence recomputed from the probabilities can differ slightly.
|
|
12
|
+
const ROUNDING = 0.02;
|
|
13
|
+
const ADDRESS = /^0x[0-9a-f]{40}$/i;
|
|
14
|
+
const short = (value) => `${value.slice(0, 6)}…${value.slice(-4)}`;
|
|
15
|
+
// Staked workers answer by address, which reads better shortened.
|
|
16
|
+
const named = (answer) => (ADDRESS.test(answer) ? short(answer) : answer);
|
|
17
|
+
const percent = (bps) => `${(bps / 100).toFixed(bps % 100 === 0 ? 0 : 2)}%`;
|
|
18
|
+
const utc = (seconds) => new Date(Number(seconds) * 1000).toISOString().replace("T", " ").slice(0, 19);
|
|
19
|
+
const same = (a, b) => a !== undefined && b !== undefined && canonicalStringify(a) === canonicalStringify(b);
|
|
20
|
+
const display = (uri) => (uri.length > 120 ? `${uri.slice(0, 117)}…` : uri);
|
|
21
|
+
export function errorMessage(error) {
|
|
22
|
+
if (error instanceof BaseError)
|
|
23
|
+
return error.details ? `${error.shortMessage} (${error.details})` : error.shortMessage;
|
|
24
|
+
return error instanceof Error ? error.message : String(error);
|
|
25
|
+
}
|
|
26
|
+
function parseJson(text) {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function decodeText(bytes) {
|
|
35
|
+
try {
|
|
36
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** TaskBoard.subjectOf(id): the task on this board on this chain. */
|
|
43
|
+
function taskSubject(chainId, board, task) {
|
|
44
|
+
return keccak256(encodeAbiParameters([{ type: "uint256" }, { type: "address" }, { type: "uint256" }], [BigInt(chainId), board, task]));
|
|
45
|
+
}
|
|
46
|
+
/** DecisionExecutor.changeSubject(target, data): this exact call, through this executor, on this chain. */
|
|
47
|
+
function changeSubject(chainId, executor, target, data) {
|
|
48
|
+
return keccak256(encodeAbiParameters([{ type: "uint256" }, { type: "address" }, { type: "address" }, { type: "bytes" }], [BigInt(chainId), executor, target, data]));
|
|
49
|
+
}
|
|
50
|
+
const plain = (value) => typeof value === "bigint"
|
|
51
|
+
? value.toString()
|
|
52
|
+
: Array.isArray(value)
|
|
53
|
+
? value.map(plain)
|
|
54
|
+
: typeof value === "object" && value !== null
|
|
55
|
+
? Object.fromEntries(Object.entries(value).map(([k, v]) => [k, plain(v)]))
|
|
56
|
+
: value;
|
|
57
|
+
// Must decode exactly as the brain did when it showed Jev the call.
|
|
58
|
+
function decodeChange(target, data) {
|
|
59
|
+
for (const abi of [taskBoardAbi, decisionExecutorAbi, modelRegistryAbi, decisionLogAbi]) {
|
|
60
|
+
try {
|
|
61
|
+
const { functionName, args } = decodeFunctionData({ abi, data });
|
|
62
|
+
return { target, function: functionName, args: plain(args ?? []) };
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Not this contract's interface; try the next one.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { target, function: "unknown", selector: data.slice(0, 10), data };
|
|
69
|
+
}
|
|
70
|
+
function isPublished(value) {
|
|
71
|
+
const r = value;
|
|
72
|
+
return (typeof r === "object" &&
|
|
73
|
+
r !== null &&
|
|
74
|
+
typeof r.kind === "string" &&
|
|
75
|
+
typeof r.subject === "string" &&
|
|
76
|
+
typeof r.answer === "string" &&
|
|
77
|
+
typeof r.confidence === "number" &&
|
|
78
|
+
typeof r.probabilities === "object" &&
|
|
79
|
+
r.probabilities !== null &&
|
|
80
|
+
typeof r.request?.state === "string" &&
|
|
81
|
+
typeof r.request.questions?.decision === "object" &&
|
|
82
|
+
r.request.questions.decision !== null);
|
|
83
|
+
}
|
|
84
|
+
function parseBrief(text) {
|
|
85
|
+
const b = (text === undefined ? undefined : parseJson(text));
|
|
86
|
+
const valid = typeof b?.title === "string" &&
|
|
87
|
+
typeof b.brief === "string" &&
|
|
88
|
+
Array.isArray(b.levels) &&
|
|
89
|
+
b.levels.every((level) => typeof level === "string") &&
|
|
90
|
+
typeof b.passIndex === "number" &&
|
|
91
|
+
Number.isInteger(b.passIndex) &&
|
|
92
|
+
b.passIndex >= 0 &&
|
|
93
|
+
b.passIndex < b.levels.length;
|
|
94
|
+
if (!valid)
|
|
95
|
+
throw new Error("the brief is not a title, a text, quality levels and a pass level");
|
|
96
|
+
return b;
|
|
97
|
+
}
|
|
98
|
+
// A records host answers 5xx while it restarts, so those get two more tries.
|
|
99
|
+
async function get(url) {
|
|
100
|
+
for (let attempt = 1;; attempt++) {
|
|
101
|
+
try {
|
|
102
|
+
// The brain reads without following redirects, so the location on chain is the one read here too.
|
|
103
|
+
const response = await fetch(url, { redirect: "error", signal: AbortSignal.timeout(15_000) });
|
|
104
|
+
const transient = response.status === 408 || response.status === 429 || response.status >= 500;
|
|
105
|
+
if (!transient || attempt === 3)
|
|
106
|
+
return response;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (attempt === 3)
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, attempt * 1_000));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function fetchBytes(uri) {
|
|
116
|
+
if (uri.startsWith("data:")) {
|
|
117
|
+
const comma = uri.indexOf(",");
|
|
118
|
+
if (comma < 0 || !uri.slice(0, comma).endsWith(";base64"))
|
|
119
|
+
throw new Error("only base64 data URIs are supported");
|
|
120
|
+
return { bytes: Uint8Array.from(Buffer.from(uri.slice(comma + 1), "base64")), withheld: false };
|
|
121
|
+
}
|
|
122
|
+
const url = new URL(uri);
|
|
123
|
+
if (url.protocol !== "https:" && url.protocol !== "http:")
|
|
124
|
+
throw new Error(`${url.protocol} URIs are not supported`);
|
|
125
|
+
const response = await get(url);
|
|
126
|
+
if (!response.ok)
|
|
127
|
+
throw new Error(`${display(uri)} answered with status ${response.status}`);
|
|
128
|
+
return {
|
|
129
|
+
bytes: new Uint8Array(await response.arrayBuffer()),
|
|
130
|
+
withheld: response.headers.get("x-brainchain-withheld") === "result",
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
class Checks {
|
|
134
|
+
list = [];
|
|
135
|
+
add(name, status, message) {
|
|
136
|
+
this.list.push({ name, status, message });
|
|
137
|
+
}
|
|
138
|
+
check(name, pass, ok, otherwise) {
|
|
139
|
+
this.add(name, pass ? "ok" : "fail", pass ? ok : otherwise);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function readDecision({ client, set }, id) {
|
|
143
|
+
return client.readContract({ address: set.log, abi: decisionLogAbi, functionName: "get", args: [id] });
|
|
144
|
+
}
|
|
145
|
+
async function briefOf({ client, set }, task) {
|
|
146
|
+
const [posted] = await client.getContractEvents({
|
|
147
|
+
address: set.board,
|
|
148
|
+
abi: taskBoardAbi,
|
|
149
|
+
eventName: "Posted",
|
|
150
|
+
args: { id: task },
|
|
151
|
+
fromBlock: BigInt(set.fromBlock),
|
|
152
|
+
strict: true,
|
|
153
|
+
});
|
|
154
|
+
if (!posted)
|
|
155
|
+
throw new Error(`task ${task} has no Posted event on board ${set.board}`);
|
|
156
|
+
const { bytes } = await fetchBytes(posted.args.briefURI);
|
|
157
|
+
if (keccak256(bytes) !== posted.args.briefHash.toLowerCase())
|
|
158
|
+
throw new Error(`task ${task}'s brief does not match its briefHash`);
|
|
159
|
+
return parseBrief(decodeText(bytes));
|
|
160
|
+
}
|
|
161
|
+
/** Decision ids about one task on the set's board, oldest first. */
|
|
162
|
+
export async function decisionsOf({ client, network, set }, task) {
|
|
163
|
+
const events = await client.getContractEvents({
|
|
164
|
+
address: set.log,
|
|
165
|
+
abi: decisionLogAbi,
|
|
166
|
+
eventName: "Recorded",
|
|
167
|
+
args: { subject: taskSubject(network.chainId, set.board, task) },
|
|
168
|
+
fromBlock: BigInt(set.fromBlock),
|
|
169
|
+
strict: true,
|
|
170
|
+
});
|
|
171
|
+
return events.map((e) => e.args.id).sort((a, b) => (a < b ? -1 : 1));
|
|
172
|
+
}
|
|
173
|
+
export async function verifyDecision(ctx, id) {
|
|
174
|
+
const { client, network, set } = ctx;
|
|
175
|
+
const c = new Checks();
|
|
176
|
+
const decision = await readDecision(ctx, id);
|
|
177
|
+
const kind = KINDS[decision.kind] ?? `kind ${decision.kind}`;
|
|
178
|
+
const report = {
|
|
179
|
+
id: Number(id),
|
|
180
|
+
kind,
|
|
181
|
+
recordedAt: `${utc(decision.recordedAt)} UTC`,
|
|
182
|
+
confidenceBps: decision.confidenceBps,
|
|
183
|
+
checks: c.list,
|
|
184
|
+
};
|
|
185
|
+
const [recorded] = await client.getContractEvents({
|
|
186
|
+
address: set.log,
|
|
187
|
+
abi: decisionLogAbi,
|
|
188
|
+
eventName: "Recorded",
|
|
189
|
+
args: { id },
|
|
190
|
+
fromBlock: BigInt(set.fromBlock),
|
|
191
|
+
strict: true,
|
|
192
|
+
});
|
|
193
|
+
if (!recorded) {
|
|
194
|
+
c.add("read", "fail", `decision ${id} has no Recorded event on DecisionLog ${set.log}`);
|
|
195
|
+
return report;
|
|
196
|
+
}
|
|
197
|
+
const e = recorded.args;
|
|
198
|
+
report.block = Number(recorded.blockNumber);
|
|
199
|
+
report.payloadURI = e.payloadURI;
|
|
200
|
+
c.check("read", e.kind === decision.kind &&
|
|
201
|
+
e.subject === decision.subject &&
|
|
202
|
+
e.inputHash === decision.inputHash &&
|
|
203
|
+
e.answer === decision.answer &&
|
|
204
|
+
e.confidenceBps === decision.confidenceBps, `read from DecisionLog ${short(set.log)}: ${kind}, recorded ${report.recordedAt} in block ${recorded.blockNumber}, confidence ${decision.confidenceBps} bps`, `the Recorded event in block ${recorded.blockNumber} differs from what DecisionLog.get(${id}) returns`);
|
|
205
|
+
let fetched;
|
|
206
|
+
try {
|
|
207
|
+
fetched = await fetchBytes(e.payloadURI);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
c.add("fetch", "error", `could not fetch the record: ${errorMessage(error)}`);
|
|
211
|
+
return report;
|
|
212
|
+
}
|
|
213
|
+
const text = decodeText(fetched.bytes);
|
|
214
|
+
const record = text === undefined ? undefined : parseJson(text);
|
|
215
|
+
if (!isPublished(record)) {
|
|
216
|
+
c.add("fetch", "fail", `${display(e.payloadURI)} is not a decision record`);
|
|
217
|
+
return report;
|
|
218
|
+
}
|
|
219
|
+
c.add("fetch", "ok", `fetched the record from ${display(e.payloadURI)}${fetched.withheld ? ", served without the work Jev did not accept" : ""}`);
|
|
220
|
+
if (fetched.withheld) {
|
|
221
|
+
c.add("record-hash", "skip", "record hash: the work was not accepted, so the record is served without it and cannot match payloadHash");
|
|
222
|
+
c.add("input-hash", "skip", "request hash: the request quotes the withheld work, so it cannot match inputHash");
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
const hash = keccak256(fetched.bytes);
|
|
226
|
+
c.check("record-hash", hash === e.payloadHash, `record hash matches payloadHash ${short(hash)}`, `record hash ${short(hash)} differs from payloadHash ${short(e.payloadHash)}`);
|
|
227
|
+
const input = keccak256(toBytes(canonicalStringify(record.request)));
|
|
228
|
+
c.check("input-hash", input === decision.inputHash, `request hash matches inputHash ${short(input)}`, `request hash ${short(input)} differs from inputHash ${short(decision.inputHash)}`);
|
|
229
|
+
}
|
|
230
|
+
c.check("kind", record.kind === kind, `kind: ${record.kind}`, `the record is a ${record.kind}; the chain recorded a ${kind}`);
|
|
231
|
+
const task = decision.kind !== Kind.Change && /^\d+$/.test(record.subject) ? BigInt(record.subject) : undefined;
|
|
232
|
+
if (task !== undefined)
|
|
233
|
+
report.task = Number(task);
|
|
234
|
+
if (decision.kind === Kind.Change) {
|
|
235
|
+
c.check("subject", record.subject.toLowerCase() === decision.subject, `subject: ${short(decision.subject)}, the exact call this change names`, "the record's subject differs from the one on chain");
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
c.check("subject", task !== undefined && taskSubject(network.chainId, set.board, task) === decision.subject, `subject: task ${record.subject} on board ${short(set.board)}, chain ${network.chainId}`, `the record names task ${record.subject}, which is not the subject recorded on chain`);
|
|
239
|
+
}
|
|
240
|
+
const question = record.request.questions.decision;
|
|
241
|
+
const options = Array.isArray(question.criteria) ? question.criteria.map((_, i) => String(i)) : Object.keys(question.criteria ?? {});
|
|
242
|
+
c.check("options", same(Object.keys(record.probabilities).sort(), [...options].sort()), "probabilities cover exactly the options Jev was asked", "the probabilities do not match the options in the question");
|
|
243
|
+
let expectedAnswer;
|
|
244
|
+
if (decision.kind === Kind.Hire) {
|
|
245
|
+
// A staked worker's name is its address, and its id the address left-padded to 32 bytes.
|
|
246
|
+
expectedAnswer = ADDRESS.test(record.answer) ? pad(record.answer) : keccak256(toBytes(record.answer));
|
|
247
|
+
}
|
|
248
|
+
else if (YES_ANSWERS.has(record.answer)) {
|
|
249
|
+
expectedAnswer = YES;
|
|
250
|
+
}
|
|
251
|
+
else if (NO_ANSWERS.has(record.answer)) {
|
|
252
|
+
expectedAnswer = NO;
|
|
253
|
+
}
|
|
254
|
+
c.check("answer", expectedAnswer?.toLowerCase() === decision.answer, `answer: ${named(record.answer)}, recorded as ${short(decision.answer)}`, `the record answers "${record.answer}"; the chain recorded ${decision.answer}`);
|
|
255
|
+
c.check("confidence", Math.round(record.confidence * 10_000) === decision.confidenceBps, `confidence: ${record.confidence} is ${decision.confidenceBps} bps, as recorded`, `the record's confidence ${record.confidence} is ${Math.round(record.confidence * 10_000)} bps; the chain recorded ${decision.confidenceBps}`);
|
|
256
|
+
const chosen = record.probabilities[record.answer];
|
|
257
|
+
if (decision.kind !== Kind.Grade) {
|
|
258
|
+
const top = Math.max(...Object.values(record.probabilities));
|
|
259
|
+
c.check("top-option", chosen !== undefined && chosen >= top - 1e-9, `${named(record.answer)} is Jev's most likely option, at ${chosen}`, `${named(record.answer)} is not Jev's most likely option`);
|
|
260
|
+
}
|
|
261
|
+
if (decision.kind === Kind.Hire) {
|
|
262
|
+
const n = options.length;
|
|
263
|
+
const p = chosen ?? 0;
|
|
264
|
+
const recomputed = (p - 1 / n) / (1 - 1 / n);
|
|
265
|
+
c.check("confidence-recomputed", Math.abs(recomputed - record.confidence) <= ROUNDING, `confidence recomputed: (${p} - 1/${n}) / (1 - 1/${n}) = ${recomputed.toFixed(4)}, within rounding of ${record.confidence}`, `confidence recomputed from the probabilities is ${recomputed.toFixed(4)}, not ${record.confidence}`);
|
|
266
|
+
}
|
|
267
|
+
if (decision.kind === Kind.Ruling || decision.kind === Kind.Change) {
|
|
268
|
+
c.check("confidence-recomputed", record.confidence === chosen, `confidence recomputed: the probability of ${record.answer}, ${chosen}`, `confidence ${record.confidence} is not the probability of ${record.answer}`);
|
|
269
|
+
}
|
|
270
|
+
const state = (parseJson(record.request.state) ?? {});
|
|
271
|
+
if (task !== undefined) {
|
|
272
|
+
await checkBrief(ctx, c, task, decision, record, state);
|
|
273
|
+
await checkTaskThreshold(ctx, c, id, task, decision, record);
|
|
274
|
+
}
|
|
275
|
+
if (decision.kind === Kind.Change)
|
|
276
|
+
await checkChange(ctx, c, id, decision, record, state);
|
|
277
|
+
return report;
|
|
278
|
+
}
|
|
279
|
+
async function checkBrief(ctx, c, task, decision, record, state) {
|
|
280
|
+
let pending = ctx.briefs.get(task);
|
|
281
|
+
if (!pending) {
|
|
282
|
+
pending = briefOf(ctx, task);
|
|
283
|
+
ctx.briefs.set(task, pending);
|
|
284
|
+
}
|
|
285
|
+
let brief;
|
|
286
|
+
try {
|
|
287
|
+
brief = await pending;
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
c.add("brief", "skip", `brief: ${errorMessage(error)}`);
|
|
291
|
+
if (decision.kind === Kind.Grade)
|
|
292
|
+
c.add("confidence-recomputed", "skip", "grade confidence: needs the brief's pass level");
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
c.check("brief", state.task?.brief === `${brief.title}\n\n${brief.brief}`, `the question quotes task ${task}'s brief, as committed on chain`, `the brief in the question is not task ${task}'s brief`);
|
|
296
|
+
const question = record.request.questions.decision;
|
|
297
|
+
if (decision.kind === Kind.Ruling) {
|
|
298
|
+
c.check("levels", same(state.task?.levels, brief.levels) && same(state.task?.paidLevels, brief.levels.slice(0, brief.passIndex + 1)), "the levels and paid levels Jev was shown are the brief's", "the levels in the question are not the brief's");
|
|
299
|
+
}
|
|
300
|
+
if (decision.kind !== Kind.Grade)
|
|
301
|
+
return;
|
|
302
|
+
const ascending = [...brief.levels].reverse();
|
|
303
|
+
c.check("levels", same(question.criteria, ascending), "the levels Jev scored against are the brief's, lowest first", "the levels in the question are not the brief's");
|
|
304
|
+
if (record.scoreConfidence === undefined) {
|
|
305
|
+
c.add("confidence-recomputed", "skip", "grade confidence: this grade predates the rule that gates on the paid levels, so its confidence is Jev's score confidence, which the probabilities alone do not give");
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const paidKeys = ascending.map((_, i) => String(i)).slice(ascending.length - 1 - brief.passIndex);
|
|
309
|
+
const parts = paidKeys.map((key) => record.probabilities[key] ?? 0);
|
|
310
|
+
const paid = Math.min(1, parts.reduce((sum, p) => sum + p, 0));
|
|
311
|
+
const accept = paid >= 0.5;
|
|
312
|
+
const confidence = accept ? paid : 1 - paid;
|
|
313
|
+
c.check("confidence-recomputed", Math.abs(confidence - record.confidence) < 1e-9 && record.answer === (accept ? "accept" : "reject"), `confidence recomputed: paid levels ${paidKeys.join(", ")} hold ${parts.join(" + ")} = ${paid.toFixed(4)}, so ${accept ? "accept" : "reject"} at ${confidence.toFixed(4)}`, `recomputed from the probabilities, the grade is ${accept ? "accept" : "reject"} at ${confidence.toFixed(4)}, not ${record.answer} at ${record.confidence}`);
|
|
314
|
+
}
|
|
315
|
+
// Applies each threshold the way TaskBoard.hire, grade and rule do, to the
|
|
316
|
+
// terms fixed when the task was posted, then compares with what the board did.
|
|
317
|
+
async function checkTaskThreshold(ctx, c, id, task, decision, record) {
|
|
318
|
+
const { client, set } = ctx;
|
|
319
|
+
const terms = await client.readContract({ address: set.board, abi: taskBoardAbi, functionName: "get", args: [task] });
|
|
320
|
+
const filter = { address: set.board, abi: taskBoardAbi, args: { id: task, decisionId: id }, fromBlock: BigInt(set.fromBlock), strict: true };
|
|
321
|
+
const bps = decision.confidenceBps;
|
|
322
|
+
const rule = (threshold, name) => `threshold: ${named(record.answer)} at ${percent(bps)} is ${bps >= threshold ? "at or above" : "below"} the ${percent(threshold)} ${name} threshold fixed when task ${task} was posted`;
|
|
323
|
+
if (decision.kind === Kind.Hire) {
|
|
324
|
+
const hires = bps >= terms.hireThresholdBps && decision.answer !== NO;
|
|
325
|
+
const expected = `${rule(terms.hireThresholdBps, "hire")}, so the board ${hires ? "hires it" : "holds the hire"}`;
|
|
326
|
+
const [hired] = await client.getContractEvents({ ...filter, eventName: "WorkerHired" });
|
|
327
|
+
const [held] = await client.getContractEvents({ ...filter, eventName: "HireHeld" });
|
|
328
|
+
if (!hired && !held) {
|
|
329
|
+
c.add("threshold", "skip", `${expected}; this decision was not applied, and a later decision or the task's deadline settled the task`);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const pass = hired ? hires && hired.args.model === decision.answer : !hires;
|
|
333
|
+
c.check("threshold", pass, `${expected}, and it did`, `${expected}, but it ${hired ? `hired ${short(hired.args.model)}` : "held the hire"}`);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (decision.kind === Kind.Grade) {
|
|
337
|
+
const accepts = decision.answer === YES && bps >= terms.gradeThresholdBps;
|
|
338
|
+
const expected = decision.answer === YES
|
|
339
|
+
? `${rule(terms.gradeThresholdBps, "grade")}, so the work is ${accepts ? "accepted" : "not accepted"}`
|
|
340
|
+
: `threshold: Jev answered ${record.answer}, so the work is not accepted at any confidence`;
|
|
341
|
+
const [graded] = await client.getContractEvents({ ...filter, eventName: "WorkGraded" });
|
|
342
|
+
if (!graded) {
|
|
343
|
+
c.add("threshold", "skip", `${expected}; this decision was not applied, and a later decision or the grade window settled the task`);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
c.check("threshold", graded.args.accepted === accepts, `${expected}, as the board recorded`, `${expected}, but the board recorded it as ${graded.args.accepted ? "accepted" : "not accepted"}`);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const confident = bps >= terms.ruleThresholdBps;
|
|
350
|
+
const expected = `${rule(terms.ruleThresholdBps, "ruling")}, so ${confident ? `the ruling decides: ${decision.answer === YES ? "pay the model" : "refund the poster"}` : "the grade stands"}`;
|
|
351
|
+
const [ruled] = await client.getContractEvents({ ...filter, eventName: "Ruled" });
|
|
352
|
+
if (!ruled) {
|
|
353
|
+
c.add("threshold", "skip", `${expected}; this decision was not applied, and the ruling window closed with the grade standing`);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const outcome = ruled.args.paid ? "the model was paid" : "the poster was refunded";
|
|
357
|
+
c.check("threshold", ruled.args.confident === confident && (!confident || ruled.args.paid === (decision.answer === YES)), `${expected}; ${outcome}`, `${expected}, but the board ${ruled.args.confident ? "applied the ruling" : "kept the grade"} and ${outcome}`);
|
|
358
|
+
}
|
|
359
|
+
async function checkChange(ctx, c, id, decision, record, state) {
|
|
360
|
+
const { client, network, set } = ctx;
|
|
361
|
+
const [queued] = await client.getContractEvents({
|
|
362
|
+
address: set.executor,
|
|
363
|
+
abi: decisionExecutorAbi,
|
|
364
|
+
eventName: "Queued",
|
|
365
|
+
args: { decisionId: id },
|
|
366
|
+
fromBlock: BigInt(set.fromBlock),
|
|
367
|
+
strict: true,
|
|
368
|
+
});
|
|
369
|
+
const at = `${record.answer} at ${percent(decision.confidenceBps)}`;
|
|
370
|
+
if (queued) {
|
|
371
|
+
const { target, data, changeId } = queued.args;
|
|
372
|
+
c.check("change-subject", changeSubject(network.chainId, set.executor, target, data) === decision.subject, "the queued call is the call this decision names", "the queued call does not match the decision's subject");
|
|
373
|
+
c.check("change-call", same(decodeChange(target, data), state.call), "the queued call is the call Jev was shown", "the call Jev was shown differs from the queued call");
|
|
374
|
+
const change = await client.readContract({ address: set.executor, abi: decisionExecutorAbi, functionName: "getChange", args: [changeId] });
|
|
375
|
+
const status = change.vetoed ? "vetoed by the guardian" : change.executed ? "executed" : `executable from ${utc(change.eta).slice(0, 16)} UTC`;
|
|
376
|
+
c.check("threshold", decision.answer === YES, `threshold: ${at} cleared the executor's change threshold, so it was queued as change ${changeId} behind the timelock, ${status}`, `the executor queued change ${changeId} on an answer other than yes`);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const threshold = await client.readContract({ address: set.executor, abi: decisionExecutorAbi, functionName: "changeThresholdBps" });
|
|
380
|
+
if (decision.answer !== YES) {
|
|
381
|
+
c.add("threshold", "ok", `threshold: Jev answered ${record.answer}, so nothing is queued and the setting stays as it is`);
|
|
382
|
+
}
|
|
383
|
+
else if (decision.confidenceBps < threshold) {
|
|
384
|
+
c.add("threshold", "ok", `threshold: ${at} is below today's ${percent(threshold)} change threshold, so nothing is queued`);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
c.add("threshold", "skip", `threshold: ${at} clears today's ${percent(threshold)} change threshold; not queued yet, or a newer decision about the same call replaced it`);
|
|
388
|
+
}
|
|
389
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brainchain-verify",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Check a BrainChain decision against the chain: the published record, its hashes, its confidence and the thresholds the contracts applied.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "BrainChain",
|
|
7
|
+
"homepage": "https://docs.usebrainchain.com/verifying-decisions",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/usebrainchain/brainchain.git",
|
|
11
|
+
"directory": "tools/verify-decision"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/usebrainchain/brainchain/issues",
|
|
14
|
+
"keywords": ["brainchain", "jev", "verify", "decision", "onchain"],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"bin": {
|
|
17
|
+
"brainchain-verify": "dist/cli.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "node scripts/deployments.mjs && tsc -p tsconfig.json",
|
|
28
|
+
"abi": "node scripts/abi.mjs",
|
|
29
|
+
"prepublishOnly": "npm run build"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"viem": "2.57.13"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "22.20.5",
|
|
36
|
+
"typescript": "5.9.3"
|
|
37
|
+
}
|
|
38
|
+
}
|