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] fix transfer event handling #3887

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add transfer e2etest
  • Loading branch information
envestcc committed Jun 13, 2023
commit 3340c2bd0a4204b08d0d77212727e31db5a70a8f
2 changes: 2 additions & 0 deletions blockindex/contractstaking/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ func (eh *contractStakingEventHandler) handleTransferEvent(event eventParam) err
}

token := tokenID.Uint64()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token -> tokenID

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

// cache token owner for stake event
eh.tokenOwner[token] = to
// update bucket owner if token exists
if bi, ok := eh.dirty.getBucketInfo(token); ok {
bi.Owner = to
return eh.dirty.updateBucketInfo(token, bi)
Expand Down
56 changes: 56 additions & 0 deletions e2etest/contract_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,62 @@ func TestContractStaking(t *testing.T) {
r.True(ok)
r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())
})

t.Run("transfer token", func(t *testing.T) {
Copy link
Member

@dustinxie dustinxie Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we discussed, every newly added funcs/APIs should be covered by a test

t.Run("transferFrom", func(t *testing.T) {
delegateIdx := 5
bt := simpleStake(_delegates[delegateIdx], big.NewInt(10), big.NewInt(10))
tokenID := bt.Index
r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())
from := common.BytesToAddress(identityset.Address(_adminID).Bytes())
newOwnerIdx := 25
to := common.BytesToAddress(identityset.Address(newOwnerIdx).Bytes())
data, err := lsdABI.Pack("transferFrom", from, to, big.NewInt(int64(tokenID)))
r.NoError(err)
param = callParam{
contractAddr: contractAddresses,
bytecode: hex.EncodeToString(data),
amount: big.NewInt(0),
gasLimit: 1000000,
gasPrice: big.NewInt(0),
sk: identityset.PrivateKey(adminID),
}
receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
r.Len(receipts, 1)
r.EqualValues("", receipts[0].ExecutionRevertMsg())
r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
bt, ok := indexer.Bucket(uint64(tokenID))
r.True(ok)
r.EqualValues(identityset.Address(newOwnerIdx).String(), bt.Owner.String())
})

t.Run("safeTransferFrom", func(t *testing.T) {
delegateIdx := 5
bt := simpleStake(_delegates[delegateIdx], big.NewInt(10), big.NewInt(10))
tokenID := bt.Index
r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())
from := common.BytesToAddress(identityset.Address(_adminID).Bytes())
newOwnerIdx := 25
to := common.BytesToAddress(identityset.Address(newOwnerIdx).Bytes())
data, err := lsdABI.Pack("safeTransferFrom", from, to, big.NewInt(int64(tokenID)))
r.NoError(err)
param = callParam{
contractAddr: contractAddresses,
bytecode: hex.EncodeToString(data),
amount: big.NewInt(0),
gasLimit: 1000000,
gasPrice: big.NewInt(0),
sk: identityset.PrivateKey(adminID),
}
receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
r.Len(receipts, 1)
r.EqualValues("", receipts[0].ExecutionRevertMsg())
r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
bt, ok := indexer.Bucket(uint64(tokenID))
r.True(ok)
r.EqualValues(identityset.Address(newOwnerIdx).String(), bt.Owner.String())
})
})
}

func prepareContractStakingBlockchain(ctx context.Context, cfg config.Config, r *require.Assertions) (blockchain.Blockchain, factory.Factory, blockdao.BlockDAO, actpool.ActPool, *contractstaking.Indexer) {
Expand Down