Search in sources :

Example 36 with Block

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

the class AbstractJsonMockBlockDb method getAccountAssetValueMap.

@Override
public final Map<UInt160, Map<UInt256, Fixed8>> getAccountAssetValueMap() {
    final Map<UInt160, Map<UInt256, Fixed8>> accountAssetValueMap = new TreeMap<>();
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        final Block block = getBlock(mockBlock, true);
        for (final Transaction transaction : block.getTransactionList()) {
            for (final TransactionOutput output : transaction.outputs) {
                if (!accountAssetValueMap.containsKey(output.scriptHash)) {
                    accountAssetValueMap.put(output.scriptHash, new TreeMap<>());
                }
                final Map<UInt256, Fixed8> assetValueMap = accountAssetValueMap.get(output.scriptHash);
                final Fixed8 value = output.value;
                if (assetValueMap.containsKey(output.assetId)) {
                    final Fixed8 oldValue = assetValueMap.get(output.assetId);
                    final Fixed8 newValue = ModelUtil.add(value, oldValue);
                    assetValueMap.put(output.assetId, newValue);
                } else {
                    assetValueMap.put(output.assetId, value);
                }
            }
        }
    }
    return accountAssetValueMap;
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) Block(neo.model.core.Block) TreeMap(java.util.TreeMap) Map(java.util.Map) UInt256(neo.model.bytes.UInt256)

Example 37 with Block

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

the class AbstractJsonMockBlockDb method getBlockIndexFromTransactionHash.

@Override
public final Long getBlockIndexFromTransactionHash(final UInt256 hash) {
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        final Block block = getBlock(mockBlock, true);
        for (final Transaction transaction : block.getTransactionList()) {
            if (transaction.getHash().equals(hash)) {
                return block.getIndexAsLong();
            }
        }
    }
    throw new RuntimeException("no transaction with hash:" + hash);
}
Also used : JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) JSONArray(org.json.JSONArray) Block(neo.model.core.Block)

Example 38 with Block

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

the class AbstractJsonMockBlockDb method getBlockWithMaxIndex.

/**
 * return the block with the maximum value in the index column.
 *
 * @param withTransactions
 *            if true, add transactions. If false, only return the block header.
 * @return the block with the maximum value in the index column.
 */
private Block getBlockWithMaxIndex(final boolean withTransactions) {
    Block maxBlock = null;
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        if (maxBlock == null) {
            maxBlock = getBlock(mockBlock, withTransactions);
        } else {
            if (mockBlock.getLong(INDEX) > maxBlock.getIndexAsLong()) {
                maxBlock = getBlock(mockBlock, withTransactions);
            }
        }
    }
    return maxBlock;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Block(neo.model.core.Block)

Example 39 with Block

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

the class AbstractJsonMockBlockDb method getUnspentTransactionOutputListMap.

@Override
public Map<UInt256, Map<TransactionOutput, CoinReference>> getUnspentTransactionOutputListMap(final UInt160 account) {
    final Map<UInt256, Map<TransactionOutput, CoinReference>> assetIdTxoMap = new TreeMap<>();
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        final Block block = getBlock(mockBlock, true);
        for (int txIx = 0; txIx < block.getTransactionList().size(); txIx++) {
            final Transaction transaction = block.getTransactionList().get(txIx);
            for (final TransactionOutput output : transaction.outputs) {
                if (output.scriptHash.equals(account)) {
                    if (!assetIdTxoMap.containsKey(output.assetId)) {
                        assetIdTxoMap.put(output.assetId, new TreeMap<>());
                    }
                    final CoinReference cr = new CoinReference(transaction.getHash(), new UInt16(txIx));
                    assetIdTxoMap.get(output.assetId).put(output, cr);
                }
            }
        }
    }
    return assetIdTxoMap;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) UInt16(neo.model.bytes.UInt16) TreeMap(java.util.TreeMap) Map(java.util.Map) UInt256(neo.model.bytes.UInt256)

Example 40 with Block

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

the class MockUtil method getMockBlock003.

public static Block getMockBlock003() {
    final Block block = getMockBlock002();
    final Transaction transaction = getMockTransaction000();
    transaction.inputs.add(getCoinReference001());
    transaction.outputs.add(getTransactionOutput001());
    transaction.scripts.add(getWitness000());
    block.getTransactionList().add(transaction);
    return block;
}
Also used : Transaction(neo.model.core.Transaction) Block(neo.model.core.Block)

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