Search in sources :

Example 1 with A0BlockHeader

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

the class ChainConfigurationTest method testValidation.

@Test
public void testValidation() {
    int n = 210;
    int k = 9;
    byte[] nonce = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    // setup mock
    A0BlockHeader.Builder builder = new A0BlockHeader.Builder();
    builder.withDifficulty(BigInteger.valueOf(1).toByteArray());
    builder.withNonce(nonce);
    A0BlockHeader header = builder.build();
    Equihash equihash = new Equihash(n, k);
    int[][] solutions;
    while (true) {
        solutions = equihash.getSolutionsForNonce(header.getHeaderBytes(true), header.getNonce());
        if (solutions.length > 0)
            break;
    }
    // compress solution
    byte[] compressedSolution = EquiUtils.getMinimalFromIndices(solutions[0], n / (k + 1));
    header.setSolution(compressedSolution);
    ChainConfiguration chainConfig = new ChainConfiguration();
    BlockHeaderValidator<A0BlockHeader> blockHeaderValidator = chainConfig.createBlockHeaderValidator();
    blockHeaderValidator.validate(header, log);
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) ChainConfiguration(org.aion.zero.impl.blockchain.ChainConfiguration) Equihash(org.aion.equihash.Equihash) Test(org.junit.Test)

Example 2 with A0BlockHeader

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

the class A0BlockHeaderTest method testBlockHeaderFromRLP.

// Test is a self referencing
@Test
public void testBlockHeaderFromRLP() throws Exception {
    long time = System.currentTimeMillis() / 1000;
    A0BlockHeader.Builder builder = new A0BlockHeader.Builder();
    builder.fromUnsafeSource().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();
    byte[] encoded = header.getEncoded();
    A0BlockHeader reconstructed = A0BlockHeader.fromRLP(encoded, true);
    assertThat(reconstructed.getCoinbase()).isEqualTo(header.getCoinbase());
    assertThat(reconstructed.getTxTrieRoot()).isEqualTo(header.getTxTrieRoot());
    assertThat(reconstructed.getExtraData()).isEqualTo(header.getExtraData());
    assertThat(reconstructed.getReceiptsRoot()).isEqualTo(header.getReceiptsRoot());
    assertThat(reconstructed.getTimestamp()).isEqualTo(header.getTimestamp());
    assertThat(reconstructed.getNumber()).isEqualTo(header.getNumber());
    assertThat(reconstructed.getEnergyConsumed()).isEqualTo(header.getEnergyConsumed());
    assertThat(reconstructed.getEnergyLimit()).isEqualTo(header.getEnergyLimit());
    assertThat(reconstructed.getParentHash()).isEqualTo(header.getParentHash());
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) Test(org.junit.Test)

Example 3 with A0BlockHeader

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

the class EquihashSolutionRuleTest method testProperSolution.

// perhaps we should generate a solution by hand
@Test
public void testProperSolution() {
    // given that all our inputs are deterministic, for given nonce, there should always
    // be a valid output solution
    final int n = 210;
    final int k = 9;
    final BigInteger givenNonce = new BigInteger("21");
    // assume a 32-byte nonce (fixed)
    byte[] unpaddedNonceBytes = givenNonce.toByteArray();
    byte[] nonceBytes = new byte[32];
    System.arraycopy(unpaddedNonceBytes, 0, nonceBytes, 32 - unpaddedNonceBytes.length, unpaddedNonceBytes.length);
    System.out.println(new ByteArrayWrapper(nonceBytes));
    A0BlockHeader header = new A0BlockHeader.Builder().build();
    header.setNonce(nonceBytes);
    byte[] headerBytes = header.getHeaderBytes(true);
    Equihash equihash = new Equihash(n, k);
    int[][] solutions = equihash.getSolutionsForNonce(headerBytes, header.getNonce());
    byte[] compressedSolution = (new EquiUtils()).getMinimalFromIndices(solutions[0], n / (k + 1));
    header.setSolution(compressedSolution);
// EquihashSolutionRule rule = new EquihashSolutionRule(new OptimizedEquiValidator(n, k));
// boolean result = rule.validate(header);
// assertThat(result).isTrue();
// assertThat(rule.getErrors()).isEmpty();
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) EquiUtils(org.aion.equihash.EquiUtils) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger) Equihash(org.aion.equihash.Equihash) Test(org.junit.Test)

Example 4 with A0BlockHeader

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

the class Equihash method mine.

/*
     * Mine for a single nonce
     */
public Solution mine(IAionBlock block, byte[] nonce) {
    // Copy blockheader to create a local copy to modify
    A0BlockHeader updateHeader = new A0BlockHeader(block.getHeader());
    // Get timestamp
    long timeStamp = System.currentTimeMillis() / 1000;
    // Update header
    updateHeader.setTimestamp(timeStamp);
    byte[] blockHeader = updateHeader.getHeaderBytes(true);
    // Target needs to be adjusted after further exploration
    BigInteger target = valueOf(2).pow(256).divide(new BigInteger(block.getHeader().getDifficulty()));
    int[][] generatedSolutions;
    // Convert byte to LE order (in place)
    toLEByteArray(nonce);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Mining Nonce: " + toHexString(nonce) + " Nonce Size: " + nonce.length + "  " + toHexString(nonce));
    }
    // Get solutions for this nonce
    generatedSolutions = getSolutionsForNonce(blockHeader, nonce);
    // Increment number of solutions
    this.totalSolGenerated.addAndGet(generatedSolutions.length);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Produced " + generatedSolutions.length + " solutions");
    }
    // Check each returned solution
    for (int i = 0; i < generatedSolutions.length; i++) {
        // Verify if any of the solutions pass the difficulty filter, return if true.
        byte[] minimal = EquiUtils.getMinimalFromIndices(generatedSolutions[i], cBitLen);
        updateHeader.setSolution(minimal);
        updateHeader.setNonce(nonce);
        // Found a valid solution
        if (isValidBlock(updateHeader, target)) {
            return new Solution(block, nonce, minimal, timeStamp);
        }
    }
    return null;
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) BigInteger(java.math.BigInteger)

Example 5 with A0BlockHeader

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

the class TaskGetBodies method run.

@Override
public void run() {
    while (run.get()) {
        HeadersWrapper hw;
        try {
            hw = headersImported.take();
        } catch (InterruptedException e) {
            continue;
        }
        int idHash = hw.getNodeIdHash();
        List<A0BlockHeader> headers = hw.getHeaders();
        if (headers.isEmpty()) {
            continue;
        }
        HeadersWrapper hwPrevious = headersSent.get(idHash);
        if (hwPrevious == null || (System.currentTimeMillis() - hwPrevious.getTimestamp()) > SENT_HEADERS_TIMEOUT) {
            this.headersSent.put(idHash, hw);
            if (log.isDebugEnabled()) {
                log.debug("<get-bodies from-num={} to-num={} node={}>", headers.get(0).getNumber(), headers.get(headers.size() - 1).getNumber(), hw.getDisplayId());
            }
            this.p2p.send(idHash, new ReqBlocksBodies(headers.stream().map(k -> k.getHash()).collect(Collectors.toList())));
        }
    }
}
Also used : IP2pMgr(org.aion.p2p.IP2pMgr) A0BlockHeader(org.aion.zero.types.A0BlockHeader) List(java.util.List) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReqBlocksBodies(org.aion.zero.impl.sync.msg.ReqBlocksBodies) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) A0BlockHeader(org.aion.zero.types.A0BlockHeader) ReqBlocksBodies(org.aion.zero.impl.sync.msg.ReqBlocksBodies)

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