@@ -30,9 +30,9 @@ public enum PrimitiveType { | |||
INT64((byte) (DataType.NUMERIC | 0x04)), | |||
/** | |||
* 日期时间; | |||
* 时间戳; | |||
*/ | |||
DATETIME((byte) (DataType.NUMERIC | 0x08)), | |||
TIMESTAMP((byte) (DataType.NUMERIC | 0x08)), | |||
/** | |||
* 文本数据; | |||
@@ -19,7 +19,7 @@ public interface AccountAccessPolicy { | |||
* @param account | |||
* @return Return true if it satisfies this policy, or false if it doesn't; | |||
*/ | |||
boolean checkCommitting(AccountHeader account); | |||
boolean checkDataWriting(AccountHeader account); | |||
boolean checkRegistering(Bytes address, PubKey pubKey); | |||
@@ -160,10 +160,8 @@ public class AccountSet implements Transactional, MerkleProvable { | |||
* | |||
* 只有最新版本的账户才能可写的,其它都是只读; | |||
* | |||
* @param address | |||
* 账户地址; | |||
* @param version | |||
* 账户版本;如果指定为 -1,则返回最新版本; | |||
* @param address 账户地址; | |||
* @param version 账户版本;如果指定为 -1,则返回最新版本; | |||
* @return | |||
*/ | |||
public BaseAccount getAccount(Bytes address, long version) { | |||
@@ -226,10 +224,8 @@ public class AccountSet implements Transactional, MerkleProvable { | |||
* | |||
* 如果指定的地址和公钥不匹配,则会引发 {@link LedgerException} 异常; | |||
* | |||
* @param address | |||
* 区块链地址; | |||
* @param pubKey | |||
* 公钥; | |||
* @param address 区块链地址; | |||
* @param pubKey 公钥; | |||
* @return 注册成功的账户对象; | |||
*/ | |||
public BaseAccount register(Bytes address, PubKey pubKey) { | |||
@@ -283,15 +279,14 @@ public class AccountSet implements Transactional, MerkleProvable { | |||
private VersioningAccount createInstance(Bytes address, PubKey pubKey, CryptoSetting cryptoSetting, | |||
String keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, long version) { | |||
return new VersioningAccount(address, pubKey, cryptoSetting, keyPrefix, exStorage, verStorage, accessPolicy, | |||
version); | |||
return new VersioningAccount(address, pubKey, cryptoSetting, keyPrefix, exStorage, verStorage, version); | |||
} | |||
private VersioningAccount deserialize(byte[] bytes, CryptoSetting cryptoSetting, String keyPrefix, | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly, long version) { | |||
AccountHeader accInfo = BinaryProtocol.decode(bytes); | |||
return new VersioningAccount(accInfo.getAddress(), accInfo.getPubKey(), accInfo.getRootHash(), cryptoSetting, | |||
keyPrefix, exStorage, verStorage, readonly, accessPolicy, version); | |||
keyPrefix, exStorage, verStorage, readonly, version); | |||
} | |||
private byte[] serialize(AccountHeader account) { | |||
@@ -388,15 +383,14 @@ public class AccountSet implements Transactional, MerkleProvable { | |||
public VersioningAccount(Bytes address, PubKey pubKey, HashDigest rootHash, CryptoSetting cryptoSetting, | |||
String keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly, | |||
AccountAccessPolicy accessPolicy, long version) { | |||
super(address, pubKey, rootHash, cryptoSetting, keyPrefix, exStorage, verStorage, readonly, accessPolicy); | |||
long version) { | |||
super(address, pubKey, rootHash, cryptoSetting, keyPrefix, exStorage, verStorage, readonly); | |||
this.version = version; | |||
} | |||
public VersioningAccount(Bytes address, PubKey pubKey, CryptoSetting cryptoSetting, String keyPrefix, | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, AccountAccessPolicy accessPolicy, | |||
long version) { | |||
super(address, pubKey, cryptoSetting, keyPrefix, exStorage, verStorage, accessPolicy); | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, long version) { | |||
super(address, pubKey, cryptoSetting, keyPrefix, exStorage, verStorage); | |||
this.version = version; | |||
} | |||
@@ -23,8 +23,6 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
protected MerkleDataSet dataset; | |||
private AccountAccessPolicy accessPolicy; | |||
/** | |||
* Create a new Account with the specified address and pubkey; <br> | |||
* | |||
@@ -38,8 +36,8 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
* @param pubKey | |||
*/ | |||
public BaseAccount(Bytes address, PubKey pubKey, CryptoSetting cryptoSetting, String keyPrefix, | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, AccountAccessPolicy accessPolicy) { | |||
this(address, pubKey, null, cryptoSetting, keyPrefix, exStorage, verStorage, false, accessPolicy); | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage) { | |||
this(address, pubKey, null, cryptoSetting, keyPrefix, exStorage, verStorage, false); | |||
} | |||
/** | |||
@@ -58,8 +56,8 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
* @param accessPolicy | |||
*/ | |||
public BaseAccount(BlockchainIdentity bcid, CryptoSetting cryptoSetting, String keyPrefix, | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, AccountAccessPolicy accessPolicy) { | |||
this(bcid, null, cryptoSetting, keyPrefix, exStorage, verStorage, false, accessPolicy); | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage) { | |||
this(bcid, null, cryptoSetting, keyPrefix, exStorage, verStorage, false); | |||
} | |||
/** | |||
@@ -69,9 +67,8 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
* | |||
* @param address | |||
* @param pubKey | |||
* @param dataRootHash | |||
* merkle root hash of account's data; if null be set, create a new | |||
* empty merkle dataset; | |||
* @param dataRootHash merkle root hash of account's data; if null be set, | |||
* create a new empty merkle dataset; | |||
* @param cryptoSetting | |||
* @param exStorage | |||
* @param verStorage | |||
@@ -79,18 +76,15 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
* @param accessPolicy | |||
*/ | |||
public BaseAccount(Bytes address, PubKey pubKey, HashDigest dataRootHash, CryptoSetting cryptoSetting, | |||
String keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly, | |||
AccountAccessPolicy accessPolicy) { | |||
String keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly) { | |||
this(new BlockchainIdentityData(address, pubKey), dataRootHash, cryptoSetting, keyPrefix, exStorage, verStorage, | |||
readonly, accessPolicy); | |||
readonly); | |||
} | |||
public BaseAccount(BlockchainIdentity bcid, HashDigest dataRootHash, CryptoSetting cryptoSetting, String keyPrefix, | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly, | |||
AccountAccessPolicy accessPolicy) { | |||
ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly) { | |||
this.bcid = bcid; | |||
this.dataset = new MerkleDataSet(dataRootHash, cryptoSetting, keyPrefix, exStorage, verStorage, readonly); | |||
this.accessPolicy = accessPolicy; | |||
} | |||
/* | |||
@@ -151,12 +145,9 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
* If updating is performed, the version of the key increase by 1. <br> | |||
* If creating is performed, the version of the key initialize by 0. <br> | |||
* | |||
* @param key | |||
* The key of data; | |||
* @param value | |||
* The value of data; | |||
* @param version | |||
* The expected version of the key. | |||
* @param key The key of data; | |||
* @param value The value of data; | |||
* @param version The expected version of the key. | |||
* @return The new version of the key. <br> | |||
* If the key is new created success, then return 0; <br> | |||
* If the key is updated success, then return the new version;<br> | |||
@@ -164,7 +155,6 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
* return -1; | |||
*/ | |||
public long setBytes(Bytes key, byte[] value, long version) { | |||
// TODO: 支持多种数据类型; | |||
return dataset.setValue(key, value, version); | |||
} | |||
@@ -207,10 +197,6 @@ public class BaseAccount implements AccountHeader, MerkleProvable, Transactional | |||
@Override | |||
public void commit() { | |||
if (!accessPolicy.checkCommitting(this)) { | |||
throw new LedgerException("Account Committing was rejected for the access policy!"); | |||
} | |||
dataset.commit(); | |||
} | |||
@@ -6,10 +6,10 @@ import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.ledger.AccountHeader; | |||
import com.jd.blockchain.ledger.BytesValue; | |||
import com.jd.blockchain.ledger.BytesValueEntry; | |||
import com.jd.blockchain.ledger.KVDataEntry; | |||
import com.jd.blockchain.ledger.KVDataObject; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; | |||
public class DataAccount implements AccountHeader, MerkleProvable { | |||
@@ -41,9 +41,22 @@ public class DataAccount implements AccountHeader, MerkleProvable { | |||
public MerkleProof getProof(Bytes key) { | |||
return baseAccount.getProof(key); | |||
} | |||
public long setBytes(Bytes key, BytesValue value, long version) { | |||
byte[] bytesValue = BinaryProtocol.encode(value, BytesValue.class); | |||
return baseAccount.setBytes(key, bytesValue, version); | |||
} | |||
public long setBytes(Bytes key, String value, long version) { | |||
BytesValue bv = BytesValueEntry.fromText(value); | |||
return setBytes(key, bv, version); | |||
} | |||
public long setBytes(Bytes key, byte[] value, long version) { | |||
return baseAccount.setBytes(key, value, version); | |||
BytesValue bv = BytesValueEntry.fromBytes(value); | |||
return setBytes(key, bv, version); | |||
} | |||
/** | |||
@@ -119,7 +132,6 @@ public class DataAccount implements AccountHeader, MerkleProvable { | |||
*/ | |||
public KVDataEntry[] getDataEntries(int fromIndex, int count) { | |||
if (getDataEntriesTotalCount() == 0 || count == 0) { | |||
return null; | |||
} | |||
@@ -162,5 +174,4 @@ public class DataAccount implements AccountHeader, MerkleProvable { | |||
} | |||
return baseAccount.dataset.getDataCount(); | |||
} | |||
} |
@@ -0,0 +1,9 @@ | |||
package com.jd.blockchain.ledger.core; | |||
import java.util.SortedSet; | |||
public interface PermissionService { | |||
boolean checkLedgerPermission(); | |||
} |
@@ -1,11 +0,0 @@ | |||
package com.jd.blockchain.ledger.core; | |||
import java.util.SortedSet; | |||
public interface Privilege { | |||
SortedSet<Byte> getOpCodes(); | |||
long getVersion(); | |||
} |
@@ -4,6 +4,6 @@ public interface PrivilegeModelSetting { | |||
long getLatestVersion(); | |||
Privilege getPrivilege(long version); | |||
PermissionService getPrivilege(long version); | |||
} |
@@ -16,7 +16,7 @@ import com.jd.blockchain.utils.Bytes; | |||
public class OpeningAccessPolicy implements AccountAccessPolicy { | |||
@Override | |||
public boolean checkCommitting(AccountHeader account) { | |||
public boolean checkDataWriting(AccountHeader account) { | |||
return true; | |||
} | |||
@@ -3,10 +3,25 @@ package com.jd.blockchain.ledger.core.impl.handles; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import com.alibaba.fastjson.JSON; | |||
import com.jd.blockchain.contract.LedgerContext; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.ledger.*; | |||
import com.jd.blockchain.ledger.AccountHeader; | |||
import com.jd.blockchain.ledger.BlockchainIdentity; | |||
import com.jd.blockchain.ledger.BytesValue; | |||
import com.jd.blockchain.ledger.BytesValueEntry; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
import com.jd.blockchain.ledger.KVDataEntry; | |||
import com.jd.blockchain.ledger.KVInfoVO; | |||
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.Operation; | |||
import com.jd.blockchain.ledger.ParticipantNode; | |||
import com.jd.blockchain.ledger.TransactionState; | |||
import com.jd.blockchain.ledger.UserInfo; | |||
import com.jd.blockchain.ledger.UserRegisterOperation; | |||
import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
import com.jd.blockchain.transaction.BlockchainQueryService; | |||
import com.jd.blockchain.transaction.DataAccountKVSetOperationBuilder; | |||
@@ -16,7 +31,6 @@ import com.jd.blockchain.transaction.KVData; | |||
import com.jd.blockchain.transaction.UserRegisterOperationBuilder; | |||
import com.jd.blockchain.transaction.UserRegisterOperationBuilderImpl; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
public class ContractLedgerContext implements LedgerContext { | |||
@@ -250,17 +264,6 @@ public class ContractLedgerContext implements LedgerContext { | |||
this.accountAddress = accountAddress; | |||
} | |||
public boolean isJson(String str) { | |||
boolean result = false; | |||
try { | |||
Object obj=JSON.parse(str); | |||
result = true; | |||
} catch (Exception e) { | |||
result=false; | |||
} | |||
return result; | |||
} | |||
@Override | |||
public DataAccountKVSetOperation getOperation() { | |||
return op; | |||
@@ -268,41 +271,88 @@ public class ContractLedgerContext implements LedgerContext { | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueEntry(BytesValueType.BYTES, value); | |||
BytesValue bytesValue = BytesValueEntry.fromBytes(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
generatedOpList.add(op); | |||
opHandleContext.handle(op); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setText(String key, String value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromText(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setBytes(String key, Bytes value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromBytes(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setInt64(String key, long value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromInt64(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
@Deprecated | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, String value, long expVersion) { | |||
BytesValue bytesValue; | |||
if (isJson(value)) { | |||
bytesValue = new BytesValueEntry(BytesValueType.JSON, value.getBytes()); | |||
} | |||
else { | |||
bytesValue = new BytesValueEntry(BytesValueType.TEXT, value.getBytes()); | |||
} | |||
BytesValue bytesValue = BytesValueEntry.fromText(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
generatedOpList.add(op); | |||
opHandleContext.handle(op); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, Bytes value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueEntry(BytesValueType.BYTES, value.toBytes()); | |||
public DataAccountKVSetOperationBuilder setJSON(String key, String value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromJSON(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
generatedOpList.add(op); | |||
opHandleContext.handle(op); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setXML(String key, String value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromXML(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setBytes(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromBytes(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setImage(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromImage(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, long value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueEntry(BytesValueType.INT64, BytesUtils.toBytes(value)); | |||
public DataAccountKVSetOperationBuilder setTimestamp(String key, long value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromTimestamp(value); | |||
this.op = new SingleKVSetOpTemplate(key, bytesValue, expVersion); | |||
handle(op); | |||
return this; | |||
} | |||
private void handle(Operation op) { | |||
generatedOpList.add(op); | |||
opHandleContext.handle(op); | |||
return this; | |||
} | |||
/** | |||
@@ -24,8 +24,7 @@ public class DataAccountKVSetOperationHandle implements OperationHandle{ | |||
DataAccount account = dataset.getDataAccountSet().getDataAccount(kvWriteOp.getAccountAddress()); | |||
KVWriteEntry[] writeset = kvWriteOp.getWriteSet(); | |||
for (KVWriteEntry kvw : writeset) { | |||
byte[] value = BinaryProtocol.encode(kvw.getValue(), BytesValue.class); | |||
account.setBytes(Bytes.fromString(kvw.getKey()), value, kvw.getExpectedVersion()); | |||
account.setBytes(Bytes.fromString(kvw.getKey()), kvw.getValue(), kvw.getExpectedVersion()); | |||
} | |||
} | |||
@@ -14,7 +14,6 @@ import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.core.BaseAccount; | |||
import com.jd.blockchain.ledger.core.CryptoConfig; | |||
import com.jd.blockchain.ledger.core.impl.OpeningAccessPolicy; | |||
import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
@@ -44,13 +43,12 @@ public class BaseAccountTest { | |||
cryptoConf.setAutoVerifyHash(true); | |||
cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
OpeningAccessPolicy accPlc = new OpeningAccessPolicy(); | |||
// OpeningAccessPolicy accPlc = new OpeningAccessPolicy(); | |||
BlockchainKeypair bck = BlockchainKeyGenerator.getInstance().generate(); | |||
// 新建账户; | |||
BaseAccount baseAccount = new BaseAccount(bck.getIdentity(), cryptoConf, keyPrefix, testStorage, testStorage, | |||
accPlc); | |||
BaseAccount baseAccount = new BaseAccount(bck.getIdentity(), cryptoConf, keyPrefix, testStorage, testStorage); | |||
assertFalse(baseAccount.isUpdated());// 空的账户; | |||
assertFalse(baseAccount.isReadonly()); | |||
@@ -4,7 +4,7 @@ import com.jd.blockchain.binaryproto.DataContract; | |||
import com.jd.blockchain.binaryproto.DataField; | |||
import com.jd.blockchain.binaryproto.PrimitiveType; | |||
import com.jd.blockchain.consts.DataCodes; | |||
import com.jd.blockchain.utils.io.BytesSlice; | |||
import com.jd.blockchain.utils.Bytes; | |||
/** | |||
* BytesValue is the base structure of Value in Blockchain Account; | |||
@@ -29,6 +29,6 @@ public interface BytesValue { | |||
* @return | |||
*/ | |||
@DataField(order = 1, primitiveType = PrimitiveType.BYTES) | |||
BytesSlice getValue(); | |||
Bytes getValue(); | |||
} |
@@ -1,18 +1,79 @@ | |||
package com.jd.blockchain.ledger; | |||
import com.jd.blockchain.utils.io.BytesSlice; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
/** | |||
* Created by zhangshuang3 on 2018/12/3. | |||
* | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
public class BytesValueEntry implements BytesValue{ | |||
BytesValueType type; | |||
BytesSlice slice; | |||
Bytes value; | |||
public BytesValueEntry(BytesValueType type, byte[] bytes) { | |||
private BytesValueEntry(BytesValueType type, byte[] bytes) { | |||
this.type = type; | |||
this.slice = new BytesSlice(bytes); | |||
this.value = new Bytes(bytes); | |||
} | |||
private BytesValueEntry(BytesValueType type, Bytes bytes) { | |||
this.type = type; | |||
this.value = bytes; | |||
} | |||
public static BytesValue fromBytes(byte[] value) { | |||
return new BytesValueEntry(BytesValueType.BYTES, value); | |||
} | |||
public static BytesValue fromBytes(Bytes value) { | |||
return new BytesValueEntry(BytesValueType.BYTES, value); | |||
} | |||
public static BytesValue fromImage(byte[] value) { | |||
return new BytesValueEntry(BytesValueType.IMG, value); | |||
} | |||
public static BytesValue fromImage(Bytes value) { | |||
return new BytesValueEntry(BytesValueType.IMG, value); | |||
} | |||
public static BytesValue fromText(String value) { | |||
return new BytesValueEntry(BytesValueType.TEXT, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromJSON(String value) { | |||
return new BytesValueEntry(BytesValueType.JSON, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromXML(String value) { | |||
return new BytesValueEntry(BytesValueType.XML, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromInt32(int value) { | |||
return new BytesValueEntry(BytesValueType.INT32, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromInt64(long value) { | |||
return new BytesValueEntry(BytesValueType.INT64, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromInt16(short value) { | |||
return new BytesValueEntry(BytesValueType.INT16, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromInt8(byte value) { | |||
return new BytesValueEntry(BytesValueType.INT8, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromTimestamp(long value) { | |||
return new BytesValueEntry(BytesValueType.TIMESTAMP, BytesUtils.toBytes(value)); | |||
} | |||
public static BytesValue fromBoolean(boolean value) { | |||
return new BytesValueEntry(BytesValueType.BOOLEAN, BytesUtils.toBytes(value)); | |||
} | |||
@Override | |||
public BytesValueType getType() { | |||
@@ -24,8 +85,8 @@ public class BytesValueEntry implements BytesValue{ | |||
} | |||
@Override | |||
public BytesSlice getValue() { | |||
return this.slice; | |||
public Bytes getValue() { | |||
return this.value; | |||
} | |||
} |
@@ -37,9 +37,9 @@ public enum BytesValueType { | |||
INT64(PrimitiveType.INT64.CODE), | |||
/** | |||
* 日期时间; | |||
* 时间戳; | |||
*/ | |||
DATETIME(PrimitiveType.DATETIME.CODE), | |||
TIMESTAMP(PrimitiveType.TIMESTAMP.CODE), | |||
/** | |||
* 文本数据; | |||
@@ -81,7 +81,6 @@ public enum BytesValueType { | |||
*/ | |||
LOCATION(PrimitiveType.LOCATION.CODE); | |||
@EnumField(type = PrimitiveType.INT8) | |||
public final byte CODE; | |||
@@ -228,7 +228,7 @@ public class KVDataObject implements KVDataEntry { | |||
* 返回日期时间值; | |||
* <p> | |||
* | |||
* 仅当数据类型 {@link #getType()} 为 {@link PrimitiveType#DATETIME} 有效; | |||
* 仅当数据类型 {@link #getType()} 为 {@link PrimitiveType#TIMESTAMP} 有效; | |||
* <p> | |||
* | |||
* 无效类型将引发 {@link IllegalStateException} 异常; | |||
@@ -236,11 +236,11 @@ public class KVDataObject implements KVDataEntry { | |||
* @return | |||
*/ | |||
public Date datetimeValue() { | |||
if (PrimitiveType.DATETIME == type) { | |||
if (PrimitiveType.TIMESTAMP == type) { | |||
long ts = BytesUtils.toLong(bytesValue); | |||
return new Date(ts); | |||
} | |||
throw new IllegalStateException(String.format("Expected type [%s], but [%s]", PrimitiveType.DATETIME, type)); | |||
throw new IllegalStateException(String.format("Expected type [%s], but [%s]", PrimitiveType.TIMESTAMP, type)); | |||
} | |||
/** | |||
@@ -0,0 +1,15 @@ | |||
package com.jd.blockchain.ledger.core; | |||
public class LedgerPermissionException extends LedgerException { | |||
private static final long serialVersionUID = 6077975401474519117L; | |||
public LedgerPermissionException(String message) { | |||
super(message); | |||
} | |||
public LedgerPermissionException(String message, Throwable cause) { | |||
super(message, cause); | |||
} | |||
} |
@@ -0,0 +1,68 @@ | |||
package com.jd.blockchain.ledger; | |||
/** | |||
* 权限类型; | |||
* | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
public enum PermissionType { | |||
/** | |||
* 账户权限配置; | |||
*/ | |||
SET_PRIVILEGE(1), | |||
/** | |||
* 注册参与方; | |||
*/ | |||
REG_PARTICIPANT(2), | |||
/** | |||
* 配置账本;包括除了{@link #SET_PRIVILEGE}、 {@link #REG_PARTICIPANT} 之外的其它账本设置,例如:设置密码参数、共识参数等; | |||
*/ | |||
CONFIG_LEDGER(4), | |||
/** | |||
* 用户注册; | |||
*/ | |||
REG_USER(8), | |||
/** | |||
* 注册数据账户; | |||
*/ | |||
REG_DATA_ACCOUNT(16), | |||
/** | |||
* 部署新的合约代码; | |||
*/ | |||
DEPLOY_CONTRACT(32), | |||
/** | |||
* 写入用户信息; | |||
*/ | |||
SET_USER(1024), | |||
/** | |||
* 写入数据; | |||
*/ | |||
SET_DATA(2048), | |||
/** | |||
* 写入数据; | |||
*/ | |||
INVOKE_CONTRACT(4096), | |||
/** | |||
* 升级合约代码; | |||
*/ | |||
UPDATE_CONTRACT(8192); | |||
public final int CODE; | |||
private PermissionType(int code) { | |||
this.CODE = code; | |||
} | |||
} |
@@ -1,42 +0,0 @@ | |||
package com.jd.blockchain.ledger; | |||
/** | |||
* 权限类型; | |||
* | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
public enum PrivilegeType { | |||
/** | |||
* 账户注册; | |||
*/ | |||
ACCOUNT_REGISTER(1), | |||
/** | |||
* 账户权限配置; | |||
*/ | |||
PRIVILEGE_CONFIG(2), | |||
/** | |||
* 状态数据写入; | |||
*/ | |||
STATE_WRITE(4), | |||
/** | |||
* 合约应用部署; | |||
*/ | |||
CONTRACT_APP_DEPLOY(8), | |||
/** | |||
* 合约应用调用; | |||
*/ | |||
CONTRACT_APP_INVOKE(16); | |||
public final int CODE; | |||
private PrivilegeType(int code) { | |||
this.CODE = code; | |||
} | |||
} |
@@ -31,7 +31,6 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
private static final ContractEventSendOperationBuilderImpl CONTRACT_EVENT_SEND_OP_BUILDER = new ContractEventSendOperationBuilderImpl(); | |||
private LedgerInitOperationBuilder ledgerInitOpBuilder = new LedgerInitOperationBuilderFilter(); | |||
private UserRegisterOperationBuilder userRegOpBuilder = new UserRegisterOperationBuilderFilter(); | |||
@@ -78,7 +77,7 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
public ContractEventSendOperationBuilder contractEvents() { | |||
return contractEventSendOpBuilder; | |||
} | |||
@Override | |||
public <T> T contract(String address, Class<T> contractIntf) { | |||
// TODO Auto-generated method stub | |||
@@ -144,40 +143,80 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
return innerBuilder.getOperation(); | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, byte[] value, long expVersion) { | |||
innerBuilder.set(key, value, expVersion); | |||
private void addOperation() { | |||
if (op == null) { | |||
op = innerBuilder.getOperation(); | |||
operationList.add(op); | |||
} | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, byte[] value, long expVersion) { | |||
innerBuilder.set(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setText(String key, String value, long expVersion) { | |||
innerBuilder.setText(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setInt64(String key, long value, long expVersion) { | |||
innerBuilder.setInt64(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setBytes(String key, Bytes value, long expVersion) { | |||
innerBuilder.setBytes(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setBytes(String key, byte[] value, long expVersion) { | |||
innerBuilder.setBytes(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setImage(String key, byte[] value, long expVersion) { | |||
innerBuilder.setImage(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, String value, long expVersion) { | |||
innerBuilder.set(key, value, expVersion); | |||
if (op == null) { | |||
op = innerBuilder.getOperation(); | |||
operationList.add(op); | |||
} | |||
innerBuilder.setText(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, long value, long expVersion) { | |||
innerBuilder.set(key, value, expVersion); | |||
if (op == null) { | |||
op = innerBuilder.getOperation(); | |||
operationList.add(op); | |||
} | |||
public DataAccountKVSetOperationBuilder setJSON(String key, String value, long expVersion) { | |||
innerBuilder.setJSON(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, Bytes value, long expVersion) { | |||
innerBuilder.set(key, value, expVersion); | |||
if (op == null) { | |||
op = innerBuilder.getOperation(); | |||
operationList.add(op); | |||
} | |||
public DataAccountKVSetOperationBuilder setXML(String key, String value, long expVersion) { | |||
innerBuilder.setXML(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setTimestamp(String key, long value, long expVersion) { | |||
innerBuilder.setTimestamp(key, value, expVersion); | |||
addOperation(); | |||
return this; | |||
} | |||
@@ -3,6 +3,10 @@ package com.jd.blockchain.transaction; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.utils.Bytes; | |||
/** | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
public interface DataAccountKVSetOperationBuilder { | |||
/** | |||
@@ -13,7 +17,7 @@ public interface DataAccountKVSetOperationBuilder { | |||
DataAccountKVSetOperation getOperation(); | |||
/** | |||
* 写入键值; | |||
* 写入字节数组; | |||
* | |||
* @param key | |||
* 键; | |||
@@ -23,7 +27,35 @@ public interface DataAccountKVSetOperationBuilder { | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
@Deprecated | |||
DataAccountKVSetOperationBuilder set(String key, byte[] value, long expVersion); | |||
/** | |||
* 写入字节数组; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;byte[]格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder setBytes(String key, byte[] value, long expVersion); | |||
/** | |||
* 写入字节数组; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;Bytes格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder setBytes(String key, Bytes value, long expVersion); | |||
/** | |||
* 写入键值; | |||
* | |||
@@ -35,21 +67,76 @@ public interface DataAccountKVSetOperationBuilder { | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder setImage(String key, byte[] value, long expVersion); | |||
/** | |||
* 写入文本键值; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;String格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
@Deprecated | |||
DataAccountKVSetOperationBuilder set(String key, String value, long expVersion); | |||
/** | |||
* 写入键值; | |||
* 写入文本键值; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;Bytes格式 | |||
* 值;String格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder set(String key, Bytes value, long expVersion); | |||
DataAccountKVSetOperationBuilder setText(String key, String value, long expVersion); | |||
/** | |||
* 写入键值; | |||
* 写入JSON键值; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;String格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder setJSON(String key, String value, long expVersion); | |||
/** | |||
* 写入XML键值; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;String格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder setXML(String key, String value, long expVersion); | |||
/** | |||
* 写入64位整数; | |||
* | |||
* @param key | |||
* 键; | |||
* @param value | |||
* 值;long格式 | |||
* @param expVersion | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder setInt64(String key, long value, long expVersion); | |||
/** | |||
* 写入时间戳; | |||
* | |||
* @param key | |||
* 键; | |||
@@ -59,6 +146,6 @@ public interface DataAccountKVSetOperationBuilder { | |||
* 预期的当前版本;如果版本不匹配,则写入失败; | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder set(String key, long value, long expVersion); | |||
DataAccountKVSetOperationBuilder setTimestamp(String key, long value, long expVersion); | |||
} |
@@ -3,15 +3,12 @@ package com.jd.blockchain.transaction; | |||
import com.jd.blockchain.ledger.BytesValue; | |||
import com.jd.blockchain.ledger.BytesValueEntry; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.ledger.BytesValueType; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; | |||
public class DataAccountKVSetOperationBuilderImpl implements DataAccountKVSetOperationBuilder{ | |||
public class DataAccountKVSetOperationBuilderImpl implements DataAccountKVSetOperationBuilder { | |||
private DataAccountKVSetOpTemplate operation; | |||
public DataAccountKVSetOperationBuilderImpl(Bytes accountAddress) { | |||
operation = new DataAccountKVSetOpTemplate(accountAddress); | |||
} | |||
@@ -21,35 +18,69 @@ public class DataAccountKVSetOperationBuilderImpl implements DataAccountKVSetOpe | |||
return operation; | |||
} | |||
@Deprecated | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueEntry(BytesValueType.BYTES, value); | |||
return setBytes(key, value, expVersion); | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setBytes(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromBytes(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setImage(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromImage(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, String value, long expVersion) { | |||
BytesValue bytesValue; | |||
if (JSONSerializeUtils.isJSON(value)) { | |||
bytesValue = new BytesValueEntry(BytesValueType.JSON, value.getBytes()); | |||
} | |||
else { | |||
bytesValue = new BytesValueEntry(BytesValueType.TEXT, value.getBytes()); | |||
} | |||
return setText(key, value, expVersion); | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setText(String key, String value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromText(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, Bytes value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueEntry(BytesValueType.BYTES, value.toBytes()); | |||
public DataAccountKVSetOperationBuilder setBytes(String key, Bytes value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromBytes(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setInt64(String key, long value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromInt64(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setJSON(String key, String value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromJSON(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder setXML(String key, String value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromXML(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, long value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueEntry(BytesValueType.INT64, BytesUtils.toBytes(value)); | |||
public DataAccountKVSetOperationBuilder setTimestamp(String key, long value, long expVersion) { | |||
BytesValue bytesValue = BytesValueEntry.fromTimestamp(value); | |||
operation.set(key, bytesValue, expVersion); | |||
return this; | |||
} | |||
@@ -13,14 +13,16 @@ public interface DataAccountOperator { | |||
DataAccountRegisterOperationBuilder dataAccounts(); | |||
/** | |||
* 写入数据; | |||
* 写入数据; <br> | |||
* | |||
* @param accountAddress | |||
* @return | |||
*/ | |||
DataAccountKVSetOperationBuilder dataAccount(String accountAddress); | |||
/** | |||
* 写入数据; | |||
* | |||
* @param accountAddress | |||
* @return | |||
*/ | |||
@@ -1,6 +1,6 @@ | |||
package com.jd.blockchain.transaction; | |||
import com.jd.blockchain.ledger.PrivilegeType; | |||
import com.jd.blockchain.ledger.PermissionType; | |||
/** | |||
* 账户权限设置操作; | |||
@@ -16,10 +16,10 @@ import com.jd.blockchain.ledger.PrivilegeType; | |||
*/ | |||
public interface PrivilegeSettingOperationBuilder { | |||
PrivilegeSettingOperationBuilder setThreshhold(PrivilegeType privilege, long threshhold); | |||
PrivilegeSettingOperationBuilder setThreshhold(PermissionType privilege, long threshhold); | |||
PrivilegeSettingOperationBuilder enable(PrivilegeType privilege, String address, int weight); | |||
PrivilegeSettingOperationBuilder enable(PermissionType privilege, String address, int weight); | |||
PrivilegeSettingOperationBuilder disable(PrivilegeType privilege, String address); | |||
PrivilegeSettingOperationBuilder disable(PermissionType privilege, String address); | |||
} |
@@ -8,22 +8,21 @@ | |||
*/ | |||
package test.com.jd.blockchain.ledger.data; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.ledger.BytesValueEntry; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.ledger.BytesValueType; | |||
import com.jd.blockchain.ledger.Operation; | |||
import com.jd.blockchain.transaction.DataAccountKVSetOpTemplate; | |||
import com.jd.blockchain.transaction.KVData; | |||
import com.jd.blockchain.utils.Bytes; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
/** | |||
* | |||
* @author shaozhuguang | |||
@@ -43,11 +42,11 @@ public class DataAccountKVSetOpTemplateTest { | |||
String accountAddress = "zhangsandhakhdkah"; | |||
data = new DataAccountKVSetOpTemplate(Bytes.fromString(accountAddress)); | |||
KVData kvData1 = | |||
new KVData("test1", new BytesValueEntry(BytesValueType.TEXT, "zhangsan".getBytes()), 9999L); | |||
new KVData("test1", BytesValueEntry.fromText("zhangsan"), 9999L); | |||
KVData kvData2 = | |||
new KVData("test2", new BytesValueEntry(BytesValueType.TEXT, "lisi".getBytes()), 9990L); | |||
new KVData("test2", BytesValueEntry.fromText("lisi"), 9990L); | |||
KVData kvData3 = | |||
new KVData("test3", new BytesValueEntry(BytesValueType.TEXT, "wangwu".getBytes()), 1990L); | |||
new KVData("test3", BytesValueEntry.fromText("wangwu"), 1990L); | |||
data.set(kvData1); | |||
data.set(kvData2); | |||
data.set(kvData3); | |||
@@ -8,20 +8,19 @@ | |||
*/ | |||
package test.com.jd.blockchain.ledger.data; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.ledger.BytesValueEntry; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.ledger.BytesValueType; | |||
import com.jd.blockchain.transaction.DataAccountKVSetOpTemplate; | |||
import com.jd.blockchain.transaction.KVData; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
/** | |||
* | |||
* @author shaozhuguang | |||
@@ -30,26 +29,26 @@ import static org.junit.Assert.assertEquals; | |||
*/ | |||
public class KVDataTest { | |||
private KVData kvData; | |||
@Before | |||
public void initKVData() throws Exception { | |||
DataContractRegistry.register(DataAccountKVSetOperation.KVWriteEntry.class); | |||
String key = "test-key"; | |||
byte[] value = "test-value".getBytes(); | |||
long expectedVersion = 9999L; | |||
kvData = new KVData(key, new BytesValueEntry(BytesValueType.BYTES, value), expectedVersion); | |||
} | |||
@Test | |||
public void testSerialize_KVEntry() throws Exception { | |||
byte[] serialBytes = BinaryProtocol.encode(kvData, DataAccountKVSetOperation.KVWriteEntry.class); | |||
DataAccountKVSetOpTemplate.KVWriteEntry resolvedKvData = BinaryProtocol.decode(serialBytes); | |||
System.out.println("------Assert start ------"); | |||
assertEquals(resolvedKvData.getKey(), kvData.getKey()); | |||
assertEquals(resolvedKvData.getExpectedVersion(), kvData.getExpectedVersion()); | |||
assertArrayEquals(resolvedKvData.getValue().getValue().toBytes(), kvData.getValue().getValue().toBytes()); | |||
System.out.println("------Assert OK ------"); | |||
} | |||
private KVData kvData; | |||
@Before | |||
public void initKVData() throws Exception { | |||
DataContractRegistry.register(DataAccountKVSetOperation.KVWriteEntry.class); | |||
String key = "test-key"; | |||
byte[] value = "test-value".getBytes(); | |||
long expectedVersion = 9999L; | |||
kvData = new KVData(key, BytesValueEntry.fromBytes(value), expectedVersion); | |||
} | |||
@Test | |||
public void testSerialize_KVEntry() throws Exception { | |||
byte[] serialBytes = BinaryProtocol.encode(kvData, DataAccountKVSetOperation.KVWriteEntry.class); | |||
DataAccountKVSetOpTemplate.KVWriteEntry resolvedKvData = BinaryProtocol.decode(serialBytes); | |||
System.out.println("------Assert start ------"); | |||
assertEquals(resolvedKvData.getKey(), kvData.getKey()); | |||
assertEquals(resolvedKvData.getExpectedVersion(), kvData.getExpectedVersion()); | |||
assertArrayEquals(resolvedKvData.getValue().getValue().toBytes(), kvData.getValue().getValue().toBytes()); | |||
System.out.println("------Assert OK ------"); | |||
} | |||
} |
@@ -1,6 +1,6 @@ | |||
package com.jd.blockchain.sdk; | |||
import com.jd.blockchain.ledger.PrivilegeType; | |||
import com.jd.blockchain.ledger.PermissionType; | |||
/** | |||
* 权限设置;<br> | |||
@@ -16,6 +16,6 @@ public interface PrivilegeSetting { | |||
long getMask(String address); | |||
boolean isEnable(String address, PrivilegeType privilege); | |||
boolean isEnable(String address, PermissionType privilege); | |||
} |
@@ -197,7 +197,7 @@ public class IntegrationTestDataAccount { | |||
// | |||
// //add kv ops for data account: Bytes, string, long, json string | |||
DataAccountKVSetOperation dataKvsetOP = txTpl.dataAccount(dataAddr).set("A", "Value_A_0".getBytes(), -1) | |||
.set("B", "Value_B_0", -1).set("C", currentTime, -1).set("D", JSON.toJSONString(jsonTest), -1) | |||
.setText("B", "Value_B_0", -1).setInt64("C", currentTime, -1).setText("D", JSON.toJSONString(jsonTest), -1) | |||
.getOperation(); | |||
// 签名; | |||
@@ -19,6 +19,10 @@ public class BytesUtils { | |||
public static final int MAX_BUFFER_SIZE = 1024 * 1024 * 1024; | |||
public static final int BUFFER_SIZE = 64; | |||
public static final byte TRUE_BYTE = 1; | |||
public static final byte FALSE_BYTE = 0; | |||
private BytesUtils() { | |||
} | |||
@@ -28,10 +32,8 @@ public class BytesUtils { | |||
* | |||
* 此方法不处理两者其中之一为 null 的情形,因为无法定义相等性,所以将引发 {@link NullPointerException} 异常; | |||
* | |||
* @param bytes1 | |||
* bytes1 | |||
* @param bytes2 | |||
* bytes2 | |||
* @param bytes1 bytes1 | |||
* @param bytes2 bytes2 | |||
* @return boolean | |||
*/ | |||
public static boolean equals(byte[] bytes1, byte[] bytes2) { | |||
@@ -64,8 +66,7 @@ public class BytesUtils { | |||
* 将输入流的所有内容都读入到字节数组返回; 如果输入流的长度超出 MAX_BUFFER_SIZE 定义的值,则抛出 | |||
* IllegalArgumentException ; | |||
* | |||
* @param in | |||
* in | |||
* @param in in | |||
* @return byte[] | |||
*/ | |||
public static byte[] copyToBytes(InputStream in) { | |||
@@ -95,15 +96,11 @@ public class BytesUtils { | |||
/** | |||
* 将输入流复制到输出流; | |||
* | |||
* @param in | |||
* 输入流; | |||
* @param out | |||
* 输出流; | |||
* @param maxSize | |||
* 最大字节大小; | |||
* @param in 输入流; | |||
* @param out 输出流; | |||
* @param maxSize 最大字节大小; | |||
* @return 返回实际复制的字节数; | |||
* @throws IOException | |||
* exception | |||
* @throws IOException exception | |||
*/ | |||
public static int copy(InputStream in, OutputStream out, int maxSize) throws IOException { | |||
byte[] buffer = new byte[BUFFER_SIZE]; | |||
@@ -126,8 +123,7 @@ public class BytesUtils { | |||
/** | |||
* 将 int 值转为4字节的二进制数组; | |||
* | |||
* @param value | |||
* value | |||
* @param value value | |||
* @return 转换后的二进制数组,高位在前,低位在后; | |||
*/ | |||
public static byte[] toBytes(int value) { | |||
@@ -142,11 +138,14 @@ public class BytesUtils { | |||
return bytes; | |||
} | |||
public static byte[] toBytes(boolean value) { | |||
return new byte[] { value ? TRUE_BYTE : FALSE_BYTE }; | |||
} | |||
/** | |||
* 将 long 值转为8字节的二进制数组; | |||
* | |||
* @param value | |||
* value | |||
* @param value value | |||
* @return 转换后的二进制数组,高位在前,低位在后; | |||
*/ | |||
public static byte[] toBytes(long value) { | |||
@@ -158,10 +157,8 @@ public class BytesUtils { | |||
/** | |||
* 将 int 值转为4字节的二进制数组; | |||
* | |||
* @param value | |||
* 要转换的int整数; | |||
* @param bytes | |||
* 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 0 开始的4个元素; | |||
* @param value 要转换的int整数; | |||
* @param bytes 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 0 开始的4个元素; | |||
*/ | |||
public static void toBytes(short value, byte[] bytes) { | |||
toBytes(value, bytes, 0); | |||
@@ -176,12 +173,9 @@ public class BytesUtils { | |||
* <p> | |||
* 以“高位在前”的方式转换,即:数值的高位保存在数组地址的低位; | |||
* | |||
* @param value | |||
* 要转换的int整数; | |||
* @param bytes | |||
* 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; | |||
* @param offset | |||
* 写入转换结果的起始位置; | |||
* @param value 要转换的int整数; | |||
* @param bytes 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; | |||
* @param offset 写入转换结果的起始位置; | |||
* @return 返回写入的长度; | |||
*/ | |||
public static int toBytes(int value, byte[] bytes, int offset) { | |||
@@ -197,12 +191,9 @@ public class BytesUtils { | |||
* <p> | |||
* 以“高位在后”的方式转换,即:数值的高位保存在数组地址的高位; | |||
* | |||
* @param value | |||
* 要转换的int整数; | |||
* @param bytes | |||
* 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; | |||
* @param offset | |||
* 写入转换结果的起始位置; | |||
* @param value 要转换的int整数; | |||
* @param bytes 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; | |||
* @param offset 写入转换结果的起始位置; | |||
* @return 返回写入的长度; | |||
*/ | |||
public static int toBytesInReverse(int value, byte[] bytes, int offset) { | |||
@@ -218,14 +209,10 @@ public class BytesUtils { | |||
* <p> | |||
* 以“高位在后”的方式转换,即:数值的高位保存在数组地址的高位; | |||
* | |||
* @param value | |||
* 要转换的int整数; | |||
* @param bytes | |||
* 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; | |||
* @param offset | |||
* 写入转换结果的起始位置; | |||
* @param len | |||
* 写入长度;必须大于 0 ,小于等于 4; | |||
* @param value 要转换的int整数; | |||
* @param bytes 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; | |||
* @param offset 写入转换结果的起始位置; | |||
* @param len 写入长度;必须大于 0 ,小于等于 4; | |||
* @return 返回写入的长度; | |||
*/ | |||
public static int toBytesInReverse(int value, byte[] bytes, int offset, int len) { | |||
@@ -263,12 +250,9 @@ public class BytesUtils { | |||
/** | |||
* 将 long 值转为8字节的二进制数组; | |||
* | |||
* @param value | |||
* 要转换的long整数; | |||
* @param bytes | |||
* 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的8个元素; | |||
* @param offset | |||
* 写入转换结果的起始位置; | |||
* @param value 要转换的long整数; | |||
* @param bytes 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的8个元素; | |||
* @param offset 写入转换结果的起始位置; | |||
* @return 返回写入的长度; | |||
*/ | |||
public static int toBytes(long value, byte[] bytes, int offset) { | |||
@@ -345,8 +329,7 @@ public class BytesUtils { | |||
/** | |||
* 按从高位到低位的顺序将指定二进制数组从位置 0 开始的 4 个字节转换为 int 整数; | |||
* | |||
* @param bytes | |||
* 要转换的二进制数组; | |||
* @param bytes 要转换的二进制数组; | |||
* @return 转换后的 int 整数; | |||
*/ | |||
public static int toInt(byte[] bytes) { | |||
@@ -362,10 +345,8 @@ public class BytesUtils { | |||
/** | |||
* 按从高位到低位的顺序将指定二进制数组从 offset 参数指定的位置开始的 2 个字节转换为 short 整数; | |||
* | |||
* @param bytes | |||
* 要转换的二进制数组; | |||
* @param offset | |||
* 要读取数据的开始位置 | |||
* @param bytes 要转换的二进制数组; | |||
* @param offset 要读取数据的开始位置 | |||
* @return 转换后的 short 整数; | |||
*/ | |||
public static short toShort(byte[] bytes, int offset) { | |||
@@ -387,10 +368,8 @@ public class BytesUtils { | |||
/** | |||
* 按从高位到低位的顺序将指定二进制数组从 offset 参数指定的位置开始的 4 个字节转换为 int 整数; | |||
* | |||
* @param bytes | |||
* 要转换的二进制数组; | |||
* @param offset | |||
* 要读取数据的开始位置 | |||
* @param bytes 要转换的二进制数组; | |||
* @param offset 要读取数据的开始位置 | |||
* @return 转换后的 int 整数; | |||
*/ | |||
public static int toInt(byte[] bytes, int offset) { | |||
@@ -407,14 +386,11 @@ public class BytesUtils { | |||
/** | |||
* 按从高位到低位的顺序将指定二进制数组从 offset 参数指定的位置开始的 4 个字节转换为 int 整数; | |||
* | |||
* @param bytes | |||
* 要转换的二进制数组; | |||
* @param offset | |||
* 要读取数据的开始位置 | |||
* @param bytes 要转换的二进制数组; | |||
* @param offset 要读取数据的开始位置 | |||
* @return 转换后的 int 整数; | |||
* | |||
* @param len | |||
* 长度;len 必须满足: len 大于等于 1 且小于等于4; | |||
* @param len 长度;len 必须满足: len 大于等于 1 且小于等于4; | |||
* @return 转换后的 int 整数; | |||
*/ | |||
public static int toInt(byte[] bytes, int offset, int len) { | |||
@@ -433,18 +409,14 @@ public class BytesUtils { | |||
/** | |||
* 按从高位到低位的顺序将指定二进制数组从 offset 参数指定的位置开始的 4 个字节转换为 int 整数; | |||
* | |||
* @param bytes | |||
* 要转换的二进制数组; | |||
* @param offset | |||
* 要读取数据的开始位置 | |||
* @param bytes 要转换的二进制数组; | |||
* @param offset 要读取数据的开始位置 | |||
* @return 转换后的 int 整数; | |||
* | |||
* @param len | |||
* 长度;len 必须满足: len 大于等于 1 且小于等于4; | |||
* @param highAlign | |||
* 是否高位对齐;<br> | |||
* true 表示参数 bytes 的首个字节对应为整数的最高8位;<br> | |||
* false 表示参数 bytes 的最后字节对应为整数的最低8位; | |||
* @param len 长度;len 必须满足: len 大于等于 1 且小于等于4; | |||
* @param highAlign 是否高位对齐;<br> | |||
* true 表示参数 bytes 的首个字节对应为整数的最高8位;<br> | |||
* false 表示参数 bytes 的最后字节对应为整数的最低8位; | |||
* @return 转换后的 int 整数; | |||
*/ | |||
public static int toInt(byte[] bytes, int offset, int len, boolean highAlign) { | |||
@@ -472,10 +444,8 @@ public class BytesUtils { | |||
/** | |||
* 按从高位到低位的顺序将指定二进制数组从 offset 参数指定的位置开始的 8个字节转换为 long 整数; | |||
* | |||
* @param bytes | |||
* 要转换的二进制数组; | |||
* @param offset | |||
* 要读取数据的开始位置 | |||
* @param bytes 要转换的二进制数组; | |||
* @param offset 要读取数据的开始位置 | |||
* @return 转换后的 long 整数; | |||
*/ | |||
public static long toLong(byte[] bytes, int offset) { | |||
@@ -495,8 +465,7 @@ public class BytesUtils { | |||
/** | |||
* 从指定的输入流中读入2个字节,由前到后按由高位到低位的方式转为 short 整数; | |||
* | |||
* @param in | |||
* in | |||
* @param in in | |||
* @return short | |||
*/ | |||
public static short readShort(InputStream in) { | |||
@@ -531,8 +500,7 @@ public class BytesUtils { | |||
/** | |||
* 从指定的输入流中读入4个字节,由前到后按由高位到低位的方式转为 int 整数; | |||
* | |||
* @param in | |||
* in | |||
* @param in in | |||
* @return int | |||
*/ | |||
public static int readInt(InputStream in) { | |||
@@ -658,11 +626,9 @@ public class BytesUtils { | |||
/** | |||
* 从字节数组获取对象 | |||
* | |||
* @param objBytes | |||
* objBytes | |||
* @param objBytes objBytes | |||
* @return object | |||
* @throws Exception | |||
* exception | |||
* @throws Exception exception | |||
*/ | |||
// public static Object getObjectFromBytes(byte[] objBytes) throws Exception { | |||
// if (objBytes == null || objBytes.length == 0) { | |||
@@ -676,11 +642,9 @@ public class BytesUtils { | |||
/** | |||
* 从对象获取一个字节数组; | |||
* | |||
* @param obj | |||
* obj | |||
* @param obj obj | |||
* @return byte array | |||
* @throws Exception | |||
* exception | |||
* @throws Exception exception | |||
*/ | |||
public static byte[] getBytesFromObject(Object obj) throws Exception { | |||
if (obj == null) { | |||