Search in sources :

Example 31 with ByteArrayWrapper

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"));
    }
}
Also used : Arrays(java.util.Arrays) ApiTxResponse(org.aion.api.server.ApiTxResponse) ArgTxCall(org.aion.api.server.types.ArgTxCall) Seal(org.aion.zero.impl.types.BlockHeader.Seal) ListIterator(java.util.ListIterator) FltrTx(org.aion.api.server.types.FltrTx) Version(org.aion.zero.impl.Version) Tx(org.aion.api.server.types.Tx) NodeWrapper(org.aion.zero.impl.sync.NodeWrapper) LRUMap(org.apache.commons.collections4.map.LRUMap) CfgApiZmq(org.aion.zero.impl.config.CfgApiZmq) AionTxReceipt(org.aion.base.AionTxReceipt) AionImpl(org.aion.zero.impl.blockchain.AionImpl) FltrBlk(org.aion.api.server.types.FltrBlk) CfgApiNrg(org.aion.zero.impl.config.CfgApiNrg) BigDecimal(java.math.BigDecimal) JSONObject(org.json.JSONObject) AddressUtils(org.aion.util.types.AddressUtils) Map(java.util.Map) BigInteger(java.math.BigInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) ZoneOffset(java.time.ZoneOffset) Block(org.aion.zero.impl.types.Block) AionAddress(org.aion.types.AionAddress) RoundingMode(java.math.RoundingMode) FltrLg(org.aion.api.server.types.FltrLg) HexConvert.hexStringToBytes(org.aion.util.types.HexConvert.hexStringToBytes) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Fltr(org.aion.api.server.types.Fltr) Keystore(org.aion.zero.impl.keystore.Keystore) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) Instant(java.time.Instant) ByteUtil(org.aion.util.bytes.ByteUtil) EventCallback(org.aion.evtmgr.impl.callback.EventCallback) TxRecpt(org.aion.api.server.types.TxRecpt) HashUtil(org.aion.crypto.HashUtil) TransactionTypes(org.aion.base.TransactionTypes) Base64(java.util.Base64) List(java.util.List) StringUtils(org.aion.util.string.StringUtils) CfgNet(org.aion.zero.impl.config.CfgNet) Entry(java.util.Map.Entry) Optional(java.util.Optional) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) Hex.toHexString(org.aion.util.conversions.Hex.toHexString) SyncInfo(org.aion.api.server.types.SyncInfo) ECKey(org.aion.crypto.ECKey) CfgAion(org.aion.zero.impl.config.CfgAion) BlockContext(org.aion.zero.impl.types.BlockContext) ArgFltr(org.aion.api.server.types.ArgFltr) CfgApiRpc(org.aion.zero.impl.config.CfgApiRpc) AccountState(org.aion.base.AccountState) CacheLoader(com.github.benmanes.caffeine.cache.CacheLoader) HashMap(java.util.HashMap) CfgApi(org.aion.zero.impl.config.CfgApi) CfgNetP2p(org.aion.zero.impl.config.CfgNetP2p) Function(java.util.function.Function) NumericalValue(org.aion.api.server.types.NumericalValue) ArrayList(java.util.ArrayList) DataWord(org.aion.util.types.DataWord) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) AccountManager(org.aion.api.server.account.AccountManager) IEventMgr(org.aion.evtmgr.IEventMgr) CfgTx(org.aion.zero.impl.config.CfgTx) IAionChain(org.aion.zero.impl.blockchain.IAionChain) StakingBlock(org.aion.zero.impl.types.StakingBlock) CfgSsl(org.aion.zero.impl.config.CfgSsl) CfgSync(org.aion.zero.impl.config.CfgSync) LinkedList(java.util.LinkedList) CfgEnergyStrategy(org.aion.zero.impl.config.CfgEnergyStrategy) ApiAion(org.aion.api.server.ApiAion) IHandler(org.aion.evtmgr.IHandler) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Blk(org.aion.api.server.types.Blk) BLOCKS_QUERY_MAX(org.aion.api.server.types.FltrLg.BLOCKS_QUERY_MAX) Evt(org.aion.api.server.types.Evt) Log(org.aion.types.Log) ImportResult(org.aion.zero.impl.core.ImportResult) CompiledContr(org.aion.api.server.types.CompiledContr) AionBlockchainImpl(org.aion.zero.impl.blockchain.AionBlockchainImpl) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) EMPTY_BYTE_ARRAY(org.aion.util.bytes.ByteUtil.EMPTY_BYTE_ARRAY) CfgConsensusUnity(org.aion.zero.impl.config.CfgConsensusUnity) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) StakingBlockHeader(org.aion.zero.impl.types.StakingBlockHeader) AionTransaction(org.aion.base.AionTransaction) Collections(java.util.Collections) JSONArray(org.json.JSONArray) AionAddress(org.aion.types.AionAddress) JSONObject(org.json.JSONObject) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) DataWord(org.aion.util.types.DataWord) Hex.toHexString(org.aion.util.conversions.Hex.toHexString)

Example 32 with ByteArrayWrapper

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());
        }
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) TxWaitingMappingUpdate(org.aion.api.server.pb.TxWaitingMappingUpdate) Map(java.util.Map) TxPendingStatus(org.aion.api.server.types.TxPendingStatus)

Example 33 with ByteArrayWrapper

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();
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 34 with ByteArrayWrapper

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;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with ByteArrayWrapper

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;
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5