Skip to content

Commit

Permalink
Simplify configuration of blockchains for example code plus add WS UR…
Browse files Browse the repository at this point in the history
…I messaging manager interface (#121)

* Refactor messaging manager group code to use a common base class. Add ability to store and get WS APIs
* Pass around BlockchainConfig objects rather than individual pieces of configuration as many parameters. Add ability to determine WS URI from MessagingManagerInterface.
* Token supply is not needed for minter burner contract
  • Loading branch information
drinkcoffee authored May 20, 2022
1 parent 51be365 commit 7ed2600
Show file tree
Hide file tree
Showing 71 changed files with 428 additions and 709 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,8 @@ public class TwentyActsManager extends AbstractBlockchain implements CrossContro

private TwentyActs twentyActs;

public TwentyActsManager(
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
public TwentyActsManager(Credentials credentials, BlockchainConfig bcConfig) throws IOException {
super(credentials, bcConfig);
}

public void deploy20ActsContract(BigInteger withdrawWaitPeriod, String infrastructureAccount)
Expand Down Expand Up @@ -261,9 +255,7 @@ public Tuple<TransactionReceipt, byte[], TwentyActs.PrepareOnSourceEventResponse
}

public TwentyActsManager forUser(Credentials user) throws IOException {
TwentyActsManager bc =
new TwentyActsManager(
user, this.blockchainId, this.uri, this.gasPriceStrategy, this.pollingInterval);
TwentyActsManager bc = new TwentyActsManager(user, this.blockchainConfig);
bc.loadCbcContract(this.twentyActs.getContractAddress());
return bc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ public void addBlockchainAndDeployContracts(
LOG.debug("Deploying Cross-Blockchain Control contract for blockchain id {}", blockchainId);

BcHolder holder = new BcHolder();
holder.cbc =
new TwentyActsManager(
creds,
bcInfo.bcId,
bcInfo.blockchainNodeRpcUri,
bcInfo.gasPriceStrategy,
bcInfo.period);
holder.cbc = new TwentyActsManager(creds, bcInfo);
holder.cbc.deployCbcContract();
holder.cbcContractAddress = holder.cbc.getCbcContractAddress();
holder.ver = messageVerification;
Expand All @@ -72,13 +66,7 @@ public void addBlockchainAndLoadCbcContract(
}

BcHolder holder = new BcHolder();
holder.cbc =
new TwentyActsManager(
creds,
bcInfo.bcId,
bcInfo.blockchainNodeRpcUri,
bcInfo.gasPriceStrategy,
bcInfo.period);
holder.cbc = new TwentyActsManager(creds, bcInfo);

holder.cbc.loadCbcContract(cbcAddress);
holder.cbcContractAddress = cbcAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,7 @@ protected void deployAndSetup() throws Exception {
this.withdrawalWaitTime = BigInteger.valueOf(DEFAULT_WITHDRAWAL_WAIT_TIME);

// Set-up Chain A
this.depChainAErc20 =
new ERC20Manager(
deployerCredsA,
chainA.bcId,
chainA.blockchainNodeRpcUri,
chainA.gasPriceStrategy,
chainA.period);
this.depChainAErc20 = new ERC20Manager(deployerCredsA, chainA);
this.depChainA20Acts = (TwentyActsManager) managerGroup.getCbcManager(chainA.bcId);
this.depChainA20Acts.setWithdrawalTime(withdrawalWaitTime);
this.depChainA20Acts.setInfBenficiary(infCreds.getAddress());
Expand All @@ -130,13 +124,7 @@ protected void deployAndSetup() throws Exception {
chainAName, chainASymbol, chainAInitialSupply, chainAErc20Owner.getAddress());

// Set-up Chain B
this.depChainBErc20 =
new ERC20Manager(
deployerCredsB,
chainB.bcId,
chainB.blockchainNodeRpcUri,
chainB.gasPriceStrategy,
chainB.period);
this.depChainBErc20 = new ERC20Manager(deployerCredsB, chainB);
this.depChainB20Acts = (TwentyActsManager) managerGroup.getCbcManager(chainB.bcId);
this.depChainB20Acts.setWithdrawalTime(withdrawalWaitTime);
this.depChainB20Acts.setInfBenficiary(infCreds.getAddress());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ public class ERC20Manager extends AbstractBlockchain {

private ERC20PresetFixedSupply erc20;

public ERC20Manager(
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
public ERC20Manager(final Credentials credentials, final BlockchainConfig bcConfig)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
super(credentials, bcConfig);
}

public void deployErc20Contract(
Expand Down Expand Up @@ -79,9 +74,7 @@ public BigInteger balanceOf(String account) throws Exception {
}

public ERC20Manager forUser(Credentials user) throws IOException {
ERC20Manager bc =
new ERC20Manager(
user, this.blockchainId, this.uri, this.gasPriceStrategy, this.pollingInterval);
ERC20Manager bc = new ERC20Manager(user, this.blockchainConfig);
bc.loadErc20Contract(this.erc20.getContractAddress());
return bc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ public static void main(String[] args) throws Exception {

// Set-up classes to manage blockchains.
Credentials appCreds = CredentialsCreator.createCredentials();
RootBc rootBlockchain =
new RootBc(
appCreds, root.bcId, root.blockchainNodeRpcUri, root.gasPriceStrategy, root.period);
OtherBc otherBlockchain =
new OtherBc(appCreds, bc2.bcId, bc2.blockchainNodeRpcUri, bc2.gasPriceStrategy, bc2.period);
RootBc rootBlockchain = new RootBc(appCreds, root);
OtherBc otherBlockchain = new OtherBc(appCreds, bc2);

// Deploy application contracts.
BlockchainId otherBcId = otherBlockchain.getBlockchainId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import java.io.IOException;
import java.math.BigInteger;
import net.consensys.gpact.common.AbstractBlockchain;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import net.consensys.gpact.common.BlockchainConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -28,14 +27,9 @@ public class OtherBc extends AbstractBlockchain {

OtherBlockchainContract otherBlockchainContract;

public OtherBc(
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
public OtherBc(final Credentials credentials, final BlockchainConfig bcConfig)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
super(credentials, bcConfig);
}

public void deployContracts(String cbcContractAddress) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import java.io.IOException;
import java.math.BigInteger;
import net.consensys.gpact.common.AbstractBlockchain;
import net.consensys.gpact.common.BlockchainConfig;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -28,14 +28,8 @@ public class RootBc extends AbstractBlockchain {

RootBlockchainContract rootBlockchainContract;

public RootBc(
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
public RootBc(final Credentials credentials, final BlockchainConfig bcConfig) throws IOException {
super(credentials, bcConfig);
}

public void deployContracts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;

public class TokenBridge {
static final Logger LOG = LogManager.getLogger(TokenBridge.class);
public class ERC20TokenBridgeExample {
static final Logger LOG = LogManager.getLogger(ERC20TokenBridgeExample.class);

public static final int NUM_TIMES_EXECUTE = 2;

Expand Down Expand Up @@ -52,22 +52,10 @@ public static void main(String[] args) throws Exception {
Credentials erc20OwnerCreds = CredentialsCreator.createCredentials();
SourceAndDestinationBlockchain chainA =
new SourceAndDestinationBlockchain(
"ChainA",
BigInteger.valueOf(CHAIN_A_TOKEN_SUPPLY),
erc20OwnerCreds,
root.bcId,
root.blockchainNodeRpcUri,
root.gasPriceStrategy,
root.period);
"ChainA", BigInteger.valueOf(CHAIN_A_TOKEN_SUPPLY), erc20OwnerCreds, root);
SourceAndDestinationBlockchain chainB =
new SourceAndDestinationBlockchain(
"ChainB",
BigInteger.valueOf(CHAIN_B_TOKEN_SUPPLY),
erc20OwnerCreds,
bc2.bcId,
bc2.blockchainNodeRpcUri,
bc2.gasPriceStrategy,
bc2.period);
"ChainB", BigInteger.valueOf(CHAIN_B_TOKEN_SUPPLY), erc20OwnerCreds, bc2);

// Deploy application contracts.
BlockchainId chainABcId = chainA.getBlockchainId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

import java.io.IOException;
import java.math.BigInteger;
import net.consensys.gpact.common.AbstractBlockchain;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import net.consensys.gpact.common.RevertReason;
import net.consensys.gpact.common.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -36,14 +33,11 @@ public class SourceAndDestinationBlockchain extends AbstractBlockchain {

public SourceAndDestinationBlockchain(
final String entity,
BigInteger tokenSupply,
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
final BigInteger tokenSupply,
final Credentials credentials,
final BlockchainConfig bcConfig)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
super(credentials, bcConfig);
this.entity = entity;
this.tokenSupply = tokenSupply;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class Erc20BridgeTest extends AbstractExampleTest {
@Test
public void directSignSerialMultiBlockchain() throws Exception {
String tempPropsFile = createPropertiesFile(true, true, false);
TokenBridge.main(new String[] {tempPropsFile});
ERC20TokenBridgeExample.main(new String[] {tempPropsFile});
}

@Test
public void transferSignSerialMultiBlockchain() throws Exception {
String tempPropsFile = createPropertiesFile(false, true, false);
TokenBridge.main(new String[] {tempPropsFile});
ERC20TokenBridgeExample.main(new String[] {tempPropsFile});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import java.io.IOException;
import java.math.BigInteger;
import net.consensys.gpact.common.AbstractBlockchain;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import net.consensys.gpact.common.BlockchainConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -33,14 +32,9 @@ public class EntityBase extends AbstractBlockchain {
public String entity;

public EntityBase(
final String entity,
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
final String entity, final Credentials credentials, final BlockchainConfig bcConfig)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
super(credentials, bcConfig);
this.entity = entity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import net.consensys.gpact.common.BlockchainConfig;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -36,10 +36,8 @@ public class EntityHotel extends EntityBase {

private Hotel hotelContract;

public EntityHotel(
BlockchainId bcId, String uri, DynamicGasProvider.Strategy gasPriceStrategy, int blockPeriod)
throws IOException {
super(NAME, Credentials.create(PKEY), bcId, uri, gasPriceStrategy, blockPeriod);
public EntityHotel(final BlockchainConfig bcConfig) throws IOException {
super(NAME, Credentials.create(PKEY), bcConfig);
}

public void deployContracts(String cbcAddress) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import net.consensys.gpact.common.BlockchainConfig;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -35,10 +35,8 @@ public class EntityTrain extends EntityBase {

private Train trainContract;

public EntityTrain(
BlockchainId bcId, String uri, DynamicGasProvider.Strategy gasPriceStrategy, int blockPeriod)
throws IOException {
super(NAME, Credentials.create(PKEY), bcId, uri, gasPriceStrategy, blockPeriod);
public EntityTrain(final BlockchainConfig bcConfig) throws IOException {
super(NAME, Credentials.create(PKEY), bcConfig);
}

public void deployContracts(String cbcAddress) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public class EntityTravelAgency extends AbstractBlockchain {

CrossControlManagerGroup crossControlManagerGroup;

public EntityTravelAgency(
BlockchainId bcId, String uri, DynamicGasProvider.Strategy gasPriceStrategy, int blockPeriod)
throws IOException {
super(Credentials.create(PKEY), bcId, uri, gasPriceStrategy, blockPeriod);
public EntityTravelAgency(final BlockchainConfig bcConfig) throws IOException {
super(Credentials.create(PKEY), bcConfig);
}

public void deploy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ public static void main(String[] args) throws Exception {
exampleManager.getCrossControlManagerGroup();

// Set-up classes to manage blockchains.
EntityHotel hotel =
new EntityHotel(bc2.bcId, bc2.blockchainNodeRpcUri, bc2.gasPriceStrategy, bc2.period);
EntityTrain train =
new EntityTrain(bc3.bcId, bc3.blockchainNodeRpcUri, bc3.gasPriceStrategy, bc3.period);
EntityTravelAgency travelAgency =
new EntityTravelAgency(
root.bcId, root.blockchainNodeRpcUri, root.gasPriceStrategy, root.period);
EntityHotel hotel = new EntityHotel(bc2);
EntityTrain train = new EntityTrain(bc3);
EntityTravelAgency travelAgency = new EntityTravelAgency(root);

// Deploy application contracts.
BlockchainId hotelBcId = hotel.getBlockchainId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import java.math.BigInteger;
import java.util.List;
import net.consensys.gpact.common.AbstractBlockchain;
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.DynamicGasProvider;
import net.consensys.gpact.common.BlockchainConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Credentials;
Expand All @@ -30,14 +29,9 @@ public class Bc1ContractA extends AbstractBlockchain {

ContractA contractA;

public Bc1ContractA(
Credentials credentials,
BlockchainId bcId,
String uri,
DynamicGasProvider.Strategy gasPriceStrategy,
int blockPeriod)
public Bc1ContractA(final Credentials credentials, final BlockchainConfig bcConfig)
throws IOException {
super(credentials, bcId, uri, gasPriceStrategy, blockPeriod);
super(credentials, bcConfig);
}

public void deployContracts(
Expand Down
Loading

0 comments on commit 7ed2600

Please sign in to comment.