github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/actionindex.go (about) 1 // Copyright (c) 2019 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 blockindex 7 8 import ( 9 "github.com/pkg/errors" 10 "google.golang.org/protobuf/proto" 11 12 "github.com/iotexproject/iotex-core/blockindex/indexpb" 13 "github.com/iotexproject/iotex-core/pkg/util/byteutil" 14 ) 15 16 type actionIndex struct { 17 blkHeight uint64 18 } 19 20 // Height returns the block height of action 21 func (a *actionIndex) BlockHeight() uint64 { 22 return a.blkHeight 23 } 24 25 // Serialize into byte stream 26 func (a *actionIndex) Serialize() []byte { 27 return byteutil.Must(proto.Marshal(a.toProto())) 28 } 29 30 // Desrialize from byte stream 31 func (a *actionIndex) Deserialize(buf []byte) error { 32 pb := &indexpb.ActionIndex{} 33 if err := proto.Unmarshal(buf, pb); err != nil { 34 return err 35 } 36 return a.fromProto(pb) 37 } 38 39 // toProto converts to protobuf 40 func (a *actionIndex) toProto() *indexpb.ActionIndex { 41 return &indexpb.ActionIndex{ 42 BlkHeight: a.blkHeight, 43 } 44 } 45 46 // fromProto converts from protobuf 47 func (a *actionIndex) fromProto(pbIndex *indexpb.ActionIndex) error { 48 if pbIndex == nil { 49 return errors.New("empty protobuf") 50 } 51 a.blkHeight = pbIndex.BlkHeight 52 return nil 53 }