Skip to content

Commit

Permalink
revert: "feat: unwrap smart contract custom abi errors (#4367)"
Browse files Browse the repository at this point in the history
This reverts commit e7f7876.
  • Loading branch information
istae committed Oct 3, 2023
1 parent e7f7876 commit 7e0a0f0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 79 deletions.
20 changes: 16 additions & 4 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"net/http"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/addressbook"
Expand Down Expand Up @@ -69,7 +71,6 @@ import (
"github.com/ethersphere/bee/pkg/topology/lightnode"
"github.com/ethersphere/bee/pkg/tracing"
"github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/bee/pkg/util/abiutil"
"github.com/ethersphere/bee/pkg/util/ioutil"
"github.com/ethersphere/bee/pkg/util/nbhdutil"
"github.com/ethersphere/bee/pkg/util/syncutil"
Expand Down Expand Up @@ -680,7 +681,10 @@ func NewBee(
return nil, errors.New("no known postage stamp addresses for this network")
}

postageStampContractABI := abiutil.MustParseABI(chainCfg.PostageStampABI)
postageStampContractABI, err := abi.JSON(strings.NewReader(chainCfg.PostageStampABI))
if err != nil {
return nil, fmt.Errorf("unable to parse postage stamp ABI: %w", err)
}

bzzTokenAddress, err := postagecontract.LookupERC20Address(ctx, transactionService, postageStampContractAddress, postageStampContractABI, chainEnabled)
if err != nil {
Expand Down Expand Up @@ -1028,7 +1032,11 @@ func NewBee(
stakingContractAddress = common.HexToAddress(o.StakingContractAddress)
}

stakingContract := staking.New(swarmAddress, overlayEthAddress, stakingContractAddress, abiutil.MustParseABI(chainCfg.StakingABI), bzzTokenAddress, transactionService, common.BytesToHash(nonce))
stakingContractABI, err := abi.JSON(strings.NewReader(chainCfg.StakingABI))
if err != nil {
return nil, fmt.Errorf("unable to parse staking ABI: %w", err)
}
stakingContract := staking.New(swarmAddress, overlayEthAddress, stakingContractAddress, stakingContractABI, bzzTokenAddress, transactionService, common.BytesToHash(nonce))

var (
pullerService *puller.Puller
Expand All @@ -1051,12 +1059,16 @@ func NewBee(
}
redistributionContractAddress = common.HexToAddress(o.RedistributionContractAddress)
}
redistributionContractABI, err := abi.JSON(strings.NewReader(chainCfg.RedistributionABI))
if err != nil {
return nil, fmt.Errorf("unable to parse redistribution ABI: %w", err)
}

isFullySynced := func() bool {
return localStore.ReserveSize() >= reserveTreshold && pullerService.SyncRate() == 0
}

redistributionContract := redistribution.New(swarmAddress, logger, transactionService, redistributionContractAddress, abiutil.MustParseABI(chainCfg.RedistributionABI))
redistributionContract := redistribution.New(swarmAddress, logger, transactionService, redistributionContractAddress, redistributionContractABI)
agent, err = storageincentives.New(
swarmAddress,
overlayEthAddress,
Expand Down
27 changes: 5 additions & 22 deletions pkg/postage/postagecontract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,25 @@ func (c *postageContract) expireLimitedBatches(ctx context.Context, count *big.I
return nil
}

func (c *postageContract) sendApproveTransaction(ctx context.Context, amount *big.Int) (receipt *types.Receipt, err error) {
func (c *postageContract) sendApproveTransaction(ctx context.Context, amount *big.Int) (*types.Receipt, error) {
callData, err := erc20ABI.Pack("approve", c.postageStampContractAddress, amount)
if err != nil {
return nil, err
}

request := &transaction.TxRequest{
txHash, err := c.transactionService.Send(ctx, &transaction.TxRequest{
To: &c.bzzTokenAddress,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 65000,
Value: big.NewInt(0),
Description: approveDescription,
}

defer func() {
err = c.transactionService.UnwrapABIError(
ctx,
request,
err,
c.postageStampContractABI.Errors,
)
}()

txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent)
}, transaction.DefaultTipBoostPercent)
if err != nil {
return nil, err
}

receipt, err = c.transactionService.WaitForReceipt(ctx, txHash)
receipt, err := c.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return nil, err
}
Expand All @@ -198,14 +187,8 @@ func (c *postageContract) sendTransaction(ctx context.Context, callData []byte,
Value: big.NewInt(0),
Description: desc,
}

defer func() {
err = c.transactionService.UnwrapABIError(
ctx,
request,
err,
c.postageStampContractABI.Errors,
)
err = c.transactionService.UnwrapRevertReason(ctx, request, err)
}()

txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent)
Expand Down
9 changes: 1 addition & 8 deletions pkg/storageincentives/redistribution/redistribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,7 @@ func (c *contract) ReserveSalt(ctx context.Context) ([]byte, error) {
}

func (c *contract) sendAndWait(ctx context.Context, request *transaction.TxRequest, boostPercent int) (txHash common.Hash, err error) {
defer func() {
err = c.txService.UnwrapABIError(
ctx,
request,
err,
c.incentivesContractABI.Errors,
)
}()
defer func() { err = c.txService.UnwrapRevertReason(ctx, request, err) }()

txHash, err = c.txService.Send(ctx, request, boostPercent)
if err != nil {
Expand Down
32 changes: 6 additions & 26 deletions pkg/storageincentives/staking/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,25 @@ func New(
}
}

func (c *contract) sendApproveTransaction(ctx context.Context, amount *big.Int) (receipt *types.Receipt, err error) {
func (c *contract) sendApproveTransaction(ctx context.Context, amount *big.Int) (*types.Receipt, error) {
callData, err := erc20ABI.Pack("approve", c.stakingContractAddress, amount)
if err != nil {
return nil, err
}

request := &transaction.TxRequest{
txHash, err := c.transactionService.Send(ctx, &transaction.TxRequest{
To: &c.bzzTokenAddress,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 65000,
Value: big.NewInt(0),
Description: approveDescription,
}

defer func() {
err = c.transactionService.UnwrapABIError(
ctx,
request,
err,
c.stakingContractABI.Errors,
)
}()

txHash, err := c.transactionService.Send(ctx, request, 0)
}, 0)
if err != nil {
return nil, err
}

receipt, err = c.transactionService.WaitForReceipt(ctx, txHash)
receipt, err := c.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return nil, err
}
Expand All @@ -118,7 +107,7 @@ func (c *contract) sendApproveTransaction(ctx context.Context, amount *big.Int)
return receipt, nil
}

func (c *contract) sendTransaction(ctx context.Context, callData []byte, desc string) (receipt *types.Receipt, err error) {
func (c *contract) sendTransaction(ctx context.Context, callData []byte, desc string) (*types.Receipt, error) {
request := &transaction.TxRequest{
To: &c.stakingContractAddress,
Data: callData,
Expand All @@ -128,21 +117,12 @@ func (c *contract) sendTransaction(ctx context.Context, callData []byte, desc st
Description: desc,
}

defer func() {
err = c.transactionService.UnwrapABIError(
ctx,
request,
err,
c.stakingContractABI.Errors,
)
}()

txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent)
if err != nil {
return nil, err
}

receipt, err = c.transactionService.WaitForReceipt(ctx, txHash)
receipt, err := c.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/transaction/mock/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (m *transactionServiceMock) TransactionFee(ctx context.Context, txHash comm
return big.NewInt(0), nil
}

func (m *transactionServiceMock) UnwrapABIError(_ context.Context, _ *transaction.TxRequest, err error, _ map[string]abi.Error) error {
func (m *transactionServiceMock) UnwrapRevertReason(_ context.Context, _ *transaction.TxRequest, err error) error {
return err
}

Expand Down
25 changes: 7 additions & 18 deletions pkg/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package transaction

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -96,9 +95,9 @@ type Service interface {
CancelTransaction(ctx context.Context, originalTxHash common.Hash) (common.Hash, error)
// TransactionFee retrieves the transaction fee
TransactionFee(ctx context.Context, txHash common.Hash) (*big.Int, error)
// UnwrapABIError tries to unwrap the ABI error if the given error is not nil.
// The original error is wrapped together with the ABI error if it exists.
UnwrapABIError(ctx context.Context, req *TxRequest, err error, abiErrors map[string]abi.Error) error
// UnwrapRevertReason tries to unwrap the revert reason if the given error is not nil.
// The original error is wrapped in case the revert reason exists.
UnwrapRevertReason(ctx context.Context, req *TxRequest, err error) error
}

type transactionService struct {
Expand Down Expand Up @@ -591,24 +590,14 @@ func (t *transactionService) TransactionFee(ctx context.Context, txHash common.H
return trx.Cost(), nil
}

func (t *transactionService) UnwrapABIError(ctx context.Context, req *TxRequest, err error, abiErrors map[string]abi.Error) error {
func (t *transactionService) UnwrapRevertReason(ctx context.Context, req *TxRequest, err error) error {
if err == nil {
return nil
}

res, cErr := t.Call(ctx, req)
if cErr != nil {
return err
}

if reason, uErr := abi.UnpackRevert(res); uErr == nil {
return fmt.Errorf("%w: %s", err, reason)
}

for _, abiError := range abiErrors {
if bytes.Equal(res[:4], abiError.ID[:4]) {
//abiError.Unpack(res[4:])
return fmt.Errorf("%w: %s", err, abiError)
if res, cErr := t.Call(ctx, req); cErr == nil {
if reason, uErr := abi.UnpackRevert(res); uErr == nil {
err = fmt.Errorf("%w: reason: %s", err, reason)
}
}

Expand Down

0 comments on commit 7e0a0f0

Please sign in to comment.