From 7a745bd0872c2352a4e3fc306f1e5cf317f7d209 Mon Sep 17 00:00:00 2001 From: envestcc Date: Mon, 22 May 2023 17:02:56 +0800 Subject: [PATCH] address comments --- .../staking/contractstaking/bucket.go | 2 +- .../staking/contractstaking/bucket_info.go | 39 ++++++++++--------- .../staking/contractstaking/bucket_type.go | 32 +++++++-------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/action/protocol/staking/contractstaking/bucket.go b/action/protocol/staking/contractstaking/bucket.go index 75548071ae..ba7866d0da 100644 --- a/action/protocol/staking/contractstaking/bucket.go +++ b/action/protocol/staking/contractstaking/bucket.go @@ -22,5 +22,5 @@ type Bucket struct { StakeStartBlockHeight uint64 UnstakeStartBlockHeight uint64 AutoStake bool - ContractAddress string + ContractAddress string // contract address for the bucket } diff --git a/action/protocol/staking/contractstaking/bucket_info.go b/action/protocol/staking/contractstaking/bucket_info.go index ab23043c5f..8683a66880 100644 --- a/action/protocol/staking/contractstaking/bucket_info.go +++ b/action/protocol/staking/contractstaking/bucket_info.go @@ -25,18 +25,6 @@ type ( } ) -func (bi *bucketInfo) toProto() *contractstakingpb.BucketInfo { - pb := &contractstakingpb.BucketInfo{ - TypeIndex: bi.TypeIndex, - Delegate: bi.Delegate.String(), - CreatedAt: bi.CreatedAt, - Owner: bi.Owner.String(), - UnlockedAt: bi.UnlockedAt, - UnstakedAt: bi.UnstakedAt, - } - return pb -} - // Serialize serializes the bucket info func (bi *bucketInfo) Serialize() []byte { return byteutil.Must(proto.Marshal(bi.toProto())) @@ -51,19 +39,32 @@ func (bi *bucketInfo) Deserialize(b []byte) error { return bi.loadProto(&m) } +func (bi *bucketInfo) toProto() *contractstakingpb.BucketInfo { + pb := &contractstakingpb.BucketInfo{ + TypeIndex: bi.TypeIndex, + Delegate: bi.Delegate.String(), + CreatedAt: bi.CreatedAt, + Owner: bi.Owner.String(), + UnlockedAt: bi.UnlockedAt, + UnstakedAt: bi.UnstakedAt, + } + return pb +} + func (bi *bucketInfo) loadProto(p *contractstakingpb.BucketInfo) error { - var err error - bi.TypeIndex = p.TypeIndex - bi.CreatedAt = p.CreatedAt - bi.UnlockedAt = p.UnlockedAt - bi.UnstakedAt = p.UnstakedAt - bi.Delegate, err = address.FromString(p.Delegate) + delegate, err := address.FromString(p.Delegate) if err != nil { return err } - bi.Owner, err = address.FromString(p.Owner) + owner, err := address.FromString(p.Owner) if err != nil { return err } + bi.TypeIndex = p.TypeIndex + bi.CreatedAt = p.CreatedAt + bi.UnlockedAt = p.UnlockedAt + bi.UnstakedAt = p.UnstakedAt + bi.Delegate = delegate + bi.Owner = owner return nil } diff --git a/action/protocol/staking/contractstaking/bucket_type.go b/action/protocol/staking/contractstaking/bucket_type.go index f26fc5ee14..98fca73346 100644 --- a/action/protocol/staking/contractstaking/bucket_type.go +++ b/action/protocol/staking/contractstaking/bucket_type.go @@ -24,6 +24,20 @@ type ( } ) +// Serialize serializes the bucket type +func (bt *BucketType) Serialize() []byte { + return byteutil.Must(proto.Marshal(bt.toProto())) +} + +// Deserialize deserializes the bucket type +func (bt *BucketType) Deserialize(b []byte) error { + m := contractstakingpb.BucketType{} + if err := proto.Unmarshal(b, &m); err != nil { + return err + } + return bt.loadProto(&m) +} + func (bt *BucketType) toProto() *contractstakingpb.BucketType { return &contractstakingpb.BucketType{ Amount: bt.Amount.String(), @@ -33,26 +47,12 @@ func (bt *BucketType) toProto() *contractstakingpb.BucketType { } func (bt *BucketType) loadProto(p *contractstakingpb.BucketType) error { - var ok bool - bt.Amount, ok = big.NewInt(0).SetString(p.Amount, 10) + amount, ok := big.NewInt(0).SetString(p.Amount, 10) if !ok { return errors.New("failed to parse amount") } + bt.Amount = amount bt.Duration = p.Duration bt.ActivatedAt = p.ActivatedAt return nil } - -// Serialize serializes the bucket type -func (bt *BucketType) Serialize() []byte { - return byteutil.Must(proto.Marshal(bt.toProto())) -} - -// Deserialize deserializes the bucket type -func (bt *BucketType) Deserialize(b []byte) error { - m := contractstakingpb.BucketType{} - if err := proto.Unmarshal(b, &m); err != nil { - return err - } - return bt.loadProto(&m) -}