Search in sources :

Example 6 with A0BlockHeader

use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.

the class ApiWeb3Aion method stratum_getHeaderByBlockNumber.

public Object stratum_getHeaderByBlockNumber(Object _blockNum) {
    JSONObject obj = new JSONObject();
    if (_blockNum != null && !_blockNum.equals(null)) {
        String bnStr = _blockNum + "";
        try {
            int bnInt = Integer.decode(bnStr);
            AionBlock block = getBlockRaw(bnInt);
            if (block != null) {
                A0BlockHeader header = block.getHeader();
                // 0 = success
                obj.put("code", 0);
                obj.put("nonce", toHexString(header.getNonce()));
                obj.put("solution", toHexString(header.getSolution()));
                obj.put("headerHash", toHexString(HashUtil.h256(header.getHeaderBytes(false))));
                obj.putOpt("blockHeader", header.toJSON());
            } else {
                obj.put("message", "Fail - Unable to find block" + bnStr);
                obj.put("code", -2);
            }
        } catch (Exception e) {
            obj.put("message", bnStr + " must be an integer value");
            obj.put("code", -3);
        }
    } else {
        obj.put("message", "Missing block number");
        obj.put("code", -1);
    }
    return obj;
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) JSONObject(org.json.JSONObject) ByteUtil.toHexString(org.aion.base.util.ByteUtil.toHexString) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 7 with A0BlockHeader

use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.

the class AionBlock method parseRLP.

public void parseRLP() {
    if (this.parsed) {
        return;
    }
    synchronized (this) {
        if (this.parsed)
            return;
        RLPList params = RLP.decode2(rlpEncoded);
        RLPList block = (RLPList) params.get(0);
        // Parse Header
        RLPList header = (RLPList) block.get(0);
        this.header = new A0BlockHeader(header);
        // Parse Transactions
        RLPList txTransactions = (RLPList) block.get(1);
        this.parseTxs(this.header.getTxTrieRoot(), txTransactions);
        this.parsed = true;
    }
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) RLPList(org.aion.rlp.RLPList)

Example 8 with A0BlockHeader

use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.

the class AionBlockHeaderWrapper method parse.

protected void parse(byte[] bytes) {
    List<RLPElement> params = RLP.decode2(bytes);
    List<RLPElement> wrapper = (RLPList) params.get(0);
    byte[] headerBytes = wrapper.get(0).getRLPData();
    this.header = new A0BlockHeader(headerBytes);
    this.nodeId = wrapper.get(1).getRLPData();
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) RLPElement(org.aion.rlp.RLPElement) RLPList(org.aion.rlp.RLPList)

Example 9 with A0BlockHeader

use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.

the class ResBlocksHeadersTest method testHeaders.

@Test
public void testHeaders() {
    int m = 192;
    List<A0BlockHeader> bhs1 = new ArrayList<A0BlockHeader>();
    for (int i = 0; i < m; i++) {
        bhs1.add(bh1);
    }
    ResBlocksHeaders rbhs1 = new ResBlocksHeaders(bhs1);
    byte[] rbhsBytes = rbhs1.encode();
    ResBlocksHeaders rbhs2 = ResBlocksHeaders.decode(rbhsBytes);
    List<A0BlockHeader> bhs2 = rbhs2.getHeaders();
    assertThat(bhs2.size()).isEqualTo(m);
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) ResBlocksHeaders(org.aion.zero.impl.sync.msg.ResBlocksHeaders) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with A0BlockHeader

use of org.aion.zero.types.A0BlockHeader in project aion by aionnetwork.

the class A0BlockHeaderTest method testBlockHeaderFromSafeBuilder.

@Test
public void testBlockHeaderFromSafeBuilder() {
    long time = System.currentTimeMillis() / 1000;
    A0BlockHeader.Builder builder = new A0BlockHeader.Builder();
    // partial build
    builder.withCoinbase(Address.wrap(COINBASE)).withStateRoot(STATE_ROOT).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.getCoinbase().toBytes()).isEqualTo(COINBASE);
    assertThat(header.getStateRoot()).isEqualTo(STATE_ROOT);
    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)

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