use of com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode in project jdchain-core by blockchain-jd-com.
the class MerkleDataNodeEncoderTest method doTestV1.
private void doTestV1(long sn, long version, Bytes key, byte[] data) {
HashFunction hashFunc = Crypto.getHashFunction(ClassicAlgorithm.SHA256);
HashDigest dataHash = hashFunc.hash(data);
MerkleDataNodeEncoder encoderV1 = new MerkleDataNodeEncoder_V1();
DataNode node = encoderV1.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
assertEquals(dataHash, node.getValueHash());
assertEquals(sn, node.getSN());
assertEquals(version, node.getVersion());
assertEquals(key, node.getKey());
byte[] nodeBytes = node.toBytes();
DataNode node_reversed = encoderV1.resolve(nodeBytes);
assertEquals(dataHash, node_reversed.getValueHash());
assertEquals(node.getNodeHash(), node_reversed.getNodeHash());
assertEquals(encoderV1.getFormatVersion(), nodeBytes[0]);
assertEquals(sn, node_reversed.getSN());
assertEquals(version, node_reversed.getVersion());
assertEquals(key, node_reversed.getKey());
}
use of com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode in project jdchain-core by blockchain-jd-com.
the class MerkleDataNodeEncoderTest method testCompatibility.
@Test
public void testCompatibility() {
Random rand = new Random();
byte[] data = new byte[512];
byte[] keyBytes = new byte[256];
rand.nextBytes(data);
rand.nextBytes(keyBytes);
Bytes key = new Bytes(keyBytes);
long sn = 1024;
long version = 1;
PreviousDataNode pdataNode = PreviousDataNode.newDataNode(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
MerkleDataNodeEncoder encoderV0 = new MerkleDataNodeEncoder_V0();
DataNode dataNode = encoderV0.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
assertEquals(pdataNode.getNodeHash(), dataNode.getNodeHash());
assertEquals(pdataNode.getSN(), dataNode.getSN());
assertEquals(pdataNode.getVersion(), dataNode.getVersion());
assertEquals(pdataNode.getKey(), dataNode.getKey());
DataNode dataNode_reversed = encoderV0.resolve(pdataNode.toBytes());
assertNull(dataNode_reversed.getValueHash());
assertEquals(pdataNode.getNodeHash(), dataNode_reversed.getNodeHash());
assertEquals(pdataNode.getSN(), dataNode_reversed.getSN());
assertEquals(pdataNode.getVersion(), dataNode_reversed.getVersion());
assertEquals(pdataNode.getKey(), dataNode_reversed.getKey());
}
use of com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode in project jdchain-core by blockchain-jd-com.
the class MerkleDataNodeEncoder_V0 method resolve.
@Override
public DataNode resolve(byte[] bytes) {
if (bytes[0] != getFormatVersion()) {
throw new IllegalArgumentException("Unsupported version of data node bytes sequence[" + bytes[0] + "]! ");
}
// resolve SN;
byte[] snBytes = new byte[8];
snBytes[0] = 0x0;
System.arraycopy(bytes, 1, snBytes, 1, 7);
long sn = BytesUtils.toLong(snBytes);
// skip bytes of SN;
int offset = 8;
// byte[] keyBytes = BytesEncoding.read(NumberMask.SHORT, in);
// String key = BytesUtils.toString(keyBytes);
int keySize = (int) NumberMask.SHORT.resolveMaskedNumber(bytes, offset);
offset += NumberMask.SHORT.getMaskLength(keySize);
byte[] keyBytes = new byte[keySize];
System.arraycopy(bytes, offset, keyBytes, 0, keySize);
offset += keySize;
// String key = BytesUtils.toString(keyBytes);
Bytes key = new Bytes(keyBytes);
// long version = BytesUtils.readLong(in);
long version = BytesUtils.toLong(bytes, offset);
offset += 8;
// byte[] dataHashBytes = BytesEncoding.read(NumberMask.SHORT, in);
int hashSize = (int) NumberMask.TINY.resolveMaskedNumber(bytes, offset);
offset += NumberMask.TINY.getMaskLength(hashSize);
byte[] nodeHashBytes = new byte[hashSize];
System.arraycopy(bytes, offset, nodeHashBytes, 0, hashSize);
offset += hashSize;
HashDigest nodeHash = Crypto.resolveAsHashDigest(nodeHashBytes);
return new DataNode(nodeHash, sn, key, version, null, bytes);
}
use of com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode in project jdchain-core by blockchain-jd-com.
the class MerkleSequenceTreeTest method testWriteAndRead.
@Test
public void testWriteAndRead() {
Random rand = new Random();
CryptoSetting setting = Mockito.mock(CryptoSetting.class);
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256.code());
when(setting.getAutoVerifyHash()).thenReturn(true);
// 测试从空的树开始,顺序增加数据节点;
ExistancePolicyKVStorageMap kvs1 = new ExistancePolicyKVStorageMap();
MerkleSequenceTree mkt = new MerkleSequenceTree(setting, Bytes.fromString(keyPrefix), kvs1);
// 初始化,按照顺序的序列号加入10条记录;
int count = 18;
byte[] dataBuf = new byte[16];
DataNode[] dataNodes = new DataNode[count];
for (int i = 0; i < count; i++) {
rand.nextBytes(dataBuf);
long sn = i;
dataNodes[i] = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
assertEquals(sn, dataNodes[i].getSN());
}
mkt.commit();
HashDigest rootHash = mkt.getRootHash();
assertNotNull(rootHash);
long maxSN = mkt.getMaxSn();
long dataCount = mkt.getDataCount();
assertEquals(count, dataCount);
// 仅仅在采用顺序加入的情况下成立;
assertEquals(dataCount - 1, maxSN);
// reload;
mkt = new MerkleSequenceTree(rootHash, setting, keyPrefix, kvs1, false);
// 校验是否与之前的一致;
maxSN = mkt.getMaxSn();
dataCount = mkt.getDataCount();
assertEquals(count, dataCount);
// 仅仅在采用顺序加入的情况下成立;
assertEquals(dataCount - 1, maxSN);
// 取每一个数据节点
for (int i = 0; i <= maxSN; i++) {
DataNode dataNode = mkt.getData(i);
assertEquals(i, dataNode.getSN());
assertEquals(dataNodes[i].getNodeHash(), dataNode.getNodeHash());
assertEquals(dataNodes[i].getKey(), dataNode.getKey());
assertEquals(dataNodes[i].getLevel(), dataNode.getLevel());
assertEquals(dataNodes[i].getVersion(), dataNode.getVersion());
}
}
use of com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode in project jdchain-core by blockchain-jd-com.
the class MerkleDataNodeEncoderTest method doTestV0.
private void doTestV0(long sn, long version, Bytes key, byte[] data) {
MerkleDataNodeEncoder encoderV0 = new MerkleDataNodeEncoder_V0();
DataNode nodeV0 = encoderV0.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
assertNull(nodeV0.getValueHash());
assertEquals(sn, nodeV0.getSN());
assertEquals(version, nodeV0.getVersion());
assertEquals(key, nodeV0.getKey());
byte[] nodeBytes = nodeV0.toBytes();
DataNode nodeV0_reversed = encoderV0.resolve(nodeBytes);
assertNull(nodeV0_reversed.getValueHash());
assertEquals(nodeV0.getNodeHash(), nodeV0_reversed.getNodeHash());
assertEquals(encoderV0.getFormatVersion(), nodeBytes[0]);
assertEquals(sn, nodeV0_reversed.getSN());
assertEquals(version, nodeV0_reversed.getVersion());
assertEquals(key, nodeV0_reversed.getKey());
}
Aggregations