Skip to content

Commit

Permalink
[blockindex] remove DeleteTipBlock() (#4495)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Dec 5, 2024
1 parent 7840b13 commit 1f852bc
Show file tree
Hide file tree
Showing 13 changed files with 7 additions and 213 deletions.
1 change: 0 additions & 1 deletion blockchain/blockdao/blockindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type (
Stop(ctx context.Context) error
Height() (uint64, error)
PutBlock(context.Context, *block.Block) error
DeleteTipBlock(context.Context, *block.Block) error
}

// BlockIndexerWithStart defines an interface to accept block to build index from a start height
Expand Down
5 changes: 0 additions & 5 deletions blockindex/contractstaking/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ func (s *Indexer) PutBlock(ctx context.Context, blk *block.Block) error {
return s.commit(handler, blk.Height())
}

// DeleteTipBlock deletes the tip block from indexer
func (s *Indexer) DeleteTipBlock(context.Context, *block.Block) error {
return errors.New("not implemented")
}

func (s *Indexer) commit(handler *contractStakingEventHandler, height uint64) error {
batch, delta := handler.Result()
// update cache
Expand Down
71 changes: 7 additions & 64 deletions blockindex/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type (
Stop(context.Context) error
PutBlock(context.Context, *block.Block) error
PutBlocks(context.Context, []*block.Block) error
DeleteTipBlock(context.Context, *block.Block) error
Height() (uint64, error)
GetBlockHash(height uint64) (hash.Hash256, error)
GetBlockHeight(hash hash.Hash256) (uint64, error)
Expand Down Expand Up @@ -143,49 +142,6 @@ func (x *blockIndexer) PutBlock(ctx context.Context, blk *block.Block) error {
return x.commit()
}

// DeleteTipBlock deletes a block's index
func (x *blockIndexer) DeleteTipBlock(ctx context.Context, blk *block.Block) error {
x.mutex.Lock()
defer x.mutex.Unlock()

// the block to be deleted must be exactly current top, otherwise counting index would not work correctly
height := blk.Height()
if height != x.tbk.Size()-1 {
return errors.Wrapf(db.ErrInvalid, "wrong block height %d, expecting %d", height, x.tbk.Size()-1)
}
// delete hash --> height
hash := blk.HashBlock()
x.batch.Delete(_blockHashToHeightNS, hash[_hashOffset:], fmt.Sprintf("failed to delete block at height %d", height))
// delete from total block index
if err := x.tbk.Revert(1); err != nil {
return err
}

// delete action index
fCtx := protocol.MustGetFeatureCtx(protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: blk.Height(),
})))
for _, selp := range blk.Actions {
actHash, err := selp.Hash()
if err != nil {
return err
}
x.batch.Delete(_actionToBlockHashNS, actHash[_hashOffset:], fmt.Sprintf("failed to delete action hash %x", actHash))
if err := x.indexAction(actHash, selp, false, fCtx.TolerateLegacyAddress); err != nil {
return err
}
}
// delete from total action index
if err := x.tac.Revert(uint64(len(blk.Actions))); err != nil {
return err
}
if err := x.kvStore.WriteBatch(x.batch); err != nil {
return err
}
x.batch.Clear()
return nil
}

// Height return the blockchain height
func (x *blockIndexer) Height() (uint64, error) {
x.mutex.RLock()
Expand Down Expand Up @@ -340,7 +296,7 @@ func (x *blockIndexer) putBlock(ctx context.Context, blk *block.Block) error {
if err := x.tac.Add(actHash[:], true); err != nil {
return err
}
if err := x.indexAction(actHash, selp, true, fCtx.TolerateLegacyAddress); err != nil {
if err := x.indexAction(actHash, selp, fCtx.TolerateLegacyAddress); err != nil {
return err
}
}
Expand Down Expand Up @@ -377,10 +333,7 @@ func (x *blockIndexer) commit() error {

// getIndexerForAddr returns the counting indexer for an address
// if batch is true, the indexer will be placed into a dirty map, to be committed later
func (x *blockIndexer) getIndexerForAddr(addr []byte, batch bool) (db.CountingIndex, error) {
if !batch {
return db.NewCountingIndexNX(x.kvStore, addr)
}
func (x *blockIndexer) getIndexerForAddr(addr []byte) (db.CountingIndex, error) {
address := hash.BytesToHash160(addr)
indexer, ok := x.dirtyAddr[address]
if !ok {
Expand All @@ -399,19 +352,14 @@ func (x *blockIndexer) getIndexerForAddr(addr []byte, batch bool) (db.CountingIn
}

// indexAction builds index for an action
func (x *blockIndexer) indexAction(actHash hash.Hash256, elp *action.SealedEnvelope, insert, tolerateLegacyAddress bool) error {
func (x *blockIndexer) indexAction(actHash hash.Hash256, elp *action.SealedEnvelope, tolerateLegacyAddress bool) error {
// add to sender's index
callerAddrBytes := elp.SrcPubkey().Hash()
sender, err := x.getIndexerForAddr(callerAddrBytes, insert)
sender, err := x.getIndexerForAddr(callerAddrBytes)
if err != nil {
return err
}
if insert {
err = sender.Add(actHash[:], insert)
} else {
err = sender.Revert(1)
}
if err != nil {
if err = sender.Add(actHash[:], true); err != nil {
return err
}

Expand All @@ -437,14 +385,9 @@ func (x *blockIndexer) indexAction(actHash hash.Hash256, elp *action.SealedEnvel
}

// add to recipient's index
recipient, err := x.getIndexerForAddr(dstAddrBytes, insert)
recipient, err := x.getIndexerForAddr(dstAddrBytes)
if err != nil {
return err
}
if insert {
err = recipient.Add(actHash[:], insert)
} else {
err = recipient.Revert(1)
}
return err
return recipient.Add(actHash[:], true)
}
48 changes: 0 additions & 48 deletions blockindex/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,54 +258,6 @@ func TestIndexer(t *testing.T) {
require.NoError(err)
require.EqualValues(len(indexTests[0].actions[i].hashes), actionCount)
}

// delete tip block one by one, verify address/action after each deletion
for i := range indexTests {
if i == 0 {
// tests[0] is the whole address/action data at block height 3
continue
}

require.NoError(indexer.DeleteTipBlock(ctx, blks[3-i]))
tipHeight, err := indexer.Height()
require.NoError(err)
require.EqualValues(uint64(3-i), tipHeight)
h, err := indexer.GetBlockHash(tipHeight)
require.NoError(err)
if i <= 2 {
require.Equal(blks[2-i].HashBlock(), h)
} else {
require.Equal(hash.ZeroHash256, h)
}

total, err := indexer.GetTotalActions()
require.NoError(err)
require.EqualValues(indexTests[i].total, total)
if total > 0 {
_, err = indexer.GetActionHashFromIndex(1, total)
require.Equal(db.ErrInvalid, errors.Cause(err))
actions, err := indexer.GetActionHashFromIndex(0, total)
require.NoError(err)
require.Equal(actions, indexTests[i].hashTotal)
}
for j := range indexTests[i].actions {
actionCount, err := indexer.GetActionCountByAddress(indexTests[i].actions[j].addr)
require.NoError(err)
require.EqualValues(len(indexTests[i].actions[j].hashes), actionCount)
if actionCount > 0 {
actions, err := indexer.GetActionsByAddress(indexTests[i].actions[j].addr, 0, actionCount)
require.NoError(err)
require.Equal(actions, indexTests[i].actions[j].hashes)
}
}
}

tipHeight, err := indexer.Height()
require.NoError(err)
require.EqualValues(0, tipHeight)
total, err := indexer.GetTotalActions()
require.NoError(err)
require.EqualValues(0, total)
}

t.Run("In-memory KV indexer", func(t *testing.T) {
Expand Down
10 changes: 0 additions & 10 deletions blockindex/sync_indexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ func (ig *SyncIndexers) PutBlock(ctx context.Context, blk *block.Block) error {
return nil
}

// DeleteTipBlock deletes the tip block from the indexers in the group
func (ig *SyncIndexers) DeleteTipBlock(ctx context.Context, blk *block.Block) error {
for _, indexer := range ig.indexers {
if err := indexer.DeleteTipBlock(ctx, blk); err != nil {
return err
}
}
return nil
}

// StartHeight returns the minimum start height of the indexers in the group
func (ig *SyncIndexers) StartHeight() uint64 {
return ig.minStartHeight
Expand Down
5 changes: 0 additions & 5 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ type (
// NewBlockBuilder creates block builder
NewBlockBuilder(context.Context, actpool.ActPool, func(action.Envelope) (*action.SealedEnvelope, error)) (*block.Builder, error)
PutBlock(context.Context, *block.Block) error
DeleteTipBlock(context.Context, *block.Block) error
WorkingSet(context.Context) (protocol.StateManager, error)
WorkingSetAtHeight(context.Context, uint64) (protocol.StateManager, error)
}
Expand Down Expand Up @@ -478,10 +477,6 @@ func (sf *factory) PutBlock(ctx context.Context, blk *block.Block) error {
return nil
}

func (sf *factory) DeleteTipBlock(_ context.Context, _ *block.Block) error {
return errors.Wrap(ErrNotSupported, "cannot delete tip block from factory")
}

// State returns a confirmed state in the state factory
func (sf *factory) State(s interface{}, opts ...protocol.StateOption) (uint64, error) {
sf.mutex.RLock()
Expand Down
4 changes: 0 additions & 4 deletions state/factory/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ func (sdb *stateDB) PutBlock(ctx context.Context, blk *block.Block) error {
return nil
}

func (sdb *stateDB) DeleteTipBlock(_ context.Context, _ *block.Block) error {
return errors.Wrap(ErrNotSupported, "cannot delete tip block from state db")
}

// State returns a confirmed state in the state factory
func (sdb *stateDB) State(s interface{}, opts ...protocol.StateOption) (uint64, error) {
cfg, err := processOptions(opts...)
Expand Down
6 changes: 0 additions & 6 deletions systemcontractindex/stakingindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type (
BucketsByCandidate(candidate address.Address, height uint64) ([]*VoteBucket, error)
TotalBucketCount(height uint64) (uint64, error)
PutBlock(ctx context.Context, blk *block.Block) error
DeleteTipBlock(ctx context.Context, blk *block.Block) error
}
// Indexer is the staking indexer
Indexer struct {
Expand Down Expand Up @@ -213,11 +212,6 @@ func (s *Indexer) PutBlock(ctx context.Context, blk *block.Block) error {
return s.commit(handler, blk.Height())
}

// DeleteTipBlock deletes the tip block from indexer
func (s *Indexer) DeleteTipBlock(context.Context, *block.Block) error {
return errors.New("not implemented")
}

func (s *Indexer) commit(handler *eventHandler, height uint64) error {
delta, dirty := handler.Finalize()
// update db
Expand Down
14 changes: 0 additions & 14 deletions test/mock/mock_blockdao/mock_blockindexer.go

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

14 changes: 0 additions & 14 deletions test/mock/mock_blockdao/mock_blockindexer_withstart.go

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

14 changes: 0 additions & 14 deletions test/mock/mock_blockindex/mock_blockindex.go

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

14 changes: 0 additions & 14 deletions test/mock/mock_blockindex/mock_indexer.go

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

14 changes: 0 additions & 14 deletions test/mock/mock_factory/mock_factory.go

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

0 comments on commit 1f852bc

Please sign in to comment.