Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blockindex]: change private to public #4205

Merged
merged 5 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions blockindex/actionindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment

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
Expand All @@ -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")
}
Expand Down
17 changes: 9 additions & 8 deletions blockindex/blockindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment

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
Expand All @@ -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,
Expand All @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions blockindex/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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),
},
Expand All @@ -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)
}
Expand Down
18 changes: 9 additions & 9 deletions blockindex/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no external usage, delete it

GetActionIndex([]byte) (*ActionIndex, error)
GetTotalActions() (uint64, error)
GetActionHashFromIndex(uint64, uint64) ([][]byte, error)
GetActionCountByAddress(hash.Hash160) (uint64, error)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -218,31 +218,31 @@ 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()

v, err := x.tbk.Get(height)
if err != nil {
return nil, err
}
b := &blockIndex{}
b := &BlockIndex{}
if err := b.Deserialize(v); err != nil {
return nil, err
}
return b, nil
}

// 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()

v, err := x.kvStore.Get(_actionToBlockHashNS, h[_hashOffset:])
if err != nil {
return nil, err
}
a := &actionIndex{}
a := &ActionIndex{}
if err := a.Deserialize(v); err != nil {
return nil, err
}
Expand Down Expand Up @@ -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()}
Expand All @@ -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
Expand Down
Loading