use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.
the class AionChainHolder method submitSignature.
@Override
public boolean submitSignature(byte[] signature, byte[] sealHash) {
if (!isUnityForkEnabled())
throw new UnsupportedOperationException();
else {
StakingBlock stakingBlock = chain.getBlockchain().getCachingStakingBlockTemplate(sealHash);
logger.debug("submitSignature: sig[{}], sealHash[{}], block[{}]", ByteUtil.toHexString(signature), ByteUtil.toHexString(sealHash), stakingBlock);
if (!stakingBlock.isSealed() && Arrays.equals(sealHash, stakingBlock.getHeader().getMineHash())) {
stakingBlock.seal(signature, stakingBlock.getHeader().getSigningPublicKey());
boolean result = addSealedBlockToPool(stakingBlock, TimeUnit.SECONDS.toMillis(stakingBlock.getTimestamp()) - System.currentTimeMillis());
logSealedBlock(stakingBlock);
return result;
} else {
logFailedSealedBlock(stakingBlock);
return false;
}
}
}
use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.
the class OpsRPCImplTest method setup.
@Before
public void setup() {
emptyPowBlock = MiningBlock.newEmptyBlock();
emptyPowBlock.setMainChain();
AionTxReceipt receipt = new AionTxReceipt();
ECKey ecKey = ECKeyFac.inst().create();
receipt.setError("");
receipt.setExecutionResult(HashUtil.h256(BigInteger.ONE.toByteArray()));
List<Log> infos = new ArrayList<>();
receipt.setLogs(infos);
receipt.setPostTxState(HashUtil.h256(BigInteger.ONE.toByteArray()));
txInfo = AionTxInfo.newInstanceWithInternalTransactions(receipt, ByteArrayWrapper.wrap(HashUtil.h256(BigInteger.ZERO.toByteArray())), 0, Collections.emptyList());
txInfo.getReceipt().setTransaction(AionTransaction.create(ecKey, BigInteger.ZERO.toByteArray(), new AionAddress(ecKey.getAddress()), BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), 10, 10, (byte) 0b1, HashUtil.h256(BigInteger.ZERO.toByteArray())));
transactionHash = ByteArray.wrap(txInfo.getReceipt().getTransaction().getTransactionHash());
txList.add(txInfo.getReceipt().getTransaction());
StakingBlockHeader.Builder builder = StakingBlockHeader.Builder.newInstance().withDefaultCoinbase().withDefaultDifficulty().withDefaultExtraData().withDefaultLogsBloom().withDefaultParentHash().withDefaultReceiptTrieRoot().withDefaultSeed().withDefaultSignature().withDefaultSigningPublicKey().withDefaultStateRoot().withDefaultTxTrieRoot();
emptyPosBlock = new StakingBlock(builder.build(), txList);
doReturn(BigInteger.ONE).when(holder).calculateReward(any());
doReturn(emptyPowBlock).when(holder).getBlockByNumber(1);
doReturn(emptyPosBlock).when(holder).getBlockByNumber(2);
doReturn(emptyPosBlock).when(holder).getBlockByHash(emptyPosBlock.getHash());
doReturn(emptyPowBlock).when(holder).getBlockByHash(emptyPowBlock.getHash());
doReturn(emptyPowBlock).when(holder).getBestBlock();
doReturn(txInfo).when(holder).getTransactionInfo(any());
doReturn(emptyPosBlock).when(holder).getBlockByHash(eq(ByteArrayWrapper.wrap(HashUtil.h256(BigInteger.ZERO.toByteArray())).toBytes()));
doReturn(BigInteger.ONE).when(holder).getTotalDifficultyByHash(any());
doReturn(new AccountState(BigInteger.TEN, BigInteger.TEN)).when(holder).getAccountState(any());
}
use of org.aion.zero.impl.types.StakingBlock 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));
}
Aggregations