Skip to content

Commit

Permalink
Merge branch 'fix-varuint64reader' into #64-warp-sync-algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
vikinatora committed May 17, 2023
2 parents 95d5ba1 + 10e4d07 commit f244d80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public WarpSyncJustification read(ScaleCodecReader reader) {
WarpSyncJustification justification = new WarpSyncJustification();
justification.setRound(new UInt64Reader().read(reader));
justification.setTargetHash(new Hash256(reader.readUint256()));
justification.setTargetBlock(new VarUint64Reader().read(reader));
justification.setTargetBlock(new VarUint64Reader(4).read(reader));

int precommitsCount = reader.readCompactInt();
Precommit[] precommits = new Precommit[precommitsCount];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PrecommitReader implements ScaleReader<Precommit> {
public Precommit read(ScaleCodecReader reader) {
Precommit precommit = new Precommit();
precommit.setTargetHash(new Hash256(reader.readUint256()));
precommit.setTargetNumber(new VarUint64Reader().read(reader));
precommit.setTargetNumber(new VarUint64Reader(4).read(reader));
precommit.setSignature(new Hash512(reader.readByteArray(64)));
precommit.setAuthorityPublicKey(new Hash256(reader.readUint256()));
return precommit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.emeraldpay.polkaj.scale.ScaleReader;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
* Decodes into a `u64` a SCALE-encoded number whose number of bytes isn't known at compile-time.
Expand All @@ -12,27 +14,21 @@
* nom_varsize_number_decode_u64</a>
*/
public class VarUint64Reader implements ScaleReader<BigInteger> {
private final int blockNumSize;

public VarUint64Reader(int blockNumSize) {
this.blockNumSize = blockNumSize;
}

@Override
public BigInteger read(ScaleCodecReader reader) {
var out = new byte[8];
// 4 comes from that blocks are encoded in 4 bytes
var block = reader.readByteArray(4);
out[0] = block[0];
out[1] = block[1];
out[2] = block[2];
out[3] = block[3];

BigInteger result = BigInteger.ZERO;
byte[] input = reader.readByteArray(blockNumSize);
byte[] slice = new byte[8];
System.arraycopy(input, 0, slice, 0, blockNumSize);

result = result.add(BigInteger.valueOf(out[0]));
result = result.add(BigInteger.valueOf(out[1]).shiftLeft(8));
result = result.add(BigInteger.valueOf(out[2]).shiftLeft(16));
result = result.add(BigInteger.valueOf(out[3]).shiftLeft(24));
result = result.add(BigInteger.valueOf(out[4]).shiftLeft(32));
result = result.add(BigInteger.valueOf(out[5]).shiftLeft(40));
result = result.add(BigInteger.valueOf(out[6]).shiftLeft(48));
result = result.add(BigInteger.valueOf(out[7]).shiftLeft(56));
ByteBuffer buffer = ByteBuffer.wrap(slice);
buffer.order(ByteOrder.LITTLE_ENDIAN);

return result;
return BigInteger.valueOf(buffer.getLong());
}
}

0 comments on commit f244d80

Please sign in to comment.