forked from bosnet/sebak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_account.go
165 lines (132 loc) · 3.86 KB
/
block_account.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
package sebak
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/owlchain/sebak/lib/common"
"github.com/owlchain/sebak/lib/storage"
)
// BlockAccount is account model in block. the storage should support,
// * find by `Address`:
// - key: `Address`: value: `ID` of BlockAccount
// * get list by created order:
//
// models
// * 'address'
// - 'ba-address-<BlockAccount.Address>': `BlockAccount`
// * 'created'
// - 'ba-created-<sequential uuid1>': `BlockAccouna.Address`
const BlockAccountPrefixAddress string = "ba-address-"
const BlockAccountPrefixCreated string = "ba-created-"
type BlockAccount struct {
Address string
Balance string
Checkpoint string
}
func NewBlockAccount(address, balance, checkpoint string) *BlockAccount {
return &BlockAccount{
Address: address,
Balance: balance,
Checkpoint: checkpoint,
}
}
func NewBlockAccountFromTransaction(address, balance string, tx Transaction) *BlockAccount {
return NewBlockAccount(address, balance, tx.NextCheckpoint())
}
func (b *BlockAccount) String() string {
return string(sebakcommon.MustJSONMarshal(b))
}
func (b *BlockAccount) Save(st *sebakstorage.LevelDBBackend) (err error) {
key := GetBlockAccountKey(b.Address)
var exists bool
exists, err = st.Has(key)
if err != nil {
return
}
if exists {
err = st.Set(key, b)
} else {
// TODO consider to use, [`Transaction`](https://godoc.org/github.com/syndtr/goleveldb/leveldb#DB.OpenTransaction)
err = st.New(key, b)
createdKey := GetBlockAccountCreatedKey(sebakcommon.GetUniqueIDFromUUID())
err = st.New(createdKey, b.Address)
}
return
}
func (b *BlockAccount) Serialize() (encoded []byte, err error) {
encoded, err = sebakcommon.EncodeJSONValue(b)
return
}
func GetBlockAccountKey(address string) string {
return fmt.Sprintf("%s%s", BlockAccountPrefixAddress, address)
}
func GetBlockAccountCreatedKey(created string) string {
return fmt.Sprintf("%s%s", BlockAccountPrefixCreated, created)
}
func ExistBlockAccount(st *sebakstorage.LevelDBBackend, address string) (exists bool, err error) {
return st.Has(GetBlockAccountKey(address))
}
func GetBlockAccount(st *sebakstorage.LevelDBBackend, address string) (b *BlockAccount, err error) {
if err = st.Get(GetBlockAccountKey(address), &b); err != nil {
return
}
return
}
func GetBlockAccountAddressesByCreated(st *sebakstorage.LevelDBBackend, reverse bool) (func() (string, bool), func()) {
iterFunc, closeFunc := st.GetIterator(BlockAccountPrefixCreated, reverse)
return (func() (string, bool) {
item, hasNext := iterFunc()
if !hasNext {
return "", false
}
var address string
json.Unmarshal(item.Value, &address)
return address, hasNext
}), (func() {
closeFunc()
})
}
func GetBlockAccountsByCreated(st *sebakstorage.LevelDBBackend, reverse bool) (func() (*BlockAccount, bool), func()) {
iterFunc, closeFunc := GetBlockAccountAddressesByCreated(st, reverse)
return (func() (*BlockAccount, bool) {
address, hasNext := iterFunc()
if !hasNext {
return nil, false
}
ba, err := GetBlockAccount(st, address)
// TODO if err != nil, stopping iteration is right? how about just
// ignoring the missing one?
if err != nil {
return nil, false
}
return ba, hasNext
}), (func() {
closeFunc()
})
}
func (b *BlockAccount) GetBalance() int64 {
n, _ := strconv.ParseInt(b.Balance, 10, 64)
return n
}
func (b *BlockAccount) GetBalanceAmount() Amount {
return Amount(b.GetBalance())
}
func (b *BlockAccount) setBalance(n int64) (err error) {
if n < 0 {
err = errors.New("wrong balance value")
return
}
b.Balance = strconv.FormatInt(n, 10)
return
}
func (b *BlockAccount) EnsureUpdate(balance int64, checkpoint string, expectedBalance int64) (err error) {
n := b.GetBalance() + balance
if n != expectedBalance {
err = errors.New("unexpected balance")
return
}
b.setBalance(n)
b.Checkpoint = checkpoint
return
}