Skip to content

Commit b63f3fa

Browse files
committed
fix(storage): keep token amounts exact instead of truncating to uint64
TokenRecord.Amount was uint64 while the backing column is amount NUMERIC(78, 0), sized for a 256-bit value. AppendToken narrowed the quantity with big.Int.Uint64(), which returns the low 64 bits with no error for anything larger, so for a token whose quantity exceeds 2^64-1 the amount column silently received a wrapped value while the authoritative hex quantity column stayed correct. Widen the field to *big.Int and insert its exact decimal representation, following the pattern already used for movement and transaction records. StoreToken now refuses a nil amount (the column is NOT NULL) and one wider than maxAmountBits, so a value the column cannot hold surfaces as an error instead of a row whose two amount columns disagree. No consumer relied on the uint64 type; the only call sites were the test fixtures updated here. Note that only Postgres can hold such a value: SQLite gives a NUMERIC column NUMERIC affinity and converts an integer literal too large for int64 to REAL. With this change that fails loudly on read rather than corrupting silently. The exact round-trip case is therefore wired into the Postgres suite, while the write-time validation case runs on every backend. Fixes #2021 Signed-off-by: AkramBitar <akram@il.ibm.com>
1 parent 62e8a9c commit b63f3fa

9 files changed

Lines changed: 151 additions & 51 deletions

File tree

token/services/selector/testutils/test_cases.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package testutils
99
import (
1010
"context"
1111
"fmt"
12+
"math/big"
1213
"sync"
1314
"sync/atomic"
1415
"testing"
@@ -173,7 +174,7 @@ func (m *enhancedManager) UpdateTokens(deleted []*token.ID, added []token.Unspen
173174
LedgerMetadata: []byte{},
174175
Quantity: t.Quantity,
175176
Type: t.Type,
176-
Amount: 0,
177+
Amount: big.NewInt(0),
177178
Owner: true,
178179
Auditor: false,
179180
Issuer: false,

token/services/storage/db/dbtest/tokenlock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package dbtest
88

99
import (
10+
"math/big"
1011
"testing"
1112
"time"
1213

@@ -79,7 +80,7 @@ func TestFully(t *testing.T, tokenDB driver3.TokenStore, tokenLockDB driver3.Tok
7980
LedgerMetadata: []byte{}, // Empty metadata
8081
Quantity: "0x64", // 100 in hex
8182
Type: "USD",
82-
Amount: 100,
83+
Amount: big.NewInt(100),
8384
Owner: true,
8485
}
8586
err = tokenTx.StoreToken(ctx, tokenRecord, []string{"owner1"})

0 commit comments

Comments
 (0)