use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class ByteArrayWrapperTest method testWrap.
/**
* 1. Wrap the input data 2. Assert to see if equal
*/
@Test
@Parameters(method = "hexValues")
public void testWrap(String inputString) {
ByteArrayWrapper tempArray;
byte[] inputByte = TestUtil.hexStringToByteArray(inputString);
try {
tempArray = ByteArrayWrapper.wrap(inputByte);
assertEquals(tempArray.toString(), inputString.toLowerCase());
assertArrayEquals(tempArray.toBytes(), tempArray.toBytes());
System.out.println("Valid " + tempArray);
} catch (NullPointerException e) {
System.out.println("Invalid");
}
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class AionBlockchainImpl method getTransactionInfoByAlias.
private Map<ByteArrayWrapper, AionTxInfo> getTransactionInfoByAlias(byte[] innerHash) {
Set<ByteArrayWrapper> metaTxHashes = transactionStore.getAliases(innerHash);
// No aliases found
if (metaTxHashes == null)
return null;
Map<ByteArrayWrapper, AionTxInfo> infoList = new HashMap<>();
for (ByteArrayWrapper metaTxHash : metaTxHashes) {
Map<ByteArrayWrapper, AionTxInfo> metaTxInfos = transactionStore.getTxInfo(metaTxHash.toBytes());
if (metaTxInfos != null) {
infoList.putAll(metaTxInfos);
}
}
// Had metaTx hash, but was not found in mainchain
return infoList;
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class AionBlockchainImpl method tryToConnect.
/**
* Imports a batch of blocks.
*
* @param blockRange the block range to be imported
* @param peerDisplayId the display identifier for the peer who provided the batch
* @return a {@link Triple} containing:
* <ol>
* <li>the best block height after the imports,</li>
* <li>the set of imported hashes,</li>
* <li>the import result for the last imported block</li>
* </ol>
*/
public Triple<Long, Set<ByteArrayWrapper>, ImportResult> tryToConnect(final List<Block> blockRange, String peerDisplayId) {
lock.lock();
try {
ImportResult importResult = null;
Set<ByteArrayWrapper> imported = new HashSet<>();
for (Block block : blockRange) {
Pair<ImportResult, Long> result = tryToConnectWithTimedExecution(new BlockWrapper(block));
importResult = result.getLeft();
long importTime = result.getRight();
// printing additional information when debug is enabled
SYNC_LOG.debug("<import-status: node = {}, hash = {}, number = {}, txs = {}, block time = {}, result = {}, time elapsed = {} ns, block td = {}, chain td = {}>", peerDisplayId, block.getShortHash(), block.getNumber(), block.getTransactionsList().size(), block.getTimestamp(), importResult, importTime, block.getTotalDifficulty(), getTotalDifficulty());
if (checkKernelShutdownForCLI()) {
break;
} else if (!importResult.isStored()) {
// stop at invalid blocks
return Triple.of(bestBlock.getNumber(), imported, importResult);
} else {
imported.add(block.getHashWrapper());
}
}
return Triple.of(bestBlock.getNumber(), imported, importResult);
} finally {
lock.unlock();
checkKernelExit();
}
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class BlockchainImplementationTest method testGetListOfHeadersEndWith_wBestBlock.
@Test
public void testGetListOfHeadersEndWith_wBestBlock() {
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
// populate chain at random
generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
Block best = chain.getBestBlock();
assertThat(best.getNumber()).isGreaterThan(0L);
byte[] bestHash = best.getHash();
// expected result with qty >= chain height
List<ByteArrayWrapper> expectedAll = new ArrayList<>();
Block block = best;
while (block.getNumber() > 0) {
expectedAll.add(ByteArrayWrapper.wrap(block.getHash()));
block = chain.getBlockByHash(block.getParentHash());
}
int qty;
// get one block
qty = 1;
assertEndWith_expectedOne(chain, bestHash, qty);
// get list of chain height from best block
// chain height
qty = (int) best.getNumber();
assertEndWith(chain, bestHash, qty, expectedAll);
// get list of half from best block
qty = (int) (best.getNumber() / 2);
List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
block = best;
for (int i = 0; i < qty; i++) {
expectedHalf.add(ByteArrayWrapper.wrap(block.getHash()));
block = chain.getBlockByHash(block.getParentHash());
}
assertEndWith(chain, bestHash, qty, expectedHalf);
// get list of 20 from genesis
// larger than block height
qty = 24;
expectedAll.add(ByteArrayWrapper.wrap(chain.getGenesis().getHash()));
assertEndWith(chain, bestHash, qty, expectedAll);
// get list of -10 from best block
// negative value
qty = -10;
assertEndWith_expectedOne(chain, bestHash, qty);
// get list of 0 from best block
// zero blocks requested
qty = 0;
assertEndWith_expectedOne(chain, bestHash, qty);
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class BlockchainImplementationTest method testGetListOfHeadersStartFromBlock_wGenesisBlock.
@Test
public void testGetListOfHeadersStartFromBlock_wGenesisBlock() {
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
// populate chain at random
generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
MiningBlock genesis = chain.getGenesis();
Block best = chain.getBestBlock();
assertThat(best.getNumber()).isGreaterThan(0L);
byte[] genesisHash = genesis.getHash();
// expected result with qty >= chain height
List<ByteArrayWrapper> expectedAll = new ArrayList<>();
Block block = chain.getBlockByHash(best.getParentHash());
while (block.getNumber() > 0) {
expectedAll.add(0, ByteArrayWrapper.wrap(block.getHash()));
block = chain.getBlockByHash(block.getParentHash());
}
expectedAll.add(0, ByteArrayWrapper.wrap(genesis.getHash()));
int qty;
// get one block
qty = 1;
assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
// get list of chain height from genesis block
// chain height
qty = (int) best.getNumber();
assertStartFromBlock(chain, 0, qty, expectedAll);
// get list of half from genesis block
qty = (int) (best.getNumber() / 2);
List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
block = chain.getBlockByNumber(qty - 1);
for (int i = 0; i < qty; i++) {
expectedHalf.add(0, ByteArrayWrapper.wrap(block.getHash()));
block = chain.getBlockByHash(block.getParentHash());
}
assertStartFromBlock(chain, 0, qty, expectedHalf);
// get list of 20 from genesis
// larger than block height
qty = 24;
expectedAll.add(ByteArrayWrapper.wrap(best.getHash()));
assertStartFromBlock(chain, 0, qty, expectedAll);
// get list of -10 from genesis block
// negative value
qty = -10;
assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
// get list of 0 from genesis block
// zero blocks requested
qty = 0;
assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
}
Aggregations