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);
}
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());
}
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();
}
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;
}
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())));
}
}
}
Aggregations