Skip to content

Commit

Permalink
ethclient: move TransactionOpts to avoid import internal package; (#2736
Browse files Browse the repository at this point in the history
)
  • Loading branch information
galaio authored Oct 11, 2024
1 parent 72ec06e commit 3a6dbe4
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 74 deletions.
3 changes: 1 addition & 2 deletions accounts/abi/bind/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions accounts/abi/bind/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions core/types/transaction_options.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ethapi_test
package types

import (
"encoding/json"
Expand All @@ -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 {
Expand All @@ -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")),
},
},
Expand All @@ -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"),
Expand All @@ -56,31 +55,31 @@ 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,
},
},
{
"BlockNumberMin",
`{"blockNumberMin":"0x1"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
BlockNumberMin: u64Ptr(1),
},
},
{
"BlockNumberMax",
`{"blockNumberMin":"0x1", "blockNumberMax":"0x2"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
BlockNumberMin: u64Ptr(1),
BlockNumberMax: u64Ptr(2),
},
Expand All @@ -89,23 +88,23 @@ func TestTransactionOptsJSONUnMarshalTrip(t *testing.T) {
"TimestampMin",
`{"timestampMin":"0xffff"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
TimestampMin: u64Ptr(0xffff),
},
},
{
"TimestampMax",
`{"timestampMax":"0xffffff"}`,
false,
ethapi.TransactionOpts{
TransactionOpts{
TimestampMax: u64Ptr(0xffffff),
},
},
}

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)
Expand Down
3 changes: 1 addition & 2 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
},
Expand Down
3 changes: 1 addition & 2 deletions ethclient/simulated/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down

0 comments on commit 3a6dbe4

Please sign in to comment.