-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathstorage.go
165 lines (147 loc) · 3.33 KB
/
storage.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package storage
import (
"context"
"encoding/binary"
"errors"
"fmt"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/metadata"
smath "github.com/ava-labs/avalanchego/utils/math"
)
type ReadState func(context.Context, [][]byte) ([][]byte, []error)
// State
// 0x0/ (hypersdk-height)
// 0x1/ (hypersdk-timestamp)
// 0x2/ (hypersdk-fee)
//
// 0x3/ (balance)
// -> [owner] => balance
const balancePrefix byte = metadata.DefaultMinimumPrefix
const BalanceChunks uint16 = 1
// [balancePrefix] + [address]
func BalanceKey(addr codec.Address) (k []byte) {
k = make([]byte, 1+codec.AddressLen+consts.Uint16Len)
k[0] = balancePrefix
copy(k[1:], addr[:])
binary.BigEndian.PutUint16(k[1+codec.AddressLen:], BalanceChunks)
return
}
// If locked is 0, then account does not exist
func GetBalance(
ctx context.Context,
im state.Immutable,
addr codec.Address,
) (uint64, error) {
_, bal, _, err := getBalance(ctx, im, addr)
return bal, err
}
func getBalance(
ctx context.Context,
im state.Immutable,
addr codec.Address,
) ([]byte, uint64, bool, error) {
k := BalanceKey(addr)
bal, exists, err := innerGetBalance(im.GetValue(ctx, k))
return k, bal, exists, err
}
// Used to serve RPC queries
func GetBalanceFromState(
ctx context.Context,
f ReadState,
addr codec.Address,
) (uint64, error) {
k := BalanceKey(addr)
values, errs := f(ctx, [][]byte{k})
bal, _, err := innerGetBalance(values[0], errs[0])
return bal, err
}
func innerGetBalance(
v []byte,
err error,
) (uint64, bool, error) {
if errors.Is(err, database.ErrNotFound) {
return 0, false, nil
}
if err != nil {
return 0, false, err
}
val, err := database.ParseUInt64(v)
if err != nil {
return 0, false, err
}
return val, true, nil
}
func SetBalance(
ctx context.Context,
mu state.Mutable,
addr codec.Address,
balance uint64,
) error {
k := BalanceKey(addr)
return setBalance(ctx, mu, k, balance)
}
func setBalance(
ctx context.Context,
mu state.Mutable,
key []byte,
balance uint64,
) error {
return mu.Insert(ctx, key, binary.BigEndian.AppendUint64(nil, balance))
}
func AddBalance(
ctx context.Context,
mu state.Mutable,
addr codec.Address,
amount uint64,
) (uint64, error) {
key, bal, _, err := getBalance(ctx, mu, addr)
if err != nil {
return 0, err
}
nbal, err := smath.Add(bal, amount)
if err != nil {
return 0, fmt.Errorf(
"%w: could not add balance (bal=%d, addr=%v, amount=%d)",
ErrInvalidBalance,
bal,
addr,
amount,
)
}
return nbal, setBalance(ctx, mu, key, nbal)
}
func SubBalance(
ctx context.Context,
mu state.Mutable,
addr codec.Address,
amount uint64,
) (uint64, error) {
key, bal, ok, err := getBalance(ctx, mu, addr)
if !ok {
return 0, ErrInvalidBalance
}
if err != nil {
return 0, err
}
nbal, err := smath.Sub(bal, amount)
if err != nil {
return 0, fmt.Errorf(
"%w: could not subtract balance (bal=%d, addr=%v, amount=%d)",
ErrInvalidBalance,
bal,
addr,
amount,
)
}
if nbal == 0 {
// If there is no balance left, we should delete the record instead of
// setting it to 0.
return 0, mu.Remove(ctx, key)
}
return nbal, setBalance(ctx, mu, key, nbal)
}