Search in sources :

Example 31 with IAionBlockchain

use of org.aion.zero.impl.blockchain.IAionBlockchain in project aion by aionnetwork.

the class BeaconHashValidatorTest method validateTxForBlockOnMainchainAndBeaconHashNotGiven.

@Test
public void validateTxForBlockOnMainchainAndBeaconHashNotGiven() {
    long unityForkNumber = 2;
    ForkUtility forkUtility = new ForkUtility();
    forkUtility.enableUnityFork(unityForkNumber);
    IAionBlockchain blockchain = mock(IAionBlockchain.class);
    Block block = mock(Block.class);
    byte[] blockParent = ByteUtil.hexStringToBytes("0x1333333333333333333333333333333333333333333333333333333333333337");
    when(block.getParentHash()).thenReturn(blockParent);
    when(blockchain.isMainChain(blockParent)).thenReturn(true);
    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 - not present
    null);
    BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
    when(block.getNumber()).thenReturn(unityForkNumber - 1);
    assertWithMessage("validation should pass if beacon hash not present").that(unit.validateTxForBlock(tx, block)).isTrue();
    when(block.getNumber()).thenReturn(unityForkNumber);
    assertWithMessage("validation should pass if beacon hash not present").that(unit.validateTxForBlock(tx, block)).isTrue();
    when(block.getNumber()).thenReturn(unityForkNumber + 1);
    assertWithMessage("validation should pass if beacon hash not present").that(unit.validateTxForBlock(tx, block)).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Example 32 with IAionBlockchain

use of org.aion.zero.impl.blockchain.IAionBlockchain in project aion by aionnetwork.

the class BeaconHashValidatorTest method validateTxForPendingStateWithoutBeaconHash.

@Test
public void validateTxForPendingStateWithoutBeaconHash() {
    long unityForkNumber = 3;
    ForkUtility forkUtility = new ForkUtility();
    forkUtility.enableUnityFork(unityForkNumber);
    IAionBlockchain blockchain = mock(IAionBlockchain.class);
    Block bestBlock = mock(Block.class);
    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 - absent
    null);
    BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
    when(bestBlock.getNumber()).thenReturn(unityForkNumber - 2);
    assertWithMessage("validation should pass for pending state if beacon hash absent").that(unit.validateTxForPendingState(tx)).isTrue();
    when(bestBlock.getNumber()).thenReturn(unityForkNumber - 1);
    assertWithMessage("validation should pass for pending state if beacon hash absent").that(unit.validateTxForPendingState(tx)).isTrue();
    when(bestBlock.getNumber()).thenReturn(unityForkNumber);
    assertWithMessage("validation should pass for pending state if beacon hash absent").that(unit.validateTxForPendingState(tx)).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Example 33 with IAionBlockchain

use of org.aion.zero.impl.blockchain.IAionBlockchain in project aion by aionnetwork.

the class RequestTrieDataHandlerTest method testReceive_incorrectMessage.

@Test
public void testReceive_incorrectMessage() {
    Logger log = mock(Logger.class);
    when(log.isTraceEnabled()).thenReturn(false);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestTrieDataHandler handler = new RequestTrieDataHandler(log, chain, p2p);
    // receive incorrect message
    byte[] outOfOderEncoding = RLP.encodeList(RLP.encodeString(STATE.toString()), RLP.encodeElement(nodeKey), RLP.encodeInt(0));
    handler.receive(peerId, displayId, outOfOderEncoding);
    verify(log, times(1)).error("<req-trie decode-error msg-bytes={} peer={}>", outOfOderEncoding.length, displayId);
    verifyZeroInteractions(chain);
    verifyZeroInteractions(p2p);
}
Also used : IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) Logger(org.slf4j.Logger) IP2pMgr(org.aion.p2p.IP2pMgr) Test(org.junit.Test)

Example 34 with IAionBlockchain

use of org.aion.zero.impl.blockchain.IAionBlockchain in project aion by aionnetwork.

the class RequestTrieDataHandlerTest method testReceive_correctMessage_limitZero_akaMaxBatchSize.

@Test
public void testReceive_correctMessage_limitZero_akaMaxBatchSize() {
    Logger log = mock(Logger.class);
    when(log.isDebugEnabled()).thenReturn(true);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    when(chain.getTrieNode(nodeKey, STATE)).thenReturn(leafValue);
    when(chain.getReferencedTrieNodes(leafValue, TRIE_DATA_REQUEST_MAXIMUM_BATCH_SIZE, STATE)).thenReturn(multipleReferences);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestTrieDataHandler handler = new RequestTrieDataHandler(log, chain, p2p);
    // receive correct message
    byte[] encoding = RLP.encodeList(RLP.encodeElement(nodeKey), RLP.encodeString(STATE.toString()), RLP.encodeInt(0));
    handler.receive(peerId, displayId, encoding);
    verify(log, times(1)).debug("<req-trie from-db={} key={} peer={}>", STATE, wrappedNodeKey, displayId);
    verify(chain, times(1)).getTrieNode(nodeKey, STATE);
    verify(chain, times(1)).getReferencedTrieNodes(leafValue, TRIE_DATA_REQUEST_MAXIMUM_BATCH_SIZE, STATE);
    ResponseTrieData expectedResponse = new ResponseTrieData(wrappedNodeKey, leafValue, multipleReferences, STATE);
    verify(p2p, times(1)).send(peerId, displayId, expectedResponse);
}
Also used : ResponseTrieData(org.aion.zero.impl.sync.msg.ResponseTrieData) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) Logger(org.slf4j.Logger) IP2pMgr(org.aion.p2p.IP2pMgr) Test(org.junit.Test)

Example 35 with IAionBlockchain

use of org.aion.zero.impl.blockchain.IAionBlockchain in project aion by aionnetwork.

the class RpcTest method testGetSeed.

@Test
public void testGetSeed() {
    IAionBlockchain chain = impl.aionHub.getBlockchain();
    Block block = chain.getBestBlock();
    List<AionTransaction> txs = Collections.emptyList();
    // expand chain to reach fork point
    block = chain.createNewMiningBlock(block, txs, false);
    ImportResult result = chain.tryToConnect(block);
    assertEquals(ImportResult.IMPORTED_BEST, result);
    block = chain.createNewMiningBlock(block, txs, false);
    result = chain.tryToConnect(block);
    assertEquals(ImportResult.IMPORTED_BEST, result);
    byte[] seed = api.getseed();
    assertNotNull(seed);
    assertArrayEquals(seed, new byte[64]);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Aggregations

IAionBlockchain (org.aion.zero.impl.blockchain.IAionBlockchain)35 Test (org.junit.Test)35 IP2pMgr (org.aion.p2p.IP2pMgr)24 Logger (org.slf4j.Logger)24 Block (org.aion.zero.impl.types.Block)18 AionTransaction (org.aion.base.AionTransaction)11 AionAddress (org.aion.types.AionAddress)10 ForkUtility (org.aion.zero.impl.forks.ForkUtility)10 ResponseBlocks (org.aion.zero.impl.sync.msg.ResponseBlocks)7 RequestBlocks (org.aion.zero.impl.sync.msg.RequestBlocks)5 ResponseTrieData (org.aion.zero.impl.sync.msg.ResponseTrieData)3 LinkedList (java.util.LinkedList)2 ImportResult (org.aion.zero.impl.core.ImportResult)1