From 9ae2229be91858f38f03c47121376ebd7f56d2f1 Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Mon, 25 Mar 2024 18:42:21 +0800 Subject: [PATCH 1/5] feat(blockindex): change private to public --- blockindex/actionindex.go | 12 ++++++------ blockindex/index_test.go | 4 ++-- blockindex/indexbuilder_test.go | 2 +- blockindex/indexer.go | 10 +++++----- blockindex/indexer_test.go | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/blockindex/actionindex.go b/blockindex/actionindex.go index 4af87c9752..1f913429e0 100644 --- a/blockindex/actionindex.go +++ b/blockindex/actionindex.go @@ -13,22 +13,22 @@ import ( "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) -type actionIndex struct { +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 +37,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/index_test.go b/blockindex/index_test.go index 1cf18fa1c9..7fd913a4f0 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) } diff --git a/blockindex/indexbuilder_test.go b/blockindex/indexbuilder_test.go index a4956edd2d..346dcea057 100644 --- a/blockindex/indexbuilder_test.go +++ b/blockindex/indexbuilder_test.go @@ -143,7 +143,7 @@ func TestIndexBuilder(t *testing.T) { } // test getNumActions/getTranferAmount - index, err := indexer.GetBlockIndex(blks[i].Height()) + index, err := indexer.(*blockIndexer).GetBlockIndex(blks[i].Height()) require.NoError(err) require.Equal(blks[i].HashBlock(), hash.BytesToHash256(index.Hash())) require.EqualValues(len(blks[i].Actions), index.NumAction()) diff --git a/blockindex/indexer.go b/blockindex/indexer.go index 7ebf7ae220..55df3396be 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) @@ -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 } @@ -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 diff --git a/blockindex/indexer_test.go b/blockindex/indexer_test.go index a973ac956e..9f533a784a 100644 --- a/blockindex/indexer_test.go +++ b/blockindex/indexer_test.go @@ -193,7 +193,7 @@ func TestIndexer(t *testing.T) { height, err := indexer.GetBlockHeight(h) require.NoError(err) require.Equal(blks[i].Height(), height) - bd, err := indexer.GetBlockIndex(blks[i].Height()) + bd, err := indexer.(*blockIndexer).GetBlockIndex(blks[i].Height()) require.NoError(err) require.Equal(h[:], bd.Hash()) require.EqualValues(len(blks[i].Actions), bd.NumAction()) From c1edb9006ab735cb445249cd6c7d519d82491d50 Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Mon, 25 Mar 2024 19:02:05 +0800 Subject: [PATCH 2/5] feat(blockindex): fix ci --- blockchain/integrity/integrity_test.go | 2 +- blockindex/blockindex.go | 16 ++++++++-------- blockindex/index_test.go | 4 ++-- blockindex/indexbuilder_test.go | 2 +- blockindex/indexer.go | 10 +++++----- blockindex/indexer_test.go | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index b92cc07481..10ce904850 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -1739,7 +1739,7 @@ func TestLoadBlockchainfromDB(t *testing.T) { verifyTxLogIndex(require, dao, blk, 10, 2) // verify genesis block index - bi, err := indexer.GetBlockIndex(0) + bi, err := indexer.(*blockindexe).GetBlockIndex(0) require.NoError(err) require.Equal(cfg.Genesis.Hash(), hash.BytesToHash256(bi.Hash())) require.EqualValues(0, bi.NumAction()) diff --git a/blockindex/blockindex.go b/blockindex/blockindex.go index 733302e132..a0601c1ab2 100644 --- a/blockindex/blockindex.go +++ b/blockindex/blockindex.go @@ -15,34 +15,34 @@ import ( "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) -type blockIndex struct { +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 +51,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 +63,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 7fd913a4f0..837ca1623a 100644 --- a/blockindex/index_test.go +++ b/blockindex/index_test.go @@ -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/indexbuilder_test.go b/blockindex/indexbuilder_test.go index 346dcea057..a4956edd2d 100644 --- a/blockindex/indexbuilder_test.go +++ b/blockindex/indexbuilder_test.go @@ -143,7 +143,7 @@ func TestIndexBuilder(t *testing.T) { } // test getNumActions/getTranferAmount - index, err := indexer.(*blockIndexer).GetBlockIndex(blks[i].Height()) + index, err := indexer.GetBlockIndex(blks[i].Height()) require.NoError(err) require.Equal(blks[i].HashBlock(), hash.BytesToHash256(index.Hash())) require.EqualValues(len(blks[i].Actions), index.NumAction()) diff --git a/blockindex/indexer.go b/blockindex/indexer.go index 55df3396be..109e32c30f 100644 --- a/blockindex/indexer.go +++ b/blockindex/indexer.go @@ -53,7 +53,7 @@ type ( Height() (uint64, error) GetBlockHash(height uint64) (hash.Hash256, error) GetBlockHeight(hash hash.Hash256) (uint64, error) - //GetBlockIndex(uint64) (*blockIndex, error) + GetBlockIndex(uint64) (*BlockIndex, error) GetActionIndex([]byte) (*ActionIndex, error) GetTotalActions() (uint64, error) GetActionHashFromIndex(uint64, uint64) ([][]byte, 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 } @@ -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()} diff --git a/blockindex/indexer_test.go b/blockindex/indexer_test.go index 9f533a784a..a973ac956e 100644 --- a/blockindex/indexer_test.go +++ b/blockindex/indexer_test.go @@ -193,7 +193,7 @@ func TestIndexer(t *testing.T) { height, err := indexer.GetBlockHeight(h) require.NoError(err) require.Equal(blks[i].Height(), height) - bd, err := indexer.(*blockIndexer).GetBlockIndex(blks[i].Height()) + bd, err := indexer.GetBlockIndex(blks[i].Height()) require.NoError(err) require.Equal(h[:], bd.Hash()) require.EqualValues(len(blks[i].Actions), bd.NumAction()) From 168bcd547738e4765e2bfc714706eafc2d62354e Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Mon, 25 Mar 2024 19:07:00 +0800 Subject: [PATCH 3/5] feat(blockindex): fix ci --- blockchain/integrity/integrity_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index 10ce904850..b92cc07481 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -1739,7 +1739,7 @@ func TestLoadBlockchainfromDB(t *testing.T) { verifyTxLogIndex(require, dao, blk, 10, 2) // verify genesis block index - bi, err := indexer.(*blockindexe).GetBlockIndex(0) + bi, err := indexer.GetBlockIndex(0) require.NoError(err) require.Equal(cfg.Genesis.Hash(), hash.BytesToHash256(bi.Hash())) require.EqualValues(0, bi.NumAction()) From 0d3c7b0f8749aead822dcd91093f39e9beaed650 Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Tue, 26 Mar 2024 10:37:25 +0800 Subject: [PATCH 4/5] feat(blockindex): add comment --- blockindex/actionindex.go | 1 + blockindex/blockindex.go | 1 + 2 files changed, 2 insertions(+) diff --git a/blockindex/actionindex.go b/blockindex/actionindex.go index 1f913429e0..4e5fe31232 100644 --- a/blockindex/actionindex.go +++ b/blockindex/actionindex.go @@ -13,6 +13,7 @@ import ( "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) +// ActionIndex change private to public for mock Indexer type ActionIndex struct { blkHeight uint64 } diff --git a/blockindex/blockindex.go b/blockindex/blockindex.go index a0601c1ab2..b8d4c565f2 100644 --- a/blockindex/blockindex.go +++ b/blockindex/blockindex.go @@ -15,6 +15,7 @@ import ( "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) +// BlockIndex change private to public for mock Indexer type BlockIndex struct { hash []byte numAction uint32 From aa4a76e386a774c090c07a940029a820f4beb305 Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Thu, 28 Mar 2024 10:55:07 +0800 Subject: [PATCH 5/5] feat(api): del redundant code --- api/coreservice.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/coreservice.go b/api/coreservice.go index d83bf4f79d..f341689ffd 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -959,7 +959,7 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei return d, h, err } -func (core *coreService) getActionsFromIndex(totalActions, start, count uint64) ([]*iotexapi.ActionInfo, error) { +func (core *coreService) getActionsFromIndex(start, count uint64) ([]*iotexapi.ActionInfo, error) { hashes, err := core.indexer.GetActionHashFromIndex(start, count) if err != nil { return nil, status.Error(codes.Unavailable, err.Error()) @@ -994,14 +994,14 @@ func (core *coreService) Actions(start uint64, count uint64) ([]*iotexapi.Action if start >= totalActions { return nil, status.Error(codes.InvalidArgument, "start exceeds the total actions in the block") } - if totalActions == uint64(0) || count == 0 { + if totalActions == uint64(0) { return []*iotexapi.ActionInfo{}, nil } if start+count > totalActions { count = totalActions - start } if core.indexer != nil { - return core.getActionsFromIndex(totalActions, start, count) + return core.getActionsFromIndex(start, count) } // Finding actions in reverse order saves time for querying most recent actions reverseStart := totalActions - (start + count)