Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat][test-tool][v0.1.0] add plugin test tool #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
66 changes: 66 additions & 0 deletions antchain-bridge-plugin-test/antchain-bridge-plugin-tester/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<groupId>com.ali.antchain</groupId>
<artifactId>antchain-bridge-plugin-tester</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>

<name>PlugsTest</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.alipay.antchain.bridge</groupId>
<artifactId>antchain-bridge-plugin-lib</artifactId>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>com.alipay.antchain.bridge</groupId>
<artifactId>antchain-bridge-spi</artifactId>
<version>0.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ali.antchain;

import com.ali.antchain.Test.GetContextTest;
import com.ali.antchain.Test.ShutDownTest;
import com.ali.antchain.Test.StartUpTest;
import com.ali.antchain.config.EthereumConfig;
import com.alipay.antchain.bridge.commons.bbc.AbstractBBCContext;
import com.alipay.antchain.bridge.commons.bbc.DefaultBBCContext;
import com.alipay.antchain.bridge.plugins.spi.bbc.AbstractBBCService;

public class EthPluginsTest extends PluginsTest {
public EthPluginsTest(AbstractBBCContext context, AbstractBBCService service) {
super(context, service);
}
EthereumConfig config;
public void EthConfigInit(String url,String key,long gasPrice,long gasLimit) {
this.config = new EthereumConfig();
config.setUrl(url);
config.setPrivateKey(key);
config.setGasPrice(gasPrice);
config.setGasLimit(gasLimit);
this.context = new DefaultBBCContext();
context.setConfForBlockchainClient(config.toJsonString().getBytes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ali.antchain;

import com.ali.antchain.Test.GetContextTest;
import com.ali.antchain.Test.ShutDownTest;
import com.ali.antchain.Test.StartUpTest;
import com.alipay.antchain.bridge.commons.bbc.AbstractBBCContext;
import com.alipay.antchain.bridge.plugins.spi.bbc.AbstractBBCService;

public class PluginsTest {

AbstractBBCContext context;
AbstractBBCService service;

public PluginsTest(AbstractBBCContext context, AbstractBBCService service) {
this.context = context;
this.service = service;
}

public void startup() throws Exception {
StartUpTest.run(context, service);
}

public void shutdown() throws Exception {
ShutDownTest.run(context,service);
}

public void getcontext() throws Exception {
GetContextTest.run(context,service);
}

public void setupamcontract() throws Exception {}

public void setupsdpcontract() throws Exception {}

public void querysdpmessageseq() throws Exception {}

public void setprotocol() throws Exception {

}

public void setamcontractandlocaldomain() throws Exception {

}


// public static void main(String[] args) throws Exception{
// String url = "http://127.0.0.1:7545";
// String key = "0x45cfa3b6addf98274bfd9e3482a695406c7dcc99e1588bd6e0127b8fc06ae916";
// long gasPrice = 2300000000L;
// long gasLimit = 3000000;
// PlugsTest test = new PlugsTest(url,key,gasPrice,gasLimit);
//// test.ethinit.init();
// test.startup();
// }


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.ali.antchain.Test;

import cn.hutool.crypto.digest.DigestUtil;
import com.ali.antchain.service.EthereumBBCService;
import com.alipay.antchain.bridge.commons.bbc.AbstractBBCContext;

public class ETHQuerySDPMessageSeqTest {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个类直接用通用的测试类即可,不涉及eth特有的操作


boolean setupBBC;


public static void run(AbstractBBCContext context) throws Exception {
ETHQuerySDPMessageSeqTest ETHQuerySDPMessageSeqTest = new ETHQuerySDPMessageSeqTest();
ETHQuerySDPMessageSeqTest.querysdpmessageseq(context);
}

public void querysdpmessageseq(AbstractBBCContext context) throws Exception {
EthereumBBCService ethereumBBCService = new EthereumBBCService();
ethereumBBCService.startup(context);

if (setupBBC) {
System.out.println("The BBC has already been set up.");
}else {
RelayAmPrepare.relayamprepare(context);
}
// //set up am
// ethereumBBCService.setupAuthMessageContract();
//
// // set up sdp
// ethereumBBCService.setupSDPMessageContract();
//
// ethereumBBCService.setProtocol(
// context.getSdpContract().getContractAddress(),
// "0");
//
// // set am to sdp
// ethereumBBCService.setAmContract(context.getAuthMessageContract().getContractAddress());
//
// // set local domain to sdp
// ethereumBBCService.setLocalDomain("receiverDomain");
//
// // check contract ready
// AbstractBBCContext ctxCheck = ethereumBBCService.getContext();
//
// System.out.println(ctxCheck.getAuthMessageContract().getStatus());
// System.out.println(ctxCheck.getSdpContract().getStatus());
//
// Web3j web3j = Web3j.build(new HttpService("http://127.0.0.1:7545"));
// Credentials credentials = Credentials.create("0x45cfa3b6addf98274bfd9e3482a695406c7dcc99e1588bd6e0127b8fc06ae916");
//
// RawTransactionManager rawTransactionManager = new RawTransactionManager(
// web3j, credentials, web3j.ethChainId().send().getChainId().longValue());
//
// AppContract appContract = AppContract.deploy(
// web3j,
// rawTransactionManager,
// new DefaultGasProvider()
// ).send();
//
// TransactionReceipt receipt = appContract.setProtocol(ethereumBBCService.getBbcContext().getSdpContract().getContractAddress()).send();
// if (receipt.isStatusOK()) {
// System.out.println("set protocol(" + appContract.getContractAddress()+ ") to app contract(" +ethereumBBCService.getBbcContext().getSdpContract().getContractAddress()+ ")");
// } else {
// throw new Exception(String.format("failed to set protocol(%s) to app contract(%s)",
// appContract.getContractAddress(),
// ethereumBBCService.getBbcContext().getSdpContract().getContractAddress()));
// }
// System.out.println(receipt);
// }

// query seq
long seq = ethereumBBCService.querySDPMessageSeq(
"senderDomain",
DigestUtil.sha256Hex("senderID"),
"receiverDomain",
DigestUtil.sha256Hex("receiverID")
);
System.out.println(seq);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.ali.antchain.Test;

import cn.hutool.core.util.HexUtil;
import com.ali.antchain.abi.SDPMsg;
import com.ali.antchain.service.EthereumBBCService;
import com.alipay.antchain.bridge.commons.bbc.AbstractBBCContext;
import org.web3j.tx.gas.DefaultGasProvider;

public class ETHSetAmContractAndLocalDomainTest {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setAmContract setLocaldmain建议分开,这是两个接口,之前的单测是为了方便写在一起了,这里作为一个通用测试工具的话,需要对每个接口进行单元测试。还有这两接口的测试你看下涉及的内容,只是调用bbc接口就可以了,说明也不涉及eth特有的部分


public static void run(AbstractBBCContext context) throws Exception {
ETHSetAmContractAndLocalDomainTest ETHSetAmContractAndLocalDomainTest = new ETHSetAmContractAndLocalDomainTest();
ETHSetAmContractAndLocalDomainTest.setamcontractandlocaldomain(context);
}
public void setamcontractandlocaldomain(AbstractBBCContext context) throws Exception {
EthereumBBCService ethereumBBCService = new EthereumBBCService();
// start up
ethereumBBCService.startup(context);

// set up am
ethereumBBCService.setupAuthMessageContract();

// set up sdp
ethereumBBCService.setupSDPMessageContract();

// get context
AbstractBBCContext ctx = ethereumBBCService.getContext();

System.out.println(ctx.getAuthMessageContract().getStatus());
System.out.println(ctx.getSdpContract().getStatus());

// set am to sdp
ethereumBBCService.setAmContract(ctx.getAuthMessageContract().getContractAddress());

String amAddr = SDPMsg.load(
ethereumBBCService.getBbcContext().getSdpContract().getContractAddress(),
ethereumBBCService.getWeb3j(),
ethereumBBCService.getCredentials(),
new DefaultGasProvider()
).getAmAddress().send();
System.out.println("amAddr: {"+amAddr+"}");

// check contract status
ctx = ethereumBBCService.getContext();
System.out.println(ctx.getSdpContract().getStatus());

// set the domain
ethereumBBCService.setLocalDomain("receiverDomain");

byte[] rawDomain = SDPMsg.load(
ethereumBBCService.getBbcContext().getSdpContract().getContractAddress(),
ethereumBBCService.getWeb3j(),
ethereumBBCService.getCredentials(),
new DefaultGasProvider()
).getLocalDomain().send();
System.out.println("domain: {"+HexUtil.encodeHexStr(rawDomain)+ "}");

// check contract status
ctx = ethereumBBCService.getContext();
System.out.println(ctx.getSdpContract().getStatus());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ali.antchain.Test;

import com.ali.antchain.abi.AuthMsg;
import com.ali.antchain.service.EthereumBBCService;
import com.alipay.antchain.bridge.commons.bbc.AbstractBBCContext;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;

public class ETHSetProtocolTest {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用通用测试类,不涉及eth特有的操作


public static void run(AbstractBBCContext context) throws Exception {
ETHSetProtocolTest ETHSetProtocolTest = new ETHSetProtocolTest();
ETHSetProtocolTest.setprotocol(context);
}
public void setprotocol(AbstractBBCContext context) throws Exception {
EthereumBBCService ethereumBBCService = new EthereumBBCService();
// start up
ethereumBBCService.startup(context);

// set up am
ethereumBBCService.setupAuthMessageContract();

// set up sdp
ethereumBBCService.setupSDPMessageContract();

// get context
AbstractBBCContext ctx = ethereumBBCService.getContext();
System.out.println(ctx.getAuthMessageContract().getStatus());
System.out.println(ctx.getSdpContract().getStatus());

// set protocol to am (sdp type: 0)
ethereumBBCService.setProtocol(
ctx.getSdpContract().getContractAddress(),
"0");

String addr = AuthMsg.load(
ethereumBBCService.getBbcContext().getAuthMessageContract().getContractAddress(),
ethereumBBCService.getWeb3j(),
ethereumBBCService.getCredentials(),
new DefaultGasProvider()
).getProtocol(BigInteger.ZERO).send();
System.out.println("protocol: {" + addr + "}");

// check am status
ctx = ethereumBBCService.getContext();
System.out.println(ctx.getAuthMessageContract().getStatus());

}
}
Loading