diff --git a/pkg/node/node.go b/pkg/node/node.go index ddcdad37771..36fea876a8b 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -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" @@ -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" @@ -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 { @@ -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 @@ -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, diff --git a/pkg/postage/postagecontract/contract.go b/pkg/postage/postagecontract/contract.go index 234cc4afdd8..666037c39d6 100644 --- a/pkg/postage/postagecontract/contract.go +++ b/pkg/postage/postagecontract/contract.go @@ -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 } @@ -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) diff --git a/pkg/storageincentives/redistribution/redistribution.go b/pkg/storageincentives/redistribution/redistribution.go index 8b83537cf90..ce0692da943 100644 --- a/pkg/storageincentives/redistribution/redistribution.go +++ b/pkg/storageincentives/redistribution/redistribution.go @@ -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 { diff --git a/pkg/storageincentives/staking/contract.go b/pkg/storageincentives/staking/contract.go index 014b1a67adc..aa05267c197 100644 --- a/pkg/storageincentives/staking/contract.go +++ b/pkg/storageincentives/staking/contract.go @@ -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 } @@ -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, @@ -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 } diff --git a/pkg/transaction/mock/transaction.go b/pkg/transaction/mock/transaction.go index 7478daa63e6..6110dfeea1d 100644 --- a/pkg/transaction/mock/transaction.go +++ b/pkg/transaction/mock/transaction.go @@ -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 } diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 28d3912c78d..05a07fbf461 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -5,7 +5,6 @@ package transaction import ( - "bytes" "errors" "fmt" "io" @@ -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 { @@ -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) } }