github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/contractstaking/bucket_info.go (about) 1 // Copyright (c) 2023 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package contractstaking 7 8 import ( 9 "github.com/iotexproject/iotex-address/address" 10 "google.golang.org/protobuf/proto" 11 12 "github.com/iotexproject/iotex-core/blockindex/contractstaking/contractstakingpb" 13 "github.com/iotexproject/iotex-core/pkg/util/byteutil" 14 ) 15 16 type ( 17 // bucketInfo is the bucket information 18 bucketInfo struct { 19 TypeIndex uint64 20 CreatedAt uint64 21 UnlockedAt uint64 22 UnstakedAt uint64 23 Delegate address.Address // owner address of the delegate 24 Owner address.Address 25 } 26 ) 27 28 // Serialize serializes the bucket info 29 func (bi *bucketInfo) Serialize() []byte { 30 return byteutil.Must(proto.Marshal(bi.toProto())) 31 } 32 33 // Deserialize deserializes the bucket info 34 func (bi *bucketInfo) Deserialize(b []byte) error { 35 m := contractstakingpb.BucketInfo{} 36 if err := proto.Unmarshal(b, &m); err != nil { 37 return err 38 } 39 return bi.loadProto(&m) 40 } 41 42 // clone clones the bucket info 43 func (bi *bucketInfo) clone() *bucketInfo { 44 delegate := bi.Delegate 45 if delegate != nil { 46 delegate, _ = address.FromBytes(delegate.Bytes()) 47 } 48 owner := bi.Owner 49 if owner != nil { 50 owner, _ = address.FromBytes(owner.Bytes()) 51 } 52 return &bucketInfo{ 53 TypeIndex: bi.TypeIndex, 54 CreatedAt: bi.CreatedAt, 55 UnlockedAt: bi.UnlockedAt, 56 UnstakedAt: bi.UnstakedAt, 57 Delegate: delegate, 58 Owner: owner, 59 } 60 } 61 62 func (bi *bucketInfo) toProto() *contractstakingpb.BucketInfo { 63 pb := &contractstakingpb.BucketInfo{ 64 TypeIndex: bi.TypeIndex, 65 Delegate: bi.Delegate.String(), 66 CreatedAt: bi.CreatedAt, 67 Owner: bi.Owner.String(), 68 UnlockedAt: bi.UnlockedAt, 69 UnstakedAt: bi.UnstakedAt, 70 } 71 return pb 72 } 73 74 func (bi *bucketInfo) loadProto(p *contractstakingpb.BucketInfo) error { 75 delegate, err := address.FromString(p.Delegate) 76 if err != nil { 77 return err 78 } 79 owner, err := address.FromString(p.Owner) 80 if err != nil { 81 return err 82 } 83 bi.TypeIndex = p.TypeIndex 84 bi.CreatedAt = p.CreatedAt 85 bi.UnlockedAt = p.UnlockedAt 86 bi.UnstakedAt = p.UnstakedAt 87 bi.Delegate = delegate 88 bi.Owner = owner 89 return nil 90 }