/
instruction_accept_block_reward_v3.go
145 lines (122 loc) · 3.76 KB
/
instruction_accept_block_reward_v3.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
package instruction
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/incognitochain/incognito-chain/common"
)
//AcceptBlockRewardV3 store block reward for one subset of committees in shard
type AcceptBlockRewardV3 struct {
subsetID byte
shardID byte
txsFee map[common.Hash]uint64
shardBlockHeight uint64
}
func NewAcceptBlockRewardV3() *AcceptBlockRewardV3 {
return &AcceptBlockRewardV3{}
}
func NewAcceptBlockRewardV3WithValue(
subsetID, shardID byte,
txsFee map[common.Hash]uint64,
shardBlockHeight uint64,
) *AcceptBlockRewardV3 {
return &AcceptBlockRewardV3{
subsetID: subsetID,
shardID: shardID,
txsFee: txsFee,
shardBlockHeight: shardBlockHeight,
}
}
func (a *AcceptBlockRewardV3) SubsetID() byte {
return a.subsetID
}
func (a *AcceptBlockRewardV3) ShardID() byte {
return a.shardID
}
//read only function
func (a *AcceptBlockRewardV3) TxsFee() map[common.Hash]uint64 {
return a.txsFee
}
func (a *AcceptBlockRewardV3) ShardBlockHeight() uint64 {
return a.shardBlockHeight
}
func (a *AcceptBlockRewardV3) IsEmpty() bool {
return reflect.DeepEqual(a, NewAcceptBlockRewardV3())
}
func (a *AcceptBlockRewardV3) GetType() string {
return ACCEPT_BLOCK_REWARD_V3_ACTION
}
func (a *AcceptBlockRewardV3) String() []string {
acceptBlockRewardStr := []string{ACCEPT_BLOCK_REWARD_V3_ACTION}
acceptBlockRewardStr = append(acceptBlockRewardStr, strconv.Itoa(int(a.shardID)))
acceptBlockRewardStr = append(acceptBlockRewardStr, strconv.Itoa(int(a.subsetID)))
content, _ := json.Marshal(a.txsFee)
acceptBlockRewardStr = append(acceptBlockRewardStr, string(content))
acceptBlockRewardStr = append(acceptBlockRewardStr, strconv.FormatUint(a.shardBlockHeight, 10))
return acceptBlockRewardStr
}
func ValidateAndImportAcceptBlockRewardV3InstructionFromString(instruction []string) (*AcceptBlockRewardV3, error) {
if err := ValidateAcceptBlockRewardInstructionSanity(instruction); err != nil {
return nil, err
}
return ImportAcceptBlockRewardInstructionFromString(instruction)
}
func ImportAcceptBlockRewardInstructionFromString(instruction []string) (*AcceptBlockRewardV3, error) {
acceptBlockRewardIns := NewAcceptBlockRewardV3()
shardID, err := strconv.Atoi(instruction[1])
if err != nil {
return acceptBlockRewardIns, err
}
subsetID, err := strconv.Atoi(instruction[2])
if err != nil {
return acceptBlockRewardIns, err
}
acceptBlockRewardIns.shardID = byte(shardID)
acceptBlockRewardIns.subsetID = byte(subsetID)
txsFee := make(map[common.Hash]uint64)
err = json.Unmarshal([]byte(instruction[3]), &txsFee)
if err != nil {
return acceptBlockRewardIns, err
}
acceptBlockRewardIns.txsFee = txsFee
shardBlockHeight, err := strconv.ParseUint(instruction[4], 10, 64)
if err != nil {
return acceptBlockRewardIns, err
}
acceptBlockRewardIns.shardBlockHeight = shardBlockHeight
return acceptBlockRewardIns, err
}
func ValidateAcceptBlockRewardInstructionSanity(instruction []string) error {
if len(instruction) != 5 {
return fmt.Errorf("invalid length, %+v", instruction)
}
if instruction[0] != ACCEPT_BLOCK_REWARD_V3_ACTION {
return fmt.Errorf("invalid accept block reward action, %+v", instruction)
}
shardID, err := strconv.Atoi(instruction[1])
if err != nil {
return err
}
if shardID < 0 || shardID > common.MaxShardNumber {
return errors.New("shardID is out of range for byte")
}
subsetID, err := strconv.Atoi(instruction[2])
if err != nil {
return err
}
if subsetID < 0 || subsetID > 2 {
return errors.New("subsetID is out of range for byte")
}
txsFee := make(map[common.Hash]uint64)
err = json.Unmarshal([]byte(instruction[3]), &txsFee)
if err != nil {
return err
}
_, err = strconv.ParseUint(instruction[4], 10, 64)
if err != nil {
return err
}
return nil
}