Search in sources :

Example 1 with ServiceProxy

use of bftsmart.tom.ServiceProxy in project aware by bergerch.

the class CounterClient method main.

public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.out.println("Usage: java ... CounterClient <process id> <increment> [<number of operations>]");
        System.out.println("       if <increment> equals 0 the request will be read-only");
        System.out.println("       default <number of operations> equals 1000");
        System.exit(-1);
    }
    ServiceProxy counterProxy = new ServiceProxy(Integer.parseInt(args[0]));
    try {
        int inc = Integer.parseInt(args[1]);
        int numberOfOps = (args.length > 2) ? Integer.parseInt(args[2]) : 1000;
        for (int i = 0; i < numberOfOps; i++) {
            ByteArrayOutputStream out = new ByteArrayOutputStream(4);
            new DataOutputStream(out).writeInt(inc);
            System.out.print("Invocation " + i);
            byte[] reply = (inc == 0) ? counterProxy.invokeUnordered(out.toByteArray()) : // magic happens here
            counterProxy.invokeOrdered(out.toByteArray());
            if (reply != null) {
                int newValue = new DataInputStream(new ByteArrayInputStream(reply)).readInt();
                System.out.println(", returned value: " + newValue);
            } else {
                System.out.println(", ERROR! Exiting.");
                break;
            }
        }
    } catch (IOException | NumberFormatException e) {
        counterProxy.close();
    }
}
Also used : ServiceProxy(bftsmart.tom.ServiceProxy) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 2 with ServiceProxy

use of bftsmart.tom.ServiceProxy in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4Bft method submitNodeStateChangeTx.

@Override
public TransactionResponse submitNodeStateChangeTx(ParticipantContext context, int activeID, TransactionRequest txRequest, List<NodeSettings> origConsensusNodes) {
    Properties systemConfig = getCustomProperties(context);
    int viewId = ((BftsmartConsensusViewSettings) getConsensusSetting(context)).getViewId();
    TransactionResponse transactionResponse = new TxResponseMessage();
    ServiceProxy peerProxy = createPeerProxy(systemConfig, viewId, origConsensusNodes, context.sslSecurity());
    byte[] result = peerProxy.invokeOrdered(BinaryProtocol.encode(txRequest, TransactionRequest.class));
    if (result == null) {
        ((TxResponseMessage) transactionResponse).setExecutionState(TransactionState.CONSENSUS_NO_REPLY_ERROR);
        return transactionResponse;
    }
    peerProxy.close();
    return txResponseWrapper(BinaryProtocol.decode(result));
}
Also used : BftsmartConsensusViewSettings(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings) ServiceProxy(bftsmart.tom.ServiceProxy) TxResponseMessage(com.jd.blockchain.transaction.TxResponseMessage) Properties(java.util.Properties)

Example 3 with ServiceProxy

use of bftsmart.tom.ServiceProxy in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4Bft method createPeerProxy.

private ServiceProxy createPeerProxy(Properties systemConfig, int viewId, List<NodeSettings> origConsensusNodes, SSLSecurity security) {
    HostsConfig hostsConfig;
    List<HostsConfig.Config> configList = new ArrayList<>();
    List<NodeNetwork> nodeAddresses = new ArrayList<>();
    try {
        int[] origConsensusProcesses = new int[origConsensusNodes.size()];
        for (int i = 0; i < origConsensusNodes.size(); i++) {
            BftsmartNodeSettings node = (BftsmartNodeSettings) origConsensusNodes.get(i);
            origConsensusProcesses[i] = node.getId();
            configList.add(new HostsConfig.Config(node.getId(), node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
            nodeAddresses.add(new NodeNetwork(node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
        }
        // 构建共识的代理客户端需要的主机配置和系统参数配置结构
        hostsConfig = new HostsConfig(configList.toArray(new HostsConfig.Config[configList.size()]));
        Properties tempSystemConfig = (Properties) systemConfig.clone();
        // 构建tom 配置
        TOMConfiguration tomConfig = new TOMConfiguration(-(new Random().nextInt(Integer.MAX_VALUE - 2) - 1), tempSystemConfig, hostsConfig);
        View view = new View(viewId, origConsensusProcesses, tomConfig.getF(), nodeAddresses.toArray(new NodeNetwork[nodeAddresses.size()]));
        LOGGER.info("ManagementController start updateView operation!, current view : {}", view.toString());
        // 构建共识的代理客户端,连接目标共识节点,并递交交易进行共识过程
        return new ServiceProxy(tomConfig, new MemoryBasedViewStorage(view), null, null, security);
    } catch (Exception e) {
        throw new CreateProxyClientException("create proxy client exception!");
    }
}
Also used : BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) HostsConfig(bftsmart.reconfiguration.util.HostsConfig) ArrayList(java.util.ArrayList) HostsConfig(bftsmart.reconfiguration.util.HostsConfig) Properties(java.util.Properties) View(bftsmart.reconfiguration.views.View) Random(java.util.Random) ServiceProxy(bftsmart.tom.ServiceProxy) MemoryBasedViewStorage(bftsmart.reconfiguration.views.MemoryBasedViewStorage) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork) TOMConfiguration(bftsmart.reconfiguration.util.TOMConfiguration)

Example 4 with ServiceProxy

use of bftsmart.tom.ServiceProxy in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4Bft method updateView.

// 通知原有的共识网络更新共识的视图ID
private View updateView(ParticipantNode node, NetworkAddress networkAddress, SSLSecurity security, ManagementController.ParticipantUpdateType participantUpdateType, Properties systemConfig, int viewId, List<NodeSettings> origConsensusNodes) {
    LOGGER.info("ManagementController start updateView operation!");
    TransactionRequest reconfigRequest = null;
    try {
        ServiceProxy peerProxy = createPeerProxy(systemConfig, viewId, origConsensusNodes, security);
        Reconfiguration reconfiguration = new Reconfiguration(peerProxy.getProcessId(), peerProxy);
        if (participantUpdateType == ManagementController.ParticipantUpdateType.ACTIVE) {
            // addServer的第一个参数指待加入共识的新参与方的编号
            reconfiguration.addServer(node.getId(), networkAddress);
            reconfigRequest = prepareReconfigTx("active");
        } else if (participantUpdateType == ManagementController.ParticipantUpdateType.DEACTIVE) {
            // 参数为待移除共识节点的id
            reconfiguration.removeServer(node.getId());
            reconfigRequest = prepareReconfigTx("deactive");
        } else if (participantUpdateType == ManagementController.ParticipantUpdateType.UPDATE) {
            // 共识参数修改,先移除后添加
            reconfiguration.removeServer(node.getId());
            reconfiguration.addServer(node.getId(), networkAddress);
            reconfigRequest = prepareReconfigTx("update");
        } else {
            throw new IllegalArgumentException("op type error!");
        }
        // 把交易作为reconfig操作的扩展信息携带,目的是为了让该操作上链,便于后续跟踪;
        reconfiguration.addExtendInfo(BinaryProtocol.encode(reconfigRequest, TransactionRequest.class));
        // 执行更新目标共识网络的视图ID
        ReconfigureReply reconfigureReply = reconfiguration.execute();
        peerProxy.close();
        // 返回新视图
        return reconfigureReply.getView();
    } catch (Exception e) {
        throw new ViewUpdateException("view update fail exception!", e);
    }
}
Also used : ReconfigureReply(bftsmart.reconfiguration.ReconfigureReply) ServiceProxy(bftsmart.tom.ServiceProxy) Reconfiguration(bftsmart.reconfiguration.Reconfiguration)

Example 5 with ServiceProxy

use of bftsmart.tom.ServiceProxy in project aware by bergerch.

the class YCSBClient method init.

@Override
public void init() {
    Properties props = getProperties();
    int initId = Integer.valueOf((String) props.get("smart-initkey"));
    myId = initId + counter.addAndGet(1);
    proxy = new ServiceProxy(myId);
    System.out.println("YCSBKVClient. Initiated client id: " + myId);
}
Also used : ServiceProxy(bftsmart.tom.ServiceProxy) Properties(java.util.Properties)

Aggregations

ServiceProxy (bftsmart.tom.ServiceProxy)5 Properties (java.util.Properties)3 Reconfiguration (bftsmart.reconfiguration.Reconfiguration)1 ReconfigureReply (bftsmart.reconfiguration.ReconfigureReply)1 HostsConfig (bftsmart.reconfiguration.util.HostsConfig)1 TOMConfiguration (bftsmart.reconfiguration.util.TOMConfiguration)1 MemoryBasedViewStorage (bftsmart.reconfiguration.views.MemoryBasedViewStorage)1 NodeNetwork (bftsmart.reconfiguration.views.NodeNetwork)1 View (bftsmart.reconfiguration.views.View)1 BftsmartConsensusViewSettings (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings)1 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)1 TxResponseMessage (com.jd.blockchain.transaction.TxResponseMessage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1