use of com.jd.blockchain.ledger.core.OpeningAccessPolicy 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"));
}
use of com.jd.blockchain.ledger.core.OpeningAccessPolicy 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());
}
Aggregations