-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsingle_tx_pool.go
More file actions
253 lines (218 loc) · 7.4 KB
/
Copy pathsingle_tx_pool.go
File metadata and controls
253 lines (218 loc) · 7.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package requester
import (
"context"
"encoding/hex"
"fmt"
"sync"
"sync/atomic"
"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"
"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"
)
const referenceBlockUpdateFrequency = time.Second * 15
// 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
// referenceBlockHeader is stored atomically to avoid races
// between request path and ticker updates.
referenceBlockHeader atomic.Value // stores *flow.BlockHeader
// todo add methods to inspect transaction pool state
}
var _ TxPool = &SingleTxPool{}
func NewSingleTxPool(
ctx context.Context,
client *CrossSporkClient,
transactionsPublisher *models.Publisher[*gethTypes.Transaction],
logger zerolog.Logger,
config config.Config,
collector metrics.Collector,
keystore *keystore.KeyStore,
) (*SingleTxPool, error) {
referenceBlockHeader, err := client.GetLatestBlockHeader(ctx, false)
if err != nil {
return nil, err
}
// initialize the available keys metric since it is only updated when sending a tx
collector.AvailableSigningKeys(keystore.AvailableKeys())
singleTxPool := &SingleTxPool{
logger: logger.With().Str("component", "tx-pool").Logger(),
client: client,
txPublisher: transactionsPublisher,
pool: &sync.Map{},
config: config,
collector: collector,
keystore: keystore,
}
singleTxPool.referenceBlockHeader.Store(referenceBlockHeader)
go singleTxPool.updateReferenceBlock(ctx)
return singleTxPool, nil
}
// 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
}
script := replaceAddresses(runTxScript, t.config.FlowNetworkID)
flowTx, err := t.buildTransaction(
ctx,
t.getReferenceBlock(),
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)
t.logger.Error().Err(err).Str("tx-hash", tx.Hash().Hex()).Msg("failed to build Flow transaction, EVM transaction dropped")
return err
}
if err := t.client.SendTransaction(ctx, *flowTx); err != nil {
t.collector.TransactionsDropped(1)
t.logger.Error().Err(err).Str("tx-hash", tx.Hash().Hex()).Msg("failed to send Flow transaction, EVM transaction dropped")
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(
ctx context.Context,
referenceBlockHeader *flow.BlockHeader,
script []byte,
args ...cadence.Value,
) (*flow.Transaction, error) {
defer func() {
t.collector.AvailableSigningKeys(t.keystore.AvailableKeys())
}()
flowTx := flow.NewTransaction().
SetScript(script).
SetReferenceBlockID(referenceBlockHeader.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)
}
}
accKey, err := t.fetchSigningAccountKey()
if err != nil {
return nil, err
}
coaAddress := t.config.COAAddress
accountKey, err := t.client.GetAccountKeyAtLatestBlock(ctx, coaAddress, accKey.Index)
if err != nil {
accKey.Done()
return nil, err
}
if err := accKey.SetProposerPayerAndSign(flowTx, coaAddress, accountKey); err != nil {
accKey.Done()
return nil, err
}
// now that the transaction is prepared, store the transaction's metadata
accKey.SetLockMetadata(flowTx.ID(), referenceBlockHeader.Height)
return flowTx, nil
}
func (t *SingleTxPool) fetchSigningAccountKey() (*keystore.AccountKey, error) {
// getting an account key from the `KeyStore` for signing transactions,
// should be lock-protected, so that we don't sign any two Flow
// transactions with the same account key
t.mux.Lock()
defer t.mux.Unlock()
return t.keystore.Take()
}
func (t *SingleTxPool) getReferenceBlock() *flow.BlockHeader {
if v := t.referenceBlockHeader.Load(); v != nil {
return v.(*flow.BlockHeader)
}
return nil
}
func (t *SingleTxPool) updateReferenceBlock(ctx context.Context) {
ticker := time.NewTicker(referenceBlockUpdateFrequency)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
blockHeader, err := t.client.GetLatestBlockHeader(ctx, false)
if err != nil {
t.logger.Error().Err(err).Msg(
"failed to update the reference block",
)
continue
}
t.referenceBlockHeader.Store(blockHeader)
}
}
}