Search in sources :

Example 16 with Request

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

the class StakingRPCImplTest method testTimeStampInSubmitSignature.

@Test
public void testTimeStampInSubmitSignature() {
    StakingBlock blockWithRightTimestamp = new StakingBlock(new byte[StakingBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[StakingBlockHeader.BLOOM_BYTE_SIZE], new byte[StakingBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000), new byte[0], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), 0, 0, DEFAULT_SIGNATURE, validSeed.toBytes(), validSigningPublicKey.toBytes());
    byte[] validSealHash = blockWithRightTimestamp.getHeader().getMineHash();
    AionImpl aionImpl = mock(AionImpl.class);
    AionBlockchainImpl chainImpl = mock(AionBlockchainImpl.class);
    chainHolder = spy(new AionChainHolder(aionImpl, accountManager));
    doReturn(true).when(chainHolder).isUnityForkEnabled();
    doReturn(chainImpl).when(aionImpl).getBlockchain();
    doReturn(blockWithRightTimestamp).when(chainImpl).getCachingStakingBlockTemplate(validSealHash);
    doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp);
    doCallRealMethod().when(chainHolder).submitSignature(validSignature.toBytes(), validSealHash);
    rpcMethods = new RPCMethods(chainHolder);
    String method = "submitsignature";
    assertTrue(execute(new Request(1, method, SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, ByteArray.wrap(validSealHash))), VersionType.Version2), BoolConverter::decode));
    // Now we test current timestamp + 1 (for testing the clock drift)
    StakingBlock blockWithRightTimestamp1 = new StakingBlock(new byte[StakingBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[StakingBlockHeader.BLOOM_BYTE_SIZE], new byte[StakingBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000 + 1), new byte[0], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), 0, 0, DEFAULT_SIGNATURE, validSeed.toBytes(), validSigningPublicKey.toBytes());
    validSealHash = blockWithRightTimestamp1.getHeader().getMineHash();
    doReturn(blockWithRightTimestamp1).when(chainImpl).getCachingStakingBlockTemplate(validSealHash);
    doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp1);
    assertTrue(execute(new Request(1, method, SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, ByteArray.wrap(validSealHash))), VersionType.Version2), BoolConverter::decode));
    // Now we test the future block timestamp (timestamp + 2)
    StakingBlock futureBlock = new StakingBlock(new byte[StakingBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[StakingBlockHeader.BLOOM_BYTE_SIZE], new byte[StakingBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000 + 2), new byte[0], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), 0, 0, DEFAULT_SIGNATURE, validSeed.toBytes(), validSigningPublicKey.toBytes());
    validSealHash = futureBlock.getHeader().getMineHash();
    doReturn(futureBlock).when(chainImpl).getCachingStakingBlockTemplate(validSealHash);
    doReturn(true).when(chainHolder).addNewBlock(futureBlock);
    assertTrue(execute(new Request(1, method, SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, ByteArray.wrap(validSealHash))), VersionType.Version2), BoolConverter::decode));
}
Also used : AionImpl(org.aion.zero.impl.blockchain.AionImpl) SubmitSignatureParams(org.aion.rpc.types.RPCTypes.SubmitSignatureParams) Request(org.aion.rpc.types.RPCTypes.Request) AionBlockchainImpl(org.aion.zero.impl.blockchain.AionBlockchainImpl) StakingBlock(org.aion.zero.impl.types.StakingBlock) 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