Search in sources :

Example 1 with BlockImportList

use of snowblossom.proto.BlockImportList 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 2 with BlockImportList

use of snowblossom.proto.BlockImportList in project snowblossom by snowblossomcoin.

the class ShardUtil method getBlockReward.

/**
 * Get Block Reward for this shard block not including fees
 */
public static long getBlockReward(NetworkParams params, BlockHeader header) {
    BigInteger reward_sum = BigInteger.ZERO;
    // We need to go higher percision math but still with integers
    // So we use BigInteger and shift everything way to the left to give
    // us some wiggle room.
    BigInteger general_block_reward = BigInteger.valueOf(PowUtil.getBlockReward(params, header.getBlockHeight())).shiftLeft(REWARD_MATH_BASE_SHIFT);
    BigInteger block_reward_direct_faction = general_block_reward.shiftRight(2).multiply(BigInteger.valueOf(3L));
    BigInteger block_reward_indirect_faction = general_block_reward.shiftRight(2);
    int shard_gen = getShardGeneration(header.getShardId());
    // Direct reward for this block
    reward_sum = reward_sum.add(block_reward_direct_faction.shiftRight(shard_gen));
    // Indirect reward for self
    reward_sum = reward_sum.add(block_reward_indirect_faction.shiftRight(shard_gen * 2));
    for (Map.Entry<Integer, BlockImportList> me : header.getShardImportMap().entrySet()) {
        reward_sum = reward_sum.add(getImportReward(params, header.getShardId(), me.getKey(), me.getValue()));
    }
    // Remove the shift and return
    return reward_sum.shiftRight(REWARD_MATH_BASE_SHIFT).longValue();
}
Also used : BigInteger(java.math.BigInteger) BlockImportList(snowblossom.proto.BlockImportList) BigInteger(java.math.BigInteger) Map(java.util.Map)

Example 3 with BlockImportList

use of snowblossom.proto.BlockImportList in project snowblossom by snowblossomcoin.

the class PowUtil method hashHeaderBits.

public static byte[] hashHeaderBits(BlockHeader header, byte[] nonce, MessageDigest md) {
    byte[] int_data = new byte[3 * 4 + 1 * 8];
    ByteBuffer bb = ByteBuffer.wrap(int_data);
    bb.putInt(header.getVersion());
    bb.putInt(header.getBlockHeight());
    bb.putLong(header.getTimestamp());
    bb.putInt(header.getSnowField());
    Assert.assertEquals(0, bb.remaining());
    md.update(nonce);
    md.update(int_data);
    md.update(header.getPrevBlockHash().toByteArray());
    md.update(header.getMerkleRootHash().toByteArray());
    md.update(header.getUtxoRootHash().toByteArray());
    md.update(header.getTarget().toByteArray());
    if (header.getVersion() == 2) {
        {
            byte[] shard_id = new byte[12];
            ByteBuffer bb_s = ByteBuffer.wrap(shard_id);
            bb_s.putInt(header.getShardId());
            bb_s.putInt(header.getTxDataSizeSum());
            bb_s.putInt(header.getTxCount());
            md.update(shard_id);
        }
        for (Map.Entry<Integer, ByteString> me : header.getShardExportRootHashMap().entrySet()) {
            int id = me.getKey();
            byte[] shard_id = new byte[4];
            ByteBuffer bb_s = ByteBuffer.wrap(shard_id);
            bb_s.putInt(id);
            md.update(shard_id);
            md.update(me.getValue().toByteArray());
        }
        for (int import_shard_id : inOrder(header.getShardImportMap().keySet())) {
            BlockImportList bil = header.getShardImportMap().get(import_shard_id);
            for (int import_height : inOrder(bil.getHeightMap().keySet())) {
                byte[] shard_id = new byte[8];
                ByteBuffer bb_s = ByteBuffer.wrap(shard_id);
                bb_s.putInt(import_shard_id);
                bb_s.putInt(import_height);
                md.update(shard_id);
                md.update(bil.getHeightMap().get(import_height).toByteArray());
            }
        }
    }
    return md.digest();
}
Also used : BigInteger(java.math.BigInteger) ByteString(com.google.protobuf.ByteString) BlockImportList(snowblossom.proto.BlockImportList) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map)

Aggregations

BlockImportList (snowblossom.proto.BlockImportList)3 ByteString (com.google.protobuf.ByteString)2 BigInteger (java.math.BigInteger)2 Map (java.util.Map)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 JSONArray (net.minidev.json.JSONArray)1 JSONObject (net.minidev.json.JSONObject)1 ChainHash (snowblossom.lib.ChainHash)1 BlockHeader (snowblossom.proto.BlockHeader)1