Search in sources :

Example 6 with ChainHash

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

the class MonitorInterfaceSystemOut method onOutbound.

public void onOutbound(Transaction tx, int tx_out_idx) {
    ChainHash hash = new ChainHash(tx.getTxHash());
    System.out.println("Outbound tx: " + hash + " output: " + tx_out_idx);
}
Also used : ChainHash(snowblossom.lib.ChainHash)

Example 7 with ChainHash

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

the class Dancer method isCompliant.

/**
 * Returns true iff the header is compliant with the dance.
 * namely, if it is a coordinator, it includes only compliant other blocks.
 * if it is not a coordinator, it includes only blocks the coordinator has included.
 */
public boolean isCompliant(BlockHeader header, int depth) {
    if (depth == 0)
        return true;
    // this isn't a super strict check
    if (header == null)
        return true;
    ChainHash hash = new ChainHash(header.getSnowHash());
    synchronized (comp_cache) {
        if (comp_cache.containsKey(hash)) {
            return comp_cache.get(hash);
        }
    }
    String key = hash.toString() + "/" + depth;
    synchronized (comp_cache_depth) {
        if (comp_cache_depth.containsKey(key)) {
            return comp_cache_depth.get(key);
        }
    }
    boolean comp = isCompliantInternal(header, depth);
    synchronized (comp_cache_depth) {
        comp_cache_depth.put(key, comp);
    }
    return comp;
}
Also used : ChainHash(snowblossom.lib.ChainHash) ByteString(com.google.protobuf.ByteString)

Example 8 with ChainHash

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

the class Dancer method isCompliantInternal.

public boolean isCompliantInternal(BlockHeader header, int depth) {
    int import_depth = node.getParams().getMaxShardSkewHeight() * 2 + 1;
    int shard_id = header.getShardId();
    if (isCoordinator(shard_id)) {
        // For each imported block, make sure it is compliant
        for (Map.Entry<Integer, BlockImportList> me : header.getShardImportMap().entrySet()) {
            int imp_shard_id = me.getKey();
            BlockImportList bil = me.getValue();
            for (Map.Entry<Integer, ByteString> me_bil : bil.getHeightMap().entrySet()) {
                int imp_height = me_bil.getKey();
                ChainHash imp_hash = new ChainHash(me_bil.getValue());
                BlockHeader imp_head = node.getForgeInfo().getHeader(imp_hash);
                if (!isCompliant(imp_head, depth - 1))
                    return false;
            }
        }
    } else {
        Map<Integer, BlockHeader> my_heads = node.getForgeInfo().getImportedShardHeads(header, import_depth);
        BlockHeader coord = node.getForgeInfo().getHighestCoordinator(my_heads.values());
        if (!isCompliant(coord, depth - 1))
            return false;
        if (coord == null) {
            logger.warning("Unable to find a coordinator for block: " + node.getForgeInfo().getHeaderString(header));
            return true;
        }
        Map<Integer, BlockHeader> coord_heads = node.getForgeInfo().getImportedShardHeads(coord, import_depth);
        // For each imported block in this current block, make sure that is in the coord_heads
        for (Map.Entry<Integer, BlockImportList> me : header.getShardImportMap().entrySet()) {
            int imp_shard_id = me.getKey();
            BlockImportList bil = me.getValue();
            for (Map.Entry<Integer, ByteString> me_bil : bil.getHeightMap().entrySet()) {
                int imp_height = me_bil.getKey();
                ChainHash imp_hash = new ChainHash(me_bil.getValue());
                BlockHeader imp_head = node.getForgeInfo().getHeader(imp_hash);
                if (!isCompliant(imp_head, depth - 1))
                    return false;
                BlockHeader coord_head = coord_heads.get(imp_shard_id);
                // Coordinator doesn't have shard
                if (coord_head == null)
                    return false;
                // or a parent of the shard in the coord_head
                if (!node.getForgeInfo().isInChain(coord_head, imp_head)) {
                    return false;
                }
            }
        }
    }
    // WOOO
    return true;
}
Also used : ChainHash(snowblossom.lib.ChainHash) ByteString(com.google.protobuf.ByteString) Map(java.util.Map)

Example 9 with ChainHash

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

the class Dancer method isCompliant.

public boolean isCompliant(BlockHeader header) {
    if (comp_cache.size() > 20000) {
        logger.info("comp cache full: " + comp_cache.size());
    }
    // this isn't a super strict check
    if (header == null)
        return true;
    ChainHash hash = new ChainHash(header.getSnowHash());
    synchronized (comp_cache) {
        if (comp_cache.containsKey(hash)) {
            return comp_cache.get(hash);
        }
    }
    boolean comp = isCompliant(header, 3);
    synchronized (comp_cache) {
        comp_cache.put(hash, comp);
    }
    if (!comp) {
        logger.warning("Block out of compliance: " + node.getForgeInfo().getHeaderString(header));
    }
    return comp;
}
Also used : ChainHash(snowblossom.lib.ChainHash)

Example 10 with ChainHash

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

the class DigestUtilTest method testMerkleRoot3.

@Test
public void testMerkleRoot3() {
    ChainHash a = getRandomHash();
    ChainHash b = getRandomHash();
    ChainHash c = getRandomHash();
    ChainHash result = DigestUtil.getMerkleRootForTxList(ImmutableList.of(a, b, c));
    ChainHash ab = treeHash(a, b);
    ChainHash ab_c = treeHash(ab, c);
    Assert.assertEquals(ab_c, result);
}
Also used : ChainHash(snowblossom.lib.ChainHash) Test(org.junit.Test)

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