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

migrate delegation delegator index #304

Open
wants to merge 3 commits into
base: rc/barnard
Choose a base branch
from
Open
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
109 changes: 109 additions & 0 deletions integrationtests/delegators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,112 @@ func TestDelegateUnDelegateAndWithdraw(t *testing.T) {
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/delegators/delegator-after-withdraw.json"), string(genericResponse.Docs[0].Source))
}

func TestDelegateAndMoveDelegationFromSp1ToSp2(t *testing.T) {
setLogLevelDebug()

esClient, err := createESClient(esURL)
require.Nil(t, err)

esProc, err := CreateElasticProcessor(esClient)
require.Nil(t, err)

body := &dataBlock.Body{
MiniBlocks: []*dataBlock.MiniBlock{
{},
},
}
header := &dataBlock.Header{
Round: 50,
TimeStamp: 5040,
ShardID: core.MetachainShardId,
}

address1 := "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs"

// delegate
delegatedValue, _ := big.NewInt(0).SetString("500000000000000000000", 10)
pool := &outport.TransactionPool{
Logs: []*outport.LogData{
{
TxHash: hex.EncodeToString([]byte("h1")),
Log: &transaction.Log{
Address: decodeAddress("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat"),
Events: []*transaction.Event{
{
Address: decodeAddress(address1),
Identifier: []byte("delegate"),
Topics: [][]byte{delegatedValue.Bytes(), delegatedValue.Bytes(), big.NewInt(10).Bytes(), delegatedValue.Bytes()},
},
nil,
},
},
},
},
}

err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

ids := []string{"Au89waoRy1f09NfieNNh66eJoAOP+k1ij5GzTSsrHaI="}
genericResponse := &GenericResponse{}
err = esClient.DoMultiGet(context.Background(), ids, indexerdata.DelegatorsIndex, true, genericResponse)
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/delegators/migrate-delegate.json"), string(genericResponse.Docs[0].Source))

// migrate delegation events
header = &dataBlock.Header{
Round: 60,
TimeStamp: 6040,
ShardID: core.MetachainShardId,
}

valueToMigrate, _ := big.NewInt(0).SetString("100000000000000000000", 10)
delegatedValue.Sub(delegatedValue, valueToMigrate)
pool = &outport.TransactionPool{
Logs: []*outport.LogData{
{
TxHash: hex.EncodeToString([]byte("h1")),
Log: &transaction.Log{
Address: decodeAddress("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat"),
Events: []*transaction.Event{
{
Address: decodeAddress(address1),
Identifier: []byte("removeDelegationFromSource"),
Topics: [][]byte{valueToMigrate.Bytes(), delegatedValue.Bytes(), big.NewInt(10).Bytes(), delegatedValue.Bytes()},
},
nil,
},
},
},

{
TxHash: hex.EncodeToString([]byte("h2")),
Log: &transaction.Log{
Address: decodeAddress("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk8llllssp7z7y"),
Events: []*transaction.Event{
{
Address: decodeAddress(address1),
Identifier: []byte("moveDelegationToDestination"),
Topics: [][]byte{valueToMigrate.Bytes(), valueToMigrate.Bytes(), big.NewInt(1).Bytes(), valueToMigrate.Bytes()},
},
nil,
},
},
},
},
}

err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

ids = []string{"Au89waoRy1f09NfieNNh66eJoAOP+k1ij5GzTSsrHaI=", "BWXCf3gA7Z4anFYDk8eOQQMbG9r+hcjTdUqaxGiDxkk="}
genericResponse = &GenericResponse{}
err = esClient.DoMultiGet(context.Background(), ids, indexerdata.DelegatorsIndex, true, genericResponse)
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/delegators/migrate-on-sp1.json"), string(genericResponse.Docs[0].Source))

err = esClient.DoMultiGet(context.Background(), ids, indexerdata.DelegatorsIndex, true, genericResponse)
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/delegators/migrate-on-sp2.json"), string(genericResponse.Docs[1].Source))
}
7 changes: 7 additions & 0 deletions integrationtests/testdata/delegators/migrate-delegate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"address": "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs",
"contract": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat",
"activeStake": "500000000000000000000",
"activeStakeNum": 500,
"timestamp": 5040
}
7 changes: 7 additions & 0 deletions integrationtests/testdata/delegators/migrate-on-sp1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"address": "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs",
"contract": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat",
"activeStake": "400000000000000000000",
"activeStakeNum": 400,
"timestamp": 6040
}
7 changes: 7 additions & 0 deletions integrationtests/testdata/delegators/migrate-on-sp2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"address": "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs",
"contract": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk8llllssp7z7y",
"activeStake": "100000000000000000000",
"activeStakeNum": 100,
"timestamp": 6040
}
26 changes: 15 additions & 11 deletions process/elasticproc/logsevents/delegatorsProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
)

const (
minNumTopicsDelegators = 4
delegateFunc = "delegate"
unDelegateFunc = "unDelegate"
withdrawFunc = "withdraw"
reDelegateRewardsFunc = "reDelegateRewards"
claimRewardsFunc = "claimRewards"
minNumTopicsDelegators = 4
delegateFunc = "delegate"
unDelegateFunc = "unDelegate"
withdrawFunc = "withdraw"
reDelegateRewardsFunc = "reDelegateRewards"
claimRewardsFunc = "claimRewards"
removeDelegationFromSourceFunc = "removeDelegationFromSource"
moveDelegationToDestinationFunc = "moveDelegationToDestination"

minNumTopicsClaimRewards = 2
numTopicsClaimRewardsWithContractAddress = 3
Expand All @@ -35,11 +37,13 @@ func newDelegatorsProcessor(
) *delegatorsProc {
return &delegatorsProc{
delegatorsOperations: map[string]struct{}{
delegateFunc: {},
unDelegateFunc: {},
withdrawFunc: {},
reDelegateRewardsFunc: {},
claimRewardsFunc: {},
delegateFunc: {},
unDelegateFunc: {},
withdrawFunc: {},
reDelegateRewardsFunc: {},
claimRewardsFunc: {},
removeDelegationFromSourceFunc: {},
moveDelegationToDestinationFunc: {},
},
pubkeyConverter: pubkeyConverter,
balanceConverter: balanceConverter,
Expand Down
58 changes: 58 additions & 0 deletions process/elasticproc/logsevents/delegatorsProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,61 @@ func TestDelegatorsProcessor_WithdrawalShouldWorkWithNewTopics(t *testing.T) {
require.True(t, res.delegator.ShouldDelete)
require.Equal(t, []string{"696431", "696432"}, res.delegator.WithdrawFundIDs)
}

func TestDelegatorProcessor_RemoveDelegationFromSource(t *testing.T) {
t.Parallel()

event := &transaction.Event{
Address: []byte("addr"),
Identifier: []byte(removeDelegationFromSourceFunc),
Topics: [][]byte{big.NewInt(1000).Bytes(), big.NewInt(100000000000).Bytes(), big.NewInt(10).Bytes(), big.NewInt(1000000000).Bytes()},
}
args := &argsProcessEvent{
timestamp: 1234,
event: event,
logAddress: []byte("contract"),
selfShardID: core.MetachainShardId,
}

balanceConverter, _ := converters.NewBalanceConverter(10)
delegatorsProcessor := newDelegatorsProcessor(&mock.PubkeyConverterMock{}, balanceConverter)

res := delegatorsProcessor.processEvent(args)
require.True(t, res.processed)
require.Equal(t, &data.Delegator{
Address: "61646472",
Contract: "636f6e7472616374",
ActiveStakeNum: 10.0,
ActiveStake: "100000000000",
Timestamp: 1234,
}, res.delegator)
}

func TestDelegatorProcessor_MoveDelegationToDestination(t *testing.T) {
t.Parallel()

event := &transaction.Event{
Address: []byte("addr"),
Identifier: []byte(moveDelegationToDestinationFunc),
Topics: [][]byte{big.NewInt(1000).Bytes(), big.NewInt(1000000000000).Bytes(), big.NewInt(10).Bytes(), big.NewInt(1000000000).Bytes()},
}
args := &argsProcessEvent{
timestamp: 1234,
event: event,
logAddress: []byte("contract"),
selfShardID: core.MetachainShardId,
}

balanceConverter, _ := converters.NewBalanceConverter(10)
delegatorsProcessor := newDelegatorsProcessor(&mock.PubkeyConverterMock{}, balanceConverter)

res := delegatorsProcessor.processEvent(args)
require.True(t, res.processed)
require.Equal(t, &data.Delegator{
Address: "61646472",
Contract: "636f6e7472616374",
ActiveStakeNum: 100.0,
ActiveStake: "1000000000000",
Timestamp: 1234,
}, res.delegator)
}
Loading