use of neo.model.core.TransactionOutput in project neo-java by coranos.
the class TestRpcServer method test021CityOfZionGetHistory.
/**
* test CoZ gethistory.
*/
@Test
public void test021CityOfZionGetHistory() {
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.HISTORY.getUriPrefix() + address;
final String method = CoreRpcCommandEnum.UNKNOWN.getName();
final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test021CityOfZionGetHistory");
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);
}
use of neo.model.core.TransactionOutput in project neo-java by coranos.
the class AbstractJsonMockBlockDb method getTransactionWithAccountList.
@Override
public List<Transaction> getTransactionWithAccountList(final UInt160 account) {
final List<Transaction> transactionList = new ArrayList<>();
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()) {
boolean transactionHasAccount = false;
for (final TransactionOutput output : transaction.outputs) {
if (output.scriptHash.equals(account)) {
transactionHasAccount = true;
}
}
if (transactionHasAccount) {
transactionList.add(transaction);
}
}
}
return transactionList;
}
use of neo.model.core.TransactionOutput 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);
}
}
use of neo.model.core.TransactionOutput 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;
}
}
use of neo.model.core.TransactionOutput in project neo-java by coranos.
the class RpcServerUtil method onGetCityOfZionClaims.
/**
* return the available claims of the address.
*
* @param controller
* the controller to use.
* @param address
* the address to use.
* @return the balance of the address.
*/
private static JSONObject onGetCityOfZionClaims(final LocalControllerNode controller, final String address) {
final UInt160 scriptHash = ModelUtil.addressToScriptHash(address);
if (LOG.isTraceEnabled()) {
LOG.trace("onGetCityOfZionClaims.scriptHash:{}", scriptHash);
}
try {
final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
final Map<UInt256, Map<TransactionOutput, CoinReference>> transactionOutputListMap = controller.getLocalNodeData().getBlockDb().getUnspentTransactionOutputListMap(scriptHash);
final JSONArray claimJa = new JSONArray();
if (transactionOutputListMap != null) {
final Map<TransactionOutput, CoinReference> neoTransactionOutputListMap = transactionOutputListMap.get(ModelUtil.NEO_HASH);
final Map<TransactionOutput, Long> blockIxByTxoMap = new TreeMap<>();
final List<Transaction> transactionList = blockDb.getTransactionWithAccountList(scriptHash);
for (final Transaction transaction : transactionList) {
final long blockIx = blockDb.getBlockIndexFromTransactionHash(transaction.getHash());
for (final TransactionOutput to : transaction.outputs) {
if (neoTransactionOutputListMap.containsKey(to)) {
blockIxByTxoMap.put(to, blockIx);
}
}
}
for (final TransactionOutput output : neoTransactionOutputListMap.keySet()) {
final CoinReference cr = neoTransactionOutputListMap.get(output);
final JSONObject unspent = toUnspentJSONObject(false, output, cr);
final JSONObject claim = new JSONObject();
final String txHashStr = unspent.getString(TXID);
claim.put(TXID, txHashStr);
claim.put(INDEX, unspent.getLong(INDEX));
claim.put(VALUE, unspent.getLong(VALUE));
final UInt256 txHash = ModelUtil.getUInt256(ByteBuffer.wrap(ModelUtil.decodeHex(txHashStr)), true);
final long start = blockDb.getBlockIndexFromTransactionHash(txHash);
claim.put(START, start);
final long end = blockIxByTxoMap.get(output);
claim.put(END, end);
claim.put(SYSFEE, computeSysFee(controller.getLocalNodeData().getTransactionSystemFeeMap(), blockDb, start, end));
claim.put("claim", calculateBonus(claim));
claimJa.put(claim);
}
}
final JSONObject response = new JSONObject();
response.put(ADDRESS, address);
response.put(CLAIMS, claimJa);
response.put(NET, controller.getLocalNodeData().getNetworkName());
return response;
} catch (final RuntimeException e) {
LOG.error("onGetCityOfZionClaims", 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;
}
}
Aggregations