Search in sources :

Example 1 with SnowMerkle

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

the class SnowMerkleProofTest method testProofMany.

private void testProofMany(int mb_size, String seed, int count) throws Exception {
    File tmp_dir = testFolder.newFolder();
    long byte_len = mb_size * 1048576L;
    File deck = new File(tmp_dir, "test.deck.a");
    File snow = new File(tmp_dir, "test.snow");
    new SnowFall(snow.getAbsolutePath(), seed, byte_len);
    ByteString root_hash = new SnowMerkle(tmp_dir, "test", true).getRootHash();
    SnowMerkleProof proofGen = new SnowMerkleProof(tmp_dir, "test");
    int words = (int) (byte_len / SnowMerkle.HASH_LEN_LONG);
    Random rnd = new Random(byte_len);
    for (int i = 0; i < count; i++) {
        long location = rnd.nextInt(words);
        SnowPowProof proof = proofGen.getProof(location);
        Assert.assertEquals(location, proof.getWordIdx());
        Assert.assertTrue(Validation.checkProof(proof, root_hash, byte_len));
    }
}
Also used : SnowMerkleProof(snowblossom.miner.SnowMerkleProof) SnowMerkle(snowblossom.lib.SnowMerkle) Random(java.util.Random) ByteString(com.google.protobuf.ByteString) SnowFall(snowblossom.lib.SnowFall) SnowPowProof(snowblossom.proto.SnowPowProof) File(java.io.File)

Example 2 with SnowMerkle

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

the class SnowFallMerkleTest method testSize.

private void testSize(int mb_size, String seed, String expected) throws Exception {
    File tmp_dir = testFolder.newFolder();
    new SnowFall(tmp_dir.getAbsolutePath() + "/test.snow", seed, mb_size * 1048576L);
    String hash = new SnowMerkle(tmp_dir, "test", true).getRootHashStr();
    Assert.assertEquals(expected, hash);
    File deck = new File(tmp_dir, "test.deck.a");
    File snow = new File(tmp_dir, "test.snow");
    Assert.assertTrue(deck.exists());
    Assert.assertTrue(snow.exists());
    checkFile(snow);
    deck.renameTo(new File(tmp_dir, "snowdeck.snow"));
    // The deck file should have the exact same merkle root hash
    String deckhash = new SnowMerkle(tmp_dir, "snowdeck", false).getRootHashStr();
    Assert.assertEquals(expected, deckhash);
    if (SnowMerkle.getNumberOfDecks(mb_size * 1048576L / SnowMerkle.HASH_LEN_LONG) > 1) {
        File deckb = new File(tmp_dir, "test.deck.b");
        Assert.assertTrue(deckb.exists());
        deckb.renameTo(new File(tmp_dir, "snowdeckb.snow"));
        String deckhashb = new SnowMerkle(tmp_dir, "snowdeckb", false).getRootHashStr();
        Assert.assertEquals(expected, deckhashb);
    }
}
Also used : SnowMerkle(snowblossom.lib.SnowMerkle) SnowFall(snowblossom.lib.SnowFall) File(java.io.File)

Example 3 with SnowMerkle

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

the class AutoSnowFall method run.

public void run() {
    try {
        SnowFieldInfo field_info = params.getSnowFieldInfo(field);
        logger.info(String.format("Started automatic snowfall of field %d - %s", field, field_info.getName()));
        String path_name = params.getFieldSeeds().get(field);
        File field_dir = new File(snow_path, path_name);
        File snow_file = new File(field_dir, path_name + ".snow");
        field_dir.mkdirs();
        new SnowFall(snow_file.getPath(), path_name, field_info.getLength());
        logger.info(String.format("Snow field written: %s", snow_file.getPath()));
        logger.info("Starting merkle deck files");
        SnowMerkle merk = new SnowMerkle(field_dir, path_name, true);
        ByteString found_root = merk.getRootHash();
        if (field_info.getMerkleRootHash().equals(found_root)) {
            logger.info(String.format("Field %d - %s successfully matches hash %s", field, field_info.getName(), merk.getRootHashStr()));
        } else {
            logger.info("Hash mismatch for field");
        }
        done = true;
    } catch (Exception e) {
        logger.log(Level.WARNING, "Exception in autosnow: " + e);
    }
}
Also used : SnowMerkle(snowblossom.lib.SnowMerkle) SnowFieldInfo(snowblossom.lib.SnowFieldInfo) ByteString(com.google.protobuf.ByteString) SnowFall(snowblossom.lib.SnowFall) ByteString(com.google.protobuf.ByteString) File(java.io.File)

Example 4 with SnowMerkle

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

the class SnowMerkleProofTest method testProofSingle.

private void testProofSingle(int mb_size, String seed, long location) throws Exception {
    File tmp_dir = testFolder.newFolder();
    long byte_len = mb_size * 1048576L;
    File deck = new File(tmp_dir, "test.deck.a");
    File snow = new File(tmp_dir, "test.snow");
    new SnowFall(snow.getAbsolutePath(), seed, byte_len);
    ByteString root_hash = new SnowMerkle(tmp_dir, "test", true).getRootHash();
    SnowMerkleProof proofGen = new SnowMerkleProof(tmp_dir, "test");
    SnowPowProof proof = proofGen.getProof(location);
    Assert.assertEquals(location, proof.getWordIdx());
    System.out.println(proof);
    Assert.assertTrue(Validation.checkProof(proof, root_hash, byte_len));
}
Also used : SnowMerkleProof(snowblossom.miner.SnowMerkleProof) SnowMerkle(snowblossom.lib.SnowMerkle) ByteString(com.google.protobuf.ByteString) SnowFall(snowblossom.lib.SnowFall) SnowPowProof(snowblossom.proto.SnowPowProof) File(java.io.File)

Example 5 with SnowMerkle

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

the class SnowMerkleProofTest method testProofShortStack.

@Test
public void testProofShortStack() throws Exception {
    long location = 0;
    int mb_size = 1;
    String seed = "zing";
    File tmp_dir = testFolder.newFolder();
    long byte_len = mb_size * 1048576L;
    File deck = new File(tmp_dir, "test.deck.a");
    File snow = new File(tmp_dir, "test.snow");
    new SnowFall(snow.getAbsolutePath(), seed, byte_len);
    ByteString root_hash = new SnowMerkle(tmp_dir, "test", true).getRootHash();
    SnowMerkleProof proofGen = new SnowMerkleProof(tmp_dir, "test");
    SnowPowProof real_proof = proofGen.getProof(location);
    SnowPowProof.Builder fake_proof = SnowPowProof.newBuilder();
    fake_proof.setWordIdx(location);
    LinkedList<ByteString> fiends = new LinkedList<ByteString>();
    fiends.addAll(real_proof.getMerkleComponentList());
    MessageDigest md = MessageDigest.getInstance(Globals.SNOW_MERKLE_HASH_ALGO);
    md.update(fiends.poll().toByteArray());
    md.update(fiends.poll().toByteArray());
    fiends.push(ByteString.copyFrom(md.digest()));
    fake_proof.addAllMerkleComponent(fiends);
    Assert.assertFalse(Validation.checkProof(fake_proof.build(), root_hash, byte_len));
}
Also used : SnowMerkle(snowblossom.lib.SnowMerkle) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) SnowPowProof(snowblossom.proto.SnowPowProof) LinkedList(java.util.LinkedList) SnowMerkleProof(snowblossom.miner.SnowMerkleProof) SnowFall(snowblossom.lib.SnowFall) MessageDigest(java.security.MessageDigest) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)6 SnowFall (snowblossom.lib.SnowFall)6 SnowMerkle (snowblossom.lib.SnowMerkle)6 ByteString (com.google.protobuf.ByteString)5 SnowMerkleProof (snowblossom.miner.SnowMerkleProof)3 SnowPowProof (snowblossom.proto.SnowPowProof)3 SnowFieldInfo (snowblossom.lib.SnowFieldInfo)2 ConfigMem (duckutil.ConfigMem)1 MessageDigest (java.security.MessageDigest)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 Test (org.junit.Test)1 NetworkParams (snowblossom.lib.NetworkParams)1