Search in sources :

Example 6 with MetricLog

use of duckutil.MetricLog in project snowblossom by snowblossomcoin.

the class SnowUserService method getAddressHistory.

@Override
public void getAddressHistory(RequestAddress req, StreamObserver<HistoryList> observer) {
    try (MetricLog mlog = new MetricLog()) {
        mlog.setModule("SnowUserService");
        mlog.setOperation("GetAddressHistory");
        AddressSpecHash spec_hash = new AddressSpecHash(req.getAddressSpecHash());
        try {
            observer.onNext(AddressHistoryUtil.getHistory(spec_hash, node.getDB(), node.getBlockIngestor().getHead()));
            observer.onCompleted();
        } catch (ValidationException e) {
            observer.onNext(HistoryList.newBuilder().setNotEnabled(true).build());
            observer.onCompleted();
        } catch (Throwable e) {
            String addr = AddressUtil.getAddressString(node.getParams().getAddressPrefix(), spec_hash);
            logger.info("Exception " + addr + " " + e.toString());
            observer.onNext(HistoryList.newBuilder().build());
            observer.onError(e);
            observer.onCompleted();
            return;
        }
    }
}
Also used : MetricLog(duckutil.MetricLog) ByteString(com.google.protobuf.ByteString)

Example 7 with MetricLog

use of duckutil.MetricLog in project snowblossom by snowblossomcoin.

the class BlockIngestor method ingestBlock.

public boolean ingestBlock(Block blk) throws ValidationException {
    ChainHash blockhash;
    try (TimeRecordAuto tra_blk = TimeRecord.openAuto("BlockIngestor.ingestBlock");
        MetricLog mlog = new MetricLog()) {
        mlog.setOperation("ingest_block");
        mlog.setModule("block_ingestor");
        Validation.checkBlockBasics(node.getParams(), blk, true, false);
        if (blk.getHeader().getShardId() != shard_id) {
            throw new ValidationException("Block for incorrect shard");
        }
        blockhash = new ChainHash(blk.getHeader().getSnowHash());
        mlog.set("hash", blockhash.toString());
        mlog.set("height", blk.getHeader().getBlockHeight());
        mlog.set("shard", blk.getHeader().getShardId());
        mlog.set("size", blk.toByteString().size());
        mlog.set("tx_count", blk.getTransactionsCount());
        if (db.getBlockSummaryMap().containsKey(blockhash.getBytes())) {
            return false;
        }
        ChainHash prevblock = new ChainHash(blk.getHeader().getPrevBlockHash());
        BlockSummary prev_summary;
        if (prevblock.equals(ChainHash.ZERO_HASH)) {
            prev_summary = getStartSummary();
        } else {
            try (TimeRecordAuto tra_prv = TimeRecord.openAuto("BlockIngestor.getPrevSummary")) {
                prev_summary = db.getBlockSummaryMap().get(prevblock.getBytes());
            }
        }
        if (prev_summary == null) {
            return false;
        }
        long tx_body_size = 0;
        for (Transaction tx : blk.getTransactionsList()) {
            tx_body_size += tx.getInnerData().size();
            tx_body_size += tx.getTxHash().size();
        }
        BlockSummary summary = BlockchainUtil.getNewSummary(blk.getHeader(), prev_summary, node.getParams(), blk.getTransactionsCount(), tx_body_size, blk.getImportedBlocksList());
        Validation.deepBlockValidation(node.getParams(), node.getUtxoHashedTrie(), blk, prev_summary);
        summary = saveOtherChainIndexBits(summary, prev_summary, blk);
        if (tx_index) {
            try (TimeRecordAuto tra_tx = TimeRecord.openAuto("BlockIngestor.saveTx")) {
                ByteString block_hash_str = blockhash.getBytes();
                HashMap<ByteString, Transaction> tx_map = new HashMap<>();
                for (Transaction tx : blk.getTransactionsList()) {
                    tx_map.put(tx.getTxHash(), tx);
                }
                db.getTransactionMap().putAll(tx_map);
            }
        }
        try (TimeRecordAuto tra_tx = TimeRecord.openAuto("BlockIngestor.blockSave")) {
            db.getBlockMap().put(blockhash.getBytes(), blk);
            saveBlockChildMapping(blk.getHeader().getPrevBlockHash(), blockhash.getBytes());
            for (ImportedBlock ib : blk.getImportedBlocksList()) {
                // not positive we actually need this, but what the hell
                saveBlockChildMapping(ib.getHeader().getPrevBlockHash(), ib.getHeader().getSnowHash());
                node.getDB().getBlockHeaderMap().put(ib.getHeader().getSnowHash(), ib.getHeader());
            }
            db.setBestBlockAt(blk.getHeader().getShardId(), blk.getHeader().getBlockHeight(), BlockchainUtil.readInteger(summary.getWorkSum()));
            // THIS IS SUPER IMPORTANT!!!!
            // the summary being saved in the summary map acts as a signal that
            // - this block is fully stored
            // - we have the utxo saved
            // - we have the block itself saved
            // - if we are using tx_index, we have the transactions saved
            // - the previous block summary is also saved, which by induction means
            // that we have every block from this one all the way back to block 0
            // In short, after the summary is written, things can depend on this being
            // a valid and correct block that goes all the way back to block 0.
            // It might not be in the main chain, but it can be counted on to be valid chain
            db.getBlockSummaryMap().put(blockhash.getBytes(), summary);
            mlog.set("saved", 1);
        }
        if (ShardUtil.shardSplit(summary, params)) {
            for (int child : ShardUtil.getShardChildIds(summary.getHeader().getShardId())) {
                mlog.set("shard_split", 1);
                try {
                    node.openShard(child);
                } catch (Exception e) {
                    logger.warning("  Unable to open shard: " + e);
                }
            }
        }
        ChainHash prev_hash = new ChainHash(blk.getHeader().getPrevBlockHash());
        logger.info(String.format("New block: Shard %d Height %d %s (tx:%d sz:%d) - from %s", shard_id, blk.getHeader().getBlockHeight(), blockhash, blk.getTransactionsCount(), blk.toByteString().size(), prev_hash));
        node.getBlockForge().tickle(summary);
        SnowUserService u = node.getUserService();
        if (u != null) {
            u.tickleBlocks();
        }
        if (BlockchainUtil.isBetter(chainhead, summary)) {
            mlog.set("head_update", 1);
            chainhead = summary;
            db.getBlockSummaryMap().put(HEAD, summary);
            // System.out.println("UTXO at new root: " + HexUtil.getHexString(summary.getHeader().getUtxoRootHash()));
            // node.getUtxoHashedTrie().printTree(summary.getHeader().getUtxoRootHash());
            updateHeights(summary);
            logger.info(String.format("New chain tip: Shard %d Height %d %s (tx:%d sz:%d)", shard_id, blk.getHeader().getBlockHeight(), blockhash, blk.getTransactionsCount(), blk.toByteString().size()));
            String age = MiscUtils.getAgeSummary(System.currentTimeMillis() - blk.getHeader().getTimestamp());
            logger.info(String.format("  The activated field is %d (%s).  This block was %s ago.", chainhead.getActivatedField(), params.getSnowFieldInfo(chainhead.getActivatedField()).getName(), age));
            if (u != null) {
                u.tickleBlocks();
            }
            node.getMemPool(shard_id).tickleBlocks(new ChainHash(summary.getHeader().getUtxoRootHash()));
            node.getPeerage().sendAllTips(summary.getHeader().getShardId());
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) MetricLog(duckutil.MetricLog) ByteString(com.google.protobuf.ByteString) BlockSummary(snowblossom.proto.BlockSummary) Transaction(snowblossom.proto.Transaction) TimeRecordAuto(duckutil.TimeRecordAuto) ImportedBlock(snowblossom.proto.ImportedBlock)

Example 8 with MetricLog

use of duckutil.MetricLog in project snowblossom by snowblossomcoin.

the class ShardBlockForge method exploreCoordinator.

private Set<BlockConcept> exploreCoordinator(int coord_shard, MetricLog mlog_parent) throws ValidationException {
    try (MetricLog mlog = new MetricLog(mlog_parent, "exploreCoordinator")) {
        mlog.set("shard", coord_shard);
        TreeSet<BlockConcept> concepts = new TreeSet<>();
        // We are just assuming the current head is following the dance.
        // Things will get real weird real quick if not.
        Collection<BlockHeader> prev_heads = node.getForgeInfo().getShardHeads(coord_shard);
        mlog.set("head_count", prev_heads.size());
        logger.info("Head count: " + prev_heads.size());
        // while(concepts.size() == 0)
        {
            // HashSet<ChainHash> parent_hash_set = new HashSet<>();
            for (BlockHeader prev_header : prev_heads) {
                exploreCoordinatorSpecific(prev_header, coord_shard, concepts);
            // parent_hash_set.add(new ChainHash(prev_header.getPrevBlockHash()));
            }
        // In case we have no good concepts, build something from a parent
        // failure is not an option
        /*LinkedList<BlockHeader> parent_heads = new LinkedList<>();
        for(ChainHash hash : parent_hash_set)
        {
          BlockHeader bh = node.getForgeInfo().getHeader(hash);
          if (bh != null)
          {
            parent_heads.add(bh);
          }
        }
        prev_heads = parent_heads;*/
        }
        mlog.set("count", concepts.size());
        return concepts;
    }
}
Also used : TreeSet(java.util.TreeSet) MetricLog(duckutil.MetricLog)

Example 9 with MetricLog

use of duckutil.MetricLog in project snowblossom by snowblossomcoin.

the class SnowUserService method getMempoolTransactionMap.

@Override
public void getMempoolTransactionMap(RequestAddress req, StreamObserver<TransactionShardMap> observer) {
    try (MetricLog mlog = new MetricLog()) {
        mlog.setModule("SnowUserService");
        mlog.setOperation("GetMempoolTransactionMap");
        AddressSpecHash spec_hash = new AddressSpecHash(req.getAddressSpecHash());
        Map<Integer, Set<ChainHash>> m = node.getMemPool().getTransactionsForAddressByShard(spec_hash);
        TransactionShardMap.Builder shard_map = TransactionShardMap.newBuilder();
        for (Map.Entry<Integer, Set<ChainHash>> me : m.entrySet()) {
            TransactionHashList.Builder lst = TransactionHashList.newBuilder();
            for (ChainHash h : me.getValue()) {
                lst.addTxHashes(h.getBytes());
            }
            shard_map.putShardMap(me.getKey(), lst.build());
        }
        observer.onNext(shard_map.build());
        observer.onCompleted();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MetricLog(duckutil.MetricLog) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with MetricLog

use of duckutil.MetricLog in project snowblossom by snowblossomcoin.

the class SnowUserService method getMempoolTransactionList.

@Override
public void getMempoolTransactionList(RequestAddress req, StreamObserver<TransactionHashList> observer) {
    try (MetricLog mlog = new MetricLog()) {
        mlog.setModule("SnowUserService");
        mlog.setOperation("GetMempoolTransactionList");
        AddressSpecHash spec_hash = new AddressSpecHash(req.getAddressSpecHash());
        TransactionHashList.Builder list = TransactionHashList.newBuilder();
        for (ChainHash h : node.getMemPool().getTransactionsForAddress(spec_hash)) {
            list.addTxHashes(h.getBytes());
        }
        observer.onNext(list.build());
        observer.onCompleted();
    }
}
Also used : MetricLog(duckutil.MetricLog)

Aggregations

MetricLog (duckutil.MetricLog)12 ByteString (com.google.protobuf.ByteString)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 Transaction (snowblossom.proto.Transaction)2 TimeRecordAuto (duckutil.TimeRecordAuto)1 BigInteger (java.math.BigInteger)1 List (java.util.List)1 Random (java.util.Random)1 TreeMap (java.util.TreeMap)1 BlockSummary (snowblossom.proto.BlockSummary)1 ImportedBlock (snowblossom.proto.ImportedBlock)1 TransactionInner (snowblossom.proto.TransactionInner)1 TransactionInput (snowblossom.proto.TransactionInput)1 TransactionOutput (snowblossom.proto.TransactionOutput)1