Search in sources :

Example 1 with HashDigest

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

the class LedgersManager method initFromStorage.

/**
 * 从磁盘中保存的拓扑信息初始化
 *
 * @param ledgers 账本列表
 * @param keyPair 接入身份
 * @return
 */
private boolean initFromStorage(Set<String> ledgers, AsymmetricKeypair keyPair) {
    logger.info("Init from storage {}, keyPair {}", ledgers, KeyGenUtils.encodePubKey(keyPair.getPubKey()));
    int ledgerSize = 0;
    ledgersLock.writeLock().lock();
    try {
        for (String ledgerHash : ledgers) {
            LedgerPeersTopology topology = context.getTopologyStorage().getTopology(ledgerHash);
            if (null != topology) {
                if (null == topology.getPubKey() || null == topology.getPrivKey()) {
                    continue;
                }
                if (null == topology.getPeerAddresses() || topology.getPeerAddresses().length == 0) {
                    continue;
                }
                HashDigest ledger = Crypto.resolveAsHashDigest(Base58Utils.decode(ledgerHash));
                if (!ledgerServices.containsKey(ledger) || !ledgerServices.get(ledger).isReady()) {
                    if (ledgerServices.containsKey(ledger)) {
                        ledgerServices.get(ledger).close();
                    }
                    LedgerPeersManager peersManager = newLedgerPeersManager(ledger, keyPair, topology.getPeerAddresses());
                    ledgerServices.put(ledger, peersManager);
                    peersManager.startTimerTask();
                }
                ledgerSize++;
            }
        }
    } finally {
        ledgersLock.writeLock().unlock();
    }
    return ledgerSize > 0;
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerPeersTopology(com.jd.blockchain.gateway.service.topology.LedgerPeersTopology)

Example 2 with HashDigest

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

the class TransactionSetEditor method getTransactions.

@Override
public LedgerTransaction[] getTransactions(int fromIndex, int count) {
    LedgerTransaction[] ledgerTransactions;
    if (count > LedgerConsts.MAX_LIST_COUNT) {
        throw new IllegalArgumentException("Count exceed the upper limit[" + LedgerConsts.MAX_LIST_COUNT + "]!");
    }
    if (ledgerDataStructure.equals(LedgerDataStructure.MERKLE_TREE)) {
        SkippingIterator<HashDigest> txReqIterator = txSequence.iterator();
        txReqIterator.skip(fromIndex);
        int txCount = (int) Math.min(txReqIterator.getCount(), (long) count);
        ledgerTransactions = new LedgerTransaction[txCount];
        for (int i = 0; i < txCount; i++) {
            HashDigest txHash = txReqIterator.next();
            ledgerTransactions[i] = getTransaction(txHash);
        }
    } else {
        int txCount = (int) Math.min(getTotalCount() - fromIndex, (long) count);
        ledgerTransactions = new LedgerTransaction[txCount];
        for (int i = 0; i < txCount; i++) {
            // HashDigest txHash = loadReqHash(fromIndex + i);
            // ledgerTransactions[i] = getTransaction(txHash);
            ledgerTransactions[i] = getTransaction(fromIndex + i);
        }
    }
    return ledgerTransactions;
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction)

Example 3 with HashDigest

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

the class RaftConsensusStateMachine method catchUp.

private void catchUp(long maxHeight) throws BlockSyncException {
    HashDigest ledger = ledgerRepository.getHash();
    while (this.currentBlockHeight.get() < maxHeight) {
        PeerId leader = null;
        if (RaftNodeServerContext.getInstance().getNode(ledger) == null) {
            // 当本地快照与本地账本不一致时,需连接Leader节点进行同步, 此时本地节点尚无leader节点信息, 需要通过客户端进行同步
            RaftNodeServerContext.getInstance().refreshRouteTable(ledger);
        }
        leader = RaftNodeServerContext.getInstance().getLeader(ledger);
        if (leader == null) {
            throw new BlockSyncException("raft group not ready, current leader is none. retry it.");
        }
        blockSyncer.sync(leader.getEndpoint(), ledger, this.currentBlockHeight.get() + 1);
        this.currentBlockHeight.incrementAndGet();
    }
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 4 with HashDigest

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

the class LedgerBindingConfigTest method assertLedgerBindingConfig.

// @Test
// public void testLedgerBindingRegister() throws IOException {
// LedgerManager ledgerManager = new LedgerManager();
// ClassPathResource ledgerBindingConfigFile = new ClassPathResource("ledger-binding-1.conf");
// InputStream in = ledgerBindingConfigFile.getInputStream();
// Exception ex = null;
// try {
// LedgerBindingConfig conf = LedgerBindingConfig.resolve(in);
// //			assertLedgerBindingConfig(conf);
// 
// HashDigest[] existingLedgerHashs = ledgerManager.getLedgerHashs();
// for (HashDigest lh : existingLedgerHashs) {
// ledgerManager.unregister(lh);
// }
// HashDigest[] ledgerHashs = conf.getLedgerHashs();
// for (HashDigest ledgerHash : ledgerHashs) {
// //				setConfig(conf,ledgerHash);
// LedgerBindingConfig.BindingConfig bindingConfig = conf.getLedger(ledgerHash);
// }
// } catch (Exception e) {
// ex =e;
// } finally {
// in.close();
// }
// 
// assertNull(ex);
// }
/**
 * 判断指定的对象跟测试模板是否一致;
 *
 * @param conf
 */
private void assertLedgerBindingConfig(LedgerBindingConfig conf) {
    String[] expectedHashs = { "j5ptBmn67B2p3yki3ji1j2ZMjnJhrUvP4kFpGmcXgvrhmk", "j5kLUENMvcUooZjKfz2bEYU6zoK9DAqbdDDU8aZEZFR4qf" };
    HashDigest[] hashs = conf.getLedgerHashs();
    for (int i = 0; i < hashs.length; i++) {
        assertEquals(expectedHashs[i], hashs[i].toBase58());
    }
    BindingConfig bindingConf_0 = conf.getLedger(hashs[0]);
    assertEquals("1", bindingConf_0.getParticipant().getAddress());
    assertEquals("keys/jd-com.priv", bindingConf_0.getParticipant().getPkPath());
    assertEquals("AdSXsf5QJpy", bindingConf_0.getParticipant().getPk());
    assertNull(bindingConf_0.getParticipant().getPassword());
    assertEquals("redis://ip:port/1", bindingConf_0.getDbConnection().getUri());
    assertEquals("kksfweffj", bindingConf_0.getDbConnection().getPassword());
    BindingConfig bindingConf_1 = conf.getLedger(hashs[1]);
    assertEquals("2", bindingConf_1.getParticipant().getAddress());
    assertEquals("keys/jd-com-1.priv", bindingConf_1.getParticipant().getPkPath());
    assertNull(bindingConf_1.getParticipant().getPk());
    assertEquals("kksafe", bindingConf_1.getParticipant().getPassword());
    assertEquals("redis://ip:port/2", bindingConf_1.getDbConnection().getUri());
    assertNull(bindingConf_1.getDbConnection().getPassword());
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) BindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig) LedgerBindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig)

Example 5 with HashDigest

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

the class LedgerEditorTest method testMerkleDataSet1.

@Test
public void testMerkleDataSet1() {
    CryptoSetting setting = LedgerTestUtils.createDefaultCryptoSetting();
    Bytes keyPrefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX).concat(MerkleHashDataset.MERKLE_TREE_PREFIX);
    MemoryKVStorage storage = new MemoryKVStorage();
    byte[] key = Base58Utils.decode("j5q7n8ShYqKVitaobZrERtBK7GowGGZ54RuaUeWjLsdPYY");
    HashDigest valueHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5o6mMnMQqE5fJKJ93FzXPnu4vFCfpBKp7u4r8tUUaFRK8"));
    long version = 0;
    MerkleHashTrie merkleTree = new MerkleHashTrie(setting, keyPrefix, storage);
    merkleTree.setData(key, version, valueHash.toBytes());
    merkleTree.commit();
    MerkleTrieData data = merkleTree.getData(key);
    assertNotNull(data);
    merkleTree = new MerkleHashTrie(merkleTree.getRootHash(), setting, keyPrefix, storage, false);
    data = merkleTree.getData(key);
    assertNotNull(data);
// MerkleDataSet1 mkds = new MerkleDataSet1(setting, keyPrefix, storage, storage);
// HashDigest ledgerHash = new HashDigest(Base58Utils.decode("j5mxiw6RiHP7fhrySjYji1ER5aRe6d2quYHArtwUfsyoHZ"));
// 
// BlockchainKeypair user1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp",
// "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
// TransactionRequest req1 = LedgerTestUtils.createTxRequest_UserReg(user1, ledgerHash, 1580315317127L, parti0,
// parti0);
// // 引发错误的参数:ts=1580315317127;
// // txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
// System.out.printf("\r\n ===||=== transactionRequest1.getTransactionContent().getHash()=[%s]\r\n",
// req1.getTransactionContent().getHash().toBase58());
}
Also used : Bytes(utils.Bytes) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) 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