/
dposparse.go
200 lines (166 loc) · 6.18 KB
/
dposparse.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2019 DxChain, All rights reserved.
// Use of this source code is governed by an Apache
// License 2.0 that can be found in the LICENSE file
package ethapi
import (
"fmt"
"reflect"
"strings"
"github.com/DxChainNetwork/godx/accounts"
"github.com/DxChainNetwork/godx/common"
"github.com/DxChainNetwork/godx/common/unit"
"github.com/DxChainNetwork/godx/consensus/dpos"
"github.com/DxChainNetwork/godx/core/state"
"github.com/DxChainNetwork/godx/core/types"
"github.com/DxChainNetwork/godx/log"
"github.com/DxChainNetwork/godx/rlp"
)
// ParseAndValidateCandidateApplyTxArgs will parse and validate the candidate apply transaction arguments
func ParseAndValidateCandidateApplyTxArgs(to common.Address, gas uint64, fields map[string]string, stateDB *state.StateDB, account *accounts.Manager) (*PrecompiledContractTxArgs, error) {
// parse the candidateAddress field
var candidateAddress common.Address
if fromStr, ok := fields["from"]; ok {
candidateAddress = common.HexToAddress(fromStr)
} else {
candidateAddress = defaultAccount(account)
log.Info("Candidate account is automatically configured", "candidateAccount", account)
}
// validate candidateAddress
if reflect.DeepEqual(candidateAddress, common.Address{}) {
return nil, fmt.Errorf("the address used for apply candidate cannot be empty")
}
// form candidate tx data
addCandidateTxData, err := formAddCandidateTxData(fields)
if err != nil {
return nil, err
}
// validate candidate tx data
if err := dpos.CandidateTxDataValidation(stateDB, addCandidateTxData, candidateAddress); err != nil {
return nil, err
}
// candidate transaction data encoding
data, err := rlp.EncodeToBytes(&addCandidateTxData)
if err != nil {
return nil, err
}
return NewPrecompiledContractTxArgs(candidateAddress, to, data, nil, gas), nil
}
// ParseAndValidateVoteTxArgs will parse and validate the vote transaction arguments
func ParseAndValidateVoteTxArgs(to common.Address, gas uint64, fields map[string]string, stateDB *state.StateDB, account *accounts.Manager) (*PrecompiledContractTxArgs, error) {
// parse the delegator account address
var delegatorAddress common.Address
if fromStr, ok := fields["from"]; ok {
delegatorAddress = common.HexToAddress(fromStr)
} else {
delegatorAddress = defaultAccount(account)
log.Info("Vote account is automatically configured", "voteAccount", account)
}
// validate delegatorAddress
if reflect.DeepEqual(delegatorAddress, common.Address{}) {
return nil, fmt.Errorf("the address used for voting cannot be empty")
}
// form the vote tx data
voteTxData, err := formVoteTxData(fields)
if err != nil {
return nil, err
}
// voteTxData validation
if err := dpos.VoteTxDepositValidation(stateDB, delegatorAddress, voteTxData); err != nil {
return nil, err
}
// encode and return the data
data, err := rlp.EncodeToBytes(&voteTxData)
if err != nil {
return nil, err
}
return NewPrecompiledContractTxArgs(delegatorAddress, to, data, nil, gas), nil
}
// formVoteTxData will parse the fields and form vote transaction data
func formVoteTxData(fields map[string]string) (data types.VoteTxData, err error) {
// get deposit
depositStr, ok := fields["deposit"]
if !ok {
return types.VoteTxData{}, fmt.Errorf("failed to form voteTxData, vote deposit is not provided")
}
// get candidates
candidatesStr, ok := fields["candidates"]
if !ok {
return types.VoteTxData{}, fmt.Errorf("failed to form voteTxData, vote candidates is not provided")
}
// parse candidates
if data.Candidates, err = parseCandidates(candidatesStr); err != nil {
return types.VoteTxData{}, err
}
// parse deposit
if data.Deposit, err = unit.ParseCurrency(depositStr); err != nil {
return types.VoteTxData{}, err
}
return
}
// formAddCandidateTxData will parse the fields and form candidate apply transaction data
func formAddCandidateTxData(fields map[string]string) (data types.AddCandidateTxData, err error) {
// get reward ratio
rewardRatioStr, ok := fields["ratio"]
if !ok {
return types.AddCandidateTxData{}, fmt.Errorf("failed to form addCandidateTxData, candidate rewardRatio is not provided")
}
// get deposit
depositStr, ok := fields["deposit"]
if !ok {
return types.AddCandidateTxData{}, fmt.Errorf("failed to form addCandidateTxData, candidate deposit is not provided")
}
// parse reward ratio
if data.RewardRatio, err = parseRewardRatio(rewardRatioStr); err != nil {
return types.AddCandidateTxData{}, err
}
// parse deposit
if data.Deposit, err = unit.ParseCurrency(depositStr); err != nil {
return types.AddCandidateTxData{}, err
}
return
}
// parseCandidates will convert the string to a list of candidate address
func parseCandidates(candidates string) ([]common.Address, error) {
// strip all white spaces
candidates = strings.Replace(candidates, " ", "", -1)
// convert it to a list of candidates
candidatesList := strings.Split(candidates, ",")
// candidates addresses conversion
var candidateAddresses []common.Address
for _, candidate := range candidatesList {
addr := common.HexToAddress(candidate)
candidateAddresses = append(candidateAddresses, addr)
}
return candidateAddresses, nil
}
// parseRewardRatio is used to convert the ratio from string to uint64
// and to validate the parsed ratio
func parseRewardRatio(ratio string) (uint64, error) {
// convert the string to uint64
rewardRatio, err := unit.ParseUint64(ratio, 1, "")
if err != nil {
return 0, ErrParseStringToUint
}
// validate the rewardRatio and return
return rewardRatioValidation(rewardRatio)
}
// rewardRatioValidation is used to validate the reward ratio
func rewardRatioValidation(ratio uint64) (uint64, error) {
// check if the reward ratio
if ratio > dpos.RewardRatioDenominator {
return 0, ErrInvalidAwardDistributionRatio
}
return ratio, nil
}
// defaultAccount will return the first account address from the first wallet
func defaultAccount(account *accounts.Manager) common.Address {
// check if there are existing account, if so, use the first one as
// default account
if wallets := account.Wallets(); len(wallets) > 0 {
if walletAccounts := wallets[0].Accounts(); len(walletAccounts) > 0 {
return walletAccounts[0].Address
}
}
// otherwise, return empty account address
return common.Address{}
}