-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsingle_tx_pool.go
More file actions
229 lines (198 loc) · 6.34 KB
/
Copy pathsingle_tx_pool.go
File metadata and controls
229 lines (198 loc) · 6.34 KB
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
220
221
222
223
224
225
226
227
228
229
package requester
import (
"context"
"encoding/hex"
"fmt"
"sync"
"time"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
flowGo "github.com/onflow/flow-go/model/flow"
"github.com/rs/zerolog"
"github.com/sethvargo/go-retry"
"golang.org/x/sync/errgroup"
"github.com/onflow/flow-evm-gateway/config"
"github.com/onflow/flow-evm-gateway/metrics"
"github.com/onflow/flow-evm-gateway/models"
"github.com/onflow/flow-evm-gateway/services/requester/keystore"
)
// SingleTxPool is a simple implementation of the `TxPool` interface that submits
// transactions as soon as they arrive, without any delays or batching strategies.
type SingleTxPool struct {
logger zerolog.Logger
client *CrossSporkClient
pool *sync.Map
txPublisher *models.Publisher[*gethTypes.Transaction]
config config.Config
mux sync.Mutex
keystore *keystore.KeyStore
collector metrics.Collector
// todo add methods to inspect transaction pool state
}
var _ TxPool = &SingleTxPool{}
func NewSingleTxPool(
client *CrossSporkClient,
transactionsPublisher *models.Publisher[*gethTypes.Transaction],
logger zerolog.Logger,
config config.Config,
collector metrics.Collector,
keystore *keystore.KeyStore,
) *SingleTxPool {
// initialize the available keys metric since it is only updated when sending a tx
collector.AvailableSigningKeys(keystore.AvailableKeys())
return &SingleTxPool{
logger: logger.With().Str("component", "tx-pool").Logger(),
client: client,
txPublisher: transactionsPublisher,
pool: &sync.Map{},
config: config,
collector: collector,
keystore: keystore,
}
}
// Add creates a Cadence transaction that wraps the given EVM transaction in
// an `EVM.run` function call for execution.
//
// The Cadence transaction is submitted to the Flow network right away.
//
// If the transaction state validation is configured to run with the
// "tx-seal" strategy, the Cadence transaction status is awaited and an error
// is returned in case of a failure in submission or an EVM validation error.
// Until the Cadence transaction is sealed the transaction will stay in the
// pool and marked as pending.
//
// If the transaction state validation is configured to run with the
// "local-index" strategy, the Cadence transaction status is not awaited,
// as the necessary EVM validation checks, such as nonce/balance checks,
// have been checked against the EVM state of the local index.
func (t *SingleTxPool) Add(
ctx context.Context,
tx *gethTypes.Transaction,
) error {
t.txPublisher.Publish(tx) // publish pending transaction event
txData, err := tx.MarshalBinary()
if err != nil {
return err
}
hexEncodedTx, err := cadence.NewString(hex.EncodeToString(txData))
if err != nil {
return err
}
coinbaseAddress, err := cadence.NewString(t.config.Coinbase.Hex())
if err != nil {
return err
}
latestBlock, account, err := t.fetchFlowLatestBlockAndCOA(ctx)
if err != nil {
return err
}
script := replaceAddresses(runTxScript, t.config.FlowNetworkID)
flowTx, err := t.buildTransaction(
latestBlock,
account,
script,
cadence.NewArray([]cadence.Value{hexEncodedTx}),
coinbaseAddress,
)
if err != nil {
// If there was any error during the transaction build
// process, we record it as a dropped transaction.
t.collector.TransactionsDropped(1)
return err
}
if err := t.client.SendTransaction(ctx, *flowTx); err != nil {
return err
}
if t.config.TxStateValidation == config.TxSealValidation {
// add to pool and delete after transaction is sealed or errored out
t.pool.Store(tx.Hash(), tx)
defer t.pool.Delete(tx.Hash())
backoff := retry.WithMaxDuration(time.Minute*1, retry.NewConstant(time.Second*1))
return retry.Do(ctx, backoff, func(ctx context.Context) error {
res, err := t.client.GetTransactionResult(ctx, flowTx.ID())
if err != nil {
return fmt.Errorf("failed to retrieve flow transaction result %s: %w", flowTx.ID(), err)
}
// retry until transaction is sealed
if res.Status < flow.TransactionStatusSealed {
return retry.RetryableError(fmt.Errorf("transaction %s not sealed", flowTx.ID()))
}
if res.Error != nil {
if err, ok := parseInvalidError(res.Error); ok {
return err
}
t.logger.Error().Err(res.Error).
Str("flow-id", flowTx.ID().String()).
Str("evm-id", tx.Hash().Hex()).
Msg("flow transaction error")
// hide specific cause since it's an implementation issue
return fmt.Errorf("failed to submit flow evm transaction %s", tx.Hash())
}
return nil
})
}
return nil
}
// buildTransaction creates a Cadence transaction from the provided script,
// with the given arguments and signs it with the configured COA account.
func (t *SingleTxPool) buildTransaction(
latestBlock *flow.Block,
account *flow.Account,
script []byte,
args ...cadence.Value,
) (*flow.Transaction, error) {
defer func() {
t.collector.AvailableSigningKeys(t.keystore.AvailableKeys())
}()
flowTx := flow.NewTransaction().
SetScript(script).
SetReferenceBlockID(latestBlock.ID).
SetComputeLimit(flowGo.DefaultMaxTransactionGasLimit)
for _, arg := range args {
if err := flowTx.AddArgument(arg); err != nil {
return nil, fmt.Errorf("failed to add argument: %s, with %w", arg, err)
}
}
// building and signing transactions should be blocking,
// so we don't have keys conflict
t.mux.Lock()
defer t.mux.Unlock()
accKey, err := t.keystore.Take()
if err != nil {
return nil, err
}
if err := accKey.SetProposerPayerAndSign(flowTx, account); err != nil {
accKey.Done()
return nil, err
}
// now that the transaction is prepared, store the transaction's metadata
accKey.SetLockMetadata(flowTx.ID(), latestBlock.Height)
t.collector.OperatorBalance(account)
return flowTx, nil
}
func (t *SingleTxPool) fetchFlowLatestBlockAndCOA(ctx context.Context) (
*flow.Block,
*flow.Account,
error,
) {
var (
g = errgroup.Group{}
err1, err2 error
latestBlock *flow.Block
account *flow.Account
)
// execute concurrently so we can speed up all the information we need for tx
g.Go(func() error {
latestBlock, err1 = t.client.GetLatestBlock(ctx, true)
return err1
})
g.Go(func() error {
account, err2 = t.client.GetAccount(ctx, t.config.COAAddress)
return err2
})
if err := g.Wait(); err != nil {
return nil, nil, err
}
return latestBlock, account, nil
}