Skip to content

Commit

Permalink
Address history now properly reports with reorgs
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Jun 26, 2018
1 parent e6870e1 commit fb271f3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/src/db/DB.java
Expand Up @@ -56,7 +56,7 @@ public void open()

if (config.getBoolean("addr_index"))
{
address_history_map = openMutationMapSet("addr_hist");
address_history_map = openMutationMapSet("addr_hist_2");
}
}

Expand Down
32 changes: 19 additions & 13 deletions node/src/AddressHistoryUtil.java
Expand Up @@ -15,10 +15,12 @@ public static void saveAddressHistory(Block blk, DB db)
TreeMultimap<ByteString, ByteString> map = DBMapMutationSet.createMap();
int height = blk.getHeader().getBlockHeight();

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

for(Transaction tx : blk.getTransactionsList())
{
ChainHash tx_id = new ChainHash(tx.getTxHash());
ByteString val = getValue(tx_id, height);
ByteString val = getValue(blk_id, tx_id, height);
TransactionInner inner = TransactionUtil.getInner(tx);

for(TransactionInput in : inner.getInputsList())
Expand All @@ -34,23 +36,23 @@ public static void saveAddressHistory(Block blk, DB db)

}
db.getAddressHistoryMap().addAll(map);

}

public static ByteString getValue(ChainHash tx_id, int height)
public static ByteString getValue(ChainHash blk_id, ChainHash tx_id, int height)
{
byte[] buff = new byte[4 + Globals.BLOCKCHAIN_HASH_LEN];
byte[] buff = new byte[4 + Globals.BLOCKCHAIN_HASH_LEN * 2];

ByteBuffer bb = ByteBuffer.wrap(buff);
bb.putInt(height);
bb.put(blk_id.toByteArray());
bb.put(tx_id.toByteArray());

return ByteString.copyFrom(buff);

}


public static HistoryList getHistory(AddressSpecHash spec_hash, DB db)
public static HistoryList getHistory(AddressSpecHash spec_hash, DB db, BlockHeightCache cache)
{
List<ByteString> value_set = db.getAddressHistoryMap().getSet(spec_hash.getBytes(), Globals.ADDRESS_HISTORY_MAX_REPLY);

Expand All @@ -62,22 +64,26 @@ public static HistoryList getHistory(AddressSpecHash spec_hash, DB db)
int height = bb.getInt();

byte[] b = new byte[Globals.BLOCKCHAIN_HASH_LEN];

bb.get(b);
ChainHash blk_hash = new ChainHash(b);

bb.get(b);
ChainHash tx_hash = new ChainHash(b);

hist_list.addEntries(HistoryEntry
.newBuilder()
.setBlockHeight(height)
.setTxHash(tx_hash.getBytes())
.build());
if (blk_hash.equals(cache.getHash(height)))
{
hist_list.addEntries(HistoryEntry
.newBuilder()
.setBlockHeight(height)
.setTxHash(tx_hash.getBytes())
.setBlockHash(blk_hash.getBytes())
.build());
}
}

return hist_list.build();




}


Expand Down
33 changes: 33 additions & 0 deletions node/src/BlockHeightCache.java
@@ -0,0 +1,33 @@
package snowblossom.node;


import java.util.TreeMap;
import snowblossom.lib.ChainHash;

public class BlockHeightCache
{
private TreeMap<Integer, ChainHash> map = new TreeMap<>();
private SnowBlossomNode node;

public BlockHeightCache(SnowBlossomNode node)
{
this.node = node;
}

public synchronized void setHash(int height, ChainHash hash)
{
map.put(height, hash);
}

public synchronized ChainHash getHash(int height)
{
ChainHash hash = map.get(height);
if (hash == null)
{
hash = node.getDB().getBlockHashAtHeight(height);
map.put(height, hash);
}
return hash;
}

}
1 change: 1 addition & 0 deletions node/src/BlockIngestor.java
Expand Up @@ -220,6 +220,7 @@ private void updateHeights(BlockSummary summary)
if ((found == null) || (!found.equals(hash)))
{
db.setBlockHashAtHeight(height, hash);
node.getBlockHeightCache().setHash(height, hash);
if (height == 0) return;
summary = db.getBlockSummaryMap().get(summary.getHeader().getPrevBlockHash());
}
Expand Down
4 changes: 4 additions & 0 deletions node/src/SnowBlossomNode.java
Expand Up @@ -48,6 +48,7 @@ public static void main(String args[]) throws Exception
private MemPool mem_pool;
private HashedTrie utxo_hashed_trie;
private Peerage peerage;
private BlockHeightCache block_height_cache;

private volatile boolean terminate;

Expand Down Expand Up @@ -90,6 +91,8 @@ private void loadWidgets()
peerage = new Peerage(this);
mem_pool.setPeerage(peerage);

block_height_cache = new BlockHeightCache(this);

}

private void startWidgets()
Expand Down Expand Up @@ -190,4 +193,5 @@ public boolean areWeSynced()
public MemPool getMemPool(){return mem_pool;}
public Peerage getPeerage(){return peerage;}
public SnowUserService getUserService() {return user_service;}
public BlockHeightCache getBlockHeightCache() {return block_height_cache; }
}
2 changes: 1 addition & 1 deletion node/src/SnowUserService.java
Expand Up @@ -279,7 +279,7 @@ public void getAddressHistory(RequestAddress req, StreamObserver<HistoryList> ob
{
AddressSpecHash spec_hash = new AddressSpecHash(req.getAddressSpecHash());

observer.onNext( AddressHistoryUtil.getHistory(spec_hash, node.getDB()) );
observer.onNext( AddressHistoryUtil.getHistory(spec_hash, node.getDB(), node.getBlockHeightCache()) );
observer.onCompleted();

}
Expand Down
1 change: 1 addition & 0 deletions protolib/snowblossom.proto
Expand Up @@ -248,6 +248,7 @@ message HistoryList {
message HistoryEntry {
int32 block_height = 1;
bytes tx_hash = 2;
bytes block_hash = 3;
}


Expand Down

0 comments on commit fb271f3

Please sign in to comment.