Skip to content

Commit

Permalink
Merge branch 'master' into sd6780
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Dec 9, 2024
2 parents 04a8f4f + 25d345f commit 9aa8662
Show file tree
Hide file tree
Showing 30 changed files with 595 additions and 462 deletions.
4 changes: 2 additions & 2 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ func TestConstantinople(t *testing.T) {
},
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
41174200,
33730920,
},
// after Vanuatu
{
action.EmptyAddress,
41174201,
33730921,
},
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
Expand Down
10 changes: 10 additions & 0 deletions action/sealedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,13 @@ func (sealed *SealedEnvelope) VerifySignature() error {
}
return nil
}

// Protected says whether the transaction is replay-protected.
func (sealed *SealedEnvelope) Protected() bool {
switch sealed.encoding {
case iotextypes.Encoding_TX_CONTAINER:
return sealed.Envelope.(*txContainer).tx.Protected()
default:
return sealed.encoding != iotextypes.Encoding_ETHEREUM_UNPROTECTED
}
}
2 changes: 1 addition & 1 deletion api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (core *coreService) SendAction(ctx context.Context, in *iotextypes.Action)
g = core.Genesis()
deployer = selp.SenderAddress()
)
if selp.Encoding() == uint32(iotextypes.Encoding_ETHEREUM_UNPROTECTED) && !g.IsDeployerWhitelisted(deployer) {
if !selp.Protected() && !g.IsDeployerWhitelisted(deployer) {
return "", status.Errorf(codes.InvalidArgument, "replay deployer %v not whitelisted", deployer.Hex())
}

Expand Down
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
2 changes: 1 addition & 1 deletion blockchain/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func defaultConfig() Genesis {
SumatraBlockHeight: 28516681,
TsunamiBlockHeight: 29275561,
UpernavikBlockHeight: 31174201,
VanuatuBlockHeight: 41174201,
VanuatuBlockHeight: 33730921,
ToBeEnabledBlockHeight: math.MaxUint64,
},
Account: Account{
Expand Down
6 changes: 3 additions & 3 deletions blockchain/genesis/heightupgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func TestNewHeightChange(t *testing.T) {
require.True(cfg.IsTsunami(uint64(29275561)))
require.False(cfg.IsUpernavik(uint64(31174200)))
require.True(cfg.IsUpernavik(uint64(31174201)))
require.False(cfg.IsVanuatu(uint64(41174200)))
require.True(cfg.IsVanuatu(uint64(41174201)))
require.False(cfg.IsVanuatu(uint64(33730920)))
require.True(cfg.IsVanuatu(uint64(33730921)))

require.Equal(cfg.PacificBlockHeight, uint64(432001))
require.Equal(cfg.AleutianBlockHeight, uint64(864001))
Expand All @@ -92,5 +92,5 @@ func TestNewHeightChange(t *testing.T) {
require.Equal(cfg.SumatraBlockHeight, uint64(28516681))
require.Equal(cfg.TsunamiBlockHeight, uint64(29275561))
require.Equal(cfg.UpernavikBlockHeight, uint64(31174201))
require.Equal(cfg.VanuatuBlockHeight, uint64(41174201))
require.Equal(cfg.VanuatuBlockHeight, uint64(33730921))
}
4 changes: 2 additions & 2 deletions blockchain/integrity/integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,7 @@ func testHistoryForAccount(t *testing.T, statetx bool) {
// check history account's balance
if statetx {
_, err = sf.WorkingSetAtHeight(ctx, 0)
require.Equal(factory.ErrNotSupported, errors.Cause(err))
require.NoError(err)
} else {
sr, err := sf.WorkingSetAtHeight(ctx, bc.TipHeight()-1)
require.NoError(err)
Expand Down Expand Up @@ -2467,7 +2467,7 @@ func testHistoryForContract(t *testing.T, statetx bool) {
// check the the original balance again
if statetx {
_, err = sf.WorkingSetAtHeight(ctx, bc.TipHeight()-1)
require.Equal(factory.ErrNotSupported, errors.Cause(err))
require.NoError(err)
} else {
sr, err := sf.WorkingSetAtHeight(ctx, bc.TipHeight()-1)
require.NoError(err)
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
1 change: 0 additions & 1 deletion blocksync/blocksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ func (bs *blockSyncer) ProcessBlock(ctx context.Context, peer string, blk *block
}
syncedHeight++
}
bs.buf.Cleanup(syncedHeight)
log.L().Debug("flush blocks", zap.Uint64("start", tip), zap.Uint64("end", syncedHeight))
if syncedHeight > bs.lastTip {
bs.lastTip = syncedHeight
Expand Down
21 changes: 0 additions & 21 deletions blocksync/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ package blocksync

import (
"sync"

"go.uber.org/zap"

"github.com/iotexproject/iotex-core/v2/pkg/log"
)

// blockBuffer is used to keep in-coming block in order.
Expand Down Expand Up @@ -47,23 +43,6 @@ func (b *blockBuffer) Pop(height uint64) []*peerBlock {
return blks
}

func (b *blockBuffer) Cleanup(height uint64) {
b.mu.Lock()
defer b.mu.Unlock()

size := len(b.blockQueues)
if size > int(b.bufferSize)*2 {
log.L().Warn("blockBuffer is leaking memory.", zap.Int("bufferSize", size))
newQueues := map[uint64]*uniQueue{}
for h := range b.blockQueues {
if h > height {
newQueues[h] = b.blockQueues[h]
}
}
b.blockQueues = newQueues
}
}

// AddBlock tries to put given block into buffer and flush buffer into blockchain.
func (b *blockBuffer) AddBlock(tipHeight uint64, blk *peerBlock) (bool, uint64) {
b.mu.Lock()
Expand Down
Loading

0 comments on commit 9aa8662

Please sign in to comment.