use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getStorageAt.
public RpcMsg eth_getStorageAt(Object _params) {
String _address;
String _index;
Object _bnOrId;
if (_params instanceof JSONArray) {
_address = ((JSONArray) _params).get(0) + "";
_index = ((JSONArray) _params).get(1) + "";
_bnOrId = ((JSONArray) _params).opt(2);
} else if (_params instanceof JSONObject) {
_address = ((JSONObject) _params).get("address") + "";
_index = ((JSONObject) _params).get("index") + "";
_bnOrId = ((JSONObject) _params).opt("block");
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
String bnOrId = formalizeBlockNumberOrId(_bnOrId);
Long bn = parseBnOrId(bnOrId);
if (bn == null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block number");
} else {
DataWord key;
try {
key = new DataWord(ByteUtil.hexStringToBytes(_index));
} catch (Exception e) {
// invalid key
LOG.debug("eth_getStorageAt: invalid storageIndex. Must be <= 16 bytes.");
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid storageIndex. Must be <= 16 bytes.");
}
AionAddress address = AddressUtils.wrapAddress(_address);
Optional<ByteArrayWrapper> storageValue = (bn == BEST_PENDING_BLOCK ? pendingState.getStorageValue(address, key.toWrapper()) : ac.getStorageValue(address, key.toWrapper()));
return storageValue.map(byteArrayWrapper -> new RpcMsg(StringUtils.toJsonHex(byteArrayWrapper.toBytes()))).orElseGet(() -> new RpcMsg(null, RpcError.EXECUTION_ERROR, "Storage value not found"));
}
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class HdlrZmq method getTxWait.
void getTxWait() {
TxWaitingMappingUpdate txWait = null;
try {
txWait = this.api.takeTxWait();
if (txWait.isDummy()) {
// shutdown process
return;
}
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.error("zmq takeTxWait failed! ", e);
}
Map.Entry<ByteArrayWrapper, ByteArrayWrapper> entry = null;
if (txWait != null) {
entry = this.api.getMsgIdMapping().get(txWait.getTxHash());
}
if (entry != null) {
this.api.getPendingStatus().add(new TxPendingStatus(txWait.getTxHash(), entry.getValue(), entry.getKey(), txWait.getState(), txWait.getTxResult(), txWait.getTxReceipt().getError()));
// INCLUDED(3);
if (txWait.getState() == 1 || txWait.getState() == 2) {
this.api.getPendingReceipts().put(txWait.getTxHash(), txWait.getTxReceipt());
} else {
this.api.getPendingReceipts().remove(txWait.getTxHash());
this.api.getMsgIdMapping().remove(txWait.getTxHash());
}
}
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class PersistentMockDB method open.
@Override
public boolean open() {
if (isOpen()) {
return true;
}
LOG.debug("init database {}", this.toString());
// using a regular map since synchronization is handled through the read-write lock
kv = new HashMap<>();
// load file from disk if it exists
File dbFile = new File(path);
if (dbFile.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(dbFile))) {
String text;
while ((text = reader.readLine()) != null) {
String[] line = text.split(":", 2);
ByteArrayWrapper key = ByteArrayWrapper.wrap(convertToByteArray(line[0]));
byte[] value = convertToByteArray(line[1]);
kv.put(key, value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
createFileAndDirectories(dbFile);
}
return isOpen();
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class PersistentMockDB method close.
@Override
public void close() {
// release resources if needed
if (kv != null) {
LOG.info("Closing database " + this.toString());
// save data to disk
File dbFile = new File(path);
if (!dbFile.exists()) {
createFileAndDirectories(dbFile);
}
try (FileWriter writer = new FileWriter(dbFile, false)) {
for (Map.Entry<ByteArrayWrapper, byte[]> entry : kv.entrySet()) {
writer.write(Arrays.toString(entry.getKey().toBytes()) + ":" + Arrays.toString(entry.getValue()) + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// clear data
kv.clear();
}
// set map to null
kv = null;
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class DebugLruDataSource method get.
@Override
public V get(byte[] key) {
ByteArrayWrapper wrappedKey = ByteArrayWrapper.wrap(key);
if (cache.containsKey(wrappedKey)) {
// gather usage data
hits++;
return cache.get(wrappedKey);
} else {
// gather usage data
missed++;
V val = super.get(key);
cache.put(wrappedKey, val);
// logging information on missed caching opportunities
if (log.isTraceEnabled()) {
log.trace("[Database:" + getName() + "] Stack trace for missed cache retrieval: ", new Exception());
}
return val;
}
}
Aggregations