-
Notifications
You must be signed in to change notification settings - Fork 339
/
staking_test.go
172 lines (146 loc) · 5.59 KB
/
staking_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api_test
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/bigint"
"math/big"
"net/http"
"testing"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/storageincentives/staking"
stakingContractMock "github.com/ethersphere/bee/pkg/storageincentives/staking/mock"
)
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)
}
t.Run("ok", func(t *testing.T) {
t.Parallel()
contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return txHash, nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(minStake), http.StatusOK)
})
t.Run("with invalid stake amount", func(t *testing.T) {
t.Parallel()
invalidMinStake := big.NewInt(0).String()
contract := stakingContractMock.New(
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})
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(invalidMinStake), http.StatusBadRequest,
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusBadRequest, Message: "insufficient stake amount"}))
})
t.Run("out of funds", func(t *testing.T) {
t.Parallel()
contract := stakingContractMock.New(
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})
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(minStake), http.StatusBadRequest)
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusBadRequest, Message: "out of funds"})
})
t.Run("internal error", func(t *testing.T) {
t.Parallel()
contract := stakingContractMock.New(
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})
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(minStake), http.StatusInternalServerError)
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusInternalServerError, Message: "cannot stake"})
})
t.Run("gas limit header", func(t *testing.T) {
t.Parallel()
contract := stakingContractMock.New(
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 txHash, nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{
DebugAPI: true,
StakingContract: contract,
})
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(minStake), http.StatusOK,
jsonhttptest.WithRequestHeader("Gas-Limit", "2000000"),
)
})
}
func TestGetStake(t *testing.T) {
t.Parallel()
t.Run("ok", func(t *testing.T) {
t.Parallel()
contract := stakingContractMock.New(
stakingContractMock.WithGetStake(func(ctx context.Context) (*big.Int, error) {
return big.NewInt(1), nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodGet, "/stake", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.GetStakeResponse{StakedAmount: bigint.Wrap(big.NewInt(1))}))
})
t.Run("with error", func(t *testing.T) {
t.Parallel()
contractWithError := stakingContractMock.New(
stakingContractMock.WithGetStake(func(ctx context.Context) (*big.Int, error) {
return big.NewInt(0), fmt.Errorf("get stake failed")
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contractWithError})
jsonhttptest.Request(t, ts, http.MethodGet, "/stake", http.StatusInternalServerError,
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusInternalServerError, Message: "get staked amount failed"}))
})
}
func Test_stakingDepositHandler_invalidInputs(t *testing.T) {
t.Parallel()
client, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true})
tests := []struct {
name string
amount string
want jsonhttp.StatusResponse
}{{
name: "amount - invalid value",
amount: "a",
want: jsonhttp.StatusResponse{
Code: http.StatusBadRequest,
Message: "invalid path params",
Reasons: []jsonhttp.Reason{
{
Field: "amount",
Error: "invalid value",
},
},
},
}}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
jsonhttptest.Request(t, client, http.MethodPost, "/stake/"+tc.amount, tc.want.Code,
jsonhttptest.WithExpectedJSONResponse(tc.want),
)
})
}
}