Search in sources :

Example 16 with StakingBlock

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;
        }
    }
}
Also used : StakingBlock(org.aion.zero.impl.types.StakingBlock)

Example 17 with StakingBlock

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());
}
Also used : AionAddress(org.aion.types.AionAddress) Log(org.aion.types.Log) StakingBlockHeader(org.aion.zero.impl.types.StakingBlockHeader) ArrayList(java.util.ArrayList) ECKey(org.aion.crypto.ECKey) AionTxReceipt(org.aion.base.AionTxReceipt) AccountState(org.aion.base.AccountState) StakingBlock(org.aion.zero.impl.types.StakingBlock) Before(org.junit.Before)

Example 18 with StakingBlock

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));
}
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

StakingBlock (org.aion.zero.impl.types.StakingBlock)18 MiningBlock (org.aion.zero.impl.types.MiningBlock)10 Block (org.aion.zero.impl.types.Block)7 AionAddress (org.aion.types.AionAddress)6 BigInteger (java.math.BigInteger)4 AionTransaction (org.aion.base.AionTransaction)4 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)4 GenesisStakingBlock (org.aion.zero.impl.types.GenesisStakingBlock)4 RetValidPreBlock (org.aion.zero.impl.types.RetValidPreBlock)4 BlockDetailsValidator.isValidBlock (org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock)4 JSONObject (org.json.JSONObject)4 ArrayList (java.util.ArrayList)3 AionBlockchainImpl (org.aion.zero.impl.blockchain.AionBlockchainImpl)3 AccountState (org.aion.base.AccountState)2 ImportResult (org.aion.zero.impl.core.ImportResult)2 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)2 StakingBlockHeader (org.aion.zero.impl.types.StakingBlockHeader)2 JSONArray (org.json.JSONArray)2 ByteString (com.google.protobuf.ByteString)1 ArrayDeque (java.util.ArrayDeque)1