Files
zeekay f7dd334ece An AMM and an order book, run on all three C-chains and compared
One market — a constant-product pool and a limit order book over the same
ledger — deployed to the Go, C++ and Rust implementations, driven through
one script by the same three keys, with the resulting book compared.

The comparison is of contract storage, not of the block's state root. Three
chains with three genesis allocations cannot share a state root however
perfectly they agree, so comparing roots would report a disagreement on every
run and mean nothing by it. Ten fixed slots are folded instead, two of which
are themselves folds: every fill in match order, and every resting order.

The fills fold is the point. Matching order is consensus: two nodes that
produce the same fills in a different sequence hold different books and will
match the next order differently, and a comparison of totals would call them
equal. So the book is built with two asks at one price, separated only by
arrival, and a third resting better — a correct sweep must take price first
and arrival second.

An agreement is worthless if disagreement was unreachable, so the harness runs
the script a second time with those two asks arriving in the other order and
nothing else changed. Every count and amount must come out identical and only
the sequence differs. If the fold does not move under that, the harness says
the comparison is not evidence rather than claiming a pass.

Reads go through eth_getStorageAt alone because the three do not serve the
same RPC: the C++ chain answers no eth_call, no receipts and no logs, and the
Rust chain no logs. Inclusion is judged by the sender's accepted nonce — not a
receipt, which two of three could not supply, and never the pending nonce,
which counts transactions a bootstrapping node has admitted but not accepted.

The three traders are pinned to the same addresses in the same roles on every
chain. Both folds take msg.sender, so letting each genesis choose its own
maker folds different addresses and reports a false disagreement about
matching order; which account pays is answered per chain.
2026-09-05 15:03:03 -07:00

57 lines
1.7 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause-Eco
package main
// Calldata, by hand. The encoding is four bytes of selector and a run of
// 32-byte words, and writing it out is shorter than the reflection needed to
// generate it — and leaves nothing between the harness and the bytes the three
// implementations disagree or agree about.
import (
"math/big"
"github.com/luxfi/crypto"
"github.com/luxfi/geth/common"
)
// selector is the first four bytes of the keccak of the canonical signature.
func selector(sig string) []byte { return crypto.Keccak256([]byte(sig))[:4] }
// word left-pads a value into one 32-byte slot, which is how every static type
// smaller than a word is passed.
func word(v *big.Int) []byte { return common.LeftPadBytes(v.Bytes(), 32) }
func num(v uint64) []byte { return word(new(big.Int).SetUint64(v)) }
func boolWord(b bool) []byte {
if b {
return num(1)
}
return num(0)
}
func addrWord(a common.Address) []byte { return common.LeftPadBytes(a.Bytes(), 32) }
func call(sig string, args ...[]byte) []byte {
out := selector(sig)
for _, a := range args {
out = append(out, a...)
}
return out
}
// deployData is the creation code: the contract's own bytecode followed by the
// constructor's arguments. The one dynamic argument is an address array, so the
// head carries its offset and the tail carries its length and elements.
func deployData(code []byte, traders []common.Address, base, quote uint64) []byte {
head := append([]byte{}, code...)
head = append(head, num(3*32)...) // offset of the array, past three head words
head = append(head, num(base)...)
head = append(head, num(quote)...)
head = append(head, num(uint64(len(traders)))...)
for _, t := range traders {
head = append(head, addrWord(t)...)
}
return head
}