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

Revert "[iip-15]sgdRegistry implementation (#3845) " #4357

Merged
merged 8 commits into from
Aug 12, 2024
Prev Previous commit
Next Next commit
Revert "[blockindex] restrict height for sgdindexer during write and …
…read operation (#3926)"

This reverts commit 66edd61.
  • Loading branch information
dustinxie committed Aug 12, 2024
commit 276bcdac3f78769e4fd6f0ada0b4b62a56c56521
10 changes: 3 additions & 7 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type (

// SGDRegistry is the interface for handling Sharing of Gas-fee with DApps
SGDRegistry interface {
CheckContract(context.Context, string, uint64) (address.Address, uint64, bool, error)
CheckContract(context.Context, string) (address.Address, uint64, bool, error)
}
)

Expand Down Expand Up @@ -324,12 +324,8 @@ func processSGD(ctx context.Context, sm protocol.StateManager, execution *action
if execution.To() == nil {
return nil, 0, nil
}
height, err := sm.Height()
if err != nil {
return nil, 0, err
}
contract, _ := address.FromBytes((*execution.To())[:])
receiver, percentage, ok, err := sgd.CheckContract(ctx, contract.String(), height-1)

receiver, percentage, ok, err := sgd.CheckContract(ctx, execution.Contract())
if err != nil || !ok {
return nil, 0, err
}
Expand Down
65 changes: 10 additions & 55 deletions blockindex/sgd_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ type (
SGDRegistry interface {
blockdao.BlockIndexerWithStart
// CheckContract returns the contract's eligibility for SGD and percentage
CheckContract(context.Context, string, uint64) (address.Address, uint64, bool, error)
CheckContract(context.Context, string) (address.Address, uint64, bool, error)
// FetchContracts returns all contracts that are eligible for SGD
FetchContracts(context.Context, uint64) ([]*SGDIndex, error)
FetchContracts(context.Context) ([]*SGDIndex, error)
}

sgdRegistry struct {
Expand Down Expand Up @@ -271,7 +271,11 @@ func (sgd *sgdRegistry) Stop(ctx context.Context) error {

// Height returns the current height of the SGDIndexer
func (sgd *sgdRegistry) Height() (uint64, error) {
return sgd.height()
h, err := sgd.kvStore.Get(_sgdToHeightNS, _sgdCurrentHeight)
if err != nil {
return 0, err
}
return byteutil.BytesToUint64BigEndian(h), nil
}

// StartHeight returns the start height of the indexer
Expand All @@ -281,17 +285,9 @@ func (sgd *sgdRegistry) StartHeight() uint64 {

// PutBlock puts a block into SGDIndexer
func (sgd *sgdRegistry) PutBlock(ctx context.Context, blk *block.Block) error {
expectHeight, err := sgd.expectHeight()
if err != nil {
return err
}
if blk.Height() < expectHeight {
if blk.Height() < sgd.startHeight {
return nil
}
if blk.Height() > expectHeight {
return errors.Errorf("invalid block height %d, expect %d", blk.Height(), expectHeight)
}

var (
r *action.Receipt
ok bool
Expand Down Expand Up @@ -434,10 +430,7 @@ func (sgd *sgdRegistry) DeleteTipBlock(context.Context, *block.Block) error {
}

// CheckContract checks if the contract is a SGD contract
func (sgd *sgdRegistry) CheckContract(ctx context.Context, contract string, height uint64) (address.Address, uint64, bool, error) {
if err := sgd.validateQueryHeight(height); err != nil {
return nil, 0, false, err
}
func (sgd *sgdRegistry) CheckContract(ctx context.Context, contract string) (address.Address, uint64, bool, error) {
addr, err := address.FromString(contract)
if err != nil {
return nil, 0, false, err
Expand Down Expand Up @@ -465,10 +458,7 @@ func (sgd *sgdRegistry) getSGDIndex(contract []byte) (*indexpb.SGDIndex, error)
}

// FetchContracts returns all contracts that are eligible for SGD
func (sgd *sgdRegistry) FetchContracts(ctx context.Context, height uint64) ([]*SGDIndex, error) {
if err := sgd.validateQueryHeight(height); err != nil {
return nil, err
}
func (sgd *sgdRegistry) FetchContracts(ctx context.Context) ([]*SGDIndex, error) {
_, values, err := sgd.kvStore.Filter(_sgdBucket, func(k, v []byte) bool { return true }, nil, nil)
if err != nil {
if errors.Cause(err) == db.ErrNotExist || errors.Cause(err) == db.ErrBucketNotExist {
Expand All @@ -491,41 +481,6 @@ func (sgd *sgdRegistry) FetchContracts(ctx context.Context, height uint64) ([]*S
return sgdIndexes, nil
}

func (sgd *sgdRegistry) validateQueryHeight(height uint64) error {
// 0 means latest height
if height == 0 {
return nil
}
tipHeight, err := sgd.height()
if err != nil {
return err
}
if height != tipHeight {
return errors.Errorf("invalid height %d, expect %d", height, tipHeight)
}
return nil
}

func (sgd *sgdRegistry) expectHeight() (uint64, error) {
tipHeight, err := sgd.height()
if err != nil {
return 0, err
}
expectHeight := tipHeight + 1
if expectHeight < sgd.startHeight {
expectHeight = sgd.startHeight
}
return expectHeight, nil
}

func (sgd *sgdRegistry) height() (uint64, error) {
h, err := sgd.kvStore.Get(_sgdToHeightNS, _sgdCurrentHeight)
if err != nil {
return 0, err
}
return byteutil.BytesToUint64BigEndian(h), nil
}

func getReceiptsFromBlock(blk *block.Block) map[hash.Hash256]*action.Receipt {
receipts := make(map[hash.Hash256]*action.Receipt, len(blk.Receipts))
for _, receipt := range blk.Receipts {
Expand Down
43 changes: 9 additions & 34 deletions blockindex/sgd_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func TestNewSGDRegistry(t *testing.T) {
}
blk := createTestingBlock(builder, 1, h, exec, logs)
r.NoError(sgdRegistry.PutBlock(ctx, blk))
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String(), 1)
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String())
r.NoError(err)
r.Equal(_sgdPercentage, percentage)
r.Equal(receiverAddress, receiver)
r.False(isApproved)

lists, err := sgdRegistry.FetchContracts(ctx, 1)
lists, err := sgdRegistry.FetchContracts(ctx)
r.NoError(err)
r.Equal(1, len(lists))
r.Equal(registerAddress.Bytes(), lists[0].Contract.Bytes())
Expand All @@ -102,38 +102,13 @@ func TestNewSGDRegistry(t *testing.T) {
Topics: []hash.Hash256{hash.Hash256(event.ID)},
Data: data,
}
blk := createTestingBlock(builder, 2, h, exec, logs)
blk := createTestingBlock(builder, 1, h, exec, logs)
r.NoError(sgdRegistry.PutBlock(ctx, blk))
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String(), 2)
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String())
r.NoError(err)
r.Equal(receiverAddress, receiver)
r.True(isApproved)
r.Equal(_sgdPercentage, percentage)

t.Run("heightRestriction", func(t *testing.T) {
cases := []struct {
height uint64
isErr bool
}{
{0, false},
{1, true},
{2, false},
{3, true},
}
for i := range cases {
if cases[i].isErr {
_, err = sgdRegistry.FetchContracts(ctx, cases[i].height)
r.ErrorContains(err, "invalid height")
_, _, _, err = sgdRegistry.CheckContract(ctx, registerAddress.String(), cases[i].height)
r.ErrorContains(err, "invalid height")
} else {
_, err = sgdRegistry.FetchContracts(ctx, cases[i].height)
r.Nil(err)
_, _, _, err = sgdRegistry.CheckContract(ctx, registerAddress.String(), cases[i].height)
r.Nil(err)
}
}
})
})
t.Run("disapproveContract", func(t *testing.T) {
builder := block.NewTestingBuilder()
Expand All @@ -147,9 +122,9 @@ func TestNewSGDRegistry(t *testing.T) {
Topics: []hash.Hash256{hash.Hash256(event.ID)},
Data: data,
}
blk := createTestingBlock(builder, 3, h, exec, logs)
blk := createTestingBlock(builder, 1, h, exec, logs)
r.NoError(sgdRegistry.PutBlock(ctx, blk))
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String(), 3)
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String())
r.NoError(err)
r.Equal(receiverAddress, receiver)
r.False(isApproved)
Expand All @@ -167,9 +142,9 @@ func TestNewSGDRegistry(t *testing.T) {
Topics: []hash.Hash256{hash.Hash256(event.ID)},
Data: data,
}
blk := createTestingBlock(builder, 4, h, exec, logs)
blk := createTestingBlock(builder, 2, h, exec, logs)
r.NoError(sgdRegistry.PutBlock(ctx, blk))
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String(), 4)
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, registerAddress.String())
r.ErrorContains(err, "not exist in DB")
r.Nil(receiver)
r.False(isApproved)
Expand All @@ -178,7 +153,7 @@ func TestNewSGDRegistry(t *testing.T) {
r.Equal(blk.Height(), hh)
r.Equal(uint64(0), percentage)

_, err = sgdRegistry.FetchContracts(ctx, blk.Height())
_, err = sgdRegistry.FetchContracts(ctx)
r.ErrorIs(err, state.ErrStateNotExist)
})
})
Expand Down
7 changes: 3 additions & 4 deletions e2etest/sgd_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-address/address"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/account"
Expand All @@ -30,6 +28,7 @@ import (
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-core/testutil"
"github.com/stretchr/testify/require"
)

type checkContractExpectation struct {
Expand Down Expand Up @@ -113,7 +112,7 @@ func TestSGDRegistry(t *testing.T) {
r.NoError(err)
kvstore, err := db.CreateKVStore(db.DefaultConfig, indexSGDDBPath)
r.NoError(err)
sgdRegistry := blockindex.NewSGDRegistry(contractAddress, 2, kvstore)
sgdRegistry := blockindex.NewSGDRegistry(contractAddress, 0, kvstore)
r.NoError(sgdRegistry.Start(ctx))
defer func() {
r.NoError(sgdRegistry.Stop(ctx))
Expand Down Expand Up @@ -260,7 +259,7 @@ func TestSGDRegistry(t *testing.T) {
)
r.NoError(sgdRegistry.PutBlock(ctx, blk))
}
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, tt.checkContractExpect.contractAddress, height)
receiver, percentage, isApproved, err := sgdRegistry.CheckContract(ctx, tt.checkContractExpect.contractAddress)
if tt.checkContractExpect.errorContains != "" {
r.ErrorContains(err, tt.checkContractExpect.errorContains)
} else {
Expand Down