Skip to content

Commit

Permalink
Refactor to use a common method to top up tester's balance
Browse files Browse the repository at this point in the history
resolve test failure in MessageTest.handleRelayMessageShouldRevertUnauthorized()
  • Loading branch information
sink772 committed Jul 14, 2023
1 parent ff87013 commit d45d7b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
20 changes: 20 additions & 0 deletions bmc/src/test/java/foundation/icon/btp/bmc/BMCIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
import foundation.icon.btp.lib.BMCScoreClient;
import foundation.icon.btp.lib.BTPAddress;
import foundation.icon.btp.lib.OwnerManagerScoreClient;
import foundation.icon.btp.mock.ChainScore;
import foundation.icon.btp.mock.ChainScoreClient;
import foundation.icon.btp.test.BTPIntegrationTest;
import foundation.icon.jsonrpc.Address;
import foundation.icon.jsonrpc.model.TransactionResult;
import foundation.icon.score.test.ScoreIntegrationTest;

import java.math.BigInteger;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -41,6 +45,22 @@ public interface BMCIntegrationTest extends BTPIntegrationTest {
ICONSpecificScoreClient iconSpecificWithTester = new ICONSpecificScoreClient(bmcWithTester);
OwnerManagerScoreClient ownerManagerWithTester = new OwnerManagerScoreClient(bmcWithTester);

static void topUpTesterBalance() {
// the caller should have enough balance more than StepLimit * StepPrice
ChainScoreClient chainScore = new ChainScoreClient(
client.endpoint(),
client._nid(),
client._wallet(),
new Address(ChainScore.ADDRESS));
BigInteger stepPrice = chainScore.getStepPrice();
BigInteger minBalance = client._stepLimit().multiply(stepPrice);
Address testerAddress = tester.getAddress();
if (client._balance(testerAddress).compareTo(minBalance) < 0) {
client._transfer(testerAddress, minBalance.multiply(BigInteger.TEN), null);
System.out.println("transferred "+testerAddress + ":" + client._balance(testerAddress));
}
}

static <T> Consumer<TransactionResult> eventLogChecker(
ScoreIntegrationTest.EventLogsSupplier<T> supplier, Consumer<T> consumer, Predicate<T> filter) {
return ScoreIntegrationTest.eventLogChecker(
Expand Down
2 changes: 2 additions & 0 deletions bmc/src/test/java/foundation/icon/btp/bmc/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ static void ensureReachable(BTPAddress link, BTPAddress[] reachable) {
@BeforeAll
static void beforeAll() {
System.out.println("MessageTest:beforeAll start");
BMCIntegrationTest.topUpTesterBalance();

bmc.setDumpJson(true);
BMVManagementTest.addVerifier(link.net(), MockBMVIntegrationTest.mockBMV._address());
LinkManagementTest.addLink(link.toString());
Expand Down
19 changes: 1 addition & 18 deletions bmc/src/test/java/foundation/icon/btp/bmc/OwnershipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package foundation.icon.btp.bmc;

import foundation.icon.btp.mock.ChainScore;
import foundation.icon.btp.mock.ChainScoreClient;
import foundation.icon.jsonrpc.Address;
import foundation.icon.score.test.ScoreIntegrationTest;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -35,8 +33,6 @@ public class OwnershipTest implements BMCIntegrationTest {
static String string = "";
static String btpAddress = Faker.btpLink().toString();
static String netAddress = Faker.btpNetwork();
static int intVal = 0;
static long longVal = 0;
static BigInteger bigInteger = BigInteger.ZERO;
static long longPositiveNumber = 1;

Expand Down Expand Up @@ -64,20 +60,7 @@ static void clearOwner(Address address) {
@BeforeAll
static void beforeAll() {
System.out.println("OwnershipTest:beforeAll start");

//the caller should have enough balance more than StepLimit * StepPrice
ChainScoreClient chainScore = new ChainScoreClient(
client.endpoint(),
client._nid(),
client._wallet(),
new Address(ChainScore.ADDRESS));
BigInteger stepPrice = chainScore.getStepPrice();
BigInteger minBalance = client._stepLimit().multiply(stepPrice);
Address testerAddress = tester.getAddress();
if (client._balance(testerAddress).compareTo(minBalance) < 0) {
client._transfer(testerAddress, minBalance.multiply(BigInteger.TEN), null);
System.out.println("transferred "+testerAddress + ":" + client._balance(testerAddress));
}
BMCIntegrationTest.topUpTesterBalance();
System.out.println("OwnershipTest:beforeAll start");
}

Expand Down
16 changes: 2 additions & 14 deletions bmc/src/test/java/foundation/icon/btp/bmc/RewardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import foundation.icon.btp.lib.BTPAddress;
import foundation.icon.btp.lib.BTPException;
import foundation.icon.btp.mock.ChainScore;
import foundation.icon.btp.mock.ChainScoreClient;
import foundation.icon.btp.test.AssertBTPException;
import foundation.icon.btp.test.BTPIntegrationTest;
import foundation.icon.btp.test.MockBMVIntegrationTest;
Expand Down Expand Up @@ -51,6 +49,8 @@ public class RewardTest implements BMCIntegrationTest {
@BeforeAll
static void beforeAll() {
System.out.println("RewardTest:beforeAll start");
BMCIntegrationTest.topUpTesterBalance();

bmc.setDumpJson(true);
BMVManagementTest.addVerifier(link.net(), MockBMVIntegrationTest.mockBMV._address());
LinkManagementTest.addLink(link.toString());
Expand All @@ -61,18 +61,6 @@ static void beforeAll() {

FeeManagementTest.setFeeTable(linkFee);

//To transfer, BMC should have enough balance more than contractCallStep * StepPrice
ChainScoreClient chainScore = new ChainScoreClient(
client.endpoint(),
client._nid(),
client._wallet(),
new Address(ChainScore.ADDRESS));
BigInteger stepPrice = chainScore.getStepPrice();
BigInteger minBalance = chainScore.getStepCost("contractCall").multiply(stepPrice);
if (client._balance(bmc._address()).compareTo(minBalance) < 0) {
client._transfer(bmc._address(), minBalance, null);
System.out.println("transferred " + bmc._address() + ":" + client._balance(bmc._address()));
}
System.out.println("RewardTest:beforeAll end");
}

Expand Down

0 comments on commit d45d7b3

Please sign in to comment.