Search in sources :

Example 11 with ChainHash

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

the class KeyUtilTest method testKeyPair.

private void testKeyPair(WalletKeyPair wkp, String name) throws Exception {
    Random rnd = new Random();
    byte[] b = new byte[Globals.BLOCKCHAIN_HASH_LEN];
    for (int i = 0; i < 8; i++) {
        rnd.nextBytes(b);
        ChainHash hash = new ChainHash(b);
        ByteString sig = SignatureUtil.sign(wkp, hash);
        SigSpec sig_spec = SigSpec.newBuilder().setSignatureType(wkp.getSignatureType()).setPublicKey(wkp.getPublicKey()).build();
        logger.info(String.format("Key report %s Pub size: %d, sig %d", name, wkp.getPublicKey().size(), sig.size()));
        logger.info("Key report: " + HexUtil.getHexString(sig));
        // logger.info("Key report: " + KeyUtil.decomposeASN1Encoded( sig ));
        Assert.assertTrue(SignatureUtil.checkSignature(sig_spec, hash.getBytes(), sig));
    }
}
Also used : ChainHash(snowblossom.lib.ChainHash) Random(java.util.Random) ByteString(com.google.protobuf.ByteString) SigSpec(snowblossom.proto.SigSpec)

Example 12 with ChainHash

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

the class DB method getBlockHashAtHeight.

@Override
public ChainHash getBlockHashAtHeight(int shard, int height) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.putInt(shard);
    bb.putInt(height);
    ByteString hash = block_height_map.get(ByteString.copyFrom(bb.array()));
    if (hash == null) {
        // old mode - height only
        bb = ByteBuffer.allocate(4);
        bb.putInt(height);
        hash = block_height_map.get(ByteString.copyFrom(bb.array()));
    }
    if (hash == null)
        return null;
    return new ChainHash(hash);
}
Also used : ChainHash(snowblossom.lib.ChainHash) ByteString(com.google.protobuf.ByteString) ByteBuffer(java.nio.ByteBuffer)

Example 13 with ChainHash

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

the class DigestUtilTest method testMerkleRootSingle.

@Test
public void testMerkleRootSingle() {
    ChainHash a = getRandomHash();
    ChainHash result = DigestUtil.getMerkleRootForTxList(ImmutableList.of(a));
    Assert.assertEquals(a, result);
}
Also used : ChainHash(snowblossom.lib.ChainHash) Test(org.junit.Test)

Example 14 with ChainHash

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

the class DigestUtilTest method testMerkleRoot8.

@Test
public void testMerkleRoot8() {
    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 result = DigestUtil.getMerkleRootForTxList(ImmutableList.of(a, b, c, d, e, f, g, h));
    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);
    Assert.assertEquals(abcdefgh, result);
}
Also used : ChainHash(snowblossom.lib.ChainHash) Test(org.junit.Test)

Example 15 with ChainHash

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

the class DigestUtilTest method testMerkleRoot2.

@Test
public void testMerkleRoot2() {
    ChainHash a = getRandomHash();
    ChainHash b = getRandomHash();
    ChainHash result = DigestUtil.getMerkleRootForTxList(ImmutableList.of(a, b));
    ChainHash ab = treeHash(a, b);
    Assert.assertEquals(ab, result);
}
Also used : ChainHash(snowblossom.lib.ChainHash) Test(org.junit.Test)

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