Browse Source

add sdk test sample for main project

tags/1.3.0^2
zhangshuang 4 years ago
parent
commit
1123450fd6
4 changed files with 195 additions and 113 deletions
  1. +57
    -0
      samples/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_ActiveParticipant.java
  2. +87
    -0
      samples/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_RegistParticipant.java
  3. +51
    -0
      samples/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDK_RegistParticipant_Demo.java
  4. +0
    -113
      samples/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Participant_State_Update_Test_.java

+ 57
- 0
samples/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_ActiveParticipant.java View File

@@ -0,0 +1,57 @@
package com.jd.blockchain.sdk.samples;

import com.jd.blockchain.ledger.TransactionResponse;
import com.jd.blockchain.utils.http.ResponseConverter;
import com.jd.blockchain.utils.web.client.WebResponseConverter;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
* @Author: zhangshuang
* @Date: 2020/5/27 5:18 PM
* Version 1.0
*/
public class SDKDemo_ActiveParticipant {

// 接受激活参与方操作的共识节点Http服务地址, 根据具体环境配置进行修改
private static String httpIp = "127.0.0.1";
private static String httpPort = "7085";


public static void main(String[] args) {

String url = "http://" + httpIp + ":" + httpPort + "/management/delegate/activeparticipant";

System.out.println("url = " + url);

HttpPost httpPost = new HttpPost(url);

List<BasicNameValuePair> para=new ArrayList<BasicNameValuePair>();

// 账本值根据具体情况进行修改
BasicNameValuePair base58LedgerHash = new BasicNameValuePair("ledgerHash", "j5n3SmYT3rS5aDgwQbDUxBYUMEP9GvVfMiQK3Tcr3vxz3M");

para.add(base58LedgerHash);

try {
httpPost.setEntity(new UrlEncodedFormEntity(para,"UTF-8"));
HttpClient httpClient = HttpClients.createDefault();

HttpResponse response = httpClient.execute(httpPost);
ResponseConverter responseConverter = new WebResponseConverter(TransactionResponse.class);
Object converterResponse = responseConverter.getResponse(null, response.getEntity().getContent(), null);
System.out.println("response result = " + ((TransactionResponse)converterResponse).getExecutionState());

} catch (Exception e) {
e.printStackTrace();
System.out.println("Active participant post request error!");
}
}
}

+ 87
- 0
samples/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_RegistParticipant.java View File

@@ -0,0 +1,87 @@
package com.jd.blockchain.sdk.samples;

import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.*;
import com.jd.blockchain.ledger.*;
import com.jd.blockchain.sdk.BlockchainService;
import com.jd.blockchain.sdk.client.GatewayServiceFactory;
import com.jd.blockchain.utils.net.NetworkAddress;

/**
* @Author: zhangshuang
* @Date: 2020/5/27 5:18 PM
* Version 1.0
*/
public class SDKDemo_RegistParticipant {

public static void main(String[] args) {
PrivKey privKey;
PubKey pubKey;

BlockchainKeypair CLIENT_CERT = null;

String GATEWAY_IPADDR = null;

int GATEWAY_PORT;

boolean SECURE;

BlockchainService service;

//根据密码工具产生的公私钥
String PUB = "3snPdw7i7PkdgqiGX7GbZuFSi1cwZn7vtjw4vifb1YoXgr9k6Kfmis";
String PRIV = "177gjtZu8w1phqHFVNiFhA35cfimXmP6VuqrBFhfbXBWK8s4TRwro2tnpffwP1Emwr6SMN6";

privKey = SDKDemo_Params.privkey1;
pubKey = SDKDemo_Params.pubKey1;

CLIENT_CERT = new BlockchainKeypair(SDKDemo_Params.pubKey0, SDKDemo_Params.privkey0);
GATEWAY_IPADDR = "127.0.0.1";
GATEWAY_PORT = 11000;
SECURE = false;
GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE,
CLIENT_CERT);
service = serviceFactory.getBlockchainService();

DataContractRegistry.register(TransactionContent.class);
DataContractRegistry.register(TransactionContentBody.class);
DataContractRegistry.register(TransactionRequest.class);
DataContractRegistry.register(NodeRequest.class);
DataContractRegistry.register(EndpointRequest.class);
DataContractRegistry.register(TransactionResponse.class);
DataContractRegistry.register(ParticipantRegisterOperation.class);
DataContractRegistry.register(ParticipantStateUpdateOperation.class);

HashDigest[] ledgerHashs = service.getLedgerHashs();
// 在本地定义注册账号的 TX;
TransactionTemplate txTemp = service.newTransaction(ledgerHashs[0]);

//existed signer
AsymmetricKeypair keyPair = new BlockchainKeypair(pubKey, privKey);

privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV, SDKDemo_Constant.PASSWORD);

pubKey = KeyGenUtils.decodePubKey(PUB);

System.out.println("Address = " + AddressEncoding.generateAddress(pubKey));

BlockchainKeypair user = new BlockchainKeypair(pubKey, privKey);

// 新参与方的共识网络地址
NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000);

// 注册参与方
txTemp.participants().register("Peer4", user.getIdentity(), networkAddress);

// TX 准备就绪;
PreparedTransaction prepTx = txTemp.prepare();

// 使用私钥进行签名;
prepTx.sign(keyPair);

// 提交交易;
TransactionResponse transactionResponse = prepTx.commit();
}


}

+ 51
- 0
samples/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDK_RegistParticipant_Demo.java View File

@@ -0,0 +1,51 @@
package com.jd.blockchain.sdk.samples;

import com.jd.blockchain.crypto.AddressEncoding;
import com.jd.blockchain.crypto.KeyGenUtils;
import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.ledger.*;
import com.jd.blockchain.utils.net.NetworkAddress;

public class SDK_RegistParticipant_Demo extends SDK_Base_Demo {

public static void main(String[] args) {
new SDK_RegistParticipant_Demo().regParticipant();
}

public void regParticipant() {

//新参与方的公私钥
String PUB = "3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9";
String PRIV = "177gjzHTznYdPgWqZrH43W3yp37onm74wYXT4v9FukpCHBrhRysBBZh7Pzdo5AMRyQGJD7x";

PrivKey privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV, SDKDemo_Constant.PASSWORD);

PubKey pubKey = KeyGenUtils.decodePubKey(PUB);

System.out.println("Address = " + AddressEncoding.generateAddress(pubKey));

BlockchainKeypair user = new BlockchainKeypair(pubKey, privKey);

// 新参与方的共识网络地址
NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000);

// 定义交易模板
TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash);

// 注册参与方
txTpl.participants().register("Peer4", user.getIdentity(), networkAddress);

// TX 准备就绪;
PreparedTransaction prepTx = txTpl.prepare();

// 使用私钥进行签名;
prepTx.sign(adminKey);

// 提交交易;
TransactionResponse transactionResponse = prepTx.commit();

System.out.println(transactionResponse.isSuccess());

}
}

+ 0
- 113
samples/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Participant_State_Update_Test_.java View File

@@ -1,113 +0,0 @@
package test.com.jd.blockchain.sdk.test;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.AddressEncoding;
import com.jd.blockchain.crypto.AsymmetricKeypair;
import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.crypto.KeyGenUtils;
import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.ledger.BlockchainKeypair;
import com.jd.blockchain.ledger.EndpointRequest;
import com.jd.blockchain.ledger.NodeRequest;
import com.jd.blockchain.ledger.ParticipantNodeState;
import com.jd.blockchain.ledger.ParticipantRegisterOperation;
import com.jd.blockchain.ledger.ParticipantStateUpdateOperation;
import com.jd.blockchain.ledger.PreparedTransaction;
import com.jd.blockchain.ledger.TransactionContent;
import com.jd.blockchain.ledger.TransactionContentBody;
import com.jd.blockchain.ledger.TransactionRequest;
import com.jd.blockchain.ledger.TransactionResponse;
import com.jd.blockchain.ledger.TransactionTemplate;
import com.jd.blockchain.sdk.BlockchainService;
import com.jd.blockchain.sdk.client.GatewayServiceFactory;
import com.jd.blockchain.sdk.samples.SDKDemo_Constant;
import com.jd.blockchain.utils.net.NetworkAddress;

/**
* 参与方状态更新测试
* @author zhangshuang
* @create 2019/7/18
* @since 1.0.0
*/
public class SDK_GateWay_Participant_State_Update_Test_ {
private PrivKey privKey;
private PubKey pubKey;

private BlockchainKeypair CLIENT_CERT = null;

private String GATEWAY_IPADDR = null;

private int GATEWAY_PORT;

private boolean SECURE;

private BlockchainService service;

//根据密码工具产生的公私钥
static String PUB = "3snPdw7i7PkdgqiGX7GbZuFSi1cwZn7vtjw4vifb1YoXgr9k6Kfmis";
String PRIV = "177gjtZu8w1phqHFVNiFhA35cfimXmP6VuqrBFhfbXBWK8s4TRwro2tnpffwP1Emwr6SMN6";

@Before
public void init() {

privKey = SDK_GateWay_KeyPair_Para.privkey1;
pubKey = SDK_GateWay_KeyPair_Para.pubKey1;

CLIENT_CERT = new BlockchainKeypair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0);
GATEWAY_IPADDR = "127.0.0.1";
GATEWAY_PORT = 11000;
SECURE = false;
GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE,
CLIENT_CERT);
service = serviceFactory.getBlockchainService();

DataContractRegistry.register(TransactionContent.class);
DataContractRegistry.register(TransactionContentBody.class);
DataContractRegistry.register(TransactionRequest.class);
DataContractRegistry.register(NodeRequest.class);
DataContractRegistry.register(EndpointRequest.class);
DataContractRegistry.register(TransactionResponse.class);
DataContractRegistry.register(ParticipantRegisterOperation.class);
DataContractRegistry.register(ParticipantStateUpdateOperation.class);
DataContractRegistry.register(ParticipantStateUpdateOperation.class);
}

@Test
public void updateParticipantState_Test() {
HashDigest[] ledgerHashs = service.getLedgerHashs();
// 在本地定义注册账号的 TX;
TransactionTemplate txTemp = service.newTransaction(ledgerHashs[0]);

//existed signer
AsymmetricKeypair keyPair = new BlockchainKeypair(pubKey, privKey);

PrivKey privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV, SDKDemo_Constant.PASSWORD);

PubKey pubKey = KeyGenUtils.decodePubKey(PUB);

System.out.println("Address = "+AddressEncoding.generateAddress(pubKey));

BlockchainKeypair user = new BlockchainKeypair(pubKey, privKey);

NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000);

txTemp.states().update(user.getIdentity(),networkAddress, ParticipantNodeState.ACTIVED);

// TX 准备就绪;
PreparedTransaction prepTx = txTemp.prepare();

// 使用私钥进行签名;
prepTx.sign(keyPair);

// 提交交易;
TransactionResponse transactionResponse = prepTx.commit();
assertTrue(transactionResponse.isSuccess());

}
}

Loading…
Cancel
Save