@@ -62,56 +62,71 @@ func (h *handleError) ReceiptStatus() uint64 {
62
62
}
63
63
64
64
func (p * Protocol ) handleCreateStake (ctx context.Context , act * action.CreateStake , csm CandidateStateManager ,
65
- ) (* receiptLog , error ) {
65
+ ) (* receiptLog , * action. Log , error ) {
66
66
actionCtx := protocol .MustGetActionCtx (ctx )
67
67
blkCtx := protocol .MustGetBlockCtx (ctx )
68
68
log := newReceiptLog (p .addr .String (), HandleCreateStake , blkCtx .BlockHeight >= p .hu .FbkMigrationBlockHeight ())
69
69
70
70
staker , fetchErr := fetchCaller (ctx , csm , act .Amount ())
71
71
if fetchErr != nil {
72
- return log , fetchErr
72
+ return log , nil , fetchErr
73
73
}
74
74
75
75
// Create new bucket and bucket index
76
76
candidate := csm .GetByName (act .Candidate ())
77
77
if candidate == nil {
78
- return log , errCandNotExist
78
+ return log , nil , errCandNotExist
79
79
}
80
80
bucket := NewVoteBucket (candidate .Owner , actionCtx .Caller , act .Amount (), act .Duration (), blkCtx .BlockTimeStamp , act .AutoStake ())
81
81
bucketIdx , err := putBucketAndIndex (csm , bucket )
82
82
if err != nil {
83
- return log , err
83
+ return log , nil , err
84
84
}
85
85
log .AddTopics (byteutil .Uint64ToBytesBigEndian (bucketIdx ), candidate .Owner .Bytes ())
86
86
87
87
// update candidate
88
88
weightedVote := p .calculateVoteWeight (bucket , false )
89
89
if err := candidate .AddVote (weightedVote ); err != nil {
90
- return log , & handleError {
90
+ return log , nil , & handleError {
91
91
err : errors .Wrapf (err , "failed to add vote for candidate %s" , candidate .Owner .String ()),
92
92
failureStatus : iotextypes .ReceiptStatus_ErrInvalidBucketAmount ,
93
93
}
94
94
}
95
95
if err := csm .Upsert (candidate ); err != nil {
96
- return log , csmErrorToHandleError (candidate .Owner .String (), err )
96
+ return log , nil , csmErrorToHandleError (candidate .Owner .String (), err )
97
97
}
98
98
99
99
// update staker balance
100
100
if err := staker .SubBalance (act .Amount ()); err != nil {
101
- return log , & handleError {
101
+ return log , nil , & handleError {
102
102
err : errors .Wrapf (err , "failed to update the balance of staker %s" , actionCtx .Caller .String ()),
103
103
failureStatus : iotextypes .ReceiptStatus_ErrNotEnoughBalance ,
104
104
}
105
105
}
106
106
// put updated staker's account state to trie
107
107
if err := accountutil .StoreAccount (csm , actionCtx .Caller , staker ); err != nil {
108
- return log , errors .Wrapf (err , "failed to store account %s" , actionCtx .Caller .String ())
108
+ return log , nil , errors .Wrapf (err , "failed to store account %s" , actionCtx .Caller .String ())
109
109
}
110
110
111
111
log .AddAddress (candidate .Owner )
112
112
log .AddAddress (actionCtx .Caller )
113
113
log .SetData (byteutil .Uint64ToBytesBigEndian (bucketIdx ))
114
- return log , nil
114
+
115
+ // generate create amount log
116
+ cLog := action.Log {
117
+ Address : p .addr .String (),
118
+ Topics : action.Topics {
119
+ action .BucketCreateAmount ,
120
+ hash .BytesToHash256 (actionCtx .Caller .Bytes ()),
121
+ hash .BytesToHash256 (p .addr .Bytes ()),
122
+ hash .BytesToHash256 (byteutil .Uint64ToBytesBigEndian (bucket .Index )),
123
+ },
124
+ Data : act .Amount ().Bytes (),
125
+ BlockHeight : blkCtx .BlockHeight ,
126
+ ActionHash : actionCtx .ActionHash ,
127
+ Index : 1 ,
128
+ }
129
+ return log , & cLog , nil
115
130
}
116
131
117
132
func (p * Protocol ) handleUnstake (ctx context.Context , act * action.Unstake , csm CandidateStateManager ,
@@ -239,8 +254,9 @@ func (p *Protocol) handleWithdrawStake(ctx context.Context, act *action.Withdraw
239
254
Address : p .addr .String (),
240
255
Topics : action.Topics {
241
256
action .BucketWithdrawAmount ,
242
- hash .BytesToHash256 (byteutil . Uint64ToBytesBigEndian ( bucket . Index )),
257
+ hash .BytesToHash256 (p . addr . Bytes ( )),
243
258
hash .BytesToHash256 (actionCtx .Caller .Bytes ()),
259
+ hash .BytesToHash256 (byteutil .Uint64ToBytesBigEndian (bucket .Index )),
244
260
},
245
261
Data : bucket .StakedAmount .Bytes (),
246
262
BlockHeight : blkCtx .BlockHeight ,
@@ -391,79 +407,93 @@ func (p *Protocol) handleConsignmentTransfer(
391
407
}
392
408
393
409
func (p * Protocol ) handleDepositToStake (ctx context.Context , act * action.DepositToStake , csm CandidateStateManager ,
394
- ) (* receiptLog , error ) {
410
+ ) (* receiptLog , * action. Log , error ) {
395
411
actionCtx := protocol .MustGetActionCtx (ctx )
396
412
blkCtx := protocol .MustGetBlockCtx (ctx )
397
413
log := newReceiptLog (p .addr .String (), HandleDepositToStake , blkCtx .BlockHeight >= p .hu .FbkMigrationBlockHeight ())
398
414
399
415
depositor , fetchErr := fetchCaller (ctx , csm , act .Amount ())
400
416
if fetchErr != nil {
401
- return log , fetchErr
417
+ return log , nil , fetchErr
402
418
}
403
419
404
420
bucket , fetchErr := p .fetchBucket (csm , actionCtx .Caller , act .BucketIndex (), false , true )
405
421
if fetchErr != nil {
406
- return log , fetchErr
422
+ return log , nil , fetchErr
407
423
}
408
424
log .AddTopics (byteutil .Uint64ToBytesBigEndian (bucket .Index ), bucket .Owner .Bytes (), bucket .Candidate .Bytes ())
409
425
if ! bucket .AutoStake {
410
- return log , & handleError {
426
+ return log , nil , & handleError {
411
427
err : errors .New ("deposit is only allowed on auto-stake bucket" ),
412
428
failureStatus : iotextypes .ReceiptStatus_ErrInvalidBucketType ,
413
429
}
414
430
}
415
431
candidate := csm .GetByOwner (bucket .Candidate )
416
432
if candidate == nil {
417
- return log , errCandNotExist
433
+ return log , nil , errCandNotExist
418
434
}
419
435
420
436
prevWeightedVotes := p .calculateVoteWeight (bucket , csm .ContainsSelfStakingBucket (act .BucketIndex ()))
421
437
// update bucket
422
438
bucket .StakedAmount .Add (bucket .StakedAmount , act .Amount ())
423
439
if err := updateBucket (csm , act .BucketIndex (), bucket ); err != nil {
424
- return log , errors .Wrapf (err , "failed to update bucket for voter %s" , bucket .Owner .String ())
440
+ return log , nil , errors .Wrapf (err , "failed to update bucket for voter %s" , bucket .Owner .String ())
425
441
}
426
442
427
443
// update candidate
428
444
if err := candidate .SubVote (prevWeightedVotes ); err != nil {
429
- return log , & handleError {
445
+ return log , nil , & handleError {
430
446
err : errors .Wrapf (err , "failed to subtract vote for candidate %s" , bucket .Candidate .String ()),
431
447
failureStatus : iotextypes .ReceiptStatus_ErrNotEnoughBalance ,
432
448
}
433
449
}
434
450
weightedVotes := p .calculateVoteWeight (bucket , csm .ContainsSelfStakingBucket (act .BucketIndex ()))
435
451
if err := candidate .AddVote (weightedVotes ); err != nil {
436
- return log , & handleError {
452
+ return log , nil , & handleError {
437
453
err : errors .Wrapf (err , "failed to add vote for candidate %s" , candidate .Owner .String ()),
438
454
failureStatus : iotextypes .ReceiptStatus_ErrInvalidBucketAmount ,
439
455
}
440
456
}
441
457
if csm .ContainsSelfStakingBucket (act .BucketIndex ()) {
442
458
if err := candidate .AddSelfStake (act .Amount ()); err != nil {
443
- return log , & handleError {
459
+ return log , nil , & handleError {
444
460
err : errors .Wrapf (err , "failed to add self stake for candidate %s" , candidate .Owner .String ()),
445
461
failureStatus : iotextypes .ReceiptStatus_ErrInvalidBucketAmount ,
446
462
}
447
463
}
448
464
}
449
465
if err := csm .Upsert (candidate ); err != nil {
450
- return log , csmErrorToHandleError (candidate .Owner .String (), err )
466
+ return log , nil , csmErrorToHandleError (candidate .Owner .String (), err )
451
467
}
452
468
453
469
// update depositor balance
454
470
if err := depositor .SubBalance (act .Amount ()); err != nil {
455
- return log , & handleError {
471
+ return log , nil , & handleError {
456
472
err : errors .Wrapf (err , "failed to update the balance of depositor %s" , actionCtx .Caller .String ()),
457
473
failureStatus : iotextypes .ReceiptStatus_ErrNotEnoughBalance ,
458
474
}
459
475
}
460
476
// put updated depositor's account state to trie
461
477
if err := accountutil .StoreAccount (csm , actionCtx .Caller , depositor ); err != nil {
462
- return log , errors .Wrapf (err , "failed to store account %s" , actionCtx .Caller .String ())
478
+ return log , nil , errors .Wrapf (err , "failed to store account %s" , actionCtx .Caller .String ())
463
479
}
464
-
465
480
log .AddAddress (actionCtx .Caller )
466
- return log , nil
481
+
482
+ // generate deposit amount log
483
+ dLog := action.Log {
484
+ Address : p .addr .String (),
485
+ Topics : action.Topics {
486
+ action .BucketDepositAmount ,
487
+ hash .BytesToHash256 (actionCtx .Caller .Bytes ()),
488
+ hash .BytesToHash256 (p .addr .Bytes ()),
489
+ hash .BytesToHash256 (byteutil .Uint64ToBytesBigEndian (bucket .Index )),
490
+ },
491
+ Data : act .Amount ().Bytes (),
492
+ BlockHeight : blkCtx .BlockHeight ,
493
+ ActionHash : actionCtx .ActionHash ,
494
+ Index : 1 ,
495
+ }
496
+ return log , & dLog , nil
467
497
}
468
498
469
499
func (p * Protocol ) handleRestake (ctx context.Context , act * action.Restake , csm CandidateStateManager ,
@@ -537,7 +567,7 @@ func (p *Protocol) handleRestake(ctx context.Context, act *action.Restake, csm C
537
567
}
538
568
539
569
func (p * Protocol ) handleCandidateRegister (ctx context.Context , act * action.CandidateRegister , csm CandidateStateManager ,
540
- ) (* receiptLog , error ) {
570
+ ) (* receiptLog , * action. Log , * action. Log , error ) {
541
571
actCtx := protocol .MustGetActionCtx (ctx )
542
572
blkCtx := protocol .MustGetBlockCtx (ctx )
543
573
log := newReceiptLog (p .addr .String (), HandleCandidateRegister , blkCtx .BlockHeight >= p .hu .FbkMigrationBlockHeight ())
@@ -546,7 +576,7 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
546
576
547
577
caller , fetchErr := fetchCaller (ctx , csm , new (big.Int ).Add (act .Amount (), registrationFee ))
548
578
if fetchErr != nil {
549
- return log , fetchErr
579
+ return log , nil , nil , fetchErr
550
580
}
551
581
552
582
owner := actCtx .Caller
@@ -558,22 +588,22 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
558
588
ownerExist := c != nil
559
589
// cannot collide with existing owner (with selfstake != 0)
560
590
if ownerExist && c .SelfStake .Cmp (big .NewInt (0 )) != 0 {
561
- return log , & handleError {
591
+ return log , nil , nil , & handleError {
562
592
err : ErrInvalidOwner ,
563
593
failureStatus : iotextypes .ReceiptStatus_ErrCandidateAlreadyExist ,
564
594
}
565
595
}
566
596
// cannot collide with existing name
567
597
if csm .ContainsName (act .Name ()) && (! ownerExist || act .Name () != c .Name ) {
568
- return log , & handleError {
598
+ return log , nil , nil , & handleError {
569
599
err : ErrInvalidCanName ,
570
600
failureStatus : iotextypes .ReceiptStatus_ErrCandidateConflict ,
571
601
}
572
602
}
573
603
// cannot collide with existing operator address
574
604
if csm .ContainsOperator (act .OperatorAddress ()) &&
575
605
(! ownerExist || ! address .Equal (act .OperatorAddress (), c .Operator )) {
576
- return log , & handleError {
606
+ return log , nil , nil , & handleError {
577
607
err : ErrInvalidOperator ,
578
608
failureStatus : iotextypes .ReceiptStatus_ErrCandidateConflict ,
579
609
}
@@ -582,7 +612,7 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
582
612
bucket := NewVoteBucket (owner , owner , act .Amount (), act .Duration (), blkCtx .BlockTimeStamp , act .AutoStake ())
583
613
bucketIdx , err := putBucketAndIndex (csm , bucket )
584
614
if err != nil {
585
- return log , err
615
+ return log , nil , nil , err
586
616
}
587
617
log .AddTopics (byteutil .Uint64ToBytesBigEndian (bucketIdx ), owner .Bytes ())
588
618
@@ -597,30 +627,61 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
597
627
}
598
628
599
629
if err := csm .Upsert (c ); err != nil {
600
- return log , csmErrorToHandleError (owner .String (), err )
630
+ return log , nil , nil , csmErrorToHandleError (owner .String (), err )
601
631
}
602
632
603
633
// update caller balance
604
634
if err := caller .SubBalance (act .Amount ()); err != nil {
605
- return log , & handleError {
635
+ return log , nil , nil , & handleError {
606
636
err : errors .Wrapf (err , "failed to update the balance of register %s" , actCtx .Caller .String ()),
607
637
failureStatus : iotextypes .ReceiptStatus_ErrNotEnoughBalance ,
608
638
}
609
639
}
610
640
// put updated caller's account state to trie
611
641
if err := accountutil .StoreAccount (csm , actCtx .Caller , caller ); err != nil {
612
- return log , errors .Wrapf (err , "failed to store account %s" , actCtx .Caller .String ())
642
+ return log , nil , nil , errors .Wrapf (err , "failed to store account %s" , actCtx .Caller .String ())
613
643
}
614
644
615
645
// put registrationFee to reward pool
616
646
if err := p .depositGas (ctx , csm , registrationFee ); err != nil {
617
- return log , errors .Wrap (err , "failed to deposit gas" )
647
+ return log , nil , nil , errors .Wrap (err , "failed to deposit gas" )
618
648
}
619
649
620
650
log .AddAddress (owner )
621
651
log .AddAddress (actCtx .Caller )
622
652
log .SetData (byteutil .Uint64ToBytesBigEndian (bucketIdx ))
623
- return log , nil
653
+
654
+ // generate self-stake log
655
+ cLog := action.Log {
656
+ Address : p .addr .String (),
657
+ Topics : action.Topics {
658
+ action .CandidateSelfStake ,
659
+ hash .BytesToHash256 (actCtx .Caller .Bytes ()),
660
+ hash .BytesToHash256 (p .addr .Bytes ()),
661
+ hash .BytesToHash256 (byteutil .Uint64ToBytesBigEndian (bucket .Index )),
662
+ },
663
+ Data : act .Amount ().Bytes (),
664
+ BlockHeight : blkCtx .BlockHeight ,
665
+ ActionHash : actCtx .ActionHash ,
666
+ Index : 1 ,
667
+ }
668
+
669
+ // generate candidate register log
670
+ rewardingAddr := hash .Hash160b ([]byte (action .RewardingProtocolID ))
671
+ rLog := action.Log {
672
+ Address : p .addr .String (),
673
+ Topics : action.Topics {
674
+ action .CandidateRegistrationFee ,
675
+ hash .BytesToHash256 (actCtx .Caller .Bytes ()),
676
+ hash .BytesToHash256 (rewardingAddr [:]),
677
+ hash .BytesToHash256 (byteutil .Uint64ToBytesBigEndian (bucket .Index )),
678
+ },
679
+ Data : registrationFee .Bytes (),
680
+ BlockHeight : blkCtx .BlockHeight ,
681
+ ActionHash : actCtx .ActionHash ,
682
+ Index : 2 ,
683
+ }
684
+ return log , & cLog , & rLog , nil
624
685
}
625
686
626
687
func (p * Protocol ) handleCandidateUpdate (ctx context.Context , act * action.CandidateUpdate , csm CandidateStateManager ,
@@ -791,8 +852,7 @@ func BucketIndexFromReceiptLog(log *iotextypes.Log) (uint64, bool) {
791
852
case hash .BytesToHash256 ([]byte (HandleCreateStake )), hash .BytesToHash256 ([]byte (HandleUnstake )),
792
853
hash .BytesToHash256 ([]byte (HandleWithdrawStake )), hash .BytesToHash256 ([]byte (HandleChangeCandidate )),
793
854
hash .BytesToHash256 ([]byte (HandleTransferStake )), hash .BytesToHash256 ([]byte (HandleDepositToStake )),
794
- hash .BytesToHash256 ([]byte (HandleRestake )), hash .BytesToHash256 ([]byte (HandleCandidateRegister )),
795
- action .BucketWithdrawAmount :
855
+ hash .BytesToHash256 ([]byte (HandleRestake )), hash .BytesToHash256 ([]byte (HandleCandidateRegister )):
796
856
return byteutil .BytesToUint64BigEndian (log .Topics [1 ][24 :]), true
797
857
default :
798
858
return 0 , false
0 commit comments