Search in sources :

Example 1 with MerkleComplecatedAccount

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

the class BaseAccountTest method basicTest.

@Test
public void basicTest() {
    String keyPrefix = "";
    MemoryKVStorage testStorage = 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);
    // OpeningAccessPolicy accPlc = new OpeningAccessPolicy();
    BlockchainKeypair bck = BlockchainKeyGenerator.getInstance().generate();
    // 新建账户;
    MerkleComplecatedAccount baseAccount = new MerkleComplecatedAccount(bck.getIdentity(), cryptoConf, Bytes.fromString(keyPrefix), testStorage, testStorage);
    // 初始化新账户时,先写入PubKey;
    assertTrue(baseAccount.isUpdated());
    assertFalse(baseAccount.isReadonly());
    // 在空白状态下写入数据;
    long v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), 0);
    // 预期失败;
    assertEquals(-1, v);
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), 1);
    // 预期失败;
    assertEquals(-1, v);
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), -1);
    // 预期成功;
    assertEquals(0, v);
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A-1"), -1);
    // 已经存在版本,指定版本号-1,预期导致失败;
    assertEquals(-1, v);
    baseAccount.commit();
    v = 0;
    for (int i = 0; i < 10; i++) {
        long s = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A_" + i), v);
        baseAccount.commit();
        // 预期成功;
        assertEquals(v + 1, s);
        v++;
    }
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A_" + v), v + 1);
    // 预期成功;
    assertEquals(-1, v);
    System.out.println("============== commit ==============");
    baseAccount.commit();
}
Also used : MerkleComplecatedAccount(com.jd.blockchain.ledger.core.MerkleComplecatedAccount) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Aggregations

CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 MerkleComplecatedAccount (com.jd.blockchain.ledger.core.MerkleComplecatedAccount)1 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)1 Test (org.junit.Test)1