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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations