ethclient: move TransactionOpts to avoid import internal package; (#2736)

This commit is contained in:
galaio 2024-10-11 16:44:50 +08:00 committed by GitHub
parent 72ec06eae7
commit 3a6dbe4d85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 73 additions and 74 deletions

@ -24,7 +24,6 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
)
var (
@ -101,7 +100,7 @@ type ContractTransactor interface {
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
// SendTransactionConditional injects the conditional transaction into the pending pool for execution after verification.
SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts ethapi.TransactionOpts) error
SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error
}
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.

@ -31,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
)
@ -76,7 +75,7 @@ func (mt *mockTransactor) SendTransaction(ctx context.Context, tx *types.Transac
return nil
}
func (mt *mockTransactor) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts ethapi.TransactionOpts) error {
func (mt *mockTransactor) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error {
return nil
}

@ -1,10 +1,9 @@
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package ethapi
package types
import (
"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
)

@ -0,0 +1,43 @@
package types
import (
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
type AccountStorage struct {
StorageRoot *common.Hash
StorageSlots map[common.Hash]common.Hash
}
func (a *AccountStorage) UnmarshalJSON(data []byte) error {
var hash common.Hash
if err := json.Unmarshal(data, &hash); err == nil {
a.StorageRoot = &hash
return nil
}
return json.Unmarshal(data, &a.StorageSlots)
}
func (a AccountStorage) MarshalJSON() ([]byte, error) {
if a.StorageRoot != nil {
return json.Marshal(*a.StorageRoot)
}
return json.Marshal(a.StorageSlots)
}
type KnownAccounts map[common.Address]AccountStorage
// It is known that marshaling is broken
// https://github.com/golang/go/issues/55890
//go:generate go run github.com/fjl/gencodec -type TransactionOpts -out gen_tx_opts_json.go
type TransactionOpts struct {
KnownAccounts KnownAccounts `json:"knownAccounts"`
BlockNumberMin *hexutil.Uint64 `json:"blockNumberMin,omitempty"`
BlockNumberMax *hexutil.Uint64 `json:"blockNumberMax,omitempty"`
TimestampMin *hexutil.Uint64 `json:"timestampMin,omitempty"`
TimestampMax *hexutil.Uint64 `json:"timestampMax,omitempty"`
}

@ -1,4 +1,4 @@
package ethapi_test
package types
import (
"encoding/json"
@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/internal/ethapi"
)
func ptr(hash common.Hash) *common.Hash {
@ -23,15 +22,15 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
name string
input string
mustFail bool
expected ethapi.TransactionOpts
expected TransactionOpts
}{
{
"StateRoot",
`{"knownAccounts":{"0x6b3A8798E5Fb9fC5603F3aB5eA2e8136694e55d0":"0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563"}}`,
false,
ethapi.TransactionOpts{
KnownAccounts: map[common.Address]ethapi.AccountStorage{
common.HexToAddress("0x6b3A8798E5Fb9fC5603F3aB5eA2e8136694e55d0"): ethapi.AccountStorage{
TransactionOpts{
KnownAccounts: map[common.Address]AccountStorage{
common.HexToAddress("0x6b3A8798E5Fb9fC5603F3aB5eA2e8136694e55d0"): AccountStorage{
StorageRoot: ptr(common.HexToHash("0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")),
},
},
@ -41,9 +40,9 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"StorageSlots",
`{"knownAccounts":{"0x6b3A8798E5Fb9fC5603F3aB5eA2e8136694e55d0":{"0xc65a7bb8d6351c1cf70c95a316cc6a92839c986682d98bc35f958f4883f9d2a8":"0x0000000000000000000000000000000000000000000000000000000000000000"}}}`,
false,
ethapi.TransactionOpts{
KnownAccounts: map[common.Address]ethapi.AccountStorage{
common.HexToAddress("0x6b3A8798E5Fb9fC5603F3aB5eA2e8136694e55d0"): ethapi.AccountStorage{
TransactionOpts{
KnownAccounts: map[common.Address]AccountStorage{
common.HexToAddress("0x6b3A8798E5Fb9fC5603F3aB5eA2e8136694e55d0"): AccountStorage{
StorageRoot: nil,
StorageSlots: map[common.Hash]common.Hash{
common.HexToHash("0xc65a7bb8d6351c1cf70c95a316cc6a92839c986682d98bc35f958f4883f9d2a8"): common.HexToHash("0x"),
@ -56,15 +55,15 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"EmptyObject",
`{"knownAccounts":{}}`,
false,
ethapi.TransactionOpts{
KnownAccounts: make(map[common.Address]ethapi.AccountStorage),
TransactionOpts{
KnownAccounts: make(map[common.Address]AccountStorage),
},
},
{
"EmptyStrings",
`{"knownAccounts":{"":""}}`,
true,
ethapi.TransactionOpts{
TransactionOpts{
KnownAccounts: nil,
},
},
@ -72,7 +71,7 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"BlockNumberMin",
`{"blockNumberMin":"0x1"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
BlockNumberMin: u64Ptr(1),
},
},
@ -80,7 +79,7 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"BlockNumberMax",
`{"blockNumberMin":"0x1", "blockNumberMax":"0x2"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
BlockNumberMin: u64Ptr(1),
BlockNumberMax: u64Ptr(2),
},
@ -89,7 +88,7 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"TimestampMin",
`{"timestampMin":"0xffff"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
TimestampMin: u64Ptr(0xffff),
},
},
@ -97,7 +96,7 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"TimestampMax",
`{"timestampMax":"0xffffff"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
TimestampMax: u64Ptr(0xffffff),
},
},
@ -105,7 +104,7 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var opts ethapi.TransactionOpts
var opts TransactionOpts
err := json.Unmarshal([]byte(test.input), &opts)
if test.mustFail && err == nil {
t.Errorf("Test %s should fail", test.name)

@ -29,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rpc"
)
@ -737,7 +736,7 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
//
// If the transaction was a contract creation use the TransactionReceipt method to get the
// contract address after the transaction has been mined.
func (ec *Client) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts ethapi.TransactionOpts) error {
func (ec *Client) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error {
data, err := tx.MarshalBinary()
if err != nil {
return err

@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
@ -770,9 +769,9 @@ func sendTransactionConditional(ec *Client) error {
}
root := common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
return ec.SendTransactionConditional(context.Background(), tx, ethapi.TransactionOpts{
KnownAccounts: map[common.Address]ethapi.AccountStorage{
testAddr: ethapi.AccountStorage{
return ec.SendTransactionConditional(context.Background(), tx, types.TransactionOpts{
KnownAccounts: map[common.Address]types.AccountStorage{
testAddr: types.AccountStorage{
StorageRoot: &root,
},
},

@ -30,7 +30,6 @@ import (
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/params"
@ -39,7 +38,7 @@ import (
// TransactionConditionalSender injects the conditional transaction into the pending pool for execution after verification.
type TransactionConditionalSender interface {
SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts ethapi.TransactionOpts) error
SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error
}
// Client exposes the methods provided by the Ethereum RPC client.

@ -2314,7 +2314,7 @@ func (s *TransactionAPI) SendRawTransaction(ctx context.Context, input hexutil.B
// SendRawTransactionConditional will add the signed transaction to the transaction pool.
// The sender/bundler is responsible for signing the transaction
func (s *TransactionAPI) SendRawTransactionConditional(ctx context.Context, input hexutil.Bytes, opts TransactionOpts) (common.Hash, error) {
func (s *TransactionAPI) SendRawTransactionConditional(ctx context.Context, input hexutil.Bytes, opts types.TransactionOpts) (common.Hash, error) {
tx := new(types.Transaction)
if err := tx.UnmarshalBinary(input); err != nil {
return common.Hash{}, err
@ -2324,7 +2324,7 @@ func (s *TransactionAPI) SendRawTransactionConditional(ctx context.Context, inpu
if state == nil || err != nil {
return common.Hash{}, err
}
if err := opts.Check(header.Number.Uint64(), header.Time, state); err != nil {
if err := TxOptsCheck(opts, header.Number.Uint64(), header.Time, state); err != nil {
return common.Hash{}, err
}
return SubmitTransaction(ctx, s.b, tx)

@ -2,52 +2,15 @@ package ethapi
import (
"bytes"
"encoding/json"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
)
type AccountStorage struct {
StorageRoot *common.Hash
StorageSlots map[common.Hash]common.Hash
}
func (a *AccountStorage) UnmarshalJSON(data []byte) error {
var hash common.Hash
if err := json.Unmarshal(data, &hash); err == nil {
a.StorageRoot = &hash
return nil
}
return json.Unmarshal(data, &a.StorageSlots)
}
func (a AccountStorage) MarshalJSON() ([]byte, error) {
if a.StorageRoot != nil {
return json.Marshal(*a.StorageRoot)
}
return json.Marshal(a.StorageSlots)
}
type KnownAccounts map[common.Address]AccountStorage
// It is known that marshaling is broken
// https://github.com/golang/go/issues/55890
//go:generate go run github.com/fjl/gencodec -type TransactionOpts -out gen_tx_opts_json.go
type TransactionOpts struct {
KnownAccounts KnownAccounts `json:"knownAccounts"`
BlockNumberMin *hexutil.Uint64 `json:"blockNumberMin,omitempty"`
BlockNumberMax *hexutil.Uint64 `json:"blockNumberMax,omitempty"`
TimestampMin *hexutil.Uint64 `json:"timestampMin,omitempty"`
TimestampMax *hexutil.Uint64 `json:"timestampMax,omitempty"`
}
const MaxNumberOfEntries = 1000
func (o *TransactionOpts) Check(blockNumber uint64, timeStamp uint64, statedb *state.StateDB) error {
func TxOptsCheck(o types.TransactionOpts, blockNumber uint64, timeStamp uint64, statedb *state.StateDB) error {
if o.BlockNumberMin != nil && blockNumber < uint64(*o.BlockNumberMin) {
return errors.New("BlockNumberMin condition not met")
}
@ -71,10 +34,10 @@ func (o *TransactionOpts) Check(blockNumber uint64, timeStamp uint64, statedb *s
if counter > MaxNumberOfEntries {
return errors.New("knownAccounts too large")
}
return o.CheckStorage(statedb)
return TxOptsCheckStorage(o, statedb)
}
func (o *TransactionOpts) CheckStorage(statedb *state.StateDB) error {
func TxOptsCheckStorage(o types.TransactionOpts, statedb *state.StateDB) error {
for address, accountStorage := range o.KnownAccounts {
if accountStorage.StorageRoot != nil {
rootHash := statedb.GetRoot(address)