Search in sources :

Example 11 with HashDigest

use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.

the class LedgerTransactionDataTest method initLedgerTransactionImpl.

@Before
public void initLedgerTransactionImpl() throws Exception {
    DataContractRegistry.register(LedgerTransaction.class);
    DataContractRegistry.register(LedgerDataSnapshot.class);
    DataContractRegistry.register(HashObject.class);
    DataContractRegistry.register(DataAccountKVSetOperation.class);
    txRequest = initTxRequestMessage(ClassicAlgorithm.SHA256);
    long blockHeight = 9986L;
    TransactionStagedSnapshot snapshot = initTransactionStagedSnapshot();
    data = new TransactionResultData(txRequest.getTransactionHash(), blockHeight, TransactionState.SUCCESS, snapshot, null);
    HashDigest hash = ClassicCryptoService.SHA256.hash("zhangsan".getBytes());
    HashDigest adminAccountHash = ClassicCryptoService.SHA256.hash("lisi".getBytes());
    HashDigest userAccountSetHash = ClassicCryptoService.SHA256.hash("wangwu".getBytes());
    HashDigest dataAccountSetHash = ClassicCryptoService.SHA256.hash("zhaoliu".getBytes());
    HashDigest contractAccountSetHash = ClassicCryptoService.SHA256.hash("sunqi".getBytes());
    snapshot.setAdminAccountHash(adminAccountHash);
    snapshot.setUserAccountSetHash(userAccountSetHash);
    snapshot.setDataAccountSetHash(dataAccountSetHash);
    snapshot.setContractAccountSetHash(contractAccountSetHash);
    ledgerTransactionData = new LedgerTransactionData(txRequest, data);
}
Also used : LedgerTransactionData(com.jd.blockchain.ledger.core.LedgerTransactionData) TransactionStagedSnapshot(com.jd.blockchain.ledger.core.TransactionStagedSnapshot) HashDigest(com.jd.blockchain.crypto.HashDigest) TransactionResultData(com.jd.blockchain.ledger.core.TransactionResultData) Before(org.junit.Before)

Example 12 with HashDigest

use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.

the class LedgerTransactionDataTest method initTxRequestMessage.

private TxRequestMessage initTxRequestMessage(CryptoAlgorithm hashAlgorithm) throws Exception {
    TransactionContent txContent = initTransactionContent();
    HashDigest txHash = TxBuilder.computeTxContentHash(hashAlgorithm, txContent);
    TxRequestMessage txRequestMessage = new TxRequestMessage(txHash, txContent);
    AsymmetricKeypair keypair2 = ClassicCryptoService.ED25519.generateKeypair();
    SignatureDigest digest1 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "zhangsan".getBytes());
    SignatureDigest digest2 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "lisi".getBytes());
    DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob(keypair.getPubKey(), digest1);
    DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob(keypair2.getPubKey(), digest2);
    txRequestMessage.addEndpointSignatures(endPoint1);
    txRequestMessage.addEndpointSignatures(endPoint2);
    AsymmetricKeypair keypair4 = ClassicCryptoService.ED25519.generateKeypair();
    SignatureDigest digest3 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "wangwu".getBytes());
    SignatureDigest digest4 = ClassicCryptoService.ED25519.sign(keypair4.getPrivKey(), "zhaoliu".getBytes());
    DigitalSignatureBlob node1 = new DigitalSignatureBlob(keypair.getPubKey(), digest3);
    DigitalSignatureBlob node2 = new DigitalSignatureBlob(keypair4.getPubKey(), digest4);
    txRequestMessage.addNodeSignatures(node1);
    txRequestMessage.addNodeSignatures(node2);
    return txRequestMessage;
}
Also used : AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) DigitalSignatureBlob(com.jd.blockchain.transaction.DigitalSignatureBlob) TxRequestMessage(com.jd.blockchain.transaction.TxRequestMessage) TransactionContent(com.jd.blockchain.ledger.TransactionContent)

Example 13 with HashDigest

use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.

the class MerkleDataNodeEncoderTest method doTestV1.

private void doTestV1(long sn, long version, Bytes key, byte[] data) {
    HashFunction hashFunc = Crypto.getHashFunction(ClassicAlgorithm.SHA256);
    HashDigest dataHash = hashFunc.hash(data);
    MerkleDataNodeEncoder encoderV1 = new MerkleDataNodeEncoder_V1();
    DataNode node = encoderV1.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
    assertEquals(dataHash, node.getValueHash());
    assertEquals(sn, node.getSN());
    assertEquals(version, node.getVersion());
    assertEquals(key, node.getKey());
    byte[] nodeBytes = node.toBytes();
    DataNode node_reversed = encoderV1.resolve(nodeBytes);
    assertEquals(dataHash, node_reversed.getValueHash());
    assertEquals(node.getNodeHash(), node_reversed.getNodeHash());
    assertEquals(encoderV1.getFormatVersion(), nodeBytes[0]);
    assertEquals(sn, node_reversed.getSN());
    assertEquals(version, node_reversed.getVersion());
    assertEquals(key, node_reversed.getKey());
}
Also used : HashFunction(com.jd.blockchain.crypto.HashFunction) HashDigest(com.jd.blockchain.crypto.HashDigest) DataNode(com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode) MerkleDataNodeEncoder_V1(com.jd.blockchain.ledger.proof.MerkleDataNodeEncoder_V1) MerkleDataNodeEncoder(com.jd.blockchain.ledger.core.MerkleDataNodeEncoder)

Example 14 with HashDigest

use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.

the class PreviousDataNode method newDataNode.

static PreviousDataNode newDataNode(short hashAlgorithm, long sn, Bytes key, long version, byte[] hashedData) {
    // byte[] keyStrBytes = BytesUtils.toBytes(key);
    // int maskSize = NumberMask.SHORT.getMaskLength(keyStrBytes.length);
    int keySize = key.size();
    int maskSize = NumberMask.SHORT.getMaskLength(keySize);
    // int bodySize = 8 + maskSize + keyStrBytes.length + 8;// sn + key + version;
    // sn + key + version;
    int bodySize = 8 + maskSize + keySize + 8;
    byte[] bodyBytes = new byte[bodySize];
    int offset = 0;
    offset += BytesUtils.toBytes(sn, bodyBytes, 0);
    // NumberMask.SHORT.writeMask(keyStrBytes.length, bodyBytes, offset);
    NumberMask.SHORT.writeMask(keySize, bodyBytes, offset);
    offset += maskSize;
    // System.arraycopy(keyStrBytes, 0, bodyBytes, offset, keyStrBytes.length);
    // System.arraycopy(keyStrBytes, 0, bodyBytes, offset, keyStrBytes.length);
    // offset += keyStrBytes.length;
    offset += key.copyTo(bodyBytes, offset, keySize);
    // TODO: version;
    offset += BytesUtils.toBytes(version, bodyBytes, offset);
    byte[] dataBytes = BytesUtils.concat(bodyBytes, hashedData);
    HashFunction hashFunc = Crypto.getHashFunction(hashAlgorithm);
    HashDigest dataHash = hashFunc.hash(dataBytes);
    int hashMaskSize = NumberMask.TINY.getMaskLength(dataHash.size());
    int dataNodeSize = bodySize + hashMaskSize + dataHash.size();
    byte[] dataNodeBytes = new byte[dataNodeSize];
    offset = 0;
    System.arraycopy(bodyBytes, 0, dataNodeBytes, offset, bodySize);
    offset += bodySize;
    NumberMask.TINY.writeMask(dataHash.size(), dataNodeBytes, offset);
    offset += hashMaskSize;
    System.arraycopy(dataHash.toBytes(), 0, dataNodeBytes, offset, dataHash.size());
    return new PreviousDataNode(sn, key, version, dataHash, dataNodeBytes);
}
Also used : HashFunction(com.jd.blockchain.crypto.HashFunction) HashDigest(com.jd.blockchain.crypto.HashDigest)

Example 15 with HashDigest

use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.

the class MerkleHashDataSetTest method testInsertSameData.

@Test
public void testInsertSameData() {
    String keyPrefix = "";
    Random rand = new Random();
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleHashDataset mds = new MerkleHashDataset(cryptoSetting, keyPrefix, storage, storage);
    Dataset<String, byte[]> ds = DatasetHelper.map(mds);
    // 初始的时候没有任何数据,总是返回 null;
    DataEntry verKVEntry = ds.getDataEntry("NULL_KEY");
    byte[] vbytes = ds.getValue("NULL_KEY");
    assertNull(verKVEntry);
    assertNull(vbytes);
    Map<String, Long> dataVersions = new HashMap<>();
    // Map<String, byte[]> dataValues = new HashMap<>();
    Map<HashDigest, Map<String, KeySnapshot>> history = new LinkedHashMap<>();
    HashDigest rootHash;
    // generate base data sample;
    // + rand.nextInt(1024);
    int count = 1024;
    String key;
    byte[] data = new byte[64];
    rand.nextBytes(data);
    long v;
    MerkleProof proof;
    for (int i = 0; i < count; i++) {
        key = "data" + i;
        v = ds.setValue(key, data, -1);
        dataVersions.put(key, v);
        // dataValues.put(key + "_" + v, data);
        assertEquals(v, 0);
    }
    mds.commit();
    // Take snapshot;
    {
        rootHash = mds.getRootHash();
        Map<String, KeySnapshot> snapshot = new HashMap<>();
        for (int i = 0; i < count; i++) {
            key = "data" + i;
            // TODO: 暂时注释掉默克尔证明相关的内容;
            // proof = mds.getProof(key);
            // assertNotNull(proof);
            // assertEquals(rootHash, proof.getRootHash());
            KeySnapshot ks = new KeySnapshot();
            ks.rootHash = mds.getRootHash();
            ks.maxVersion = ds.getVersion(key);
            snapshot.put(key, ks);
        }
        history.put(rootHash, snapshot);
    }
    // verify;
    {
        MerkleHashDataset mdsReload = new MerkleHashDataset(rootHash, cryptoSetting, keyPrefix, storage, storage, true);
        Dataset<String, byte[]> dsReload = DatasetHelper.map(mdsReload);
        // verify every keys;
        Map<String, KeySnapshot> snapshot = history.get(rootHash);
        MerkleProof expProof;
        for (int i = 0; i < count; i++) {
            key = "data" + i;
            // TODO: 暂时注释掉默克尔证明相关的内容;
            // proof = mdsReload.getProof(key);
            // assertNotNull(proof);
            // assertEquals(rootHash, proof.getRootHash());
            // expProof = snapshot.get(key).rootHash;
            // assertEquals(expProof.toString(), proof.toString());
            byte[] value = dsReload.getValue(key);
            assertTrue(BytesUtils.equals(data, value));
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MerkleHashDataset(com.jd.blockchain.ledger.core.MerkleHashDataset) Dataset(utils.Dataset) MerkleHashDataset(com.jd.blockchain.ledger.core.MerkleHashDataset) MerkleProof(com.jd.blockchain.ledger.MerkleProof) LinkedHashMap(java.util.LinkedHashMap) DataEntry(utils.DataEntry) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) Random(java.util.Random) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

HashDigest (com.jd.blockchain.crypto.HashDigest)138 Test (org.junit.Test)57 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)39 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)29 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)23 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)20 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)19 Bytes (utils.Bytes)16 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)12 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)11 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)11 Random (java.util.Random)11 HashFunction (com.jd.blockchain.crypto.HashFunction)10 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)10 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)10 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)9 ArrayList (java.util.ArrayList)9 MerkleProof (com.jd.blockchain.ledger.MerkleProof)8 UserAccount (com.jd.blockchain.ledger.core.UserAccount)8 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)8