Search in sources :

Example 46 with Transaction

use of neo.model.core.Transaction in project neo-java by coranos.

the class RpcServerUtil method onGetAccountList.

/**
 * returns the account list for accounts that were active between the given
 * timestamps.
 *
 * @param controller
 *            the controller to use.
 * @param id
 *            the id to use.
 * @param params
 *            the parameters to use.
 * @return the list of account data.
 */
private static JSONObject onGetAccountList(final LocalControllerNode controller, final int id, final JSONArray params) {
    try {
        LOG.trace("getaccountlist 0");
        final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
        final long fromTs = params.getLong(0);
        final long toTs = params.getLong(1);
        final long minHeight = 0;
        final long maxHeight = blockDb.getBlockCount();
        final long fromHeight = getHeightOfTs(controller, 0, minHeight, maxHeight, fromTs);
        final long toHeight = getHeightOfTs(controller, 0, fromHeight, maxHeight, toTs);
        LOG.trace("getaccountlist 1 fromHeight:{};toHeight:{};", fromHeight, toHeight);
        LOG.trace("getaccountlist 2 accountStateCache STARTED");
        final Map<UInt160, Map<UInt256, Fixed8>> addressStateCache = blockDb.getAccountAssetValueMap();
        LOG.trace("getaccountlist 2 accountStateCache SUCCESS, count:{}", addressStateCache.size());
        final Map<UInt160, Long> neoTxByAddress = new TreeMap<>();
        final Map<UInt160, Long> gasTxByAddress = new TreeMap<>();
        final Map<UInt160, Long> claimTxByAddress = new TreeMap<>();
        final Map<UInt160, Long> neoInByAddress = new TreeMap<>();
        final Map<UInt160, Long> gasInByAddress = new TreeMap<>();
        final Map<UInt160, Long> neoOutByAddress = new TreeMap<>();
        final Map<UInt160, Long> gasOutByAddress = new TreeMap<>();
        final Map<UInt160, Long> firstTsByAddress = new TreeMap<>();
        final Map<UInt160, Long> lastTsByAddress = new TreeMap<>();
        for (long index = fromHeight; index < toHeight; index++) {
            LOG.trace("getaccountlist 3 fromHeight:{};toHeight:{};index:{};", fromHeight, toHeight, index);
            final Block block = blockDb.getFullBlockFromHeight(index);
            for (final Transaction t : block.getTransactionList()) {
                final Map<UInt160, Map<UInt256, Long>> addressAssetMap = getAddressAssetMap(blockDb, t);
                for (final UInt160 friend : addressAssetMap.keySet()) {
                    if (!firstTsByAddress.containsKey(friend)) {
                        firstTsByAddress.put(friend, block.timestamp.asLong());
                    }
                    lastTsByAddress.put(friend, block.timestamp.asLong());
                    if (t.type.equals(TransactionType.CLAIM_TRANSACTION)) {
                        MapUtil.increment(claimTxByAddress, friend);
                    }
                    if (addressAssetMap.get(friend).containsKey(ModelUtil.NEO_HASH)) {
                        MapUtil.increment(neoTxByAddress, friend);
                        final long value = addressAssetMap.get(friend).get(ModelUtil.NEO_HASH);
                        if (value < 0) {
                            MapUtil.increment(neoInByAddress, friend, -value);
                        } else {
                            MapUtil.increment(neoOutByAddress, friend, value);
                        }
                    }
                    if (addressAssetMap.get(friend).containsKey(ModelUtil.GAS_HASH)) {
                        MapUtil.increment(gasTxByAddress, friend);
                        final long value = addressAssetMap.get(friend).get(ModelUtil.GAS_HASH);
                        if (value < 0) {
                            MapUtil.increment(gasInByAddress, friend, -value);
                        } else {
                            MapUtil.increment(gasOutByAddress, friend, value);
                        }
                    }
                }
            }
        }
        LOG.trace("getaccountlist 4 addressByAccount STARTED");
        final Map<UInt160, String> addressByScriptHash = new TreeMap<>();
        for (final UInt160 key : addressStateCache.keySet()) {
            final String address = ModelUtil.scriptHashToAddress(key);
            addressByScriptHash.put(key, address);
        }
        LOG.trace("getaccountlist 4 addressByAccount SUCCESS, address count:{};", addressByScriptHash.size());
        LOG.trace("getaccountlist 5 returnList STARTED");
        final JSONArray returnList = new JSONArray();
        for (final UInt160 key : addressStateCache.keySet()) {
            LOG.trace("getaccountlist 6 key:{};", key);
            if (addressByScriptHash.containsKey(key)) {
                final Map<UInt256, Fixed8> addressState = addressStateCache.get(key);
                final String address = addressByScriptHash.get(key);
                LOG.trace("getaccountlist 7 key:{}; address:{};", key, address);
                final JSONObject entry = new JSONObject();
                entry.put("account", address);
                if (addressState.containsKey(ModelUtil.NEO_HASH)) {
                    entry.put(ModelUtil.NEO, ModelUtil.toRoundedLong(addressState.get(ModelUtil.NEO_HASH).value));
                } else {
                    entry.put(ModelUtil.NEO, 0);
                }
                if (addressState.containsKey(ModelUtil.GAS_HASH)) {
                    entry.put(ModelUtil.GAS, ModelUtil.toRoundedDouble(addressState.get(ModelUtil.GAS_HASH).value));
                } else {
                    entry.put(ModelUtil.GAS, 0);
                }
                if (neoInByAddress.containsKey(key)) {
                    entry.put(NEO_IN, ModelUtil.toRoundedLong(neoInByAddress.get(key)));
                } else {
                    entry.put(NEO_IN, 0);
                }
                if (neoOutByAddress.containsKey(key)) {
                    entry.put(NEO_OUT, ModelUtil.toRoundedLong(neoOutByAddress.get(key)));
                } else {
                    entry.put(NEO_OUT, 0);
                }
                if (gasInByAddress.containsKey(key)) {
                    entry.put(GAS_IN, ModelUtil.toRoundedDouble(gasInByAddress.get(key)));
                } else {
                    entry.put(GAS_IN, 0);
                }
                if (gasOutByAddress.containsKey(key)) {
                    entry.put(GAS_OUT, ModelUtil.toRoundedDouble(gasOutByAddress.get(key)));
                } else {
                    entry.put(GAS_OUT, 0);
                }
                if (neoTxByAddress.containsKey(key)) {
                    entry.put(NEO_TX, neoTxByAddress.get(key));
                } else {
                    entry.put(NEO_TX, 0);
                }
                if (gasTxByAddress.containsKey(key)) {
                    entry.put(GAS_TX, gasTxByAddress.get(key));
                } else {
                    entry.put(GAS_TX, 0);
                }
                if (claimTxByAddress.containsKey(key)) {
                    entry.put(CLAIM_TX, claimTxByAddress.get(key));
                } else {
                    entry.put(CLAIM_TX, 0);
                }
                if (firstTsByAddress.containsKey(key)) {
                    entry.put(FIRST_TS, firstTsByAddress.get(key));
                } else {
                    entry.put(FIRST_TS, 0);
                }
                if (lastTsByAddress.containsKey(key)) {
                    entry.put(LAST_TS, lastTsByAddress.get(key));
                } else {
                    entry.put(LAST_TS, 0);
                }
                returnList.put(entry);
            }
        }
        LOG.trace("getaccountlist 5 returnList SUCCESS, returnList.size:{};", returnList.length());
        LOG.trace("getaccountlist 6 return");
        final JSONObject response = new JSONObject();
        response.put(ID, id);
        response.put(JSONRPC, VERSION_2_0);
        response.put(RESULT, returnList);
        return response;
    } catch (final RuntimeException e) {
        LOG.error("error in onGetAccountList:", e);
        final JSONObject response = new JSONObject();
        response.put(ERROR, e.getMessage());
        response.put(EXPECTED, new JSONArray());
        response.put(ACTUAL, new JSONArray());
        return response;
    }
}
Also used : JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) Map(java.util.Map) TreeMap(java.util.TreeMap) UInt256(neo.model.bytes.UInt256)

Example 47 with Transaction

use of neo.model.core.Transaction in project neo-java by coranos.

the class RemoteNodeControllerRunnable method recieveMessages.

/**
 * recieve messages.
 *
 * @param readTimeOut
 *            the read timeout.
 * @param magic
 *            the magic number to check for valid messages.
 * @param in
 *            the input stream.
 * @throws IOException
 *             if an error occurs.
 */
private void recieveMessages(final long readTimeOut, final long magic, final InputStream in) throws IOException {
    Message messageRecieved = getMessageOrTimeOut(readTimeOut, in);
    while (messageRecieved != null) {
        if (messageRecieved.magic != magic) {
            LOG.debug(" magic was {} expected {} closing peer.", messageRecieved.magic, magic);
            data.setGoodPeer(false);
        } else {
            MapUtil.increment(LocalNodeData.API_CALL_MAP, RemoteNodeData.IN_BYTES, messageRecieved.getPayloadByteArray().length + 24);
            if (messageRecieved.commandEnum != null) {
                final long apiCallCount;
                final String apiCallRoot = "in-" + messageRecieved.commandEnum.name().toLowerCase();
                if (messageRecieved.commandEnum.equals(CommandEnum.TX)) {
                    final Transaction tx = messageRecieved.getPayload(Transaction.class);
                    final String apiCall = apiCallRoot + DASH + tx.type.name().toLowerCase();
                    apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCall);
                } else if (messageRecieved.commandEnum.equals(CommandEnum.INV)) {
                    final InvPayload payload = messageRecieved.getPayload(InvPayload.class);
                    final String apiCall = apiCallRoot + DASH + payload.getType().name().toLowerCase();
                    final String apiCallHash = apiCallRoot + DASH + payload.getType().name().toLowerCase() + "-hashes";
                    apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCall);
                    MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCallHash, payload.getHashes().size());
                } else {
                    apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCallRoot);
                }
                LOG.debug("response from {}:{} {}", data.getHostAddress(), messageRecieved.command, apiCallCount);
            }
            localControllerNode.onMessage(RemoteNodeControllerRunnable.this, messageRecieved);
        }
        if (!data.isGoodPeer()) {
            return;
        }
        messageRecieved = getMessageOrTimeOut(readTimeOut, in);
    }
}
Also used : Message(neo.model.network.Message) Transaction(neo.model.core.Transaction) InvPayload(neo.model.network.InvPayload)

Example 48 with Transaction

use of neo.model.core.Transaction in project neo-java by coranos.

the class TestBlockSerialization method assertTransactionTypeEquals.

/**
 * reads in the test json for the given test name, gets the tx at the given
 * index, and verifys that it's transaction type is the expected transaction
 * type.
 *
 * @param testFunctionName
 *            the test function name to use.
 * @param txIx
 *            the transaction index to use.
 * @param expectedTransactionType
 *            the expected transaction type to use.
 */
private void assertTransactionTypeEquals(final String testFunctionName, final int txIx, final TransactionType expectedTransactionType) {
    try {
        final String blockJsonStr = TestUtil.getJsonTestResourceAsString("test", getClass().getSimpleName(), testFunctionName);
        final JSONObject blockJson = new JSONObject(blockJsonStr);
        final String blockStr = TestUtil.fromHexJsonObject(blockJson);
        final byte[] blockBa = Hex.decodeHex(blockStr.toCharArray());
        final Block block = new Block(ByteBuffer.wrap(blockBa));
        final Transaction tx = block.getTransactionList().get(txIx);
        final TransactionType actulaTransactionType = tx.type;
        Assert.assertEquals("transaction types must match", expectedTransactionType, actulaTransactionType);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TransactionType(neo.model.core.TransactionType) JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) ClientProtocolException(org.apache.http.client.ClientProtocolException) DecoderException(org.apache.commons.codec.DecoderException) IOException(java.io.IOException)

Example 49 with Transaction

use of neo.model.core.Transaction in project neo-java by coranos.

the class Message method createPayload.

/**
 * creates a payload.
 *
 * @param cl
 *            the payload class.
 * @param <T>
 *            the payload type.
 * @return the payload.
 */
@SuppressWarnings("unchecked")
private <T extends Payload> T createPayload(final Class<T> cl) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("initPayload payloadBa {}", Hex.encodeHexString(payloadBa));
    }
    final Payload payload;
    switch(command) {
        case "version":
            payload = new VersionPayload(ByteBuffer.wrap(payloadBa));
            break;
        case "inv":
            payload = new InvPayload(ByteBuffer.wrap(payloadBa));
            break;
        case "addr":
            payload = new AddrPayload(ByteBuffer.wrap(payloadBa));
            break;
        case "headers":
            payload = new HeadersPayload(ByteBuffer.wrap(payloadBa));
            break;
        case "verack":
            payload = null;
            break;
        case "getaddr":
            payload = null;
            break;
        case "getdata":
            payload = null;
            break;
        case "getblocks":
            payload = null;
            break;
        case "mempool":
            payload = null;
            break;
        case "":
            payload = null;
            break;
        case "getheaders":
            payload = null;
            break;
        case "consensus":
            payload = null;
            break;
        case "block":
            payload = new Block(ByteBuffer.wrap(payloadBa));
            break;
        case "tx":
            payload = new Transaction(ByteBuffer.wrap(payloadBa));
            break;
        default:
            if (!command.matches(LOWERCASE_ALPHABET)) {
                LOG.debug("unknown payload type for non alphabetic command \"{}\"", command);
                payload = null;
            } else {
                LOG.error("unknown payload type for command \"{}\"", command);
                payload = null;
            }
    }
    return (T) payload;
}
Also used : Transaction(neo.model.core.Transaction) Block(neo.model.core.Block)

Aggregations

Transaction (neo.model.core.Transaction)49 JSONObject (org.json.JSONObject)26 Block (neo.model.core.Block)25 JSONArray (org.json.JSONArray)19 TransactionOutput (neo.model.core.TransactionOutput)17 Test (org.junit.Test)14 UInt256 (neo.model.bytes.UInt256)13 TreeMap (java.util.TreeMap)12 BlockDb (neo.model.db.BlockDb)9 Map (java.util.Map)8 Fixed8 (neo.model.bytes.Fixed8)8 UInt160 (neo.model.bytes.UInt160)8 CoinReference (neo.model.core.CoinReference)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 LocalNodeData (neo.network.model.LocalNodeData)5 Timestamp (java.sql.Timestamp)4 UInt16 (neo.model.bytes.UInt16)4 List (java.util.List)3 TreeSet (java.util.TreeSet)3