use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class AionTxInfo method decodeToTxInfo.
private static AionTxInfo decodeToTxInfo(SharedRLPList rlpTxInfo) {
Objects.requireNonNull(rlpTxInfo);
AionTxReceipt receipt = new AionTxReceipt((SharedRLPList) rlpTxInfo.get(INDEX_RECEIPT));
ByteArrayWrapper blockHash = ByteArrayWrapper.wrap(rlpTxInfo.get(INDEX_BLOCK_HASH).getRLPData());
int index;
byte[] txIndex = rlpTxInfo.get(INDEX_TX_INDEX).getRLPData();
if (txIndex == null) {
index = 0;
} else {
index = new BigInteger(1, txIndex).intValue();
}
boolean createdWithInternalTx;
List<InternalTransaction> internalTransactions;
switch(rlpTxInfo.size()) {
case SIZE_OF_OLD_ENCODING:
// old encodings are incomplete since internal tx were not stored
createdWithInternalTx = false;
internalTransactions = null;
break;
case SIZE_WITH_BASE_DATA:
// read the completeness flag from storage
createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
internalTransactions = null;
break;
case SIZE_WITH_INTERNAL_TRANSACTIONS:
// read the completeness flag from storage
createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
// decode the internal transactions
internalTransactions = new ArrayList<>();
SharedRLPList internalTxRlp = (SharedRLPList) rlpTxInfo.get(INDEX_INTERNAL_TX);
for (RLPElement item : internalTxRlp) {
internalTransactions.add(fromRlp((SharedRLPList) item));
}
break;
default:
// incorrect encoding
return null;
}
return new AionTxInfo(receipt, blockHash, index, internalTransactions, createdWithInternalTx);
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class Cache method setDB.
@SuppressWarnings("OptionalGetWithoutIsPresent")
public void setDB(ByteArrayKeyValueStore kvds) {
if (this.dataSource == kvds) {
return;
}
Map<byte[], byte[]> rows = new HashMap<>();
if (this.dataSource == null) {
for (ByteArrayWrapper key : nodes.keySet()) {
Node node = nodes.get(key);
if (node == null) {
rows.put(key.toBytes(), null);
} else if (!node.isDirty()) {
rows.put(key.toBytes(), node.getValue().encode());
}
}
} else {
Iterator<byte[]> iterator = dataSource.keys();
while (iterator.hasNext()) {
byte[] key = iterator.next();
rows.put(key, this.dataSource.get(key).get());
}
try {
this.dataSource.close();
} catch (Exception e) {
LOG.error("Unable to close data source.", e);
}
}
kvds.putBatch(rows);
this.dataSource = kvds;
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class Cache method delete.
public void delete(byte[] key) {
ByteArrayWrapper wrappedKey = wrap(key);
this.nodes.remove(wrappedKey);
if (dataSource != null) {
this.dataSource.delete(key);
}
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class AvmContractDetailsTest 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);
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));
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);
storageHash = trie.getRootHash();
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)).isEqualTo(storage.get(key));
}
assertThat(db.isEmpty()).isFalse();
}
use of org.aion.util.types.ByteArrayWrapper 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();
}
Aggregations