-
Notifications
You must be signed in to change notification settings - Fork 249
/
saved_addresses.go
214 lines (186 loc) · 5.96 KB
/
saved_addresses.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
package wallet
import (
"database/sql"
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
)
type savedAddressMeta struct {
Removed bool
UpdateClock uint64 // wall clock used to deconflict concurrent updates
}
type SavedAddress struct {
Address common.Address `json:"address"`
// TODO: Add Emoji
// Emoji string `json:"emoji"`
Name string `json:"name"`
Favourite bool `json:"favourite"`
ChainShortNames string `json:"chainShortNames"` // used with address only, not with ENSName
ENSName string `json:"ens"`
IsTest bool `json:"isTest"`
CreatedAt int64 `json:"createdAt"`
savedAddressMeta
}
func (s *SavedAddress) ID() string {
return fmt.Sprintf("%s-%s-%t", s.Address.Hex(), s.ENSName, s.IsTest)
}
type SavedAddressesManager struct {
db *sql.DB
}
func NewSavedAddressesManager(db *sql.DB) *SavedAddressesManager {
return &SavedAddressesManager{db: db}
}
const rawQueryColumnsOrder = "address, name, favourite, removed, update_clock, chain_short_names, ens_name, is_test, created_at"
// getSavedAddressesFromDBRows retrieves all data based on SELECT Query using rawQueryColumnsOrder
func getSavedAddressesFromDBRows(rows *sql.Rows) ([]SavedAddress, error) {
var addresses []SavedAddress
for rows.Next() {
sa := SavedAddress{}
// based on rawQueryColumnsOrder
err := rows.Scan(&sa.Address, &sa.Name, &sa.Favourite, &sa.Removed, &sa.UpdateClock, &sa.ChainShortNames, &sa.ENSName, &sa.IsTest, &sa.CreatedAt)
if err != nil {
return nil, err
}
addresses = append(addresses, sa)
}
return addresses, nil
}
func (sam *SavedAddressesManager) getSavedAddresses(condition string) ([]SavedAddress, error) {
var whereCondition string
if condition != "" {
whereCondition = fmt.Sprintf("WHERE %s", condition)
}
rows, err := sam.db.Query(fmt.Sprintf("SELECT %s FROM saved_addresses %s", rawQueryColumnsOrder, whereCondition))
if err != nil {
return nil, err
}
defer rows.Close()
addresses, err := getSavedAddressesFromDBRows(rows)
return addresses, err
}
func (sam *SavedAddressesManager) GetSavedAddresses() ([]SavedAddress, error) {
return sam.getSavedAddresses("removed != 1")
}
// GetRawSavedAddresses provides access to the soft-delete and sync metadata
func (sam *SavedAddressesManager) GetRawSavedAddresses() ([]SavedAddress, error) {
return sam.getSavedAddresses("")
}
func (sam *SavedAddressesManager) upsertSavedAddress(sa SavedAddress, tx *sql.Tx) (err error) {
if tx == nil {
tx, err = sam.db.Begin()
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
_ = tx.Rollback()
}()
}
rows, err := tx.Query(
fmt.Sprintf("SELECT %s FROM saved_addresses WHERE address = ? AND is_test = ? AND ens_name = ?", rawQueryColumnsOrder),
sa.Address, sa.IsTest, sa.ENSName,
)
if err != nil {
return err
}
defer rows.Close()
savedAddresses, err := getSavedAddressesFromDBRows(rows)
if err != nil {
return err
}
sa.CreatedAt = time.Now().Unix()
for _, savedAddress := range savedAddresses {
if savedAddress.Address == sa.Address && savedAddress.IsTest == sa.IsTest && savedAddress.ENSName == sa.ENSName {
sa.CreatedAt = savedAddress.CreatedAt
break
}
}
sqlStatement := "INSERT OR REPLACE INTO saved_addresses (address, name, favourite, removed, update_clock, chain_short_names, ens_name, is_test, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
insert, err := tx.Prepare(sqlStatement)
if err != nil {
return err
}
defer insert.Close()
_, err = insert.Exec(sa.Address, sa.Name, sa.Favourite, sa.Removed, sa.UpdateClock, sa.ChainShortNames, sa.ENSName, sa.IsTest, sa.CreatedAt)
return err
}
func (sam *SavedAddressesManager) UpdateMetadataAndUpsertSavedAddress(sa SavedAddress) (updatedClock uint64, err error) {
sa.UpdateClock = uint64(time.Now().Unix())
err = sam.upsertSavedAddress(sa, nil)
if err != nil {
return 0, err
}
return sa.UpdateClock, nil
}
func (sam *SavedAddressesManager) startTransactionAndCheckIfNewerChange(address common.Address, ens string, isTest bool, updateClock uint64) (newer bool, tx *sql.Tx, err error) {
tx, err = sam.db.Begin()
if err != nil {
return false, nil, err
}
row := tx.QueryRow("SELECT update_clock FROM saved_addresses WHERE address = ? AND is_test = ? AND ens_name = ?", address, isTest, ens)
if err != nil {
return false, tx, err
}
var dbUpdateClock uint64
err = row.Scan(&dbUpdateClock)
if err != nil {
return err == sql.ErrNoRows, tx, err
}
return dbUpdateClock < updateClock, tx, nil
}
func (sam *SavedAddressesManager) AddSavedAddressIfNewerUpdate(sa SavedAddress, updateClock uint64) (insertedOrUpdated bool, err error) {
newer, tx, err := sam.startTransactionAndCheckIfNewerChange(sa.Address, sa.ENSName, sa.IsTest, updateClock)
defer func() {
if err == nil {
err = tx.Commit()
return
}
_ = tx.Rollback()
}()
if !newer {
return false, err
}
sa.UpdateClock = updateClock
err = sam.upsertSavedAddress(sa, tx)
if err != nil {
return false, err
}
return true, err
}
func (sam *SavedAddressesManager) DeleteSavedAddress(address common.Address, ens string, isTest bool, updateClock uint64) (deleted bool, err error) {
if err != nil {
return false, err
}
newer, tx, err := sam.startTransactionAndCheckIfNewerChange(address, ens, isTest, updateClock)
defer func() {
if err == nil {
err = tx.Commit()
return
}
_ = tx.Rollback()
}()
if !newer {
return false, err
}
update, err := tx.Prepare(`UPDATE saved_addresses SET removed = 1, update_clock = ? WHERE address = ? AND is_test = ? AND ens_name = ?`)
if err != nil {
return false, err
}
defer update.Close()
res, err := update.Exec(updateClock, address, isTest, ens)
if err != nil {
return false, err
}
nRows, err := res.RowsAffected()
if err != nil {
return false, err
}
return nRows > 0, nil
}
func (sam *SavedAddressesManager) DeleteSoftRemovedSavedAddresses(threshold uint64) error {
_, err := sam.db.Exec(`DELETE FROM saved_addresses WHERE removed = 1 AND update_clock < ?`, threshold)
return err
}