Search in sources :

Example 16 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class DigestUtilTest method testMerkleRoot9.

@Test
public void testMerkleRoot9() {
    ChainHash a = getRandomHash();
    ChainHash b = getRandomHash();
    ChainHash c = getRandomHash();
    ChainHash d = getRandomHash();
    ChainHash e = getRandomHash();
    ChainHash f = getRandomHash();
    ChainHash g = getRandomHash();
    ChainHash h = getRandomHash();
    ChainHash i = getRandomHash();
    ChainHash result = DigestUtil.getMerkleRootForTxList(ImmutableList.of(a, b, c, d, e, f, g, h, i));
    ChainHash ab = treeHash(a, b);
    ChainHash cd = treeHash(c, d);
    ChainHash ef = treeHash(e, f);
    ChainHash gh = treeHash(g, h);
    ChainHash abcd = treeHash(ab, cd);
    ChainHash efgh = treeHash(ef, gh);
    ChainHash abcdefgh = treeHash(abcd, efgh);
    ChainHash r = treeHash(abcdefgh, i);
    Assert.assertEquals(r, result);
}
Also used : ChainHash(snowblossom.lib.ChainHash) Test(org.junit.Test)

Example 17 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class DigestUtilTest method treeHash.

private ChainHash treeHash(ChainHash a, ChainHash b) {
    MessageDigest md = DigestUtil.getMD();
    md.update(a.toByteArray());
    md.update(b.toByteArray());
    ChainHash r = new ChainHash(md.digest());
    System.out.println("" + a + " + " + b + " -> " + r);
    return r;
}
Also used : ChainHash(snowblossom.lib.ChainHash) MessageDigest(java.security.MessageDigest)

Example 18 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class HistoryUtil method getSummaryLine.

/**
 * Returns a single line summary of a transaction with respect to a wallet.
 * If the only non-wallet address is an output, it will show as a payment out to that address
 * If the only wallet address is an output, then it will be shown as a payment in to that address
 * If all addresses are wallet addresses it will be shown as internal
 * If no addresses are wallet addresses it will be shown as external (strange case)
 * Otherwise it will show as "complex"
 */
public static String getSummaryLine(Transaction tx, Set<AddressSpecHash> wallet_addresses, NetworkParams params, SnowBlossomClient client) {
    ChainHash tx_hash = new ChainHash(tx.getTxHash());
    TransactionInner inner = TransactionUtil.getInner(tx);
    long delta = 0L;
    AddressSpecHash non_wallet_output = null;
    AddressSpecHash wallet_output = null;
    int wallet_addr = 0;
    int non_wallet_addr = 0;
    AddressSpecHash action_addr = null;
    String mode = "complex";
    for (TransactionInput in : inner.getInputsList()) {
        AddressSpecHash addr = new AddressSpecHash(in.getSpecHash());
        double v = getValue(in, client);
        if (wallet_addresses.contains(addr)) {
            delta -= v;
            wallet_addr++;
        } else {
            non_wallet_addr++;
        }
    }
    for (TransactionOutput out : inner.getOutputsList()) {
        AddressSpecHash addr = new AddressSpecHash(out.getRecipientSpecHash());
        double v = out.getValue();
        if (wallet_addresses.contains(addr)) {
            delta += v;
            wallet_addr++;
            wallet_output = addr;
        } else {
            non_wallet_addr++;
            non_wallet_output = addr;
        }
    }
    if (inner.getIsCoinbase()) {
        if (wallet_output != null) {
            action_addr = wallet_output;
        }
        mode = "block reward";
    } else if ((wallet_addr == 1) && (wallet_output != null)) {
        action_addr = wallet_output;
        mode = "payment in";
    } else if ((non_wallet_addr == 1) && (non_wallet_output != null)) {
        action_addr = non_wallet_output;
        mode = "payment out";
    } else if (wallet_addr == 0) {
        mode = "external";
    } else if (non_wallet_addr == 0) {
        mode = "internal";
    }
    DecimalFormat df = new DecimalFormat("0.000000");
    String val = df.format(delta / Globals.SNOW_VALUE_D);
    String action = "";
    if (action_addr != null) {
        action = action_addr.toAddressString(params);
    }
    return String.format("%s SNOW / %s / %s / %s", val, tx_hash.toString(), mode, action);
}
Also used : ChainHash(snowblossom.lib.ChainHash) DecimalFormat(java.text.DecimalFormat) ByteString(com.google.protobuf.ByteString) AddressSpecHash(snowblossom.lib.AddressSpecHash)

Example 19 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class HistoryUtil method getValue.

public static long getValue(TransactionInput in, SnowBlossomClient client) {
    Transaction src = getTx(new ChainHash(in.getSrcTxId()), client);
    TransactionInner inner = TransactionUtil.getInner(src);
    return inner.getOutputs(in.getSrcTxOutIdx()).getValue();
}
Also used : ChainHash(snowblossom.lib.ChainHash)

Example 20 with ChainHash

use of snowblossom.lib.ChainHash in project snowblossom by snowblossomcoin.

the class HistoryUtil method printHistory.

public static void printHistory(PrintStream out, WalletDatabase db, NetworkParams params, SnowBlossomClient client) {
    HashSet<AddressSpecHash> all = new HashSet<>();
    all.addAll(WalletUtil.getAddressesByAge(db, params));
    TreeSet<Pair<Integer, ChainHash>> transaction_history = new TreeSet<>();
    TreeSet<ChainHash> mempool_list = new TreeSet<>();
    boolean incomplete_no_history = false;
    // If this gets super slow, parallelize this loop
    for (AddressSpecHash hash : all) {
        HistoryList hl = client.getStub().getAddressHistory(RequestAddress.newBuilder().setAddressSpecHash(hash.getBytes()).build());
        if (hl.getNotEnabled()) {
            incomplete_no_history = true;
        }
        for (HistoryEntry e : hl.getEntriesList()) {
            int height = e.getBlockHeight();
            ChainHash tx_hash = new ChainHash(e.getTxHash());
            transaction_history.add(new Pair(height, tx_hash));
        }
        TransactionHashList tl = client.getStub().getMempoolTransactionList(RequestAddress.newBuilder().setAddressSpecHash(hash.getBytes()).build());
        for (ByteString h : tl.getTxHashesList()) {
            ChainHash tx_hash = new ChainHash(h);
            mempool_list.add(tx_hash);
        }
    }
    if (incomplete_no_history) {
        out.println("THIS HISTORY IS MISSING - CONNECTED NODE DOES NOT SUPPORT ADDRESS HISTORY");
    }
    for (ChainHash tx_hash : mempool_list) {
        Transaction tx = getTx(tx_hash, client);
        out.println(String.format("pending / %s", getSummaryLine(tx, all, params, client)));
    }
    for (Pair<Integer, ChainHash> p : transaction_history.descendingSet()) {
        int block = p.getA();
        ChainHash tx_hash = p.getB();
        Transaction tx = getTx(tx_hash, client);
        out.println(String.format("block %d / %s", block, getSummaryLine(tx, all, params, client)));
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ChainHash(snowblossom.lib.ChainHash) TreeSet(java.util.TreeSet) AddressSpecHash(snowblossom.lib.AddressSpecHash) HashSet(java.util.HashSet) Pair(duckutil.Pair)

Aggregations

ChainHash (snowblossom.lib.ChainHash)24 ByteString (com.google.protobuf.ByteString)10 Test (org.junit.Test)5 AddressSpecHash (snowblossom.lib.AddressSpecHash)5 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Random (java.util.Random)2 Pair (duckutil.Pair)1 ByteBuffer (java.nio.ByteBuffer)1 KeyPair (java.security.KeyPair)1 MessageDigest (java.security.MessageDigest)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 JSONArray (net.minidev.json.JSONArray)1 JSONObject (net.minidev.json.JSONObject)1 TransactionBridge (snowblossom.lib.TransactionBridge)1 ValidationException (snowblossom.lib.ValidationException)1 BlockHeader (snowblossom.proto.BlockHeader)1