Search in sources :

Example 1 with SubmissionResult

use of org.aion.rpc.types.RPCTypes.SubmissionResult 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)

Example 2 with SubmissionResult

use of org.aion.rpc.types.RPCTypes.SubmissionResult in project aion by aionnetwork.

the class MiningRPCImplTest method testSubmitSolution.

@Test
public void testSubmitSolution() {
    String method = "submitBlock";
    try {
        Request request = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatDoesNotExistInCache)));
        execute(request, SubmissionResultConverter::decode);
        fail();
    } catch (RPCExceptions.BlockTemplateNotFoundRPCException e) {
    /*We expect this error*/
    }
    Request request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
    SubmissionResult submissionResult = execute(request1, SubmissionResultConverter::decode);
    assertNotNull(submissionResult);
    assertTrue(submissionResult.result);
    request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCannotBeSealed)));
    submissionResult = execute(request1, SubmissionResultConverter::decode);
    assertNotNull(submissionResult);
    // check that the result can be encoded for return
    assertFalse(submissionResult.result);
    try {
        request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatWillThrow)));
        execute(request1, SubmissionResultConverter::decode);
        fail();
    } catch (RPCExceptions.FailedToSealBlockRPCException e) {
    /*We expect this error*/
    }
}
Also used : SubmissionResultConverter(org.aion.rpc.types.RPCTypesConverter.SubmissionResultConverter) SubmissionResult(org.aion.rpc.types.RPCTypes.SubmissionResult) Request(org.aion.rpc.types.RPCTypes.Request) RPCExceptions(org.aion.rpc.errors.RPCExceptions) SubmitBlockParams(org.aion.rpc.types.RPCTypes.SubmitBlockParams) Test(org.junit.Test)

Aggregations

Request (org.aion.rpc.types.RPCTypes.Request)2 SubmissionResult (org.aion.rpc.types.RPCTypes.SubmissionResult)2 SubmitBlockParams (org.aion.rpc.types.RPCTypes.SubmitBlockParams)2 SubmissionResultConverter (org.aion.rpc.types.RPCTypesConverter.SubmissionResultConverter)2 Test (org.junit.Test)2 RPCExceptions (org.aion.rpc.errors.RPCExceptions)1 AionBlockchainImpl (org.aion.zero.impl.blockchain.AionBlockchainImpl)1 AionImpl (org.aion.zero.impl.blockchain.AionImpl)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1