use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnMainchainAfterSidechainFork.
@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnMainchainAfterSidechainFork() {
/*
* Visualization of this case (x is the block of beacon hash , * is the new block):
*
* o--o--o---x--o main chain
* |
* \--o--o--* side chain
*/
long unityForkNumber = 2;
ForkUtility forkUtility = new ForkUtility();
forkUtility.enableUnityFork(unityForkNumber);
IAionBlockchain blockchain = mock(IAionBlockchain.class);
// mocking up a block as if it were on the main chain but not the sidechain
Block mainchainOnlyBlock = mock(Block.class);
long mainchainOnlyBlockNum = 3l;
byte[] mainchainOnlyBlockHash = ByteUtil.hexStringToBytes("0xcafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe");
when(mainchainOnlyBlock.getNumber()).thenReturn(mainchainOnlyBlockNum);
when(blockchain.isMainChain(mainchainOnlyBlockHash)).thenReturn(true);
when(blockchain.getBlockByHash(mainchainOnlyBlockHash)).thenReturn(mainchainOnlyBlock);
// need to set up the side chain so that its head block number is > mainchainOnlyBlockNum
LinkedList<Block> sideChain = mockChain(unityForkNumber, 6, blockchain);
byte[] beaconHash = new byte[32];
System.arraycopy(mainchainOnlyBlockHash, 0, beaconHash, 0, 32);
AionTransaction tx = AionTransaction.createWithoutKey(// nonce - any val
new byte[] { 1 }, new AionAddress(// sender - any val
ByteUtil.hexStringToBytes("0xa000000000000000000000000000000000000000000000000000000000000000")), new AionAddress(// destination - any val
ByteUtil.hexStringToBytes("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")), // value - any val
new byte[] { 1 }, // data - any val
new byte[] {}, // energyLimit - any val
1l, // energyPrice - any val
1l, // type - any val
(byte) 1, // beacon hash
beaconHash);
BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
assertWithMessage("beacon hash not on chain of new block should fail if block_number = unityFork_number").that(unit.validateTxForBlock(tx, sideChain.get(0))).isFalse();
// not doing all the "if new block > unityFork" and "new block < unityFork" cases
// for the side chain test cases because those paths already checked by the
// mainchain tests
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnMainchainBeforeSidechainFork.
@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnMainchainBeforeSidechainFork() {
/*
* Visualization of this case (x is the block of beacon hash , * is the new block):
*
* o--x--o---o--o main chain
* |
* \--o--o--* side chain
*/
long unityForkNumber = 2;
ForkUtility forkUtility = new ForkUtility();
forkUtility.enableUnityFork(unityForkNumber);
IAionBlockchain blockchain = mock(IAionBlockchain.class);
LinkedList<Block> sideChain = mockChain(unityForkNumber, 7, blockchain);
when(blockchain.isMainChain(sideChain.get(2).getHash(), sideChain.get(2).getNumber())).thenReturn(true);
when(blockchain.isMainChain(sideChain.get(4).getHash())).thenReturn(true);
byte[] beaconHash = new byte[32];
System.arraycopy(sideChain.get(4).getHash(), 0, beaconHash, 0, 32);
AionTransaction tx = AionTransaction.createWithoutKey(// nonce - any val
new byte[] { 1 }, new AionAddress(// sender - any val
ByteUtil.hexStringToBytes("0xa000000000000000000000000000000000000000000000000000000000000000")), new AionAddress(// destination - any val
ByteUtil.hexStringToBytes("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")), // value - any val
new byte[] { 1 }, // data - any val
new byte[] {}, // energyLimit - any val
1l, // energyPrice - any val
1l, // type - any val
(byte) 1, // beacon hash
beaconHash);
BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
assertWithMessage("beacon hash on chain of new block should pass if block_number = unityFork_number").that(unit.validateTxForBlock(tx, sideChain.get(0))).isTrue();
// not doing all the "if new block > unityFork" and "new block < unityFork" cases
// for the side chain test cases because those paths already checked by the
// mainchain tests
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class AionTransactionTest method testBeaconHashAbsent.
@Test
public void testBeaconHashAbsent() {
byte[] nonce = RandomUtils.nextBytes(16);
AionAddress to = new AionAddress(RandomUtils.nextBytes(32));
byte[] value = RandomUtils.nextBytes(16);
byte[] data = RandomUtils.nextBytes(64);
long nrg = 0;
long nrgPrice = 0;
byte type = 0;
AionTransaction tx = AionTransaction.create(key, nonce, to, value, data, nrg, nrgPrice, type, null);
assertNull("beacon hash should be null", tx.getBeaconHash());
SharedRLPList decoded = (SharedRLPList) RLP.decode2SharedList(tx.getEncoded()).get(0);
assertEquals("wrong number of elements in RLP encoding of AionTransaction without beacon hash", TxUtil.RLP_TX_SIG, decoded.size() - 1);
AionTransaction tx2 = TxUtil.decodeUsingRlpSharedList(tx.getEncoded());
assertNotNull(tx2);
assertTransactionEquals(tx, tx2);
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class TXValidatorTest method validateTxSignatureSwapForkEnabledReceiverAddressTest.
@Test
public void validateTxSignatureSwapForkEnabledReceiverAddressTest() {
boolean signatureSwapEnabled = true;
byte[] dest = RandomUtils.nextBytes(32);
dest[0] = (byte) 0xa1;
AionTransaction tx = AionTransaction.create(key, nonce, new AionAddress(dest), value, data, nrg, nrgPrice, type, null);
TxResponse response = TXValidator.validateTx(tx, unityForkEnabled, signatureSwapEnabled);
assertEquals(TxResponse.INVALID_TX_DESTINATION, response);
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class TXValidatorTest method validateTxSignatureSwapForkEnabledReceiverAddressIsPreCompiledTest.
@Test
public void validateTxSignatureSwapForkEnabledReceiverAddressIsPreCompiledTest() {
boolean signatureSwapEnabled = true;
for (ContractInfo contractInfo : ContractInfo.values()) {
AionTransaction tx = AionTransaction.create(key, nonce, contractInfo.contractAddress, value, data, nrg, nrgPrice, type, null);
TxResponse response = TXValidator.validateTx(tx, unityForkEnabled, signatureSwapEnabled);
// Note: TOTAL CURRENCY contract has been disabled.
if (contractInfo.contractAddress.equals(ContractInfo.TOTAL_CURRENCY.contractAddress)) {
assertEquals(TxResponse.INVALID_TX_DESTINATION, response);
} else {
assertEquals(TxResponse.SUCCESS, response);
}
}
}
Aggregations