use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withNullAddress.
@Test(expected = NullPointerException.class)
public void testDecode_withNullAddress() {
RLPContractDetails input = new RLPContractDetails(null, false, null, null, null);
AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, mockRoot);
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withInLineStorageAndEmptyStorageTrie.
@Test
public void testDecode_withInLineStorageAndEmptyStorageTrie() {
SecureTrie trie = new SecureTrie(null);
Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
for (int i = 0; i < 3; i++) {
byte[] key = RandomUtils.nextBytes(32);
byte[] value = RandomUtils.nextBytes(100);
trie.update(key, RLP.encodeElement(value));
storage.put(ByteArrayWrapper.wrap(key), ByteArrayWrapper.wrap(value));
}
RLPElement storageTrie = RLP.decode2SharedList(trie.serialize());
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] codeBytes = RandomUtils.nextBytes(100);
RLPElement code = mock(SharedRLPItem.class);
when(code.getRLPData()).thenReturn(codeBytes);
byte[] rootHash = RandomUtils.nextBytes(32);
RLPElement root = mock(SharedRLPItem.class);
when(root.getRLPData()).thenReturn(rootHash);
byte[] storageHash = EMPTY_TRIE_HASH;
byte[] graphHash = RandomUtils.nextBytes(32);
byte[] graphBytes = RandomUtils.nextBytes(100);
when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
Logger log = mock(Logger.class);
ByteArrayKeyValueDatabase db = new MockDB("db", log);
db.open();
assertThat(db.isEmpty()).isTrue();
RLPContractDetails input = new RLPContractDetails(address, false, root, storageTrie, code);
AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, db, mockDatabase, rootHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
assertThat(details.getCodes().size()).isEqualTo(1);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
byte[] concatenated = new byte[storageHash.length + graphHash.length];
System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
for (ByteArrayWrapper key : storage.keySet()) {
assertThat(details.get(key)).isNull();
}
assertThat(db.isEmpty()).isFalse();
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withExternalStorageAndMultiCode.
@Test
public void testDecode_withExternalStorageAndMultiCode() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] rootHash = RandomUtils.nextBytes(32);
RLPElement root = mock(RLPItem.class);
when(root.getRLPData()).thenReturn(rootHash);
byte[] codeBytes1 = RandomUtils.nextBytes(100);
byte[] codeBytes2 = RandomUtils.nextBytes(100);
RLPList code = new RLPList();
code.add(new RLPItem(codeBytes1));
code.add(new RLPItem(codeBytes2));
byte[] storageHash = RandomUtils.nextBytes(32);
byte[] graphHash = RandomUtils.nextBytes(32);
byte[] graphBytes = RandomUtils.nextBytes(100);
when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
assertThat(details.getCodes().size()).isEqualTo(2);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes1));
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes2));
assertThat(details.getCode(h256(codeBytes1))).isEqualTo(codeBytes1);
assertThat(details.getCode(h256(codeBytes2))).isEqualTo(codeBytes2);
byte[] concatenated = new byte[storageHash.length + graphHash.length];
System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withIncorrectEncodingForConcatenatedData.
@Test(expected = IllegalArgumentException.class)
public void testDecode_withIncorrectEncodingForConcatenatedData() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] rootHash = RandomUtils.nextBytes(32);
RLPElement root = mock(RLPItem.class);
when(root.getRLPData()).thenReturn(rootHash);
byte[] codeBytes1 = RandomUtils.nextBytes(100);
byte[] codeBytes2 = RandomUtils.nextBytes(100);
RLPList code = new RLPList();
code.add(new RLPItem(codeBytes1));
code.add(new RLPItem(codeBytes2));
byte[] storageHash = RandomUtils.nextBytes(32);
when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeElement(storageHash)));
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class BlockchainAccountStateBenchmark method testExpandContractsStorage.
@Test
public void testExpandContractsStorage() throws InterruptedException {
try {
StandaloneBlockchain.Bundle bundle = this.bundle;
StandaloneBlockchain bc = bundle.bc;
int r = random.nextInt(bundle.privateKeys.size());
ECKey key = bundle.privateKeys.get(r);
// deploy contract
Pair<MiningBlock, byte[]> res = createContract(bc, key, bc.getGenesis());
bc.tryToConnect(res.getLeft());
AionTxInfo info = bc.getTransactionInfo(res.getRight());
assertNotNull(info);
assertThat(info.getReceipt().isValid()).isTrue();
AionTransaction tx = info.getReceipt().getTransaction();
AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
byte[] contractCode = bc.getRepository().getCode(contractAddress);
System.out.println("deployed contract code: " + ByteUtil.toHexString(contractCode));
System.out.println("deployed at: " + contractAddress);
RLPContractDetails acdi = DetailsDataStore.fromEncoding(bc.getRepository().getContractDetails(contractAddress).getEncoded());
assertTrue(acdi.isExternalStorage);
// storage.
for (int i = 0; i < 9; i++) {
createContractBundle(bc, key, bc.getBestBlock(), contractAddress, 50);
}
acdi = DetailsDataStore.fromEncoding(bc.getRepository().getContractDetails(contractAddress).getEncoded());
assertTrue(acdi.isExternalStorage);
} catch (Exception e) {
e.printStackTrace();
} finally {
bundle.bc.getRepository().close();
Thread.sleep(1000L);
}
}
Aggregations