-
Notifications
You must be signed in to change notification settings - Fork 646
/
utxos.go
143 lines (113 loc) · 4.02 KB
/
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
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"context"
"sync"
"golang.org/x/exp/maps"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/avax"
)
var (
_ UTXOs = (*utxos)(nil)
_ ChainUTXOs = (*chainUTXOs)(nil)
)
type UTXOs interface {
AddUTXO(ctx context.Context, sourceChainID, destinationChainID ids.ID, utxo *avax.UTXO) error
RemoveUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error
UTXOs(ctx context.Context, sourceChainID, destinationChainID ids.ID) ([]*avax.UTXO, error)
GetUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*avax.UTXO, error)
}
type ChainUTXOs interface {
AddUTXO(ctx context.Context, destinationChainID ids.ID, utxo *avax.UTXO) error
RemoveUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) error
UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
GetUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) (*avax.UTXO, error)
}
func NewUTXOs() UTXOs {
return &utxos{
sourceToDestToUTXOIDToUTXO: make(map[ids.ID]map[ids.ID]map[ids.ID]*avax.UTXO),
}
}
func NewChainUTXOs(chainID ids.ID, utxos UTXOs) ChainUTXOs {
return &chainUTXOs{
utxos: utxos,
chainID: chainID,
}
}
type utxos struct {
lock sync.RWMutex
// sourceChainID -> destinationChainID -> utxoID -> utxo
sourceToDestToUTXOIDToUTXO map[ids.ID]map[ids.ID]map[ids.ID]*avax.UTXO
}
func (u *utxos) AddUTXO(_ context.Context, sourceChainID, destinationChainID ids.ID, utxo *avax.UTXO) error {
u.lock.Lock()
defer u.lock.Unlock()
destToUTXOIDToUTXO, ok := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
if !ok {
destToUTXOIDToUTXO = make(map[ids.ID]map[ids.ID]*avax.UTXO)
u.sourceToDestToUTXOIDToUTXO[sourceChainID] = destToUTXOIDToUTXO
}
utxoIDToUTXO, ok := destToUTXOIDToUTXO[destinationChainID]
if !ok {
utxoIDToUTXO = make(map[ids.ID]*avax.UTXO)
destToUTXOIDToUTXO[destinationChainID] = utxoIDToUTXO
}
utxoIDToUTXO[utxo.InputID()] = utxo
return nil
}
func (u *utxos) RemoveUTXO(_ context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error {
u.lock.Lock()
defer u.lock.Unlock()
destToUTXOIDToUTXO := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
utxoIDToUTXO := destToUTXOIDToUTXO[destinationChainID]
_, ok := utxoIDToUTXO[utxoID]
if !ok {
return nil
}
delete(utxoIDToUTXO, utxoID)
if len(utxoIDToUTXO) != 0 {
return nil
}
delete(destToUTXOIDToUTXO, destinationChainID)
if len(destToUTXOIDToUTXO) != 0 {
return nil
}
delete(u.sourceToDestToUTXOIDToUTXO, sourceChainID)
return nil
}
func (u *utxos) UTXOs(_ context.Context, sourceChainID, destinationChainID ids.ID) ([]*avax.UTXO, error) {
u.lock.RLock()
defer u.lock.RUnlock()
destToUTXOIDToUTXO := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
utxoIDToUTXO := destToUTXOIDToUTXO[destinationChainID]
return maps.Values(utxoIDToUTXO), nil
}
func (u *utxos) GetUTXO(_ context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*avax.UTXO, error) {
u.lock.RLock()
defer u.lock.RUnlock()
destToUTXOIDToUTXO := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
utxoIDToUTXO := destToUTXOIDToUTXO[destinationChainID]
utxo, ok := utxoIDToUTXO[utxoID]
if !ok {
return nil, database.ErrNotFound
}
return utxo, nil
}
type chainUTXOs struct {
utxos UTXOs
chainID ids.ID
}
func (c *chainUTXOs) AddUTXO(ctx context.Context, destinationChainID ids.ID, utxo *avax.UTXO) error {
return c.utxos.AddUTXO(ctx, c.chainID, destinationChainID, utxo)
}
func (c *chainUTXOs) RemoveUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) error {
return c.utxos.RemoveUTXO(ctx, sourceChainID, c.chainID, utxoID)
}
func (c *chainUTXOs) UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error) {
return c.utxos.UTXOs(ctx, sourceChainID, c.chainID)
}
func (c *chainUTXOs) GetUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) (*avax.UTXO, error) {
return c.utxos.GetUTXO(ctx, sourceChainID, c.chainID, utxoID)
}