From 690871a665c270c816c640b4031c054b6e0860dd Mon Sep 17 00:00:00 2001 From: miiu Date: Tue, 12 Nov 2024 15:45:20 +0200 Subject: [PATCH 1/3] migrate delegation delegator index --- .../logsevents/delegatorsProcessor.go | 26 +++++---- .../logsevents/delegatorsProcessor_test.go | 58 +++++++++++++++++++ 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/process/elasticproc/logsevents/delegatorsProcessor.go b/process/elasticproc/logsevents/delegatorsProcessor.go index 1cf6c6e7..707f5ef3 100644 --- a/process/elasticproc/logsevents/delegatorsProcessor.go +++ b/process/elasticproc/logsevents/delegatorsProcessor.go @@ -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 @@ -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, diff --git a/process/elasticproc/logsevents/delegatorsProcessor_test.go b/process/elasticproc/logsevents/delegatorsProcessor_test.go index 8d8a5471..d138b994 100644 --- a/process/elasticproc/logsevents/delegatorsProcessor_test.go +++ b/process/elasticproc/logsevents/delegatorsProcessor_test.go @@ -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) +} From a0088ee73aebcb588b37a2a7549baae820d1549a Mon Sep 17 00:00:00 2001 From: miiu Date: Thu, 14 Nov 2024 11:16:05 +0200 Subject: [PATCH 2/3] integration tests migrate delegation --- integrationtests/delegators_test.go | 109 ++++++++++++++++++ .../testdata/delegators/migrate-delegate.json | 7 ++ .../testdata/delegators/migrate-on-sp1.json | 7 ++ .../testdata/delegators/migrate-on-sp2.json | 7 ++ 4 files changed, 130 insertions(+) create mode 100644 integrationtests/testdata/delegators/migrate-delegate.json create mode 100644 integrationtests/testdata/delegators/migrate-on-sp1.json create mode 100644 integrationtests/testdata/delegators/migrate-on-sp2.json diff --git a/integrationtests/delegators_test.go b/integrationtests/delegators_test.go index 3517b26e..babf530a 100644 --- a/integrationtests/delegators_test.go +++ b/integrationtests/delegators_test.go @@ -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)) +} diff --git a/integrationtests/testdata/delegators/migrate-delegate.json b/integrationtests/testdata/delegators/migrate-delegate.json new file mode 100644 index 00000000..97c25cdf --- /dev/null +++ b/integrationtests/testdata/delegators/migrate-delegate.json @@ -0,0 +1,7 @@ +{ + "address": "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs", + "contract": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat", + "activeStake": "500000000000000000000", + "activeStakeNum": 500, + "timestamp": 5040 +} \ No newline at end of file diff --git a/integrationtests/testdata/delegators/migrate-on-sp1.json b/integrationtests/testdata/delegators/migrate-on-sp1.json new file mode 100644 index 00000000..775e3e04 --- /dev/null +++ b/integrationtests/testdata/delegators/migrate-on-sp1.json @@ -0,0 +1,7 @@ +{ + "address": "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs", + "contract": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat", + "activeStake": "400000000000000000000", + "activeStakeNum": 400, + "timestamp": 6040 +} \ No newline at end of file diff --git a/integrationtests/testdata/delegators/migrate-on-sp2.json b/integrationtests/testdata/delegators/migrate-on-sp2.json new file mode 100644 index 00000000..3bdfbf3e --- /dev/null +++ b/integrationtests/testdata/delegators/migrate-on-sp2.json @@ -0,0 +1,7 @@ +{ + "address": "erd1zmn5ujvgkwsr8jufg57mrhzv48z4vgf5yaskksqnsvdu444ye6jq4s95hs", + "contract": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk8llllssp7z7y", + "activeStake": "100000000000000000000", + "activeStakeNum": 100, + "timestamp": 6040 +} From ddc62ecf2c04d42955249538dc656c712db599b3 Mon Sep 17 00:00:00 2001 From: miiu Date: Thu, 14 Nov 2024 11:16:25 +0200 Subject: [PATCH 3/3] empty lines --- integrationtests/testdata/delegators/migrate-delegate.json | 2 +- integrationtests/testdata/delegators/migrate-on-sp1.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integrationtests/testdata/delegators/migrate-delegate.json b/integrationtests/testdata/delegators/migrate-delegate.json index 97c25cdf..c90e7d29 100644 --- a/integrationtests/testdata/delegators/migrate-delegate.json +++ b/integrationtests/testdata/delegators/migrate-delegate.json @@ -4,4 +4,4 @@ "activeStake": "500000000000000000000", "activeStakeNum": 500, "timestamp": 5040 -} \ No newline at end of file +} diff --git a/integrationtests/testdata/delegators/migrate-on-sp1.json b/integrationtests/testdata/delegators/migrate-on-sp1.json index 775e3e04..0b1895f2 100644 --- a/integrationtests/testdata/delegators/migrate-on-sp1.json +++ b/integrationtests/testdata/delegators/migrate-on-sp1.json @@ -4,4 +4,4 @@ "activeStake": "400000000000000000000", "activeStakeNum": 400, "timestamp": 6040 -} \ No newline at end of file +}