Search in sources :

Example 1 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class ShardUtxoImport method checkTipTrust.

/**
 * Take an inbound tip and see if it is signing a block
 * with a key we trust.  If so, trust that block and maybe some parent blocks.
 *
 * Returns a list of hashes to request ImportedBlock for
 */
public List<ChainHash> checkTipTrust(MetricLog mlog, PeerChainTip tip) throws ValidationException {
    // If there is no block
    if (tip.getHeader().getSnowHash().size() == 0)
        return null;
    int shard_id = tip.getHeader().getShardId();
    // If this is a shard we actually track
    if (node.getInterestShards().contains(shard_id))
        return null;
    mlog.set("trusted_signers", trusted_signers.size());
    // We trust no one
    if (trusted_signers.size() == 0)
        return null;
    if (tip.getSignedHead() == null)
        return null;
    if (tip.getSignedHead().getPayload().size() == 0)
        return null;
    SignedMessagePayload payload = MsgSigUtil.validateSignedMessage(tip.getSignedHead(), node.getParams());
    mlog.set("valid_payload", 1);
    AddressSpecHash signer = AddressUtil.getHashForSpec(payload.getClaim());
    if (!trusted_signers.contains(signer))
        return null;
    mlog.set("trusted_sig", 1);
    // Now we have a valid signed head from a trusted signer
    logger.finer(String.format("Got signed tip from trusted peer (signer:%s)", AddressUtil.getAddressString("node", signer)));
    PeerTipInfo tip_info = payload.getPeerTipInfo();
    if (tip_info.getCoordHead().getSnowHash().size() > 0) {
        BlockPreview bp = tip_info.getCoordHead();
        node.getForgeInfo().saveExtCoordHead(bp);
    }
    LinkedList<ChainHash> request_list = new LinkedList<>();
    for (BlockPreview bp : tip_info.getPreviewsList()) {
        ChainHash hash = new ChainHash(bp.getSnowHash());
        node.getDB().setBlockTrust(hash);
        node.getDB().getChildBlockMapSet().add(bp.getPrevBlockHash(), bp.getSnowHash());
        ImportedBlock ib = getImportBlock(hash);
        if (ib == null) {
            // If we don't have the block, maybe request it
            if (reserveBlock(hash)) {
                request_list.add(hash);
            }
        }
    }
    return request_list;
}
Also used : ChainHash(snowblossom.lib.ChainHash) AddressSpecHash(snowblossom.lib.AddressSpecHash) LinkedList(java.util.LinkedList)

Example 2 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class ShardUtxoImport method addImportedBlock.

public void addImportedBlock(ImportedBlock ib) throws ValidationException {
    Validation.validateImportedBlock(node.getParams(), ib);
    logger.info(String.format("Added ImportedBlock (s:%d h:%d %s)", ib.getHeader().getShardId(), ib.getHeader().getBlockHeight(), new ChainHash(ib.getHeader().getSnowHash()).toString()));
    node.getDB().getChildBlockMapSet().add(ib.getHeader().getPrevBlockHash(), ib.getHeader().getSnowHash());
    node.getDB().getImportedBlockMap().put(ib.getHeader().getSnowHash(), ib);
    saveHighestForKnownShard(ib);
}
Also used : ChainHash(snowblossom.lib.ChainHash)

Example 3 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class ShardUtxoImport method getHighestKnownForShard.

public Set<ChainHash> getHighestKnownForShard(int shard) {
    ByteString key = ByteString.copyFrom(new String("ext-" + shard).getBytes());
    ExternalHeadList list = node.getDB().getExternalShardHeadMap().get(key);
    HashSet<ChainHash> out = new HashSet<>();
    if (list != null) {
        for (ByteString bs : list.getHeadHashesList()) {
            out.add(new ChainHash(bs));
        }
    }
    logger.fine(String.format("Highest known for %d - %s", shard, out.toString()));
    return out;
}
Also used : ChainHash(snowblossom.lib.ChainHash) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) HashSet(java.util.HashSet)

Example 4 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class GraphOutput method getGraph.

public static JSONObject getGraph(Collection<BlockHeader> blocks) {
    JSONArray node_array = new JSONArray();
    JSONArray link_array = new JSONArray();
    HashMap<ChainHash, BlockHeader> block_map = new HashMap<>();
    HashMap<ChainHash, String> name_map = new HashMap<>();
    HashMap<ChainHash, Integer> id_map = new HashMap<>();
    int id = 1;
    // Set nodes
    for (BlockHeader bh : blocks) {
        ChainHash hash = new ChainHash(bh.getSnowHash());
        block_map.put(hash, bh);
        String name = getName(bh);
        name_map.put(hash, name);
        id_map.put(hash, id);
        JSONObject node = new JSONObject();
        node.put("hash", hash.toString());
        node.put("name", name);
        node.put("shard", bh.getShardId());
        node.put("timestamp", bh.getTimestamp());
        node.put("height", bh.getBlockHeight());
        node_array.add(node);
        id++;
    }
    // Build links
    for (BlockHeader bh : blocks) {
        ChainHash hash = new ChainHash(bh.getSnowHash());
        ChainHash prev = new ChainHash(bh.getPrevBlockHash());
        int my_id = id_map.get(hash);
        if (id_map.containsKey(prev)) {
            JSONObject link = new JSONObject();
            link.put("source", hash.toString());
            link.put("target", prev.toString());
            link.put("parent", 1);
            link_array.add(link);
        }
        for (BlockImportList bil : bh.getShardImportMap().values()) {
            for (ByteString bs : bil.getHeightMap().values()) {
                ChainHash imp_hash = new ChainHash(bs);
                if (id_map.containsKey(imp_hash)) {
                    JSONObject link = new JSONObject();
                    link.put("source", hash.toString());
                    link.put("target", imp_hash.toString());
                    link.put("import", 1);
                    link_array.add(link);
                }
            }
        }
    }
    JSONObject graph = new JSONObject();
    graph.put("nodes", node_array);
    graph.put("links", link_array);
    return graph;
}
Also used : HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) BlockImportList(snowblossom.proto.BlockImportList) JSONArray(net.minidev.json.JSONArray) ByteString(com.google.protobuf.ByteString) ChainHash(snowblossom.lib.ChainHash) JSONObject(net.minidev.json.JSONObject) BlockHeader(snowblossom.proto.BlockHeader)

Example 5 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class MonitorInterfaceSystemOut method onInbound.

public void onInbound(Transaction tx, int tx_in_idx) {
    ChainHash hash = new ChainHash(tx.getTxHash());
    System.out.println("Inbound tx: " + hash + " input: " + tx_in_idx);
}
Also used : ChainHash(snowblossom.lib.ChainHash)

Aggregations

ChainHash (snowblossom.lib.ChainHash)24 ByteString (com.google.protobuf.ByteString)10 Test (org.junit.Test)5 AddressSpecHash (snowblossom.lib.AddressSpecHash)5 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Random (java.util.Random)2 Pair (duckutil.Pair)1 ByteBuffer (java.nio.ByteBuffer)1 KeyPair (java.security.KeyPair)1 MessageDigest (java.security.MessageDigest)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 JSONArray (net.minidev.json.JSONArray)1 JSONObject (net.minidev.json.JSONObject)1 TransactionBridge (snowblossom.lib.TransactionBridge)1 ValidationException (snowblossom.lib.ValidationException)1 BlockHeader (snowblossom.proto.BlockHeader)1