use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class DetailsDataStore method fromEncoding.
/**
* Extracts an RLPContractDetails object from the RLP encoding.
*
* Accepted encodings:
* <ul>
* <li>{ 0:address, 1:isExternalStorage, 2:storageRoot, 3:storageTrie, 4:code }
* <li>{ 0:address, 1:storageRoot, 2:code }
* </ul>
*
* @param encoding The encoded ContractDetails to decode.
*/
public static RLPContractDetails fromEncoding(byte[] encoding) {
if (encoding == null) {
throw new NullPointerException("Cannot decode ContractDetails from null RLP encoding.");
}
if (encoding.length == 0) {
throw new IllegalArgumentException("Cannot decode ContractDetails from empty RLP encoding.");
}
SharedRLPList decoded = (SharedRLPList) (RLP.decode2SharedList(encoding)).get(0);
int elements = decoded.size();
if (elements == 3 || elements == 5) {
// extract 0:address from the encoding
AionAddress address;
RLPElement addressRLP = decoded.get(0);
if (addressRLP == null || addressRLP.getRLPData() == null || addressRLP.getRLPData().length != AionAddress.LENGTH) {
throw new IllegalArgumentException("Cannot decode ContractDetails with invalid contract address.");
} else {
address = new AionAddress(addressRLP.getRLPData());
}
if (elements == 3) {
return new RLPContractDetails(address, true, decoded.get(1), null, decoded.get(2));
} else {
boolean isExternalStorage = decoded.get(1).getRLPData().length > 0;
return new RLPContractDetails(address, isExternalStorage, decoded.get(2), decoded.get(3), decoded.get(4));
}
} else {
throw new IllegalStateException("Incompatible data storage. Please shutdown the kernel and perform database migration to version 1.0 (Denali) of the kernel as instructed in the release.");
}
}
use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class FvmContractDetailsTest method testEncodingCorrectSize.
@Test
public void testEncodingCorrectSize() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] code = RandomUtils.nextBytes(512);
FvmContractDetails details = new FvmContractDetails(address, mockDatabase);
details.setCode(code);
// ensure correct size after VM type is set
SharedRLPList data = (SharedRLPList) RLP.decode2SharedList(details.getEncoded()).get(0);
assertThat(data.size()).isEqualTo(3);
}
use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class AvmContractDetailsTest method testEncodingCorrectSize.
@Test
public void testEncodingCorrectSize() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] code = RandomUtils.nextBytes(512);
AvmContractDetails details = new AvmContractDetails(address, mockDatabase, mockDatabase);
details.setCode(code);
// ensure correct size after VM type is set
SharedRLPList data = (SharedRLPList) RLP.decode2SharedList(details.getEncoded()).get(0);
assertThat(data.size()).isEqualTo(3);
}
use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class SyncMgr method validateAndAddBlocks.
/**
* @param _nodeIdHashcode int
* @param _displayId String
* @param _bodies List<byte[]> Assemble and validate blocks batch and add batch to import queue
* from network response blocks bodies
*/
public void validateAndAddBlocks(int _nodeIdHashcode, String _displayId, final List<SharedRLPList> _bodies) {
if (_bodies == null)
return;
log.debug("<received-bodies size={} node={}>", _bodies.size(), _displayId);
// the requests are made such that the size varies to better map headers to bodies
ByteArrayWrapper firstNodeRoot = ByteArrayWrapper.wrap(BlockUtil.getTxTrieRootFromUnsafeSource(_bodies.get(0)));
List<BlockHeader> headers = syncHeaderRequestManager.matchAndDropHeaders(_nodeIdHashcode, _bodies.size(), firstNodeRoot);
if (headers == null) {
log.debug("<assemble-and-validate-blocks could not match headers for node={} size={} txTrieRoot={}>", _displayId, _bodies.size(), firstNodeRoot);
return;
}
// assemble batch
List<Block> blocks = new ArrayList<>(_bodies.size());
Iterator<BlockHeader> headerIt = headers.iterator();
Iterator<SharedRLPList> bodyIt = _bodies.iterator();
while (headerIt.hasNext() && bodyIt.hasNext()) {
Block block = BlockUtil.newBlockWithHeaderFromUnsafeSource(headerIt.next(), (SharedRLPList) bodyIt.next().get(0));
if (block == null) {
log.debug("<assemble-and-validate-blocks node={} size={}>", _displayId, _bodies.size());
break;
} else {
blocks.add(block);
}
}
int m = blocks.size();
if (m == 0) {
return;
}
log.debug("<assembled-blocks from={} size={} node={}>", blocks.get(0).getNumber(), blocks.size(), _displayId);
// add batch
syncExecutors.execute(() -> filterBlocks(new BlocksWrapper(_nodeIdHashcode, _displayId, blocks)));
}
use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class BroadcastTx method decode.
/* return the encodedData of the Transaction list, the caller function need to cast the return byte[] array
*/
public static List<byte[]> decode(final byte[] _msgBytes) {
SharedRLPList paramsList = (SharedRLPList) RLP.decode2SharedList(_msgBytes).get(0);
List<byte[]> txl = new ArrayList<>();
for (RLPElement aParamsList : paramsList) {
txl.add(SharedRLPList.getRLPDataCopy((SharedRLPList) aParamsList));
}
return txl;
}
Aggregations