-
Notifications
You must be signed in to change notification settings - Fork 665
/
atomic_utxos.go
101 lines (88 loc) · 2.27 KB
/
atomic_utxos.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avax
import (
"fmt"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
)
const (
maxUTXOsToFetch = 1024
)
var _ AtomicUTXOManager = &atomicUTXOManager{}
type AtomicUTXOManager interface {
// GetAtomicUTXOs returns exported UTXOs such that at least one of the
// addresses in [addrs] is referenced.
//
// Returns at most [limit] UTXOs. If [limit] <= 0 or
// [limit] > [maxUTXOsToFetch], [limit] is set to [maxUTXOsToFetch].
//
// Returns:
// * The fetched UTXOs
// * The address associated with the last UTXO fetched
// * The ID of the last UTXO fetched
// * Any error that may have occurred upstream.
GetAtomicUTXOs(
chainID ids.ID,
addrs ids.ShortSet,
startAddr ids.ShortID,
startUTXOID ids.ID,
limit int,
) ([]*UTXO, ids.ShortID, ids.ID, error)
}
type atomicUTXOManager struct {
sm atomic.SharedMemory
codec codec.Manager
}
func NewAtomicUTXOManager(sm atomic.SharedMemory, codec codec.Manager) AtomicUTXOManager {
return &atomicUTXOManager{
sm: sm,
codec: codec,
}
}
func (a *atomicUTXOManager) GetAtomicUTXOs(
chainID ids.ID,
addrs ids.ShortSet,
startAddr ids.ShortID,
startUTXOID ids.ID,
limit int,
) ([]*UTXO, ids.ShortID, ids.ID, error) {
if limit <= 0 || limit > maxUTXOsToFetch {
limit = maxUTXOsToFetch
}
addrsList := make([][]byte, addrs.Len())
i := 0
for addr := range addrs {
copied := addr
addrsList[i] = copied[:]
i++
}
allUTXOBytes, lastAddr, lastUTXO, err := a.sm.Indexed(
chainID,
addrsList,
startAddr.Bytes(),
startUTXOID[:],
limit,
)
if err != nil {
return nil, ids.ShortID{}, ids.ID{}, fmt.Errorf("error fetching atomic UTXOs: %w", err)
}
lastAddrID, err := ids.ToShortID(lastAddr)
if err != nil {
lastAddrID = ids.ShortEmpty
}
lastUTXOID, err := ids.ToID(lastUTXO)
if err != nil {
lastUTXOID = ids.Empty
}
utxos := make([]*UTXO, len(allUTXOBytes))
for i, utxoBytes := range allUTXOBytes {
utxo := &UTXO{}
if _, err := a.codec.Unmarshal(utxoBytes, utxo); err != nil {
return nil, ids.ShortID{}, ids.ID{}, fmt.Errorf("error parsing UTXO: %w", err)
}
utxos[i] = utxo
}
return utxos, lastAddrID, lastUTXOID, nil
}