forked from filecoin-project/specs-actors
/
balancetable.go
105 lines (92 loc) · 2.89 KB
/
balancetable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package adt
import (
addr "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
cid "github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
// Bitwidth of balance table HAMTs, determined empirically from mutation
// patterns and projections of mainnet data
const BalanceTableBitwidth = 6
// A specialization of a map of addresses to (positive) token amounts.
// Absent keys implicitly have a balance of zero.
type BalanceTable Map
// Interprets a store as balance table with root `r`.
func AsBalanceTable(s Store, r cid.Cid) (*BalanceTable, error) {
m, err := AsMap(s, r, BalanceTableBitwidth)
if err != nil {
return nil, err
}
return &BalanceTable{
root: m.root,
store: s,
}, nil
}
// Returns the root cid of underlying HAMT.
func (t *BalanceTable) Root() (cid.Cid, error) {
return (*Map)(t).Root()
}
// Gets the balance for a key, which is zero if they key has never been added to.
func (t *BalanceTable) Get(key addr.Address) (abi.TokenAmount, error) {
var value abi.TokenAmount
found, err := (*Map)(t).Get(abi.AddrKey(key), &value)
if !found || err != nil {
value = big.Zero()
}
return value, err
}
// Adds an amount to a balance, requiring the resulting balance to be non-negative.
func (t *BalanceTable) Add(key addr.Address, value abi.TokenAmount) error {
prev, err := t.Get(key)
if err != nil {
return err
}
sum := big.Add(prev, value)
sign := sum.Sign()
if sign < 0 {
return xerrors.Errorf("adding %v to balance %v would give negative: %v", value, prev, sum)
} else if sign == 0 && !prev.IsZero() {
return (*Map)(t).Delete(abi.AddrKey(key))
}
return (*Map)(t).Put(abi.AddrKey(key), &sum)
}
// Subtracts up to the specified amount from a balance, without reducing the balance below some minimum.
// Returns the amount subtracted.
func (t *BalanceTable) SubtractWithMinimum(key addr.Address, req abi.TokenAmount, floor abi.TokenAmount) (abi.TokenAmount, error) {
prev, err := t.Get(key)
if err != nil {
return big.Zero(), err
}
available := big.Max(big.Zero(), big.Sub(prev, floor))
sub := big.Min(available, req)
if sub.Sign() > 0 {
err = t.Add(key, sub.Neg())
if err != nil {
return big.Zero(), err
}
}
return sub, nil
}
// MustSubtract subtracts the given amount from the account's balance.
// Returns an error if the account has insufficient balance
func (t *BalanceTable) MustSubtract(key addr.Address, req abi.TokenAmount) error {
prev, err := t.Get(key)
if err != nil {
return err
}
if req.GreaterThan(prev) {
return xerrors.New("couldn't subtract the requested amount")
}
return t.Add(key, req.Neg())
}
// Returns the total balance held by this BalanceTable
func (t *BalanceTable) Total() (abi.TokenAmount, error) {
total := big.Zero()
var cur abi.TokenAmount
err := (*Map)(t).ForEach(&cur, func(key string) error {
total = big.Add(total, cur)
return nil
})
return total, err
}