|
| 1 | +// Petri net execution parity test — must match Go TestRuntimeSolidityParity. |
| 2 | +// Run: node --experimental-vm-modules public/parity_test.mjs |
| 3 | + |
| 4 | +import { Model } from './model.js'; |
| 5 | +import { State } from './state.js'; |
| 6 | + |
| 7 | +let failures = 0; |
| 8 | + |
| 9 | +function assert(condition, msg) { |
| 10 | + if (!condition) { |
| 11 | + console.error('FAIL:', msg); |
| 12 | + failures++; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +// ============ ERC20: mint → transfer → burn ============ |
| 17 | +function testERC20() { |
| 18 | + const m = new Model('ERC20'); |
| 19 | + m.addPlace({ id: 'BALANCES', schema: 'map[address]uint256', exported: true }); |
| 20 | + m.addPlace({ id: 'TOTAL_SUPPLY', schema: 'uint256', initial: 0 }); |
| 21 | + m.addTransition({ id: 'mint' }); |
| 22 | + m.addTransition({ id: 'transfer' }); |
| 23 | + m.addTransition({ id: 'burn' }); |
| 24 | + |
| 25 | + // mint arcs: mint -|amount|> BALANCES[to], mint -|amount|> TOTAL_SUPPLY |
| 26 | + m.addArc({ source: 'mint', target: 'BALANCES', keys: ['to'], value: 'amount' }); |
| 27 | + m.addArc({ source: 'mint', target: 'TOTAL_SUPPLY', value: 'amount' }); |
| 28 | + |
| 29 | + // transfer arcs: BALANCES[from] -|amount|> transfer, transfer -|amount|> BALANCES[to] |
| 30 | + m.addArc({ source: 'BALANCES', target: 'transfer', keys: ['from'], value: 'amount' }); |
| 31 | + m.addArc({ source: 'transfer', target: 'BALANCES', keys: ['to'], value: 'amount' }); |
| 32 | + |
| 33 | + // burn arcs: BALANCES[from] -|amount|> burn, TOTAL_SUPPLY -|amount|> burn |
| 34 | + m.addArc({ source: 'BALANCES', target: 'burn', keys: ['from'], value: 'amount' }); |
| 35 | + m.addArc({ source: 'TOTAL_SUPPLY', target: 'burn', value: 'amount' }); |
| 36 | + |
| 37 | + const s = new State(m); |
| 38 | + |
| 39 | + // Mint 1000 to Alice |
| 40 | + s.executeWithBindings('mint', { to: 'Alice', amount: 1000 }); |
| 41 | + assert(s.getDataMap('BALANCES')['Alice'] === 1000, `ERC20 mint: BALANCES[Alice]=${s.getDataMap('BALANCES')['Alice']}, want 1000`); |
| 42 | + assert(s.getTokens('TOTAL_SUPPLY') === 1000, `ERC20 mint: TOTAL_SUPPLY=${s.getTokens('TOTAL_SUPPLY')}, want 1000`); |
| 43 | + |
| 44 | + // Transfer 300 from Alice to Bob |
| 45 | + s.executeWithBindings('transfer', { from: 'Alice', to: 'Bob', amount: 300 }); |
| 46 | + assert(s.getDataMap('BALANCES')['Alice'] === 700, `ERC20 transfer: BALANCES[Alice]=${s.getDataMap('BALANCES')['Alice']}, want 700`); |
| 47 | + assert(s.getDataMap('BALANCES')['Bob'] === 300, `ERC20 transfer: BALANCES[Bob]=${s.getDataMap('BALANCES')['Bob']}, want 300`); |
| 48 | + assert(s.getTokens('TOTAL_SUPPLY') === 1000, `ERC20 transfer: TOTAL_SUPPLY=${s.getTokens('TOTAL_SUPPLY')}, want 1000`); |
| 49 | + |
| 50 | + // Burn 100 from Alice |
| 51 | + s.executeWithBindings('burn', { from: 'Alice', amount: 100 }); |
| 52 | + assert(s.getDataMap('BALANCES')['Alice'] === 600, `ERC20 burn: BALANCES[Alice]=${s.getDataMap('BALANCES')['Alice']}, want 600`); |
| 53 | + assert(s.getTokens('TOTAL_SUPPLY') === 900, `ERC20 burn: TOTAL_SUPPLY=${s.getTokens('TOTAL_SUPPLY')}, want 900`); |
| 54 | + |
| 55 | + console.log('ERC20 parity: OK'); |
| 56 | +} |
| 57 | + |
| 58 | +// ============ Counter: increment → decrement ============ |
| 59 | +function testCounter() { |
| 60 | + const m = new Model('Counter'); |
| 61 | + m.addPlace({ id: 'COUNT', schema: 'uint256', initial: 0 }); |
| 62 | + m.addTransition({ id: 'increment' }); |
| 63 | + m.addTransition({ id: 'decrement' }); |
| 64 | + |
| 65 | + m.addArc({ source: 'increment', target: 'COUNT', value: 'amount' }); |
| 66 | + m.addArc({ source: 'COUNT', target: 'decrement', value: 'amount' }); |
| 67 | + |
| 68 | + const s = new State(m); |
| 69 | + |
| 70 | + // Increment by 5 |
| 71 | + s.executeWithBindings('increment', { amount: 5 }); |
| 72 | + assert(s.getTokens('COUNT') === 5, `Counter increment: COUNT=${s.getTokens('COUNT')}, want 5`); |
| 73 | + |
| 74 | + // Decrement by 2 |
| 75 | + s.executeWithBindings('decrement', { amount: 2 }); |
| 76 | + assert(s.getTokens('COUNT') === 3, `Counter decrement: COUNT=${s.getTokens('COUNT')}, want 3`); |
| 77 | + |
| 78 | + console.log('Counter parity: OK'); |
| 79 | +} |
| 80 | + |
| 81 | +// ============ Nested maps: approve + transferFrom ============ |
| 82 | +function testAllowances() { |
| 83 | + const m = new Model('ERC20Approve'); |
| 84 | + m.addPlace({ id: 'BALANCES', schema: 'map[address]uint256' }); |
| 85 | + m.addPlace({ id: 'ALLOWANCES', schema: 'map[address]map[address]uint256' }); |
| 86 | + m.addTransition({ id: 'mint' }); |
| 87 | + m.addTransition({ id: 'approve' }); |
| 88 | + m.addTransition({ id: 'transferFrom' }); |
| 89 | + |
| 90 | + m.addArc({ source: 'mint', target: 'BALANCES', keys: ['to'], value: 'amount' }); |
| 91 | + m.addArc({ source: 'approve', target: 'ALLOWANCES', keys: ['owner', 'spender'], value: 'amount' }); |
| 92 | + m.addArc({ source: 'BALANCES', target: 'transferFrom', keys: ['from'], value: 'amount' }); |
| 93 | + m.addArc({ source: 'ALLOWANCES', target: 'transferFrom', keys: ['from', 'spender'], value: 'amount' }); |
| 94 | + m.addArc({ source: 'transferFrom', target: 'BALANCES', keys: ['to'], value: 'amount' }); |
| 95 | + |
| 96 | + const s = new State(m); |
| 97 | + |
| 98 | + // Mint 1000 to Alice |
| 99 | + s.executeWithBindings('mint', { to: 'Alice', amount: 1000 }); |
| 100 | + assert(s.getDataMap('BALANCES')['Alice'] === 1000, 'mint balance'); |
| 101 | + |
| 102 | + // Alice approves Bob for 500 |
| 103 | + s.executeWithBindings('approve', { owner: 'Alice', spender: 'Bob', amount: 500 }); |
| 104 | + const allowances = s.getDataMap('ALLOWANCES'); |
| 105 | + assert(allowances['Alice'] && allowances['Alice']['Bob'] === 500, `approve: ALLOWANCES[Alice][Bob]=${allowances['Alice']?.['Bob']}, want 500`); |
| 106 | + |
| 107 | + // Bob transfers 200 from Alice to Charlie |
| 108 | + s.executeWithBindings('transferFrom', { from: 'Alice', spender: 'Bob', to: 'Charlie', amount: 200 }); |
| 109 | + assert(s.getDataMap('BALANCES')['Alice'] === 800, `transferFrom: BALANCES[Alice]=${s.getDataMap('BALANCES')['Alice']}, want 800`); |
| 110 | + assert(s.getDataMap('BALANCES')['Charlie'] === 200, `transferFrom: BALANCES[Charlie]=${s.getDataMap('BALANCES')['Charlie']}, want 200`); |
| 111 | + assert(s.getDataMap('ALLOWANCES')['Alice']['Bob'] === 300, `transferFrom: ALLOWANCES[Alice][Bob]=${s.getDataMap('ALLOWANCES')['Alice']['Bob']}, want 300`); |
| 112 | + |
| 113 | + console.log('Allowances parity: OK'); |
| 114 | +} |
| 115 | + |
| 116 | +testERC20(); |
| 117 | +testCounter(); |
| 118 | +testAllowances(); |
| 119 | + |
| 120 | +if (failures > 0) { |
| 121 | + console.error(`\n${failures} parity failures`); |
| 122 | + process.exit(1); |
| 123 | +} else { |
| 124 | + console.log('\nPetri net execution parity: all tests passed'); |
| 125 | +} |
0 commit comments