mirror of
https://github.com/luxfi/precompile.git
synced 2026-09-24 00:26:21 +00:00
slhdsa 97.9 · mlkem 97.4 · mldsa 96.8 · xwing 96.2 · bls12381 97.2 · kzg4844 97.8 · v3 98.7. All build, vet and gofmt clean, all green under -count=1, and preserved here rather than lost to a session limit. v3's lane did report before it died: 78.1% to 98.7%, 70 of 70 mutations caught, four of which survived a first pass and needed tests written. The two that matter are rounding: burn rounding principal UP, and fee growth rounding UP, neither of which any behaviour test could see -- a Q128.128 off-by-one is 2^-128 of a token and floors to zero through every payout path, so it took an exact-value assertion. It also found that a full drain of a pool with overlapping positions REVERTS rather than paying out, because deposits round down once per position while payouts round down once per step over combined liquidity, and floor(a+b) >= floor(a)+floor(b). Custody holds; not one unbacked wei leaves. Liveness, not theft. The other six carry no report -- their lanes were killed mid-run. Build, vet, gofmt and the suite are all I can vouch for.
81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
// Copyright (C) 2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package xwing
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/luxfi/geth/common"
|
|
"github.com/luxfi/precompile/contract"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Every reachable branch of RequiredGas charges something. A branch that
|
|
// charges nothing is an unmetered path: the caller pays only for the CALL
|
|
// itself while the node still does the work of refusing.
|
|
func TestGas_EveryBranchCharges(t *testing.T) {
|
|
inputs := map[string][]byte{"nil": nil, "empty": {}, "encapsulate": {OpEncapsulate}}
|
|
for op := range 256 {
|
|
if byte(op) == OpEncapsulate {
|
|
continue
|
|
}
|
|
inputs[string(rune('a'+op%26))+"-op"] = []byte{byte(op)}
|
|
}
|
|
for name, in := range inputs {
|
|
require.NotZerof(t, XWingPrecompile.RequiredGas(in), "%s: unmetered branch", name)
|
|
}
|
|
}
|
|
|
|
// Doing the work costs more than declining to.
|
|
func TestGas_WorkCostsMoreThanRefusal(t *testing.T) {
|
|
work := XWingPrecompile.RequiredGas([]byte{OpEncapsulate})
|
|
refuse := XWingPrecompile.RequiredGas([]byte{0xFF})
|
|
require.Greater(t, work, refuse)
|
|
require.Equal(t, uint64(GasEncapsulate), work)
|
|
require.Equal(t, uint64(GasRefuse), refuse)
|
|
}
|
|
|
|
// The fee does not move with calldata length: X-Wing encapsulation is a
|
|
// fixed-size operation, so a longer frame is refused rather than charged for.
|
|
func TestGas_IndependentOfLength(t *testing.T) {
|
|
base := XWingPrecompile.RequiredGas([]byte{OpEncapsulate})
|
|
for _, n := range []int{1, 33, 1249, 100_000} {
|
|
in := make([]byte, n)
|
|
in[0] = OpEncapsulate
|
|
require.Equal(t, base, XWingPrecompile.RequiredGas(in), "length %d", n)
|
|
}
|
|
}
|
|
|
|
// One wei short of the fee and the call never reaches the crypto.
|
|
func TestGas_ExactFeeIsTheThreshold(t *testing.T) {
|
|
in, _ := frame(t, 0x01)
|
|
gas := XWingPrecompile.RequiredGas(in)
|
|
|
|
_, remaining, err := XWingPrecompile.Run(nil, common.Address{}, ContractAddress, in, gas-1, true)
|
|
require.ErrorIs(t, err, contract.ErrOutOfGas)
|
|
require.Zero(t, remaining)
|
|
|
|
_, remaining, err = XWingPrecompile.Run(nil, common.Address{}, ContractAddress, in, gas, true)
|
|
require.NoError(t, err)
|
|
require.Zero(t, remaining)
|
|
|
|
_, remaining, err = XWingPrecompile.Run(nil, common.Address{}, ContractAddress, in, gas+7, true)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(7), remaining, "only the fee is consumed")
|
|
}
|
|
|
|
// Zero supplied gas is refused for every shape of input.
|
|
func TestGas_ZeroSuppliedIsRefused(t *testing.T) {
|
|
in, _ := frame(t, 0x01)
|
|
for name, calldata := range map[string][]byte{
|
|
"valid frame": in,
|
|
"unknown op": {0xFF},
|
|
"empty": {},
|
|
} {
|
|
_, remaining, err := XWingPrecompile.Run(nil, common.Address{}, ContractAddress, calldata, 0, true)
|
|
require.ErrorIsf(t, err, contract.ErrOutOfGas, "%s", name)
|
|
require.Zerof(t, remaining, "%s", name)
|
|
}
|
|
}
|