-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Transaction Simulation to the TXM #12503
Changes from 24 commits
5a87d87
bdb9fef
46b13be
141dfea
4835554
27922a3
befeaf0
fbdd110
2f7078f
fc70e7f
389e18f
37b783e
cdc58b2
e126805
0c22a6f
c7eae50
53efe22
8ad2510
3f53867
ce8a3c7
d7605cc
f301d43
eb34bc7
68de594
1a25270
6798e4d
87c8eac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chainlink": minor | ||
--- | ||
|
||
Added a tx simulation feature to the chain client to enable testing for zk out-of-counter (OOC) errors |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,8 @@ type chainClient struct { | |
RPCClient, | ||
rpc.BatchElem, | ||
] | ||
logger logger.SugaredLogger | ||
logger logger.SugaredLogger | ||
chainType config.ChainType | ||
} | ||
|
||
func NewChainClient( | ||
|
@@ -269,3 +270,12 @@ func (c *chainClient) TransactionReceipt(ctx context.Context, txHash common.Hash | |
func (c *chainClient) LatestFinalizedBlock(ctx context.Context) (*evmtypes.Head, error) { | ||
return c.multiNode.LatestFinalizedBlock(ctx) | ||
} | ||
|
||
func (c *chainClient) CheckTxValidity(ctx context.Context, from common.Address, to common.Address, data []byte) *SendError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prashant and I talked this over a bit in the thread. I don't think this could be handled with just a bool or a basic error type. There might be some additional errors that the product teams could check for when simulating like service unavailable or timeout. If it was a simple bool or error type, they wouldn't be able to differentiate between these which is why we opted to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an opportunity to clean up our error responses handling across ALL of chain client interface. |
||
msg := ethereum.CallMsg{ | ||
From: from, | ||
To: &to, | ||
Data: data, | ||
} | ||
return SimulateTransaction(ctx, c, c.logger, c.chainType, msg) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why create a separate simulatorClient instead of adding the logic directly to the existing client? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before when we were using the custom zkEvm method, it was so we could maintain chain specific code without crowding the client. It's possible now that it's simplified but I think there's a realistic possibility that we may need to support a custom method for a chain that's still in-progress. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might regret this in the future, but I still prefer to have this inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would maybe argue that chain client is more so EVM specific code. So if something requires chain specific code where different EVM chains have different implementation, my opinion is we'd want to separate that from the chain client. That being said, we skipped on using the zkevm chain specific code here so I'm not against moving this to the chain client. We could always separate this out again later when there's a need. But before I do, let me get agreement from others. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be ok with anything here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to wait to avoid another back and forth while product teams are waiting. But I agree we'll have more changes come to this code in the near future when we can make this decision. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink/v2/common/config" | ||
) | ||
|
||
type simulatorClient interface { | ||
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error | ||
} | ||
|
||
// ZK chains can return an out-of-counters error | ||
// This method allows a caller to determine if a tx would fail due to OOC error by simulating the transaction | ||
// Used as an entry point in case custom simulation is required across different chains | ||
func SimulateTransaction(ctx context.Context, client simulatorClient, lggr logger.SugaredLogger, chainType config.ChainType, msg ethereum.CallMsg) *SendError { | ||
err := simulateTransactionDefault(ctx, client, msg) | ||
return NewSendError(err) | ||
} | ||
|
||
// eth_estimateGas returns out-of-counters (OOC) error if the transaction would result in an overflow | ||
func simulateTransactionDefault(ctx context.Context, client simulatorClient, msg ethereum.CallMsg) error { | ||
var result hexutil.Big | ||
return client.CallContext(ctx, &result, "eth_estimateGas", toCallArg(msg), "pending") | ||
} | ||
|
||
func toCallArg(msg ethereum.CallMsg) interface{} { | ||
arg := map[string]interface{}{ | ||
"from": msg.From, | ||
"to": msg.To, | ||
} | ||
if len(msg.Data) > 0 { | ||
arg["input"] = hexutil.Bytes(msg.Data) | ||
} | ||
if msg.Value != nil { | ||
arg["value"] = (*hexutil.Big)(msg.Value) | ||
} | ||
if msg.Gas != 0 { | ||
arg["gas"] = hexutil.Uint64(msg.Gas) | ||
} | ||
if msg.GasPrice != nil { | ||
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) | ||
} | ||
if msg.GasFeeCap != nil { | ||
arg["maxFeePerGas"] = (*hexutil.Big)(msg.GasFeeCap) | ||
} | ||
if msg.GasTipCap != nil { | ||
arg["maxPriorityFeePerGas"] = (*hexutil.Big)(msg.GasTipCap) | ||
} | ||
return arg | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package client_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/gjson" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" | ||
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest" | ||
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils" | ||
) | ||
|
||
func TestSimulateTx_Default(t *testing.T) { | ||
t.Parallel() | ||
|
||
fromAddress := testutils.NewAddress() | ||
toAddress := testutils.NewAddress() | ||
ctx := testutils.Context(t) | ||
|
||
amit-momin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Run("returns without error if simulation passes", func(t *testing.T) { | ||
wsURL := testutils.NewWSServer(t, &cltest.FixtureChainID, func(method string, params gjson.Result) (resp testutils.JSONRPCResponse) { | ||
switch method { | ||
case "eth_subscribe": | ||
resp.Result = `"0x00"` | ||
resp.Notify = headResult | ||
return | ||
case "eth_unsubscribe": | ||
resp.Result = "true" | ||
return | ||
case "eth_estimateGas": | ||
resp.Result = `"0x100"` | ||
} | ||
return | ||
}).WSURL().String() | ||
|
||
ethClient := mustNewChainClient(t, wsURL) | ||
err := ethClient.Dial(ctx) | ||
require.NoError(t, err) | ||
|
||
msg := ethereum.CallMsg{ | ||
From: fromAddress, | ||
To: &toAddress, | ||
Data: []byte("0x00"), | ||
} | ||
sendErr := client.SimulateTransaction(ctx, ethClient, logger.TestSugared(t), "", msg) | ||
require.Empty(t, sendErr) | ||
}) | ||
|
||
t.Run("returns error if simulation returns zk out-of-counters error", func(t *testing.T) { | ||
wsURL := testutils.NewWSServer(t, &cltest.FixtureChainID, func(method string, params gjson.Result) (resp testutils.JSONRPCResponse) { | ||
switch method { | ||
case "eth_subscribe": | ||
resp.Result = `"0x00"` | ||
resp.Notify = headResult | ||
return | ||
case "eth_unsubscribe": | ||
resp.Result = "true" | ||
return | ||
case "eth_estimateGas": | ||
resp.Error.Code = -32000 | ||
resp.Result = `"0x100"` | ||
resp.Error.Message = "not enough keccak counters to continue the execution" | ||
} | ||
return | ||
}).WSURL().String() | ||
|
||
ethClient := mustNewChainClient(t, wsURL) | ||
err := ethClient.Dial(ctx) | ||
require.NoError(t, err) | ||
|
||
msg := ethereum.CallMsg{ | ||
From: fromAddress, | ||
To: &toAddress, | ||
Data: []byte("0x00"), | ||
} | ||
sendErr := client.SimulateTransaction(ctx, ethClient, logger.TestSugared(t), "", msg) | ||
require.Equal(t, true, sendErr.IsOutOfCounters()) | ||
}) | ||
|
||
t.Run("returns without error if simulation returns non-OOC error", func(t *testing.T) { | ||
wsURL := testutils.NewWSServer(t, &cltest.FixtureChainID, func(method string, params gjson.Result) (resp testutils.JSONRPCResponse) { | ||
switch method { | ||
case "eth_subscribe": | ||
resp.Result = `"0x00"` | ||
resp.Notify = headResult | ||
return | ||
case "eth_unsubscribe": | ||
resp.Result = "true" | ||
return | ||
case "eth_estimateGas": | ||
resp.Error.Code = -32000 | ||
resp.Error.Message = "something went wrong" | ||
} | ||
return | ||
}).WSURL().String() | ||
|
||
ethClient := mustNewChainClient(t, wsURL) | ||
err := ethClient.Dial(ctx) | ||
require.NoError(t, err) | ||
|
||
msg := ethereum.CallMsg{ | ||
From: fromAddress, | ||
To: &toAddress, | ||
Data: []byte("0x00"), | ||
} | ||
sendErr := client.SimulateTransaction(ctx, ethClient, logger.TestSugared(t), "", msg) | ||
require.Equal(t, false, sendErr.IsOutOfCounters()) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would it make sense to rename this function to something like
CheckTxOverflow
or something like that to highlight the Zk scope?CheckTxValidity
seems a bit vague.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this change