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);
}
}
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);
}
}
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.");
}
}
});
}
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());
}
}
}
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;
}
}
Aggregations