Commit 2fc7fa29 authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

chore: remove use of some deprecated go-ethereum apis (#2421)

parent 0633417b
......@@ -103,7 +103,14 @@ func TestDefaultSignerSignTx(t *testing.T) {
chainID := big.NewInt(10)
tx, err := signer.SignTx(types.NewTransaction(0, beneficiary, big.NewInt(0), 21000, big.NewInt(1), []byte{1}), chainID)
tx, err := signer.SignTx(types.NewTx(&types.LegacyTx{
Nonce: 0,
To: &beneficiary,
Value: big.NewInt(0),
Gas: 21000,
GasPrice: big.NewInt(1),
Data: []byte{1},
}), chainID)
if err != nil {
t.Fatal(err)
}
......
......@@ -24,7 +24,14 @@ func TestMatchesSender(t *testing.T) {
nonce := uint64(2)
trx := common.HexToAddress("0x1").Bytes()
signedTx := types.NewTransaction(nonce, recipient, value, estimatedGasLimit, suggestedGasPrice, txData)
signedTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: value,
Gas: estimatedGasLimit,
GasPrice: suggestedGasPrice,
Data: txData,
})
t.Run("fail to retrieve tx from backend", func(t *testing.T) {
txByHash := backendmock.WithTransactionByHashFunc(func(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) {
......
......@@ -142,7 +142,7 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest) (txHa
return common.Hash{}, err
}
tx, err := prepareTransaction(ctx, request, t.sender, t.backend, nonce)
tx, err := t.prepareTransaction(ctx, request, nonce)
if err != nil {
return common.Hash{}, err
}
......@@ -243,11 +243,11 @@ func (t *transactionService) StoredTransaction(txHash common.Hash) (*StoredTrans
}
// prepareTransaction creates a signable transaction based on a request.
func prepareTransaction(ctx context.Context, request *TxRequest, from common.Address, backend Backend, nonce uint64) (tx *types.Transaction, err error) {
func (t *transactionService) prepareTransaction(ctx context.Context, request *TxRequest, nonce uint64) (tx *types.Transaction, err error) {
var gasLimit uint64
if request.GasLimit == 0 {
gasLimit, err = backend.EstimateGas(ctx, ethereum.CallMsg{
From: from,
gasLimit, err = t.backend.EstimateGas(ctx, ethereum.CallMsg{
From: t.sender,
To: request.To,
Data: request.Data,
})
......@@ -263,7 +263,7 @@ func prepareTransaction(ctx context.Context, request *TxRequest, from common.Add
var gasPrice *big.Int
if request.GasPrice == nil {
gasPrice, err = backend.SuggestGasPrice(ctx)
gasPrice, err = t.backend.SuggestGasPrice(ctx)
if err != nil {
return nil, err
}
......@@ -271,24 +271,14 @@ func prepareTransaction(ctx context.Context, request *TxRequest, from common.Add
gasPrice = request.GasPrice
}
if request.To != nil {
return types.NewTransaction(
nonce,
*request.To,
request.Value,
gasLimit,
gasPrice,
request.Data,
), nil
}
return types.NewContractCreation(
nonce,
request.Value,
gasLimit,
gasPrice,
request.Data,
), nil
return types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: request.To,
Value: request.Value,
Gas: gasLimit,
GasPrice: gasPrice,
Data: request.Data,
}), nil
}
func (t *transactionService) nonceKey() string {
......@@ -382,25 +372,14 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo
return err
}
var tx *types.Transaction
if storedTransaction.To != nil {
tx = types.NewTransaction(
storedTransaction.Nonce,
*storedTransaction.To,
storedTransaction.Value,
storedTransaction.GasLimit,
storedTransaction.GasPrice,
storedTransaction.Data,
)
} else {
tx = types.NewContractCreation(
storedTransaction.Nonce,
storedTransaction.Value,
storedTransaction.GasLimit,
storedTransaction.GasPrice,
storedTransaction.Data,
)
}
tx := types.NewTx(&types.LegacyTx{
Nonce: storedTransaction.Nonce,
To: storedTransaction.To,
Value: storedTransaction.Value,
Gas: storedTransaction.GasLimit,
GasPrice: storedTransaction.GasPrice,
Data: storedTransaction.Data,
})
signedTx, err := t.signer.SignTx(tx, t.chainID)
if err != nil {
......@@ -433,14 +412,14 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa
return common.Hash{}, ErrGasPriceTooLow
}
signedTx, err := t.signer.SignTx(types.NewTransaction(
storedTransaction.Nonce,
t.sender,
big.NewInt(0),
21000,
gasPrice,
[]byte{},
), t.chainID)
signedTx, err := t.signer.SignTx(types.NewTx(&types.AccessListTx{
Nonce: storedTransaction.Nonce,
To: &t.sender,
Value: big.NewInt(0),
Gas: 21000,
GasPrice: gasPrice,
Data: []byte{},
}), t.chainID)
if err != nil {
return common.Hash{}, err
}
......
......@@ -82,7 +82,14 @@ func TestTransactionSend(t *testing.T) {
chainID := big.NewInt(5)
t.Run("send", func(t *testing.T) {
signedTx := types.NewTransaction(nonce, recipient, value, estimatedGasLimit, suggestedGasPrice, txData)
signedTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: value,
Gas: estimatedGasLimit,
GasPrice: suggestedGasPrice,
Data: txData,
})
request := &transaction.TxRequest{
To: &recipient,
Data: txData,
......@@ -193,7 +200,14 @@ func TestTransactionSend(t *testing.T) {
})
t.Run("send_no_nonce", func(t *testing.T) {
signedTx := types.NewTransaction(nonce, recipient, value, estimatedGasLimit, suggestedGasPrice, txData)
signedTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: value,
Gas: estimatedGasLimit,
GasPrice: suggestedGasPrice,
Data: txData,
})
request := &transaction.TxRequest{
To: &recipient,
Data: txData,
......@@ -256,7 +270,14 @@ func TestTransactionSend(t *testing.T) {
t.Run("send_skipped_nonce", func(t *testing.T) {
nextNonce := nonce + 5
signedTx := types.NewTransaction(nextNonce, recipient, value, estimatedGasLimit, suggestedGasPrice, txData)
signedTx := types.NewTx(&types.LegacyTx{
Nonce: nextNonce,
To: &recipient,
Value: value,
Gas: estimatedGasLimit,
GasPrice: suggestedGasPrice,
Data: txData,
})
request := &transaction.TxRequest{
To: &recipient,
Data: txData,
......@@ -319,58 +340,6 @@ func TestTransactionSend(t *testing.T) {
t.Fatalf("did not store nonce correctly. wanted %d, got %d", nextNonce+1, storedNonce)
}
})
t.Run("deploy", func(t *testing.T) {
signedTx := types.NewContractCreation(nonce, value, estimatedGasLimit, suggestedGasPrice, txData)
request := &transaction.TxRequest{
To: nil,
Data: txData,
Value: value,
}
transactionService, err := transaction.NewService(logger,
backendmock.New(
backendmock.WithSendTransactionFunc(func(ctx context.Context, tx *types.Transaction) error {
if tx != signedTx {
t.Fatal("not sending signed transaction")
}
return nil
}),
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
if call.To != nil {
t.Fatalf("estimating with recipient. wanted nil, got %x", call.To)
}
if !bytes.Equal(call.Data, txData) {
t.Fatal("estimating with wrong data")
}
return estimatedGasLimit, nil
}),
backendmock.WithSuggestGasPriceFunc(func(ctx context.Context) (*big.Int, error) {
return suggestedGasPrice, nil
}),
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
return nonce, nil
}),
),
signerMockForTransaction(signedTx, sender, chainID, t),
storemock.NewStateStore(),
chainID,
monitormock.New(),
)
if err != nil {
t.Fatal(err)
}
defer transactionService.Close()
txHash, err := transactionService.Send(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(txHash.Bytes(), signedTx.Hash().Bytes()) {
t.Fatal("returning wrong transaction hash")
}
})
}
func TestTransactionWaitForReceipt(t *testing.T) {
......@@ -444,7 +413,14 @@ func TestTransactionResend(t *testing.T) {
store := storemock.NewStateStore()
defer store.Close()
signedTx := types.NewTransaction(nonce, recipient, value, gasLimit, gasPrice, data)
signedTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: value,
Gas: gasLimit,
GasPrice: gasPrice,
Data: data,
})
err := store.Put(transaction.StoredTransactionKey(signedTx.Hash()), transaction.StoredTransaction{
Nonce: nonce,
......@@ -496,7 +472,14 @@ func TestTransactionCancel(t *testing.T) {
store := storemock.NewStateStore()
defer store.Close()
signedTx := types.NewTransaction(nonce, recipient, value, gasLimit, gasPrice, data)
signedTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: value,
Gas: gasLimit,
GasPrice: gasPrice,
Data: data,
})
err := store.Put(transaction.StoredTransactionKey(signedTx.Hash()), transaction.StoredTransaction{
Nonce: nonce,
To: &recipient,
......@@ -510,14 +493,14 @@ func TestTransactionCancel(t *testing.T) {
}
t.Run("ok", func(t *testing.T) {
cancelTx := types.NewTransaction(
nonce,
recipient,
big.NewInt(0),
21000,
new(big.Int).Add(gasPrice, big.NewInt(1)),
[]byte{},
)
cancelTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: big.NewInt(0),
Gas: 21000,
GasPrice: new(big.Int).Add(gasPrice, big.NewInt(1)),
Data: []byte{},
})
transactionService, err := transaction.NewService(logger,
backendmock.New(
......@@ -550,15 +533,14 @@ func TestTransactionCancel(t *testing.T) {
t.Run("custom gas price", func(t *testing.T) {
customGasPrice := big.NewInt(5)
cancelTx := types.NewTransaction(
nonce,
recipient,
big.NewInt(0),
21000,
customGasPrice,
[]byte{},
)
cancelTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: big.NewInt(0),
Gas: 21000,
GasPrice: customGasPrice,
Data: []byte{},
})
transactionService, err := transaction.NewService(logger,
backendmock.New(
......@@ -592,15 +574,14 @@ func TestTransactionCancel(t *testing.T) {
t.Run("too low gas price", func(t *testing.T) {
customGasPrice := big.NewInt(0)
cancelTx := types.NewTransaction(
nonce,
recipient,
big.NewInt(0),
21000,
customGasPrice,
[]byte{},
)
cancelTx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: big.NewInt(0),
Gas: 21000,
GasPrice: customGasPrice,
Data: []byte{},
})
transactionService, err := transaction.NewService(logger,
backendmock.New(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment