Search in sources :

Example 1 with MerkleAccountSetEditor

use of com.jd.blockchain.ledger.core.MerkleAccountSetEditor in project jdchain-core by blockchain-jd-com.

the class MerkleAccountSetTest method testRegister.

@Test
public void testRegister() {
    final OpeningAccessPolicy POLICY = new OpeningAccessPolicy();
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    Bytes KEY_PREFIX = Bytes.fromString("/ACCOUNT");
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setSupportedProviders(LedgerTestUtils.getContextProviders());
    cryptoConfig.setAutoVerifyHash(true);
    cryptoConfig.setHashAlgorithm(Crypto.getAlgorithm("SHA256"));
    MerkleAccountSetEditor accountsetEditor = new MerkleAccountSetEditor(cryptoConfig, KEY_PREFIX, STORAGE, STORAGE, POLICY);
    BlockchainKeypair key1 = BlockchainKeyGenerator.getInstance().generate();
    accountsetEditor.register(key1.getIdentity());
    accountsetEditor.commit();
    CompositeAccount acc1 = accountsetEditor.getAccount(key1.getAddress());
    assertNotNull(acc1);
    assertEquals(0, accountsetEditor.getVersion(key1.getAddress()));
    acc1.getDataset().setValue("K1", TypedValue.fromText("V0"), -1);
    TypedValue v1 = acc1.getDataset().getValue("K1");
    assertNotNull(v1);
    assertEquals(0, acc1.getDataset().getVersion("K1"));
    accountsetEditor.commit();
    v1 = acc1.getDataset().getValue("K1");
    assertNotNull(v1);
    assertEquals(0, acc1.getDataset().getVersion("K1"));
}
Also used : Bytes(utils.Bytes) MerkleAccountSetEditor(com.jd.blockchain.ledger.core.MerkleAccountSetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) OpeningAccessPolicy(com.jd.blockchain.ledger.core.OpeningAccessPolicy) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) CompositeAccount(com.jd.blockchain.ledger.core.CompositeAccount) TypedValue(com.jd.blockchain.ledger.TypedValue) Test(org.junit.Test)

Example 2 with MerkleAccountSetEditor

use of com.jd.blockchain.ledger.core.MerkleAccountSetEditor in project jdchain-core by blockchain-jd-com.

the class AccountSetTest method test.

@Test
public void test() {
    OpeningAccessPolicy accessPolicy = new OpeningAccessPolicy();
    MemoryKVStorage storage = new MemoryKVStorage();
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoConf = new CryptoConfig();
    cryptoConf.setSupportedProviders(supportedProviders);
    cryptoConf.setAutoVerifyHash(true);
    cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256);
    String keyPrefix = "";
    MerkleAccountSetEditor accset = new MerkleAccountSetEditor(cryptoConf, Bytes.fromString(keyPrefix), storage, storage, accessPolicy);
    BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate();
    accset.register(userKey.getAddress(), userKey.getPubKey());
    // 尚未提交之前,可以检索到账户的存在,但版本仍然标记为 -1;
    CompositeAccount userAcc = accset.getAccount(userKey.getAddress());
    assertNotNull(userAcc);
    assertTrue(accset.contains(userKey.getAddress()));
    accset.commit();
    HashDigest rootHash = accset.getRootHash();
    assertNotNull(rootHash);
    MerkleAccountSetEditor reloadAccSet = new MerkleAccountSetEditor(rootHash, cryptoConf, Bytes.fromString(keyPrefix), storage, storage, true, accessPolicy);
    CompositeAccount reloadUserAcc = reloadAccSet.getAccount(userKey.getAddress());
    assertNotNull(reloadUserAcc);
    assertTrue(reloadAccSet.contains(userKey.getAddress()));
    assertEquals(userAcc.getID().getAddress(), reloadUserAcc.getID().getAddress());
    assertEquals(userAcc.getID().getPubKey(), reloadUserAcc.getID().getPubKey());
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleAccountSetEditor(com.jd.blockchain.ledger.core.MerkleAccountSetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) OpeningAccessPolicy(com.jd.blockchain.ledger.core.OpeningAccessPolicy) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) CompositeAccount(com.jd.blockchain.ledger.core.CompositeAccount) Test(org.junit.Test)

Aggregations

BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 CompositeAccount (com.jd.blockchain.ledger.core.CompositeAccount)2 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)2 MerkleAccountSetEditor (com.jd.blockchain.ledger.core.MerkleAccountSetEditor)2 OpeningAccessPolicy (com.jd.blockchain.ledger.core.OpeningAccessPolicy)2 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)2 Test (org.junit.Test)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)1 HashDigest (com.jd.blockchain.crypto.HashDigest)1 TypedValue (com.jd.blockchain.ledger.TypedValue)1 Bytes (utils.Bytes)1