Search in sources :

Example 1 with RegistryPackage

use of com.polyu.blockchain.common.wrapper.RegistryPackage in project blockchain by vincentbin.

the class BusinessHandler method synChain.

/**
 * syn chain
 */
public static void synChain(ArrayList<Block> blockChain, HashMap<String, Output> UTXOs, Transaction genesisTransaction) {
    Map<RegistryPackage, Channel> map = PeerServerConnectKeeper.getMap();
    for (RegistryPackage key : map.keySet()) {
        Channel peer = map.get(key);
        Message message = new Message();
        message.setSynChain(true);
        message.setBlockChain(blockChain);
        message.setUTXOs(UTXOs);
        message.setGenesisTransaction(genesisTransaction);
        peer.writeAndFlush(message);
    }
}
Also used : Message(com.polyu.blockchain.common.wrapper.Message) Channel(io.netty.channel.Channel) RegistryPackage(com.polyu.blockchain.common.wrapper.RegistryPackage)

Example 2 with RegistryPackage

use of com.polyu.blockchain.common.wrapper.RegistryPackage in project blockchain by vincentbin.

the class BusinessHandler method mineBroadcast.

/**
 * broadcast mining block
 */
public static void mineBroadcast(Block block) {
    Map<RegistryPackage, Channel> map = PeerServerConnectKeeper.getMap();
    String uuid = UUID.randomUUID().toString();
    for (RegistryPackage key : map.keySet()) {
        Channel peer = map.get(key);
        Message message = new Message();
        message.setMineRequest(true);
        message.setUuid(uuid);
        message.setContent(block);
        peer.writeAndFlush(message);
    }
}
Also used : Message(com.polyu.blockchain.common.wrapper.Message) Channel(io.netty.channel.Channel) RegistryPackage(com.polyu.blockchain.common.wrapper.RegistryPackage)

Example 3 with RegistryPackage

use of com.polyu.blockchain.common.wrapper.RegistryPackage in project blockchain by vincentbin.

the class Connector method connect.

public static void connect(String serverIp, int serverPort) {
    InetSocketAddress remotePeer = new InetSocketAddress(serverIp, serverPort);
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
    ChannelFuture channelFuture = bootstrap.connect(remotePeer);
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture channelFuture) {
            if (channelFuture.isSuccess()) {
                logger.info("client connect server succeed.");
                Channel channel = channelFuture.channel();
                PeerServerConnectKeeper.add(channel, new RegistryPackage(serverIp, serverPort));
            } else {
                logger.error("client connect server failed.");
            }
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ClientChannelInitializer(com.polyu.blockchain.p2p.netty.ClientChannelInitializer) ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RegistryPackage(com.polyu.blockchain.common.wrapper.RegistryPackage)

Example 4 with RegistryPackage

use of com.polyu.blockchain.common.wrapper.RegistryPackage in project blockchain by vincentbin.

the class ClientHandler method mineBroadcast.

/**
 * broadcast mining block
 */
public static void mineBroadcast(Block block) {
    Map<RegistryPackage, Channel> map = PeerServerConnectKeeper.getMap();
    String uuid = UUID.randomUUID().toString();
    for (RegistryPackage key : map.keySet()) {
        Channel peer = map.get(key);
        Message message = new Message();
        message.setMineRequest(true);
        message.setUuid(uuid);
        ArrayList<Transaction> transactions = block.getTransactions();
        for (Transaction transaction : transactions) {
            transaction.setSenderStr(KeyUtil.PublicKeyToString(transaction.getSender()));
            transaction.setRecipientStr(KeyUtil.PublicKeyToString(transaction.getRecipient()));
            ArrayList<Input> inputs = transaction.getInputs();
            ArrayList<Output> outputs = transaction.getOutputs();
            if (inputs != null) {
                for (Input input : inputs) {
                    Output output = input.getUTXO();
                    output.setRecipientStr(KeyUtil.PublicKeyToString(output.getRecipient()));
                }
            }
            for (Output output : outputs) {
                output.setRecipientStr(KeyUtil.PublicKeyToString(output.getRecipient()));
            }
        }
        message.setContent(block);
        logger.info("peer active status: {}.", peer.isActive());
        try {
            ChannelFuture sync = peer.writeAndFlush(message).sync();
            logger.error("sync = {}.", sync);
        } catch (Exception e) {
            logger.error("error message = {}.", e.getMessage());
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Message(com.polyu.blockchain.common.wrapper.Message) Channel(io.netty.channel.Channel) RegistryPackage(com.polyu.blockchain.common.wrapper.RegistryPackage) Input(com.polyu.blockchain.chain.transaction.Input) Transaction(com.polyu.blockchain.chain.transaction.Transaction) Output(com.polyu.blockchain.chain.transaction.Output)

Example 5 with RegistryPackage

use of com.polyu.blockchain.common.wrapper.RegistryPackage in project blockchain by vincentbin.

the class ClientHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    Message message = (Message) msg;
    logger.info(message.toString());
    if (message.isAddressQuery()) {
        String serverAddress = BootStrap.getServerAddress();
        String[] address = serverAddress.split(":");
        Message rep = new Message(address[0], Integer.parseInt(address[1]));
        rep.setAddressReply(true);
        if (message.isSynChain()) {
            ArrayList<Block> blockChain = MainChain.blockChain;
            for (Block block : blockChain) {
                ArrayList<Transaction> transactions = block.getTransactions();
                for (Transaction transaction : transactions) {
                    transaction.setSenderStr(KeyUtil.PublicKeyToString(transaction.getSender()));
                    transaction.setRecipientStr(KeyUtil.PublicKeyToString(transaction.getRecipient()));
                    ArrayList<Input> inputs = transaction.getInputs();
                    ArrayList<Output> outputs = transaction.getOutputs();
                    if (inputs != null) {
                        for (Input input : inputs) {
                            Output utxo = input.getUTXO();
                            utxo.setRecipientStr(KeyUtil.PublicKeyToString(utxo.getRecipient()));
                        }
                    }
                    for (Output output : outputs) {
                        output.setRecipientStr(KeyUtil.PublicKeyToString(output.getRecipient()));
                    }
                }
            }
            rep.setBlockChain(blockChain);
            HashMap<String, Output> utxOs = MainChain.UTXOs;
            for (Map.Entry<String, Output> utox : utxOs.entrySet()) {
                Output output = utox.getValue();
                output.setRecipientStr(KeyUtil.PublicKeyToString(output.getRecipient()));
            }
            rep.setUTXOs(utxOs);
            Transaction genesisTransaction = MainChain.genesisTransaction;
            genesisTransaction.setSenderStr(KeyUtil.PublicKeyToString(genesisTransaction.getSender()));
            genesisTransaction.setRecipientStr(KeyUtil.PublicKeyToString(genesisTransaction.getRecipient()));
            ArrayList<Input> inputs = genesisTransaction.getInputs();
            ArrayList<Output> outputs = genesisTransaction.getOutputs();
            if (inputs != null) {
                for (Input input : inputs) {
                    Output utxo = input.getUTXO();
                    utxo.setRecipientStr(KeyUtil.PublicKeyToString(utxo.getRecipient()));
                }
            }
            for (Output output : outputs) {
                output.setRecipientStr(KeyUtil.PublicKeyToString(output.getRecipient()));
            }
            rep.setGenesisTransaction(genesisTransaction);
            rep.setSynChain(true);
        }
        ctx.channel().writeAndFlush(rep);
        return;
    }
    if (message.isAddressReply()) {
        PeerServerConnectKeeper.add(ctx.channel(), new RegistryPackage(message.getIp(), message.getPort()));
        if (message.isSynChain()) {
            ArrayList<Block> blockChain = message.getBlockChain();
            for (Block block : blockChain) {
                ArrayList<Transaction> transactions = block.getTransactions();
                for (Transaction transaction : transactions) {
                    transaction.setSender(KeyUtil.stringToPublicKey(transaction.getSenderStr()));
                    transaction.setRecipient(KeyUtil.stringToPublicKey(transaction.getRecipientStr()));
                    ArrayList<Input> inputs = transaction.getInputs();
                    ArrayList<Output> outputs = transaction.getOutputs();
                    if (inputs != null) {
                        for (Input input : inputs) {
                            Output utxo = input.getUTXO();
                            utxo.setRecipient(KeyUtil.stringToPublicKey(utxo.getRecipientStr()));
                        }
                    }
                    for (Output output : outputs) {
                        output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
                    }
                }
            }
            MainChain.blockChain = blockChain;
            HashMap<String, Output> utxOs = message.getUTXOs();
            for (Map.Entry<String, Output> utxo : utxOs.entrySet()) {
                Output output = utxo.getValue();
                output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
            }
            MainChain.UTXOs = utxOs;
            Transaction genesisTransaction = message.getGenesisTransaction();
            genesisTransaction.setSender(KeyUtil.stringToPublicKey(genesisTransaction.senderStr));
            genesisTransaction.setRecipient(KeyUtil.stringToPublicKey(genesisTransaction.recipientStr));
            ArrayList<Input> inputs = genesisTransaction.getInputs();
            ArrayList<Output> outputs = genesisTransaction.getOutputs();
            if (inputs != null) {
                for (Input input : inputs) {
                    Output output = input.getUTXO();
                    output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
                }
            }
            for (Output output : outputs) {
                output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
            }
            MainChain.genesisTransaction = genesisTransaction;
        }
        return;
    }
    // mine request process
    if (message.isMineRequest()) {
        PeerServerConnectKeeper.getUuidSet().add(message.getUuid());
        Block block = message.getContent();
        ArrayList<Transaction> transactions = block.getTransactions();
        for (Transaction transaction : transactions) {
            transaction.setSender(KeyUtil.stringToPublicKey(transaction.getSenderStr()));
            transaction.setRecipient(KeyUtil.stringToPublicKey(transaction.getRecipientStr()));
            ArrayList<Input> inputs = transaction.getInputs();
            ArrayList<Output> outputs = transaction.getOutputs();
            if (inputs != null) {
                for (Input input : inputs) {
                    Output output = input.getUTXO();
                    output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
                }
            }
            for (Output output : outputs) {
                output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
            }
        }
        new Thread(new Runnable() {

            @Override
            public void run() {
                // mine
                block.mineBlock(MainChain.getDifficulty());
                // add to local chain
                MainChain.add(block);
                if (!MainChain.isChainValid()) {
                    ArrayList<Block> blockChain = MainChain.getBlockChain();
                    blockChain.remove(blockChain.size() - 1);
                    return;
                }
                for (Transaction t : block.getTransactions()) {
                    t.updateLocalUTXO();
                }
                // success added, remove id, avoid others' result modify local chain
                PeerServerConnectKeeper.getUuidSet().remove(message.getUuid());
                // compute hash completed -> broadcast
                Map<RegistryPackage, Channel> map = PeerServerConnectKeeper.getMap();
                for (RegistryPackage key : map.keySet()) {
                    Channel channel = map.get(key);
                    Message replyMsg = new Message();
                    replyMsg.setMineReply(true);
                    replyMsg.setUuid(message.getUuid());
                    replyMsg.setContent(block);
                    channel.writeAndFlush(replyMsg);
                }
            }
        }).start();
        return;
    }
    // mine reply process
    if (message.isMineReply()) {
        Block block = message.getContent();
        ArrayList<Transaction> transactions = block.getTransactions();
        for (Transaction transaction : transactions) {
            transaction.setSender(KeyUtil.stringToPublicKey(transaction.getSenderStr()));
            transaction.setRecipient(KeyUtil.stringToPublicKey(transaction.getRecipientStr()));
            ArrayList<Input> inputs = transaction.getInputs();
            ArrayList<Output> outputs = transaction.getOutputs();
            if (inputs != null) {
                for (Input input : inputs) {
                    Output output = input.getUTXO();
                    output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
                }
            }
            for (Output output : outputs) {
                output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
            }
        }
        MainChain.add(block);
        if (!MainChain.isChainValid()) {
            ArrayList<Block> blockChain = MainChain.getBlockChain();
            blockChain.remove(blockChain.size() - 1);
            return;
        }
        // update local UTXO
        for (Transaction t : transactions) {
            t.updateLocalUTXO();
        }
        return;
    }
    // syn chain process
    if (message.isSynChain()) {
        ArrayList<Block> blockChain = message.getBlockChain();
        for (Block block : blockChain) {
            ArrayList<Transaction> transactions = block.getTransactions();
            for (Transaction transaction : transactions) {
                transaction.setSender(KeyUtil.stringToPublicKey(transaction.getSenderStr()));
                transaction.setRecipient(KeyUtil.stringToPublicKey(transaction.getRecipientStr()));
                ArrayList<Input> inputs = transaction.getInputs();
                ArrayList<Output> outputs = transaction.getOutputs();
                if (inputs != null) {
                    for (Input input : inputs) {
                        Output utxo = input.getUTXO();
                        utxo.setRecipient(KeyUtil.stringToPublicKey(utxo.getRecipientStr()));
                    }
                }
                for (Output output : outputs) {
                    output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
                }
            }
        }
        MainChain.blockChain = blockChain;
        HashMap<String, Output> utxOs = message.getUTXOs();
        for (Map.Entry<String, Output> utxo : utxOs.entrySet()) {
            Output output = utxo.getValue();
            output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
        }
        MainChain.UTXOs = utxOs;
        Transaction genesisTransaction = message.getGenesisTransaction();
        genesisTransaction.setSender(KeyUtil.stringToPublicKey(genesisTransaction.senderStr));
        genesisTransaction.setRecipient(KeyUtil.stringToPublicKey(genesisTransaction.recipientStr));
        ArrayList<Input> inputs = genesisTransaction.getInputs();
        ArrayList<Output> outputs = genesisTransaction.getOutputs();
        if (inputs != null) {
            for (Input input : inputs) {
                Output output = input.getUTXO();
                output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
            }
        }
        for (Output output : outputs) {
            output.setRecipient(KeyUtil.stringToPublicKey(output.getRecipientStr()));
        }
        MainChain.genesisTransaction = genesisTransaction;
    }
}
Also used : Message(com.polyu.blockchain.common.wrapper.Message) Channel(io.netty.channel.Channel) RegistryPackage(com.polyu.blockchain.common.wrapper.RegistryPackage) Input(com.polyu.blockchain.chain.transaction.Input) Transaction(com.polyu.blockchain.chain.transaction.Transaction) Output(com.polyu.blockchain.chain.transaction.Output) Block(com.polyu.blockchain.chain.Block) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RegistryPackage (com.polyu.blockchain.common.wrapper.RegistryPackage)11 Channel (io.netty.channel.Channel)8 Message (com.polyu.blockchain.common.wrapper.Message)7 Transaction (com.polyu.blockchain.chain.transaction.Transaction)5 Input (com.polyu.blockchain.chain.transaction.Input)4 Output (com.polyu.blockchain.chain.transaction.Output)4 Block (com.polyu.blockchain.chain.Block)3 ChannelFuture (io.netty.channel.ChannelFuture)2 Map (java.util.Map)2 SneakyThrows (lombok.SneakyThrows)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 ChildData (org.apache.curator.framework.recipes.cache.ChildData)2 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)2 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)2 ConnectionState (org.apache.curator.framework.state.ConnectionState)2 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)2 ClientChannelInitializer (com.polyu.blockchain.p2p.netty.ClientChannelInitializer)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1