Search in sources :

Example 11 with Request

use of org.aion.rpc.types.RPCTypes.Request 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 12 with Request

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

Example 13 with Request

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

the class PersonalRPCImplTest method testPersonal_newAccount.

@Test
public void testPersonal_newAccount() {
    AionAddress expectedAddress = new AionAddress(ByteUtil.hexStringToBytes("a07913c03686c9659c1b614d098fd1db380a52b71fd58526b53d8107f7b355d5"));
    String password = "password";
    doReturn(expectedAddress).when(chainHolder).newAccount(anyString());
    Request request = new Request(idGenerator.generateID(), newAccountMethod, PasswordParamsConverter.encode(new PasswordParams(password)), VersionType.Version2);
    AionAddress responseAddress = execute(request, AddressConverter::decode);
    assertEquals(expectedAddress, responseAddress);
}
Also used : AionAddress(org.aion.types.AionAddress) PasswordParams(org.aion.rpc.types.RPCTypes.PasswordParams) Request(org.aion.rpc.types.RPCTypes.Request) AddressConverter(org.aion.rpc.types.RPCTypesConverter.AddressConverter) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 14 with Request

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

the class Web3EntryPoint method call.

public String call(String requestString) {
    logger.debug("Received request: {}", requestString);
    Request request = null;
    RpcError err = null;
    Integer id = null;
    Object resultUnion = null;
    Stopwatch stopwatch = null;
    if (logger.isDebugEnabled()) {
        stopwatch = Stopwatch.createStarted();
    }
    try {
        request = readRequest(requestString);
        id = request.id;
        if (checkMethod(request.method)) {
            resultUnion = RPCServerMethods.execute(request, rpc);
        } else {
            logger.debug("Request attempted to call a method on a disabled interface: {}", request.method);
            err = InvalidRequestRPCException.INSTANCE.getError();
        }
    } catch (InvalidRequestRPCException e) {
        // Don't log this error since it may already be logged elsewhere
        err = e.getError();
    } catch (RPCException e) {
        logger.debug("Request failed due to an RPC exception: {}", e.getMessage());
        err = e.getError();
    } catch (Exception e) {
        logger.error("Call to {} failed.", request == null ? "null" : request.method);
        logger.error("Request failed due to an internal error: ", e);
        err = new InternalErrorRPCException(e.getClass().getSimpleName() + ":" + e.getMessage()).getError();
    // creating an instance of RpcError
    }
    final String resultString = ResponseConverter.encodeStr(new Response(id, resultUnion, err, VersionType.Version2));
    if (stopwatch != null) {
        logger.debug("Produced response: {}bytes in <{}>ms", resultString.getBytes().length, stopwatch.elapsed().toMillis());
    }
    return resultString;
}
Also used : Response(org.aion.rpc.types.RPCTypes.Response) InternalErrorRPCException(org.aion.rpc.errors.RPCExceptions.InternalErrorRPCException) InvalidRequestRPCException(org.aion.rpc.errors.RPCExceptions.InvalidRequestRPCException) RPCException(org.aion.rpc.errors.RPCExceptions.RPCException) Request(org.aion.rpc.types.RPCTypes.Request) RpcError(org.aion.rpc.types.RPCTypes.RpcError) Stopwatch(com.google.common.base.Stopwatch) InternalErrorRPCException(org.aion.rpc.errors.RPCExceptions.InternalErrorRPCException) InternalErrorRPCException(org.aion.rpc.errors.RPCExceptions.InternalErrorRPCException) InvalidRequestRPCException(org.aion.rpc.errors.RPCExceptions.InvalidRequestRPCException) RPCException(org.aion.rpc.errors.RPCExceptions.RPCException) InvalidRequestRPCException(org.aion.rpc.errors.RPCExceptions.InvalidRequestRPCException)

Example 15 with Request

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

the class StakingRPCImplTest method testSubmitSignature.

@Test
public void testSubmitSignature() {
    assertTrue(execute(new Request(1, "submitsignature", SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, validSealHash)), VersionType.Version2), BoolConverter::decode));
    assertFalse(execute(new Request(1, "submitsignature", SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(invalidSignature, invalidSealHash)), VersionType.Version2), BoolConverter::decode));
    try {
        execute(new Request(1, "submitsignature", SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(invalidSignature, missingSealHash)), null), BoolConverter::decode);
        fail();
    } catch (BlockTemplateNotFoundRPCException e) {
    // We expect this exception
    }
}
Also used : SubmitSignatureParams(org.aion.rpc.types.RPCTypes.SubmitSignatureParams) Request(org.aion.rpc.types.RPCTypes.Request) BoolConverter(org.aion.rpc.types.RPCTypesConverter.BoolConverter) BlockTemplateNotFoundRPCException(org.aion.rpc.errors.RPCExceptions.BlockTemplateNotFoundRPCException) Test(org.junit.Test)

Aggregations

Request (org.aion.rpc.types.RPCTypes.Request)16 Test (org.junit.Test)15 AionAddress (org.aion.types.AionAddress)5 VoidParams (org.aion.rpc.types.RPCTypes.VoidParams)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 SubmissionResult (org.aion.rpc.types.RPCTypes.SubmissionResult)2 SubmitBlockParams (org.aion.rpc.types.RPCTypes.SubmitBlockParams)2 SubmitSignatureParams (org.aion.rpc.types.RPCTypes.SubmitSignatureParams)2 AddressConverter (org.aion.rpc.types.RPCTypesConverter.AddressConverter)2 SubmissionResultConverter (org.aion.rpc.types.RPCTypesConverter.SubmissionResultConverter)2 AionBlockchainImpl (org.aion.zero.impl.blockchain.AionBlockchainImpl)2 AionImpl (org.aion.zero.impl.blockchain.AionImpl)2 Stopwatch (com.google.common.base.Stopwatch)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 AccountManager (org.aion.api.server.account.AccountManager)1 ECKey (org.aion.crypto.ECKey)1 RPCExceptions (org.aion.rpc.errors.RPCExceptions)1 BlockTemplateNotFoundRPCException (org.aion.rpc.errors.RPCExceptions.BlockTemplateNotFoundRPCException)1 InternalErrorRPCException (org.aion.rpc.errors.RPCExceptions.InternalErrorRPCException)1