Skip to content

Layer 2 prototype, part 1: shared multi-currency payment state (deterministic executor + replay tests) - #28

Open
cno127 wants to merge 1 commit into
ci/revive-linux-cifrom
venus/layer2-prototype
Open

cno127 wants to merge 1 commit into
ci/revive-linux-cifrom
venus/layer2-prototype

Conversation

@cno127

@cno127 cno127 commented Nov 19, 2026

Copy link
Copy Markdown
Owner

What this is

The first part of the Layer 2 prototype: the shared multi-currency payment state as a deterministic executor. Spec base: article 19, section "Layer 2: shared multi-currency payment execution" (one account-based state, transactions ordered in batches, independent nodes replay the same bytes and calculate the same resulting state, a payment with conversion is one operation: either every leg succeeds, or none does).

Prototype, test value only. Nothing here is linked into a node binary; no consensus file and no existing source file is changed.

  • src/o_layer2/payment_state.h/.cpp (new library o_layer2, linked into test_bitcoin only)
  • src/test/o_layer2_tests.cpp (suite o_layer2_tests, 7 cases)
  • 2 CMake lists (the library + the test file)

What it does

  • Accounts hold balances in several currencies. Operations: DEPOSIT, WITHDRAW, PAY, PAY_CONVERT, SET_CONVERTER.
  • A batch has one byte form (fixed-width, little-endian; unused fields must be zero; rates in strict order; exact length). ApplyBatch(bytes) refuses a batch that is malformed, out of turn, or built on another state root, and then changes nothing.
  • Pay with conversion = one operation with four legs (payer pays currency A to a converter, the converter pays currency B to the payee). Any failed test (rate, consent, funds on either side, nonce) = no leg applied.
  • An operation that fails changes nothing and gets a status; the order of the tests is fixed, so every node gives the same status.
  • State root = SHA256 over the ordered state (accounts, nonces, balances, supply per currency, batch count, last batch hash).
  • Integers only: no floating point, no 128-bit type (MulDivFloor is a 64x64/64 long division, checked against __int128 in the test where the compiler has it).

Tested, and how

No Boost / CMake on the machine this was written on, so the committed test file was compiled together with the module and src/crypto/sha256.cpp against a small Boost.Test macro stub:

Build Result
g++ 13.3.0 (libstdc++) 7 cases, 0 failures
zig 0.17.1 c++ (clang, libc++) 7 cases, 0 failures
g++ -fsanitize=address,undefined 7 cases, 0 failures
  • Replay: one instance builds a run of 60 batches (12 operations each, many wrong on purpose); three other instances that only receive the bytes reach the same root, statuses and batch hash after every batch. A batch out of turn or twice is refused; one changed byte in batch 1 gives another root and the honest batch 2 no longer applies.
  • Fixed roots: the empty-state root and the root after that run are written in the test, and both compilers / standard libraries give them.
  • Conservation: over 150 mixed batches, after every batch the balances of each currency add up to its supply, and the supply equals deposits minus withdrawals. Payments and conversions create and destroy nothing.
  • Each rule broken once: 17 single-line faults put into a copy of payment_state.cpp (converter not debited, rate not enforced, consent not required, rounding up, nonce not tested, previous root not tested, trailing bytes accepted, a field left out of the root, ...): the test fails on all 17.

NOT tested / NOT done

  • Not built with CMake and real Boost.Test here. The CI run on this PR is the first real build of the two CMake edits and of the test under Boost. The base branch's ctest step is red for known reasons (Audit 2026-09: Revive CI and register the O unit/functional tests (CMake lists + test_runner) #24, Decision needed: mainnet genesis block is not mined (placeholder nonce) — ~30 unit-test fixtures fail #27), so the result to read is the suite o_layer2_tests itself.
  • "Independent nodes" = separate executor instances and two compilers on one machine. No network, no second machine, no sequencer.
  • No signature check: an operation in a batch is taken as authorized. No storage (state in memory), no Layer 1 link (a deposit / withdrawal is not checked against the chain, the root is not posted), no fraud or validity proof, no data-availability rule.
  • No throughput figure. Nothing was measured, and a number without signatures, storage and network would mean little. The targets on record stay targets: the wiki page Scalability-Analysis (about 93,000 / 278,000 / 694,000 TPS for 1 / 3 / 7.5 payments per person per day, "100K+ TPS with Layer 2") and article 19 ("hundreds of thousands, potentially approaching one million at peak ... an engineering headroom target, not a performance claim").
  • The merchant + wallet API is the next part, not in this PR.

Not decided here (the project owner's calls)

  • Who converts. Built as a swap with a named account that agreed to convert (SET_CONVERTER) and holds the other currency: no unit of any currency is created or destroyed. Whether the protocol itself should be that party (mint / burn at the reference rate) is an economic choice and is not built.
  • Rounding. The payee receives floor(paid x rate); the remainder (less than one smallest unit) stays with the converter. One rule had to be picked for the result to be reproducible; it is one function (ConvertFloor).
  • Where the rates come from. Each batch carries its rate table as data (in the design: the reference rates Layer 1 publishes). Rate terms are 32-bit numerator / denominator.
  • The amount unit (Audit 2026-09: Decide the canonical money unit — OAmount::O_COIN=100 vs consensus COIN=1e8 (same value renders 1,000,000x apart) #22): an amount is a count of the currency's smallest unit, whatever that is.
  • No fee (the stated direction). No reward and no spam limit: neither exists in this code; both wait for a rule and figures.
  • A failed operation does not use up its nonce (it changes nothing at all).

About the base branch

Opened against ci/revive-linux-ci (PR #26) and not main, because only a branch that carries the new ci.yml gets a CI run. Nothing is merged; the 3 new files and the 2 CMake edits apply to main unchanged if that is preferred.

— Written and opened by Venus (automated coding worker) for Christophe's review. Not reviewed by a second model yet.

… part)

A deterministic executor for the Layer 2 payment state described in the article "Why Proof of Work Was Built for
Security, Not Speed": accounts holding several currencies, operations ordered in batches, one byte form per batch,
and a payment with conversion applied as one operation (every leg or none).

New library o_layer2 (src/o_layer2/payment_state.h/.cpp), linked into test_bitcoin only: no node binary, no
consensus file and no existing source is changed. Integers only (no floating point, no 128-bit type), ordered
containers only, SHA256 from src/crypto.

Operations: DEPOSIT, WITHDRAW, PAY, PAY_CONVERT (swap with a named account that agreed to convert, at the batch's
rate table, payee receives floor(paid x rate)), SET_CONVERTER. A batch that is malformed, out of turn or built on
another state is refused whole; an operation that fails changes nothing.

Tests (src/test/o_layer2_tests.cpp, suite o_layer2_tests, 7 cases): the byte form, nonces, the all-or-nothing
conversion, conservation per currency over a mixed run, replay of the same bytes by separate instances, fixed roots.

Test value only: no signature check, no sequencer, no network, no storage, no Layer 1 link, no proofs, no fee,
no reward, no spam limit. The amount unit (#22), who converts, the source of the rates and the rounding rule are
not decided here (see the header).
@cno127

cno127 commented Nov 19, 2026

Copy link
Copy Markdown
Owner Author

CI result for this PR: run 44569229914 on 9b05f4e19 (ubuntu-24.04, gcc, CMake).

  • Build: success. o_layer2/payment_state.cpp compiled, libo_layer2.a linked, o_layer2_tests.cpp compiled into test_bitcoin. No compiler warning names the new files.
  • o_layer2_tests: Passed (ctest #133, 0.14 s) under the real Boost.Test. So the fixed roots written in the test also come out of this third build (after g++ 13.3 and clang / libc++ locally).
  • The run itself is red, as expected on this base: 49 of 148 suites failed, the same 49 as in run 34714709801 of ci/revive-linux-ci, compared name by name (none new, none fewer). The O functional tests step was skipped because it only runs after a green ctest.

Still not done, as in the description: no network or second machine, no signature check, no storage, no Layer 1 link, no throughput figure.

— Venus (automated coding worker)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant