Skip to content

Commit

Permalink
feat(staking): add tx hash
Browse files Browse the repository at this point in the history
  • Loading branch information
umerm-work committed Nov 22, 2022
1 parent 6050e6a commit 9866200
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 53 deletions.
22 changes: 14 additions & 8 deletions openapi/SwarmCommon.yaml
Expand Up @@ -9,7 +9,7 @@ externalDocs:
description: Browse the documentation @ the Swarm Docs
url: "https://docs.swarm.eth"

paths: {}
paths: { }
components:
schemas:
Address:
Expand Down Expand Up @@ -407,7 +407,7 @@ components:
properties:
beeMode:
type: string
enum: [light, full, dev]
enum: [ light, full, dev ]
description: >
Gives back in what mode the Bee client has been started. The modes are mutually exclusive
* `light` - light node; does not participate in forwarding or storing chunks
Expand All @@ -423,7 +423,7 @@ components:
properties:
status:
type: string
enum: [ok, nok]
enum: [ ok, nok ]
description: >
Indicates health state of node
* `ok` - node is healthy
Expand Down Expand Up @@ -584,7 +584,13 @@ components:
type: object
properties:
stakedAmount:
$ref: "#/components/schemas/BigInt"
$ref: "#/components/schemas/BigInt"

StakeDepositResponse:
type: object
properties:
stakedAmount:
$ref: "#/components/schemas/TransactionHash"

SwarmOnlyReference:
oneOf:
Expand Down Expand Up @@ -680,9 +686,9 @@ components:
xDai:
$ref: "#/components/schemas/BigInt"
chainID:
type: integer
type: integer
contractAddress:
$ref: "#/components/schemas/EthereumAddress"
$ref: "#/components/schemas/EthereumAddress"

PendingTransactionsResponse:
type: object
Expand Down Expand Up @@ -752,7 +758,7 @@ components:
LoggerTreeNode:
type: object
additionalProperties:
$ref: "#/components/schemas/LoggerTreeData"
$ref: "#/components/schemas/LoggerTreeData"

Logger:
type: object
Expand All @@ -770,7 +776,7 @@ components:
type: object
properties:
tree:
$ref: "#/components/schemas/LoggerTreeNode"
$ref: "#/components/schemas/LoggerTreeNode"
loggers:
type: array
items:
Expand Down
2 changes: 1 addition & 1 deletion openapi/SwarmDebug.yaml
Expand Up @@ -1035,7 +1035,7 @@ paths:
- $ref: "SwarmCommon.yaml#/components/parameters/GasLimitParameter"
responses:
"200":
description: Amount successfully staked
$ref: "SwarmCommon.yaml#/components/schemas/StakeDepositResponse"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
"500":
Expand Down
17 changes: 11 additions & 6 deletions pkg/api/staking.go
Expand Up @@ -32,6 +32,9 @@ func (s *Service) stakingAccessHandler(h http.Handler) http.Handler {
type getStakeResponse struct {
StakedAmount *bigint.BigInt `json:"stakedAmount"`
}
type stakeDepositResponse struct {
TxHash string `json:"txhash"`
}

func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("post_stake_deposit").Build()
Expand All @@ -44,32 +47,34 @@ func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request)
return
}

err := s.stakingContract.DepositStake(r.Context(), paths.Amount)
txHash, err := s.stakingContract.DepositStake(r.Context(), paths.Amount)
if err != nil {
if errors.Is(err, staking.ErrInsufficientStakeAmount) {
logger.Debug("insufficient stake amount", "minimum_stake", staking.MinimumStakeAmount, "error", err)
logger.Debug("insufficient stake amount", "minimum_stake", staking.MinimumStakeAmount, "error", err, "txHash", txHash)
logger.Error(nil, "insufficient stake amount")
jsonhttp.BadRequest(w, "insufficient stake amount")
return
}
if errors.Is(err, staking.ErrNotImplemented) {
logger.Debug("not implemented", "error", err)
logger.Debug("not implemented", "error", err, "txHash", txHash)
logger.Error(nil, "not implemented")
jsonhttp.NotImplemented(w, "not implemented")
return
}
if errors.Is(err, staking.ErrInsufficientFunds) {
logger.Debug("out of funds", "error", err)
logger.Debug("out of funds", "error", err, "txHash", txHash)
logger.Error(nil, "out of funds")
jsonhttp.BadRequest(w, "out of funds")
return
}
logger.Debug("deposit failed", "error", err)
logger.Debug("deposit failed", "error", err, "txHash", txHash)
logger.Error(nil, "deposit failed")
jsonhttp.InternalServerError(w, "cannot stake")
return
}
jsonhttp.OK(w, nil)
jsonhttp.OK(w, stakeDepositResponse{
TxHash: txHash.String(),
})
}

func (s *Service) getStakedAmountHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
22 changes: 12 additions & 10 deletions pkg/api/staking_test.go
Expand Up @@ -7,6 +7,7 @@ package api_test
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/bigint"
"math/big"
"net/http"
Expand All @@ -23,6 +24,7 @@ import (
func TestDepositStake(t *testing.T) {
t.Parallel()

txHash := common.HexToHash("0x1234")
minStake := big.NewInt(100000000000000000).String()
depositStake := func(amount string) string {
return fmt.Sprintf("/stake/%s", amount)
Expand All @@ -32,8 +34,8 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) error {
return nil
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return txHash, nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
Expand All @@ -45,8 +47,8 @@ func TestDepositStake(t *testing.T) {

invalidMinStake := big.NewInt(0).String()
contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) error {
return staking.ErrInsufficientStakeAmount
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, staking.ErrInsufficientStakeAmount
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
Expand All @@ -58,8 +60,8 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) error {
return staking.ErrInsufficientFunds
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, staking.ErrInsufficientFunds
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
Expand All @@ -71,8 +73,8 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) error {
return fmt.Errorf("some error")
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, fmt.Errorf("some error")
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
Expand All @@ -84,12 +86,12 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) error {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
gasLimit := sctx.GetGasLimit(ctx)
if gasLimit != 2000000 {
t.Fatalf("want 2000000, got %d", gasLimit)
}
return nil
return txHash, nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{
Expand Down
4 changes: 2 additions & 2 deletions pkg/node/devnode.go
Expand Up @@ -370,8 +370,8 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
mockSteward := new(mockSteward.Steward)

mockStaking := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) error {
return staking.ErrNotImplemented
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, staking.ErrNotImplemented
}))

debugOpts := api.ExtraOptions{
Expand Down
29 changes: 17 additions & 12 deletions pkg/storageincentives/staking/contract.go
Expand Up @@ -35,7 +35,7 @@ var (
)

type Contract interface {
DepositStake(ctx context.Context, stakedAmount *big.Int) error
DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error)
GetStake(ctx context.Context) (*big.Int, error)
}

Expand Down Expand Up @@ -159,37 +159,42 @@ func (s *contract) getStake(ctx context.Context, overlay swarm.Address) (*big.In
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}

func (s *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) error {
func (s *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (txHash common.Hash, err error) {
prevStakedAmount, err := s.GetStake(ctx)
if err != nil {
return err
return
}

if len(prevStakedAmount.Bits()) == 0 {
if stakedAmount.Cmp(MinimumStakeAmount) == -1 {
return ErrInsufficientStakeAmount
err = ErrInsufficientStakeAmount
return
}
}

balance, err := s.getBalance(ctx)
if err != nil {
return err
return
}

if balance.Cmp(stakedAmount) < 0 {
return ErrInsufficientFunds
err = ErrInsufficientFunds
return
}

_, err = s.sendApproveTransaction(ctx, stakedAmount)
receipt, err := s.sendApproveTransaction(ctx, stakedAmount)
if receipt != nil {
txHash = receipt.TxHash
}
if err != nil {
return err
return
}

_, err = s.sendDepositStakeTransaction(ctx, s.owner, stakedAmount, s.overlayNonce)
if err != nil {
return err
receipt, err = s.sendDepositStakeTransaction(ctx, s.owner, stakedAmount, s.overlayNonce)
if receipt != nil {
txHash = receipt.TxHash
}
return nil
return
}

func (s *contract) GetStake(ctx context.Context) (*big.Int, error) {
Expand Down
22 changes: 11 additions & 11 deletions pkg/storageincentives/staking/contract_test.go
Expand Up @@ -81,7 +81,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err = contract.DepositStake(ctx, stakedAmount)
_, err = contract.DepositStake(ctx, stakedAmount)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err = contract.DepositStake(ctx, stakedAmount)
_, err = contract.DepositStake(ctx, stakedAmount)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err := contract.DepositStake(ctx, big.NewInt(0))
_, err := contract.DepositStake(ctx, big.NewInt(0))
if !errors.Is(err, staking2.ErrInsufficientStakeAmount) {
t.Fatal(fmt.Errorf("wanted %w, got %v", staking2.ErrInsufficientStakeAmount, err))
}
Expand All @@ -192,7 +192,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err := contract.DepositStake(ctx, big.NewInt(100000000000000000))
_, err := contract.DepositStake(ctx, big.NewInt(100000000000000000))
if !errors.Is(err, staking2.ErrInsufficientFunds) {
t.Fatal(fmt.Errorf("wanted %w, got %v", staking2.ErrInsufficientFunds, err))
}
Expand All @@ -217,7 +217,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err := contract.DepositStake(ctx, big.NewInt(100000000000000000))
_, err := contract.DepositStake(ctx, big.NewInt(100000000000000000))
if !errors.Is(err, staking2.ErrInsufficientFunds) {
t.Fatal(fmt.Errorf("wanted %w, got %v", staking2.ErrInsufficientStakeAmount, err))
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err := contract.DepositStake(ctx, stakedAmount)
_, err := contract.DepositStake(ctx, stakedAmount)
if err == nil {
t.Fatal("expected error")
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err = contract.DepositStake(ctx, stakedAmount)
_, err = contract.DepositStake(ctx, stakedAmount)
if err == nil {
t.Fatal("expected error")
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestDepositStake(t *testing.T) {
}
return txHashDeposited, nil
}
return common.Hash{}, errors.New("sent to wrong contract")
return txHashDeposited, errors.New("sent to wrong contract")
}),
transactionMock.WithWaitForReceiptFunc(func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
if txHash == txHashDeposited {
Expand All @@ -344,7 +344,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err = contract.DepositStake(ctx, stakedAmount)
_, err = contract.DepositStake(ctx, stakedAmount)
if !errors.Is(err, transaction.ErrTransactionReverted) {
t.Fatalf("expeted %v, got %v", transaction.ErrTransactionReverted, err)
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err = contract.DepositStake(ctx, stakedAmount)
_, err = contract.DepositStake(ctx, stakedAmount)
if err == nil {
t.Fatalf("expected error")
}
Expand All @@ -415,7 +415,7 @@ func TestDepositStake(t *testing.T) {
})),
nonce)

err := contract.DepositStake(ctx, stakedAmount)
_, err := contract.DepositStake(ctx, stakedAmount)
if err == nil {
t.Fatalf("expected error")
}
Expand Down

0 comments on commit 9866200

Please sign in to comment.