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

contract indexer handle BucketExpanded event #3881

Merged
merged 7 commits into from
Jun 8, 2023
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
105 changes: 32 additions & 73 deletions blockindex/contractstaking/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,6 @@ import (
const (
// StakingContractABI is the ABI of system staking contract
StakingContractABI = `[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "AmountIncreased",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -149,25 +130,6 @@ const (
"name": "DelegateChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "duration",
"type": "uint256"
}
],
"name": "DurationExtended",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -357,6 +319,31 @@ const (
],
"name": "Withdrawal",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "duration",
"type": "uint256"
}
],
"name": "BucketExpanded",
"type": "event"
}
]`
)
Expand Down Expand Up @@ -417,10 +404,8 @@ func (eh *contractStakingEventHandler) HandleEvent(ctx context.Context, blk *blo
return eh.handleUnstakedEvent(event, blk.Height())
case "Merged":
return eh.handleMergedEvent(event)
case "DurationExtended":
return eh.handleDurationExtendedEvent(event)
case "AmountIncreased":
return eh.handleAmountIncreasedEvent(event)
case "BucketExpanded":
return eh.handleBucketExpandedEvent(event)
case "DelegateChanged":
return eh.handleDelegateChangedEvent(event)
case "Withdrawal":
Expand Down Expand Up @@ -614,38 +599,16 @@ func (eh *contractStakingEventHandler) handleMergedEvent(event eventParam) error
return eh.dirty.updateBucketInfo(tokenIDsParam[0].Uint64(), b)
}

func (eh *contractStakingEventHandler) handleDurationExtendedEvent(event eventParam) error {
func (eh *contractStakingEventHandler) handleBucketExpandedEvent(event eventParam) error {
tokenIDParam, err := event.IndexedFieldUint256("tokenId")
if err != nil {
return err
}
durationParam, err := event.FieldUint256("duration")
if err != nil {
return err
}

b, ok := eh.dirty.getBucketInfo(tokenIDParam.Uint64())
if !ok {
return errors.Wrapf(ErrBucketNotExist, "token id %d", tokenIDParam.Uint64())
}
bt, ok := eh.dirty.getBucketType(b.TypeIndex)
if !ok {
return errors.Wrapf(errBucketTypeNotExist, "id %d", b.TypeIndex)
}
newBtIdx, _, ok := eh.dirty.matchBucketType(bt.Amount, durationParam.Uint64())
if !ok {
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", bt.Amount.Int64(), durationParam.Uint64())
}
b.TypeIndex = newBtIdx
return eh.dirty.updateBucketInfo(tokenIDParam.Uint64(), b)
}

func (eh *contractStakingEventHandler) handleAmountIncreasedEvent(event eventParam) error {
tokenIDParam, err := event.IndexedFieldUint256("tokenId")
amountParam, err := event.FieldUint256("amount")
if err != nil {
return err
}
amountParam, err := event.FieldUint256("amount")
durationParam, err := event.FieldUint256("duration")
if err != nil {
return err
}
Expand All @@ -654,13 +617,9 @@ func (eh *contractStakingEventHandler) handleAmountIncreasedEvent(event eventPar
if !ok {
return errors.Wrapf(ErrBucketNotExist, "token id %d", tokenIDParam.Uint64())
}
bt, ok := eh.dirty.getBucketType(b.TypeIndex)
newBtIdx, _, ok := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
if !ok {
return errors.Wrapf(errBucketTypeNotExist, "id %d", b.TypeIndex)
}
newBtIdx, _, ok := eh.dirty.matchBucketType(amountParam, bt.Duration)
if !ok {
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", amountParam.Int64(), bt.Duration)
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", amountParam.Int64(), durationParam.Uint64())
}
b.TypeIndex = newBtIdx
return eh.dirty.updateBucketInfo(tokenIDParam.Uint64(), b)
Expand Down
56 changes: 56 additions & 0 deletions blockindex/contractstaking/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,53 @@ func TestContractStakingIndexerBucketInfo(t *testing.T) {
r.EqualValues(1, indexer.TotalBucketCount())
}

func TestContractStakingIndexerChangeBucketType(t *testing.T) {
r := require.New(t)
testDBPath, err := testutil.PathOfTempFile("staking.db")
r.NoError(err)
defer testutil.CleanupPath(testDBPath)
cfg := db.DefaultConfig
cfg.DbPath = testDBPath
kvStore := db.NewBoltDB(cfg)
indexer, err := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(indexer.Start(context.Background()))

// init bucket type
bucketTypeData := [][2]int64{
{10, 10},
{20, 10},
{10, 100},
{20, 100},
}
height := uint64(1)
handler := newContractStakingEventHandler(indexer.cache, height)
for _, data := range bucketTypeData {
activateBucketType(r, handler, data[0], data[1], height)
}
err = indexer.commit(handler)
r.NoError(err)

t.Run("expand bucket type", func(t *testing.T) {
owner := identityset.Address(0)
delegate := identityset.Address(1)
height++
handler = newContractStakingEventHandler(indexer.cache, height)
stake(r, handler, owner, delegate, 1, 10, 100, height)
r.NoError(err)
r.NoError(indexer.commit(handler))
bucket, ok := indexer.Bucket(1)
r.True(ok)

expandBucketType(r, handler, int64(bucket.Index), 20, 100)
r.NoError(indexer.commit(handler))
bucket, ok = indexer.Bucket(bucket.Index)
r.True(ok)
r.EqualValues(20, bucket.StakedAmount.Int64())
r.EqualValues(100, bucket.StakedDurationBlockNumber)
})
}

func BenchmarkIndexer_PutBlockBeforeContractHeight(b *testing.B) {
// Create a new Indexer with a contract height of 100
indexer := &Indexer{contractDeployHeight: 100}
Expand Down Expand Up @@ -458,3 +505,12 @@ func withdraw(r *require.Assertions, handler *contractStakingEventHandler, token
})
r.NoError(err)
}

func expandBucketType(r *require.Assertions, handler *contractStakingEventHandler, token, amount, duration int64) {
err := handler.handleBucketExpandedEvent(eventParam{
"tokenId": big.NewInt(token),
"amount": big.NewInt(amount),
"duration": big.NewInt(duration),
})
r.NoError(err)
}
Loading