forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
170 lines (143 loc) · 4.54 KB
/
sender.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package relayer
import (
"context"
"crypto/ecdsa"
"math/big"
"slices"
"strings"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/lib/txmgr"
"github.com/omni-network/omni/lib/xchain"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// Sender uses txmgr to send transactions to the destination chain.
type Sender struct {
txMgr txmgr.TxManager
portal common.Address
abi *abi.ABI
chain netconf.Chain
chainNames map[uint64]string
rpcClient ethclient.Client
}
// NewSender creates a new sender that uses txmgr to send transactions to the destination chain.
func NewSender(chain netconf.Chain, rpcClient ethclient.Client,
privateKey ecdsa.PrivateKey, chainNames map[uint64]string) (Sender, error) {
// we want to query receipts every 1/3 of the block time
cfg, err := txmgr.NewConfig(txmgr.NewCLIConfig(
chain.ID,
chain.BlockPeriod/3,
txmgr.DefaultSenderFlagValues,
),
&privateKey,
rpcClient,
)
if err != nil {
return Sender{}, err
}
txMgr, err := txmgr.NewSimple(chain.Name, cfg)
if err != nil {
return Sender{}, errors.Wrap(err, "create tx mgr")
}
// Create ABI
parsedAbi, err := abi.JSON(strings.NewReader(bindings.OmniPortalMetaData.ABI))
if err != nil {
return Sender{}, errors.Wrap(err, "parse abi error")
}
return Sender{
txMgr: txMgr,
portal: chain.PortalAddress,
abi: &parsedAbi,
chain: chain,
chainNames: chainNames,
rpcClient: rpcClient,
}, nil
}
// SendTransaction sends the submission to the destination chain.
func (o Sender) SendTransaction(ctx context.Context, submission xchain.Submission) error {
if o.txMgr == nil {
return errors.New("tx mgr not found", "dest_chain_id", submission.DestChainID)
} else if submission.DestChainID != o.chain.ID {
return errors.New("unexpected destination chain [BUG]",
"got", submission.DestChainID, "expect", o.chain.ID)
}
// Get some info for logging
var startOffset uint64
if len(submission.Msgs) > 0 {
startOffset = submission.Msgs[0].StreamOffset
}
dstChain := o.chain.Name
srcChain := o.chainNames[submission.BlockHeader.SourceChainID]
// Request attributes added to context (for downstream logging) and manually added to errors (for upstream logging).
reqAttrs := []any{
"req_id", randomHex7(),
"src_chain", srcChain,
}
ctx = log.WithCtx(ctx, reqAttrs...)
log.Debug(ctx, "Received submission",
"start_offset", startOffset,
"msgs", len(submission.Msgs),
)
const gasLimit = 1_000_000 // TODO(lazar): make configurable
txData, err := o.getXSubmitBytes(submissionToBinding(submission))
if err != nil {
return err
}
candidate := txmgr.TxCandidate{
TxData: txData,
To: &o.portal,
GasLimit: gasLimit,
Value: big.NewInt(0),
}
tx, rec, err := o.txMgr.Send(ctx, candidate)
if err != nil {
return errors.Wrap(err, "failed to send tx", reqAttrs...)
}
submissionTotal.WithLabelValues(srcChain, dstChain).Inc()
msgTotal.WithLabelValues(srcChain, dstChain).Add(float64(len(submission.Msgs)))
receiptAttrs := []any{
"valset_id", submission.ValidatorSetID,
"status", rec.Status,
"nonce", tx.Nonce(),
"gas_used", rec.GasUsed,
"tx_hash", rec.TxHash,
}
if rec.Status == 0 {
// Try and get debug information of the reverted transaction
resp, err := o.rpcClient.CallContract(ctx, ethereum.CallMsg{
From: o.txMgr.From(),
To: tx.To(),
Gas: tx.Gas(),
GasPrice: tx.GasPrice(),
GasFeeCap: tx.GasFeeCap(),
GasTipCap: tx.GasTipCap(),
Value: tx.Value(),
Data: tx.Data(),
AccessList: tx.AccessList(),
BlobGasFeeCap: tx.BlobGasFeeCap(),
BlobHashes: tx.BlobHashes(),
}, rec.BlockNumber)
errAttrs := slices.Concat(receiptAttrs, reqAttrs, []any{
"call_resp", hexutil.Encode(resp),
"call_err", err,
})
revertedSubmissionTotal.WithLabelValues(srcChain, dstChain).Inc()
return errors.New("submission reverted", errAttrs...)
}
log.Info(ctx, "Sent submission", receiptAttrs...)
return nil
}
// getXSubmitBytes returns the byte representation of the xsubmit function call.
func (o Sender) getXSubmitBytes(sub bindings.XSubmission) ([]byte, error) {
bytes, err := o.abi.Pack("xsubmit", sub)
if err != nil {
return nil, errors.Wrap(err, "pack xsubmit")
}
return bytes, nil
}