Skip to content

Commit

Permalink
SDK Function Call API + main class for SDK (Consensys#91)
Browse files Browse the repository at this point in the history
Create Function Call layer API plus SDK main class
  • Loading branch information
drinkcoffee authored Feb 3, 2022
1 parent 90ea172 commit 5330ea5
Show file tree
Hide file tree
Showing 58 changed files with 1,320 additions and 763 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.consensys.gpact.functioncall.gpact.calltree;
package net.consensys.gpact.functioncall.common;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -7,6 +7,8 @@
import net.consensys.gpact.common.BlockchainId;
import net.consensys.gpact.common.Tuple;
import net.consensys.gpact.common.test.AbstractWeb3Test;
import net.consensys.gpact.functioncall.CallExecutionTree;
import net.consensys.gpact.functioncall.CallExecutionTreeException;
import org.junit.jupiter.api.Test;

public abstract class CallExecutionTreeTestCommon extends AbstractWeb3Test {
Expand Down Expand Up @@ -48,24 +50,24 @@ public abstract class CallExecutionTreeTestCommon extends AbstractWeb3Test {
String function8 = "0x410203040a010203040506070809aa09";

abstract Tuple<BigInteger, String, String> extractFunction(byte[] encodedCallTree, int[] callPath)
throws CallTreeException;
throws CallExecutionTreeException;

@Test
public void singleFunc() throws CallTreeException {
public void singleFunc() throws CallExecutionTreeException {
CallExecutionTree seg = new CallExecutionTree(blockchainId1, contract1, function1);
seg.dump();
seg.toString();
}

@Test
public void singleFuncNoFuncData() throws CallTreeException {
public void singleFuncNoFuncData() throws CallExecutionTreeException {
CallExecutionTree seg = new CallExecutionTree(blockchainId1, contract1, noFuncData);
seg.dump();
seg.toString();
}

@Test
public void singleFuncOnlyFunctionSelector() throws CallTreeException {
public void singleFuncOnlyFunctionSelector() throws CallExecutionTreeException {
CallExecutionTree seg = new CallExecutionTree(blockchainId1, contract1, onlyFunctionSelection);
seg.dump();
seg.toString();
}

@Test
Expand All @@ -74,7 +76,7 @@ public void rootOneSeg() throws Exception {
ArrayList<CallExecutionTree> rootCalls1 = new ArrayList<>();
rootCalls1.add(seg);
CallExecutionTree root = new CallExecutionTree(blockchainId1, contract1, function1, rootCalls1);
root.dump();
root.toString();

byte[] encoded = root.encode();
Tuple<BigInteger, String, String> func = extractFunction(encoded, new int[] {0});
Expand All @@ -96,7 +98,7 @@ public void rootTwoSeg() throws Exception {
rootCalls1.add(seg1);
rootCalls1.add(seg2);
CallExecutionTree root = new CallExecutionTree(blockchainId1, contract1, function1, rootCalls1);
root.dump();
root.toString();

byte[] encoded = root.encode();
Tuple<BigInteger, String, String> func = extractFunction(encoded, new int[] {0});
Expand Down Expand Up @@ -137,7 +139,7 @@ public void complex() throws Exception {
rootCalls1.add(seg6);
rootCalls1.add(seg7);
CallExecutionTree root = new CallExecutionTree(blockchainId1, contract1, function1, rootCalls1);
root.dump();
root.toString();

byte[] encoded = root.encode();
Tuple<BigInteger, String, String> func = extractFunction(encoded, new int[] {0});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package net.consensys.gpact.functioncall.gpact.calltree;
package net.consensys.gpact.functioncall.common;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import net.consensys.gpact.common.FormatConversion;
import net.consensys.gpact.common.Tuple;
import net.consensys.gpact.functioncall.CallExecutionTreeException;
import net.consensys.gpact.soliditywrappers.functioncall.gpact.CallExecutionTreeTest;
import org.junit.jupiter.api.BeforeEach;
import org.web3j.tuples.generated.Tuple3;
Expand All @@ -14,7 +15,7 @@ public class SolidityCallExecutionTreeTest extends CallExecutionTreeTestCommon {
CallExecutionTreeTest callExecutionTreeContract;

Tuple<BigInteger, String, String> extractFunction(byte[] encodedCallTree, int[] callPath)
throws CallTreeException {
throws CallExecutionTreeException {
List<BigInteger> callPathB = new ArrayList<>();
for (int callPathElement : callPath) {
callPathB.add(BigInteger.valueOf(callPathElement));
Expand All @@ -30,7 +31,7 @@ Tuple<BigInteger, String, String> extractFunction(byte[] encodedCallTree, int[]
result.component2(),
FormatConversion.byteArrayToString(result.component3()));
} catch (Exception ex) {
throw new CallTreeException(ex.toString());
throw new CallExecutionTreeException(ex.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import net.consensys.gpact.common.*;
import net.consensys.gpact.examples.gpact.conditional.sim.SimOtherContract;
import net.consensys.gpact.examples.gpact.conditional.sim.SimRootContract;
import net.consensys.gpact.functioncall.gpact.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.gpact.CrosschainExecutor;
import net.consensys.gpact.functioncall.gpact.calltree.CallExecutionTree;
import net.consensys.gpact.functioncall.gpact.engine.ExecutionEngine;
import net.consensys.gpact.functioncall.CallExecutionTree;
import net.consensys.gpact.functioncall.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.CrosschainCallResult;
import net.consensys.gpact.helpers.CredentialsCreator;
import net.consensys.gpact.helpers.ExecutionEngineType;
import net.consensys.gpact.helpers.GpactExampleSystemManager;
Expand Down Expand Up @@ -50,10 +49,10 @@ public static void main(String[] args) throws Exception {
"This example will not work with a paralell execution engine as it has two segments that interact with the same contract on the same blockchain");
}

BlockchainInfo root = exampleManager.getRootBcInfo();
BlockchainInfo bc2 = exampleManager.getBc2Info();
BlockchainConfig root = exampleManager.getRootBcInfo();
BlockchainConfig bc2 = exampleManager.getBc2Info();
CrossControlManagerGroup crossControlManagerGroup =
exampleManager.getGpactCrossControlManagerGroup();
exampleManager.getCrossControlManagerGroup();

// Set-up classes to manage blockchains.
Credentials appCreds = CredentialsCreator.createCredentials();
Expand Down Expand Up @@ -138,13 +137,12 @@ public static void main(String[] args) throws Exception {
byte[] encoded = rootCall.encode();
LOG.info(CallExecutionTree.dump(encoded));

CrosschainExecutor executor = new CrosschainExecutor(crossControlManagerGroup);
ExecutionEngine executionEngine = exampleManager.getExecutionEngine(executor);
CrosschainCallResult result =
crossControlManagerGroup.executeCrosschainCall(
exampleManager.getExecutionEngine(), rootCall, 300);

boolean success = executionEngine.execute(rootCall, 300);

LOG.info("Success: {}", success);
if (success) {
LOG.info("Success: {}", result.isSuccessful());
if (result.isSuccessful()) {
LOG.info(
" Simulated expected values: Root val1: {}, val2: {}, Other val: {}",
simRootContract.getVal1(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import net.consensys.gpact.CrosschainProtocols;
import net.consensys.gpact.common.*;
import net.consensys.gpact.functioncall.gpact.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.gpact.CrosschainExecutor;
import net.consensys.gpact.functioncall.gpact.calltree.CallExecutionTree;
import net.consensys.gpact.functioncall.gpact.engine.ExecutionEngine;
import net.consensys.gpact.functioncall.gpact.engine.SerialExecutionEngine;
import net.consensys.gpact.functioncall.CallExecutionTree;
import net.consensys.gpact.functioncall.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.CrosschainCallResult;
import net.consensys.gpact.helpers.CredentialsCreator;
import net.consensys.gpact.messaging.MessagingVerificationInterface;
import net.consensys.gpact.soliditywrappers.examples.gpact.erc20bridge.GpactERC20Bridge;
Expand Down Expand Up @@ -43,8 +41,8 @@ public class Erc20User {

private CrossControlManagerGroup crossControlManagerGroup;

private BlockchainInfo bcInfoA;
private BlockchainInfo bcInfoB;
private BlockchainConfig bcInfoA;
private BlockchainConfig bcInfoB;

protected Erc20User(
String name,
Expand All @@ -68,18 +66,19 @@ protected Erc20User(
}

public void createCbcManager(
BlockchainInfo bcInfoA,
List<String> infrastructureContractAddressOnBcA,
BlockchainConfig bcInfoA,
String cbcContractAddressOnBcA,
MessagingVerificationInterface msgVerA,
BlockchainInfo bcInfoB,
List<String> infrastructureContractAddressOnBcB,
BlockchainConfig bcInfoB,
String cbcContractAddressOnBcB,
MessagingVerificationInterface msgVerB)
throws Exception {
this.crossControlManagerGroup = new CrossControlManagerGroup();
this.crossControlManagerGroup.addBlockchainAndLoadContracts(
this.creds, bcInfoA, infrastructureContractAddressOnBcA, msgVerA);
this.crossControlManagerGroup.addBlockchainAndLoadContracts(
this.creds, bcInfoB, infrastructureContractAddressOnBcB, msgVerB);
this.crossControlManagerGroup =
CrosschainProtocols.getFunctionCallInstance(CrosschainProtocols.GPACT).get();
this.crossControlManagerGroup.addBlockchainAndLoadCbcContract(
this.creds, bcInfoA, cbcContractAddressOnBcA, msgVerA);
this.crossControlManagerGroup.addBlockchainAndLoadCbcContract(
this.creds, bcInfoB, cbcContractAddressOnBcB, msgVerB);

this.bcInfoA = bcInfoA;
this.bcInfoB = bcInfoB;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void transfer(boolean fromAToB, int amountInt) throws Exception {
fromAToB ? this.addressOfERC20BcB : this.addressOfERC20BcA;
String sourceERC20ContractAddress = fromAToB ? this.addressOfERC20BcA : this.addressOfERC20BcB;

BlockchainInfo bcInfo = fromAToB ? this.bcInfoA : this.bcInfoB;
BlockchainConfig bcInfo = fromAToB ? this.bcInfoA : this.bcInfoB;

final int RETRY = 20;
Web3j web3j =
Expand Down Expand Up @@ -162,15 +161,12 @@ public void transfer(boolean fromAToB, int amountInt) throws Exception {
byte[] encoded = root.encode();
LOG.info(CallExecutionTree.dump(encoded));

CrosschainExecutor executor = new CrosschainExecutor(this.crossControlManagerGroup);
// Note: There is no point using a parallel execution engine: there is nothing to execute in
// parallel!
ExecutionEngine executionEngine = new SerialExecutionEngine(executor);
boolean success = executionEngine.execute(root, 300);
CrosschainCallResult result =
this.crossControlManagerGroup.executeCrosschainCall(CrosschainProtocols.SERIAL, root, 300);

LOG.info("Success: {}", success);
LOG.info("Success: {}", result.isSuccessful());

if (!success) {
if (!result.isSuccessful()) {
throw new Exception("Crosschain Execution failed. See log for details");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.math.BigInteger;
import net.consensys.gpact.common.*;
import net.consensys.gpact.functioncall.gpact.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.CrossControlManagerGroup;
import net.consensys.gpact.helpers.CredentialsCreator;
import net.consensys.gpact.helpers.GpactExampleSystemManager;
import org.apache.logging.log4j.LogManager;
Expand All @@ -40,10 +40,10 @@ public static void main(String[] args) throws Exception {
GpactExampleSystemManager exampleManager = new GpactExampleSystemManager(args[0]);
exampleManager.gpactStandardExampleConfig(2);

BlockchainInfo root = exampleManager.getRootBcInfo();
BlockchainInfo bc2 = exampleManager.getBc2Info();
BlockchainConfig root = exampleManager.getRootBcInfo();
BlockchainConfig bc2 = exampleManager.getBc2Info();
CrossControlManagerGroup crossControlManagerGroup =
exampleManager.getGpactCrossControlManagerGroup();
exampleManager.getCrossControlManagerGroup();

final int CHAIN_A_TOKEN_SUPPLY = 1000;
final int CHAIN_B_TOKEN_SUPPLY = 2000;
Expand Down Expand Up @@ -120,24 +120,24 @@ public static void main(String[] args) throws Exception {

user1.createCbcManager(
root,
crossControlManagerGroup.getInfrastructureAddresses(chainABcId),
crossControlManagerGroup.getCbcAddress(chainABcId),
crossControlManagerGroup.getMessageVerification(chainABcId),
bc2,
crossControlManagerGroup.getInfrastructureAddresses(chainBBcId),
crossControlManagerGroup.getCbcAddress(chainBBcId),
crossControlManagerGroup.getMessageVerification(chainBBcId));
user2.createCbcManager(
root,
crossControlManagerGroup.getInfrastructureAddresses(chainABcId),
crossControlManagerGroup.getCbcAddress(chainABcId),
crossControlManagerGroup.getMessageVerification(chainABcId),
bc2,
crossControlManagerGroup.getInfrastructureAddresses(chainBBcId),
crossControlManagerGroup.getCbcAddress(chainBBcId),
crossControlManagerGroup.getMessageVerification(chainBBcId));
user3.createCbcManager(
root,
crossControlManagerGroup.getInfrastructureAddresses(chainABcId),
crossControlManagerGroup.getCbcAddress(chainABcId),
crossControlManagerGroup.getMessageVerification(chainABcId),
bc2,
crossControlManagerGroup.getInfrastructureAddresses(chainBBcId),
crossControlManagerGroup.getCbcAddress(chainBBcId),
crossControlManagerGroup.getMessageVerification(chainBBcId));

// Give some balance to the users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import net.consensys.gpact.CrosschainProtocols;
import net.consensys.gpact.common.*;
import net.consensys.gpact.common.AbstractBlockchain;
import net.consensys.gpact.functioncall.gpact.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.gpact.CrosschainExecutor;
import net.consensys.gpact.functioncall.gpact.calltree.CallExecutionTree;
import net.consensys.gpact.functioncall.gpact.engine.ExecutionEngine;
import net.consensys.gpact.functioncall.CallExecutionTree;
import net.consensys.gpact.functioncall.CrossControlManagerGroup;
import net.consensys.gpact.functioncall.CrosschainCallResult;
import net.consensys.gpact.helpers.GpactExampleSystemManager;
import net.consensys.gpact.messaging.MessagingVerificationInterface;
import net.consensys.gpact.soliditywrappers.examples.gpact.hoteltrain.LockableERC20PresetFixedSupply;
Expand Down Expand Up @@ -89,23 +88,24 @@ public void deploy(
}

public void createCbcManager(
BlockchainInfo bcInfoTravel,
List<String> infratructureContractAddressOnBcTravel,
BlockchainConfig bcInfoTravel,
String cbcContractAddressOnBcTravel,
MessagingVerificationInterface msgVerTravel,
BlockchainInfo bcInfoHotel,
List<String> infrastructureContractAddressOnBcHotel,
BlockchainConfig bcInfoHotel,
String cbcContractAddressOnBcHotel,
MessagingVerificationInterface msgVerHotel,
BlockchainInfo bcInfoTrain,
List<String> infrastructureContractAddressOnBcTrain,
BlockchainConfig bcInfoTrain,
String cbcContractAddressOnBcTrain,
MessagingVerificationInterface msgVerTrain)
throws Exception {
this.crossControlManagerGroup = new CrossControlManagerGroup();
this.crossControlManagerGroup.addBlockchainAndLoadContracts(
this.credentials, bcInfoTravel, infratructureContractAddressOnBcTravel, msgVerTravel);
this.crossControlManagerGroup.addBlockchainAndLoadContracts(
this.credentials, bcInfoHotel, infrastructureContractAddressOnBcHotel, msgVerHotel);
this.crossControlManagerGroup.addBlockchainAndLoadContracts(
this.credentials, bcInfoTrain, infrastructureContractAddressOnBcTrain, msgVerTrain);
this.crossControlManagerGroup =
CrosschainProtocols.getFunctionCallInstance(CrosschainProtocols.GPACT).get();
this.crossControlManagerGroup.addBlockchainAndLoadCbcContract(
this.credentials, bcInfoTravel, cbcContractAddressOnBcTravel, msgVerTravel);
this.crossControlManagerGroup.addBlockchainAndLoadCbcContract(
this.credentials, bcInfoHotel, cbcContractAddressOnBcHotel, msgVerHotel);
this.crossControlManagerGroup.addBlockchainAndLoadCbcContract(
this.credentials, bcInfoTrain, cbcContractAddressOnBcTrain, msgVerTrain);
}

public BigInteger book(final int dateInt, GpactExampleSystemManager exampleManager)
Expand Down Expand Up @@ -138,13 +138,13 @@ public BigInteger book(final int dateInt, GpactExampleSystemManager exampleManag
new CallExecutionTree(
this.blockchainId, this.travelAgencyAddress, rlpBookHotelAndTrain, rootCalls);

CrosschainExecutor executor = new CrosschainExecutor(this.crossControlManagerGroup);
ExecutionEngine executionEngine = exampleManager.getExecutionEngine(executor);
boolean success = executionEngine.execute(rootCall, 300);
CrosschainCallResult result =
this.crossControlManagerGroup.executeCrosschainCall(
exampleManager.getExecutionEngine(), rootCall, 300);

LOG.info("Success: {}", success);
LOG.info("Success: {}", result.isSuccessful());

if (!success) {
if (!result.isSuccessful()) {
throw new Exception("Crosschain Execution failed. See log for details");
}

Expand Down
Loading

0 comments on commit 5330ea5

Please sign in to comment.