use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class AvmContractDetails method decodeAtRoot.
/**
* Decodes an AvmContractDetails object from the RLP encoding and returns a snapshot to the
* specific point in the blockchain history given by the consensus root hash.
*
* @param input the stored encoding representing the contract details
* @param storageSource the data source for the contract storage data
* @param objectGraphSource the data source for the object graph
* @param consensusRoot the consensus root linking to specific external storage and object graph
* data at the point of interest in the blockchain history
* @return a snapshot of the contract details with the information it contained at the specified
* point in the blockchain history
*/
public static AvmContractDetails decodeAtRoot(RLPContractDetails input, ByteArrayKeyValueStore storageSource, ByteArrayKeyValueStore objectGraphSource, byte[] consensusRoot) {
Objects.requireNonNull(input, "The contract data for the snapshot cannot be null.");
Objects.requireNonNull(consensusRoot, "The consensus root for the snapshot cannot be null.");
// additional null check are performed by the constructor
AvmContractDetails details = new AvmContractDetails(input.address, storageSource, objectGraphSource);
RLPElement code = input.code;
if (code instanceof SharedRLPList) {
for (RLPElement e : ((SharedRLPList) code)) {
if (e.isList()) {
details.setCode(SharedRLPList.getRLPDataCopy((SharedRLPList) e));
} else {
details.setCode(e.getRLPData());
}
}
} else if (code instanceof RLPList) {
for (RLPElement e : ((RLPList) code)) {
details.setCode(e.getRLPData());
}
} else {
details.setCode(code.getRLPData());
}
// The root is the concatenated storage hash.
// It points to the external storage hash and the object graph hash.
RLPElement storage = input.storageTrie;
// Instantiates the storage interpreting the storage root according to the VM specification.
byte[] storageRootHash;
Optional<byte[]> concatenatedData = details.objectGraphSource.get(consensusRoot);
if (concatenatedData.isPresent()) {
SharedRLPList data = RLP.decode2SharedList(concatenatedData.get());
if (!(data.get(0).isList())) {
throw new IllegalArgumentException("Invalid concatenated storage for AVM.");
}
SharedRLPList pair = (SharedRLPList) data.get(0);
if (pair.size() != 2) {
throw new IllegalArgumentException("Invalid concatenated storage for AVM.");
}
storageRootHash = pair.get(0).getRLPData();
details.objectGraphHash = pair.get(1).getRLPData();
} else {
// As a result the concatenated storage hash cannot be missing from the database.
throw new IllegalArgumentException("Invalid concatenated storage for AVM.");
}
// load/deserialize storage trie
if (input.isExternalStorage) {
// ensure transition from old encoding
details.storageTrie = new SecureTrie(details.externalStorageSource, storageRootHash);
} else {
details.storageTrie = new SecureTrie(null);
details.storageTrie.deserialize((SharedRLPList) storage);
// switch from in-memory to external storage
details.storageTrie.getCache().setDB(details.externalStorageSource);
details.storageTrie.sync();
}
if (Arrays.equals(storageRootHash, ConstantUtil.EMPTY_TRIE_HASH)) {
details.storageTrie = new SecureTrie(details.storageTrie.getCache(), "".getBytes());
}
return details;
}
use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class FvmContractDetails method decodeAtRoot.
/**
* Decodes an FvmContractDetails object from the RLP encoding and returns a snapshot to the
* specific point in the blockchain history given by the consensus root hash.
*
* @param input the stored encoding representing the contract details
* @param storageSource the data source for the contract storage data
* @param consensusRoot the consensus root linking to specific external storage and object graph
* data at the point of interest in the blockchain history
* @return a snapshot of the contract details with the information it contained at the specified
* point in the blockchain history
*/
public static FvmContractDetails decodeAtRoot(RLPContractDetails input, ByteArrayKeyValueStore storageSource, byte[] consensusRoot) {
Objects.requireNonNull(input, "The contract data for the snapshot cannot be null.");
Objects.requireNonNull(consensusRoot, "The consensus root for the snapshot cannot be null.");
// additional null check are performed by the constructor
FvmContractDetails details = new FvmContractDetails(input.address, storageSource);
RLPElement code = input.code;
if (code instanceof SharedRLPList) {
for (RLPElement e : ((SharedRLPList) code)) {
if (e.isList()) {
details.setCode(SharedRLPList.getRLPDataCopy((SharedRLPList) e));
} else {
details.setCode(e.getRLPData());
}
}
} else if (code instanceof RLPList) {
for (RLPElement e : ((RLPList) code)) {
details.setCode(e.getRLPData());
}
} else {
details.setCode(code.getRLPData());
}
// NOTE: under normal circumstances the VM type is set by the details data store
// Do not forget to set the vmType value externally during tests!!!
RLPElement storage = input.storageTrie;
// load/deserialize storage trie
if (input.isExternalStorage) {
// ensure transition from old encoding
details.storageTrie = new SecureTrie(details.externalStorageSource, consensusRoot);
} else {
details.storageTrie = new SecureTrie(null);
details.storageTrie.deserialize((SharedRLPList) storage);
// switch from in-memory to external storage
details.storageTrie.getCache().setDB(details.externalStorageSource);
details.storageTrie.sync();
}
if (Arrays.equals(consensusRoot, ConstantUtil.EMPTY_TRIE_HASH)) {
details.storageTrie = new SecureTrie(details.storageTrie.getCache(), "".getBytes());
}
return details;
}
use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class AbstractBlockSummary method decodeMap.
protected static <K, V> Map<K, V> decodeMap(RLPList list, Functional.Function<byte[], K> keyDecoder, Functional.Function<byte[], V> valueDecoder) {
Map<K, V> result = new HashMap<>();
for (RLPElement entry : list) {
K key = keyDecoder.apply(((RLPList) entry).get(0).getRLPData());
V value = valueDecoder.apply(((RLPList) entry).get(1).getRLPData());
result.put(key, value);
}
return result;
}
use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class CipherParams method parse.
public static CipherParams parse(byte[] bytes) throws UnsupportedEncodingException {
RLPList list = (RLPList) RLP.decode2(bytes).get(0);
CipherParams cp = new CipherParams();
cp.setIv(new String(list.get(0).getRLPData(), "US-ASCII"));
return cp;
}
use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class KeystoreCrypto method parse.
public static KeystoreCrypto parse(byte[] bytes) throws UnsupportedEncodingException {
RLPList list = (RLPList) RLP.decode2(bytes).get(0);
KeystoreCrypto kc = new KeystoreCrypto();
kc.setCipher(new String(list.get(0).getRLPData(), "UTF-8"));
kc.setCipherText(new String(list.get(1).getRLPData(), "US-ASCII"));
kc.setKdf(new String(list.get(2).getRLPData(), "UTF-8"));
kc.setMac(new String(list.get(3).getRLPData(), "US-ASCII"));
kc.setCipherParams(CipherParams.parse(list.get(4).getRLPData()));
kc.setKdfParams(KdfParams.parse(list.get(5).getRLPData()));
return kc;
}
Aggregations