-
Notifications
You must be signed in to change notification settings - Fork 337
/
addressbook.go
220 lines (191 loc) · 7.02 KB
/
addressbook.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package swap
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
var (
peerPrefix = "swap_chequebook_peer_"
peerChequebookPrefix = "swap_peer_chequebook_"
beneficiaryPeerPrefix = "swap_beneficiary_peer_"
peerBeneficiaryPrefix = "swap_peer_beneficiary_"
deductedForPeerPrefix = "swap_deducted_for_peer_"
deductedByPeerPrefix = "swap_deducted_by_peer_"
)
// Addressbook maps peers to beneficaries, chequebooks and in reverse.
type Addressbook interface {
// Beneficiary returns the beneficiary for the given peer.
Beneficiary(peer swarm.Address) (beneficiary common.Address, known bool, err error)
// Chequebook returns the chequebook for the given peer.
Chequebook(peer swarm.Address) (chequebookAddress common.Address, known bool, err error)
// BeneficiaryPeer returns the peer for a beneficiary.
BeneficiaryPeer(beneficiary common.Address) (peer swarm.Address, known bool, err error)
// ChequebookPeer returns the peer for a beneficiary.
ChequebookPeer(chequebook common.Address) (peer swarm.Address, known bool, err error)
// PutBeneficiary stores the beneficiary for the given peer.
PutBeneficiary(peer swarm.Address, beneficiary common.Address) error
// PutChequebook stores the chequebook for the given peer.
PutChequebook(peer swarm.Address, chequebook common.Address) error
// AddDeductionFor peer stores the flag indicating the peer have already issued a cheque that has been deducted
AddDeductionFor(peer swarm.Address) error
// AddDeductionFor peer stores the flag indicating the peer have already received a cheque that has been deducted
AddDeductionBy(peer swarm.Address) error
// GetDeductionFor returns whether a peer have already issued a cheque that has been deducted
GetDeductionFor(peer swarm.Address) (bool, error)
// GetDeductionBy returns whether a peer have already received a cheque that has been deducted
GetDeductionBy(peer swarm.Address) (bool, error)
// MigratePeer returns whether a peer have already received a cheque that has been deducted
MigratePeer(oldPeer, newPeer swarm.Address) error
}
type addressbook struct {
store storage.StateStorer
}
// NewAddressbook creates a new addressbook using the store.
func NewAddressbook(store storage.StateStorer) Addressbook {
return &addressbook{
store: store,
}
}
func (a *addressbook) MigratePeer(oldPeer, newPeer swarm.Address) error {
ba, known, err := a.Beneficiary(oldPeer)
if err != nil {
return err
}
if !known {
return errors.New("old beneficiary not known")
}
cb, known, err := a.Chequebook(oldPeer)
if err != nil {
return err
}
if err := a.PutBeneficiary(newPeer, ba); err != nil {
return err
}
if err := a.store.Delete(peerBeneficiaryKey(oldPeer)); err != nil {
return err
}
if known {
if err := a.PutChequebook(newPeer, cb); err != nil {
return err
}
if err := a.store.Delete(peerKey(oldPeer)); err != nil {
return err
}
}
return nil
}
// Beneficiary returns the beneficiary for the given peer.
func (a *addressbook) Beneficiary(peer swarm.Address) (beneficiary common.Address, known bool, err error) {
err = a.store.Get(peerBeneficiaryKey(peer), &beneficiary)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return common.Address{}, false, err
}
return common.Address{}, false, nil
}
return beneficiary, true, nil
}
// BeneficiaryPeer returns the peer for a beneficiary.
func (a *addressbook) BeneficiaryPeer(beneficiary common.Address) (peer swarm.Address, known bool, err error) {
err = a.store.Get(beneficiaryPeerKey(beneficiary), &peer)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return swarm.Address{}, false, err
}
return swarm.Address{}, false, nil
}
return peer, true, nil
}
// Chequebook returns the chequebook for the given peer.
func (a *addressbook) Chequebook(peer swarm.Address) (chequebookAddress common.Address, known bool, err error) {
err = a.store.Get(peerKey(peer), &chequebookAddress)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return common.Address{}, false, err
}
return common.Address{}, false, nil
}
return chequebookAddress, true, nil
}
// ChequebookPeer returns the peer for a beneficiary.
func (a *addressbook) ChequebookPeer(chequebook common.Address) (peer swarm.Address, known bool, err error) {
err = a.store.Get(chequebookPeerKey(chequebook), &peer)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return swarm.Address{}, false, err
}
return swarm.Address{}, false, nil
}
return peer, true, nil
}
// PutBeneficiary stores the beneficiary for the given peer.
func (a *addressbook) PutBeneficiary(peer swarm.Address, beneficiary common.Address) error {
err := a.store.Put(peerBeneficiaryKey(peer), beneficiary)
if err != nil {
return err
}
return a.store.Put(beneficiaryPeerKey(beneficiary), peer)
}
// PutChequebook stores the chequebook for the given peer.
func (a *addressbook) PutChequebook(peer swarm.Address, chequebook common.Address) error {
err := a.store.Put(peerKey(peer), chequebook)
if err != nil {
return err
}
return a.store.Put(chequebookPeerKey(chequebook), peer)
}
func (a *addressbook) AddDeductionFor(peer swarm.Address) error {
return a.store.Put(peerDeductedForKey(peer), struct{}{})
}
func (a *addressbook) AddDeductionBy(peer swarm.Address) error {
return a.store.Put(peerDeductedByKey(peer), struct{}{})
}
func (a *addressbook) GetDeductionFor(peer swarm.Address) (bool, error) {
var nothing struct{}
err := a.store.Get(peerDeductedForKey(peer), ¬hing)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return false, err
}
return false, nil
}
return true, nil
}
func (a *addressbook) GetDeductionBy(peer swarm.Address) (bool, error) {
var nothing struct{}
err := a.store.Get(peerDeductedByKey(peer), ¬hing)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return false, err
}
return false, nil
}
return true, nil
}
// peerKey computes the key where to store the chequebook from a peer.
func peerKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", peerPrefix, peer)
}
// chequebookPeerKey computes the key where to store the peer for a chequebook.
func chequebookPeerKey(chequebook common.Address) string {
return fmt.Sprintf("%s%x", peerChequebookPrefix, chequebook)
}
// peerBeneficiaryKey computes the key where to store the beneficiary for a peer.
func peerBeneficiaryKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", peerBeneficiaryPrefix, peer)
}
// beneficiaryPeerKey computes the key where to store the peer for a beneficiary.
func beneficiaryPeerKey(peer common.Address) string {
return fmt.Sprintf("%s%x", beneficiaryPeerPrefix, peer)
}
func peerDeductedByKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", deductedByPeerPrefix, peer.String())
}
func peerDeductedForKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", deductedForPeerPrefix, peer.String())
}