/
BlockIngestor.java
259 lines (212 loc) · 7.83 KB
/
BlockIngestor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package snowblossom.node;
import com.google.protobuf.ByteString;
import duckutil.TimeRecord;
import duckutil.TimeRecordAuto;
import snowblossom.lib.*;
import snowblossom.lib.db.DB;
import snowblossom.proto.Block;
import snowblossom.proto.BlockHeader;
import snowblossom.proto.BlockSummary;
import snowblossom.proto.Transaction;
import snowblossom.lib.trie.HashUtils;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.logging.Logger;
import java.text.DecimalFormat;
/**
* This class takes in new blocks, validates them and stores them in the db.
* In appropritate, updates the tip of the chain.
*/
public class BlockIngestor
{
private static final Logger logger = Logger.getLogger("snowblossom.blockchain");
private SnowBlossomNode node;
private DB db;
private NetworkParams params;
private volatile BlockSummary chainhead;
private static final ByteString HEAD = ByteString.copyFrom(new String("head").getBytes());
private LRUCache<ChainHash, Long> block_pull_map = new LRUCache<>(1000);
private PrintStream block_log;
private TimeRecord time_record;
private boolean tx_index=false;
private boolean addr_index=false;
public BlockIngestor(SnowBlossomNode node)
throws Exception
{
this.node = node;
this.db = node.getDB();
this.params = node.getParams();
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);
if (chainhead != null)
{
logger.info(String.format("Loaded chain tip: %d %s",
chainhead.getHeader().getBlockHeight(),
new ChainHash(chainhead.getHeader().getSnowHash())));
}
tx_index = node.getConfig().getBoolean("tx_index");
addr_index = node.getConfig().getBoolean("addr_index");
}
public boolean ingestBlock(Block blk)
throws ValidationException
{
if (time_record != null) time_record.reset();
ChainHash blockhash;
try(TimeRecordAuto tra_blk = TimeRecord.openAuto("BlockIngestor.ingestBlock"))
{
Validation.checkBlockBasics(node.getParams(), blk, true, false);
blockhash = new ChainHash(blk.getHeader().getSnowHash());
if (db.getBlockSummaryMap().containsKey(blockhash.getBytes() ))
{
return false;
}
ChainHash prevblock = new ChainHash(blk.getHeader().getPrevBlockHash());
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 (prev_summary == null)
{
return false;
}
BlockSummary summary = BlockchainUtil.getNewSummary(blk.getHeader(), prev_summary, node.getParams(), blk.getTransactionsCount() );
Validation.deepBlockValidation(node.getParams(), node.getUtxoHashedTrie(), blk, prev_summary);
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 (addr_index)
{
try(TimeRecordAuto tra_tx = TimeRecord.openAuto("BlockIngestor.saveAddrHist"))
{
AddressHistoryUtil.saveAddressHistory(blk, db);
}
}
try(TimeRecordAuto tra_tx = TimeRecord.openAuto("BlockIngestor.blockSave"))
{
db.getBlockMap().put( blockhash.getBytes(), blk);
// THIS IS SUPER IMPORTANT!!!!
// the summary being saved in the summary map acts as a signal that
// - this block is fully stored
// - we have the utxo saved
// - we have the block itself saved
// - if we are using tx_index, we have the transactions saved
// - the previous block summary is also saved, which by induction means
// that we have every block from this one all the way back to block 0
// In short, after the summary is written, things can depend on this being
// a valid and correct block that goes all the way back to block 0.
// It might not be in the main chain, but it can be counted on to be valid chain
db.getBlockSummaryMap().put( blockhash.getBytes(), 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 (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);
logger.info(String.format("New chain tip: Height %d %s (tx:%d sz:%d)", blk.getHeader().getBlockHeight(), blockhash, blk.getTransactionsCount(), blk.toByteString().size()));
double age_min = System.currentTimeMillis() - blk.getHeader().getTimestamp();
age_min = age_min / 60000.0;
DecimalFormat df = new DecimalFormat("0.0");
logger.info(String.format(" The activated field is %d (%s). This block was %s minutes ago.",
chainhead.getActivatedField(),
params.getSnowFieldInfo(chainhead.getActivatedField()).getName(),
df.format(age_min)));
SnowUserService u = node.getUserService();
if (u != null)
{
u.tickleBlocks();
}
node.getMemPool().tickleBlocks(new ChainHash(summary.getHeader().getUtxoRootHash()));
}
node.getPeerage().sendAllTips();
}
if (block_log != null)
{
block_log.println("-------------------------------------------------------------");
block_log.println("Block: " + blk.getHeader().getBlockHeight() + " " + blockhash );
for(Transaction tx : blk.getTransactionsList())
{
TransactionUtil.prettyDisplayTx(tx, block_log, params);
block_log.println();
}
time_record.printReport(block_log);
time_record.reset();
}
return true;
}
private void updateHeights(BlockSummary summary)
{
while(true)
{
int height = summary.getHeader().getBlockHeight();
ChainHash found = db.getBlockHashAtHeight(height);
ChainHash hash = new ChainHash(summary.getHeader().getSnowHash());
if ((found == null) || (!found.equals(hash)))
{
db.setBlockHashAtHeight(height, hash);
if (height == 0) return;
summary = db.getBlockSummaryMap().get(summary.getHeader().getPrevBlockHash());
}
else
{
return;
}
}
}
public BlockSummary getHead()
{
return chainhead;
}
public int getHeight()
{
BlockSummary summ = getHead();
if (summ == null) return 0;
return summ.getHeader().getBlockHeight();
}
public boolean reserveBlock(ChainHash hash)
{
synchronized(block_pull_map)
{
long tm = System.currentTimeMillis();
if (block_pull_map.containsKey(hash) && (block_pull_map.get(hash) + 15000L > tm))
{
return false;
}
block_pull_map.put(hash, tm);
return true;
}
}
}