-
Notifications
You must be signed in to change notification settings - Fork 328
/
grantreward.go
137 lines (118 loc) · 3.48 KB
/
grantreward.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
// Copyright (c) 2019 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 (
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/v2/pkg/util/byteutil"
)
var (
_grantRewardMethod abi.Method
_ EthCompatibleAction = (*GrantReward)(nil)
)
const (
// BlockReward indicates that the action is to grant block reward
BlockReward = iota
// EpochReward indicates that the action is to grant epoch reward
EpochReward
_grantrewardInterfaceABI = `[
{
"inputs": [
{
"internalType": "int8",
"name": "rewardType",
"type": "int8"
},
{
"internalType": "uint64",
"name": "height",
"type": "uint64"
}
],
"name": "grantReward",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
func init() {
grantRewardInterface, err := abi.JSON(strings.NewReader(_grantrewardInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_grantRewardMethod, ok = grantRewardInterface.Methods["grantReward"]
if !ok {
panic("fail to load the method")
}
}
// GrantReward is the action to grant either block or epoch reward
type GrantReward struct {
reward_common
rewardType int
height uint64
}
func NewGrantReward(rewardType int, height uint64) *GrantReward {
return &GrantReward{
rewardType: rewardType,
height: height,
}
}
// RewardType returns the grant reward type
func (g *GrantReward) RewardType() int { return g.rewardType }
// Height returns the block height to grant reward
func (g *GrantReward) Height() uint64 { return g.height }
// Serialize returns a raw byte stream of a grant reward action
func (g *GrantReward) Serialize() []byte {
return byteutil.Must(proto.Marshal(g.Proto()))
}
func (act *GrantReward) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_GrantReward{GrantReward: act.Proto()}
}
// Proto converts a grant reward action struct to a grant reward action protobuf
func (g *GrantReward) Proto() *iotextypes.GrantReward {
gProto := iotextypes.GrantReward{
Height: g.height,
}
switch g.rewardType {
case BlockReward:
gProto.Type = iotextypes.RewardType_BlockReward
case EpochReward:
gProto.Type = iotextypes.RewardType_EpochReward
}
return &gProto
}
// LoadProto converts a grant reward action protobuf to a grant reward action struct
func (g *GrantReward) LoadProto(gProto *iotextypes.GrantReward) error {
*g = GrantReward{
height: gProto.Height,
}
switch gProto.Type {
case iotextypes.RewardType_BlockReward:
g.rewardType = BlockReward
case iotextypes.RewardType_EpochReward:
g.rewardType = EpochReward
}
return nil
}
// IntrinsicGas returns the intrinsic gas of a grant reward action, which is 0
func (*GrantReward) IntrinsicGas() (uint64, error) {
return 0, nil
}
func (*GrantReward) SanityCheck() error { return nil }
// EthData returns the ABI-encoded data for converting to eth tx
func (g *GrantReward) EthData() ([]byte, error) {
data, err := _grantRewardMethod.Inputs.Pack(
int8(g.rewardType),
g.height,
)
if err != nil {
return nil, err
}
return append(_grantRewardMethod.ID, data...), nil
}