-
Notifications
You must be signed in to change notification settings - Fork 644
/
backend.go
154 lines (129 loc) · 3.23 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
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package c
import (
"errors"
"fmt"
"math/big"
"sync"
"github.com/ava-labs/coreth/plugin/evm"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
stdcontext "context"
ethcommon "github.com/ethereum/go-ethereum/common"
)
var (
_ Backend = (*backend)(nil)
errUnknownTxType = errors.New("unknown tx type")
)
// Backend defines the full interface required to support a C-chain wallet.
type Backend interface {
common.ChainUTXOs
BuilderBackend
SignerBackend
AcceptAtomicTx(ctx stdcontext.Context, tx *evm.Tx) error
}
type backend struct {
Context
common.ChainUTXOs
accountsLock sync.RWMutex
accounts map[ethcommon.Address]*Account
}
type Account struct {
Balance *big.Int
Nonce uint64
}
func NewBackend(
ctx Context,
utxos common.ChainUTXOs,
accounts map[ethcommon.Address]*Account,
) Backend {
return &backend{
Context: ctx,
ChainUTXOs: utxos,
accounts: accounts,
}
}
func (b *backend) AcceptAtomicTx(ctx stdcontext.Context, tx *evm.Tx) error {
switch tx := tx.UnsignedAtomicTx.(type) {
case *evm.UnsignedImportTx:
for _, input := range tx.ImportedInputs {
utxoID := input.InputID()
if err := b.RemoveUTXO(ctx, tx.SourceChain, utxoID); err != nil {
return err
}
}
b.accountsLock.Lock()
defer b.accountsLock.Unlock()
for _, output := range tx.Outs {
account, ok := b.accounts[output.Address]
if !ok {
continue
}
balance := new(big.Int).SetUint64(output.Amount)
balance.Mul(balance, avaxConversionRate)
account.Balance.Add(account.Balance, balance)
}
case *evm.UnsignedExportTx:
txID := tx.ID()
for i, out := range tx.ExportedOutputs {
err := b.AddUTXO(
ctx,
tx.DestinationChain,
&avax.UTXO{
UTXOID: avax.UTXOID{
TxID: txID,
OutputIndex: uint32(i),
},
Asset: avax.Asset{ID: out.AssetID()},
Out: out.Out,
},
)
if err != nil {
return err
}
}
b.accountsLock.Lock()
defer b.accountsLock.Unlock()
for _, input := range tx.Ins {
account, ok := b.accounts[input.Address]
if !ok {
continue
}
balance := new(big.Int).SetUint64(input.Amount)
balance.Mul(balance, avaxConversionRate)
if account.Balance.Cmp(balance) == -1 {
return errInsufficientFunds
}
account.Balance.Sub(account.Balance, balance)
newNonce, err := math.Add64(input.Nonce, 1)
if err != nil {
return err
}
account.Nonce = newNonce
}
default:
return fmt.Errorf("%w: %T", errUnknownTxType, tx)
}
return nil
}
func (b *backend) Balance(_ stdcontext.Context, addr ethcommon.Address) (*big.Int, error) {
b.accountsLock.RLock()
defer b.accountsLock.RUnlock()
account, exists := b.accounts[addr]
if !exists {
return nil, database.ErrNotFound
}
return account.Balance, nil
}
func (b *backend) Nonce(_ stdcontext.Context, addr ethcommon.Address) (uint64, error) {
b.accountsLock.RLock()
defer b.accountsLock.RUnlock()
account, exists := b.accounts[addr]
if !exists {
return 0, database.ErrNotFound
}
return account.Nonce, nil
}