use of org.aion.zero.impl.forks.ForkUtility in project aion by aionnetwork.
the class BeaconHashValidatorTest method validateTxForPendingStateWhenBeaconHashNotOnMainchain.
@Test
public void validateTxForPendingStateWhenBeaconHashNotOnMainchain() {
long unityForkNumber = 3;
ForkUtility forkUtility = new ForkUtility();
forkUtility.enableUnityFork(unityForkNumber);
IAionBlockchain blockchain = mock(IAionBlockchain.class);
Block bestBlock = mock(Block.class);
byte[] badBlockhash = ByteUtil.hexStringToBytes("0xbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbad0");
when(blockchain.isMainChain(badBlockhash)).thenReturn(false);
when(blockchain.getBestBlock()).thenReturn(bestBlock);
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 - does not exist in blockstore
badBlockhash);
BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
when(bestBlock.getNumber()).thenReturn(unityForkNumber - 2);
assertWithMessage("any beacon hash should fail for pending state if best_block_number+1 < unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
when(bestBlock.getNumber()).thenReturn(unityForkNumber - 1);
assertWithMessage("non-mainchain hash validation should fail for pending state if best_block_number+1 = unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
when(bestBlock.getNumber()).thenReturn(unityForkNumber);
assertWithMessage("non-mainchain hash validation should fail for pending state if best_block_number+1 > unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
}
use of org.aion.zero.impl.forks.ForkUtility in project aion by aionnetwork.
the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnSideChain.
@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnSideChain() {
/*
* Visualization of this case (x is the block of beacon hash , * is the new block):
*
* o--o--o---o--o main chain
* |
* \--x--o--* side chain
*/
long unityForkNumber = 5;
ForkUtility forkUtility = new ForkUtility();
forkUtility.enableUnityFork(unityForkNumber);
IAionBlockchain blockchain = mock(IAionBlockchain.class);
LinkedList<Block> sideChain = mockChain(unityForkNumber, 4, blockchain);
byte[] beaconHash = new byte[32];
System.arraycopy(sideChain.get(3).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.zero.impl.forks.ForkUtility 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.zero.impl.forks.ForkUtility in project aion by aionnetwork.
the class BeaconHashValidatorTest method isUnityForkActiveAllCombinations.
@Test
public void isUnityForkActiveAllCombinations() {
// unity fork disabled
ForkUtility forkUtility = new ForkUtility();
assertWithMessage("isUnityForkActive should always be false if fork 0.5.0 disabled").that(forkUtility.isUnityForkActive(312l)).isFalse();
assertWithMessage("isUnityForkActive should always be false if fork 0.5.0 disabled").that(forkUtility.isUnityForkActive(Long.MAX_VALUE)).isFalse();
// unity fork enabled after block 1337
forkUtility.enableUnityFork(1337);
assertWithMessage("isUnityForkActive should be false if block_number < unityFork_number").that(forkUtility.isUnityForkActive(1336)).isFalse();
assertWithMessage("isUnityForkActive should be false if block_number = unityFork_number").that(forkUtility.isUnityForkActive(1337)).isFalse();
assertWithMessage("isUnityForkActive should be true if block_number > unityFork_number").that(forkUtility.isUnityForkActive(1338)).isTrue();
}
use of org.aion.zero.impl.forks.ForkUtility 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
}
Aggregations