use of org.aion.zero.impl.blockchain.StandaloneBlockchain in project aion by aionnetwork.
the class TaskImportBlocksTest method testFilterBatch_woPruningRestrictions.
@Test
public void testFilterBatch_woPruningRestrictions() {
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
// populate chain at random
generateRandomChain(chain, 3, 1, accounts, 10);
// populate initial input lists
List<Block> batch = new ArrayList<>();
Map<ByteArrayWrapper, Object> imported = new HashMap<>();
Block current = chain.getBestBlock();
while (current.getNumber() > 0) {
batch.add(current);
imported.put(ByteArrayWrapper.wrap(current.getHash()), true);
current = chain.getBlockByHash(current.getParentHash());
}
batch.add(current);
imported.put(ByteArrayWrapper.wrap(current.getHash()), true);
// will filter out all blocks
assertThat(filterBatch(batch, chain, imported)).isEmpty();
// will filter out none of the blocks
assertThat(filterBatch(batch, chain, new HashMap<>())).isEqualTo(batch);
}
use of org.aion.zero.impl.blockchain.StandaloneBlockchain in project aion by aionnetwork.
the class TaskImportBlocksTest method testFilterBatch_wPruningRestrictions.
@Test
public void testFilterBatch_wPruningRestrictions() {
int current_count = 5, height = 10;
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).withRepoConfig(new RepositoryConfig() {
@Override
public String getDbPath() {
return "";
}
@Override
public PruneConfig getPruneConfig() {
// top pruning without archiving
return new PruneConfig() {
@Override
public boolean isEnabled() {
return true;
}
@Override
public boolean isArchived() {
return false;
}
@Override
public int getCurrentCount() {
return current_count;
}
@Override
public int getArchiveRate() {
return 0;
}
};
}
@Override
public Properties getDatabaseConfig(String db_name) {
Properties props = new Properties();
props.setProperty(DatabaseFactory.Props.DB_TYPE, DBVendor.MOCKDB.toValue());
return props;
}
}).build();
StandaloneBlockchain chain = bundle.bc;
// populate chain at random
generateRandomChain(chain, height, 1, accounts, 10);
// populate initial input lists
List<Block> allBlocks = new ArrayList<>();
Map<ByteArrayWrapper, Object> allHashes = new HashMap<>();
List<Block> unrestrictedBlocks = new ArrayList<>();
Map<ByteArrayWrapper, Object> unrestrictedHashes = new HashMap<>();
for (long i = 0; i <= height; i++) {
Block current = chain.getBlockByNumber(i);
allBlocks.add(current);
allHashes.put(ByteArrayWrapper.wrap(current.getHash()), true);
if (i >= height - current_count + 1) {
unrestrictedBlocks.add(current);
unrestrictedHashes.put(ByteArrayWrapper.wrap(current.getHash()), true);
}
}
// will filter out all blocks
assertThat(filterBatch(allBlocks, chain, allHashes)).isEmpty();
// will filter out all blocks
assertThat(filterBatch(allBlocks, chain, unrestrictedHashes)).isEmpty();
// will filter out the prune restricted blocks
assertThat(filterBatch(allBlocks, chain, new HashMap<>())).isEqualTo(unrestrictedBlocks);
}
use of org.aion.zero.impl.blockchain.StandaloneBlockchain in project aion by aionnetwork.
the class TxRecptLgTest method TestTxRecptLg.
@Test
public void TestTxRecptLg() throws InterruptedException, IOException {
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
ECKey deployerAccount = bundle.privateKeys.get(0);
// ======================
// DEPLOY contract A & B
// ======================
Compiler.Result r = Compiler.getInstance().compile(readContract("contract/contract.sol"), Compiler.Options.ABI, Compiler.Options.BIN);
CompilationResult cr = CompilationResult.parse(r.output);
String contractA = cr.contracts.get("A").bin;
String contractB = cr.contracts.get("B").bin;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx1 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
nonce = nonce.add(BigInteger.ONE);
AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractB), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1, tx2), false);
ImportResult result = bc.tryToConnect(context.block);
assertEquals(result, ImportResult.IMPORTED_BEST);
AionAddress addressA = TxUtil.calculateContractAddress(tx1);
System.out.println("contract A address = " + addressA);
AionAddress addressB = TxUtil.calculateContractAddress(tx2);
System.out.println("contract B address = " + addressB);
Thread.sleep(1000);
// ======================
// CALL function A.AA
// ======================
nonce = nonce.add(BigInteger.ONE);
byte[] functionAA = new byte[4];
System.arraycopy(HashUtil.keccak256("AA(address)".getBytes()), 0, functionAA, 0, 4);
AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(functionAA, addressB.toByteArray()), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx3), false);
result = bc.tryToConnect(context.block);
assertEquals(result, ImportResult.IMPORTED_BEST);
AionTxInfo info = bc.getTransactionInfo(tx3.getTransactionHash());
AionTxReceipt receipt = info.getReceipt();
System.out.println(receipt);
assertEquals(4, receipt.getLogInfoList().size());
// ======================
// Test
// ======================
TxRecptLg[] logs = new TxRecptLg[receipt.getLogInfoList().size()];
for (int i = 0; i < logs.length; i++) {
Log logInfo = receipt.getLogInfoList().get(i);
logs[i] = new TxRecptLg(logInfo, context.block, info.getIndex(), receipt.getTransaction(), i, true);
}
String ctAddrA = "0x" + addressA.toString();
String ctAddrB = "0x" + addressB.toString();
// AE
assertEquals(ctAddrA, logs[0].address);
// AEA
assertEquals(ctAddrA, logs[1].address);
// b.BB
assertEquals(ctAddrB, logs[2].address);
// AEB
assertEquals(ctAddrA, logs[3].address);
}
Aggregations