Search in sources :

Example 41 with Block

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

the class TestRpcServer method test012CoreGetTransactionOutput.

/**
 * test reading core address with no subcategory.
 */
@Test
public void test012CoreGetTransactionOutput() {
    final JSONArray params = new JSONArray();
    final Block block = CONTROLLER.getLocalNodeData().getBlockDb().getFullBlockFromHeight(0);
    final Transaction transaction = block.getTransactionList().get(block.getTransactionList().size() - 1);
    final String txHash = transaction.getHash().toHexString();
    params.put(txHash);
    params.put(transaction.outputs.size() - 1);
    final String method = CoreRpcCommandEnum.GETTXOUT.getName();
    final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test012CoreGetTransactionOutput");
    final String actualStrRaw = TestRpcServerUtil.getResponse(CONTROLLER, "", RpcServerUtil.VERSION_2_0, params, method);
    final String expectedStr = new JSONObject(expectedStrRaw).toString(2);
    final String actualStr = new JSONObject(actualStrRaw).toString(2);
    Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
}
Also used : Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Block(neo.model.core.Block) Test(org.junit.Test)

Example 42 with Block

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

the class TestRpcServer method test022CityOfZionGetClaims.

/**
 * test CoZ gethistory.
 */
@Test
public void test022CityOfZionGetClaims() {
    final JSONArray params = new JSONArray();
    final Block block = CONTROLLER.getLocalNodeData().getBlockDb().getFullBlockFromHeight(0);
    final Transaction transaction = block.getTransactionList().get(block.getTransactionList().size() - 1);
    final TransactionOutput to = transaction.outputs.get(0);
    final String address = ModelUtil.scriptHashToAddress(to.scriptHash);
    final String uri = CityOfZionCommandEnum.CLAIMS.getUriPrefix() + address;
    final String method = CoreRpcCommandEnum.UNKNOWN.getName();
    final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test022CityOfZionGetClaims");
    CityOfZionCommandEnum.getCommandStartingWith(uri);
    final String actualStrRaw = TestRpcServerUtil.getResponse(CONTROLLER, uri, RpcServerUtil.VERSION_2_0, params, method);
    final String expectedStr = new JSONObject(expectedStrRaw).toString(2);
    final String actualStr = new JSONObject(actualStrRaw).toString(2);
    Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Block(neo.model.core.Block) Test(org.junit.Test)

Example 43 with Block

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

the class TestRpcServer method test020CityOfZionGetBalance.

/**
 * test CoZ getbalance.
 */
@Test
public void test020CityOfZionGetBalance() {
    final JSONArray params = new JSONArray();
    final Block block = CONTROLLER.getLocalNodeData().getBlockDb().getFullBlockFromHeight(0);
    final Transaction transaction = block.getTransactionList().get(block.getTransactionList().size() - 1);
    final TransactionOutput to = transaction.outputs.get(0);
    final String address = ModelUtil.scriptHashToAddress(to.scriptHash);
    final String uri = CityOfZionCommandEnum.BALANCE.getUriPrefix() + address;
    final String method = CoreRpcCommandEnum.UNKNOWN.getName();
    final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test020CityOfZionGetBalance");
    CityOfZionCommandEnum.getCommandStartingWith(uri);
    final String actualStrRaw = TestRpcServerUtil.getResponse(CONTROLLER, uri, RpcServerUtil.VERSION_2_0, params, method);
    final String expectedStr = new JSONObject(expectedStrRaw).toString(2);
    final String actualStr = new JSONObject(actualStrRaw).toString(2);
    Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Block(neo.model.core.Block) Test(org.junit.Test)

Example 44 with Block

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

the class TestAccountPng method test001GetAccountSankey.

@Test
public void test001GetAccountSankey() throws JSONException, IOException {
    LOG.debug("STARTED png");
    final LocalNodeData localNodeData = CONTROLLER.getLocalNodeData();
    final BlockDb blockDb = localNodeData.getBlockDb();
    final long maxIndex = blockDb.getHeaderOfBlockWithMaxIndex().getIndexAsLong();
    final CoinData neoData = new CoinData();
    final CoinData gasData = new CoinData();
    final Map<UInt256, CoinData> coinDataMap = new TreeMap<>();
    coinDataMap.put(ModelUtil.NEO_HASH, neoData);
    coinDataMap.put(ModelUtil.GAS_HASH, gasData);
    final File dir = new File("/Users/dps/git-coranos.github.io/neo/charts");
    final Map<UInt256, File> coinFileMap = new TreeMap<>();
    coinFileMap.put(ModelUtil.NEO_HASH, new File(dir, "neo-account.png"));
    coinFileMap.put(ModelUtil.GAS_HASH, new File(dir, "gas-account.png"));
    long startMs = -1;
    for (long blockIx = 0; blockIx <= maxIndex; blockIx++) {
        LOG.debug("STARTED png {} of {} ", blockIx, maxIndex);
        final Block block = blockDb.getFullBlockFromHeight(blockIx);
        for (final Transaction t : block.getTransactionList()) {
            // update assets based on tx inputs.
            for (final CoinReference cr : t.inputs) {
                final TransactionOutput ti = ModelUtil.getTransactionOutput(blockDb, cr);
                if (coinDataMap.containsKey(ti.assetId)) {
                    final CoinData coinData = coinDataMap.get(ti.assetId);
                    final UInt160 input = ti.scriptHash;
                    if (!coinData.accountValueMap.containsKey(input)) {
                        coinData.accountValueMap.put(input, ModelUtil.FIXED8_ZERO);
                        coinData.newAccountSet.add(input);
                    }
                    final Fixed8 oldValue = coinData.accountValueMap.get(input);
                    final Fixed8 newValue = ModelUtil.subtract(ti.value, oldValue);
                    if (newValue.equals(ModelUtil.FIXED8_ZERO)) {
                        coinData.accountValueMap.remove(input);
                    } else {
                        coinData.accountValueMap.put(input, newValue);
                    }
                }
            }
            // update assets based on tx outputs.
            for (int outputIx = 0; outputIx < t.outputs.size(); outputIx++) {
                final TransactionOutput to = t.outputs.get(outputIx);
                final UInt160 output = to.scriptHash;
                if (coinDataMap.containsKey(to.assetId)) {
                    final CoinData coinData = coinDataMap.get(to.assetId);
                    coinData.txAccountSet.add(output);
                    if (!coinData.accountValueMap.containsKey(output)) {
                        coinData.accountValueMap.put(output, ModelUtil.FIXED8_ZERO);
                        coinData.newAccountSet.add(output);
                    }
                    final Fixed8 oldValue = coinData.accountValueMap.get(output);
                    final Fixed8 newValue = ModelUtil.add(oldValue, to.value);
                    if (newValue.equals(ModelUtil.FIXED8_ZERO)) {
                        coinData.accountValueMap.remove(output);
                    } else {
                        coinData.accountValueMap.put(output, newValue);
                    }
                }
            }
            for (final UInt256 assetId : coinDataMap.keySet()) {
                final CoinData coinData = coinDataMap.get(assetId);
                for (final UInt160 txAccount : coinData.txAccountSet) {
                    MapUtil.increment(coinData.newAccountTxCountMap, txAccount);
                }
                coinData.txAccountSet.clear();
            }
        }
        final Timestamp blockTs = block.getTimestamp();
        if (startMs < 0) {
            startMs = blockTs.getTime();
        }
        final long ms = blockTs.getTime() - startMs;
        if (ms > (86400 * 1000)) {
            final String targetDateStr = DATE_FORMAT.format(blockTs);
            for (final UInt256 assetId : coinDataMap.keySet()) {
                final CoinData coinData = coinDataMap.get(assetId);
                final List<UInt160> accountList = new ArrayList<>();
                accountList.addAll(coinData.accountValueMap.keySet());
                Collections.sort(accountList, new Comparator<UInt160>() {

                    @Override
                    public int compare(final UInt160 account1, final UInt160 account2) {
                        final Long value1 = coinData.accountValueMap.get(account1).value;
                        final Long value2 = coinData.accountValueMap.get(account2).value;
                        return value1.compareTo(value2);
                    }
                });
                Collections.reverse(accountList);
                long maxTx = 0;
                long totalValue = 0;
                {
                    final List<DrawingData> drawingData = new ArrayList<>();
                    coinData.drawingDataList.add(drawingData);
                    int startDayPixel = 0;
                    int visibleAccountCount = 0;
                    for (final UInt160 account : accountList) {
                        totalValue += coinData.accountValueMap.get(account).value;
                    }
                    for (final UInt160 account : accountList) {
                        final int scaleLength = getScaleLength(coinData.accountValueMap, account, totalValue);
                        if (scaleLength != 0) {
                            visibleAccountCount++;
                        }
                        MapUtil.touch(coinData.newAccountTxCountMap, account);
                        maxTx = Math.max(maxTx, coinData.newAccountTxCountMap.get(account));
                    }
                    final float greenStep = 1.0f / visibleAccountCount;
                    final float redStep = 1.0f / maxTx;
                    float green = 1.0f;
                    for (final UInt160 account : accountList) {
                        final int scaleLength = getScaleLength(coinData.accountValueMap, account, totalValue);
                        if (scaleLength != 0) {
                            final float blue;
                            if (coinData.newAccountSet.contains(account)) {
                                blue = 1.0f;
                            } else {
                                blue = 0.0f;
                            }
                            float red = 0.0f;
                            if (coinData.newAccountTxCountMap.containsKey(account)) {
                                red = redStep * coinData.newAccountTxCountMap.get(account);
                            } else {
                                red = 0.0f;
                            }
                            drawingData.add(new DrawingData(startDayPixel, scaleLength, red, green, blue));
                        }
                        green -= greenStep;
                        startDayPixel += scaleLength;
                    }
                }
                final BufferedImage im = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
                final Graphics2D g = (Graphics2D) im.getGraphics();
                g.scale((WIDTH * 1.0) / coinData.drawingDataList.size(), 1.0 / HEIGHT_SUBPIXELS);
                for (int x = 0; x < coinData.drawingDataList.size(); x++) {
                    final List<DrawingData> drawingData = coinData.drawingDataList.get(x);
                    for (final DrawingData drawing : drawingData) {
                        g.setColor(new Color(drawing.r, drawing.g, drawing.b));
                        g.fillRect(x, drawing.y, 1, drawing.h);
                        g.setColor(Color.black);
                        g.drawLine(x, drawing.y, x + 1, drawing.y);
                    }
                }
                g.dispose();
                final File file = coinFileMap.get(assetId);
                ImageIO.write(im, "png", file);
                LOG.info("INTERIM file {}, {} of {}, date {}, accountList {}, tx {}, value {}", file.getName(), INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), targetDateStr, INTEGER_FORMAT.format(accountList.size()), INTEGER_FORMAT.format(maxTx), INTEGER_FORMAT.format(totalValue / ModelUtil.DECIMAL_DIVISOR));
                startMs = blockTs.getTime();
                coinData.newAccountSet.clear();
                coinData.newAccountTxCountMap.clear();
            }
        }
    }
    LOG.debug("SUCCESS png");
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) BufferedImage(java.awt.image.BufferedImage) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) ArrayList(java.util.ArrayList) List(java.util.List) CoinReference(neo.model.core.CoinReference) LocalNodeData(neo.network.model.LocalNodeData) Color(java.awt.Color) TreeMap(java.util.TreeMap) Graphics2D(java.awt.Graphics2D) Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) File(java.io.File) UInt256(neo.model.bytes.UInt256) Test(org.junit.Test)

Example 45 with Block

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

the class TestRpcServer method test001GetAccountList.

@Test
public void test001GetAccountList() {
    final JSONArray params = new JSONArray();
    params.put(1512622800);
    params.put(1517806800);
    final String method = CoreRpcCommandEnum.GETACCOUNTLIST.getName();
    final Block block1674628 = CONTROLLER.getLocalNodeData().getBlockDb().getFullBlockFromHeight(1674628);
    LOG.error("block1674628 hash:{}", block1674628.hash);
    for (int ix = 0; ix < block1674628.getTransactionList().size(); ix++) {
        LOG.error("block1674628.tx[{}] hash:{}", ix, block1674628.getTransactionList().get(ix).getHash());
    }
    final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test001GetAccountList");
    final JSONObject requestJson = TestRpcServerUtil.createInputJson(RpcServerUtil.VERSION_2_0, method, params);
    final String requestStr = requestJson.toString();
    final JSONObject actualJsonRaw = RpcServerUtil.process(CONTROLLER, "/", requestStr);
    final String actualStrRaw = actualJsonRaw.toString();
    final String expectedStr = new JSONObject(expectedStrRaw).toString(2);
    final String actualStr = new JSONObject(actualStrRaw).toString(2);
    Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Block(neo.model.core.Block) Test(org.junit.Test)

Aggregations

Block (neo.model.core.Block)65 Test (org.junit.Test)26 Transaction (neo.model.core.Transaction)25 JSONObject (org.json.JSONObject)24 JSONArray (org.json.JSONArray)16 TransactionOutput (neo.model.core.TransactionOutput)9 BlockDb (neo.model.db.BlockDb)9 IOException (java.io.IOException)8 TreeMap (java.util.TreeMap)8 UInt256 (neo.model.bytes.UInt256)8 LocalNodeData (neo.network.model.LocalNodeData)6 Fixed8 (neo.model.bytes.Fixed8)5 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 TreeSet (java.util.TreeSet)4 OutputStream (java.io.OutputStream)3 SQLException (java.sql.SQLException)3 UInt160 (neo.model.bytes.UInt160)3 UInt32 (neo.model.bytes.UInt32)3