Skip to content

Commit

Permalink
Fix Int hashcode method (#2278)
Browse files Browse the repository at this point in the history
* Fix Int hashcode method

* update

* Update packages/utils/src/types/u8aUtils.ts

Co-authored-by: Scott Twiname <[email protected]>

* update2

* update2

* Update packages/utils/src/types/u8aUtils.ts

Co-authored-by: Scott Twiname <[email protected]>

* Update packages/utils/src/types/supported/Int.ts

Co-authored-by: Scott Twiname <[email protected]>

---------

Co-authored-by: Scott Twiname <[email protected]>
  • Loading branch information
jiqiang90 and stwiname authored Feb 29, 2024
1 parent e60288f commit 006283e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
22 changes: 21 additions & 1 deletion packages/node-core/src/indexer/StoreOperations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,26 @@ const testOperations = [
const testOperations2 = [
{
operation: OperationType.Set,
entityType: 'StarterEntity',
entityType: 'IndexEra',
data: {
id: '0x2494bd5d089cf370c366351e851755ee42e8477df1c17ea1d9c2ae94e4f77ea8',
field1: 41914,
},
},
];

// negative in the field
const testOperations3 = [
{
operation: OperationType.Set,
entityType: 'StarterEntity',
data: {
id: '0x2494bd5d089cf370c366351e851755ee42e8477df1c17ea1d9c2ae94e4f77ea8',
field1: -1,
},
},
];

const falseOperation = {
operation: OperationType.Remove,
entityType: 'StarterEntity',
Expand Down Expand Up @@ -238,4 +250,12 @@ describe('StoreOperations', () => {
`Remove operation only accept data in string type`
);
});

it('could generate operation hash with negative value', () => {
const operationStack = new StoreOperations(models);
for (const o of testOperations3) {
operationStack.put(o.operation, o.entityType, o.data);
}
expect(() => operationStack.makeOperationMerkleTree()).not.toThrow();
});
});
3 changes: 3 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fixed Int hashCode method failed due to original method not support negative value

## [2.7.0] - 2024-01-25
### Added
- export sequelize support types (#2179)
Expand Down
21 changes: 21 additions & 0 deletions packages/utils/src/types/generalTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import {isU8a, numberToU8a, u8aConcat} from '@polkadot/util';
import {u8aEq} from '@subql/utils';
import {getTypeByScalarName} from '../types';
import {wrappedNumToU8a} from './u8aUtils';

describe('general types', () => {
it('can get json type', () => {
Expand All @@ -17,4 +20,22 @@ describe('general types', () => {
it('get unsupported type', () => {
expect(getTypeByScalarName('Unsupported')).toBe(undefined);
});

it('Int type should able to hash negative value', () => {
const testNum0 = 0;
const testNum = -1;
const testNum2 = -989899999;
const testNum3 = 10000;
const testNum4 = -98989999999999;
expect(() => wrappedNumToU8a(testNum0)).not.toThrow();
const testArray = wrappedNumToU8a(testNum);
const testArray2 = wrappedNumToU8a(testNum2);
const testArray4 = wrappedNumToU8a(testNum4);
expect(u8aEq(wrappedNumToU8a(testNum3), numberToU8a(testNum3))).toBeTruthy();
expect(isU8a(testArray)).toBeTruthy();
expect(isU8a(testArray2)).toBeTruthy();
expect(isU8a(testArray4)).toBeTruthy();

expect(u8aEq(u8aConcat(new Uint8Array([0]), wrappedNumToU8a(1000)), wrappedNumToU8a(-1000))).toBeTruthy();
});
});
3 changes: 2 additions & 1 deletion packages/utils/src/types/supported/Int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import {numberToU8a} from '@polkadot/util';
import {TypeClass} from '../TypeClass';
import {wrappedNumToU8a} from '../u8aUtils';

export const Int = new TypeClass(
'Int',
(data: number): Uint8Array => {
return numberToU8a(data);
return wrappedNumToU8a(data);
},
'number',
'Int',
Expand Down
18 changes: 18 additions & 0 deletions packages/utils/src/types/u8aUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import {numberToU8a, u8aConcat} from '@polkadot/util';

/**
* Due to current polkadotjs `numberToU8a` not support negative number,
* The helper method used to convert negative to a uint8Array
*
* @param byteArray
*/

export function wrappedNumToU8a(num: number) {
if (num >= 0) {
return numberToU8a(num);
}
return u8aConcat(new Uint8Array([0]), numberToU8a(Math.abs(num)));
}

0 comments on commit 006283e

Please sign in to comment.