Search in sources :

Example 1 with AionImpl

use of org.aion.zero.impl.blockchain.AionImpl in project aion by aionnetwork.

the class ApiAion0Test method testProcessGetTxByBlockHashAndIndex.

@Test
@Ignore
public void testProcessGetTxByBlockHashAndIndex() throws Exception {
    AionImpl impl = AionImpl.instForTest();
    AionRepositoryImpl repo = AionRepositoryImpl.inst();
    Block parentBlk = impl.getBlockchain().getBestBlock();
    AionTransaction tx = AionTransaction.create(key, repo.getNonce(AddressUtils.ZERO_ADDRESS).toByteArray(), AddressUtils.ZERO_ADDRESS, BigInteger.ONE.toByteArray(), msg, 100000, 100000, TransactionTypes.DEFAULT, null);
    Block blk = impl.getAionHub().getBlockchain().createNewMiningBlock(parentBlk, Collections.singletonList(tx), false);
    impl.getAionHub().getBlockchain().tryToConnect(blk);
    Message.req_getTransactionByBlockHashAndIndex reqBody = Message.req_getTransactionByBlockHashAndIndex.newBuilder().setBlockHash(ByteString.copyFrom(blk.getHash())).setTxIndex(0).build();
    rsp = sendRequest(Message.Servs.s_chain_VALUE, Message.Funcs.f_getTransactionByBlockHashAndIndex_VALUE, reqBody.toByteArray());
    assertEquals(Message.Retcode.r_success_VALUE, rsp[1]);
    Message.rsp_getTransaction rslt = Message.rsp_getTransaction.parseFrom(stripHeader(rsp));
    assertEquals(blk.getNumber(), rslt.getBlocknumber());
    assertEquals(ByteString.copyFrom(tx.getData()), rslt.getData());
    assertEquals(tx.getEnergyPrice(), rslt.getNrgPrice());
    rsp = sendRequest(Message.Servs.s_hb_VALUE, Message.Funcs.f_getTransactionByBlockHashAndIndex_VALUE);
    assertEquals(Message.Retcode.r_fail_service_call_VALUE, rsp[1]);
}
Also used : AionImpl(org.aion.zero.impl.blockchain.AionImpl) Block(org.aion.zero.impl.types.Block) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with AionImpl

use of org.aion.zero.impl.blockchain.AionImpl in project aion by aionnetwork.

the class ApiAion0Test method testProcessGetTxByBlockNumberAndIndex.

@Test
@Ignore
public void testProcessGetTxByBlockNumberAndIndex() throws Exception {
    AionImpl impl = AionImpl.instForTest();
    AionRepositoryImpl repo = AionRepositoryImpl.inst();
    Block parentBlk = impl.getBlockchain().getBestBlock();
    AionTransaction tx = AionTransaction.create(key, repo.getNonce(AddressUtils.ZERO_ADDRESS).toByteArray(), AddressUtils.ZERO_ADDRESS, BigInteger.ONE.toByteArray(), msg, 100000, 100000, TransactionTypes.DEFAULT, null);
    Block blk = impl.getAionHub().getBlockchain().createNewMiningBlock(parentBlk, Collections.singletonList(tx), false);
    impl.getAionHub().getBlockchain().tryToConnect(blk);
    Message.req_getTransactionByBlockNumberAndIndex reqBody = Message.req_getTransactionByBlockNumberAndIndex.newBuilder().setBlockNumber(blk.getNumber()).setTxIndex(0).build();
    rsp = sendRequest(Message.Servs.s_chain_VALUE, Message.Funcs.f_getTransactionByBlockNumberAndIndex_VALUE, reqBody.toByteArray());
    assertEquals(Message.Retcode.r_success_VALUE, rsp[1]);
    Message.rsp_getTransaction rslt = Message.rsp_getTransaction.parseFrom(stripHeader(rsp));
    assertEquals(blk.getNumber(), rslt.getBlocknumber());
    assertEquals(ByteString.copyFrom(tx.getData()), rslt.getData());
    assertEquals(tx.getEnergyPrice(), rslt.getNrgPrice());
    rsp = sendRequest(Message.Servs.s_hb_VALUE, Message.Funcs.f_getTransactionByBlockNumberAndIndex_VALUE);
    assertEquals(Message.Retcode.r_fail_service_call_VALUE, rsp[1]);
}
Also used : AionImpl(org.aion.zero.impl.blockchain.AionImpl) Block(org.aion.zero.impl.types.Block) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with AionImpl

use of org.aion.zero.impl.blockchain.AionImpl in project aion by aionnetwork.

the class AionChainHolder method addNewBlock.

@Override
public boolean addNewBlock(Block block) {
    ImportResult result = ((AionImpl) chain).addNewBlock(block);
    logger.info("{} block {} to the blockchain DB <num={}, hash={}, diff={}, tx={}>", block.getHeader().getSealType() == Seal.PROOF_OF_WORK ? "mining" : "staking", result.isSuccessful() ? "sealed" : "cannot seal", block.getNumber(), block.getShortHash(), block.getDifficultyBI(), block.getTransactionsList().size());
    return result.isSuccessful();
}
Also used : AionImpl(org.aion.zero.impl.blockchain.AionImpl) ImportResult(org.aion.zero.impl.core.ImportResult)

Example 4 with AionImpl

use of org.aion.zero.impl.blockchain.AionImpl in project aion by aionnetwork.

the class ApiAion0Test method testProcessGetTxByHash.

@Test
@Ignore
public void testProcessGetTxByHash() throws Exception {
    AionImpl impl = AionImpl.instForTest();
    AionRepositoryImpl repo = AionRepositoryImpl.inst();
    Block parentBlk = impl.getBlockchain().getBestBlock();
    AionTransaction tx = AionTransaction.create(key, repo.getNonce(AddressUtils.ZERO_ADDRESS).toByteArray(), AddressUtils.ZERO_ADDRESS, BigInteger.ONE.toByteArray(), msg, 100000, 100000, TransactionTypes.DEFAULT, null);
    Block blk = impl.getAionHub().getBlockchain().createNewMiningBlock(parentBlk, Collections.singletonList(tx), false);
    impl.getAionHub().getBlockchain().tryToConnect(blk);
    Message.req_getTransactionByHash reqBody = Message.req_getTransactionByHash.newBuilder().setTxHash(ByteString.copyFrom(tx.getTransactionHash())).build();
    rsp = sendRequest(Message.Servs.s_chain_VALUE, Message.Funcs.f_getTransactionByHash_VALUE, reqBody.toByteArray());
    assertEquals(Message.Retcode.r_success_VALUE, rsp[1]);
    Message.rsp_getTransaction rslt = Message.rsp_getTransaction.parseFrom(stripHeader(rsp));
    assertEquals(blk.getNumber(), rslt.getBlocknumber());
    assertEquals(ByteString.copyFrom(tx.getData()), rslt.getData());
    assertEquals(tx.getEnergyPrice(), rslt.getNrgPrice());
    rsp = sendRequest(Message.Servs.s_hb_VALUE, Message.Funcs.f_getTransactionByHash_VALUE);
    assertEquals(Message.Retcode.r_fail_service_call_VALUE, rsp[1]);
}
Also used : AionImpl(org.aion.zero.impl.blockchain.AionImpl) Block(org.aion.zero.impl.types.Block) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with AionImpl

use of org.aion.zero.impl.blockchain.AionImpl in project aion by aionnetwork.

the class MiningRPCImplTest method testTimestampInSubmitSolution.

@Test
public void testTimestampInSubmitSolution() {
    MiningBlock blockWithRightTimestamp = new MiningBlock(new byte[MiningBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[MiningBlockHeader.BLOOM_BYTE_SIZE], new byte[MiningBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000), new byte[0], new byte[1], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), equihashSolution.toBytes(), 0, 0);
    AionImpl aionImpl = mock(AionImpl.class);
    AionBlockchainImpl chainImpl = mock(AionBlockchainImpl.class);
    ChainHolder chainHolder = spy(new AionChainHolder(aionImpl, accountManager));
    doReturn(true).when(chainHolder).isUnityForkEnabled();
    doReturn(chainImpl).when(aionImpl).getBlockchain();
    doReturn(blockWithRightTimestamp).when(chainImpl).getCachingMiningBlockTemplate(blockThatCanBeSealed.toBytes());
    doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp);
    doCallRealMethod().when(chainHolder).submitBlock(nonce.toBytes(), equihashSolution.toBytes(), blockThatCanBeSealed.toBytes());
    rpcMethods = new RPCMethods(chainHolder);
    String method = "submitBlock";
    Request request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
    SubmissionResult submissionResult = execute(request1, SubmissionResultConverter::decode);
    assertNotNull(submissionResult);
    assertTrue(submissionResult.result);
    // Now we test current timestamp + 1 (for testing the clock drift)
    MiningBlock blockWithRightTimestamp1 = new MiningBlock(new byte[MiningBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[MiningBlockHeader.BLOOM_BYTE_SIZE], new byte[MiningBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000) + 1, new byte[0], new byte[1], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), equihashSolution.toBytes(), 0, 0);
    doReturn(blockWithRightTimestamp1).when(chainImpl).getCachingMiningBlockTemplate(blockThatCanBeSealed.toBytes());
    doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp1);
    request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
    submissionResult = execute(request1, SubmissionResultConverter::decode);
    assertNotNull(submissionResult);
    assertTrue(submissionResult.result);
    // Now we test the future block timestamp (timestamp + 2)
    MiningBlock blockWithFutureTimestamp = new MiningBlock(new byte[MiningBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[MiningBlockHeader.BLOOM_BYTE_SIZE], new byte[MiningBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000) + 2, new byte[0], new byte[1], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), equihashSolution.toBytes(), 0, 0);
    doReturn(blockWithFutureTimestamp).when(chainImpl).getCachingMiningBlockTemplate(blockThatCanBeSealed.toBytes());
    doReturn(true).when(chainHolder).addNewBlock(blockWithFutureTimestamp);
    request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
    submissionResult = execute(request1, SubmissionResultConverter::decode);
    assertNotNull(submissionResult);
    assertFalse(submissionResult.result);
}
Also used : AionImpl(org.aion.zero.impl.blockchain.AionImpl) SubmissionResultConverter(org.aion.rpc.types.RPCTypesConverter.SubmissionResultConverter) SubmissionResult(org.aion.rpc.types.RPCTypes.SubmissionResult) Request(org.aion.rpc.types.RPCTypes.Request) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockchainImpl(org.aion.zero.impl.blockchain.AionBlockchainImpl) SubmitBlockParams(org.aion.rpc.types.RPCTypes.SubmitBlockParams) Test(org.junit.Test)

Aggregations

AionImpl (org.aion.zero.impl.blockchain.AionImpl)10 Test (org.junit.Test)9 AionTransaction (org.aion.base.AionTransaction)7 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)7 Block (org.aion.zero.impl.types.Block)7 Ignore (org.junit.Ignore)7 Request (org.aion.rpc.types.RPCTypes.Request)2 AionBlockchainImpl (org.aion.zero.impl.blockchain.AionBlockchainImpl)2 SubmissionResult (org.aion.rpc.types.RPCTypes.SubmissionResult)1 SubmitBlockParams (org.aion.rpc.types.RPCTypes.SubmitBlockParams)1 SubmitSignatureParams (org.aion.rpc.types.RPCTypes.SubmitSignatureParams)1 SubmissionResultConverter (org.aion.rpc.types.RPCTypesConverter.SubmissionResultConverter)1 ImportResult (org.aion.zero.impl.core.ImportResult)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1 StakingBlock (org.aion.zero.impl.types.StakingBlock)1