use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class DetailsDataStoreTest method testDecodingWithSizeFiveExternal.
@Test
public void testDecodingWithSizeFiveExternal() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] code = RandomUtils.nextBytes(512);
// create encoding
byte[] rlpAddress = RLP.encodeElement(address.toByteArray());
byte[] rlpIsExternalStorage = RLP.encodeByte((byte) 1);
byte[] root = RandomUtils.nextBytes(32);
byte[] rlpStorageRoot = RLP.encodeElement(root);
byte[] rlpStorageTrie = RLP.encodeElement(EMPTY_BYTE_ARRAY);
byte[] rlpCode = RLP.encodeElement(code);
byte[] encoding = RLP.encodeList(rlpAddress, rlpIsExternalStorage, rlpStorageRoot, rlpStorageTrie, rlpCode);
// decode
RLPContractDetails details = DetailsDataStore.fromEncoding(encoding);
assertThat(details.address).isEqualTo(address);
assertThat(details.isExternalStorage).isTrue();
assertThat(details.storageRoot.getRLPData()).isEqualTo(root);
assertThat(details.storageTrie.getRLPData()).isEqualTo(EMPTY_BYTE_ARRAY);
assertThat(details.code.getRLPData()).isEqualTo(code);
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecode_withInLineStorageAndEmptyStorageTrie.
@Test
public void testDecode_withInLineStorageAndEmptyStorageTrie() {
SecureTrie trie = new SecureTrie(null);
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[] storageHash = EMPTY_TRIE_HASH;
RLPElement root = mock(SharedRLPItem.class);
when(root.getRLPData()).thenReturn(storageHash);
ByteArrayKeyValueDatabase db = new MockDB("db", log);
db.open();
assertThat(db.isEmpty()).isTrue();
RLPContractDetails input = new RLPContractDetails(address, false, root, storageTrie, code);
FvmContractDetails details = FvmContractDetails.decodeAtRoot(input, db, storageHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getCodes().size()).isEqualTo(1);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
assertThat(details.getStorageHash()).isEqualTo(storageHash);
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecode_withNullAddress.
@Test(expected = NullPointerException.class)
public void testDecode_withNullAddress() {
RLPContractDetails input = new RLPContractDetails(null, false, null, null, null);
FvmContractDetails.decodeAtRoot(input, mockDatabase, mockRoot);
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecode_withInLineStorageAndTransition.
@Test
public void testDecode_withInLineStorageAndTransition() {
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);
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);
FvmContractDetails details = FvmContractDetails.decodeAtRoot(input, db, rootHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getCodes().size()).isEqualTo(1);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
byte[] storageHash = trie.getRootHash();
assertThat(details.getStorageHash()).isEqualTo(storageHash);
for (ByteArrayWrapper key : storage.keySet()) {
assertThat(details.get(key)).isEqualTo(storage.get(key));
}
assertThat(db.isEmpty()).isFalse();
}
use of org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withIncorrectListForConcatenatedData.
@Test(expected = IllegalArgumentException.class)
public void testDecode_withIncorrectListForConcatenatedData() {
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.encodeList(RLP.encodeElement(storageHash))));
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
}
Aggregations