| @@ -1,7 +1,7 @@ | |||
| package com.jd.blockchain.binaryproto; | |||
| /** | |||
| * 基础类型; | |||
| * 基础类型标志; | |||
| * | |||
| * @author huanghaiquan | |||
| * | |||
| @@ -19,9 +19,29 @@ public interface BaseType { | |||
| public static final byte BOOLEAN = (byte) 0x01; | |||
| /** | |||
| * 数值; | |||
| * 整数; | |||
| */ | |||
| public static final byte NUMERIC = (byte) 0x10; | |||
| public static final byte INTEGER = (byte) 0x10; | |||
| /** | |||
| * 8位整数; | |||
| */ | |||
| public static final byte INT8 = (byte) (INTEGER | 0x01); | |||
| /** | |||
| * 16位整数; | |||
| */ | |||
| public static final byte INT16 = (byte) (INTEGER | 0x02); | |||
| /** | |||
| * 32位整数; | |||
| */ | |||
| public static final byte INT32 = (byte) (INTEGER | 0x03); | |||
| /** | |||
| * 64位整数; | |||
| */ | |||
| public static final byte INT64 = (byte) (INTEGER | 0x04); | |||
| /** | |||
| * 文本 | |||
| @@ -19,80 +19,34 @@ public enum PrimitiveType { | |||
| BOOLEAN(BaseType.BOOLEAN), | |||
| /** | |||
| * 数值型: | |||
| * 8位的整数: | |||
| */ | |||
| INT8((byte) (BaseType.NUMERIC | 0x01)), | |||
| INT16((byte) (BaseType.NUMERIC | 0x02)), | |||
| INT32((byte) (BaseType.NUMERIC | 0x03)), | |||
| INT64((byte) (BaseType.NUMERIC | 0x04)), | |||
| INT8(BaseType.INT8), | |||
| /** | |||
| * 时间戳; | |||
| * 16位整数; | |||
| */ | |||
| TIMESTAMP((byte) (BaseType.NUMERIC | 0x08)), | |||
| INT16(BaseType.INT16), | |||
| /** | |||
| * 文本数据; | |||
| * 32位整数; | |||
| */ | |||
| TEXT(BaseType.TEXT), | |||
| INT32(BaseType.INT32), | |||
| /** | |||
| * 文本数据; | |||
| * 64位整数; | |||
| */ | |||
| JSON((byte) (BaseType.TEXT | 0x01)), | |||
| INT64(BaseType.INT64), | |||
| /** | |||
| * 文本数据; | |||
| * 文本; | |||
| */ | |||
| XML((byte) (BaseType.TEXT | 0x02)), | |||
| TEXT(BaseType.TEXT), | |||
| /** | |||
| * 二进制数据; | |||
| */ | |||
| BYTES(BaseType.BYTES), | |||
| /** | |||
| * 大整数; | |||
| */ | |||
| BIG_INT((byte) (BaseType.BYTES | 0x01)), | |||
| /** | |||
| * 图片; | |||
| */ | |||
| IMG((byte) (BaseType.BYTES | 0x02)), | |||
| /** | |||
| * 视频; | |||
| */ | |||
| VIDEO((byte) (BaseType.BYTES | 0x03)), | |||
| /** | |||
| * 位置坐标; | |||
| */ | |||
| LOCATION((byte) (BaseType.BYTES | 0x04)), | |||
| /** | |||
| * 公钥; | |||
| */ | |||
| PUB_KEY((byte) (BaseType.BYTES | 0x05)), | |||
| /** | |||
| * 签名摘要; | |||
| */ | |||
| SIGNATURE_DIGEST((byte) (BaseType.BYTES | 0x06)), | |||
| /** | |||
| * 哈希摘要; | |||
| */ | |||
| HASH_DIGEST((byte) (BaseType.BYTES | 0x07)), | |||
| /** | |||
| * 加密数据; | |||
| */ | |||
| ENCRYPTED_DATA((byte) (BaseType.BYTES | 0x08)); | |||
| BYTES(BaseType.BYTES); | |||
| public final byte CODE; | |||
| @@ -18,7 +18,7 @@ import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.BytesValueType; | |||
| import com.jd.blockchain.ledger.DataType; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| @@ -109,7 +109,7 @@ public class LedgerEditerTest { | |||
| // 验证数据读写的一致性; | |||
| BytesValue bytes = dataAccount.getBytes("A"); | |||
| assertEquals(BytesValueType.TEXT, bytes.getType()); | |||
| assertEquals(DataType.TEXT, bytes.getType()); | |||
| String textValue = bytes.getValue().toUTF8String(); | |||
| assertEquals("abc", textValue); | |||
| } | |||
| @@ -21,7 +21,7 @@ public interface BytesValue { | |||
| * @return | |||
| */ | |||
| @DataField(order = 0, refEnum = true) | |||
| BytesValueType getType(); | |||
| DataType getType(); | |||
| /** | |||
| * 数据值的二进制序列; | |||
| @@ -9,37 +9,37 @@ import com.jd.blockchain.utils.io.BytesUtils; | |||
| * | |||
| */ | |||
| public class BytesValueEntry implements BytesValue { | |||
| BytesValueType type; | |||
| DataType type; | |||
| Bytes value; | |||
| private BytesValueEntry(BytesValueType type, byte[] bytes) { | |||
| private BytesValueEntry(DataType type, byte[] bytes) { | |||
| this.type = type; | |||
| this.value = new Bytes(bytes); | |||
| } | |||
| private BytesValueEntry(BytesValueType type, Bytes bytes) { | |||
| private BytesValueEntry(DataType type, Bytes bytes) { | |||
| this.type = type; | |||
| this.value = bytes; | |||
| } | |||
| public static BytesValue fromType(BytesValueType type, byte[] value) { | |||
| public static BytesValue fromType(DataType type, byte[] value) { | |||
| return new BytesValueEntry(type, value); | |||
| } | |||
| public static BytesValue fromBytes(byte[] value) { | |||
| return new BytesValueEntry(BytesValueType.BYTES, value); | |||
| return new BytesValueEntry(DataType.BYTES, value); | |||
| } | |||
| public static BytesValue fromBytes(Bytes value) { | |||
| return new BytesValueEntry(BytesValueType.BYTES, value); | |||
| return new BytesValueEntry(DataType.BYTES, value); | |||
| } | |||
| public static BytesValue fromImage(byte[] value) { | |||
| return new BytesValueEntry(BytesValueType.IMG, value); | |||
| return new BytesValueEntry(DataType.IMG, value); | |||
| } | |||
| public static BytesValue fromImage(Bytes value) { | |||
| return new BytesValueEntry(BytesValueType.IMG, value); | |||
| return new BytesValueEntry(DataType.IMG, value); | |||
| } | |||
| /** | |||
| @@ -49,7 +49,7 @@ public class BytesValueEntry implements BytesValue { | |||
| * @return | |||
| */ | |||
| public static BytesValue fromText(String value) { | |||
| return new BytesValueEntry(BytesValueType.TEXT, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.TEXT, BytesUtils.toBytes(value)); | |||
| } | |||
| /** | |||
| @@ -62,51 +62,51 @@ public class BytesValueEntry implements BytesValue { | |||
| if (bytesValue == null) { | |||
| return null; | |||
| } | |||
| if (bytesValue.getType() != BytesValueType.TEXT) { | |||
| throw new ValueTypeCastException("The expected value type is " + BytesValueType.TEXT.toString() | |||
| if (bytesValue.getType() != DataType.TEXT) { | |||
| throw new ValueTypeCastException("The expected value type is " + DataType.TEXT.toString() | |||
| + ", but it is actually " + bytesValue.getType().toString() + "!"); | |||
| } | |||
| return bytesValue.getValue().toUTF8String(); | |||
| } | |||
| public static BytesValue fromJSON(String value) { | |||
| return new BytesValueEntry(BytesValueType.JSON, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.JSON, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromXML(String value) { | |||
| return new BytesValueEntry(BytesValueType.XML, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.XML, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromInt32(int value) { | |||
| return new BytesValueEntry(BytesValueType.INT32, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.INT32, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromInt64(long value) { | |||
| return new BytesValueEntry(BytesValueType.INT64, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.INT64, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromInt16(short value) { | |||
| return new BytesValueEntry(BytesValueType.INT16, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.INT16, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromInt8(byte value) { | |||
| return new BytesValueEntry(BytesValueType.INT8, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.INT8, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromTimestamp(long value) { | |||
| return new BytesValueEntry(BytesValueType.TIMESTAMP, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.TIMESTAMP, BytesUtils.toBytes(value)); | |||
| } | |||
| public static BytesValue fromBoolean(boolean value) { | |||
| return new BytesValueEntry(BytesValueType.BOOLEAN, BytesUtils.toBytes(value)); | |||
| return new BytesValueEntry(DataType.BOOLEAN, BytesUtils.toBytes(value)); | |||
| } | |||
| @Override | |||
| public BytesValueType getType() { | |||
| public DataType getType() { | |||
| return this.type; | |||
| } | |||
| public void setType(BytesValueType type) { | |||
| public void setType(DataType type) { | |||
| this.type = type; | |||
| } | |||
| @@ -1,8 +1,9 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.binaryproto.BaseType; | |||
| import com.jd.blockchain.binaryproto.EnumContract; | |||
| import com.jd.blockchain.binaryproto.EnumField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| /** | |||
| @@ -11,8 +12,8 @@ import com.jd.blockchain.consts.DataCodes; | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| @EnumContract(code = DataCodes.ENUM_TYPE_BYTES_VALUE_TYPE, name = "BytesValueType", decription = "") | |||
| public enum BytesValueType { | |||
| @EnumContract(code = DataCodes.ENUM_TYPE_BYTES_VALUE_TYPE) | |||
| public enum DataType { | |||
| /** | |||
| * 空; | |||
| @@ -35,61 +36,88 @@ public enum BytesValueType { | |||
| INT32(PrimitiveType.INT32.CODE), | |||
| INT64(PrimitiveType.INT64.CODE), | |||
| /** | |||
| * 时间戳; | |||
| */ | |||
| TIMESTAMP(PrimitiveType.TIMESTAMP.CODE), | |||
| /** | |||
| * 文本数据; | |||
| */ | |||
| TEXT(PrimitiveType.TEXT.CODE), | |||
| /** | |||
| * 文本数据; | |||
| * 二进制数据; | |||
| */ | |||
| JSON(PrimitiveType.JSON.CODE), | |||
| BYTES(PrimitiveType.BYTES.CODE), | |||
| /** | |||
| * 时间戳; | |||
| */ | |||
| TIMESTAMP((byte) (BaseType.INTEGER | 0x08)), | |||
| /** | |||
| * 文本数据; | |||
| */ | |||
| XML(PrimitiveType.XML.CODE), | |||
| JSON((byte) (BaseType.TEXT | 0x01)), | |||
| /** | |||
| * 二进制数据; | |||
| * 文本数据; | |||
| */ | |||
| BYTES(PrimitiveType.BYTES.CODE), | |||
| XML((byte) (BaseType.TEXT | 0x02)), | |||
| /** | |||
| * 大整数; | |||
| */ | |||
| BIG_INT(PrimitiveType.BIG_INT.CODE), | |||
| BIG_INT((byte) (BaseType.BYTES | 0x01)), | |||
| /** | |||
| * 图片; | |||
| */ | |||
| IMG(PrimitiveType.IMG.CODE), | |||
| IMG((byte) (BaseType.BYTES | 0x02)), | |||
| /** | |||
| * 视频; | |||
| */ | |||
| VIDEO(PrimitiveType.VIDEO.CODE), | |||
| VIDEO((byte) (BaseType.BYTES | 0x03)), | |||
| /** | |||
| * 位置; | |||
| * 位置坐标; | |||
| */ | |||
| LOCATION(PrimitiveType.LOCATION.CODE); | |||
| LOCATION((byte) (BaseType.BYTES | 0x04)), | |||
| /** | |||
| * 公钥; | |||
| */ | |||
| PUB_KEY((byte) (BaseType.BYTES | 0x05)), | |||
| /** | |||
| * 签名摘要; | |||
| */ | |||
| SIGNATURE_DIGEST((byte) (BaseType.BYTES | 0x06)), | |||
| /** | |||
| * 哈希摘要; | |||
| */ | |||
| HASH_DIGEST((byte) (BaseType.BYTES | 0x07)), | |||
| /** | |||
| * 加密数据; | |||
| */ | |||
| ENCRYPTED_DATA((byte) (BaseType.BYTES | 0x08)); | |||
| @EnumField(type = PrimitiveType.INT8) | |||
| public final byte CODE; | |||
| private BytesValueType(byte code) { | |||
| private DataType(byte code) { | |||
| this.CODE = code; | |||
| } | |||
| public static BytesValueType valueOf(byte code) { | |||
| for (BytesValueType dataType : BytesValueType.values()) { | |||
| public static DataType valueOf(byte code) { | |||
| for (DataType dataType : DataType.values()) { | |||
| if (dataType.CODE == code) { | |||
| return dataType; | |||
| } | |||
| @@ -25,7 +25,7 @@ public interface KVDataEntry { | |||
| * | |||
| * @return | |||
| */ | |||
| BytesValueType getType(); | |||
| DataType getType(); | |||
| /** | |||
| * 值; | |||
| @@ -58,8 +58,8 @@ public class KVDataObject implements KVDataEntry { | |||
| * @see com.jd.blockchain.ledger.KVDataEntry#getType() | |||
| */ | |||
| @Override | |||
| public BytesValueType getType() { | |||
| return bytesValue == null ? BytesValueType.NIL : bytesValue.getType(); | |||
| public DataType getType() { | |||
| return bytesValue == null ? DataType.NIL : bytesValue.getType(); | |||
| } | |||
| @Override | |||
| @@ -95,7 +95,7 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public boolean isNil() { | |||
| return bytesValue == null || BytesValueType.NIL == bytesValue.getType(); | |||
| return bytesValue == null || DataType.NIL == bytesValue.getType(); | |||
| } | |||
| /** | |||
| @@ -119,10 +119,10 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public byte tinyValue() { | |||
| if (BytesValueType.INT8 == getType()) { | |||
| if (DataType.INT8 == getType()) { | |||
| return bytesValue.getValue().toBytes()[0]; | |||
| } | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", BytesValueType.INT8, getType())); | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", DataType.INT8, getType())); | |||
| } | |||
| /** | |||
| @@ -137,10 +137,10 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public short shortValue() { | |||
| if (BytesValueType.INT16 == getType()) { | |||
| if (DataType.INT16 == getType()) { | |||
| return BytesUtils.toShort(bytesValue.getValue().toBytes(), 0); | |||
| } | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", BytesValueType.INT16, getType())); | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", DataType.INT16, getType())); | |||
| } | |||
| /** | |||
| @@ -155,10 +155,10 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public int intValue() { | |||
| if (BytesValueType.INT32 == getType()) { | |||
| if (DataType.INT32 == getType()) { | |||
| return BytesUtils.toInt(bytesValue.getValue().toBytes(), 0); | |||
| } | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", BytesValueType.INT32, getType())); | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", DataType.INT32, getType())); | |||
| } | |||
| /** | |||
| @@ -173,10 +173,10 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public long longValue() { | |||
| if (BytesValueType.INT64 == getType()) { | |||
| if (DataType.INT64 == getType()) { | |||
| return BytesUtils.toLong(bytesValue.getValue().toBytes(), 0); | |||
| } | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", BytesValueType.INT64, getType())); | |||
| throw new IllegalStateException(String.format("Expected type [%s], but [%s]", DataType.INT64, getType())); | |||
| } | |||
| @@ -192,11 +192,11 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public BigInteger bigIntValue() { | |||
| if (BytesValueType.BIG_INT == getType()) { | |||
| if (DataType.BIG_INT == getType()) { | |||
| return new BigInteger(bytesValue.getValue().toBytes()); | |||
| } | |||
| throw new IllegalStateException( | |||
| String.format("Expected type [%s], but [%s]", BytesValueType.BIG_INT, getType())); | |||
| String.format("Expected type [%s], but [%s]", DataType.BIG_INT, getType())); | |||
| } | |||
| /** | |||
| @@ -211,11 +211,11 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public boolean boolValue() { | |||
| if (BytesValueType.BOOLEAN == getType()) { | |||
| if (DataType.BOOLEAN == getType()) { | |||
| return BytesUtils.toBoolean(bytesValue.getValue().toBytes()[0]); | |||
| } | |||
| throw new IllegalStateException( | |||
| String.format("Expected type [%s], but [%s]", BytesValueType.BOOLEAN, getType())); | |||
| String.format("Expected type [%s], but [%s]", DataType.BOOLEAN, getType())); | |||
| } | |||
| /** | |||
| @@ -230,12 +230,12 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public Date datetimeValue() { | |||
| if (BytesValueType.TIMESTAMP == getType()) { | |||
| if (DataType.TIMESTAMP == getType()) { | |||
| long ts = BytesUtils.toLong(bytesValue.getValue().toBytes()); | |||
| return new Date(ts); | |||
| } | |||
| throw new IllegalStateException( | |||
| String.format("Expected type [%s], but [%s]", BytesValueType.TIMESTAMP, getType())); | |||
| String.format("Expected type [%s], but [%s]", DataType.TIMESTAMP, getType())); | |||
| } | |||
| /** | |||
| @@ -251,12 +251,12 @@ public class KVDataObject implements KVDataEntry { | |||
| * @return | |||
| */ | |||
| public String stringValue() { | |||
| BytesValueType type = getType(); | |||
| if (BytesValueType.TEXT == type || BytesValueType.JSON == type || BytesValueType.XML == type) { | |||
| DataType type = getType(); | |||
| if (DataType.TEXT == type || DataType.JSON == type || DataType.XML == type) { | |||
| return bytesValue.getValue().toUTF8String(); | |||
| } | |||
| throw new IllegalStateException(String.format("Expected type [%s] or [%s] or [%s] , but [%s]", | |||
| PrimitiveType.TEXT, BytesValueType.JSON, BytesValueType.XML, type)); | |||
| PrimitiveType.TEXT, DataType.JSON, DataType.XML, type)); | |||
| } | |||
| // // ---------------- | |||