From d2b81177a394ae47ba7541651767187e587692ce Mon Sep 17 00:00:00 2001 From: Wilmer Rondon Date: Thu, 30 May 2024 15:01:05 -0400 Subject: [PATCH 1/6] Integration Tests for FeePerKb --- .../peg/feeperkb/FeePerKbIntegrationTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java new file mode 100644 index 00000000000..f981f31a76b --- /dev/null +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java @@ -0,0 +1,66 @@ +package co.rsk.peg.feeperkb; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import co.rsk.bitcoinj.core.Coin; +import co.rsk.core.RskAddress; +import co.rsk.peg.InMemoryStorage; +import co.rsk.peg.feeperkb.constants.FeePerKbConstants; +import co.rsk.peg.feeperkb.constants.FeePerKbMainNetConstants; +import co.rsk.peg.storage.StorageAccessor; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.ethereum.core.SignatureCache; +import org.ethereum.core.Transaction; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class FeePerKbIntegrationTest { + private final FeePerKbConstants feePerKbConstants = FeePerKbMainNetConstants.getInstance(); + private final StorageAccessor inMemoryStorage = new InMemoryStorage(); + private FeePerKbSupport feePerKbSupport; + private FeePerKbStorageProvider feePerKbStorageProvider; + + @BeforeEach + void setUp() { + feePerKbStorageProvider = new FeePerKbStorageProviderImpl(inMemoryStorage); + feePerKbSupport = new FeePerKbSupportImpl(feePerKbConstants,feePerKbStorageProvider); + } + + @Test + void voteFeePerKbChange_winnerFeePerKbValue_shouldReturnSuccessfulFeeVotedResponseCode() { + SignatureCache signatureCache = mock(SignatureCache.class); + + //First Vote + Transaction firstTx = getTransactionFromAuthorizedCaller(signatureCache, 1); + Coin firstFeePerKbVote = Coin.valueOf(50_000L); + feePerKbSupport.voteFeePerKbChange(firstTx, firstFeePerKbVote, signatureCache); + + //Second vote + Transaction secondTx = getTransactionFromAuthorizedCaller(signatureCache, 2); + Coin secondFeePerKbVote = Coin.valueOf(50_000L); + Integer ActualResult = feePerKbSupport.voteFeePerKbChange(secondTx, secondFeePerKbVote, signatureCache); + + Integer expectedResult = FeePerKbResponseCode.SUCCESSFUL_VOTE.getCode(); + assertEquals(expectedResult, ActualResult); + } + + private Transaction getTransactionFromAuthorizedCaller(SignatureCache signatureCache, int authorizedRskAddressIndex) { + RskAddress authorizedRskAddress = this.getAuthorizedRskAddresses().get(authorizedRskAddressIndex); + Transaction txFromAuthorizedCaller = mock(Transaction.class); + when(txFromAuthorizedCaller.getSender(signatureCache)).thenReturn(authorizedRskAddress); + + return txFromAuthorizedCaller; + } + + private List getAuthorizedRskAddresses(){ + return Stream.of( + "a02db0ed94a5894bc6f9079bb9a2d93ada1917f3", + "180a7edda4e640ea5a3e495e17a1efad260c39e9", + "8418edc8fea47183116b4c8cd6a12e51a7e169c1" + ).map(RskAddress::new).collect(Collectors.toList()); + } +} From e8a07eab10d75f95bf2c6273595c43a4d9c3acbe Mon Sep 17 00:00:00 2001 From: Marcos Date: Thu, 30 May 2024 16:48:58 -0300 Subject: [PATCH 2/6] Create a first integration test --- .../peg/feeperkb/FeePerKbIntegrationTest.java | 150 +++++++++++++++--- 1 file changed, 132 insertions(+), 18 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java index f981f31a76b..b99d1c70157 100644 --- a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java @@ -20,38 +20,152 @@ class FeePerKbIntegrationTest { private final FeePerKbConstants feePerKbConstants = FeePerKbMainNetConstants.getInstance(); - private final StorageAccessor inMemoryStorage = new InMemoryStorage(); private FeePerKbSupport feePerKbSupport; - private FeePerKbStorageProvider feePerKbStorageProvider; + private SignatureCache signatureCache; @BeforeEach void setUp() { - feePerKbStorageProvider = new FeePerKbStorageProviderImpl(inMemoryStorage); - feePerKbSupport = new FeePerKbSupportImpl(feePerKbConstants,feePerKbStorageProvider); + StorageAccessor inMemoryStorage = new InMemoryStorage(); + FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(inMemoryStorage); + feePerKbSupport = new FeePerKbSupportImpl(feePerKbConstants, feePerKbStorageProvider); + signatureCache = mock(SignatureCache.class); } @Test - void voteFeePerKbChange_winnerFeePerKbValue_shouldReturnSuccessfulFeeVotedResponseCode() { - SignatureCache signatureCache = mock(SignatureCache.class); + void feePerKbIntegrationTest() { + Coin genesisFeePerKb = feePerKbConstants.getGenesisFeePerKb(); - //First Vote - Transaction firstTx = getTransactionFromAuthorizedCaller(signatureCache, 1); + // Get fee per kb before any voting, should return the genesis fee per kb + assertFeePerKbValue(genesisFeePerKb); + + /* + * First voting, authorizer 1 and 2 vote the same value + */ Coin firstFeePerKbVote = Coin.valueOf(50_000L); - feePerKbSupport.voteFeePerKbChange(firstTx, firstFeePerKbVote, signatureCache); + // Send 1 vote from an authorizer + voteFeePerKb(firstFeePerKbVote, 0); + + // Get fee per kb, shouldn't have changed because only vote was added + assertFeePerKbValue(genesisFeePerKb); + + // Add a second vote from a different authorizer + voteFeePerKb(firstFeePerKbVote, 1); + + // Get fee per kb, should return the newly voted value + assertFeePerKbValue(firstFeePerKbVote); + + /* + * Second voting, authorizers 2 and 3 vote the same value + */ + Coin secondFeePerKbVote = Coin.valueOf(100_000L); + // First vote for a new value + voteFeePerKb(secondFeePerKbVote, 2); + + // Get fee per kb, should return the first value voted because the new value has only 1 vote + assertFeePerKbValue(firstFeePerKbVote); + + // Add a second vote for the same value, different authorizer + voteFeePerKb(secondFeePerKbVote, 1); + + // Get fee per kb, should return the second vote value + assertFeePerKbValue(secondFeePerKbVote); + + /* + * Third voting, authorizers 1 and 2 vote different values. Then authorizer 3 votes the same value as authorizer 1 + */ + Coin thirdFeePerKbVote = Coin.valueOf(40_000L); + // First vote for a new value + voteFeePerKb(thirdFeePerKbVote, 0); + + // Get fee per kb, should return the second voted value because the third value has only 1 vote + assertFeePerKbValue(secondFeePerKbVote); + + // Add a second vote for a different value, different authorizer + Coin differentFeePerKbVote = Coin.valueOf(60_000L); + voteFeePerKb(differentFeePerKbVote, 1); + + // Get fee per kb, should return the second voted value because the third value has only 2 different votes + assertFeePerKbValue(secondFeePerKbVote); + + // Add a third vote from authorizer 3, same value as the one voted from authorizer 1 + voteFeePerKb(thirdFeePerKbVote, 2); - //Second vote - Transaction secondTx = getTransactionFromAuthorizedCaller(signatureCache, 2); - Coin secondFeePerKbVote = Coin.valueOf(50_000L); - Integer ActualResult = feePerKbSupport.voteFeePerKbChange(secondTx, secondFeePerKbVote, signatureCache); + // Get fee per kb, should return the third vote value + assertFeePerKbValue(thirdFeePerKbVote); - Integer expectedResult = FeePerKbResponseCode.SUCCESSFUL_VOTE.getCode(); - assertEquals(expectedResult, ActualResult); + /* + * Fourth voting, authorizers 1 and 2 vote the same value. Then authorizer 3 votes a different value + */ + Coin fourthFeePerKbVote = Coin.valueOf(70_000L); } - private Transaction getTransactionFromAuthorizedCaller(SignatureCache signatureCache, int authorizedRskAddressIndex) { - RskAddress authorizedRskAddress = this.getAuthorizedRskAddresses().get(authorizedRskAddressIndex); + private void assertFeePerKbValue(Coin expectedValue) { + Coin feePerKb = feePerKbSupport.getFeePerKb(); + assertEquals(expectedValue, feePerKb); + } + + private void voteFeePerKb(Coin value, int authorizerIndex) { + RskAddress authorizerAddress = this.getAuthorizedRskAddresses().get(authorizerIndex); + Transaction voteTx = getTransactionFromAuthorizedCaller(authorizerAddress); + feePerKbSupport.voteFeePerKbChange(voteTx, value, signatureCache); + } + + + + +// @Test +// void getFeePerKbBeforeAnyVotes_shouldReturnGenesisFeePerKb() { +// Coin genesisFeePerKb = feePerKbConstants.getGenesisFeePerKb(); +// +// // Get fee per kb before any voting, should return the genesis fee per kb +// Coin feePerKb = feePerKbSupport.getFeePerKb(); +// assertEquals(genesisFeePerKb, feePerKb); +// } +// +// @Test +// void testFeePerKb_authorizers1and2_sameVote_updateFeePerKb() { +// // Send 1 vote from an authorizer +// Transaction firstTx = getTransactionFromAuthorizedCaller(signatureCache, 1); +// Coin firstFeePerKbVote = Coin.valueOf(50_000L); +// feePerKbSupport.voteFeePerKbChange(firstTx, firstFeePerKbVote, signatureCache); +// +// // Get fee per kb, shouldn't have changed because only vote was added +// feePerKb = feePerKbSupport.getFeePerKb(); +// assertEquals(genesisFeePerKb, feePerKb); +// +// // Add a second vote from a different authorizer +// Transaction secondTx = getTransactionFromAuthorizedCaller(signatureCache, 2); +// feePerKbSupport.voteFeePerKbChange(secondTx, firstFeePerKbVote, signatureCache); +// +// // Get fee per kb, should return the newly voted value +// feePerKb = feePerKbSupport.getFeePerKb(); +// assertEquals(firstFeePerKbVote, feePerKb); +// } + + + +// @Test +// void voteFeePerKbChange_winnerFeePerKbValue_shouldReturnSuccessfulFeeVotedResponseCode() { +// SignatureCache signatureCache = mock(SignatureCache.class); +// +// //First Vote +// Transaction firstTx = getTransactionFromAuthorizedCaller(signatureCache, 1); +// Coin firstFeePerKbVote = Coin.valueOf(50_000L); +// feePerKbSupport.voteFeePerKbChange(firstTx, firstFeePerKbVote, signatureCache); +// +// //Second vote +// Transaction secondTx = getTransactionFromAuthorizedCaller(signatureCache, 2); +// Coin secondFeePerKbVote = Coin.valueOf(50_000L); +// Integer ActualResult = feePerKbSupport.voteFeePerKbChange(secondTx, secondFeePerKbVote, signatureCache); +// +// Integer expectedResult = FeePerKbResponseCode.SUCCESSFUL_VOTE.getCode(); +// assertEquals(expectedResult, ActualResult); +// } + + + private Transaction getTransactionFromAuthorizedCaller(RskAddress authorizer) { Transaction txFromAuthorizedCaller = mock(Transaction.class); - when(txFromAuthorizedCaller.getSender(signatureCache)).thenReturn(authorizedRskAddress); + when(txFromAuthorizedCaller.getSender(signatureCache)).thenReturn(authorizer); return txFromAuthorizedCaller; } From 2e77db14172f44c8182ab2c4b379376f59c9b487 Mon Sep 17 00:00:00 2001 From: Wilmer Rondon Date: Sat, 1 Jun 2024 18:33:04 -0400 Subject: [PATCH 3/6] Integration Tests for FeePerKb Making use of the newly created InMemoryStorage class, create tests simulating different real-life scenarios, avoiding using mocks as much as possible. Integration tests are written to observe the behavior simulating real-life scenarios and stressing the InMemoryStorage executing them one after another. White space before open brace - Adding white space before open brace to enhance the indentation. Improving Comments Adding 16th voting test - Authorized callers vote same current fee per KB value. Refactoring Test Integration for Fee Per KB - Adding some annotations such as @TestInstance, and @BeforeAll to help sharing state in the test. - Adding annotations such as @TestMethodOrder, and @Order to create an order of test execution. - Adding Caller enum to get the code more expressive. - After adding the Caller enum, adapt the voteFeePerKb method to receive as an In Parameter RskAddress. - Removing voteFeePerKbByUnauthorizedCaller, getAuthorizedRskAddresses, and getUnauthorizedRskAddress methods since they are no longer needed after the adaptation of the Caller enum in the code. Renaming and Moving Enum to Another Package - Renaming Enum from Caller to FeePerKbVoteCaller. - Moving Enum from utils to feeperkb package. Changing type of variable - Changing type of variable from instance to local. Renaming Test Methods - Renaming Test Methods to fit our method names for IT. - Removing JavaDocs for Methods since with new method names they are not needed anymore. Renaming Class Name - Renaming class name, to apply new naming conventions for Integration Tests(IT). This consists of adding at the end of the class name the suffix IT. Changing differentFeePerKbVote From Variable To Constant. --- .../java/co/rsk/peg/feeperkb/FeePerKbIT.java | 497 ++++++++++++++++++ .../peg/feeperkb/FeePerKbIntegrationTest.java | 180 ------- .../rsk/peg/feeperkb/FeePerKbVoteCaller.java | 24 + 3 files changed, 521 insertions(+), 180 deletions(-) create mode 100644 rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java delete mode 100644 rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java create mode 100644 rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java new file mode 100644 index 00000000000..260d300369d --- /dev/null +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java @@ -0,0 +1,497 @@ +package co.rsk.peg.feeperkb; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import co.rsk.bitcoinj.core.Coin; +import co.rsk.core.RskAddress; +import co.rsk.peg.InMemoryStorage; +import co.rsk.peg.feeperkb.constants.FeePerKbConstants; +import co.rsk.peg.feeperkb.constants.FeePerKbMainNetConstants; +import co.rsk.peg.storage.StorageAccessor; +import org.ethereum.core.SignatureCache; +import org.ethereum.core.Transaction; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import org.junit.jupiter.api.TestMethodOrder; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@TestInstance(Lifecycle.PER_CLASS) +class FeePerKbIT { + private static final Coin differentFeePerKbVote = Coin.valueOf(60_000L); + private final FeePerKbConstants feePerKbConstants = FeePerKbMainNetConstants.getInstance(); + private FeePerKbSupport feePerKbSupport; + private SignatureCache signatureCache; + private Coin currentFeePerKb; + + @BeforeAll + void setUp() { + StorageAccessor inMemoryStorage = new InMemoryStorage(); + FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(inMemoryStorage); + feePerKbSupport = new FeePerKbSupportImpl(feePerKbConstants, feePerKbStorageProvider); + signatureCache = mock(SignatureCache.class); + } + + @Test + @Order(0) + void getGenesisFeePerKb_whenNotExistsPreviousVoting_shouldReturnGenesisFeePerKbValue() { + Coin genesisFeePerKb = feePerKbConstants.getGenesisFeePerKb(); + currentFeePerKb = genesisFeePerKb; + assertFeePerKbValue(genesisFeePerKb); + } + + @Test + @Order(1) + void voteFeePerKbChange_whenAuthorizerOneAndTwoVoteSameValue_shouldSetValueVoted() { + Coin firstFeePerKbVote = Coin.valueOf(50_000L); + // Send 1 vote from an authorizer + voteFeePerKb(firstFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + // Get fee per kb, shouldn't have changed because only one vote was added + assertFeePerKbValue(currentFeePerKb); + + // Second vote from a different authorizer + voteFeePerKb(firstFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + currentFeePerKb = firstFeePerKbVote; + + // Get fee per kb, should return the newly voted value + assertFeePerKbValue(firstFeePerKbVote); + } + + @Test + @Order(2) + void voteFeePerKbChange_whenAuthorizerTwoAndThreeVoteSameValue_shouldSetValueVoted() { + Coin secondFeePerKbVote = Coin.valueOf(100_000L); + // First vote for a new value + voteFeePerKb(secondFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the first value voted because the new value has only 1 vote + assertFeePerKbValue(currentFeePerKb); + + // Second vote for the same value, different authorizer + voteFeePerKb(secondFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = secondFeePerKbVote; + + // Get fee per kb, should return the second round vote value + assertFeePerKbValue(secondFeePerKbVote); + } + + @Test + @Order(3) + void voteFeePerKbChange_whenAuthorizerOneAndTwoVoteDifferentValueAndAuthorizerVoteSameAsAuthorizerOne_shouldSetValueVotedByOneAndThree() { + Coin thirdFeePerKbVote = Coin.valueOf(40_000L); + + // First vote for a new value + voteFeePerKb(thirdFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the second voted value because the third value has only 1 vote + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a different value, different authorizer + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the second round voting value because the third value has only 2 different votes + assertFeePerKbValue(currentFeePerKb); + + // Third vote from authorizer 3, same value as the first one voted from authorizer 1 + voteFeePerKb(thirdFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = thirdFeePerKbVote; + + // Get fee per kb, should return the third vote value + assertFeePerKbValue(thirdFeePerKbVote); + } + + @Test + @Order(4) + void voteFeePerKbChange_whenAuthorizerOneAndTwoVoteSameValueAndAuthorizerThreeVotesDifferentValue_shouldSetValueVotedByOneAndTwo() { + Coin fourthFeePerKbVote = Coin.valueOf(70_000L); + + // First vote for a new value + voteFeePerKb(fourthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the third round voted value because the fourth round has only 1 vote + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a same value, different authorizer + voteFeePerKb(fourthFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + // Get fee per kb, should have changed to fourthFeePerKb vote + assertFeePerKbValue(fourthFeePerKbVote); + + // Third vote for a different value, different authorizer + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = fourthFeePerKbVote; + + // Get fee per kb, should return fourthFeePerKbVote since the third vote is considered a new round. + assertFeePerKbValue(fourthFeePerKbVote); + } + + @Test + @Order(5) + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameValueThenLastVoteOfPreviousVotingTakenAsFirstDifferentVote_shouldSetValueVotedByOneAndThree() { + Coin fifthFeePerKbVote = Coin.valueOf(80_000L); + + // Send 1 vote from an authorizer + voteFeePerKb(fifthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + // Get fee per kb, shouldn't have changed because the two first votes are different + assertFeePerKbValue(currentFeePerKb); + + // Second vote from a different authorizer + voteFeePerKb(fifthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = fifthFeePerKbVote; + + // Get fee per kb, should return the newly voted value + assertFeePerKbValue(fifthFeePerKbVote); + } + + @Test + @Order(6) + void voteFeePerKbChange_whenAuthorizerOneVotesDifferentValueAndAuthorizerTwoAndThreeVoteSameValue_shouldSetValueVotedByTwoAndThree() { + Coin sixthFeePerKbVote = Coin.valueOf(30_000L); + + // First vote for a new value + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the fifthFeePerKbVote voted value because the voting has only 1 vote + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a different value, different authorizer + voteFeePerKb(sixthFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the second round voting value because the third value has only 2 different votes + assertFeePerKbValue(currentFeePerKb); + + // Third vote from authorizer 3, same value as the second one voted + voteFeePerKb(sixthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = sixthFeePerKbVote; + + // Get fee per kb, should return sixthFeePerKbVote value + assertFeePerKbValue(sixthFeePerKbVote); + } + + @Test + @Order(7) + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameValueAndInBetweenVotesUnauthorizedCallerSameValueThanOne_shouldSetValueWithLastVoteEmittedByThree() { + Coin seventhFeePerKbVote = Coin.valueOf(40_000L); + + // First vote for a new value + voteFeePerKb(seventhFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + // Get fee per kb, should return the second voted value because the third value has only 1 vote + assertFeePerKbValue(currentFeePerKb); + + // Second vote for an unauthorized caller + voteFeePerKb(seventhFeePerKbVote, FeePerKbVoteCaller.UNAUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote from authorizer 3, same value as the first one voted from authorizer 1 + voteFeePerKb(seventhFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = seventhFeePerKbVote; + + // Get fee per kb, should return the third vote value + assertFeePerKbValue(seventhFeePerKbVote); + } + + @Test + @Order(8) + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteAfterAuthorizerOneVoteAuthorizerTwoDifferentValueAfterThatUnauthorizedCallerVotesSameThanAuthorizerTwoAndLasVoteIsEmittedByAuthorizerThree_shouldSetValueVotedByOneAndThree() { + Coin eighthFeePerKbVote = Coin.valueOf(40_000L); + + // First vote for a new value + voteFeePerKb(eighthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a new different value + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote is same than the second vote value, but it is an unauthorized caller + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.UNAUTHORIZED.getRskAddress()); + //It still has the previous voting round value, since it shouldn't change with an unauthorized caller vote + assertFeePerKbValue(currentFeePerKb); + + // Fourth vote from authorizer 3, same value as the first one voted from authorizer 1 + voteFeePerKb(eighthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = eighthFeePerKbVote; + + // Get fee per kb, should return the third vote value + assertFeePerKbValue(eighthFeePerKbVote); + } + + @Test + @Order(9) + void voteFeePerKbChange_whenUnauthorizedCallerAndAuthorizerTwoVoteSameValueThenNotChangeValueAndAuthorizerOneAndTwoVoteSameValue_shouldSetValueVotedByOneAndThree() { + Coin ninthFeePerKbVote = Coin.valueOf(90_000L); + + // First vote is by an unauthorized caller + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.UNAUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote is by authorizer 1 + voteFeePerKb(ninthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote is by authorizer 2. it votes similar then the unauthorized caller + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value, since it shouldn't change with an unauthorized caller vote + assertFeePerKbValue(currentFeePerKb); + + // Fourth vote is by authorizer 3, same value than authorizer 1 + voteFeePerKb(ninthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = ninthFeePerKbVote; + + // Get fee per kb, should return the third vote value + assertFeePerKbValue(ninthFeePerKbVote); + } + + @Test + @Order(10) + void voteFeePerKbChange_whenAuthorizerOneAndTwoVoteSameValueAndFirstTimeAuthorizerTwoVotesNegativeValueAndAuthorizerThreeVotesSameNegativeValueThanAuthorizerTwo_shouldSetSamePositiveValueVotedByOneAndTwo() { + Coin tenthFeePerKbVote = Coin.valueOf(20_000L); + + // First vote for a new value + voteFeePerKb(tenthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote, authorizer 2 votes a negative value + voteFeePerKb(Coin.NEGATIVE_SATOSHI, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote, authorizer 3, votes a negative value + voteFeePerKb(Coin.NEGATIVE_SATOSHI, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Fourth vote, authorizer 2 votes again, this time for the same value that authorizer 1 + voteFeePerKb(tenthFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + currentFeePerKb = tenthFeePerKbVote; + + // Get fee per kb, should return the value voted by authorizer 1 and 2 (second vote) + assertFeePerKbValue(tenthFeePerKbVote); + } + + @Test + @Order(11) + void voteFeePerKbChange_whenAuthorizerOneAndTwoVoteSameValueAndAuthorizerTwoVotesSameTimeExcessiveValueAndAuthorizerThreeVotesSameExcessiveValueAndSecondTimeAuthorizerTwoVotesSameThanOne_shouldSetValueVotedByOneAndTwo() { + Coin eleventhFeePerKbVote = Coin.valueOf(20_000L); + Coin maxFeePerKbVote = feePerKbConstants.getMaxFeePerKb(); + Coin excessiveFeePerKbVote = maxFeePerKbVote.add(Coin.SATOSHI); + + // First vote for a new value + voteFeePerKb(eleventhFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote, authorizer 2 votes an excessive value + voteFeePerKb(excessiveFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote, authorizer 3, votes an excessive value + voteFeePerKb(excessiveFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Fourth vote, authorizer 2 votes again, this time for the same value that authorizer 1 + voteFeePerKb(eleventhFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + currentFeePerKb = eleventhFeePerKbVote; + + // Get fee per kb, should return the value voted by authorizer 1 and 2 (second vote) + assertFeePerKbValue(eleventhFeePerKbVote); + } + + @Test + @Order(12) + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameMaxValuePermittedAndInBetweenAuthorizerTwoVoteDifferentValue_shouldSetValueVotedByOneAndThree() { + Coin twelfthFeePerKbVote = feePerKbConstants.getMaxFeePerKb(); + + // First vote for a new value + voteFeePerKb(twelfthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a different value, different authorizer + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote from authorizer 3, same value as the first one voted from authorizer 1 + voteFeePerKb(twelfthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = twelfthFeePerKbVote; + + // Get fee per kb, should return the value voted by authorizer 1 and 3 + assertFeePerKbValue(twelfthFeePerKbVote); + } + + @Test + @Order(13) + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameVeryLowValueAndInBetweenAuthorizerTwoVotesDifferentValue_shouldSetValueVotedByOneAndThree() { + Coin thirteenthFeePerKbVote = Coin.SATOSHI; + + // First vote for a new value + voteFeePerKb(thirteenthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a different value, different authorizer + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote from authorizer 3, same value as the first one voted from authorizer 1 + voteFeePerKb(thirteenthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = thirteenthFeePerKbVote; + + // Get fee per kb, should return the value voted by authorizer 1 and 3 + assertFeePerKbValue(thirteenthFeePerKbVote); + } + + @Test + @Order(14) + void voteFeePerKbChange_whenAuthorizerOneAndTwoVoteSameValidValueAndFirstTimeAuthorizerTwoVotesZeroAndAuthorizerThreeVotesSameZeroValueAndLastVoteAuthorizerTwoVotesSameThanOne_shouldSetValueVotedByOneAndTwo() { + Coin fourteenthFeePerKbVote = Coin.valueOf(10_000L); + + // First vote for a new value + voteFeePerKb(fourteenthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote, authorizer 2 votes a zero value + voteFeePerKb(Coin.ZERO, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote, authorizer 3, votes a zero value + voteFeePerKb(Coin.ZERO, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Fourth vote, authorizer 2 votes again, this time for the same value that authorizer 1 + voteFeePerKb(fourteenthFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + currentFeePerKb = fourteenthFeePerKbVote; + + // Get fee per kb, should return the value voted by authorizer 1 and 2 (second vote) + assertFeePerKbValue(fourteenthFeePerKbVote); + } + + @Test + @Order(15) + void voteFeePerKbChange_whenSeveralAuthorizersVoteSeveralTimesDifferentValuesAndLastTwoVotesByAuthorizerOneAndTwoAreEqual_shouldSetValueVotedByOneAndTwoLastVotes() { + Coin fifteenthFeePerKbVote = Coin.valueOf(130_000L); + + Coin firstVoteFeePerKb = Coin.valueOf(121_000L); + // First vote for a new value + voteFeePerKb(firstVoteFeePerKb, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + Coin secondVoteFeePerKb = Coin.valueOf(122_000L); + // Second vote for a different value, different authorizer + voteFeePerKb(secondVoteFeePerKb, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + Coin thirdVoteFeePerKb = Coin.valueOf(123_000L); + // Third vote for a different value, different authorizer + voteFeePerKb(thirdVoteFeePerKb, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Again authorized callers vote + + Coin fourthVoteFeePerKb = Coin.valueOf(124_000L); + // First vote for a new value + voteFeePerKb(fourthVoteFeePerKb, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + Coin fifthVoteFeePerKb = Coin.valueOf(125_000L); + // Second vote for a different value, different authorizer + voteFeePerKb(fifthVoteFeePerKb, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + Coin sixthVoteFeePerKb = Coin.valueOf(126_000L); + // Third vote for a different value, different authorizer + voteFeePerKb(sixthVoteFeePerKb, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Again authorized callers vote + + Coin seventhVoteFeePerKb = Coin.valueOf(127_000L); + // First vote for a new value + voteFeePerKb(seventhVoteFeePerKb, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + Coin eighthVoteFeePerKb = Coin.valueOf(128_000L); + // Second vote for a different value, different authorizer + voteFeePerKb(eighthVoteFeePerKb, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + Coin ninthVoteFeePerKb = Coin.valueOf(129_000L); + // Third vote for a different value, different authorizer + voteFeePerKb(ninthVoteFeePerKb, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Again authorized callers vote + + // First vote for a new value + voteFeePerKb(fifteenthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a same value than the previous one + voteFeePerKb(fifteenthFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + currentFeePerKb = fifteenthFeePerKbVote; + + //It still has the previous voting round value + assertFeePerKbValue(fifteenthFeePerKbVote); + } + + @Test + @Order(16) + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameCurrentFeePerKbValueAndInBetweenAuthorizerTwoVotesDifferentValue_shouldSetValueVotedByOneAndThree() { + Coin sixteenthFeePerKbVote = feePerKbSupport.getFeePerKb(); + + // First vote for a new value + voteFeePerKb(sixteenthFeePerKbVote, FeePerKbVoteCaller.FIRST_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Second vote for a different value, different authorizer + voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); + //It still has the previous voting round value + assertFeePerKbValue(currentFeePerKb); + + // Third vote from authorizer 3, same value as the first one voted from authorizer 1 + voteFeePerKb(sixteenthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); + currentFeePerKb = sixteenthFeePerKbVote; + + // Get fee per kb, should return the value voted by authorizer 1 and 3 + assertFeePerKbValue(sixteenthFeePerKbVote); + } + + private void assertFeePerKbValue(Coin feePerKbExpectedResult) { + Coin feePerKbActualResult = feePerKbSupport.getFeePerKb(); + assertEquals(feePerKbExpectedResult, feePerKbActualResult); + } + + private void voteFeePerKb(Coin feePerKb, RskAddress caller) { + Transaction voteTx = getTransactionFromCaller(caller); + feePerKbSupport.voteFeePerKbChange(voteTx, feePerKb, signatureCache); + } + + private Transaction getTransactionFromCaller(RskAddress authorizer) { + Transaction txFromAuthorizedCaller = mock(Transaction.class); + when(txFromAuthorizedCaller.getSender(signatureCache)).thenReturn(authorizer); + + return txFromAuthorizedCaller; + } +} diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java deleted file mode 100644 index b99d1c70157..00000000000 --- a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIntegrationTest.java +++ /dev/null @@ -1,180 +0,0 @@ -package co.rsk.peg.feeperkb; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import co.rsk.bitcoinj.core.Coin; -import co.rsk.core.RskAddress; -import co.rsk.peg.InMemoryStorage; -import co.rsk.peg.feeperkb.constants.FeePerKbConstants; -import co.rsk.peg.feeperkb.constants.FeePerKbMainNetConstants; -import co.rsk.peg.storage.StorageAccessor; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.ethereum.core.SignatureCache; -import org.ethereum.core.Transaction; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class FeePerKbIntegrationTest { - private final FeePerKbConstants feePerKbConstants = FeePerKbMainNetConstants.getInstance(); - private FeePerKbSupport feePerKbSupport; - private SignatureCache signatureCache; - - @BeforeEach - void setUp() { - StorageAccessor inMemoryStorage = new InMemoryStorage(); - FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(inMemoryStorage); - feePerKbSupport = new FeePerKbSupportImpl(feePerKbConstants, feePerKbStorageProvider); - signatureCache = mock(SignatureCache.class); - } - - @Test - void feePerKbIntegrationTest() { - Coin genesisFeePerKb = feePerKbConstants.getGenesisFeePerKb(); - - // Get fee per kb before any voting, should return the genesis fee per kb - assertFeePerKbValue(genesisFeePerKb); - - /* - * First voting, authorizer 1 and 2 vote the same value - */ - Coin firstFeePerKbVote = Coin.valueOf(50_000L); - // Send 1 vote from an authorizer - voteFeePerKb(firstFeePerKbVote, 0); - - // Get fee per kb, shouldn't have changed because only vote was added - assertFeePerKbValue(genesisFeePerKb); - - // Add a second vote from a different authorizer - voteFeePerKb(firstFeePerKbVote, 1); - - // Get fee per kb, should return the newly voted value - assertFeePerKbValue(firstFeePerKbVote); - - /* - * Second voting, authorizers 2 and 3 vote the same value - */ - Coin secondFeePerKbVote = Coin.valueOf(100_000L); - // First vote for a new value - voteFeePerKb(secondFeePerKbVote, 2); - - // Get fee per kb, should return the first value voted because the new value has only 1 vote - assertFeePerKbValue(firstFeePerKbVote); - - // Add a second vote for the same value, different authorizer - voteFeePerKb(secondFeePerKbVote, 1); - - // Get fee per kb, should return the second vote value - assertFeePerKbValue(secondFeePerKbVote); - - /* - * Third voting, authorizers 1 and 2 vote different values. Then authorizer 3 votes the same value as authorizer 1 - */ - Coin thirdFeePerKbVote = Coin.valueOf(40_000L); - // First vote for a new value - voteFeePerKb(thirdFeePerKbVote, 0); - - // Get fee per kb, should return the second voted value because the third value has only 1 vote - assertFeePerKbValue(secondFeePerKbVote); - - // Add a second vote for a different value, different authorizer - Coin differentFeePerKbVote = Coin.valueOf(60_000L); - voteFeePerKb(differentFeePerKbVote, 1); - - // Get fee per kb, should return the second voted value because the third value has only 2 different votes - assertFeePerKbValue(secondFeePerKbVote); - - // Add a third vote from authorizer 3, same value as the one voted from authorizer 1 - voteFeePerKb(thirdFeePerKbVote, 2); - - // Get fee per kb, should return the third vote value - assertFeePerKbValue(thirdFeePerKbVote); - - /* - * Fourth voting, authorizers 1 and 2 vote the same value. Then authorizer 3 votes a different value - */ - Coin fourthFeePerKbVote = Coin.valueOf(70_000L); - } - - private void assertFeePerKbValue(Coin expectedValue) { - Coin feePerKb = feePerKbSupport.getFeePerKb(); - assertEquals(expectedValue, feePerKb); - } - - private void voteFeePerKb(Coin value, int authorizerIndex) { - RskAddress authorizerAddress = this.getAuthorizedRskAddresses().get(authorizerIndex); - Transaction voteTx = getTransactionFromAuthorizedCaller(authorizerAddress); - feePerKbSupport.voteFeePerKbChange(voteTx, value, signatureCache); - } - - - - -// @Test -// void getFeePerKbBeforeAnyVotes_shouldReturnGenesisFeePerKb() { -// Coin genesisFeePerKb = feePerKbConstants.getGenesisFeePerKb(); -// -// // Get fee per kb before any voting, should return the genesis fee per kb -// Coin feePerKb = feePerKbSupport.getFeePerKb(); -// assertEquals(genesisFeePerKb, feePerKb); -// } -// -// @Test -// void testFeePerKb_authorizers1and2_sameVote_updateFeePerKb() { -// // Send 1 vote from an authorizer -// Transaction firstTx = getTransactionFromAuthorizedCaller(signatureCache, 1); -// Coin firstFeePerKbVote = Coin.valueOf(50_000L); -// feePerKbSupport.voteFeePerKbChange(firstTx, firstFeePerKbVote, signatureCache); -// -// // Get fee per kb, shouldn't have changed because only vote was added -// feePerKb = feePerKbSupport.getFeePerKb(); -// assertEquals(genesisFeePerKb, feePerKb); -// -// // Add a second vote from a different authorizer -// Transaction secondTx = getTransactionFromAuthorizedCaller(signatureCache, 2); -// feePerKbSupport.voteFeePerKbChange(secondTx, firstFeePerKbVote, signatureCache); -// -// // Get fee per kb, should return the newly voted value -// feePerKb = feePerKbSupport.getFeePerKb(); -// assertEquals(firstFeePerKbVote, feePerKb); -// } - - - -// @Test -// void voteFeePerKbChange_winnerFeePerKbValue_shouldReturnSuccessfulFeeVotedResponseCode() { -// SignatureCache signatureCache = mock(SignatureCache.class); -// -// //First Vote -// Transaction firstTx = getTransactionFromAuthorizedCaller(signatureCache, 1); -// Coin firstFeePerKbVote = Coin.valueOf(50_000L); -// feePerKbSupport.voteFeePerKbChange(firstTx, firstFeePerKbVote, signatureCache); -// -// //Second vote -// Transaction secondTx = getTransactionFromAuthorizedCaller(signatureCache, 2); -// Coin secondFeePerKbVote = Coin.valueOf(50_000L); -// Integer ActualResult = feePerKbSupport.voteFeePerKbChange(secondTx, secondFeePerKbVote, signatureCache); -// -// Integer expectedResult = FeePerKbResponseCode.SUCCESSFUL_VOTE.getCode(); -// assertEquals(expectedResult, ActualResult); -// } - - - private Transaction getTransactionFromAuthorizedCaller(RskAddress authorizer) { - Transaction txFromAuthorizedCaller = mock(Transaction.class); - when(txFromAuthorizedCaller.getSender(signatureCache)).thenReturn(authorizer); - - return txFromAuthorizedCaller; - } - - private List getAuthorizedRskAddresses(){ - return Stream.of( - "a02db0ed94a5894bc6f9079bb9a2d93ada1917f3", - "180a7edda4e640ea5a3e495e17a1efad260c39e9", - "8418edc8fea47183116b4c8cd6a12e51a7e169c1" - ).map(RskAddress::new).collect(Collectors.toList()); - } -} diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java new file mode 100644 index 00000000000..7175cf2f214 --- /dev/null +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java @@ -0,0 +1,24 @@ +package co.rsk.peg.feeperkb; + +import co.rsk.core.RskAddress; + +/** + * Created to makes easier the way to get RskAddress of 3 authorized and 1 unauthorized Callers. It + * contains hex encoded string associated to each caller and return the RskAddress of each one. + */ +public enum FeePerKbVoteCaller { + FIRST_AUTHORIZED("a02db0ed94a5894bc6f9079bb9a2d93ada1917f3"), + SECOND_AUTHORIZED("180a7edda4e640ea5a3e495e17a1efad260c39e9"), + THIRD_AUTHORIZED("8418edc8fea47183116b4c8cd6a12e51a7e169c1"), + UNAUTHORIZED("e2a5070b4e2cb77fe22dff05d9dcdc4d3eaa6ead"); + + private final String RskAddress; + + FeePerKbVoteCaller(String rskAddress) { + RskAddress = rskAddress; + } + + public RskAddress getRskAddress() { + return new RskAddress(RskAddress); + } +} From 77666c7613ab0d3c0ad5164be610d8d2f92e2a9a Mon Sep 17 00:00:00 2001 From: Wilmer Rondon Date: Mon, 24 Jun 2024 11:03:08 -0400 Subject: [PATCH 4/6] Fixing Naming Convention For A Variable Name - Fixing naming convention for a variable name to use camel case. Improving Comment --- rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java | 2 +- .../test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java index 260d300369d..756ab57fa31 100644 --- a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java @@ -232,7 +232,7 @@ void voteFeePerKbChange_whenUnauthorizedCallerAndAuthorizerTwoVoteSameValueThenN //It still has the previous voting round value assertFeePerKbValue(currentFeePerKb); - // Third vote is by authorizer 2. it votes similar then the unauthorized caller + // Third vote is by authorizer 2, who votes the same as the unauthorized caller voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); //It still has the previous voting round value, since it shouldn't change with an unauthorized caller vote assertFeePerKbValue(currentFeePerKb); diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java index 7175cf2f214..af479eff244 100644 --- a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbVoteCaller.java @@ -12,13 +12,13 @@ public enum FeePerKbVoteCaller { THIRD_AUTHORIZED("8418edc8fea47183116b4c8cd6a12e51a7e169c1"), UNAUTHORIZED("e2a5070b4e2cb77fe22dff05d9dcdc4d3eaa6ead"); - private final String RskAddress; + private final String rskAddress; FeePerKbVoteCaller(String rskAddress) { - RskAddress = rskAddress; + this.rskAddress = rskAddress; } public RskAddress getRskAddress() { - return new RskAddress(RskAddress); + return new RskAddress(rskAddress); } } From b0faaed3804703d6c6565f41e1d325ce60213fe1 Mon Sep 17 00:00:00 2001 From: Wilmer Rondon Date: Mon, 24 Jun 2024 13:04:25 -0400 Subject: [PATCH 5/6] Improving Method Name Improving Method Name for @Order(8) --- rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java index 756ab57fa31..031e61e2091 100644 --- a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java @@ -191,7 +191,7 @@ void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameValueAndInBetweenVotesU @Test @Order(8) - void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteAfterAuthorizerOneVoteAuthorizerTwoDifferentValueAfterThatUnauthorizedCallerVotesSameThanAuthorizerTwoAndLasVoteIsEmittedByAuthorizerThree_shouldSetValueVotedByOneAndThree() { + void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameValueThenInBetweenAuthorizerTwoAndUnauthorizedCallerVoteSameValue_shouldSetValueVotedByOneAndThree() { Coin eighthFeePerKbVote = Coin.valueOf(40_000L); // First vote for a new value @@ -457,7 +457,7 @@ void voteFeePerKbChange_whenSeveralAuthorizersVoteSeveralTimesDifferentValuesAnd @Test @Order(16) - void voteFeePerKbChange_whenAuthorizerOneAndThreeVoteSameCurrentFeePerKbValueAndInBetweenAuthorizerTwoVotesDifferentValue_shouldSetValueVotedByOneAndThree() { + void voteFeePerKbChange_whenVotingSameCurrentFeePerKbValue_shouldSetValueVoted() { Coin sixteenthFeePerKbVote = feePerKbSupport.getFeePerKb(); // First vote for a new value From 6f6282734590b86f71dc44b69c5537e864bd8f12 Mon Sep 17 00:00:00 2001 From: Wilmer Rondon Date: Mon, 24 Jun 2024 15:06:53 -0400 Subject: [PATCH 6/6] Adding a comment to make clearer the use of currentFeePerKb variable. Removing Unnecessary Comment Removing Unnecessary Vote - Removing unnecessary vote since it doesn't help to go straight to the point to the sense of the test method. --- .../src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java index 031e61e2091..f4f2a27ec81 100644 --- a/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/feeperkb/FeePerKbIT.java @@ -27,7 +27,7 @@ class FeePerKbIT { private final FeePerKbConstants feePerKbConstants = FeePerKbMainNetConstants.getInstance(); private FeePerKbSupport feePerKbSupport; private SignatureCache signatureCache; - private Coin currentFeePerKb; + private Coin currentFeePerKb; // it is used to guarantee the value from getFeePerKb() contains the value expected @BeforeAll void setUp() { @@ -465,12 +465,7 @@ void voteFeePerKbChange_whenVotingSameCurrentFeePerKbValue_shouldSetValueVoted() //It still has the previous voting round value assertFeePerKbValue(currentFeePerKb); - // Second vote for a different value, different authorizer - voteFeePerKb(differentFeePerKbVote, FeePerKbVoteCaller.SECOND_AUTHORIZED.getRskAddress()); - //It still has the previous voting round value - assertFeePerKbValue(currentFeePerKb); - - // Third vote from authorizer 3, same value as the first one voted from authorizer 1 + // Second vote from authorizer 3 is the same as first vote voteFeePerKb(sixteenthFeePerKbVote, FeePerKbVoteCaller.THIRD_AUTHORIZED.getRskAddress()); currentFeePerKb = sixteenthFeePerKbVote;