/
grantreward.go
104 lines (86 loc) · 2.93 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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 (
"math/big"
"github.com/golang/protobuf/proto"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)
const (
// BlockReward indicates that the action is to grant block reward
BlockReward = iota
// EpochReward indicates that the action is to grant epoch reward
EpochReward
)
// GrantReward is the action to grant either block or epoch reward
type GrantReward struct {
AbstractAction
rewardType int
height uint64
}
// 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()))
}
// 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
}
// Cost returns the total cost of a grant reward action
func (*GrantReward) Cost() (*big.Int, error) {
return big.NewInt(0), nil
}
// GrantRewardBuilder is the struct to build GrantReward
type GrantRewardBuilder struct {
Builder
grantReward GrantReward
}
// SetRewardType sets the grant reward type
func (b *GrantRewardBuilder) SetRewardType(t int) *GrantRewardBuilder {
b.grantReward.rewardType = t
return b
}
// SetHeight sets the grant reward block height
func (b *GrantRewardBuilder) SetHeight(height uint64) *GrantRewardBuilder {
b.grantReward.height = height
return b
}
// Build builds a new grant reward action
func (b *GrantRewardBuilder) Build() GrantReward {
b.grantReward.AbstractAction = b.Builder.Build()
return b.grantReward
}