Fix large-value Compact encoding (#281)

This commit is contained in:
Jaco Greeff
2018-12-21 10:04:11 +01:00
committed by Amaury Martiny
parent 32daf3636a
commit 5585a16814
6 changed files with 19 additions and 38 deletions
+1 -1
View File
@@ -21,7 +21,7 @@
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@polkadot/dev": "^0.23.1",
"@polkadot/dev": "^0.23.3",
"@polkadot/ts": "^0.1.44"
}
}
+2 -2
View File
@@ -33,7 +33,7 @@ describe('compactFromU8a', () => {
it('decodes from same u32 as u64 encoded value (full, default)', () => {
expect(
compactFromU8a(new Uint8Array([3, 249, 255, 255, 255]), 64)
).toEqual([9, new BN(0xfffffff9)]);
compactFromU8a(new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255]), 64)
).toEqual([5, new BN(0xfffffff9)]);
});
});
+5 -1
View File
@@ -35,7 +35,11 @@ export default function compactFromU8a (_input: Uint8Array | string, bitLength:
return [4, u8aToBn(input.slice(0, 4), true).shrn(2)];
}
const offset = 1 + (bitLength / 8);
const length = new BN(input[0])
.shrn(2) // clear flag
.addn(4) // add 4 for base length
.toNumber();
const offset = 1 + length;
return [offset, u8aToBn(input.subarray(1, offset), true)];
}
+1 -1
View File
@@ -51,7 +51,7 @@ describe('encode', () => {
expect(
compactToU8a(0xfffffff9)
).toEqual(
new Uint8Array([3, 249, 255, 255, 255])
new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255])
);
});
});
+6 -1
View File
@@ -39,7 +39,12 @@ export default function compactToU8a (_value: BN | number, bitLength: BitLength
return u8aConcat(
new Uint8Array([
0b11
// FIXME For optimal encoding, detemine actual used bit (do not just use max)
new BN(bitLength / 8)
.subn(4) // at least 4
.shln(2) // clear low
.addn(0b11) // add flag
.toNumber()
]),
bnToU8a(value, bitLength, true)
);
+4 -32
View File
@@ -114,17 +114,6 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/helper-replace-supers" "^7.1.0"
"@babel/helper-create-class-features-plugin@^7.2.2":
version "7.2.2"
resolved "https://registry.yarnpkg.com/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.2.2.tgz#aac79552e41c94716a804d371e943f20171df0c5"
integrity sha512-Q4qZE5wS3NWpOS6UV9yhIS/NmSyf2keF0E0IwDvx8WxTheVopVIY6BSQ/0vz72OTTruz0cOA2yIUh6Kdg3qprA==
dependencies:
"@babel/helper-function-name" "^7.1.0"
"@babel/helper-member-expression-to-functions" "^7.0.0"
"@babel/helper-optimise-call-expression" "^7.0.0"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/helper-replace-supers" "^7.1.0"
"@babel/helper-define-map@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/helper-define-map/-/helper-define-map-7.1.0.tgz#3b74caec329b3c80c116290887c0dd9ae468c20c"
@@ -306,15 +295,6 @@
"@babel/helper-create-class-features-plugin" "^7.2.1"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-proposal-decorators@^7.2.2":
version "7.2.2"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.2.2.tgz#e7ce762732760542a5268a49bd19251f06dccd08"
integrity sha512-ylkKcbbS5IUocCti0gk762LMiMODWmlwRzyj1OCbPa83vSBlTKLmeBo9KMhoNpydibPcVwrP5rYJd0cKTVu7hw==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.2.2"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-decorators" "^7.2.0"
"@babel/plugin-proposal-json-strings@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-json-strings/-/plugin-proposal-json-strings-7.2.0.tgz#568ecc446c6148ae6b267f02551130891e29f317"
@@ -355,13 +335,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-decorators@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.2.0.tgz#c50b1b957dcc69e4b1127b65e1c33eef61570c1b"
integrity sha512-38QdqVoXdHUQfTpZo3rQwqQdWtCn5tMv4uV6r2RMfTqNBuv4ZBhz79SfaQWKTVmxHjeFv/DnXVC/+agHCklYWA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-json-strings@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.2.0.tgz#72bd13f6ffe1d25938129d2a186b11fd62951470"
@@ -1403,15 +1376,14 @@
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.2.tgz#54c5a964462be3d4d78af631363c18d6fa91ac26"
integrity sha512-yprFYuno9FtNsSHVlSWd+nRlmGoAbqbeCwOryP6sC/zoCjhpArcRMYp19EvpSUSizJAlsXEwJv+wcWS9XaXdMw==
"@polkadot/dev@^0.23.1":
version "0.23.1"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.23.1.tgz#e66a24ade33912976776f1029733ec057585d9a4"
integrity sha512-lheKMUUUAB9du7gfcbfIG/1DzYpYAP+2ubuy0glt2hzlZxEu/ZT9/gj0JRXCzSR1qnN0+EZCI1HWRBMnO8qM/A==
"@polkadot/dev@^0.23.3":
version "0.23.3"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.23.3.tgz#ee9bc6ccb1f985c8c79c8537bbebcd97cefbec1b"
integrity sha512-jAUbcEP0kQBkU9cXnlzghjqaDFda/cKjdXxR5nbgA8aIt5DvkACbihHSc0+ZDw5dr4NrbtDxx7hNuriWffKstg==
dependencies:
"@babel/cli" "^7.2.0"
"@babel/core" "^7.2.2"
"@babel/plugin-proposal-class-properties" "^7.2.0"
"@babel/plugin-proposal-decorators" "^7.2.2"
"@babel/plugin-proposal-object-rest-spread" "^7.2.0"
"@babel/plugin-transform-runtime" "^7.2.0"
"@babel/preset-env" "^7.2.0"