Skip to content

Commit

Permalink
Added data trie to channel
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Sep 3, 2019
1 parent 2cb49b5 commit e3c49dd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Expand Up @@ -23,7 +23,7 @@ maven_jar(
git_repository(
name = "snowblossom",
remote = "https://github.com/snowblossomcoin/snowblossom",
tag = "1.5.2.1",
tag = "1.5.2.2",
)

git_repository(
Expand Down
10 changes: 8 additions & 2 deletions protolib/channels.proto
Expand Up @@ -168,7 +168,7 @@ message ChannelBlockSummary
ChannelBlockHeader header = 3;
ChannelSettings effective_settings = 4;
SignedMessage signed_header = 5;

bytes data_root_hash = 6;

}

Expand Down Expand Up @@ -211,12 +211,18 @@ message ContentInfo {
// and included in a block. But the block creator can include any content they want
// regardless of this field
repeated bytes broadcast_channel_ids = 5;


/* What this content is in reference to, if anything */
ContentReference parent_ref = 10;
repeated ContentReference refs = 11;
repeated ContentInfo included_content = 12;

// Things to be stored in the channel map.
// The keys with prefix "www/" will be visible as static web files in the channel.
// Note: those keys should point to message ids of content infos
// Any prefixes can be used, but it is recommented to use "chandata/" for channel specific
// data as other prefixes with special handling may be added.
map<string, bytes> chan_map_updates = 20;
}


Expand Down
47 changes: 40 additions & 7 deletions src/ChannelBlockIngestor.java
Expand Up @@ -7,12 +7,15 @@
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import snowblossom.channels.proto.ChannelBlock;
import snowblossom.channels.proto.ContentInfo;
import snowblossom.channels.proto.ChannelBlockHeader;
import snowblossom.channels.proto.ChannelBlockSummary;
import snowblossom.channels.proto.SignedMessage;
import snowblossom.lib.*;
import snowblossom.lib.trie.HashUtils;

/**
* This class takes in new blocks, validates them and stores them in the db.
Expand Down Expand Up @@ -88,20 +91,36 @@ public boolean ingestBlock(ChannelBlock blk)
}
}

ChannelBlockSummary summary = ChannelValidation.deepBlockValidation(cid, blk, prev_summary);
ChannelBlockSummary.Builder summary = ChannelValidation.deepBlockValidation(cid, blk, prev_summary);

ByteString data_hash = HashUtils.hashOfEmpty();
if (prev_summary != null)
{
data_hash = prev_summary.getDataRootHash();
}

try(TimeRecordAuto tra_tx = TimeRecord.openAuto("ChannelBlockIngestor.blockSave"))
{
HashMap<ByteString, SignedMessage> content_put_map = new HashMap<>(16,0.5f);
for(SignedMessage content : blk.getContentList())
{
content_put_map.put( content.getMessageId(), content);
ContentInfo ci = ChannelSigUtil.quickPayload(content).getContentInfo();
if (ci.getChanMapUpdatesCount() > 0)
{
data_hash = updateDataTrie( ci, data_hash);

}
}
db.getContentMap().putAll(content_put_map);
db.getBlockMap().put( blockhash.getBytes(), blk);
db.getBlockSummaryMap().put( blockhash.getBytes(), summary);
db.getBlockSummaryMap().put( blockhash.getBytes(), summary.build());
}

summary.setDataRootHash(data_hash);

ChannelBlockSummary summary_fin = summary.build();

BigInteger summary_work_sum = BlockchainUtil.readInteger(summary.getWorkSum());
BigInteger chainhead_work_sum = BigInteger.ZERO;
if (chainhead != null)
Expand All @@ -112,16 +131,16 @@ public boolean ingestBlock(ChannelBlock blk)
// TODO - implement tie breakers
if (summary_work_sum.compareTo(chainhead_work_sum) > 0)
{
chainhead = summary;
db.getBlockSummaryMap().put(HEAD, summary);
chainhead = summary_fin;
db.getBlockSummaryMap().put(HEAD, summary_fin);
//System.out.println("UTXO at new root: " + HexUtil.getHexString(summary.getHeader().getUtxoRootHash()));
//node.getUtxoHashedTrie().printTree(summary.getHeader().getUtxoRootHash());

updateHeights(summary);
updateHeights(summary_fin);

logger.info(String.format("New chain tip: Height %d %s (tx:%d sz:%d)", summary.getHeader().getBlockHeight(), blockhash, blk.getContentCount(), blk.toByteString().size()));
logger.info(String.format("New chain tip: Height %d %s (tx:%d sz:%d)", summary_fin.getHeader().getBlockHeight(), blockhash, blk.getContentCount(), blk.toByteString().size()));

double age_min = System.currentTimeMillis() - summary.getHeader().getTimestamp();
double age_min = System.currentTimeMillis() - summary_fin.getHeader().getTimestamp();
age_min = age_min / 60000.0;

DecimalFormat df = new DecimalFormat("0.0");
Expand All @@ -136,6 +155,20 @@ public boolean ingestBlock(ChannelBlock blk)

}

private ByteString updateDataTrie(ContentInfo ci, ByteString data_hash)
{
HashMap<ByteString, ByteString> update_map = new HashMap<>();
for(Map.Entry<String, ByteString> me : ci.getChanMapUpdates().entrySet())
{
ByteString key = ByteString.copyFrom(me.getKey().getBytes());
update_map.put(key, me.getValue());
}

data_hash = db.getDataTrie().mergeBatch(data_hash, update_map);
return data_hash;

}

private void updateHeights(ChannelBlockSummary summary)
{
while(true)
Expand Down
4 changes: 2 additions & 2 deletions src/ChannelValidation.java
Expand Up @@ -188,7 +188,7 @@ public static void validateRef(ContentReference ref)
}


public static ChannelBlockSummary deepBlockValidation(ChannelID chan_id, ChannelBlock blk, ChannelBlockSummary prev_summary)
public static ChannelBlockSummary.Builder deepBlockValidation(ChannelID chan_id, ChannelBlock blk, ChannelBlockSummary prev_summary)
throws ValidationException
{
checkBlockBasics(chan_id, blk, true);
Expand Down Expand Up @@ -274,7 +274,7 @@ public static ChannelBlockSummary deepBlockValidation(ChannelID chan_id, Channel
sum.setBlockId(blk.getSignedHeader().getMessageId());
sum.setEffectiveSettings(settings);

return sum.build();
return sum;

}

Expand Down
9 changes: 9 additions & 0 deletions src/SingleChannelDB.java
Expand Up @@ -18,6 +18,8 @@
import snowblossom.lib.db.DBProvider;
import snowblossom.lib.db.ProtoDBMap;
import snowblossom.lib.db.rocksdb.JRocksDB;
import snowblossom.lib.trie.HashedTrie;
import snowblossom.lib.trie.TrieDBMap;

/**
* Database releated to a single channel
Expand All @@ -36,6 +38,8 @@ public class SingleChannelDB
protected ProtoDBMap<SignedMessage> content_map;
protected ProtoDBMap<ChannelBlockSummary> summary_map;
protected DBMap block_height_map;
protected DBMap data_map;
protected HashedTrie data_trie;


public SingleChannelDB(Config base_config, ChannelID cid)
Expand Down Expand Up @@ -71,12 +75,17 @@ public void open()
summary_map = new ProtoDBMap(ChannelBlockSummary.newBuilder().build().getParserForType(), prov.openMap("block_sum"));
block_height_map = prov.openMap("height");

data_map = prov.openMap("d");
data_trie = new HashedTrie(new TrieDBMap(data_map), true, true);


}

public ProtoDBMap<ChannelBlock> getBlockMap(){return block_map; }
public ProtoDBMap<LocalPeerInfo> getPeerMap(){return peer_map; }
public ProtoDBMap<SignedMessage> getContentMap(){return content_map; }
public ProtoDBMap<ChannelBlockSummary> getBlockSummaryMap(){return summary_map; }
public HashedTrie getDataTrie() {return data_trie; }

public ChainHash getBlockHashAtHeight(long height)
{
Expand Down
6 changes: 0 additions & 6 deletions test/ChannelPeerTest.java
Expand Up @@ -82,12 +82,9 @@ public void testChannelPeerAndSync()
header.setPrevBlockHash( ChainHash.ZERO_HASH.getBytes());
header.setContentMerkle( ChainHash.ZERO_HASH.getBytes());


ChannelBlock.Builder blk = ChannelBlock.newBuilder();
blk.setSignedHeader( ChannelSigUtil.signMessage(admin_db.getAddresses(0), admin_db.getKeys(0),
SignedMessagePayload.newBuilder().setChannelBlockHeader(header.build()).build()));



ctx_a = node_a.getChannelSubscriber().openChannel(chan_id);
Thread.sleep(500);
Expand All @@ -96,9 +93,6 @@ public void testChannelPeerAndSync()
prev_hash = new ChainHash(blk.getSignedHeader().getMessageId());

ctx_a.block_ingestor.ingestBlock(blk.build());

//sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), null);

}

for(int i=0; i<20; i++)
Expand Down
8 changes: 4 additions & 4 deletions test/ChannelValidationTest.java
Expand Up @@ -68,7 +68,7 @@ public void testChannelValidateGood()
SignedMessagePayload.newBuilder().setChannelBlockHeader(header.build()).build()));


sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), null);
sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), null).build();

}

Expand Down Expand Up @@ -113,7 +113,7 @@ public void testChannelValidateGood()
}


sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), sum);
sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), sum).build();
}

// Set new settings
Expand All @@ -138,7 +138,7 @@ public void testChannelValidateGood()
blk.setSignedHeader( ChannelSigUtil.signMessage(admin_db.getAddresses(0), admin_db.getKeys(0),
SignedMessagePayload.newBuilder().setChannelBlockHeader(header.build()).build()));

sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), sum);
sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), sum).build();

}
// try new settings
Expand All @@ -156,7 +156,7 @@ public void testChannelValidateGood()
blk.setSignedHeader( ChannelSigUtil.signMessage(block_db2.getAddresses(0), block_db2.getKeys(0),
SignedMessagePayload.newBuilder().setChannelBlockHeader(header.build()).build()));

sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), sum);
sum = ChannelValidation.deepBlockValidation(chan_id, blk.build(), sum).build();

}

Expand Down

0 comments on commit e3c49dd

Please sign in to comment.