use of org.bcos.web3j.crypto.Credentials in project web3sdk by FISCO-BCOS.
the class LoadTestRunner method main.
public static void main(String[] args) throws Exception {
logger.debug("初始化");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Service service = context.getBean(Service.class);
service.run();
System.out.println("3s后开始测试...");
Thread.sleep(1000);
System.out.println("2s后开始测试...");
Thread.sleep(1000);
System.out.println("1s后开始测试...");
Thread.sleep(1000);
System.out.println("开始测试");
System.out.println("===================================================================");
ChannelEthereumService channelEthereumService = new ChannelEthereumService();
channelEthereumService.setChannelService(service);
channelEthereumService.setTimeout(10000);
Web3j web3 = Web3j.build(channelEthereumService);
Thread.sleep(2000);
// 先部署一个测试合约
ECKeyPair keyPair = Keys.createEcKeyPair();
Credentials credentials = Credentials.create(keyPair);
RawTransactionManager rawTransactionManager = new RawTransactionManager(web3, credentials);
java.math.BigInteger gasPrice = new BigInteger("30000000");
java.math.BigInteger gasLimit = new BigInteger("30000000");
java.math.BigInteger initialWeiValue = new BigInteger("0");
LoadTest loadTest = LoadTest.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
System.out.println("LoadTest getContractAddress " + loadTest.getContractAddress());
// 发送交易
for (Integer i = 0; i < 10; ++i) {
Thread txThread = new Thread() {
public void run() {
try {
while (true) {
Thread.sleep(1000);
// TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1));
}
} catch (Exception e) {
logger.error("系统错误", e);
}
}
};
txThread.start();
}
// 读取合约
for (Integer i = 0; i < 10; ++i) {
Thread callThread = new Thread() {
public void run() {
try {
while (true) {
Address origin = loadTest._origin().get();
Address sender = loadTest._sender().get();
logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
}
} catch (Exception e) {
logger.error("系统错误", e);
}
}
};
callThread.start();
}
// 200个线程,发链上链下消息
for (Integer i = 0; i < 10; ++i) {
Thread messageThread = new Thread() {
public void run() {
try {
while (true) {
ChannelRequest request = new ChannelRequest();
request.setToTopic("test");
request.setFromOrg("WB");
request.setToOrg("EB");
request.setMessageID(service.newSeq());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 500; ++i) {
sb.append("hello world!:");
}
request.setContent(sb.toString() + request.getMessageID());
request.setTimeout(2000);
// ChannelResponse response = service.sendChannelMessage2(request);
logger.info("发送链上链下消息:{}", request.getMessageID());
ChannelResponse response = service.sendChannelMessage(request);
logger.info("收到链上链下响应:{} {}", response.getMessageID(), response.getErrorCode());
}
} catch (Exception e) {
logger.error("系统错误", e);
}
}
};
messageThread.start();
}
for (int i = 0; i < 10; ++i) {
TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
Address origin = loadTest._origin().get();
Address sender = loadTest._sender().get();
List<Type> addresses = loadTest.constGetInf().get();
logger.info("const sender:{} origin:{}", (((Address) addresses.get(0)).toString()), (((Address) addresses.get(1)).toString()));
logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
}
}
use of org.bcos.web3j.crypto.Credentials in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildFunctionDefinitions.
private static List<MethodSpec> buildFunctionDefinitions(String className, TypeSpec.Builder classBuilder, List<AbiDefinition> functionDefinitions) throws ClassNotFoundException {
List<MethodSpec> methodSpecs = new ArrayList<>();
boolean constructor = false;
for (AbiDefinition functionDefinition : functionDefinitions) {
if (functionDefinition.getType().equals("function")) {
methodSpecs.add(buildFunction(functionDefinition));
// 添加带有callback的方法
if (!functionDefinition.isConstant()) {
methodSpecs.add(buildFunctionWithCallback(functionDefinition));
}
} else if (functionDefinition.getType().equals("event")) {
buildEventFunctions(functionDefinition, classBuilder);
} else if (functionDefinition.getType().equals("constructor")) {
constructor = true;
methodSpecs.add(buildDeploy(className, functionDefinition, Credentials.class, CREDENTIALS));
methodSpecs.add(buildDeploy(className, functionDefinition, TransactionManager.class, TRANSACTION_MANAGER));
}
}
// constructor will not be specified in ABI file if its empty
if (!constructor) {
MethodSpec.Builder credentialsMethodBuilder = getDeployMethodSpec(className, Credentials.class, CREDENTIALS);
methodSpecs.add(buildDeployAsyncNoParams(credentialsMethodBuilder, className, CREDENTIALS));
MethodSpec.Builder transactionManagerMethodBuilder = getDeployMethodSpec(className, TransactionManager.class, TRANSACTION_MANAGER);
methodSpecs.add(buildDeployAsyncNoParams(transactionManagerMethodBuilder, className, TRANSACTION_MANAGER));
}
return methodSpecs;
}
use of org.bcos.web3j.crypto.Credentials in project web3sdk by FISCO-BCOS.
the class KeyImporter method createWalletFile.
private void createWalletFile(String privateKey) {
if (!WalletUtils.isValidPrivateKey(privateKey)) {
exitError("Invalid private key specified, must be " + PRIVATE_KEY_LENGTH_IN_HEX + " digit hex value");
}
Credentials credentials = Credentials.create(privateKey);
String password = getPassword("Please enter a wallet file password: ");
String destinationDir = getDestinationDir();
File destination = createDir(destinationDir);
try {
String walletFileName = WalletUtils.generateWalletFile(password, credentials.getEcKeyPair(), destination, true);
console.printf("Wallet file " + walletFileName + " successfully created in: " + destinationDir + "\n");
} catch (CipherException | IOException e) {
exitError(e);
}
}
use of org.bcos.web3j.crypto.Credentials in project web3sdk by FISCO-BCOS.
the class WalletSendFunds method run.
private void run(String walletFileLocation, String destinationAddress) {
File walletFile = new File(walletFileLocation);
Credentials credentials = getCredentials(walletFile);
console.printf("Wallet for address " + credentials.getAddress() + " loaded\n");
if (!WalletUtils.isValidAddress(destinationAddress)) {
exitError("Invalid destination address specified");
}
Web3j web3j = getEthereumClient();
BigDecimal amountToTransfer = getAmountToTransfer();
Convert.Unit transferUnit = getTransferUnit();
BigDecimal amountInWei = Convert.toWei(amountToTransfer, transferUnit);
confirmTransfer(amountToTransfer, transferUnit, amountInWei, destinationAddress);
TransactionReceipt transactionReceipt = performTransfer(web3j, destinationAddress, credentials, amountInWei);
console.printf("Funds have been successfully transferred from %s to %s%n" + "Transaction hash: %s%nMined block number: %s%n", credentials.getAddress(), destinationAddress, transactionReceipt.getTransactionHash(), transactionReceipt.getBlockNumber());
}
Aggregations