use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.
the class A0BlockHeaderTest method testBlockHeaderFromUnsafeSource.
@Test
public void testBlockHeaderFromUnsafeSource() {
long time = System.currentTimeMillis() / 1000;
A0BlockHeader.Builder builder = new A0BlockHeader.Builder();
// partial build
builder.fromUnsafeSource().withStateRoot(STATE_ROOT).withCoinbase(Address.wrap(COINBASE)).withTxTrieRoot(TRIE_ROOT).withExtraData(EXTRA_DATA).withReceiptTrieRoot(RECEIPT_ROOT).withTimestamp(time).withNumber(NUMBER_BYTES).withEnergyConsumed(ENERGY_CONSUMED_BYTES).withEnergyLimit(ENERGY_LIMIT_BYTES).withParentHash(PARENT_HASH);
A0BlockHeader header = builder.build();
assertThat(header.getStateRoot()).isEqualTo(STATE_ROOT);
assertThat(header.getCoinbase().toBytes()).isEqualTo(COINBASE);
assertThat(header.getTxTrieRoot()).isEqualTo(TRIE_ROOT);
assertThat(header.getExtraData()).isEqualTo(EXTRA_DATA);
assertThat(header.getReceiptsRoot()).isEqualTo(RECEIPT_ROOT);
assertThat(header.getTimestamp()).isEqualTo(time);
assertThat(header.getNumber()).isEqualTo(NUMBER);
assertThat(header.getEnergyConsumed()).isEqualTo(ENERGY_CONSUMED);
assertThat(header.getEnergyLimit()).isEqualTo(ENERGY_LIMIT);
assertThat(header.getSolution()).isEqualTo(new byte[1408]);
}
use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.
the class SyncMgr method validateAndAddHeaders.
/**
* @param _nodeIdHashcode int
* @param _displayId String
* @param _headers List validate headers batch and add batch to imported headers
*/
public void validateAndAddHeaders(int _nodeIdHashcode, String _displayId, List<A0BlockHeader> _headers) {
if (_headers == null || _headers.isEmpty()) {
return;
}
if (log.isDebugEnabled()) {
log.debug("<incoming-headers from-num={} to-num={} node={}>", _headers.get(0).getNumber(), _headers.get(_headers.size() - 1).getNumber(), _displayId);
}
// filter imported block headers
List<A0BlockHeader> filtered = new ArrayList<>();
A0BlockHeader prev = null;
for (A0BlockHeader current : _headers) {
// ignore this batch if any invalidated header
if (!this.blockHeaderValidator.validate(current, log)) {
log.debug("<invalid-header num={} hash={}>", current.getNumber(), current.getHash());
// Print header to allow debugging
log.debug("Invalid header: {}", current.toString());
return;
}
// break if not consisting
if (prev != null && (current.getNumber() != (prev.getNumber() + 1) || !Arrays.equals(current.getParentHash(), prev.getHash()))) {
log.debug("<inconsistent-block-headers>");
return;
}
// add if not cached
if (!importedBlockHashes.containsKey(ByteArrayWrapper.wrap(current.getHash())))
filtered.add(current);
prev = current;
}
// _headers.sort((h1, h2) -> (int) (h1.getNumber() - h2.getNumber()));
if (filtered.size() > 0)
importedHeaders.add(new HeadersWrapper(_nodeIdHashcode, _displayId, filtered));
}
use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.
the class SyncMgr method validateAndAddBlocks.
/**
* @param _nodeIdHashcode int
* @param _displayId String
* @param _bodies List<byte[]>
* Assemble and validate blocks batch and add batch
* to import queue from network response blocks bodies
*/
public void validateAndAddBlocks(int _nodeIdHashcode, String _displayId, final List<byte[]> _bodies) {
if (importedBlocks.size() > blocksQueueMax) {
log.debug("Imported blocks queue is full. Stop validating incoming bodies");
return;
}
HeadersWrapper hw = this.sentHeaders.remove(_nodeIdHashcode);
if (hw == null || _bodies == null)
return;
// assemble batch
List<A0BlockHeader> headers = hw.getHeaders();
List<AionBlock> blocks = new ArrayList<>(_bodies.size());
Iterator<A0BlockHeader> headerIt = headers.iterator();
Iterator<byte[]> bodyIt = _bodies.iterator();
while (headerIt.hasNext() && bodyIt.hasNext()) {
AionBlock block = AionBlock.createBlockFromNetwork(headerIt.next(), bodyIt.next());
if (block == null) {
log.error("<assemble-and-validate-blocks node={}>", _displayId);
break;
} else
blocks.add(block);
}
int m = blocks.size();
if (m == 0)
return;
if (log.isDebugEnabled()) {
log.debug("<incoming-bodies from-num={} to-num={} node={}>", blocks.get(0).getNumber(), blocks.get(blocks.size() - 1).getNumber(), _displayId);
}
// add batch
importedBlocks.add(new BlocksWrapper(_nodeIdHashcode, _displayId, blocks));
}
use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.
the class ResBlocksHeaders method decode.
public static ResBlocksHeaders decode(final byte[] _msgBytes) {
if (_msgBytes == null || _msgBytes.length == 0)
return null;
else {
try {
RLPList list = (RLPList) RLP.decode2(_msgBytes).get(0);
List<A0BlockHeader> blockHeaders = new ArrayList<>();
for (RLPElement aList : list) {
RLPList rlpData = ((RLPList) aList);
blockHeaders.add(A0BlockHeader.fromRLP(rlpData, true));
}
return new ResBlocksHeaders(blockHeaders);
} catch (Exception ex) {
return null;
}
}
}
Aggregations