Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #25 from ChainSafeSystems/greg/little-edian
Browse files Browse the repository at this point in the history
Closes #23 - switch to little edian
  • Loading branch information
GregTheGreek authored Jan 24, 2019
2 parents 91405f7 + d81064a commit 5dc535b
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 558 deletions.
678 changes: 339 additions & 339 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "src/index.js",
"scripts": {
"test": "mocha",
"lint": "standard"
"lint": "standard",
"lint-fix": "standard --fix"
},
"repository": {
"type": "git",
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function serialize (value, type) {
// return (length + bytes)
let byteLengthBuffer = Buffer.alloc(4)
// write length to buffer as 4 byte int
byteLengthBuffer.writeUInt32BE(value.byteLength) // bigendian
byteLengthBuffer.writeUInt32LE(value.byteLength) // little endian
// write bytes to buffer
return Buffer.concat([byteLengthBuffer, value])
}
Expand All @@ -69,7 +69,7 @@ function serialize (value, type) {

// write length to buffer as 4 byte int
let byteLengthBuffer = Buffer.alloc(4)
byteLengthBuffer.writeUInt32BE(totalByteLength) // bigendian
byteLengthBuffer.writeUInt32LE(totalByteLength) // little endian

// start from end of the length number (4 bytes)
let ass = serializedValues.map((ab) => Buffer.from(ab))
Expand All @@ -85,7 +85,7 @@ function serialize (value, type) {

let totalByteLength = buffers.reduce((acc, v) => acc + v.byteLength, 0)
let byteLengthBuffer = Buffer.alloc(4)
byteLengthBuffer.writeUInt32BE(totalByteLength) // bigendian
byteLengthBuffer.writeUInt32LE(totalByteLength) // little endian

return Buffer.concat([byteLengthBuffer, ...buffers])
}
Expand Down Expand Up @@ -295,7 +295,7 @@ function assertEnoughBytes (data, start, length) {
function merkleHash (list) {
// Store length of list (to compensate for non-bijectiveness of padding)
const dataLen = Buffer.alloc(32)
dataLen.writeUInt32BE(list.length, 28) // big endian
dataLen.writeUInt32LE(list.length) // little endian
let chunkz
if (list.length === 0) {
chunkz = [Buffer.alloc(SSZ_CHUNK_SIZE)]
Expand Down
4 changes: 2 additions & 2 deletions src/intBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ function readIntBytes (type) {
let byteSize = intSize / 8

return (buffer, offset) => {
let bnResult = new BN([...buffer.slice(offset, (offset + byteSize))], 16, 'be').fromTwos(intSize)
let bnResult = new BN([...buffer.slice(offset, (offset + byteSize))], 16, 'le').fromTwos(intSize)
return intSize <= 32 ? bnResult.toNumber() : bnResult
}
}

function writeIntBytes (type) {
let intSize = parseInt(type.match(/\d+/g))
let byteSize = intSize / 8
return (buffer, value) => { new BN(value).toTwos(intSize).toBuffer('be', byteSize).copy(buffer) }
return (buffer, value) => { new BN(value).toTwos(intSize).toBuffer('le', byteSize).copy(buffer) }
}

exports.intByteLength = intByteLength
Expand Down
100 changes: 51 additions & 49 deletions test/test_ssz_deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe(`SimpleSerialize - deserializes bytes32, bytes96, bytes97`, () => {
assert.isNotNull(result, 'bytes32 result should not be null');
assert.equal(result.deserializedData.toString('hex'), bytesInput.toString('hex'), 'bytes32 result should be same as input');
assert.equal(result.offset, 32, 'Offset is should be 32');

});

it(`deserializes bytes96`, () => {
Expand All @@ -55,9 +55,9 @@ describe(`SimpleSerialize - deserializes bytes32, bytes96, bytes97`, () => {
assert.isNotNull(result, 'bytes96 result should not be null');
assert.equal(result.deserializedData.toString('hex'), bytesInput.toString('hex'), 'bytes96 result should be same as input');
assert.equal(result.offset, 96, 'Offset is should be 96');

});

it(`deserializes bytes97`, () => {

let bytesInput = hexToBytes('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015adba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015adba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015adaa');
Expand All @@ -66,14 +66,14 @@ describe(`SimpleSerialize - deserializes bytes32, bytes96, bytes97`, () => {
assert.isNotNull(result, 'bytes97 result should not be null');
assert.equal(result.deserializedData.toString('hex'), bytesInput.toString('hex'), 'bytes97 result should be same as input');
assert.equal(result.offset, 97, 'Offset is should be 97');

});
});

describe('SimpleSerialize - deserializes bytes20', () => {

it(`deserializes bytes20`, () => {

let bytes20Input = hexToBytes('e17cb53f339a726e0b347bbad221ad7b50dc2a30');
let result = deserialize(serialize(bytes20Input, 'bytes20'), 0, 'bytes20');

Expand All @@ -87,8 +87,8 @@ describe('SimpleSerialize - deserializes bytes20', () => {

describe('SimpleSerialize - deserializes signed integers', () => {

it(`deserializes int8`, () => {
it(`deserializes int8`, () => {

let intInput = 5;
let result = deserialize(serialize(intInput, 'int8'), 0, 'int8');

Expand All @@ -98,8 +98,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

});

it(`deserializes int16`, () => {
it(`deserializes int16`, () => {

let intInput = 32000;
let result = deserialize(serialize(intInput, 'int16'), 0, 'int16');

Expand All @@ -120,8 +120,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

});

it(`deserializes int32`, () => {
it(`deserializes int32`, () => {

let intInput = 1000000000;
let result = deserialize(serialize(intInput, 'int32'), 0, 'int32');

Expand All @@ -131,8 +131,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

});

it(`deserializes int64`, () => {
it(`deserializes int64`, () => {

let intInput = new BN(100000000000);
let result = deserialize(serialize(intInput, 'int64'), 0, 'int64');

Expand All @@ -142,8 +142,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

});

it(`deserializes int64 (negative)`, () => {
it(`deserializes int64 (negative)`, () => {

let intInput = new BN(-100000000000);
let result = deserialize(serialize(intInput, 'int64'), 0, 'int64');

Expand All @@ -153,8 +153,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

});

it(`deserializes int256`, () => {
it(`deserializes int256`, () => {

let intInput = new BN('123').pow(new BN(25));
let result = deserialize(serialize(intInput, 'int256'), 0, 'int256');

Expand All @@ -164,8 +164,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

});

it(`deserializes int256 (negative)`, () => {
it(`deserializes int256 (negative)`, () => {

let intInput = new BN('-123').pow(new BN(25));
let result = deserialize(serialize(intInput, 'int256'), 0, 'int256');

Expand All @@ -179,8 +179,8 @@ describe('SimpleSerialize - deserializes signed integers', () => {

describe('SimpleSerialize - deserializes unsigned integers', () => {

it(`deserializes uint8`, () => {
it(`deserializes uint8`, () => {

let intInput = 5;
let result = deserialize(serialize(intInput, 'uint8'), 0, 'uint8');

Expand All @@ -190,8 +190,8 @@ describe('SimpleSerialize - deserializes unsigned integers', () => {

});

it(`deserializes uint16`, () => {
it(`deserializes uint16`, () => {

let intInput = 32000;
let result = deserialize(serialize(intInput, 'uint16'), 0, 'uint16');

Expand All @@ -212,8 +212,8 @@ describe('SimpleSerialize - deserializes unsigned integers', () => {

});

it(`deserializes uint32`, () => {
it(`deserializes uint32`, () => {

let intInput = 1000000000;
let result = deserialize(serialize(intInput, 'uint32'), 0, 'uint32');

Expand All @@ -223,8 +223,8 @@ describe('SimpleSerialize - deserializes unsigned integers', () => {

});

it(`deserializes uint64`, () => {
it(`deserializes uint64`, () => {

let intInput = new BN(100000000000);
let result = deserialize(serialize(intInput, 'uint64'), 0, 'uint64');

Expand All @@ -235,8 +235,8 @@ describe('SimpleSerialize - deserializes unsigned integers', () => {

});

it(`deserializes uint128`, () => {
it(`deserializes uint128`, () => {

let intInput = new BN(1000000000);
let result = deserialize(serialize(intInput, 'uint128'), 0, 'uint128');

Expand All @@ -247,8 +247,8 @@ describe('SimpleSerialize - deserializes unsigned integers', () => {

});

it(`deserializes uint256`, () => {
it(`deserializes uint256`, () => {

let intInput = new BN(100000000000000);
let result = deserialize(serialize(intInput, 'uint256'), 0, 'uint256');

Expand All @@ -263,12 +263,12 @@ describe('SimpleSerialize - deserializes unsigned integers', () => {

describe('SimpleSerialize - deserialize bytes', () => {

it(`deserializes bytes`, () => {

it(`deserializes bytes`, () => {
let bytesArray = [];
for(var i = 0; i < 280; i++){
bytesArray.push(1);
}

let bytesInput = Buffer.from(bytesArray);
let result = deserialize(serialize(bytesInput, 'bytes'), 0, 'bytes');

Expand Down Expand Up @@ -456,13 +456,13 @@ describe('SimpleSerialize - deserialize objects', () => {
assert.isTrue(actualValue.eq(expectedValue), `Object serialised properties do not match input - actual ${actualValue} expected ${expectedValue}`);
}
else if(typeof actualValue.equals === 'function'){
assert.isTrue(actualValue.equals(expectedValue), `Object serialised properties do not match input - actual ${actualValue} expected ${expectedValue}`);
assert.isTrue(actualValue.equals(expectedValue), `Object serialised properties do not match input - actual ${actualValue} expected ${expectedValue}`);
}
else {
assert.equal(actualValue, expectedValue, 'Object serialised properties do not match input');
}
});

});

it(`deserializes objects containing objects`, () => {
Expand All @@ -471,36 +471,37 @@ describe('SimpleSerialize - deserialize objects', () => {
let recentHash1 = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
let recentHash2 = 'aa1116bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
testObj.recentBlockHashes = [
hexToBytes(recentHash1),
hexToBytes(recentHash1),
hexToBytes(recentHash2)
]
];

let attestRecord1 = new AttestationRecord(0, 1, Buffer.from([11, 12, 13, 14]));
let attestRecord2 = new AttestationRecord(2, 3, Buffer.from([255, 254, 253, 252]));
testObj.pendingAttestations = [
attestRecord1,
attestRecord2
]
];

let result = deserialize(serialize(testObj, ActiveState), 0, ActiveState);
assert.deepEqual(result.deserializedData, testObj);

});

const testCases = [
[["00000003000000", SimpleObject], new SimpleObject({b:0,a:0})],
[["00000003000201", SimpleObject], new SimpleObject({b:2,a:1})],
[["0000000703000000020006", OuterObject], new OuterObject({v:3, subV: new InnerObject({v:6})})],
[["000000120000000e0000000300020100000003000403", ArrayObject], new ArrayObject({v: [new SimpleObject({b:2,a:1}), new SimpleObject({b:4,a:3})]})],
[["0000001600000007030000000200060000000705000000020007", [OuterObject]], [new OuterObject({v:3, subV: new InnerObject({v:6})}), new OuterObject({v:5, subV: new InnerObject({v:7})})]],
]
[["30000000000000", SimpleObject], new SimpleObject({b:0,a:0})],
[["03000000020001", SimpleObject], new SimpleObject({b:2,a:1})],
[["0700000003020000000600", OuterObject], new OuterObject({v:3, subV: new InnerObject({v:6})})],
[["120000000E0000000300000002000103000000040003", ArrayObject], new ArrayObject({v: [new SimpleObject({b:2,a:1}), new SimpleObject({b:4,a:3})]})],
[["1600000007000000030200000006000700000005020000020700", [OuterObject]], [new OuterObject({v:3, subV: new InnerObject({v:6})}), new OuterObject({v:5, subV: new InnerObject({v:7})})]],
];

// like a deepEqual,but reliant on type, instead of inspection of ownProperties
const genericEqual = (obj1, obj2, type) => {
if (typeof type === 'string') { // simple type
assert.equal(obj1, obj2)
} else if (Array.isArray(type)) { // array type
const elementType = type[0]
assert.equal(obj1.length, obj2.length)
const elementType = type[0];
assert.equal(obj1.length, obj2.length);
for (let i = 0; i < obj1.length; i++) {
genericEqual(obj1[i], obj2[i], elementType)
}
Expand All @@ -509,11 +510,12 @@ describe('SimpleSerialize - deserialize objects', () => {
genericEqual(obj1[fieldName], obj2[fieldName], fieldType)
}
}
}
};

for (const [input, output] of testCases) {
const [bytes, type] = input
const [bytes, type] = input;
it(`successfully decodes objects - ${type.name || typeof type}`, () => {
const result = deserialize(Buffer.from(bytes, 'hex'), 0, type).deserializedData
const result = deserialize(Buffer.from(bytes, 'hex'), 0, type).deserializedData;
genericEqual(result, output, type)
})
}
Expand Down
Loading

0 comments on commit 5dc535b

Please sign in to comment.