Search in sources :

Example 1 with BlockDb

use of neo.model.db.BlockDb in project neo-java by coranos.

the class TestVm method testTransaction.

private void testTransaction(final long blockHeight, final int txIx) {
    final LocalNodeData localNodeData = CONTROLLER.getLocalNodeData();
    final BlockDb blockDb = localNodeData.getBlockDb();
    final Block block = blockDb.getFullBlockFromHeight(blockHeight);
    final int maxTxIx = block.getTransactionList().size();
    final Transaction tx = block.getTransactionList().get(txIx);
    final long maxIndex = blockDb.getHeaderOfBlockWithMaxIndex().getIndexAsLong();
    LOG.info("STARTED block {} of {} tx {} of {} : {} {}", blockHeight, maxIndex, txIx, maxTxIx, tx.type, tx.getHash());
    final ScriptVerificationResultEnum verifyScriptsResult = VerifyScriptUtil.verifyScripts(blockDb, tx);
    if (!verifyScriptsResult.equals(ScriptVerificationResultEnum.PASS)) {
        LOG.error("FAILURE block {} of {} tx {} of {} : {} {} : {}", blockHeight, maxIndex, txIx, maxTxIx, tx.type, tx.getHash(), verifyScriptsResult);
        throw new RuntimeException("script failed : " + verifyScriptsResult);
    } else {
        LOG.info("SUCCESS block {} of {} tx {} of {} : {} {}", blockHeight, maxIndex, txIx, maxTxIx, tx.type, tx.getHash());
    }
}
Also used : LocalNodeData(neo.network.model.LocalNodeData) Transaction(neo.model.core.Transaction) ScriptVerificationResultEnum(neo.model.ScriptVerificationResultEnum) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb)

Example 2 with BlockDb

use of neo.model.db.BlockDb in project neo-java by coranos.

the class TestBlockSerialization method test00ZAllBlocks.

/**
 * pulls all the blocks (slow) to check for full coverage.
 *
 * @throws ClientProtocolException
 *             if an error occurs.
 * @throws IOException
 *             if an error occurs.
 * @throws DecoderException
 *             if an error occurs.
 * @throws InterruptedException
 *             if an error occurs.
 */
@Test
@Ignore
public void test00ZAllBlocks() throws ClientProtocolException, IOException, DecoderException, InterruptedException {
    final BlockDb blockDb = new AbstractJsonMockBlockDb() {

        @Override
        public JSONArray getMockBlockDb() {
            return new JSONArray();
        }
    };
    final long maxBlockIx = blockDb.getHeaderOfBlockWithMaxIndex().getIndexAsLong();
    final Set<TransactionType> knownTypeSet = new TreeSet<>();
    for (long blockIx = 0; blockIx <= maxBlockIx; blockIx++) {
        final Block block = blockDb.getFullBlockFromHeight(blockIx);
        for (int txIx = 0; txIx < block.getTransactionList().size(); txIx++) {
            final Transaction tx = block.getTransactionList().get(txIx);
            if (!knownTypeSet.contains(tx.type)) {
                LOG.error("getBlock {} {} tx {} {}", block.getIndexAsLong(), block.prevHash, txIx, tx.type);
                knownTypeSet.add(tx.type);
            }
        }
    }
    blockDb.close();
}
Also used : TransactionType(neo.model.core.TransactionType) Transaction(neo.model.core.Transaction) TreeSet(java.util.TreeSet) AbstractJsonMockBlockDb(neo.rpc.client.test.util.AbstractJsonMockBlockDb) JSONArray(org.json.JSONArray) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) AbstractJsonMockBlockDb(neo.rpc.client.test.util.AbstractJsonMockBlockDb) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with BlockDb

use of neo.model.db.BlockDb in project neo-java by coranos.

the class BlockImportExportUtil method importBlocks.

/**
 * imports the blocks from the file.
 *
 * @param controller
 *            the controller.
 */
public static void importBlocks(final LocalControllerNode controller) {
    final LocalNodeData localNodeData = controller.getLocalNodeData();
    final BlockDb blockDb = localNodeData.getBlockDb();
    try (OutputStream statsFileOut = new FileOutputStream(localNodeData.getChainExportStatsFileName());
        PrintWriter statsWriter = new PrintWriter(statsFileOut, true)) {
        statsWriter.println(OPEN_BRACKET);
        long maxIndex = 0;
        try (InputStream fileIn = new FileInputStream(localNodeData.getChainExportDataFileName());
            BufferedInputStream buffIn = new BufferedInputStream(fileIn, 1024 * 1024 * 32);
            DataInputStream in = new DataInputStream(buffIn)) {
            final byte[] maxIndexBa = new byte[UInt32.SIZE];
            in.read(maxIndexBa);
            if (LOG.isTraceEnabled()) {
                LOG.info("import maxIndexBa asread {}", Hex.encode(maxIndexBa));
            }
            ArrayUtils.reverse(maxIndexBa);
            maxIndex = new UInt32(maxIndexBa).asLong();
            LOG.info("started import {}", INTEGER_FORMAT.format(maxIndex));
            long startMs = -1;
            long interimBlocks = 0;
            long interimBytes = 0;
            final long[] interimTx = new long[TransactionType.values().length];
            final long[] interimTxNetworkFees = new long[TransactionType.values().length];
            long totalTx = 0;
            final Map<String, Long> numBlocksByTxCountMap = new TreeMap<>();
            @SuppressWarnings("unchecked") final Set<UInt160>[] activeAccountSet = new Set[TransactionType.values().length];
            for (int txOrdinal = 0; txOrdinal < activeAccountSet.length; txOrdinal++) {
                activeAccountSet[txOrdinal] = new TreeSet<>();
            }
            long procStartMs = System.currentTimeMillis();
            for (long blockIx = 0; blockIx < maxIndex; blockIx++) {
                final int length = Integer.reverseBytes(in.readInt());
                LOG.debug("STARTED import {} of {} length {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), INTEGER_FORMAT.format(length));
                final byte[] ba = new byte[length];
                in.read(ba);
                final Block block = new Block(ByteBuffer.wrap(ba));
                final boolean forceSynch = (blockIx % BlockDb.BLOCK_FORCE_SYNCH_INTERVAL) == 0;
                blockDb.put(forceSynch, block);
                interimBlocks++;
                interimBytes += ba.length;
                for (final Transaction tx : block.getTransactionList()) {
                    interimTx[tx.type.ordinal()]++;
                    final Fixed8 systemFee = localNodeData.getTransactionSystemFeeMap().get(tx.type);
                    interimTxNetworkFees[tx.type.ordinal()] += getNetworkFee(blockDb, tx, systemFee).value;
                    totalTx++;
                    for (final TransactionOutput txOut : tx.outputs) {
                        activeAccountSet[tx.type.ordinal()].add(txOut.scriptHash);
                    }
                }
                LOG.debug("SUCCESS import {} of {} hash {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), block.hash);
                MapUtil.increment(numBlocksByTxCountMap, String.valueOf(block.getTransactionList().size()));
                final Timestamp blockTs = block.getTimestamp();
                if (startMs < 0) {
                    startMs = blockTs.getTime();
                }
                final long ms = blockTs.getTime() - startMs;
                if (ms > (86400 * 1000)) {
                    blockDb.put(true);
                    final Block maxBlockHeader = blockDb.getHeaderOfBlockWithMaxIndex();
                    final JSONObject stats = getStats(blockDb, interimBlocks, interimBytes, interimTx, activeAccountSet, procStartMs, blockTs, interimTxNetworkFees, numBlocksByTxCountMap);
                    if (blockIx > 0) {
                        statsWriter.println(COMMA);
                    }
                    statsWriter.println(stats);
                    final long maxBlockHeaderIndex = maxBlockHeader.getIndexAsLong();
                    LOG.info("INTERIM import {} of {}, bx {}, tx {} json {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), INTEGER_FORMAT.format(maxBlockHeaderIndex), INTEGER_FORMAT.format(totalTx), stats);
                    startMs = blockTs.getTime();
                    for (int ix = 0; ix < interimTx.length; ix++) {
                        interimTx[ix] = 0;
                        interimTxNetworkFees[ix] = 0;
                    }
                    interimBlocks = 0;
                    interimBytes = 0;
                    for (int txOrdinal = 0; txOrdinal < activeAccountSet.length; txOrdinal++) {
                        activeAccountSet[txOrdinal].clear();
                    }
                    numBlocksByTxCountMap.clear();
                    procStartMs = System.currentTimeMillis();
                }
            }
            blockDb.put(true);
            LOG.info("SUCCESS import {}, synched", INTEGER_FORMAT.format(maxIndex));
        } catch (final IOException e) {
            if (e instanceof EOFException) {
                blockDb.put(true);
                final Block maxBlockHeader = blockDb.getHeaderOfBlockWithMaxIndex();
                LOG.error("FAILURE import {} of {}, synched, EOFException", INTEGER_FORMAT.format(maxBlockHeader.getIndexAsLong()), maxIndex);
                LOG.error("EOFException", e);
                return;
            } else {
                throw new RuntimeException(e);
            }
        } finally {
            statsWriter.println(CLOSE_BRACKET);
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) TransactionOutput(neo.model.core.TransactionOutput) BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Timestamp(java.sql.Timestamp) BufferedInputStream(java.io.BufferedInputStream) Fixed8(neo.model.bytes.Fixed8) EOFException(java.io.EOFException) PrintWriter(java.io.PrintWriter) LocalNodeData(neo.network.model.LocalNodeData) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) FileOutputStream(java.io.FileOutputStream) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) UInt32(neo.model.bytes.UInt32)

Example 4 with BlockDb

use of neo.model.db.BlockDb in project neo-java by coranos.

the class RpcServerUtil method onGetBlock.

/**
 * responds to a "getblock" command.
 *
 * @param controller
 *            the controller to use.
 * @param id
 *            the request id to use.
 * @param params
 *            the parameters to use.
 * @return the response.
 */
private static JSONObject onGetBlock(final LocalControllerNode controller, final int id, final JSONArray params) {
    if (params.length() == 0) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, "no parameters, expected a hash or an index");
        response.put(EXPECTED, 0);
        response.put(ACTUAL, NULL);
        return response;
    } else {
        final boolean verbose;
        if (params.length() >= 2) {
            if (params.get(1) instanceof Number) {
                final long index = params.getLong(1);
                verbose = index == 1;
            } else {
                verbose = false;
            }
        } else {
            verbose = false;
        }
        final Block block;
        final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
        if (params.get(0) instanceof String) {
            final String hashStr = params.getString(0);
            final byte[] ba = ModelUtil.decodeHex(hashStr);
            final UInt256 hash = new UInt256(ByteBuffer.wrap(ba));
            try {
                block = blockDb.getFullBlockFromHash(hash);
            } catch (final RuntimeException e) {
                final JSONObject response = new JSONObject();
                response.put(ERROR, e.getMessage());
                response.put(EXPECTED, EXPECTED_GENERIC_HEX);
                response.put(ACTUAL, params.get(0));
                return response;
            }
        } else if (params.get(0) instanceof Number) {
            final long index = params.getLong(0);
            try {
                block = blockDb.getFullBlockFromHeight(index);
            } catch (final RuntimeException e) {
                final JSONObject response = new JSONObject();
                response.put(ERROR, e.getMessage());
                response.put(EXPECTED, 0);
                response.put(ACTUAL, params.get(0));
                return response;
            }
        } else {
            final JSONObject response = new JSONObject();
            response.put(ERROR, "bad parameters, expected a hash or an index");
            response.put(EXPECTED, 0);
            response.put(ACTUAL, params.get(0));
            return response;
        }
        final JSONObject response = new JSONObject();
        response.put(ID, id);
        response.put(JSONRPC, VERSION_2_0);
        if (verbose) {
            response.put(RESULT, block.toJSONObject());
        } else {
            response.put(RESULT, Hex.encodeHexString(block.toByteArray()));
        }
        return response;
    }
}
Also used : JSONObject(org.json.JSONObject) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) UInt256(neo.model.bytes.UInt256)

Example 5 with BlockDb

use of neo.model.db.BlockDb in project neo-java by coranos.

the class RpcServerUtil method onGetCityOfZionHistory.

/**
 * return the transaction history of the address.
 *
 * @param controller
 *            the controller to use.
 * @param address
 *            the address to use.
 * @return the balance of the address.
 */
private static JSONObject onGetCityOfZionHistory(final LocalControllerNode controller, final String address) {
    final UInt160 scriptHash = ModelUtil.addressToScriptHash(address);
    if (LOG.isTraceEnabled()) {
        LOG.trace("onGetCityOfZionHistory.scriptHash:{}", scriptHash);
    }
    try {
        final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
        final List<Transaction> transactionList = blockDb.getTransactionWithAccountList(scriptHash);
        final JSONArray historyJa = new JSONArray();
        if (transactionList != null) {
            for (final Transaction transaction : transactionList) {
                Fixed8 neo = ModelUtil.FIXED8_ZERO;
                Fixed8 gas = ModelUtil.FIXED8_ZERO;
                for (final TransactionOutput to : transaction.outputs) {
                    if (to.scriptHash.equals(scriptHash)) {
                        if (to.assetId.equals(ModelUtil.NEO_HASH)) {
                            neo = ModelUtil.add(neo, to.value);
                        }
                        if (to.assetId.equals(ModelUtil.GAS_HASH)) {
                            gas = ModelUtil.add(gas, to.value);
                        }
                    }
                }
                final JSONObject transactionResponse = new JSONObject();
                transactionResponse.put(GAS, ModelUtil.toRoundedDouble(gas.value));
                transactionResponse.put(NEO, ModelUtil.toRoundedLong(neo.value));
                final Long blockIndex = blockDb.getBlockIndexFromTransactionHash(transaction.getHash());
                transactionResponse.put("block_index", blockIndex);
                transactionResponse.put(TXID, transaction.getHash().toString());
                historyJa.put(transactionResponse);
            }
        }
        final JSONObject response = new JSONObject();
        response.put(ADDRESS, address);
        response.put(HISTORY, historyJa);
        response.put(NET, controller.getLocalNodeData().getNetworkName());
        return response;
    } catch (final RuntimeException e) {
        LOG.error("onGetCityOfZionHistory", e);
        final JSONObject response = new JSONObject();
        if (e.getMessage() == null) {
            response.put(ERROR, e.getClass().getName());
        } else {
            response.put(ERROR, e.getMessage());
        }
        response.put(EXPECTED, EXPECTED_GENERIC_HEX);
        response.put(ACTUAL, address);
        return response;
    }
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) JSONArray(org.json.JSONArray) BlockDb(neo.model.db.BlockDb)

Aggregations

BlockDb (neo.model.db.BlockDb)13 Block (neo.model.core.Block)9 Transaction (neo.model.core.Transaction)9 JSONObject (org.json.JSONObject)8 TreeMap (java.util.TreeMap)6 Fixed8 (neo.model.bytes.Fixed8)6 UInt160 (neo.model.bytes.UInt160)5 UInt256 (neo.model.bytes.UInt256)5 TransactionOutput (neo.model.core.TransactionOutput)5 LocalNodeData (neo.network.model.LocalNodeData)5 JSONArray (org.json.JSONArray)5 Test (org.junit.Test)5 Timestamp (java.sql.Timestamp)4 IOException (java.io.IOException)3 Map (java.util.Map)3 TreeSet (java.util.TreeSet)3 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2