-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpool.go
117 lines (99 loc) · 3.59 KB
/
pool.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
package requester
import (
"context"
"fmt"
"regexp"
"sync"
"time"
"github.com/onflow/flow-go-sdk"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/rs/zerolog"
"github.com/sethvargo/go-retry"
"github.com/onflow/flow-evm-gateway/config"
"github.com/onflow/flow-evm-gateway/models"
errs "github.com/onflow/flow-evm-gateway/models/errors"
)
const (
evmErrorRegex = `evm_error=(.*)\n`
)
// todo this is a simple implementation of the transaction pool that is mostly used
// to track the status of submitted transaction, but transactions will always be submitted
// right away, future improvements can make it so the transactions are collected in the pool
// and after submitted based on different strategies.
type TxPool struct {
logger zerolog.Logger
client *CrossSporkClient
pool *sync.Map
txPublisher *models.Publisher[*gethTypes.Transaction]
config config.Config
// todo add methods to inspect transaction pool state
}
func NewTxPool(
client *CrossSporkClient,
transactionsPublisher *models.Publisher[*gethTypes.Transaction],
logger zerolog.Logger,
config config.Config,
) *TxPool {
return &TxPool{
logger: logger.With().Str("component", "tx-pool").Logger(),
client: client,
txPublisher: transactionsPublisher,
pool: &sync.Map{},
config: config,
}
}
// Send flow transaction that executes EVM run function which takes in the encoded EVM transaction.
// The flow transaction status is awaited and an error is returned in case of a failure in submission,
// or an EVM validation error.
// Until the flow transaction is sealed the transaction will stay in the transaction pool marked as pending.
func (t *TxPool) Send(
ctx context.Context,
flowTx *flow.Transaction,
evmTx *gethTypes.Transaction,
) error {
t.txPublisher.Publish(evmTx) // publish pending transaction event
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(evmTx.Hash(), evmTx)
defer t.pool.Delete(evmTx.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", evmTx.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", evmTx.Hash())
}
return nil
})
}
return nil
}
// this will extract the evm specific error from the Flow transaction error message
// the run.cdc script panics with the evm specific error as the message which we
// extract and return to the client. Any error returned that is evm specific
// is a validation error due to assert statement in the run.cdc script.
func parseInvalidError(err error) (error, bool) {
r := regexp.MustCompile(evmErrorRegex)
matches := r.FindStringSubmatch(err.Error())
if len(matches) != 2 {
return nil, false
}
return errs.NewFailedTransactionError(matches[1]), true
}