Search in sources :

Example 11 with A0BlockHeader

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]);
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) Test(org.junit.Test)

Example 12 with A0BlockHeader

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));
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader)

Example 13 with A0BlockHeader

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));
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 14 with A0BlockHeader

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;
        }
    }
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) RLPList(org.aion.rlp.RLPList)

Aggregations

A0BlockHeader (org.aion.zero.types.A0BlockHeader)14 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 RLPList (org.aion.rlp.RLPList)3 BigInteger (java.math.BigInteger)2 Equihash (org.aion.equihash.Equihash)2 RLPElement (org.aion.rlp.RLPElement)2 AionBlock (org.aion.zero.impl.types.AionBlock)2 List (java.util.List)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Collectors (java.util.stream.Collectors)1 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)1 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)1 EquiUtils (org.aion.equihash.EquiUtils)1 IP2pMgr (org.aion.p2p.IP2pMgr)1 ChainConfiguration (org.aion.zero.impl.blockchain.ChainConfiguration)1 ReqBlocksBodies (org.aion.zero.impl.sync.msg.ReqBlocksBodies)1 ResBlocksHeaders (org.aion.zero.impl.sync.msg.ResBlocksHeaders)1