Skip to content

Commit 0723934

Browse files
committed
Balance to return big.Int
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent d370298 commit 0723934

35 files changed

Lines changed: 307 additions & 1904 deletions

File tree

docs/imgs/storage_db.png

-134 KB
Loading

docs/imgs/storage_db.puml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package "Transaction & Audit Store (TTXDB / AuditDB)" {
2424
sender_eid : TEXT
2525
recipient_eid : TEXT
2626
token_type : TEXT <<NOT NULL>>
27-
amount : BIGINT <<NOT NULL>>
27+
amount : NUMERIC(78,0) <<NOT NULL>>
2828
stored_at : TIMESTAMP <<NOT NULL>>
2929
}
3030

@@ -34,7 +34,7 @@ package "Transaction & Audit Store (TTXDB / AuditDB)" {
3434
tx_id : TEXT <<FK, NOT NULL>>
3535
enrollment_id : TEXT
3636
token_type : TEXT <<NOT NULL>>
37-
amount : BIGINT <<NOT NULL>>
37+
amount : NUMERIC(78,0) <<NOT NULL>>
3838
stored_at : TIMESTAMP <<NOT NULL>>
3939
}
4040

@@ -65,7 +65,7 @@ package "Token Store (TokenDB)" {
6565
* tx_id : TEXT <<PK, NOT NULL>>
6666
* idx : INT <<PK, NOT NULL>>
6767
--
68-
amount : BIGINT <<NOT NULL>>
68+
amount : NUMERIC(78,0) <<NOT NULL>>
6969
token_type : TEXT <<NOT NULL>>
7070
quantity : TEXT <<NOT NULL>>
7171
issuer_raw : BYTEA

docs/services/storage.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,29 @@ The storage layer is built on a provider-based architecture that supports multip
1212

1313
The Fabric Token SDK storage is organized into logical databases, each serving a specific role in the transaction lifecycle and identity management.
1414

15+
### Amount Storage Strategy
16+
17+
**All token amounts** across the storage layer are stored as `NUMERIC(78, 0)` in the database, supporting arbitrary precision integers up to 78 digits. This design choice enables:
18+
- Representation of token amounts exceeding the uint64 maximum value (2^64-1 ≈ 1.8×10^19)
19+
- Support for tokens with high precision or extremely large supply
20+
- Consistent handling of amounts across all tables (Tokens, Transactions, Movements)
21+
22+
The SDK uses Go's `*big.Int` type throughout the codebase to handle these large values, with automatic conversion between database NUMERIC values and in-memory big.Int representations via the `BigInt` scanner type.
23+
1524
### Transaction & Audit Store (TTXDB / AuditDB)
1625
These tables track the lifecycle of token requests from assembly to finality. `AuditDB` uses the same schema but is isolated for compliance reporting.
1726

1827
* **Requests**: Tracks high-level token request state. Contains marshaled requests, current status (Pending, Confirmed, Deleted), and application/public metadata.
19-
* **Transactions**: Records individual actions (Issue, Transfer, Redeem) within a request, including sender/recipient IDs and amounts.
20-
* **Movements**: Aggregates net value changes per enrollment ID. Used to efficiently calculate balances and history.
28+
* **Transactions**: Records individual actions (Issue, Transfer, Redeem) within a request, including sender/recipient IDs and amounts (stored as `NUMERIC(78, 0)`).
29+
* **Movements**: Aggregates net value changes per enrollment ID (amounts stored as `NUMERIC(78, 0)`). Used to efficiently calculate balances and history.
2130
* **Validations**: Stores cryptographic validation metadata produced during the request verification phase.
2231
* **Endorsements**: Collects digital signatures from participants and auditors required for transaction finality.
2332

2433
### Token Store (TokenDB)
2534
This store serves as the authoritative registry for all tokens (UTXOs) known to the node.
2635

2736
* **Tokens**: The core UTXO registry. Stores token identifiers, amounts, types, ownership info, and ledger-specific state.
37+
* **Amount Storage**: Token amounts are stored as `NUMERIC(78, 0)` in the database, supporting arbitrary precision integers up to 78 digits. This enables representation of token amounts that exceed the uint64 maximum value (2^64-1 ≈ 1.8×10^19). The SDK uses Go's `*big.Int` type throughout the codebase to handle these large values, with automatic conversion between database NUMERIC values and in-memory big.Int representations.
2838
* **TokenOwners**: A mapping table linking specific tokens to wallet identifiers for fast lookups.
2939
* **PublicParameters**: A cache for the network's cryptographic public parameters and their hashes.
3040
* **TokenCertifications**: Stores third-party certifications for tokens, often required by privacy-preserving drivers.

integration/token/dvp/views/balance.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package views
99
import (
1010
"encoding/json"
1111
"fmt"
12-
"strconv"
1312

1413
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
1514
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
@@ -45,7 +44,7 @@ func (b *BalanceView) Call(context view.Context) (interface{}, error) {
4544
return nil, err
4645
}
4746

48-
return Balance{Quantity: strconv.FormatUint(balance, 10), Type: b.Type}, nil
47+
return Balance{Quantity: balance.String(), Type: b.Type}, nil
4948
}
5049

5150
type BalanceViewFactory struct{}

integration/token/fungible/views/accept.go

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

99
import (
10+
"math/big"
11+
1012
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
1113
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
1214
"github.com/hyperledger-labs/fabric-token-sdk/token"
@@ -44,7 +46,7 @@ func (a *AcceptCashView) Call(context view.Context) (interface{}, error) {
4446
}
4547
balance, err := ttx.MyWallet(context, token.WithTMSID(tx.TMSID())).Balance(context.Context(), ttx.WithType(output.Type))
4648
assert.NoError(err, "failed retrieving balance for type [%s]", output.Type)
47-
assert.True(balance <= 3000, "cannot have more than 3000 unspent quantity for type [%s]", output.Type)
49+
assert.True(balance.Cmp(big.NewInt(3000)) <= 0, "cannot have more than 3000 unspent quantity for type [%s]", output.Type)
4850
}
4951

5052
// If everything is fine, the recipient accepts and sends back her signature.

integration/token/fungible/views/balance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (b *BalanceView) Call(context view.Context) (interface{}, error) {
6767
if err != nil {
6868
return nil, err
6969
}
70-
if sum.ToBigInt().Uint64() != balance {
71-
return nil, errors.Errorf("balance doesn't match [%d]!=[%d]", balance, sum.ToBigInt().Uint64())
70+
if sum.ToBigInt().Cmp(balance) != 0 {
71+
return nil, errors.Errorf("balance doesn't match [%s]!=[%s]", balance.String(), sum.Decimal())
7272
}
7373
}
7474

integration/token/interop/views/balance.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package views
88

99
import (
1010
"encoding/json"
11-
"strconv"
1211

1312
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
1413
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections/iterators"
@@ -67,7 +66,7 @@ func (b *BalanceView) Call(context view.Context) (interface{}, error) {
6766
assert.NoError(err, "failed to compute the sum of the htlc expired tokens")
6867

6968
return BalanceResult{
70-
Quantity: strconv.FormatUint(balance, 10),
69+
Quantity: balance.String(),
7170
Locked: lockedSum.Decimal(),
7271
Expired: expiredSum.Decimal(),
7372
Type: b.Type,

token/core/common/driver/mock/keystore.go

Lines changed: 61 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token/core/zkatdlog/nogh/v1/audit/mock/signing_identity.go

Lines changed: 0 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token/core/zkatdlog/nogh/v1/issue/mock/signing_identity.go

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)