use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.
the class SnowMerkleProof method getProof.
public SnowPowProof getProof(long word_index) throws java.io.IOException {
try (TimeRecordAuto tra = TimeRecord.openAuto("SnowMerkleProof.getProof")) {
LinkedList<ByteString> partners = new LinkedList<ByteString>();
MessageDigest md;
try {
md = MessageDigest.getInstance(Globals.SNOW_MERKLE_HASH_ALGO);
} catch (java.security.NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
getInnerProof(md, partners, word_index, 0, total_words);
SnowPowProof.Builder builder = SnowPowProof.newBuilder();
builder.setWordIdx(word_index);
builder.addAllMerkleComponent(partners);
return builder.build();
}
}
use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.
the class GetUTXOUtil method getCurrentUtxoShardHashes.
public synchronized Map<Integer, ChainHash> getCurrentUtxoShardHashes() {
if (utxo_shard_time + UTXO_ROOT_EXPIRE < System.currentTimeMillis()) {
try (TimeRecordAuto tra = TimeRecord.openAuto("GetUTXOUtil.UtxoHashesUpdate")) {
NodeStatus ns = getStub().getNodeStatus(NullRequest.newBuilder().build());
if (ns.getNetwork().length() > 0) {
if (!ns.getNetwork().equals(params.getNetworkName())) {
throw new RuntimeException(String.format("Network name mismatch. Expected %s, got %s", params.getNetworkName(), ns.getNetwork()));
}
}
TreeMap<Integer, ChainHash> utxo_map = new TreeMap<>();
for (Map.Entry<Integer, ByteString> me : ns.getShardHeadMap().entrySet()) {
ChainHash block_hash = new ChainHash(me.getValue());
utxo_map.put(me.getKey(), new ChainHash(getHeader(block_hash).getUtxoRootHash()));
}
utxo_shard_map = ImmutableMap.copyOf(utxo_map);
utxo_shard_time = System.currentTimeMillis();
logger.log(Level.FINE, "UTXO shard map: " + utxo_shard_map);
}
}
return utxo_shard_map;
}
use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.
the class GetUTXOUtil method getSpendableWithMempool.
public Map<String, TransactionBridge> getSpendableWithMempool(AddressSpecHash addr) throws ValidationException {
List<TransactionBridge> confirmed_bridges = getSpendableValidated(addr);
HashMap<String, TransactionBridge> bridge_map = new HashMap<>();
for (TransactionBridge b : confirmed_bridges) {
bridge_map.put(b.getKeyString(), b);
}
Map<Integer, TransactionHashList> tx_shard_map;
try (TimeRecordAuto tra = TimeRecord.openAuto("GetUTXOUtil.getSpendableWithMempool_mempool")) {
tx_shard_map = getStub().getMempoolTransactionMap(RequestAddress.newBuilder().setAddressSpecHash(addr.getBytes()).build()).getShardMap();
}
try (TimeRecordAuto tra = TimeRecord.openAuto("GetUTXOUtil.getSpendableWithMempool_slice")) {
for (Map.Entry<Integer, TransactionHashList> me : tx_shard_map.entrySet()) {
int shard_id = me.getKey();
for (ByteString tx_hash : me.getValue().getTxHashesList()) {
Transaction tx = getTransaction(new ChainHash(tx_hash));
TransactionInner inner = TransactionUtil.getInner(tx);
for (TransactionInput in : inner.getInputsList()) {
if (addr.equals(in.getSpecHash())) {
TransactionBridge b_in = new TransactionBridge(in, shard_id);
String key = b_in.getKeyString();
if (bridge_map.containsKey(key)) {
bridge_map.get(key).spent = true;
} else {
bridge_map.put(key, b_in);
}
}
}
for (int o = 0; o < inner.getOutputsCount(); o++) {
TransactionOutput out = inner.getOutputs(o);
if (addr.equals(out.getRecipientSpecHash())) {
// shard (shard_id) will be immediately spendable
if (ShardUtil.getCoverSet(shard_id, params).contains(out.getTargetShard())) {
TransactionBridge b_out = new TransactionBridge(out, o, new ChainHash(tx_hash), shard_id);
String key = b_out.getKeyString();
b_out.unconfirmed = true;
if (bridge_map.containsKey(key)) {
if (bridge_map.get(key).spent) {
b_out.spent = true;
}
}
bridge_map.put(key, b_out);
}
}
}
}
}
}
return bridge_map;
}
use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.
the class LoadTestShard method trySend.
private boolean trySend(TreeMap<Integer, LinkedList<TransactionBridge>> spendable_map, SplittableRandom rnd) throws Exception {
try (TimeRecordAuto tra_rate = TimeRecord.openAuto("LoadTestShard.rate_limit")) {
rate_limit.waitForRate(1.0);
}
try (TimeRecordAuto tra_sendone = TimeRecord.openAuto("LoadTestShard.send_one")) {
long min_send = 5000L;
long max_send = 50000L;
long send_delta = max_send - min_send;
int output_count = 1;
long fee = 12500;
while (rnd.nextDouble() < 0.5) output_count++;
LinkedList<TransactionOutput> out_list = new LinkedList<>();
long total_out = fee;
for (int i = 0; i < output_count; i++) {
long value = min_send + rnd.nextLong(send_delta);
int dst_shard = active_shards.get(rnd.nextInt(active_shards.size()));
if (preferred_shard >= 0) {
if (rnd.nextDouble() < 0.95) {
dst_shard = preferred_shard;
if (preferred_shards.size() > 0) {
dst_shard = preferred_shards.get(rnd.nextInt(preferred_shards.size()));
}
}
}
out_list.add(TransactionOutput.newBuilder().setRecipientSpecHash(TransactionUtil.getRandomChangeAddress(client.getPurse().getDB()).getBytes()).setValue(value).setTargetShard(dst_shard).build());
total_out += value;
}
ArrayList<Integer> source_shards = new ArrayList<>();
source_shards.addAll(spendable_map.keySet());
Collections.shuffle(source_shards);
int source_shard = source_shards.get(0);
long needed_funds = total_out;
TransactionFactoryConfig.Builder tx_config = TransactionFactoryConfig.newBuilder();
LinkedList<TransactionBridge> source_list = spendable_map.get(source_shard);
while (needed_funds > 0L) {
if (source_list.size() == 0) {
// Ran out on this shard input
spendable_map.remove(source_shard);
return false;
}
TransactionBridge br = source_list.pop();
tx_config.addInputs(br.toUTXOEntry());
needed_funds -= br.value;
}
tx_config.setSign(true);
tx_config.addAllOutputs(out_list);
tx_config.setInputSpecificList(true);
tx_config.setChangeRandomFromWallet(true);
tx_config.setFeeUseEstimate(true);
tx_config.setSplitChangeOver(2500000L);
tx_config.setChangeShardId(active_shards.get(rnd.nextInt(active_shards.size())));
if (preferred_shard >= 0) {
tx_config.setChangeShardId(preferred_shard);
if (preferred_shards.size() > 0) {
tx_config.setChangeShardId(preferred_shards.get(rnd.nextInt(preferred_shards.size())));
}
} else {
tx_config.setChangeShardId(active_shards.get(rnd.nextInt(active_shards.size())));
}
TransactionFactoryResult res = TransactionFactory.createTransaction(tx_config.build(), client.getPurse().getDB(), client);
for (Transaction tx : res.getTxsList()) {
TransactionInner inner = TransactionUtil.getInner(tx);
ChainHash tx_hash = new ChainHash(tx.getTxHash());
// logger.info("Transaction: " + new ChainHash(tx.getTxHash()) + " - " + tx.toByteString().size());
// TransactionUtil.prettyDisplayTx(tx, System.out, client.getParams());
client.getUTXOUtil().cacheTransaction(tx);
try (TimeRecordAuto tra_submit = TimeRecord.openAuto("LoadTestShard.submitAsync")) {
client.getAsyncStub().submitTransaction(tx, this);
rate_sent.record(1L);
}
}
}
return true;
}
use of duckutil.TimeRecordAuto in project snowblossom by snowblossomcoin.
the class BlockchainUtil method addBlockToHistory.
private static void addBlockToHistory(BlockSummary.Builder bs, int shard, int height, ChainHash hash) {
try (TimeRecordAuto tra = TimeRecord.openAuto("BlockchainUtil.addBlockToHistory")) {
if (!bs.getShardHistoryMap().containsKey(shard)) {
bs.putShardHistoryMap(shard, BlockImportList.newBuilder().build());
}
BlockImportList.Builder b = BlockImportList.newBuilder();
b.mergeFrom(bs.getShardHistoryMap().get(shard));
b.putHeightMap(height, hash.getBytes());
bs.putShardHistoryMap(shard, b.build());
}
}
Aggregations