mirror of
https://github.com/luxfi/api.git
synced 2026-09-24 00:24:53 +00:00
The set was Uint32, Uint64, Float64. Two widths the chain actually uses were missing, so callers reached past this package for both. Uint16 is what platformvm's API already asks for today. Uint256 is the width the EVM carries. A wei value, a token balance or a supply does not fit in 64 bits — an 18-decimal balance passes 2^64 at about 18.4 tokens — so every such field arriving here had to become a bare string or a hand-rolled big.Int. It is uint256.Int rather than big.Int because a fixed-size value cannot be nil and does not allocate; the module was already in the graph. Every numeric here is a QUOTED decimal, which is the point of the package: a JSON number is a float64, and a uint64 above 2^53 does not survive one. The test asserts Uint64 REFUSES a value it cannot hold, which is the failure Uint256 exists to prevent. Unquoting was written out once per type and is now read in one place. Adding two more types would have made five copies of it.
76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package types
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/holiman/uint256"
|
|
)
|
|
|
|
// Every numeric at this boundary is carried as a QUOTED decimal, because
|
|
// JSON numbers are float64 and a uint64 above 2^53 does not survive one.
|
|
func TestQuotedDecimalRoundTrip(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
in any
|
|
want string
|
|
}{
|
|
{"uint16 max", Uint16(65535), `"65535"`},
|
|
{"uint32 max", Uint32(4294967295), `"4294967295"`},
|
|
{"uint64 max", Uint64(18446744073709551615), `"18446744073709551615"`},
|
|
{"uint256 max", Uint256(*uint256.MustFromDecimal("115792089237316195423570985008687907853269984665640564039457584007913129639935")),
|
|
`"115792089237316195423570985008687907853269984665640564039457584007913129639935"`},
|
|
{"uint256 one wei over uint64", Uint256(*uint256.MustFromDecimal("18446744073709551616")), `"18446744073709551616"`},
|
|
} {
|
|
got, err := json.Marshal(tc.in)
|
|
if err != nil {
|
|
t.Fatalf("%s: marshal: %v", tc.name, err)
|
|
}
|
|
if string(got) != tc.want {
|
|
t.Errorf("%s: got %s want %s", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUint256SurvivesWhatUint64Cannot(t *testing.T) {
|
|
// A token balance with 18 decimals passes 2^64 at ~18.4 tokens.
|
|
const wei = "1000000000000000000000" // 1000 tokens
|
|
var u Uint256
|
|
if err := json.Unmarshal([]byte(`"`+wei+`"`), &u); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if v := uint256.Int(u); v.Dec() != wei {
|
|
t.Errorf("got %s want %s", v.Dec(), wei)
|
|
}
|
|
var small Uint64
|
|
if err := json.Unmarshal([]byte(`"`+wei+`"`), &small); err == nil {
|
|
t.Error("Uint64 accepted a value it cannot hold; that is the bug Uint256 exists for")
|
|
}
|
|
}
|
|
|
|
func TestNullLeavesTheValueAlone(t *testing.T) {
|
|
u16, u32, u64, f := Uint16(7), Uint32(7), Uint64(7), Float64(7)
|
|
u256 := Uint256(*uint256.NewInt(7))
|
|
for _, p := range []json.Unmarshaler{&u16, &u32, &u64, &u256, &f} {
|
|
if err := p.UnmarshalJSON([]byte(Null)); err != nil {
|
|
t.Fatalf("null: %v", err)
|
|
}
|
|
}
|
|
if u16 != 7 || u32 != 7 || u64 != 7 || f != 7 {
|
|
t.Error("null overwrote a value")
|
|
}
|
|
if v := uint256.Int(u256); v.Dec() != "7" {
|
|
t.Error("null overwrote the 256-bit value")
|
|
}
|
|
}
|
|
|
|
func TestUnquotedIsAlsoAccepted(t *testing.T) {
|
|
var u Uint64
|
|
if err := json.Unmarshal([]byte(`12345`), &u); err != nil || u != 12345 {
|
|
t.Errorf("bare number: got %d err %v", u, err)
|
|
}
|
|
}
|