| @@ -22,7 +22,7 @@ public interface DataCodes { | |||
| public static final int DATA_SNAPSHOT = 0x130; | |||
| public static final int LEDGER_ADMIN_INFO = 0x131; | |||
| // public static final int LEDGER_ADMIN_DATA = 0x131; | |||
| public static final int TX = 0x200; | |||
| @@ -48,6 +48,10 @@ public class AccountSet implements Transactional, MerkleProvable { | |||
| public boolean isReadonly() { | |||
| return merkleDataset.isReadonly(); | |||
| } | |||
| void setReadonly() { | |||
| merkleDataset.setReadonly(); | |||
| } | |||
| public AccountSet(CryptoSetting cryptoSetting, String keyPrefix, ExPolicyKVStorage exStorage, | |||
| VersioningKVStorage verStorage, AccountAccessPolicy accessPolicy) { | |||
| @@ -33,6 +33,10 @@ public class ContractAccountSet implements MerkleProvable, Transactional { | |||
| return accountSet.isReadonly(); | |||
| } | |||
| void setReadonly() { | |||
| accountSet.setReadonly(); | |||
| } | |||
| @Override | |||
| public HashDigest getRootHash() { | |||
| return accountSet.getRootHash(); | |||
| @@ -33,6 +33,10 @@ public class DataAccountSet implements MerkleProvable, Transactional { | |||
| return accountSet.isReadonly(); | |||
| } | |||
| void setReadonly() { | |||
| accountSet.setReadonly(); | |||
| } | |||
| @Override | |||
| public HashDigest getRootHash() { | |||
| return accountSet.getRootHash(); | |||
| @@ -77,20 +77,28 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminInfo { | |||
| private ExPolicyKVStorage storage; | |||
| private HashDigest adminAccountHash; | |||
| private HashDigest adminDataHash; | |||
| private boolean readonly; | |||
| private boolean updated; | |||
| public HashDigest getHash() { | |||
| return adminAccountHash; | |||
| return adminDataHash; | |||
| } | |||
| public boolean isReadonly() { | |||
| return readonly; | |||
| } | |||
| void setReadonly() { | |||
| this.readonly = true; | |||
| } | |||
| public LedgerSettings getPreviousSetting() { | |||
| return previousSettings; | |||
| } | |||
| @Override | |||
| public RolePrivilegeSettings getRolePrivileges() { | |||
| return rolePrivileges; | |||
| @@ -132,7 +140,7 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminInfo { | |||
| initSetting.getCryptoSetting()); | |||
| this.previousSettings = new LedgerConfiguration(settings); | |||
| this.previousSettingHash = null; | |||
| this.adminAccountHash = null; | |||
| this.adminDataHash = null; | |||
| // 基于原配置初始化参与者列表; | |||
| String partiPrefix = keyPrefix + LEDGER_PARTICIPANT_PREFIX; | |||
| @@ -168,7 +176,7 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminInfo { | |||
| // 复制记录一份配置作为上一个区块的原始配置,该实例仅供读取,不做修改,也不会回写到存储; | |||
| this.previousSettings = new LedgerConfiguration(settings); | |||
| this.previousSettingHash = metadata.getSettingsHash(); | |||
| this.adminAccountHash = adminAccountHash; | |||
| this.adminDataHash = adminAccountHash; | |||
| String partiPrefix = keyPrefix + LEDGER_PARTICIPANT_PREFIX; | |||
| this.participants = new ParticipantDataset(metadata.getParticipantsHash(), previousSettings.getCryptoSetting(), | |||
| @@ -238,17 +246,17 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminInfo { | |||
| return metadata; | |||
| } | |||
| /** | |||
| * 返回原来的账本配置; | |||
| * | |||
| * <br> | |||
| * 此方法总是返回从上一个区块加载的账本配置,即时调用 {@link #setLedgerSetting(LedgerSettings)} 做出了新的更改; | |||
| * | |||
| * @return | |||
| */ | |||
| public LedgerSettings getPreviousSetting() { | |||
| return previousSettings; | |||
| } | |||
| // /** | |||
| // * 返回原来的账本配置; | |||
| // * | |||
| // * <br> | |||
| // * 此方法总是返回从上一个区块加载的账本配置,即时调用 {@link #setLedgerSetting(LedgerSettings)} 做出了新的更改; | |||
| // * | |||
| // * @return | |||
| // */ | |||
| // public LedgerSettings getPreviousSetting() { | |||
| // return previousSettings; | |||
| // } | |||
| /** | |||
| * 返回当前设置的账本配置; | |||
| @@ -339,7 +347,7 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminInfo { | |||
| byte[] metadataBytes = serializeMetadata(metadata); | |||
| HashDigest metadataHash = hashFunc.hash(metadataBytes); | |||
| if (adminAccountHash == null || !adminAccountHash.equals(metadataHash)) { | |||
| if (adminDataHash == null || !adminDataHash.equals(metadataHash)) { | |||
| // update modify; | |||
| // String base58MetadataHash = metadataHash.toBase58(); | |||
| // String metadataKey = encodeMetadataKey(base58MetadataHash); | |||
| @@ -354,7 +362,7 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminInfo { | |||
| throw new LedgerException(errMsg); | |||
| } | |||
| adminAccountHash = metadataHash; | |||
| adminDataHash = metadataHash; | |||
| } | |||
| updated = false; | |||
| @@ -1,23 +0,0 @@ | |||
| package com.jd.blockchain.ledger.core; | |||
| import com.jd.blockchain.ledger.LedgerAdminInfo; | |||
| /** | |||
| * {@link LedgerDataSet} 表示账本在某一个区块上的数据集合; | |||
| * | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| public interface LedgerDataSet{ | |||
| boolean isReadonly(); | |||
| LedgerAdminInfo getAdminAccount(); | |||
| UserAccountSet getUserAccountSet(); | |||
| DataAccountSet getDataAccountSet(); | |||
| ContractAccountSet getContractAccountSet(); | |||
| } | |||
| @@ -1,34 +1,31 @@ | |||
| package com.jd.blockchain.ledger.core.impl; | |||
| package com.jd.blockchain.ledger.core; | |||
| import com.jd.blockchain.ledger.LedgerAdminInfo; | |||
| import com.jd.blockchain.ledger.core.*; | |||
| import com.jd.blockchain.utils.Transactional; | |||
| public class LedgerDataSetImpl implements LedgerDataSet, Transactional { | |||
| public class LedgerDataSetImpl implements LedgerDataset, Transactional { | |||
| private LedgerAdminDataset adminAccount; | |||
| private LedgerAdminDataset adminDataset; | |||
| private UserAccountSet userAccountSet; | |||
| private DataAccountSet dataAccountSet; | |||
| private ContractAccountSet contractAccountSet; | |||
| private boolean readonly; | |||
| private boolean readonly; | |||
| /** | |||
| * Create new block; | |||
| * | |||
| * @param adminAccount | |||
| * @param userAccountSet | |||
| * @param dataAccountSet | |||
| * @param contractAccountSet | |||
| * @param readonly | |||
| */ | |||
| public LedgerDataSetImpl(LedgerAdminDataset adminAccount, | |||
| UserAccountSet userAccountSet, DataAccountSet dataAccountSet, ContractAccountSet contractAccountSet, | |||
| boolean readonly) { | |||
| this.adminAccount = adminAccount; | |||
| public LedgerDataSetImpl(LedgerAdminDataset adminAccount, UserAccountSet userAccountSet, | |||
| DataAccountSet dataAccountSet, ContractAccountSet contractAccountSet, boolean readonly) { | |||
| this.adminDataset = adminAccount; | |||
| this.userAccountSet = userAccountSet; | |||
| this.dataAccountSet = dataAccountSet; | |||
| this.contractAccountSet = contractAccountSet; | |||
| @@ -37,8 +34,8 @@ public class LedgerDataSetImpl implements LedgerDataSet, Transactional { | |||
| } | |||
| @Override | |||
| public LedgerAdminInfo getAdminAccount() { | |||
| return adminAccount; | |||
| public LedgerAdminDataset getAdminDataset() { | |||
| return adminDataset; | |||
| } | |||
| @Override | |||
| @@ -52,13 +49,13 @@ public class LedgerDataSetImpl implements LedgerDataSet, Transactional { | |||
| } | |||
| @Override | |||
| public ContractAccountSet getContractAccountSet() { | |||
| public ContractAccountSet getContractAccountset() { | |||
| return contractAccountSet; | |||
| } | |||
| @Override | |||
| public boolean isUpdated() { | |||
| return adminAccount.isUpdated() || userAccountSet.isUpdated() || dataAccountSet.isUpdated() | |||
| return adminDataset.isUpdated() || userAccountSet.isUpdated() || dataAccountSet.isUpdated() | |||
| || contractAccountSet.isUpdated(); | |||
| } | |||
| @@ -71,7 +68,7 @@ public class LedgerDataSetImpl implements LedgerDataSet, Transactional { | |||
| return; | |||
| } | |||
| adminAccount.commit(); | |||
| adminDataset.commit(); | |||
| userAccountSet.commit(); | |||
| dataAccountSet.commit(); | |||
| contractAccountSet.commit(); | |||
| @@ -79,7 +76,7 @@ public class LedgerDataSetImpl implements LedgerDataSet, Transactional { | |||
| @Override | |||
| public void cancel() { | |||
| adminAccount.cancel(); | |||
| adminDataset.cancel(); | |||
| userAccountSet.cancel(); | |||
| dataAccountSet.cancel(); | |||
| contractAccountSet.cancel(); | |||
| @@ -90,4 +87,12 @@ public class LedgerDataSetImpl implements LedgerDataSet, Transactional { | |||
| return readonly; | |||
| } | |||
| void setReadonly() { | |||
| this.readonly = true; | |||
| this.adminDataset.setReadonly(); | |||
| this.userAccountSet.setReadonly(); | |||
| this.dataAccountSet.setReadonly(); | |||
| this.contractAccountSet.setReadonly(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| package com.jd.blockchain.ledger.core; | |||
| /** | |||
| * {@link LedgerDataset} 表示账本在某一个区块上的数据集合; | |||
| * | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| public interface LedgerDataset{ | |||
| boolean isReadonly(); | |||
| LedgerAdminDataset getAdminDataset(); | |||
| UserAccountSet getUserAccountSet(); | |||
| DataAccountSet getDataAccountSet(); | |||
| ContractAccountSet getContractAccountset(); | |||
| } | |||
| @@ -11,7 +11,7 @@ import com.jd.blockchain.ledger.TransactionRequest; | |||
| * <p> | |||
| * | |||
| * {@link LedgerEditor} 以上一个区块作为数据编辑的起点; <br> | |||
| * 对账本数据({@link #getDataSet()})的批量更改可以作为一个交易({@link LedgerTransaction})整体提交暂存,形成暂存点; | |||
| * 对账本数据({@link #getDataset()})的批量更改可以作为一个交易({@link LedgerTransaction})整体提交暂存,形成暂存点; | |||
| * <br> | |||
| * | |||
| * @author huanghaiquan | |||
| @@ -33,11 +33,25 @@ public interface LedgerEditor { | |||
| */ | |||
| long getBlockHeight(); | |||
| /** | |||
| * 最新的账本数据集; | |||
| * | |||
| * @return | |||
| */ | |||
| LedgerDataset getLedgerDataset(); | |||
| /** | |||
| * 最新的交易集合; | |||
| * | |||
| * @return | |||
| */ | |||
| TransactionSet getTransactionSet(); | |||
| /** | |||
| * 开始新事务;<br> | |||
| * | |||
| * 方法返回之前,将会校验交易请求的用户签名列表和节点签名列表,并在后续对数据集 | |||
| * {@link LedgerTransactionContext#getDataSet()} 的操作时,校验这些用户和节点是否具备权限;<br> | |||
| * {@link LedgerTransactionContext#getDataset()} 的操作时,校验这些用户和节点是否具备权限;<br> | |||
| * | |||
| * 校验失败将引发异常 {@link LedgerException}; | |||
| * <p> | |||
| @@ -52,7 +66,8 @@ public interface LedgerEditor { | |||
| * | |||
| * | |||
| * | |||
| * 注:方法不解析、不执行交易中的操作;<p> | |||
| * 注:方法不解析、不执行交易中的操作; | |||
| * <p> | |||
| * | |||
| * @param txRequest 交易请求; | |||
| * @return | |||
| @@ -53,22 +53,22 @@ public interface LedgerRepository extends Closeable { | |||
| LedgerBlock getBlock(long height); | |||
| LedgerAdminInfo getAdminInfo(); | |||
| LedgerAdminInfo getAdminInfo(LedgerBlock block); | |||
| LedgerBlock getBlock(HashDigest hash); | |||
| LedgerDataSet getDataSet(LedgerBlock block); | |||
| LedgerDataset getDataSet(LedgerBlock block); | |||
| TransactionSet getTransactionSet(LedgerBlock block); | |||
| LedgerAdminInfo getAdminAccount(LedgerBlock block); | |||
| UserAccountSet getUserAccountSet(LedgerBlock block); | |||
| DataAccountSet getDataAccountSet(LedgerBlock block); | |||
| ContractAccountSet getContractAccountSet(LedgerBlock block); | |||
| default LedgerDataSet getDataSet() { | |||
| default LedgerDataset getDataSet() { | |||
| return getDataSet(getLatestBlock()); | |||
| } | |||
| @@ -76,10 +76,6 @@ public interface LedgerRepository extends Closeable { | |||
| return getTransactionSet(getLatestBlock()); | |||
| } | |||
| default LedgerAdminInfo getAdminAccount() { | |||
| return getAdminAccount(getLatestBlock()); | |||
| } | |||
| default UserAccountSet getUserAccountSet() { | |||
| return getUserAccountSet(getLatestBlock()); | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package com.jd.blockchain.ledger.core.impl; | |||
| package com.jd.blockchain.ledger.core; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| @@ -12,18 +12,8 @@ import com.jd.blockchain.ledger.LedgerDataSnapshot; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.LedgerSettings; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.core.AccountAccessPolicy; | |||
| import com.jd.blockchain.ledger.core.ContractAccountSet; | |||
| import com.jd.blockchain.ledger.core.DataAccountSet; | |||
| import com.jd.blockchain.ledger.core.LedgerAdminDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerConsts; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| import com.jd.blockchain.ledger.core.SettingContext; | |||
| import com.jd.blockchain.ledger.core.TransactionSet; | |||
| import com.jd.blockchain.ledger.core.UserAccountSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerBlockData; | |||
| import com.jd.blockchain.ledger.core.impl.OpeningAccessPolicy; | |||
| import com.jd.blockchain.storage.service.ExPolicyKVStorage; | |||
| import com.jd.blockchain.storage.service.VersioningKVStorage; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| @@ -57,11 +47,6 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| private static final Bytes TRANSACTION_SET_PREFIX = Bytes.fromString("TXS" + LedgerConsts.KEY_SEPERATOR); | |||
| private static final AccountAccessPolicy DEFAULT_ACCESS_POLICY = new OpeningAccessPolicy(); | |||
| /** | |||
| * 经过上一轮共识确认的账本管理配置; | |||
| */ | |||
| private LedgerAdminInfo approvedAdminInfo; | |||
| private HashDigest ledgerHash; | |||
| @@ -78,7 +63,7 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| private volatile LedgerEditor nextBlockEditor; | |||
| private volatile boolean closed = false; | |||
| public LedgerRepositoryImpl(HashDigest ledgerHash, String keyPrefix, ExPolicyKVStorage exPolicyStorage, | |||
| VersioningKVStorage versioningStorage) { | |||
| this.keyPrefix = keyPrefix; | |||
| @@ -91,6 +76,8 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| if (getLatestBlockHeight() < 0) { | |||
| throw new RuntimeException("Ledger doesn't exist!"); | |||
| } | |||
| retrieveLatestState(); | |||
| } | |||
| /* | |||
| @@ -121,25 +108,37 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| @Override | |||
| public LedgerBlock getLatestBlock() { | |||
| LedgerState state = getLatestState(); | |||
| return state.block; | |||
| return latestState.block; | |||
| } | |||
| private LedgerState getLatestState() { | |||
| LedgerState state = latestState; | |||
| if (state == null) { | |||
| LedgerBlock latestBlock = innerGetBlock(innerGetLatestBlockHeight()); | |||
| state = new LedgerState(latestBlock); | |||
| latestState = state; | |||
| } | |||
| return state; | |||
| // private LedgerState getLatestState() { | |||
| // LedgerState state = latestState; | |||
| // if (state == null) { | |||
| // LedgerBlock latestBlock = innerGetBlock(innerGetLatestBlockHeight()); | |||
| // state = new LedgerState(latestBlock); | |||
| // latestState = state; | |||
| // } | |||
| // return state; | |||
| // } | |||
| /** | |||
| * 重新检索加载最新的状态; | |||
| * | |||
| * @return | |||
| */ | |||
| private LedgerState retrieveLatestState() { | |||
| LedgerBlock latestBlock = innerGetBlock(innerGetLatestBlockHeight()); | |||
| LedgerDataset ledgerDataset = innerGetLedgerDataset(latestBlock); | |||
| TransactionSet txSet = loadTransactionSet(latestBlock.getTransactionSetHash(), | |||
| ledgerDataset.getAdminDataset().getSettings().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| this.latestState = new LedgerState(latestBlock, ledgerDataset, txSet); | |||
| return latestState; | |||
| } | |||
| @Override | |||
| public LedgerBlock retrieveLatestBlock() { | |||
| LedgerBlock latestBlock = innerGetBlock(innerGetLatestBlockHeight()); | |||
| latestState = new LedgerState(latestBlock); | |||
| return latestBlock; | |||
| return retrieveLatestState().block; | |||
| } | |||
| @Override | |||
| @@ -198,7 +197,7 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| if (height < 0) { | |||
| return null; | |||
| } | |||
| return innerGetBlock(getBlockHash(height)); | |||
| return innerGetBlock(innerGetBlockHash(height)); | |||
| } | |||
| @Override | |||
| @@ -220,26 +219,18 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| throw new RuntimeException("Block hash not equals to it's storage key!"); | |||
| } | |||
| // verify hash; | |||
| // boolean requiredVerifyHash = | |||
| // adminAccount.getMetadata().getSetting().getCryptoSetting().getAutoVerifyHash(); | |||
| // TODO: 未实现从配置中加载是否校验 Hash 的设置; | |||
| if (SettingContext.queryingSettings().verifyHash()) { | |||
| byte[] blockBodyBytes = null; | |||
| if (block.getHeight() == 0) { | |||
| // 计算创世区块的 hash 时,不包括 ledgerHash 字段; | |||
| block.setLedgerHash(null); | |||
| blockBodyBytes = BinaryProtocol.encode(block, BlockBody.class); | |||
| // 恢复; | |||
| block.setLedgerHash(block.getHash()); | |||
| } else { | |||
| blockBodyBytes = BinaryProtocol.encode(block, BlockBody.class); | |||
| } | |||
| HashFunction hashFunc = Crypto.getHashFunction(blockHash.getAlgorithm()); | |||
| boolean pass = hashFunc.verify(blockHash, blockBodyBytes); | |||
| if (!pass) { | |||
| throw new RuntimeException("Block hash verification fail!"); | |||
| } | |||
| // verify block hash; | |||
| byte[] blockBodyBytes = null; | |||
| if (block.getHeight() == 0) { | |||
| // 计算创世区块的 hash 时,不包括 ledgerHash 字段; | |||
| blockBodyBytes = BinaryProtocol.encode(block, BlockBody.class); | |||
| } else { | |||
| blockBodyBytes = BinaryProtocol.encode(block, BlockBody.class); | |||
| } | |||
| HashFunction hashFunc = Crypto.getHashFunction(blockHash.getAlgorithm()); | |||
| boolean pass = hashFunc.verify(blockHash, blockBodyBytes); | |||
| if (!pass) { | |||
| throw new RuntimeException("Block hash verification fail!"); | |||
| } | |||
| // verify height; | |||
| @@ -254,9 +245,18 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| return block; | |||
| } | |||
| /** | |||
| * 获取最新区块的账本参数; | |||
| * | |||
| * @return | |||
| */ | |||
| private LedgerSettings getLatestSettings() { | |||
| return getAdminInfo().getSettings(); | |||
| } | |||
| @Override | |||
| public LedgerAdminInfo getAdminInfo() { | |||
| return getAdminAccount(getLatestBlock()); | |||
| return getAdminInfo(getLatestBlock()); | |||
| } | |||
| private LedgerBlock deserialize(byte[] blockBytes) { | |||
| @@ -266,140 +266,169 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| @Override | |||
| public TransactionSet getTransactionSet(LedgerBlock block) { | |||
| long height = getLatestBlockHeight(); | |||
| TransactionSet transactionSet = null; | |||
| // TransactionSet transactionSet = null; | |||
| if (height == block.getHeight()) { | |||
| // 缓存读; | |||
| LedgerState state = getLatestState(); | |||
| transactionSet = state.transactionSet; | |||
| if (transactionSet == null) { | |||
| LedgerAdminInfo adminAccount = getAdminAccount(block); | |||
| transactionSet = loadTransactionSet(block.getTransactionSetHash(), | |||
| adminAccount.getSettings().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| state.transactionSet = transactionSet; | |||
| } | |||
| return transactionSet; | |||
| } | |||
| LedgerAdminInfo adminAccount = getAdminAccount(block); | |||
| // // 缓存最近一个区块的数据; | |||
| // LedgerState state = getLatestState(); | |||
| // transactionSet = state.transactionSet; | |||
| // if (transactionSet == null) { | |||
| // LedgerAdminInfo adminAccount = getAdminInfo(block); | |||
| // transactionSet = loadTransactionSet(block.getTransactionSetHash(), | |||
| // adminAccount.getSettings().getCryptoSetting(), keyPrefix, exPolicyStorage, versioningStorage, | |||
| // true); | |||
| // state.transactionSet = transactionSet; | |||
| // } | |||
| // return transactionSet; | |||
| // 从缓存中返回最新区块的数据集; | |||
| return latestState.getTransactionSet(); | |||
| } | |||
| LedgerAdminInfo adminAccount = getAdminInfo(block); | |||
| // All of existing block is readonly; | |||
| return loadTransactionSet(block.getTransactionSetHash(), | |||
| adminAccount.getSettings().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| return loadTransactionSet(block.getTransactionSetHash(), adminAccount.getSettings().getCryptoSetting(), | |||
| keyPrefix, exPolicyStorage, versioningStorage, true); | |||
| } | |||
| @Override | |||
| public LedgerAdminDataset getAdminAccount(LedgerBlock block) { | |||
| public LedgerAdminDataset getAdminInfo(LedgerBlock block) { | |||
| long height = getLatestBlockHeight(); | |||
| LedgerAdminDataset adminAccount = null; | |||
| // LedgerAdminDataset adminAccount = null; | |||
| if (height == block.getHeight()) { | |||
| // 缓存读; | |||
| LedgerState state = getLatestState(); | |||
| adminAccount = state.adminAccount; | |||
| if (adminAccount == null) { | |||
| adminAccount = new LedgerAdminDataset(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| state.adminAccount = adminAccount; | |||
| } | |||
| return adminAccount; | |||
| // // 缓存读; | |||
| // LedgerState state = getLatestState(); | |||
| // adminAccount = state.adminAccount; | |||
| // if (adminAccount == null) { | |||
| // adminAccount = new LedgerAdminDataset(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, | |||
| // versioningStorage, true); | |||
| // state.adminAccount = adminAccount; | |||
| // } | |||
| // return adminAccount; | |||
| return latestState.getAdminDataset(); | |||
| } | |||
| return createAdminDataset(block); | |||
| } | |||
| private LedgerAdminDataset createAdminDataset(LedgerBlock block) { | |||
| return new LedgerAdminDataset(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, versioningStorage, true); | |||
| } | |||
| @Override | |||
| public UserAccountSet getUserAccountSet(LedgerBlock block) { | |||
| long height = getLatestBlockHeight(); | |||
| UserAccountSet userAccountSet = null; | |||
| // UserAccountSet userAccountSet = null; | |||
| if (height == block.getHeight()) { | |||
| // 缓存读; | |||
| LedgerState state = getLatestState(); | |||
| userAccountSet = state.userAccountSet; | |||
| if (userAccountSet == null) { | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| userAccountSet = loadUserAccountSet(block.getUserAccountSetHash(), | |||
| adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| state.userAccountSet = userAccountSet; | |||
| } | |||
| return userAccountSet; | |||
| } | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| return loadUserAccountSet(block.getUserAccountSetHash(), adminAccount.getPreviousSetting().getCryptoSetting(), | |||
| keyPrefix, exPolicyStorage, versioningStorage, true); | |||
| // // 缓存读; | |||
| // LedgerState state = getLatestState(); | |||
| // userAccountSet = state.userAccountSet; | |||
| // if (userAccountSet == null) { | |||
| // LedgerAdminDataset adminAccount = getAdminInfo(block); | |||
| // userAccountSet = loadUserAccountSet(block.getUserAccountSetHash(), | |||
| // adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| // versioningStorage, true); | |||
| // state.userAccountSet = userAccountSet; | |||
| // } | |||
| // return userAccountSet; | |||
| return latestState.getUserAccountSet(); | |||
| } | |||
| LedgerAdminDataset adminAccount = getAdminInfo(block); | |||
| return createUserAccountSet(block, adminAccount.getSettings().getCryptoSetting()); | |||
| } | |||
| private UserAccountSet createUserAccountSet(LedgerBlock block, CryptoSetting cryptoSetting) { | |||
| return loadUserAccountSet(block.getUserAccountSetHash(), cryptoSetting, keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| } | |||
| @Override | |||
| public DataAccountSet getDataAccountSet(LedgerBlock block) { | |||
| long height = getLatestBlockHeight(); | |||
| DataAccountSet dataAccountSet = null; | |||
| // DataAccountSet dataAccountSet = null; | |||
| if (height == block.getHeight()) { | |||
| // 缓存读; | |||
| LedgerState state = getLatestState(); | |||
| dataAccountSet = state.dataAccountSet; | |||
| if (dataAccountSet == null) { | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| dataAccountSet = loadDataAccountSet(block.getDataAccountSetHash(), | |||
| adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| state.dataAccountSet = dataAccountSet; | |||
| } | |||
| return dataAccountSet; | |||
| // // 缓存读; | |||
| // LedgerState state = getLatestState(); | |||
| // dataAccountSet = state.dataAccountSet; | |||
| // if (dataAccountSet == null) { | |||
| // LedgerAdminDataset adminAccount = getAdminInfo(block); | |||
| // dataAccountSet = loadDataAccountSet(block.getDataAccountSetHash(), | |||
| // adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| // versioningStorage, true); | |||
| // state.dataAccountSet = dataAccountSet; | |||
| // } | |||
| // return dataAccountSet; | |||
| return latestState.getDataAccountSet(); | |||
| } | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| return loadDataAccountSet(block.getDataAccountSetHash(), adminAccount.getPreviousSetting().getCryptoSetting(), | |||
| keyPrefix, exPolicyStorage, versioningStorage, true); | |||
| LedgerAdminDataset adminAccount = getAdminInfo(block); | |||
| return createDataAccountSet(block, adminAccount.getSettings().getCryptoSetting()); | |||
| } | |||
| private DataAccountSet createDataAccountSet(LedgerBlock block, CryptoSetting setting) { | |||
| return loadDataAccountSet(block.getDataAccountSetHash(), setting, keyPrefix, exPolicyStorage, versioningStorage, | |||
| true); | |||
| } | |||
| @Override | |||
| public ContractAccountSet getContractAccountSet(LedgerBlock block) { | |||
| long height = getLatestBlockHeight(); | |||
| ContractAccountSet contractAccountSet = null; | |||
| // ContractAccountSet contractAccountSet = null; | |||
| if (height == block.getHeight()) { | |||
| // 缓存读; | |||
| LedgerState state = getLatestState(); | |||
| contractAccountSet = state.contractAccountSet; | |||
| if (contractAccountSet == null) { | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| contractAccountSet = loadContractAccountSet(block.getContractAccountSetHash(), | |||
| adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| state.contractAccountSet = contractAccountSet; | |||
| } | |||
| return contractAccountSet; | |||
| // // 缓存读; | |||
| // LedgerState state = getLatestState(); | |||
| // contractAccountSet = state.contractAccountSet; | |||
| // if (contractAccountSet == null) { | |||
| // LedgerAdminDataset adminAccount = getAdminInfo(block); | |||
| // contractAccountSet = loadContractAccountSet(block.getContractAccountSetHash(), | |||
| // adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, | |||
| // versioningStorage, true); | |||
| // state.contractAccountSet = contractAccountSet; | |||
| // } | |||
| // return contractAccountSet; | |||
| return latestState.getContractAccountSet(); | |||
| } | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| return loadContractAccountSet(block.getContractAccountSetHash(), | |||
| adminAccount.getPreviousSetting().getCryptoSetting(), keyPrefix, exPolicyStorage, versioningStorage, | |||
| true); | |||
| LedgerAdminDataset adminAccount = getAdminInfo(block); | |||
| return createContractAccountSet(block, adminAccount.getSettings().getCryptoSetting()); | |||
| } | |||
| private ContractAccountSet createContractAccountSet(LedgerBlock block, CryptoSetting cryptoSetting) { | |||
| return loadContractAccountSet(block.getContractAccountSetHash(), cryptoSetting, keyPrefix, exPolicyStorage, | |||
| versioningStorage, true); | |||
| } | |||
| @Override | |||
| public LedgerDataSet getDataSet(LedgerBlock block) { | |||
| public LedgerDataset getDataSet(LedgerBlock block) { | |||
| long height = getLatestBlockHeight(); | |||
| LedgerDataSet ledgerDataSet = null; | |||
| // LedgerDataSet ledgerDataSet = null; | |||
| if (height == block.getHeight()) { | |||
| // 缓存读; | |||
| LedgerState state = getLatestState(); | |||
| ledgerDataSet = state.ledgerDataSet; | |||
| if (ledgerDataSet == null) { | |||
| ledgerDataSet = innerDataSet(block); | |||
| state.ledgerDataSet = ledgerDataSet; | |||
| } | |||
| return ledgerDataSet; | |||
| // // 缓存读; | |||
| // LedgerState state = getLatestState(); | |||
| // ledgerDataSet = state.ledgerDataSet; | |||
| // if (ledgerDataSet == null) { | |||
| // ledgerDataSet = innerDataSet(block); | |||
| // state.ledgerDataSet = ledgerDataSet; | |||
| // } | |||
| // return ledgerDataSet; | |||
| return latestState.getLedgerDataset(); | |||
| } | |||
| // All of existing block is readonly; | |||
| return innerDataSet(block); | |||
| return innerGetLedgerDataset(block); | |||
| } | |||
| private LedgerDataSet innerDataSet(LedgerBlock block) { | |||
| LedgerAdminDataset adminAccount = getAdminAccount(block); | |||
| UserAccountSet userAccountSet = getUserAccountSet(block); | |||
| DataAccountSet dataAccountSet = getDataAccountSet(block); | |||
| ContractAccountSet contractAccountSet = getContractAccountSet(block); | |||
| return new LedgerDataSetImpl(adminAccount, userAccountSet, dataAccountSet, contractAccountSet, true); | |||
| private LedgerDataset innerGetLedgerDataset(LedgerBlock block) { | |||
| LedgerAdminDataset adminDataset = createAdminDataset(block); | |||
| CryptoSetting cryptoSetting = adminDataset.getSettings().getCryptoSetting(); | |||
| UserAccountSet userAccountSet = createUserAccountSet(block, cryptoSetting); | |||
| DataAccountSet dataAccountSet = createDataAccountSet(block, cryptoSetting); | |||
| ContractAccountSet contractAccountSet = createContractAccountSet(block, cryptoSetting); | |||
| return new LedgerDataSetImpl(adminDataset, userAccountSet, dataAccountSet, contractAccountSet, true); | |||
| } | |||
| @Override | |||
| @@ -412,9 +441,8 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| "A new block is in process, cann't create another one until it finish by committing or canceling."); | |||
| } | |||
| LedgerBlock previousBlock = getLatestBlock(); | |||
| LedgerTransactionalEditor editor = LedgerTransactionalEditor.createEditor(previousBlock, | |||
| getAdminInfo().getSettings(), keyPrefix, exPolicyStorage, | |||
| versioningStorage); | |||
| LedgerTransactionalEditor editor = LedgerTransactionalEditor.createEditor(previousBlock, getLatestSettings(), | |||
| keyPrefix, exPolicyStorage, versioningStorage); | |||
| NewBlockCommittingMonitor committingMonitor = new NewBlockCommittingMonitor(editor, this); | |||
| this.nextBlockEditor = committingMonitor; | |||
| return committingMonitor; | |||
| @@ -503,12 +531,12 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| return transactionSet; | |||
| } | |||
| static LedgerDataSetImpl loadDataSet(LedgerDataSnapshot dataSnapshot, String keyPrefix, | |||
| static LedgerDataSetImpl loadDataSet(LedgerDataSnapshot dataSnapshot, CryptoSetting cryptoSetting, String keyPrefix, | |||
| ExPolicyKVStorage ledgerExStorage, VersioningKVStorage ledgerVerStorage, boolean readonly) { | |||
| LedgerAdminDataset adminAccount = new LedgerAdminDataset(dataSnapshot.getAdminAccountHash(), keyPrefix, | |||
| ledgerExStorage, ledgerVerStorage, readonly); | |||
| CryptoSetting cryptoSetting = adminAccount.getPreviousSetting().getCryptoSetting(); | |||
| // CryptoSetting cryptoSetting = adminAccount.getPreviousSetting().getCryptoSetting(); | |||
| UserAccountSet userAccountSet = loadUserAccountSet(dataSnapshot.getUserAccountSetHash(), cryptoSetting, | |||
| keyPrefix, ledgerExStorage, ledgerVerStorage, readonly); | |||
| @@ -597,6 +625,16 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| return editor.getBlockHeight(); | |||
| } | |||
| @Override | |||
| public LedgerDataset getLedgerDataset() { | |||
| return editor.getLedgerDataset(); | |||
| } | |||
| @Override | |||
| public TransactionSet getTransactionSet() { | |||
| return editor.getTransactionSet(); | |||
| } | |||
| @Override | |||
| public LedgerTransactionContext newTransaction(TransactionRequest txRequest) { | |||
| return editor.newTransaction(txRequest); | |||
| @@ -612,7 +650,8 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| try { | |||
| editor.commit(); | |||
| LedgerBlock latestBlock = editor.getCurrentBlock(); | |||
| ledgerRepo.latestState = new LedgerState(latestBlock); | |||
| ledgerRepo.latestState = new LedgerState(latestBlock, editor.getLedgerDataset(), | |||
| editor.getTransactionSet()); | |||
| } finally { | |||
| ledgerRepo.nextBlockEditor = null; | |||
| } | |||
| @@ -639,20 +678,39 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| private final LedgerBlock block; | |||
| private volatile LedgerAdminDataset adminAccount; | |||
| private final TransactionSet transactionSet; | |||
| private final LedgerDataset ledgerDataset; | |||
| public LedgerState(LedgerBlock block, LedgerDataset ledgerDataset, TransactionSet transactionSet) { | |||
| this.block = block; | |||
| this.ledgerDataset = ledgerDataset; | |||
| this.transactionSet = transactionSet; | |||
| } | |||
| private volatile UserAccountSet userAccountSet; | |||
| public LedgerAdminDataset getAdminDataset() { | |||
| return ledgerDataset.getAdminDataset(); | |||
| } | |||
| private volatile DataAccountSet dataAccountSet; | |||
| public LedgerDataset getLedgerDataset() { | |||
| return ledgerDataset; | |||
| } | |||
| private volatile ContractAccountSet contractAccountSet; | |||
| public ContractAccountSet getContractAccountSet() { | |||
| return ledgerDataset.getContractAccountset(); | |||
| } | |||
| private volatile TransactionSet transactionSet; | |||
| public DataAccountSet getDataAccountSet() { | |||
| return ledgerDataset.getDataAccountSet(); | |||
| } | |||
| private volatile LedgerDataSet ledgerDataSet; | |||
| public UserAccountSet getUserAccountSet() { | |||
| return ledgerDataset.getUserAccountSet(); | |||
| } | |||
| public LedgerState(LedgerBlock block) { | |||
| this.block = block; | |||
| public TransactionSet getTransactionSet() { | |||
| return transactionSet; | |||
| } | |||
| } | |||
| @@ -13,11 +13,18 @@ import java.util.List; | |||
| public interface LedgerTransactionContext { | |||
| /** | |||
| * 账本数据; | |||
| * 账本数据集合; | |||
| * | |||
| * @return | |||
| */ | |||
| LedgerDataSet getDataSet(); | |||
| LedgerDataset getDataset(); | |||
| /** | |||
| * 事务集合; | |||
| * | |||
| * @return | |||
| */ | |||
| TransactionSet getTransactionSet(); | |||
| /** | |||
| * 交易请求; | |||
| @@ -1,4 +1,4 @@ | |||
| package com.jd.blockchain.ledger.core.impl; | |||
| package com.jd.blockchain.ledger.core; | |||
| import java.util.List; | |||
| @@ -20,17 +20,14 @@ import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRollbackException; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| import com.jd.blockchain.ledger.core.SettingContext; | |||
| import com.jd.blockchain.ledger.core.TransactionSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerBlockData; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerTransactionData; | |||
| import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | |||
| import com.jd.blockchain.storage.service.ExPolicyKVStorage; | |||
| import com.jd.blockchain.storage.service.VersioningKVStorage; | |||
| import com.jd.blockchain.storage.service.utils.BufferedKVStorage; | |||
| import com.jd.blockchain.transaction.SignatureUtils; | |||
| import com.jd.blockchain.transaction.TxBuilder; | |||
| import com.jd.blockchain.transaction.TxRequestBuilder; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| @@ -70,10 +67,8 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| private BufferedKVStorage baseStorage; | |||
| /** | |||
| * 上一个交易的上下文; | |||
| * 上一个交易产生的账本快照; | |||
| */ | |||
| // private LedgerTransactionContextImpl previousTxCtx; | |||
| private TxSnapshot previousTxSnapshot; | |||
| /** | |||
| @@ -81,6 +76,16 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| */ | |||
| private volatile LedgerTransactionContextImpl currentTxCtx; | |||
| /** | |||
| * 最后提交的账本数据集; | |||
| */ | |||
| private volatile LedgerDataSetImpl latestLedgerDataset; | |||
| /** | |||
| * 最后提交的交易集合; | |||
| */ | |||
| private volatile TransactionSet latestTransactionSet; | |||
| /** | |||
| * @param ledgerHash | |||
| * @param cryptoSetting | |||
| @@ -160,6 +165,10 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| private void commitTxSnapshot(TxSnapshot snapshot) { | |||
| previousTxSnapshot = snapshot; | |||
| latestLedgerDataset = currentTxCtx.getDataset(); | |||
| latestLedgerDataset.setReadonly(); | |||
| latestTransactionSet = currentTxCtx.getTransactionSet(); | |||
| latestTransactionSet.setReadonly(); | |||
| currentTxCtx = null; | |||
| } | |||
| @@ -181,13 +190,23 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| return ledgerHash; | |||
| } | |||
| @Override | |||
| public LedgerDataset getLedgerDataset() { | |||
| return latestLedgerDataset; | |||
| } | |||
| @Override | |||
| public TransactionSet getTransactionSet() { | |||
| return latestTransactionSet; | |||
| } | |||
| /** | |||
| * 检查当前账本是否是指定交易请求的账本; | |||
| * | |||
| * @param txRequest | |||
| * @return | |||
| */ | |||
| private boolean isRequestedLedger(TransactionRequest txRequest) { | |||
| private boolean isRequestMatched(TransactionRequest txRequest) { | |||
| HashDigest reqLedgerHash = txRequest.getTransactionContent().getLedgerHash(); | |||
| if (ledgerHash == reqLedgerHash) { | |||
| return true; | |||
| @@ -226,7 +245,8 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| @Override | |||
| public synchronized LedgerTransactionContext newTransaction(TransactionRequest txRequest) { | |||
| if (SettingContext.txSettings().verifyLedger() && !isRequestedLedger(txRequest)) { | |||
| // if (SettingContext.txSettings().verifyLedger() && !isRequestMatched(txRequest)) { | |||
| if (!isRequestMatched(txRequest)) { | |||
| throw new IllegalTransactionException( | |||
| "Transaction request is dispatched to a wrong ledger! --[TxHash=" | |||
| + txRequest.getTransactionContent().getHash() + "]!", | |||
| @@ -234,7 +254,8 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| } | |||
| // TODO: 把验签和创建交易并行化; | |||
| if (SettingContext.txSettings().verifySignature() && !verifyTxContent(txRequest)) { | |||
| // if (SettingContext.txSettings().verifySignature() && !verifyTxContent(txRequest)) { | |||
| if (!verifyTxContent(txRequest)) { | |||
| // 抛弃哈希和签名校验失败的交易请求; | |||
| throw new IllegalTransactionException( | |||
| "Wrong transaction signature! --[TxHash=" + txRequest.getTransactionContent().getHash() + "]!", | |||
| @@ -262,18 +283,18 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| GenesisSnapshot snpht = (GenesisSnapshot) startingPoint; | |||
| txDataset = LedgerRepositoryImpl.newDataSet(snpht.initSetting, ledgerKeyPrefix, txBufferedStorage, | |||
| txBufferedStorage); | |||
| txset = LedgerRepositoryImpl.newTransactionSet(txDataset.getAdminAccount().getSettings(), | |||
| txset = LedgerRepositoryImpl.newTransactionSet(txDataset.getAdminDataset().getSettings(), | |||
| ledgerKeyPrefix, txBufferedStorage, txBufferedStorage); | |||
| } else if (startingPoint instanceof TxSnapshot) { | |||
| // 新的区块; | |||
| // TxSnapshot; reload dataset and txset; | |||
| TxSnapshot snpht = (TxSnapshot) startingPoint; | |||
| // load dataset; | |||
| txDataset = LedgerRepositoryImpl.loadDataSet(snpht.dataSnapshot, ledgerKeyPrefix, txBufferedStorage, | |||
| txBufferedStorage, false); | |||
| txDataset = LedgerRepositoryImpl.loadDataSet(snpht.dataSnapshot, cryptoSetting, ledgerKeyPrefix, | |||
| txBufferedStorage, txBufferedStorage, false); | |||
| // load txset; | |||
| txset = LedgerRepositoryImpl.loadTransactionSet(snpht.txsetHash, this.cryptoSetting, ledgerKeyPrefix, | |||
| txset = LedgerRepositoryImpl.loadTransactionSet(snpht.txsetHash, cryptoSetting, ledgerKeyPrefix, | |||
| txBufferedStorage, txBufferedStorage, false); | |||
| } else { | |||
| // Unreachable; | |||
| @@ -283,11 +304,11 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| } else { | |||
| // Reuse previous object to optimize performance; | |||
| // load dataset; | |||
| txDataset = LedgerRepositoryImpl.loadDataSet(previousTxSnapshot.dataSnapshot, ledgerKeyPrefix, | |||
| txBufferedStorage, txBufferedStorage, false); | |||
| txDataset = LedgerRepositoryImpl.loadDataSet(previousTxSnapshot.dataSnapshot, cryptoSetting, | |||
| ledgerKeyPrefix, txBufferedStorage, txBufferedStorage, false); | |||
| // load txset; | |||
| txset = LedgerRepositoryImpl.loadTransactionSet(previousTxSnapshot.txsetHash, this.cryptoSetting, | |||
| txset = LedgerRepositoryImpl.loadTransactionSet(previousTxSnapshot.txsetHash, cryptoSetting, | |||
| ledgerKeyPrefix, txBufferedStorage, txBufferedStorage, false); | |||
| } | |||
| @@ -476,28 +497,6 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| } | |||
| // /** | |||
| // * 账本的数据上下文; | |||
| // * | |||
| // * @author huanghaiquan | |||
| // * | |||
| // */ | |||
| // private static class LedgerDataContext { | |||
| // | |||
| // protected LedgerDataSetImpl dataset; | |||
| // | |||
| // protected TransactionSet txset; | |||
| // | |||
| // protected BufferedKVStorage storage; | |||
| // | |||
| // public LedgerDataContext(LedgerDataSetImpl dataset, TransactionSet txset, BufferedKVStorage storage) { | |||
| // this.dataset = dataset; | |||
| // this.txset = txset; | |||
| // this.storage = storage; | |||
| // } | |||
| // | |||
| // } | |||
| /** | |||
| * 交易的上下文; | |||
| * | |||
| @@ -534,10 +533,15 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| } | |||
| @Override | |||
| public LedgerDataSet getDataSet() { | |||
| public LedgerDataSetImpl getDataset() { | |||
| return dataset; | |||
| } | |||
| @Override | |||
| public TransactionSet getTransactionSet() { | |||
| return txset; | |||
| } | |||
| @Override | |||
| public TransactionRequest getTransactionRequest() { | |||
| return txRequest; | |||
| @@ -620,8 +624,8 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| private TransactionStagedSnapshot takeDataSnapshot() { | |||
| TransactionStagedSnapshot txDataSnapshot = new TransactionStagedSnapshot(); | |||
| txDataSnapshot.setAdminAccountHash(dataset.getAdminAccount().getHash()); | |||
| txDataSnapshot.setContractAccountSetHash(dataset.getContractAccountSet().getRootHash()); | |||
| txDataSnapshot.setAdminAccountHash(dataset.getAdminDataset().getHash()); | |||
| txDataSnapshot.setContractAccountSetHash(dataset.getContractAccountset().getRootHash()); | |||
| txDataSnapshot.setDataAccountSetHash(dataset.getDataAccountSet().getRootHash()); | |||
| txDataSnapshot.setUserAccountSetHash(dataset.getUserAccountSet().getRootHash()); | |||
| return txDataSnapshot; | |||
| @@ -124,6 +124,10 @@ public class MerkleDataSet implements Transactional, MerkleProvable { | |||
| return readonly; | |||
| } | |||
| void setReadonly() { | |||
| this.readonly = true; | |||
| } | |||
| public long getDataCount() { | |||
| return merkleTree.getDataCount(); | |||
| } | |||
| @@ -30,8 +30,8 @@ public interface OperationHandle { | |||
| * | |||
| * @return 操作执行结果 | |||
| */ | |||
| BytesValue process(Operation op, LedgerDataSet newBlockDataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService); | |||
| BytesValue process(Operation op, LedgerDataset newBlockDataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService); | |||
| // /** | |||
| // * 异步解析和执行操作; | |||
| @@ -153,6 +153,10 @@ public class TransactionSet implements Transactional, MerkleProvable { | |||
| public boolean isReadonly() { | |||
| return txSet.isReadonly(); | |||
| } | |||
| void setReadonly() { | |||
| txSet.setReadonly(); | |||
| } | |||
| @Override | |||
| public boolean isUpdated() { | |||
| @@ -46,6 +46,10 @@ public class UserAccountSet implements Transactional, MerkleProvable { | |||
| public boolean isReadonly() { | |||
| return accountSet.isReadonly(); | |||
| } | |||
| void setReadonly() { | |||
| accountSet.setReadonly(); | |||
| } | |||
| @Override | |||
| public HashDigest getRootHash() { | |||
| @@ -14,6 +14,8 @@ import com.jd.blockchain.ledger.core.LedgerConsts; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerManage; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.LedgerRepositoryImpl; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionalEditor; | |||
| import com.jd.blockchain.storage.service.ExPolicyKVStorage; | |||
| import com.jd.blockchain.storage.service.KVStorageService; | |||
| import com.jd.blockchain.storage.service.VersioningKVStorage; | |||
| @@ -69,7 +71,7 @@ public class LedgerManager implements LedgerManage { | |||
| ledgerVersioningStorage); | |||
| // 校验 crypto service provider ; | |||
| CryptoSetting cryptoSetting = ledgerRepo.getAdminAccount().getSettings().getCryptoSetting(); | |||
| CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting(); | |||
| checkCryptoSetting(cryptoSetting, ledgerHash); | |||
| // 创建账本上下文; | |||
| @@ -44,7 +44,7 @@ public class LedgerQueryService implements BlockchainQueryService { | |||
| public LedgerAdminInfo getLedgerAdminInfo(HashDigest ledgerHash) { | |||
| LedgerRepository ledger = ledgerService.getLedger(ledgerHash); | |||
| LedgerBlock block = ledger.getLatestBlock(); | |||
| LedgerAdminInfo administration = ledger.getAdminAccount(block); | |||
| LedgerAdminInfo administration = ledger.getAdminInfo(block); | |||
| return administration; | |||
| } | |||
| @@ -1,9 +0,0 @@ | |||
| package com.jd.blockchain.ledger.core.impl; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| import com.jd.blockchain.storage.service.utils.BufferedKVStorage; | |||
| @@ -23,7 +23,7 @@ import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.TransactionRollbackException; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.UserDoesNotExistException; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| @@ -43,7 +43,7 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| private LedgerEditor newBlockEditor; | |||
| private LedgerDataSet previousBlockDataset; | |||
| private LedgerDataset previousBlockDataset; | |||
| private OperationHandleRegisteration opHandles; | |||
| @@ -61,7 +61,7 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| * @param previousBlockDataset 新区块的前一个区块的数据集;即未提交新区块之前的经过共识的账本最新数据集; | |||
| * @param opHandles 操作处理对象注册表; | |||
| */ | |||
| public TransactionBatchProcessor(LedgerEditor newBlockEditor, LedgerDataSet previousBlockDataset, | |||
| public TransactionBatchProcessor(LedgerEditor newBlockEditor, LedgerDataset previousBlockDataset, | |||
| OperationHandleRegisteration opHandles, LedgerService ledgerService) { | |||
| this.newBlockEditor = newBlockEditor; | |||
| this.previousBlockDataset = previousBlockDataset; | |||
| @@ -135,7 +135,7 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| TransactionState result; | |||
| List<OperationResult> operationResults = new ArrayList<>(); | |||
| try { | |||
| LedgerDataSet dataset = txCtx.getDataSet(); | |||
| LedgerDataset dataset = txCtx.getDataset(); | |||
| TransactionRequestContext reqCtx = new TransactionRequestContextImpl(request); | |||
| // TODO: 验证签名者的有效性; | |||
| for (Bytes edpAddr : reqCtx.getEndpoints()) { | |||
| @@ -7,7 +7,7 @@ import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| @@ -44,7 +44,7 @@ public class TransactionEngineImpl implements TransactionEngine { | |||
| LedgerBlock ledgerBlock = ledgerRepo.getLatestBlock(); | |||
| LedgerEditor newBlockEditor = ledgerRepo.createNextBlock(); | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerBlock); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(ledgerBlock); | |||
| batch = new InnerTransactionBatchProcessor(ledgerHash, newBlockEditor, previousBlockDataset, opHdlRegs, | |||
| ledgerService, ledgerBlock.getHeight()); | |||
| batchs.put(ledgerHash, batch); | |||
| @@ -79,7 +79,7 @@ public class TransactionEngineImpl implements TransactionEngine { | |||
| * 操作处理对象注册表; | |||
| */ | |||
| public InnerTransactionBatchProcessor(HashDigest ledgerHash, LedgerEditor newBlockEditor, | |||
| LedgerDataSet previousBlockDataset, OperationHandleRegisteration opHandles, | |||
| LedgerDataset previousBlockDataset, OperationHandleRegisteration opHandles, | |||
| LedgerService ledgerService, long blockHeight) { | |||
| super(newBlockEditor, previousBlockDataset, opHandles, ledgerService); | |||
| this.ledgerHash = ledgerHash; | |||
| @@ -10,7 +10,7 @@ import com.jd.blockchain.ledger.LedgerException; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.core.ContractAccount; | |||
| import com.jd.blockchain.ledger.core.ContractAccountSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| @@ -26,12 +26,12 @@ public abstract class AbtractContractEventHandle implements OperationHandle { | |||
| } | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService) { | |||
| public BytesValue process(Operation op, LedgerDataset dataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService) { | |||
| ContractEventSendOperation contractOP = (ContractEventSendOperation) op; | |||
| // 先从账本校验合约的有效性; | |||
| // 注意:必须在前一个区块的数据集中进行校验,因为那是经过共识的数据;从当前新区块链数据集校验则会带来攻击风险:未经共识的合约得到执行; | |||
| ContractAccountSet contractSet = previousBlockDataset.getContractAccountSet(); | |||
| ContractAccountSet contractSet = previousBlockDataset.getContractAccountset(); | |||
| if (!contractSet.contains(contractOP.getContractAddress())) { | |||
| throw new LedgerException(String.format("Contract was not registered! --[ContractAddress=%s]", | |||
| contractOP.getContractAddress())); | |||
| @@ -5,7 +5,7 @@ import org.springframework.stereotype.Service; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.ContractCodeDeployOperation; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| @@ -15,14 +15,14 @@ import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
| public class ContractCodeDeployOperationHandle implements OperationHandle { | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| public BytesValue process(Operation op, LedgerDataset dataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| ContractCodeDeployOperation contractOP = (ContractCodeDeployOperation) op; | |||
| // TODO: 校验合约代码的正确性; | |||
| // TODO: 请求者应该提供合约账户的公钥签名,已确定注册的地址的唯一性; | |||
| dataset.getContractAccountSet().deploy(contractOP.getContractID().getAddress(), | |||
| dataset.getContractAccountset().deploy(contractOP.getContractID().getAddress(), | |||
| contractOP.getContractID().getPubKey(), contractOP.getAddressSignature(), contractOP.getChainCode()); | |||
| return null; | |||
| @@ -9,7 +9,7 @@ import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
| import com.jd.blockchain.ledger.DataAccountKVSetOperation.KVWriteEntry; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.core.DataAccount; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| @@ -23,8 +23,8 @@ public class DataAccountKVSetOperationHandle implements OperationHandle { | |||
| } | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| public BytesValue process(Operation op, LedgerDataset dataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| DataAccountKVSetOperation kvWriteOp = (DataAccountKVSetOperation) op; | |||
| DataAccount account = dataset.getDataAccountSet().getDataAccount(kvWriteOp.getAccountAddress()); | |||
| if (account == null) { | |||
| @@ -6,7 +6,7 @@ import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| @@ -16,8 +16,8 @@ import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
| public class DataAccountRegisterOperationHandle implements OperationHandle { | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| public BytesValue process(Operation op, LedgerDataset dataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| DataAccountRegisterOperation dataAccountRegOp = (DataAccountRegisterOperation) op; | |||
| BlockchainIdentity bid = dataAccountRegOp.getAccountID(); | |||
| @@ -4,7 +4,7 @@ import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| @@ -15,8 +15,8 @@ import com.jd.blockchain.utils.Bytes; | |||
| public class UserRegisterOperationHandle implements OperationHandle { | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| public BytesValue process(Operation op, LedgerDataset dataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| UserRegisterOperation userRegOp = (UserRegisterOperation) op; | |||
| @@ -7,7 +7,6 @@ import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.*; | |||
| import com.jd.blockchain.ledger.core.impl.DefaultOperationHandleRegisteration; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerTransactionalEditor; | |||
| import com.jd.blockchain.ledger.core.impl.TransactionBatchProcessor; | |||
| import com.jd.blockchain.service.TransactionBatchResultHandle; | |||
| import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||
| @@ -72,7 +71,7 @@ public class ContractInvokingTest { | |||
| // 创建新区块的交易处理器; | |||
| LedgerBlock preBlock = ledgerRepo.getLatestBlock(); | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(preBlock); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(preBlock); | |||
| // 加载合约 | |||
| LedgerEditor newBlockEditor = ledgerRepo.createNextBlock(); | |||
| @@ -127,7 +126,7 @@ public class ContractInvokingTest { | |||
| BlockchainKeypair contractKey) { | |||
| // 创建新区块的交易处理器; | |||
| LedgerBlock preBlock = ledgerRepo.getLatestBlock(); | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(preBlock); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(preBlock); | |||
| // 加载合约 | |||
| LedgerEditor newBlockEditor = ledgerRepo.createNextBlock(); | |||
| @@ -160,7 +159,7 @@ public class ContractInvokingTest { | |||
| TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest(partiKeys); | |||
| LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq); | |||
| LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | |||
| LedgerDataset ldgDS = genisisTxCtx.getDataset(); | |||
| for (int i = 0; i < partiKeys.length; i++) { | |||
| UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), | |||
| @@ -27,11 +27,11 @@ import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.DataAccount; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionalEditor; | |||
| import com.jd.blockchain.ledger.core.UserAccount; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerTransactionalEditor; | |||
| import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||
| import com.jd.blockchain.transaction.ConsensusParticipantData; | |||
| import com.jd.blockchain.transaction.LedgerInitSettingData; | |||
| @@ -95,7 +95,7 @@ public class LedgerEditorTest { | |||
| public void testWriteDataAccoutKvOp() { | |||
| LedgerEditor ldgEdt = createLedgerInitEditor(); | |||
| LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants); | |||
| LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | |||
| LedgerDataset ldgDS = genisisTxCtx.getDataset(); | |||
| AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | |||
| BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); | |||
| @@ -128,7 +128,7 @@ public class LedgerEditorTest { | |||
| public void testGennesisBlockCreation() { | |||
| LedgerEditor ldgEdt = createLedgerInitEditor(); | |||
| LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants); | |||
| LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | |||
| LedgerDataset ldgDS = genisisTxCtx.getDataset(); | |||
| AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | |||
| BlockchainKeypair userKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); | |||
| @@ -35,7 +35,7 @@ import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.ContractAccountSet; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.DataAccountSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| @@ -94,7 +94,7 @@ public class LedgerManagerTest { | |||
| // 记录交易,注册用户; | |||
| LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | |||
| LedgerDataSet ldgDS = txCtx.getDataSet(); | |||
| LedgerDataset ldgDS = txCtx.getDataset(); | |||
| BlockchainKeypair userKP = BlockchainKeyGenerator.getInstance().generate(); | |||
| UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey()); | |||
| @@ -121,6 +121,9 @@ public class LedgerManagerTest { | |||
| // 提交数据,写入存储; | |||
| ldgEdt.commit(); | |||
| assertNull(genesisBlock.getLedgerHash()); | |||
| assertNotNull(genesisBlock.getHash()); | |||
| // 重新加载并校验结果; | |||
| LedgerManager reloadLedgerManager = new LedgerManager(); | |||
| @@ -145,7 +148,7 @@ public class LedgerManagerTest { | |||
| TransactionRequest txRequest = txReqBuilder.buildRequest(); | |||
| LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest); | |||
| txCtx1.getDataSet().getDataAccountSet().register(dataKey.getAddress(), dataKey.getPubKey(), null); | |||
| txCtx1.getDataset().getDataAccountSet().register(dataKey.getAddress(), dataKey.getPubKey(), null); | |||
| txCtx1.commit(TransactionState.SUCCESS); | |||
| LedgerBlock block1 = editor1.prepare(); | |||
| @@ -26,14 +26,14 @@ import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.DataAccount; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionContext; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionalEditor; | |||
| import com.jd.blockchain.ledger.core.UserAccount; | |||
| import com.jd.blockchain.ledger.core.impl.DefaultOperationHandleRegisteration; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerTransactionalEditor; | |||
| import com.jd.blockchain.ledger.core.impl.OperationHandleRegisteration; | |||
| import com.jd.blockchain.ledger.core.impl.TransactionBatchProcessor; | |||
| import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||
| @@ -75,7 +75,7 @@ public class TransactionBatchProcessorTest { | |||
| LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE); | |||
| // 验证参与方账户的存在; | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| UserAccount user0 = previousBlockDataset.getUserAccountSet().getUser(parti0.getAddress()); | |||
| assertNotNull(user0); | |||
| boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress()); | |||
| @@ -120,7 +120,7 @@ public class TransactionBatchProcessorTest { | |||
| LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE); | |||
| // 验证参与方账户的存在; | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| UserAccount user0 = previousBlockDataset.getUserAccountSet().getUser(parti0.getAddress()); | |||
| assertNotNull(user0); | |||
| boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress()); | |||
| @@ -158,7 +158,7 @@ public class TransactionBatchProcessorTest { | |||
| assertEquals(newBlock.getHash(), latestBlock.getHash()); | |||
| assertEquals(1, newBlock.getHeight()); | |||
| LedgerDataSet ledgerDS = ledgerRepo.getDataSet(latestBlock); | |||
| LedgerDataset ledgerDS = ledgerRepo.getDataSet(latestBlock); | |||
| boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress()); | |||
| boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress()); | |||
| assertTrue(existUser1); | |||
| @@ -177,7 +177,7 @@ public class TransactionBatchProcessorTest { | |||
| LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE); | |||
| // 验证参与方账户的存在; | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| UserAccount user0 = previousBlockDataset.getUserAccountSet().getUser(parti0.getAddress()); | |||
| assertNotNull(user0); | |||
| boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress()); | |||
| @@ -235,7 +235,7 @@ public class TransactionBatchProcessorTest { | |||
| assertNotNull(tx3); | |||
| assertEquals(TransactionState.SUCCESS, tx3.getExecutionState()); | |||
| LedgerDataSet ledgerDS = ledgerRepo.getDataSet(latestBlock); | |||
| LedgerDataset ledgerDS = ledgerRepo.getDataSet(latestBlock); | |||
| boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress()); | |||
| boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress()); | |||
| boolean existUser3 = ledgerDS.getUserAccountSet().contains(userKeypair3.getAddress()); | |||
| @@ -256,7 +256,7 @@ public class TransactionBatchProcessorTest { | |||
| LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE); | |||
| // 验证参与方账户的存在; | |||
| LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| LedgerDataset previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock()); | |||
| UserAccount user0 = previousBlockDataset.getUserAccountSet().getUser(parti0.getAddress()); | |||
| assertNotNull(user0); | |||
| boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress()); | |||
| @@ -378,7 +378,7 @@ public class TransactionBatchProcessorTest { | |||
| TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest(partiKeys); | |||
| LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq); | |||
| LedgerDataSet ldgDS = genisisTxCtx.getDataSet(); | |||
| LedgerDataset ldgDS = genisisTxCtx.getDataset(); | |||
| for (int i = 0; i < partiKeys.length; i++) { | |||
| UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), | |||
| @@ -1,29 +1,17 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| @DataContract(code = DataCodes.LEDGER_ADMIN_INFO, name = "LEDGER-ADMIN-INFO") | |||
| public interface LedgerAdminInfo { | |||
| @DataField(order = 1, refContract = true) | |||
| LedgerMetadata_V2 getMetadata(); | |||
| @DataField(order = 2, refContract = true) | |||
| LedgerSettings getSettings(); | |||
| @DataField(order = 3, primitiveType = PrimitiveType.INT64) | |||
| long getParticipantCount(); | |||
| @DataField(order = 4, refContract = true, list = true) | |||
| ParticipantNode[] getParticipants(); | |||
| @DataField(order = 5, refContract = true) | |||
| UserRoleSettings getUserRoles(); | |||
| @DataField(order = 6, refContract = true) | |||
| RolePrivilegeSettings getRolePrivileges(); | |||
| } | |||
| @@ -226,7 +226,7 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag | |||
| LedgerRepository ledgerRepository = ledgerManager.register(ledgerHash, dbConnNew.getStorageService()); | |||
| // load provider; | |||
| LedgerAdminInfo ledgerAdminAccount = ledgerRepository.getAdminAccount(); | |||
| LedgerAdminInfo ledgerAdminAccount = ledgerRepository.getAdminInfo(); | |||
| String consensusProvider = ledgerAdminAccount.getSettings().getConsensusProvider(); | |||
| ConsensusProvider provider = ConsensusProviders.getProvider(consensusProvider); | |||
| // find current node; | |||
| @@ -1,7 +1,19 @@ | |||
| package com.jd.blockchain.sdk.proxy; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.AccountHeader; | |||
| import com.jd.blockchain.ledger.ContractInfo; | |||
| import com.jd.blockchain.ledger.KVDataEntry; | |||
| import com.jd.blockchain.ledger.KVInfoVO; | |||
| import com.jd.blockchain.ledger.LedgerAdminInfo; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInfo; | |||
| import com.jd.blockchain.ledger.LedgerMetadata; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.TransactionTemplate; | |||
| import com.jd.blockchain.ledger.UserInfo; | |||
| import com.jd.blockchain.sdk.BlockchainEventHandle; | |||
| import com.jd.blockchain.sdk.BlockchainEventListener; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| @@ -30,7 +30,7 @@ import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.impl.DefaultOperationHandleRegisteration; | |||
| @@ -278,7 +278,7 @@ public class LedgerPerformanceTest { | |||
| ConsoleUtils.info("\r\n\r\n================= 准备测试交易 [执行合约] ================="); | |||
| LedgerBlock latestBlock = ledger.getLatestBlock(); | |||
| LedgerDataSet previousDataSet = ledger.getDataSet(latestBlock); | |||
| LedgerDataset previousDataSet = ledger.getDataSet(latestBlock); | |||
| LedgerEditor newEditor = ledger.createNextBlock(); | |||
| TransactionBatchProcessor txProc = new TransactionBatchProcessor(newEditor, previousDataSet, opHandler, | |||
| ledgerManager); | |||
| @@ -311,7 +311,7 @@ public class LedgerPerformanceTest { | |||
| long batchStartTs = System.currentTimeMillis(); | |||
| for (int i = 0; i < batchCount; i++) { | |||
| LedgerBlock latestBlock = ledger.getLatestBlock(); | |||
| LedgerDataSet previousDataSet = ledger.getDataSet(latestBlock); | |||
| LedgerDataset previousDataSet = ledger.getDataSet(latestBlock); | |||
| if (statistic) { | |||
| ConsoleUtils.info("------ 开始执行交易, 即将生成区块[高度:%s] ------", (latestBlock.getHeight() + 1)); | |||
| } | |||
| @@ -24,7 +24,7 @@ import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.impl.DefaultOperationHandleRegisteration; | |||
| @@ -76,7 +76,7 @@ public class LedgerBlockGeneratingTest { | |||
| LedgerBlock latestBlock = ledger.getLatestBlock(); | |||
| assertEquals(height + i, latestBlock.getHeight()); | |||
| LedgerDataSet previousDataSet = ledger.getDataSet(latestBlock); | |||
| LedgerDataset previousDataSet = ledger.getDataSet(latestBlock); | |||
| ConsoleUtils.info("------ 开始执行交易, 即将生成区块[%s] ------", (latestBlock.getHeight() + 1)); | |||
| long startTs = System.currentTimeMillis(); | |||
| @@ -423,7 +423,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| // TODO:暂时只支持注册用户的初始化操作; | |||
| for (int i = 1; i < ops.length; i++) { | |||
| UserRegisterOperation userRegOP = (UserRegisterOperation) ops[i]; | |||
| txCtx.getDataSet().getUserAccountSet().register(userRegOP.getUserID().getAddress(), | |||
| txCtx.getDataset().getUserAccountSet().register(userRegOP.getUserID().getAddress(), | |||
| userRegOP.getUserID().getPubKey()); | |||
| } | |||
| @@ -286,7 +286,7 @@ public class MockerLedgerInitializer implements LedgerInitProcess, LedgerInitCon | |||
| // TODO:暂时只支持注册用户的初始化操作; | |||
| for (int i = 1; i < ops.length; i++) { | |||
| UserRegisterOperation userRegOP = (UserRegisterOperation) ops[i]; | |||
| txCtx.getDataSet().getUserAccountSet().register(userRegOP.getUserID().getAddress(), | |||
| txCtx.getDataset().getUserAccountSet().register(userRegOP.getUserID().getAddress(), | |||
| userRegOP.getUserID().getPubKey()); | |||
| } | |||
| @@ -51,7 +51,7 @@ import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.UserInfo; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| @@ -122,7 +122,7 @@ public class MockerNodeContext implements BlockchainQueryService { | |||
| DataContractRegistry.register(ClientIdentifications.class); | |||
| DataContractRegistry.register(ClientIdentification.class); | |||
| DataContractRegistry.register(LedgerAdminInfo.class); | |||
| // DataContractRegistry.register(LedgerAdminInfo.class); | |||
| ByteArrayObjectUtil.init(); | |||
| } | |||
| @@ -442,7 +442,7 @@ public class MockerNodeContext implements BlockchainQueryService { | |||
| public OperationResult[] txProcess(TransactionRequest txRequest) { | |||
| LedgerEditor newEditor = ledgerRepository.createNextBlock(); | |||
| LedgerBlock latestBlock = ledgerRepository.getLatestBlock(); | |||
| LedgerDataSet previousDataSet = ledgerRepository.getDataSet(latestBlock); | |||
| LedgerDataset previousDataSet = ledgerRepository.getDataSet(latestBlock); | |||
| TransactionBatchProcessor txProc = new TransactionBatchProcessor(newEditor, previousDataSet, opHandler, | |||
| ledgerManager); | |||
| TransactionResponse txResp = txProc.schedule(txRequest); | |||
| @@ -10,7 +10,7 @@ import com.jd.blockchain.contract.EventProcessingAware; | |||
| import com.jd.blockchain.contract.LedgerContext; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| @@ -29,8 +29,8 @@ public class MockerContractExeHandle implements OperationHandle { | |||
| private HashDigest ledgerHash; | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService) { | |||
| public BytesValue process(Operation op, LedgerDataset dataset, TransactionRequestContext requestContext, | |||
| LedgerDataset previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService) { | |||
| ContractEventSendOperation contractOP = (ContractEventSendOperation) op; | |||
| HashDigest txHash = requestContext.getRequest().getTransactionContent().getHash(); | |||