-
Notifications
You must be signed in to change notification settings - Fork 328
/
depositreward_test.go
106 lines (89 loc) · 3.59 KB
/
depositreward_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
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package action
import (
"encoding/hex"
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
var (
_defaultGasPrice = big.NewInt(1000000000000)
_errNegativeNumberMsg = "negative value"
)
func TestDepositRewardSerialize(t *testing.T) {
r := require.New(t)
t.Run("proto", func(t *testing.T) {
rp := NewDepositToRewardingFund(big.NewInt(100), []byte{1})
data := rp.Serialize()
r.EqualValues("0a03313030120101", hex.EncodeToString(data))
rp2 := DepositToRewardingFund{}
r.NoError(rp2.LoadProto(rp.Proto()))
r.Equal(rp.Amount(), rp2.Amount())
r.Equal(rp.Data(), rp2.Data())
})
t.Run("intrinsic gas", func(t *testing.T) {
rp := &DepositToRewardingFund{}
gas, err := rp.IntrinsicGas()
r.NoError(err)
r.EqualValues(10000, gas)
rp.amount = big.NewInt(100000000)
gas, err = rp.IntrinsicGas()
r.NoError(err)
r.EqualValues(10000, gas)
rp.data = []byte{1}
gas, err = rp.IntrinsicGas()
r.NoError(err)
r.EqualValues(10100, gas)
})
t.Run("sanity check", func(t *testing.T) {
rp := &DepositToRewardingFund{amount: big.NewInt(1)}
err := rp.SanityCheck()
r.NoError(err)
rp.amount = big.NewInt(-1)
err = rp.SanityCheck()
r.NotNil(err)
r.EqualValues(_errNegativeNumberMsg, err.Error())
})
t.Run("cost", func(t *testing.T) {
rp := &DepositToRewardingFund{amount: big.NewInt(100)}
elp := (&EnvelopeBuilder{}).SetGasPrice(_defaultGasPrice).
SetAction(rp).Build()
cost, err := elp.Cost()
r.NoError(err)
r.EqualValues("10000000000000100", cost.String())
rp.data = []byte{1}
cost, err = elp.Cost()
r.NoError(err)
r.EqualValues("10100000000000100", cost.String())
})
}
func TestDepositRewardEncodeABIBinary(t *testing.T) {
r := require.New(t)
rp := &DepositToRewardingFund{}
rp.amount = big.NewInt(101)
data, err := rp.EthData()
r.NoError(err)
r.EqualValues(
"27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
hex.EncodeToString(data),
)
rp.data = []byte{1, 2, 3}
data, err = rp.EthData()
r.NoError(err)
r.EqualValues(
"27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
hex.EncodeToString(data),
)
}
func TestNewRewardingDepositFromABIBinary(t *testing.T) {
r := require.New(t)
data, _ := hex.DecodeString("27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003")
rp, err := NewDepositToRewardingFundFromABIBinary(data)
r.NoError(err)
r.IsType(&DepositToRewardingFund{}, rp)
r.EqualValues("101", rp.Amount().String())
r.EqualValues([]byte{1, 2, 3}, rp.Data())
}