Search in sources :

Example 26 with TimeRecordAuto

use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.

the class SnowUserService method submitTransaction.

@Override
public void submitTransaction(Transaction tx, StreamObserver<SubmitReply> responseObserver) {
    try (TimeRecordAuto tra = TimeRecord.openAuto("SnowUserService.submitTransaction")) {
        try {
            tx = Transaction.parseFrom(tx.toByteString());
            if (node.getMemPool().addTransaction(tx, false)) {
                node.getTxBroadcaster().send(tx);
            } else {
                logger.fine("Rejecting transaction: no mempool accepted");
                responseObserver.onNext(SubmitReply.newBuilder().setSuccess(false).setErrorMessage("no mempool accepted").build());
                responseObserver.onCompleted();
                return;
            }
        } catch (ValidationException e) {
            logger.info("Rejecting transaction: " + e);
            responseObserver.onNext(SubmitReply.newBuilder().setSuccess(false).setErrorMessage(e.toString()).build());
            responseObserver.onCompleted();
            return;
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
            logger.info("Rejecting transaction, strange error: " + e);
            responseObserver.onNext(SubmitReply.newBuilder().setSuccess(false).setErrorMessage(e.toString()).build());
            responseObserver.onCompleted();
            return;
        }
        responseObserver.onNext(SubmitReply.newBuilder().setSuccess(true).build());
        responseObserver.onCompleted();
    }
}
Also used : TimeRecordAuto(duckutil.TimeRecordAuto)

Example 27 with TimeRecordAuto

use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.

the class ForgeInfo method getShardHead.

public BlockHeader getShardHead(int shard_id) {
    try (TimeRecordAuto tra = TimeRecord.openAuto("ForgeInfo.getShardHead")) {
        if (node.getBlockIngestor(shard_id) != null) {
            BlockSummary bs = node.getBlockIngestor(shard_id).getHead();
            if (bs != null)
                return bs.getHeader();
            return null;
        }
        BlockPreview ext_coord_head_bp = null;
        synchronized (ext_coord_head) {
            if (ext_coord_head.containsKey(shard_id)) {
                ext_coord_head_bp = ext_coord_head.get(shard_id);
            }
        }
        if (ext_coord_head_bp != null) {
            BlockHeader bh = getHeader(new ChainHash(ext_coord_head_bp.getSnowHash()));
            if (bh != null) {
                return bh;
            } else {
                logger.fine(String.format("We heard ext_coord_head_hash of %s but don't have the header", new ChainHash(ext_coord_head_bp.getSnowHash()).toString()));
            }
        }
        Set<ChainHash> head_list = node.getShardUtxoImport().getHighestKnownForShard(shard_id);
        // send them all, not just one random
        ArrayList<ChainHash> lst = new ArrayList<>();
        lst.addAll(head_list);
        if (lst.size() == 0)
            return null;
        Collections.shuffle(lst);
        ImportedBlock ib = node.getShardUtxoImport().getImportBlock(lst.get(0));
        if (ib != null)
            return ib.getHeader();
        return null;
    }
}
Also used : TimeRecordAuto(duckutil.TimeRecordAuto) ArrayList(java.util.ArrayList)

Example 28 with TimeRecordAuto

use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.

the class ForgeInfo method getShardHeads.

public List<BlockHeader> getShardHeads(int shard_id) {
    try (TimeRecordAuto tra = TimeRecord.openAuto("ForgeInfo.getShardHeads")) {
        List<BlockHeader> out = new LinkedList<>();
        if (node.getInterestShards().contains(shard_id))
            if (node.getBlockIngestor(shard_id) != null) {
                BlockSummary bs = node.getBlockIngestor(shard_id).getHead();
                if (bs != null)
                    out.add(bs.getHeader());
                return out;
            }
        BlockPreview ext_coord_head_bp = null;
        synchronized (ext_coord_head) {
            if (ext_coord_head.containsKey(shard_id)) {
                ext_coord_head_bp = ext_coord_head.get(shard_id);
            }
        }
        if (ext_coord_head_bp != null) {
            BlockHeader bh = getHeader(new ChainHash(ext_coord_head_bp.getSnowHash()));
            if (bh != null) {
                return ImmutableList.of(bh);
            } else {
                logger.fine(String.format("We heard ext_coord_head_hash of %s but don't have the header", new ChainHash(ext_coord_head_bp.getSnowHash()).toString()));
            }
        }
        Set<ChainHash> head_list = node.getShardUtxoImport().getHighestKnownForShard(shard_id);
        logger.fine(String.format("Get shard heads %d - %s", shard_id, head_list.toString()));
        // send them all, not just one random
        for (ChainHash hash : head_list) {
            ImportedBlock ib = node.getShardUtxoImport().getImportBlock(hash);
            if (ib == null) {
                logger.info(String.format("Request for shard %d head: %s but we do not have an imported block for that.", shard_id, hash.toString()));
            } else {
                out.add(ib.getHeader());
            }
        }
        return out;
    }
}
Also used : TimeRecordAuto(duckutil.TimeRecordAuto) LinkedList(java.util.LinkedList)

Example 29 with TimeRecordAuto

use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.

the class ForgeInfo method getNetworkActiveShards.

/**
 * Return the set of shards that appear to be active on the network.
 * So this will be all shards that have blocks we know about minus those
 * who have both children shards having blocks of much higher height.
 */
public Map<Integer, BlockHeader> getNetworkActiveShards() {
    try (TimeRecordAuto tra = TimeRecord.openAuto("ForgeInfo.getNetworkActiveShards")) {
        TreeMap<Integer, BlockHeader> network_active = new TreeMap<>();
        int max_height = 0;
        for (int i = 0; i <= node.getParams().getMaxShardId(); i++) {
            // We might not have info on intermediate shards that we are not tracking
            {
                BlockHeader h = getShardHead(i);
                if (h != null) {
                    network_active.put(i, h);
                    max_height = Math.max(max_height, h.getBlockHeight());
                }
            }
        }
        // Remove shards where there are both children
        // that have enough height to not worry about
        TreeSet<Integer> to_remove = new TreeSet<>();
        for (int shard_id : network_active.keySet()) {
            int h = network_active.get(shard_id).getBlockHeight();
            int child_count = 0;
            for (int c : ShardUtil.getShardChildIds(shard_id)) {
                if (network_active.containsKey(c)) {
                    int hc = network_active.get(c).getBlockHeight();
                    if (hc > h + node.getParams().getMaxShardSkewHeight() * 2)
                        child_count++;
                }
            }
            if (child_count == 2)
                to_remove.add(shard_id);
            if (h + node.getParams().getMaxShardSkewHeight() + 2 < max_height)
                to_remove.add(shard_id);
        }
        for (int x : to_remove) {
            network_active.remove(x);
        }
        return network_active;
    }
}
Also used : TimeRecordAuto(duckutil.TimeRecordAuto) TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap)

Example 30 with TimeRecordAuto

use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.

the class ForgeInfo method getLatestShard.

/**
 * Return the highest block of shard that has been included in the given header
 * or null if no such information can be found.
 */
public BlockHeader getLatestShard(BlockHeader h, int shard_id) {
    try (TimeRecordAuto tra = TimeRecord.openAuto("ForgeInfo.getLatestShard")) {
        if (h == null)
            return null;
        Set<Integer> parents = ShardUtil.getAllParents(shard_id);
        parents.add(shard_id);
        if (parents.contains(h.getShardId()))
            return h;
        for (int s : parents) {
            if (h.getShardImportMap().containsKey(s)) {
                BlockImportList bil = h.getShardImportMap().get(s);
                TreeSet<Integer> heights = new TreeSet<>();
                heights.addAll(bil.getHeightMap().keySet());
                ChainHash hash = new ChainHash(bil.getHeightMap().get(heights.last()));
                return getHeader(hash);
            }
        }
        return getLatestShard(getHeader(new ChainHash(h.getPrevBlockHash())), shard_id);
    }
}
Also used : TimeRecordAuto(duckutil.TimeRecordAuto) TreeSet(java.util.TreeSet)

Aggregations

TimeRecordAuto (duckutil.TimeRecordAuto)31 ByteString (com.google.protobuf.ByteString)11 LinkedList (java.util.LinkedList)10 BigInteger (java.math.BigInteger)7 TreeMap (java.util.TreeMap)7 MessageDigest (java.security.MessageDigest)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 CodedInputStream (com.google.protobuf.CodedInputStream)1 MetricLog (duckutil.MetricLog)1 DecimalFormat (java.text.DecimalFormat)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 SplittableRandom (java.util.SplittableRandom)1 ValidationException (snowblossom.lib.ValidationException)1 HashedTrie (snowblossom.lib.trie.HashedTrie)1 TrieDBMem (snowblossom.lib.trie.TrieDBMem)1