Skip to content

Commit

Permalink
[index] Add TxNumber in ActionIndex (#4451)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Oct 21, 2024
1 parent 436e4d1 commit 84ee08b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 49 deletions.
3 changes: 3 additions & 0 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,9 @@ func (core *coreService) ActionByActionHash(h hash.Hash256) (*action.SealedEnvel
if err != nil {
return nil, nil, 0, errors.Wrap(ErrNotFound, err.Error())
}
if actIndex.TxNumber() > 0 {
return blk.Actions[actIndex.TxNumber()-1], blk, actIndex.TxNumber() - 1, nil
}
selp, index, err := blk.ActionByHash(h)
if err != nil {
return nil, nil, 0, errors.Wrap(ErrNotFound, err.Error())
Expand Down
11 changes: 11 additions & 0 deletions blockindex/actionindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ import (
// ActionIndex change private to public for mock Indexer
type ActionIndex struct {
blkHeight uint64
// txNumber is the %-th of action in a block
// txNumber starts from 1
// 0 means no txNumber for backward compatibility
txNumber uint32
}

// Height returns the block height of action
func (a *ActionIndex) BlockHeight() uint64 {
return a.blkHeight
}

// TxNumber returns the transaction number of action
func (a *ActionIndex) TxNumber() uint32 {
return a.txNumber
}

// Serialize into byte stream
func (a *ActionIndex) Serialize() []byte {
return byteutil.Must(proto.Marshal(a.toProto()))
Expand All @@ -41,6 +50,7 @@ func (a *ActionIndex) Deserialize(buf []byte) error {
func (a *ActionIndex) toProto() *indexpb.ActionIndex {
return &indexpb.ActionIndex{
BlkHeight: a.blkHeight,
TxNumber: a.txNumber,
}
}

Expand All @@ -50,5 +60,6 @@ func (a *ActionIndex) fromProto(pbIndex *indexpb.ActionIndex) error {
return errors.New("empty protobuf")
}
a.blkHeight = pbIndex.BlkHeight
a.txNumber = pbIndex.TxNumber
return nil
}
4 changes: 2 additions & 2 deletions blockindex/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestActionIndex(t *testing.T) {
require := require.New(t)

ad := []*ActionIndex{
{1048000},
{1048001},
{1048000, 0},
{1048001, 0},
}

for i := range ad {
Expand Down
5 changes: 2 additions & 3 deletions blockindex/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,19 @@ 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{
blkHeight: blk.Height()}).Serialize()
if err := x.tac.UseBatch(x.batch); err != nil {
return err
}
// index actions in the block
fCtx := protocol.MustGetFeatureCtx(protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: blk.Height(),
})))
for _, selp := range blk.Actions {
for idx, selp := range blk.Actions {
actHash, err := selp.Hash()
if err != nil {
return err
}
ad := (&ActionIndex{blkHeight: blk.Height(), txNumber: uint32(idx + 1)}).Serialize()
x.batch.Put(_actionToBlockHashNS, actHash[_hashOffset:], ad, fmt.Sprintf("failed to put action hash %x", actHash))
// add to total account index
if err := x.tac.Add(actHash[:], true); err != nil {
Expand Down
103 changes: 59 additions & 44 deletions blockindex/indexpb/index.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions blockindex/indexpb/index.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// protoc --go_out=plugins=grpc:. *.proto
syntax = "proto3";
package indexpb;
option go_package = "github.com/iotexproject/iotex-core/blockindex/indexpb";

message BlockIndex {
uint32 numAction = 1;
Expand All @@ -16,4 +17,5 @@ message BlockIndex {

message ActionIndex {
uint64 blkHeight = 1;
uint32 txNumber = 2;
}

0 comments on commit 84ee08b

Please sign in to comment.