-
Notifications
You must be signed in to change notification settings - Fork 670
/
backend.go
81 lines (69 loc) · 1.95 KB
/
backend.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txstest
import (
"context"
"math"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
"github.com/ava-labs/avalanchego/wallet/chain/p/signer"
)
var (
_ builder.Backend = (*Backend)(nil)
_ signer.Backend = (*Backend)(nil)
)
func newBackend(
addrs set.Set[ids.ShortID],
state state.State,
sharedMemory atomic.SharedMemory,
) *Backend {
return &Backend{
addrs: addrs,
state: state,
sharedMemory: sharedMemory,
}
}
type Backend struct {
addrs set.Set[ids.ShortID]
state state.State
sharedMemory atomic.SharedMemory
}
func (b *Backend) UTXOs(_ context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error) {
if sourceChainID == constants.PlatformChainID {
return avax.GetAllUTXOs(b.state, b.addrs)
}
utxos, _, _, err := avax.GetAtomicUTXOs(
b.sharedMemory,
txs.Codec,
sourceChainID,
b.addrs,
ids.ShortEmpty,
ids.Empty,
math.MaxInt,
)
return utxos, err
}
func (b *Backend) GetUTXO(_ context.Context, chainID, utxoID ids.ID) (*avax.UTXO, error) {
if chainID == constants.PlatformChainID {
return b.state.GetUTXO(utxoID)
}
utxoBytes, err := b.sharedMemory.Get(chainID, [][]byte{utxoID[:]})
if err != nil {
return nil, err
}
utxo := avax.UTXO{}
if _, err := txs.Codec.Unmarshal(utxoBytes[0], &utxo); err != nil {
return nil, err
}
return &utxo, nil
}
func (b *Backend) GetSubnetOwner(_ context.Context, subnetID ids.ID) (fx.Owner, error) {
return b.state.GetSubnetOwner(subnetID)
}