use of org.apache.tuweni.bytes.Bytes in project xdagj by XDagger.
the class XdagSync method requestBlocks.
private void requestBlocks(long t, long dt) {
// 如果当前状态不是sync start
if (status != Status.SYNCING) {
return;
}
List<Channel> any = getAnyNode();
long randomSeq;
SettableFuture<Bytes> sf = SettableFuture.create();
if (any != null && any.size() != 0) {
// TODO:随机选一个
int index = RandomUtils.nextInt() % any.size();
Channel xc = any.get(index);
if (dt <= REQUEST_BLOCKS_MAX_TIME) {
randomSeq = xc.getXdag().sendGetBlocks(t, t + dt);
// log.debug("sendGetBlocks seq:{}",randomSeq);
blocksRequestMap.put(randomSeq, sf);
try {
sf.get(REQUEST_WAIT, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
blocksRequestMap.remove(randomSeq);
log.error(e.getMessage(), e);
return;
}
blocksRequestMap.remove(randomSeq);
} else {
// byte[] lSums = new byte[256];
MutableBytes lSums = MutableBytes.create(256);
Bytes rSums;
if (blockStore.loadSum(t, t + dt, lSums) <= 0) {
return;
}
// log.debug("lSum is " + Hex.toHexString(lSums));
randomSeq = xc.getXdag().sendGetSums(t, t + dt);
sumsRequestMap.put(randomSeq, sf);
// log.debug("sendGetSums seq:{}.", randomSeq);
try {
Bytes sums = sf.get(REQUEST_WAIT, TimeUnit.SECONDS);
// rSums = Arrays.copyOf(sums, 256);
rSums = sums.copy();
} catch (InterruptedException | ExecutionException | TimeoutException e) {
sumsRequestMap.remove(randomSeq);
log.error(e.getMessage(), e);
return;
}
sumsRequestMap.remove(randomSeq);
// log.debug("rSum is " + Hex.toHexString(rSums));
dt >>= 4;
for (int i = 0; i < 16; i++) {
// long lSumsSum = BytesUtils.bytesToLong(lSums, i * 16, true);
long lSumsSum = lSums.getLong(i * 16, ByteOrder.LITTLE_ENDIAN);
// long lSumsSize = BytesUtils.bytesToLong(lSums, i * 16 + 8, true);
long lSumsSize = lSums.getLong(i * 16 + 8, ByteOrder.LITTLE_ENDIAN);
// long rSumsSum = BytesUtils.bytesToLong(rSums, i * 16, true);
long rSumsSum = rSums.getLong(i * 16, ByteOrder.LITTLE_ENDIAN);
// long rSumsSize = BytesUtils.bytesToLong(rSums, i * 16 + 8, true);
long rSumsSize = rSums.getLong(i * 16 + 8, ByteOrder.LITTLE_ENDIAN);
if (lSumsSize != rSumsSize || lSumsSum != rSumsSum) {
requestBlocks(t + i * dt, dt);
}
}
}
}
}
use of org.apache.tuweni.bytes.Bytes in project xdagj by XDagger.
the class Block method getSubRawData.
/**
* 根据length获取前length个字段的数据 主要用于签名*
*/
public MutableBytes getSubRawData(int length) {
Bytes data = getXdagBlock().getData();
// byte[] res = new byte[512];
MutableBytes res = MutableBytes.create(512);
// System.arraycopy(data, 0, res, 0, (length + 1) * 32);
res.set(0, data.slice(0, (length + 1) * 32));
for (int i = length + 1; i < 16; i++) {
// long type = BytesUtils.bytesToLong(data, 8, true);
long type = data.getLong(8, ByteOrder.LITTLE_ENDIAN);
byte typeB = (byte) (type >> (i << 2) & 0xf);
if (XDAG_FIELD_SIGN_IN.asByte() == typeB || XDAG_FIELD_SIGN_OUT.asByte() == typeB) {
continue;
}
// System.arraycopy(data, (i) * 32, res, (i) * 32, 32);
res.set((i) * 32, data.slice((i) * 32, 32));
}
return res;
}
use of org.apache.tuweni.bytes.Bytes in project xdagj by XDagger.
the class Block method verifiedKeys.
/**
* 只匹配输入签名 并返回有用的key
*/
public List<SECP256K1.PublicKey> verifiedKeys() {
List<SECP256K1.PublicKey> keys = getPubKeys();
List<SECP256K1.PublicKey> res = new ArrayList<>();
Bytes digest;
Bytes32 hash;
for (SECP256K1.Signature sig : this.getInsigs().keySet()) {
digest = getSubRawData(this.getInsigs().get(sig) - 1);
for (SECP256K1.PublicKey publicKey : keys) {
// TODO: paulochen 是不是可以替换
byte[] pubkeyBytes = publicKey.asEcPoint().getEncoded(true);
hash = Hash.hashTwice(Bytes.wrap(digest, Bytes.wrap(pubkeyBytes)));
if (SECP256K1.verifyHashed(hash, sig, publicKey)) {
res.add(publicKey);
}
}
}
digest = getSubRawData(getOutsigIndex() - 2);
for (SECP256K1.PublicKey publicKey : keys) {
// TODO: paulochen 是不是可以替换
byte[] pubkeyBytes = publicKey.asEcPoint().getEncoded(true);
hash = Hash.hashTwice(Bytes.wrap(digest, Bytes.wrap(pubkeyBytes)));
if (SECP256K1.verifyHashed(hash, this.getOutsig(), publicKey)) {
res.add(publicKey);
}
}
return res;
}
use of org.apache.tuweni.bytes.Bytes in project xdagj by XDagger.
the class BlockStore method loadSum.
public int loadSum(long starttime, long endtime, MutableBytes sums) {
int level;
String key;
endtime -= starttime;
if (endtime == 0 || (endtime & (endtime - 1)) != 0) {
return -1;
}
for (level = -6; endtime != 0; level++, endtime >>= 4) {
;
}
List<String> files = getFileName((starttime) & 0xffffff000000L);
if (level < 2) {
key = files.get(3);
} else if (level < 4) {
key = files.get(2);
} else if (level < 6) {
key = files.get(1);
} else {
key = files.get(0);
}
Bytes buf = getSums(key);
if (buf == null) {
// Arrays.fill(sums, (byte)0);
sums.fill((byte) 0);
return 1;
}
long size = 0;
long sum = 0;
if ((level & 1) != 0) {
// Arrays.fill(sums, (byte)0);
sums.fill((byte) 0);
for (int i = 0; i < 256; i++) {
// long totalsum = BytesUtils.bytesToLong(buf, i * 16, true);
long totalsum = buf.getLong(i * 16, ByteOrder.LITTLE_ENDIAN);
sum += totalsum;
// long totalsize = BytesUtils.bytesToLong(buf, i * 16 + 8, true);
long totalsize = buf.getLong(i * 16 + 8, ByteOrder.LITTLE_ENDIAN);
size += totalsize;
if (i % 16 == 0 && i != 0) {
// System.arraycopy(BytesUtils.longToBytes(sum, true), 0, sums, i - 16, 8);
sums.set(i - 16, Bytes.wrap(BytesUtils.longToBytes(sum, true)));
// System.arraycopy(BytesUtils.longToBytes(size, true), 0, sums, i - 8, 8);
sums.set(i - 8, Bytes.wrap(BytesUtils.longToBytes(size, true)));
sum = 0;
size = 0;
}
}
} else {
long index = (starttime >> (level + 4) * 4) & 0xf0;
// System.arraycopy(buf, (int) (index * 16), sums, 0, 16 * 16);
sums.set(0, buf.slice((int) index * 16, 16 * 16));
}
return 1;
}
use of org.apache.tuweni.bytes.Bytes in project xdagj by XDagger.
the class BlockchainImpl method checkMineAndAdd.
public boolean checkMineAndAdd(Block block) {
List<SECP256K1.KeyPair> ourkeys = wallet.getAccounts();
// 输出签名只有一个
SECP256K1.Signature signature = block.getOutsig();
// 遍历所有key
for (int i = 0; i < ourkeys.size(); i++) {
SECP256K1.KeyPair ecKey = ourkeys.get(i);
// TODO: 优化
byte[] publicKeyBytes = ecKey.publicKey().asEcPoint().getEncoded(true);
Bytes digest = Bytes.wrap(block.getSubRawData(block.getOutsigIndex() - 2), Bytes.wrap(publicKeyBytes));
Bytes32 hash = Hash.hashTwice(Bytes.wrap(digest));
// if (ecKey.verify(hash.toArray(), signature)) { // TODO: 耗时长
if (SECP256K1.verifyHashed(hash, signature, ecKey.publicKey())) {
log.debug("Validate Success");
addOurBlock(i, block);
return true;
}
}
return false;
}
Aggregations