-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallet.go
184 lines (156 loc) · 5 KB
/
wallet.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package testutil
import (
"fmt"
"slices"
"sort"
"sync"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/coreutils/wallet"
)
// An EphemeralWalletStore is a Store that does not persist its state to disk. It is
// primarily useful for testing or as a reference implementation.
type (
EphemeralWalletStore struct {
privateKey types.PrivateKey
mu sync.Mutex
uncommitted []*chain.ApplyUpdate
tip types.ChainIndex
utxos map[types.SiacoinOutputID]wallet.SiacoinElement
events []wallet.Event
}
ephemeralWalletUpdateTxn struct {
store *EphemeralWalletStore
}
)
func (et *ephemeralWalletUpdateTxn) WalletStateElements() (elements []types.StateElement, _ error) {
for _, se := range et.store.utxos {
elements = append(elements, se.StateElement)
}
return
}
func (et *ephemeralWalletUpdateTxn) UpdateStateElements(elements []types.StateElement) error {
for _, se := range elements {
utxo := et.store.utxos[types.SiacoinOutputID(se.ID)]
utxo.StateElement = se
et.store.utxos[types.SiacoinOutputID(se.ID)] = utxo
}
return nil
}
func (et *ephemeralWalletUpdateTxn) AddEvents(events []wallet.Event) error {
et.store.events = append(events, et.store.events...)
return nil
}
func (et *ephemeralWalletUpdateTxn) AddSiacoinElements(elements []wallet.SiacoinElement) error {
for _, se := range elements {
if _, ok := et.store.utxos[types.SiacoinOutputID(se.ID)]; ok {
return fmt.Errorf("siacoin element %q already exists", se.ID)
}
et.store.utxos[types.SiacoinOutputID(se.ID)] = se
}
return nil
}
func (et *ephemeralWalletUpdateTxn) RemoveSiacoinElements(ids []types.SiacoinOutputID) error {
for _, id := range ids {
if _, ok := et.store.utxos[id]; !ok {
return fmt.Errorf("siacoin element %q does not exist", id)
}
delete(et.store.utxos, id)
}
return nil
}
func (et *ephemeralWalletUpdateTxn) RevertIndex(index types.ChainIndex) error {
// remove any transactions that were added in the reverted block
filtered := et.store.events[:0]
for i := range et.store.events {
if et.store.events[i].Index == index {
continue
}
filtered = append(filtered, et.store.events[i])
}
et.store.events = filtered
// remove any siacoin elements that were added in the reverted block
for id, se := range et.store.utxos {
if se.Index == index {
delete(et.store.utxos, id)
}
}
return nil
}
// WalletEvents returns the wallet's events.
func (es *EphemeralWalletStore) WalletEvents(limit, offset int) ([]wallet.Event, error) {
es.mu.Lock()
defer es.mu.Unlock()
n := len(es.events)
start, end := offset, offset+limit
if start > n {
return nil, nil
} else if end > n {
end = n
}
// events are inserted in chronological order, reverse the slice to get the
// correct display order then sort by maturity height, so
// immature events are displayed first.
events := append([]wallet.Event(nil), es.events...)
slices.Reverse(events)
sort.SliceStable(events, func(i, j int) bool {
return events[i].MaturityHeight > events[j].MaturityHeight
})
return events[start:end], nil
}
// WalletEventCount returns the number of events relevant to the wallet.
func (es *EphemeralWalletStore) WalletEventCount() (uint64, error) {
es.mu.Lock()
defer es.mu.Unlock()
return uint64(len(es.events)), nil
}
// UnspentSiacoinElements returns the wallet's unspent siacoin outputs.
func (es *EphemeralWalletStore) UnspentSiacoinElements() (utxos []wallet.SiacoinElement, _ error) {
es.mu.Lock()
defer es.mu.Unlock()
for _, se := range es.utxos {
utxos = append(utxos, se)
}
return utxos, nil
}
// Tip returns the last indexed tip of the wallet.
func (es *EphemeralWalletStore) Tip() (types.ChainIndex, error) {
es.mu.Lock()
defer es.mu.Unlock()
return es.tip, nil
}
// ProcessChainApplyUpdate implements chain.Subscriber.
func (es *EphemeralWalletStore) ProcessChainApplyUpdate(cau *chain.ApplyUpdate, mayCommit bool) error {
es.mu.Lock()
defer es.mu.Unlock()
es.uncommitted = append(es.uncommitted, cau)
if !mayCommit {
return nil
}
address := types.StandardUnlockHash(es.privateKey.PublicKey())
ephemeralWalletUpdateTxn := &ephemeralWalletUpdateTxn{store: es}
if err := wallet.ApplyChainUpdates(ephemeralWalletUpdateTxn, address, es.uncommitted); err != nil {
return err
}
es.tip = cau.State.Index
es.uncommitted = nil
return nil
}
// ProcessChainRevertUpdate implements chain.Subscriber.
func (es *EphemeralWalletStore) ProcessChainRevertUpdate(cru *chain.RevertUpdate) error {
es.mu.Lock()
defer es.mu.Unlock()
if len(es.uncommitted) > 0 && es.uncommitted[len(es.uncommitted)-1].State.Index == cru.State.Index {
es.uncommitted = es.uncommitted[:len(es.uncommitted)-1]
return nil
}
address := types.StandardUnlockHash(es.privateKey.PublicKey())
return wallet.RevertChainUpdate(&ephemeralWalletUpdateTxn{store: es}, address, cru)
}
// NewEphemeralWalletStore returns a new EphemeralWalletStore.
func NewEphemeralWalletStore(pk types.PrivateKey) *EphemeralWalletStore {
return &EphemeralWalletStore{
privateKey: pk,
utxos: make(map[types.SiacoinOutputID]wallet.SiacoinElement),
}
}