use of org.aion.p2p.Msg in project aion by aionnetwork.
the class TaskWrite method run.
@Override
public void run() {
// reset allocated buffer and clear messages if the channel is closed
if (channelBuffer.isClosed.get()) {
clearChannelBuffer();
return;
}
if (channelBuffer.onWrite.compareAndSet(false, true)) {
/*
* @warning header set len (body len) before header encode
*/
byte[] bodyBytes = msg.encode();
int bodyLen = bodyBytes == null ? 0 : bodyBytes.length;
Header h = msg.getHeader();
h.setLen(bodyLen);
byte[] headerBytes = h.encode();
// print route
// System.out.println("write " + h.getVer() + "-" + h.getCtrl() + "-" + h.getAction());
ByteBuffer buf = ByteBuffer.allocate(headerBytes.length + bodyLen);
buf.put(headerBytes);
if (bodyBytes != null)
buf.put(bodyBytes);
buf.flip();
try {
while (buf.hasRemaining()) {
sc.write(buf);
}
} catch (ClosedChannelException ex1) {
if (showLog) {
System.out.println("<p2p closed-channel-exception node=" + this.nodeShortId + ">");
}
channelBuffer.isClosed.set(true);
} catch (IOException ex2) {
if (showLog) {
System.out.println("<p2p write-msg-io-exception node=" + this.nodeShortId + ">");
}
} finally {
channelBuffer.onWrite.set(false);
if (channelBuffer.isClosed.get()) {
clearChannelBuffer();
} else {
Msg msg = channelBuffer.messages.poll();
if (msg != null) {
// System.out.println("write " + h.getCtrl() + "-" + h.getAction());
workers.submit(new TaskWrite(workers, showLog, nodeShortId, sc, msg, channelBuffer, p2pMgr));
}
}
}
} else {
// message may get dropped here when the message queue is full.
channelBuffer.messages.offer(msg);
}
}
use of org.aion.p2p.Msg in project aion by aionnetwork.
the class BlockPropagationTest method testPropagateBlockToPeer.
// given two peers, and one sends you a new block, propagate to the other
@Test
public void testPropagateBlockToPeer() {
List<ECKey> accounts = generateDefaultAccounts();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
MiningBlock block = bundle.bc.createNewMiningBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
assertThat(block.getNumber()).isEqualTo(1);
byte[] sender = HashUtil.h256("node1".getBytes());
byte[] receiver = HashUtil.h256("receiver".getBytes());
NodeMock senderMock = new NodeMock(sender, 1);
NodeMock receiverMock = new NodeMock(receiver, 0);
Map<Integer, INode> node = new HashMap<>();
node.put(1, senderMock);
node.put(2, receiverMock);
AtomicInteger times = new AtomicInteger();
P2pMock p2pMock = new P2pMock(node) {
@Override
public void send(int _nodeId, String s, Msg _msg) {
if (_nodeId != receiverMock.getIdHash()) {
throw new RuntimeException("should only send to receiver");
}
times.getAndIncrement();
}
};
StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
assertThat(block.getParentHash()).isEqualTo(bundle.bc.genesis.getHash());
assertThat(block.getParentHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
Block bestBlock = bundle.bc.getBestBlock();
assertThat(bestBlock.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
SyncStats syncStats = new SyncStats(bestBlock.getNumber(), true);
BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
// block is processed
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
assertThat(times.get()).isEqualTo(1);
}
use of org.aion.p2p.Msg in project aion by aionnetwork.
the class BlockPropagationTest method testBlockPropagationReceiver.
/**
* Test that we don't propagate back to the sender
*/
@Test
public void testBlockPropagationReceiver() {
List<ECKey> accounts = generateDefaultAccounts();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
MiningBlock block = bundle.bc.createNewMiningBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
assertThat(block.getNumber()).isEqualTo(1);
byte[] sender = HashUtil.h256("node1".getBytes());
NodeMock senderMock = new NodeMock(sender, 1);
Map<Integer, INode> node = new HashMap<>();
node.put(1, senderMock);
SyncStats syncStats = new SyncStats(block.getNumber(), true);
P2pMock p2pMock = new P2pMock(node) {
@Override
public void send(int _nodeId, String s, Msg _msg) {
throw new RuntimeException("should not have called send");
}
};
StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.CONNECTED);
}
use of org.aion.p2p.Msg in project aion by aionnetwork.
the class BlockPropagationTest method testIgnoreSelfBlock.
// this test scenario: we propagate a block out, and someone propagates back
@Test
public void testIgnoreSelfBlock() {
List<ECKey> accounts = generateDefaultAccounts();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
MiningBlock block = bundle.bc.createNewMiningBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
assertThat(block.getNumber()).isEqualTo(1);
byte[] sender = HashUtil.h256("node1".getBytes());
NodeMock senderMock = new NodeMock(sender, 1);
Map<Integer, INode> node = new HashMap<>();
node.put(1, senderMock);
AtomicInteger sendCount = new AtomicInteger();
P2pMock p2pMock = new P2pMock(node) {
@Override
public void send(int _nodeId, String s, Msg _msg) {
sendCount.getAndIncrement();
}
};
StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
SyncStats syncStats = new SyncStats(bundle.bc.getBestBlock().getNumber(), true);
BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
// pretend that we propagate the new block
// send counter incremented
handler.propagateNewBlock(block);
// recall that we're using another blockchain and faked the propagation
// so our blockchain should view this block as a new block
// therefore if the filter fails, this block will actually be CONNECTED
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
// we expect the counter to be incremented once (on propagation)
assertThat(sendCount.get()).isEqualTo(1);
}
use of org.aion.p2p.Msg in project aion by aionnetwork.
the class BlockPropagationTest method testIgnoreSameBlock.
@Test
public void testIgnoreSameBlock() {
List<ECKey> accounts = generateDefaultAccounts();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
MiningBlock block = bundle.bc.createNewMiningBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
assertThat(block.getNumber()).isEqualTo(1);
byte[] sender = HashUtil.h256("node1".getBytes());
byte[] receiver = HashUtil.h256("receiver".getBytes());
NodeMock senderMock = new NodeMock(sender, 1);
NodeMock receiverMock = new NodeMock(receiver, 0);
Map<Integer, INode> node = new HashMap<>();
node.put(1, senderMock);
node.put(2, receiverMock);
AtomicInteger times = new AtomicInteger();
P2pMock p2pMock = new P2pMock(node) {
@Override
public void send(int _nodeId, String s, Msg _msg) {
if (_nodeId != receiverMock.getIdHash()) {
throw new RuntimeException("should only send to receiver");
}
times.getAndIncrement();
}
};
StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
SyncStats syncStats = new SyncStats(bundle.bc.getBestBlock().getNumber(), true);
BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
// block is processed
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
assertThat(times.get()).isEqualTo(1);
}
Aggregations