forked from stellar/go
/
asset_loader.go
219 lines (197 loc) · 5.7 KB
/
asset_loader.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
package history
import (
"context"
"database/sql/driver"
"fmt"
"sort"
"strings"
sq "github.com/Masterminds/squirrel"
"github.com/pownieh/stellar_go/support/collections/set"
"github.com/pownieh/stellar_go/support/db"
"github.com/pownieh/stellar_go/support/errors"
"github.com/pownieh/stellar_go/support/ordered"
"github.com/pownieh/stellar_go/xdr"
)
type AssetKey struct {
Type string
Code string
Issuer string
}
func (key AssetKey) String() string {
if key.Type == xdr.AssetTypeToString[xdr.AssetTypeAssetTypeNative] {
return key.Type
}
return key.Type + "/" + key.Code + "/" + key.Issuer
}
// AssetKeyFromXDR constructs an AssetKey from an xdr asset
func AssetKeyFromXDR(asset xdr.Asset) AssetKey {
return AssetKey{
Type: xdr.AssetTypeToString[asset.Type],
Code: strings.TrimRight(asset.GetCode(), "\x00"),
Issuer: asset.GetIssuer(),
}
}
// FutureAssetID represents a future history asset.
// A FutureAssetID is created by an AssetLoader and
// the asset id is available after calling Exec() on
// the AssetLoader.
type FutureAssetID struct {
asset AssetKey
loader *AssetLoader
}
// Value implements the database/sql/driver Valuer interface.
func (a FutureAssetID) Value() (driver.Value, error) {
return a.loader.GetNow(a.asset)
}
// AssetLoader will map assets to their history
// asset ids. If there is no existing mapping for a given sset,
// the AssetLoader will insert into the history_assets table to
// establish a mapping.
type AssetLoader struct {
sealed bool
set set.Set[AssetKey]
ids map[AssetKey]int64
}
// NewAssetLoader will construct a new AssetLoader instance.
func NewAssetLoader() *AssetLoader {
return &AssetLoader{
sealed: false,
set: set.Set[AssetKey]{},
ids: map[AssetKey]int64{},
}
}
// GetFuture registers the given asset into the loader and
// returns a FutureAssetID which will hold the history asset id for
// the asset after Exec() is called.
func (a *AssetLoader) GetFuture(asset AssetKey) FutureAssetID {
if a.sealed {
panic(errSealed)
}
a.set.Add(asset)
return FutureAssetID{
asset: asset,
loader: a,
}
}
// GetNow returns the history asset id for the given asset.
// GetNow should only be called on values which were registered by
// GetFuture() calls. Also, Exec() must be called before any GetNow
// call can succeed.
func (a *AssetLoader) GetNow(asset AssetKey) (int64, error) {
if !a.sealed {
return 0, fmt.Errorf(`invalid asset loader state,
Exec was not called yet to properly seal and resolve %v id`, asset)
}
if internalID, ok := a.ids[asset]; !ok {
return 0, fmt.Errorf(`asset loader id %v was not found`, asset)
} else {
return internalID, nil
}
}
func (a *AssetLoader) lookupKeys(ctx context.Context, q *Q, keys []AssetKey) error {
var rows []Asset
for i := 0; i < len(keys); i += loaderLookupBatchSize {
end := ordered.Min(len(keys), i+loaderLookupBatchSize)
subset := keys[i:end]
keyStrings := make([]string, 0, len(subset))
for _, key := range subset {
keyStrings = append(keyStrings, key.Type+"/"+key.Code+"/"+key.Issuer)
}
err := q.Select(ctx, &rows, sq.Select("*").From("history_assets").Where(sq.Eq{
"concat(asset_type, '/', asset_code, '/', asset_issuer)": keyStrings,
}))
if err != nil {
return errors.Wrap(err, "could not select assets")
}
for _, row := range rows {
a.ids[AssetKey{
Type: row.Type,
Code: row.Code,
Issuer: row.Issuer,
}] = row.ID
}
}
return nil
}
// Exec will look up all the history asset ids for the assets registered in the loader.
// If there are no history asset ids for a given set of assets, Exec will insert rows
// into the history_assets table.
func (a *AssetLoader) Exec(ctx context.Context, session db.SessionInterface) error {
a.sealed = true
if len(a.set) == 0 {
return nil
}
q := &Q{session}
keys := make([]AssetKey, 0, len(a.set))
for key := range a.set {
keys = append(keys, key)
}
if err := a.lookupKeys(ctx, q, keys); err != nil {
return err
}
assetTypes := make([]string, 0, len(a.set)-len(a.ids))
assetCodes := make([]string, 0, len(a.set)-len(a.ids))
assetIssuers := make([]string, 0, len(a.set)-len(a.ids))
// sort entries before inserting rows to prevent deadlocks on acquiring a ShareLock
// https://github.com/pownieh/stellar_go/issues/2370
sort.Slice(keys, func(i, j int) bool {
return keys[i].String() < keys[j].String()
})
insert := 0
for _, key := range keys {
if _, ok := a.ids[key]; ok {
continue
}
assetTypes = append(assetTypes, key.Type)
assetCodes = append(assetCodes, key.Code)
assetIssuers = append(assetIssuers, key.Issuer)
keys[insert] = key
insert++
}
if insert == 0 {
return nil
}
keys = keys[:insert]
err := bulkInsert(
ctx,
q,
"history_assets",
[]string{"asset_code", "asset_type", "asset_issuer"},
[]bulkInsertField{
{
name: "asset_code",
dbType: "character varying(12)",
objects: assetCodes,
},
{
name: "asset_issuer",
dbType: "character varying(56)",
objects: assetIssuers,
},
{
name: "asset_type",
dbType: "character varying(64)",
objects: assetTypes,
},
},
)
if err != nil {
return err
}
return a.lookupKeys(ctx, q, keys)
}
// AssetLoaderStub is a stub wrapper around AssetLoader which allows
// you to manually configure the mapping of assets to history asset ids
type AssetLoaderStub struct {
Loader *AssetLoader
}
// NewAssetLoaderStub returns a new AssetLoaderStub instance
func NewAssetLoaderStub() AssetLoaderStub {
return AssetLoaderStub{Loader: NewAssetLoader()}
}
// Insert updates the wrapped AssetLoaderStub so that the given asset
// address is mapped to the provided history asset id
func (a AssetLoaderStub) Insert(asset AssetKey, id int64) {
a.Loader.sealed = true
a.Loader.ids[asset] = id
}