Skip to content

Commit 3ee3f92

Browse files
authored
candidate register/update unit test update (#4724)
1 parent c94f2cf commit 3ee3f92

13 files changed

+183
-140
lines changed

action/candidate_register.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,29 @@ type CandidateRegister struct {
6363
duration uint32
6464
autoStake bool
6565
payload []byte
66-
pubKey []byte // BLS public key
66+
blsPubKey []byte
6767
}
6868

6969
func init() {
7070
var ok bool
71-
_candidateRegisterMethod, ok = NativeStakingContractABI().Methods["candidateRegister"]
71+
abi := NativeStakingContractABI()
72+
_candidateRegisterMethod, ok = abi.Methods["candidateRegister"]
7273
if !ok {
7374
panic("fail to load the method")
7475
}
75-
_candidateRegisterWithBLSMethod, ok = NativeStakingContractABI().Methods["candidateRegisterWithBLS"]
76+
_candidateRegisterWithBLSMethod, ok = abi.Methods["candidateRegisterWithBLS"]
7677
if !ok {
7778
panic("fail to load the method")
7879
}
79-
_candidateRegisteredEvent, ok = NativeStakingContractABI().Events["CandidateRegistered"]
80+
_candidateRegisteredEvent, ok = abi.Events["CandidateRegistered"]
8081
if !ok {
8182
panic("fail to load the event")
8283
}
83-
_stakedEvent, ok = NativeStakingContractABI().Events["Staked"]
84+
_stakedEvent, ok = abi.Events["Staked"]
8485
if !ok {
8586
panic("fail to load the event")
8687
}
87-
_candidateActivatedEvent, ok = NativeStakingContractABI().Events["CandidateActivated"]
88+
_candidateActivatedEvent, ok = abi.Events["CandidateActivated"]
8889
if !ok {
8990
panic("fail to load the event")
9091
}
@@ -137,19 +138,21 @@ func NewCandidateRegisterWithBLS(
137138
name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr string,
138139
duration uint32,
139140
autoStake bool,
141+
blsPubKey []byte,
140142
payload []byte,
141-
pubKey []byte,
142143
) (*CandidateRegister, error) {
143144
cr, err := NewCandidateRegister(name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr, duration, autoStake, payload)
144145
if err != nil {
145146
return nil, err
146147
}
147-
_, err = crypto.BLS12381PublicKeyFromBytes(pubKey)
148+
_, err = crypto.BLS12381PublicKeyFromBytes(blsPubKey)
148149
if err != nil {
149150
return nil, errors.Wrap(err, "failed to parse BLS public key")
150151
}
151-
cr.pubKey = make([]byte, len(pubKey))
152-
copy(cr.pubKey, pubKey)
152+
cr.value = cr.amount
153+
cr.amount = nil
154+
cr.blsPubKey = make([]byte, len(blsPubKey))
155+
copy(cr.blsPubKey, blsPubKey)
153156
return cr, nil
154157
}
155158

@@ -189,12 +192,12 @@ func (cr *CandidateRegister) OwnerAddress() address.Address { return cr.ownerAdd
189192

190193
// WithBLS returns true if the candidate register action is with BLS public key
191194
func (cr *CandidateRegister) WithBLS() bool {
192-
return len(cr.pubKey) > 0
195+
return len(cr.blsPubKey) > 0
193196
}
194197

195-
// PubKey returns the BLS public key if the candidate register action is with BLS public key
196-
func (cr *CandidateRegister) PubKey() []byte {
197-
return cr.pubKey
198+
// BLSPubKey returns the BLS public key if the candidate register action is with BLS public key
199+
func (cr *CandidateRegister) BLSPubKey() []byte {
200+
return cr.blsPubKey
198201
}
199202

200203
// Serialize returns a raw byte stream of the CandidateRegister struct
@@ -229,8 +232,8 @@ func (cr *CandidateRegister) Proto() *iotextypes.CandidateRegister {
229232

230233
switch {
231234
case cr.WithBLS():
232-
act.Candidate.PubKey = make([]byte, len(cr.pubKey))
233-
copy(act.Candidate.PubKey, cr.pubKey)
235+
act.Candidate.BlsPubKey = make([]byte, len(cr.blsPubKey))
236+
copy(act.Candidate.BlsPubKey, cr.blsPubKey)
234237
if cr.value != nil {
235238
act.StakedAmount = cr.value.String()
236239
}
@@ -266,10 +269,10 @@ func (cr *CandidateRegister) LoadProto(pbAct *iotextypes.CandidateRegister) erro
266269
cr.duration = pbAct.GetStakedDuration()
267270
cr.autoStake = pbAct.GetAutoStake()
268271

269-
withBLS := len(pbAct.Candidate.GetPubKey()) > 0
272+
withBLS := len(pbAct.Candidate.GetBlsPubKey()) > 0
270273
if withBLS {
271-
cr.pubKey = make([]byte, len(pbAct.Candidate.GetPubKey()))
272-
copy(cr.pubKey, pbAct.Candidate.GetPubKey())
274+
cr.blsPubKey = make([]byte, len(pbAct.Candidate.GetBlsPubKey()))
275+
copy(cr.blsPubKey, pbAct.Candidate.GetBlsPubKey())
273276
}
274277
if len(pbAct.GetStakedAmount()) > 0 {
275278
amount, ok := new(big.Int).SetString(pbAct.GetStakedAmount(), 10)
@@ -335,15 +338,14 @@ func (cr *CandidateRegister) EthData() ([]byte, error) {
335338
common.BytesToAddress(cr.operatorAddress.Bytes()),
336339
common.BytesToAddress(cr.rewardAddress.Bytes()),
337340
common.BytesToAddress(cr.ownerAddress.Bytes()),
338-
cr.amount,
339341
cr.duration,
340342
cr.autoStake,
341-
cr.pubKey,
343+
cr.blsPubKey,
342344
cr.payload)
343345
if err != nil {
344346
return nil, err
345347
}
346-
return append(_candidateRegisterMethod.ID, data...), nil
348+
return append(_candidateRegisterWithBLSMethod.ID, data...), nil
347349
default:
348350
data, err := _candidateRegisterMethod.Inputs.Pack(
349351
cr.name,
@@ -368,13 +370,13 @@ func PackCandidateRegisteredEvent(
368370
ownerAddress address.Address,
369371
name string,
370372
rewardAddress address.Address,
371-
blsPublicKey []byte,
373+
blsPubKey []byte,
372374
) (Topics, []byte, error) {
373375
data, err := _candidateRegisteredEvent.Inputs.NonIndexed().Pack(
374376
operatorAddress.Bytes(),
375377
name,
376378
rewardAddress.Bytes(),
377-
blsPublicKey,
379+
blsPubKey,
378380
)
379381
if err != nil {
380382
return nil, nil, errors.Wrap(err, "failed to pack CandidateRegisterWithBLS event")
@@ -475,15 +477,15 @@ func NewCandidateRegisterFromABIBinary(data []byte, value *big.Int) (*CandidateR
475477
// specific fields parsing for methods
476478
if withBLS {
477479
if value != nil {
478-
cr.value.Set(value)
480+
cr.value = new(big.Int).Set(value)
479481
}
480-
if cr.pubKey, ok = paramsMap["pubKey"].([]byte); !ok {
481-
return nil, errors.Wrapf(errDecodeFailure, "invalid pubKey %+v", paramsMap["pubKey"])
482+
if cr.blsPubKey, ok = paramsMap["blsPubKey"].([]byte); !ok {
483+
return nil, errors.Wrapf(errDecodeFailure, "invalid pubKey %+v", paramsMap["blsPubKey"])
482484
}
483-
if len(cr.pubKey) == 0 {
485+
if len(cr.blsPubKey) == 0 {
484486
return nil, errors.Wrap(errDecodeFailure, "pubKey is empty")
485487
}
486-
_, err := crypto.BLS12381PublicKeyFromBytes(cr.pubKey)
488+
_, err := crypto.BLS12381PublicKeyFromBytes(cr.blsPubKey)
487489
if err != nil {
488490
return nil, errors.Wrap(err, "failed to parse BLS public key")
489491
}

action/candidate_update.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type CandidateUpdate struct {
4040
name string
4141
operatorAddress address.Address
4242
rewardAddress address.Address
43-
pubKey []byte
43+
blsPubKey []byte
4444
}
4545

4646
// CandidateUpdateOption defines the method to customize CandidateUpdate
@@ -53,8 +53,8 @@ func WithCandidateUpdatePubKey(pubKey []byte) CandidateUpdateOption {
5353
if err != nil {
5454
return errors.Wrap(err, "failed to parse BLS public key")
5555
}
56-
cu.pubKey = make([]byte, len(pubKey))
57-
copy(cu.pubKey, pubKey)
56+
cu.blsPubKey = make([]byte, len(pubKey))
57+
copy(cu.blsPubKey, pubKey)
5858
return nil
5959
}
6060
}
@@ -108,8 +108,8 @@ func NewCandidateUpdateWithBLS(name, operatorAddrStr, rewardAddrStr string, pubk
108108
if err != nil {
109109
return nil, errors.Wrap(err, "failed to parse BLS public key")
110110
}
111-
cu.pubKey = make([]byte, len(pubkey))
112-
copy(cu.pubKey, pubkey)
111+
cu.blsPubKey = make([]byte, len(pubkey))
112+
copy(cu.blsPubKey, pubkey)
113113
return cu, nil
114114
}
115115

@@ -122,14 +122,14 @@ func (cu *CandidateUpdate) OperatorAddress() address.Address { return cu.operato
122122
// RewardAddress returns candidate rewardAddress to update
123123
func (cu *CandidateUpdate) RewardAddress() address.Address { return cu.rewardAddress }
124124

125-
// PubKey returns candidate public key to update
126-
func (cu *CandidateUpdate) PubKey() []byte {
127-
return cu.pubKey
125+
// BLSPubKey returns candidate public key to update
126+
func (cu *CandidateUpdate) BLSPubKey() []byte {
127+
return cu.blsPubKey
128128
}
129129

130130
// WithBLS returns true if the candidate update action is with BLS public key
131131
func (cu *CandidateUpdate) WithBLS() bool {
132-
return len(cu.pubKey) > 0
132+
return len(cu.blsPubKey) > 0
133133
}
134134

135135
// Serialize returns a raw byte stream of the CandidateUpdate struct
@@ -155,9 +155,9 @@ func (cu *CandidateUpdate) Proto() *iotextypes.CandidateBasicInfo {
155155
act.RewardAddress = cu.rewardAddress.String()
156156
}
157157

158-
if len(cu.pubKey) > 0 {
159-
act.PubKey = make([]byte, len(cu.pubKey))
160-
copy(act.PubKey, cu.pubKey)
158+
if len(cu.blsPubKey) > 0 {
159+
act.BlsPubKey = make([]byte, len(cu.blsPubKey))
160+
copy(act.BlsPubKey, cu.blsPubKey)
161161
}
162162
return act
163163
}
@@ -185,9 +185,9 @@ func (cu *CandidateUpdate) LoadProto(pbAct *iotextypes.CandidateBasicInfo) error
185185
}
186186
cu.rewardAddress = rewardAddr
187187
}
188-
if len(pbAct.GetPubKey()) > 0 {
189-
cu.pubKey = make([]byte, len(pbAct.GetPubKey()))
190-
copy(cu.pubKey, pbAct.GetPubKey())
188+
if len(pbAct.GetBlsPubKey()) > 0 {
189+
cu.blsPubKey = make([]byte, len(pbAct.GetBlsPubKey()))
190+
copy(cu.blsPubKey, pbAct.GetBlsPubKey())
191191
}
192192
return nil
193193
}
@@ -217,7 +217,7 @@ func (cu *CandidateUpdate) EthData() ([]byte, error) {
217217
case cu.WithBLS():
218218
data, err := _candidateUpdateWithBLSMethod.Inputs.Pack(cu.name,
219219
common.BytesToAddress(cu.operatorAddress.Bytes()),
220-
common.BytesToAddress(cu.rewardAddress.Bytes()), cu.pubKey)
220+
common.BytesToAddress(cu.rewardAddress.Bytes()), cu.blsPubKey)
221221
if err != nil {
222222
return nil, err
223223
}
@@ -269,13 +269,13 @@ func NewCandidateUpdateFromABIBinary(data []byte) (*CandidateUpdate, error) {
269269
return nil, err
270270
}
271271
if withBLS {
272-
if cu.pubKey, ok = paramsMap["pubKey"].([]byte); !ok {
273-
return nil, errors.Wrapf(errDecodeFailure, "pubKey is not []byte: %v", paramsMap["pubKey"])
272+
if cu.blsPubKey, ok = paramsMap["blsPubKey"].([]byte); !ok {
273+
return nil, errors.Wrapf(errDecodeFailure, "blsPubKey is not []byte: %v", paramsMap["pubKey"])
274274
}
275-
if len(cu.pubKey) == 0 {
275+
if len(cu.blsPubKey) == 0 {
276276
return nil, errors.Wrapf(errDecodeFailure, "empty BLS public key")
277277
}
278-
_, err := crypto.BLS12381PublicKeyFromBytes(cu.pubKey)
278+
_, err := crypto.BLS12381PublicKeyFromBytes(cu.blsPubKey)
279279
if err != nil {
280280
return nil, errors.Wrap(err, "failed to parse BLS public key")
281281
}
@@ -289,13 +289,13 @@ func PackCandidateUpdatedEvent(
289289
ownerAddress address.Address,
290290
name string,
291291
rewardAddress address.Address,
292-
blsPublicKey []byte,
292+
blsPubKey []byte,
293293
) (Topics, []byte, error) {
294294
data, err := _candidateUpdateWithBLSEvent.Inputs.NonIndexed().Pack(
295295
rewardAddress.Bytes(),
296296
name,
297297
common.BytesToAddress(operatorAddress.Bytes()),
298-
blsPublicKey,
298+
blsPubKey,
299299
)
300300
if err != nil {
301301
return nil, nil, errors.Wrap(err, "failed to pack CandidateUpdateWithBLS event data")

0 commit comments

Comments
 (0)