diff --git a/blockindex/actionindex.go b/blockindex/actionindex.go index 4af87c9752..4e5fe31232 100644 --- a/blockindex/actionindex.go +++ b/blockindex/actionindex.go @@ -13,22 +13,23 @@ import ( "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) -type actionIndex struct { +// ActionIndex change private to public for mock Indexer +type ActionIndex struct { blkHeight uint64 } // Height returns the block height of action -func (a *actionIndex) BlockHeight() uint64 { +func (a *ActionIndex) BlockHeight() uint64 { return a.blkHeight } // Serialize into byte stream -func (a *actionIndex) Serialize() []byte { +func (a *ActionIndex) Serialize() []byte { return byteutil.Must(proto.Marshal(a.toProto())) } // Desrialize from byte stream -func (a *actionIndex) Deserialize(buf []byte) error { +func (a *ActionIndex) Deserialize(buf []byte) error { pb := &indexpb.ActionIndex{} if err := proto.Unmarshal(buf, pb); err != nil { return err @@ -37,14 +38,14 @@ func (a *actionIndex) Deserialize(buf []byte) error { } // toProto converts to protobuf -func (a *actionIndex) toProto() *indexpb.ActionIndex { +func (a *ActionIndex) toProto() *indexpb.ActionIndex { return &indexpb.ActionIndex{ BlkHeight: a.blkHeight, } } // fromProto converts from protobuf -func (a *actionIndex) fromProto(pbIndex *indexpb.ActionIndex) error { +func (a *ActionIndex) fromProto(pbIndex *indexpb.ActionIndex) error { if pbIndex == nil { return errors.New("empty protobuf") } diff --git a/blockindex/blockindex.go b/blockindex/blockindex.go index 733302e132..b8d4c565f2 100644 --- a/blockindex/blockindex.go +++ b/blockindex/blockindex.go @@ -15,34 +15,35 @@ import ( "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) -type blockIndex struct { +// BlockIndex change private to public for mock Indexer +type BlockIndex struct { hash []byte numAction uint32 tsfAmount *big.Int } // Hash returns the hash -func (b *blockIndex) Hash() []byte { +func (b *BlockIndex) Hash() []byte { return b.hash } // NumAction returns number of actions -func (b *blockIndex) NumAction() uint32 { +func (b *BlockIndex) NumAction() uint32 { return b.numAction } // TsfAmount returns transfer amount -func (b *blockIndex) TsfAmount() *big.Int { +func (b *BlockIndex) TsfAmount() *big.Int { return b.tsfAmount } // Serialize into byte stream -func (b *blockIndex) Serialize() []byte { +func (b *BlockIndex) Serialize() []byte { return byteutil.Must(proto.Marshal(b.toProto())) } // Desrialize from byte stream -func (b *blockIndex) Deserialize(buf []byte) error { +func (b *BlockIndex) Deserialize(buf []byte) error { pb := &indexpb.BlockIndex{} if err := proto.Unmarshal(buf, pb); err != nil { return err @@ -51,7 +52,7 @@ func (b *blockIndex) Deserialize(buf []byte) error { } // toProto converts to protobuf -func (b *blockIndex) toProto() *indexpb.BlockIndex { +func (b *BlockIndex) toProto() *indexpb.BlockIndex { index := &indexpb.BlockIndex{ NumAction: b.numAction, Hash: b.hash, @@ -63,7 +64,7 @@ func (b *blockIndex) toProto() *indexpb.BlockIndex { } // fromProto converts from protobuf -func (b *blockIndex) fromProto(pbIndex *indexpb.BlockIndex) error { +func (b *BlockIndex) fromProto(pbIndex *indexpb.BlockIndex) error { if pbIndex == nil { return errors.New("empty protobuf") } diff --git a/blockindex/index_test.go b/blockindex/index_test.go index 1cf18fa1c9..837ca1623a 100644 --- a/blockindex/index_test.go +++ b/blockindex/index_test.go @@ -11,14 +11,14 @@ import ( func TestActionIndex(t *testing.T) { require := require.New(t) - ad := []*actionIndex{ + ad := []*ActionIndex{ {1048000}, {1048001}, } for i := range ad { s := ad[i].Serialize() - bd2 := &actionIndex{} + bd2 := &ActionIndex{} require.NoError(bd2.Deserialize(s)) require.Equal(ad[i], bd2) } @@ -28,7 +28,7 @@ func TestBlockIndex(t *testing.T) { require := require.New(t) h, _ := hex.DecodeString("d1ff0e7fe2a54600a171d3bcc9e222c656d584b3a0e7b33373e634de3f8cd010") - bd := []*blockIndex{ + bd := []*BlockIndex{ { h, 1048000, big.NewInt(1048000), }, @@ -42,7 +42,7 @@ func TestBlockIndex(t *testing.T) { for i := range bd { s := bd[i].Serialize() - bd2 := &blockIndex{} + bd2 := &BlockIndex{} require.NoError(bd2.Deserialize(s)) require.Equal(bd[i], bd2) } diff --git a/blockindex/indexer.go b/blockindex/indexer.go index 7ebf7ae220..109e32c30f 100644 --- a/blockindex/indexer.go +++ b/blockindex/indexer.go @@ -53,8 +53,8 @@ type ( Height() (uint64, error) GetBlockHash(height uint64) (hash.Hash256, error) GetBlockHeight(hash hash.Hash256) (uint64, error) - GetBlockIndex(uint64) (*blockIndex, error) - GetActionIndex([]byte) (*actionIndex, error) + GetBlockIndex(uint64) (*BlockIndex, error) + GetActionIndex([]byte) (*ActionIndex, error) GetTotalActions() (uint64, error) GetActionHashFromIndex(uint64, uint64) ([][]byte, error) GetActionCountByAddress(hash.Hash160) (uint64, error) @@ -103,7 +103,7 @@ func (x *blockIndexer) Start(ctx context.Context) error { } if x.tbk.Size() == 0 { // insert genesis block - if err = x.tbk.Add((&blockIndex{ + if err = x.tbk.Add((&BlockIndex{ x.genesisHash[:], 0, big.NewInt(0)}).Serialize(), false); err != nil { @@ -218,7 +218,7 @@ func (x *blockIndexer) GetBlockHeight(hash hash.Hash256) (uint64, error) { } // GetBlockIndex return the index of block -func (x *blockIndexer) GetBlockIndex(height uint64) (*blockIndex, error) { +func (x *blockIndexer) GetBlockIndex(height uint64) (*BlockIndex, error) { x.mutex.RLock() defer x.mutex.RUnlock() @@ -226,7 +226,7 @@ func (x *blockIndexer) GetBlockIndex(height uint64) (*blockIndex, error) { if err != nil { return nil, err } - b := &blockIndex{} + b := &BlockIndex{} if err := b.Deserialize(v); err != nil { return nil, err } @@ -234,7 +234,7 @@ func (x *blockIndexer) GetBlockIndex(height uint64) (*blockIndex, error) { } // GetActionIndex return the index of action -func (x *blockIndexer) GetActionIndex(h []byte) (*actionIndex, error) { +func (x *blockIndexer) GetActionIndex(h []byte) (*ActionIndex, error) { x.mutex.RLock() defer x.mutex.RUnlock() @@ -242,7 +242,7 @@ func (x *blockIndexer) GetActionIndex(h []byte) (*actionIndex, error) { if err != nil { return nil, err } - a := &actionIndex{} + a := &ActionIndex{} if err := a.Deserialize(v); err != nil { return nil, err } @@ -310,7 +310,7 @@ func (x *blockIndexer) putBlock(ctx context.Context, blk *block.Block) error { x.batch.Put(_blockHashToHeightNS, hash[_hashOffset:], byteutil.Uint64ToBytesBigEndian(height), "failed to put hash -> height mapping") // index height --> block hash, number of actions, and total transfer amount - bd := &blockIndex{ + bd := &BlockIndex{ hash: hash[:], numAction: uint32(len(blk.Actions)), tsfAmount: blk.CalculateTransferAmount()} @@ -322,7 +322,7 @@ func (x *blockIndexer) putBlock(ctx context.Context, blk *block.Block) error { } // store height of the block, so getReceiptByActionHash() can use height to directly pull receipts - ad := (&actionIndex{ + ad := (&ActionIndex{ blkHeight: blk.Height()}).Serialize() if err := x.tac.UseBatch(x.batch); err != nil { return err