Skip to content

Commit

Permalink
[iip-13] contract indexer handle BucketExpanded event (#3881)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Jun 8, 2023
1 parent ac12909 commit 0eddba5
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 128 deletions.
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

0 comments on commit 0eddba5

Please sign in to comment.