Skip to content

Commit

Permalink
Merge branch 'master' of github.com:snowblossomcoin/snowblossom
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Jun 2, 2018
2 parents 826bf15 + 6c60c4c commit ccd61d1
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 382 deletions.
132 changes: 78 additions & 54 deletions src/BlockIngestor.java
@@ -1,6 +1,8 @@
package snowblossom;

import com.google.protobuf.ByteString;
import duckutil.TimeRecord;
import duckutil.TimeRecordAuto;
import snowblossom.db.DB;
import snowblossom.proto.Block;
import snowblossom.proto.BlockHeader;
Expand Down Expand Up @@ -33,6 +35,7 @@ public class BlockIngestor
private LRUCache<ChainHash, Long> block_pull_map = new LRUCache<>(1000);

private PrintStream block_log;
private TimeRecord time_record;

private boolean tx_index=false;

Expand All @@ -47,6 +50,8 @@ public BlockIngestor(SnowBlossomNode node)
if (node.getConfig().isSet("block_log"))
{
block_log = new PrintStream(new FileOutputStream(node.getConfig().get("block_log"), true));
time_record = new TimeRecord();
TimeRecord.setSharedRecord(time_record);
}

chainhead = db.getBlockSummaryMap().get(HEAD);
Expand All @@ -64,80 +69,95 @@ public BlockIngestor(SnowBlossomNode node)
public boolean ingestBlock(Block blk)
throws ValidationException
{
Validation.checkBlockBasics(node.getParams(), blk, true);
if (time_record != null) time_record.reset();

ChainHash blockhash = new ChainHash(blk.getHeader().getSnowHash());

if (db.getBlockSummaryMap().containsKey(blockhash.getBytes() ))
ChainHash blockhash;
try(TimeRecordAuto tra_blk = TimeRecord.openAuto("BlockIngestor.ingestBlock"))
{
return false;
}

ChainHash prevblock = new ChainHash(blk.getHeader().getPrevBlockHash());
Validation.checkBlockBasics(node.getParams(), blk, true);

BlockSummary prev_summary;
if (prevblock.equals(ChainHash.ZERO_HASH))
{
prev_summary = BlockSummary.newBuilder()
.setHeader(BlockHeader.newBuilder().setUtxoRootHash( HashUtils.hashOfEmpty() ).build())
.build();
}
else
{
prev_summary = db.getBlockSummaryMap().get( prevblock.getBytes() );
}
blockhash = new ChainHash(blk.getHeader().getSnowHash());

if (prev_summary == null)
{
return false;
}
if (db.getBlockSummaryMap().containsKey(blockhash.getBytes() ))
{
return false;
}

BlockSummary summary = getNewSummary(blk.getHeader(), prev_summary, node.getParams(), blk.getTransactionsCount() );
ChainHash prevblock = new ChainHash(blk.getHeader().getPrevBlockHash());

Validation.deepBlockValidation(node, blk, prev_summary);
BlockSummary prev_summary;
if (prevblock.equals(ChainHash.ZERO_HASH))
{
prev_summary = BlockSummary.newBuilder()
.setHeader(BlockHeader.newBuilder().setUtxoRootHash( HashUtils.hashOfEmpty() ).build())
.build();
}
else
{
try(TimeRecordAuto tra_prv = TimeRecord.openAuto("BlockIngestor.getPrevSummary"))
{
prev_summary = db.getBlockSummaryMap().get( prevblock.getBytes() );
}
}

if (tx_index)
{
HashMap<ByteString, Transaction> tx_map = new HashMap<>();
for(Transaction tx : blk.getTransactionsList())
if (prev_summary == null)
{
tx_map.put(tx.getTxHash(), tx);
return false;
}
db.getTransactionMap().putAll(tx_map);
}

BlockSummary summary = getNewSummary(blk.getHeader(), prev_summary, node.getParams(), blk.getTransactionsCount() );

db.getBlockMap().put( blockhash.getBytes(), blk);
db.getBlockSummaryMap().put( blockhash.getBytes(), summary);
Validation.deepBlockValidation(node, blk, prev_summary);

BigInteger summary_work_sum = BlockchainUtil.readInteger(summary.getWorkSum());
BigInteger chainhead_work_sum = BigInteger.ZERO;
if (chainhead != null)
{
chainhead_work_sum = BlockchainUtil.readInteger(chainhead.getWorkSum());
}
if (tx_index)
{
try(TimeRecordAuto tra_tx = TimeRecord.openAuto("BlockIngestor.saveTx"))
{
HashMap<ByteString, Transaction> tx_map = new HashMap<>();
for(Transaction tx : blk.getTransactionsList())
{
tx_map.put(tx.getTxHash(), tx);
}
db.getTransactionMap().putAll(tx_map);
}
}

if (summary_work_sum.compareTo(chainhead_work_sum) > 0)
{
chainhead = summary;
db.getBlockSummaryMap().put(HEAD, summary);
//System.out.println("UTXO at new root: " + HexUtil.getHexString(summary.getHeader().getUtxoRootHash()));
//node.getUtxoHashedTrie().printTree(summary.getHeader().getUtxoRootHash());

updateHeights(summary);
try(TimeRecordAuto tra_tx = TimeRecord.openAuto("BlockIngestor.blockSave"))
{
db.getBlockMap().put( blockhash.getBytes(), blk);
db.getBlockSummaryMap().put( blockhash.getBytes(), summary);
}

logger.info(String.format("New chain tip: Height %d %s (tx:%d sz:%d)", blk.getHeader().getBlockHeight(), blockhash, blk.getTransactionsCount(), blk.toByteString().size()));
BigInteger summary_work_sum = BlockchainUtil.readInteger(summary.getWorkSum());
BigInteger chainhead_work_sum = BigInteger.ZERO;
if (chainhead != null)
{
chainhead_work_sum = BlockchainUtil.readInteger(chainhead.getWorkSum());
}

SnowUserService u = node.getUserService();
if (u != null)
if (summary_work_sum.compareTo(chainhead_work_sum) > 0)
{
u.tickleBlocks();
chainhead = summary;
db.getBlockSummaryMap().put(HEAD, summary);
//System.out.println("UTXO at new root: " + HexUtil.getHexString(summary.getHeader().getUtxoRootHash()));
//node.getUtxoHashedTrie().printTree(summary.getHeader().getUtxoRootHash());

updateHeights(summary);

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

SnowUserService u = node.getUserService();
if (u != null)
{
u.tickleBlocks();
}
node.getMemPool().tickleBlocks(new ChainHash(summary.getHeader().getUtxoRootHash()));
}
node.getMemPool().tickleBlocks(new ChainHash(summary.getHeader().getUtxoRootHash()));
}


node.getPeerage().sendAllTips();
node.getPeerage().sendAllTips();
}

if (block_log != null)
{
Expand All @@ -148,6 +168,10 @@ public boolean ingestBlock(Block blk)
TransactionUtil.prettyDisplayTx(tx, block_log, params);
block_log.println();
}
time_record.printReport(block_log);
time_record.reset();


}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Globals.java
Expand Up @@ -4,7 +4,7 @@

public class Globals
{
public static final String VERSION = "1.0.4";
public static final String VERSION = "1.0.4-dev";

public static final int POW_LOOK_PASSES = 6;

Expand Down

0 comments on commit ccd61d1

Please sign in to comment.