-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Stuck tx detector alpha * Update stuck tx detection * Add stuck_tx_detection and dual broadcast client * Add support for TXMv2 * Fix orchestrator's monitoring call * Fix AttemptBuilder * Enable DualBroadcast client * Switch DualBroadcast params to pointers * Add context to client * Fix lint * Fix DualBroadcast client * More lint fixes * Fix lint
- Loading branch information
Showing
22 changed files
with
801 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import ( | ||
"net/url" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" | ||
) | ||
|
||
type txmv2Config struct { | ||
c toml.TxmV2 | ||
} | ||
|
||
func (t *txmv2Config) Enabled() bool { | ||
return *t.c.Enabled | ||
} | ||
|
||
func (t *txmv2Config) BlockTime() *time.Duration { | ||
d := t.c.BlockTime.Duration() | ||
return &d | ||
} | ||
|
||
func (t *txmv2Config) CustomURL() *url.URL { | ||
return t.c.CustomURL.URL() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package clientwrappers | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" | ||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txm/types" | ||
) | ||
|
||
type ChainClient struct { | ||
c client.Client | ||
} | ||
|
||
func NewChainClient(client client.Client) *ChainClient { | ||
return &ChainClient{c: client} | ||
} | ||
|
||
func (c *ChainClient) NonceAt(ctx context.Context, address common.Address, blockNumber *big.Int) (uint64, error) { | ||
return c.c.NonceAt(ctx, address, blockNumber) | ||
} | ||
|
||
func (c *ChainClient) PendingNonceAt(ctx context.Context, address common.Address) (uint64, error) { | ||
return c.c.PendingNonceAt(ctx, address) | ||
} | ||
|
||
func (c *ChainClient) SendTransaction(ctx context.Context, _ *types.Transaction, attempt *types.Attempt) error { | ||
return c.c.SendTransaction(ctx, attempt.SignedTransaction) | ||
} |
130 changes: 130 additions & 0 deletions
130
core/chains/evm/txm/clientwrappers/dual_broadcast_client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package clientwrappers | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math/big" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" | ||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txm/types" | ||
) | ||
|
||
type DualBroadcastClientKeystore interface { | ||
SignMessage(ctx context.Context, address common.Address, message []byte) ([]byte, error) | ||
} | ||
|
||
type DualBroadcastClient struct { | ||
c client.Client | ||
keystore DualBroadcastClientKeystore | ||
customURL *url.URL | ||
} | ||
|
||
func NewDualBroadcastClient(c client.Client, keystore DualBroadcastClientKeystore, customURL *url.URL) *DualBroadcastClient { | ||
return &DualBroadcastClient{ | ||
c: c, | ||
keystore: keystore, | ||
customURL: customURL, | ||
} | ||
} | ||
|
||
func (d *DualBroadcastClient) NonceAt(ctx context.Context, address common.Address, blockNumber *big.Int) (uint64, error) { | ||
return d.c.NonceAt(ctx, address, blockNumber) | ||
} | ||
|
||
func (d *DualBroadcastClient) PendingNonceAt(ctx context.Context, address common.Address) (uint64, error) { | ||
body := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["%s","pending"]}`, address.String())) | ||
response, err := d.signAndPostMessage(ctx, address, body, "") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
nonce, err := hexutil.DecodeUint64(response) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to decode response %v into uint64: %w", response, err) | ||
} | ||
return nonce, nil | ||
} | ||
|
||
func (d *DualBroadcastClient) SendTransaction(ctx context.Context, tx *types.Transaction, attempt *types.Attempt) error { | ||
meta, err := tx.GetMeta() | ||
if err != nil { | ||
return err | ||
} | ||
//nolint:revive //linter nonsense | ||
if meta != nil && meta.DualBroadcast != nil && *meta.DualBroadcast && !tx.IsPurgeable { | ||
data, err := attempt.SignedTransaction.MarshalBinary() | ||
if err != nil { | ||
return err | ||
} | ||
params := "" | ||
if meta.DualBroadcastParams != nil { | ||
params = *meta.DualBroadcastParams | ||
} | ||
body := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["%s"]}`, hexutil.Encode(data))) | ||
if _, err = d.signAndPostMessage(ctx, tx.FromAddress, body, params); err != nil { | ||
return err | ||
} | ||
return nil | ||
} else { | ||
return d.c.SendTransaction(ctx, attempt.SignedTransaction) | ||
} | ||
} | ||
|
||
func (d *DualBroadcastClient) signAndPostMessage(ctx context.Context, address common.Address, body []byte, urlParams string) (result string, err error) { | ||
bodyReader := bytes.NewReader(body) | ||
postReq, err := http.NewRequestWithContext(ctx, http.MethodPost, d.customURL.String()+"?"+urlParams, bodyReader) | ||
if err != nil { | ||
return | ||
} | ||
|
||
hashedBody := crypto.Keccak256Hash(body).Hex() | ||
signedMessage, err := d.keystore.SignMessage(ctx, address, []byte(hashedBody)) | ||
if err != nil { | ||
return | ||
} | ||
|
||
postReq.Header.Add("X-Flashbots-signature", address.String()+":"+hexutil.Encode(signedMessage)) | ||
postReq.Header.Add("Content-Type", "application/json") | ||
|
||
resp, err := http.DefaultClient.Do(postReq) | ||
if err != nil { | ||
return result, fmt.Errorf("request %v failed: %w", postReq, err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
return result, fmt.Errorf("request %v failed with status: %d", postReq, resp.StatusCode) | ||
} | ||
|
||
keyJSON, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return | ||
} | ||
var response postResponse | ||
err = json.Unmarshal(keyJSON, &response) | ||
if err != nil { | ||
return result, fmt.Errorf("failed to unmarshal response into struct: %w: %s", err, string(keyJSON)) | ||
} | ||
if response.Error.Message != "" { | ||
return result, errors.New(response.Error.Message) | ||
} | ||
return response.Result, nil | ||
} | ||
|
||
type postResponse struct { | ||
Result string `json:"result,omitempty"` | ||
Error postError | ||
} | ||
|
||
type postError struct { | ||
Message string `json:"message,omitempty"` | ||
} |
Oops, something went wrong.