Fix getting negative values from signed integers. (#88)

Co-authored-by: nikugogoi <95nikass@gmail.com>
This commit is contained in:
Ashwin Phatak 2021-06-23 15:23:59 +05:30 committed by GitHub
parent fc30290685
commit 27b732fef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 6 deletions

View File

@ -208,8 +208,17 @@ describe('Get value from storage', () => {
describe('signed integer type', () => {
let integers: Contract, storageLayout: StorageLayout, blockHash: string;
const int1Value = 12;
const int2Value = 34;
const int2Value = -34;
const int3Value = 123;
const int4Value = -12;
const int5Value = -123;
const int6Value = -456;
const int7Value = -789;
const int9Value = -1234;
const int10Value = -4567;
const int11Value = -12345;
const int12Value = -67890;
const int13Value = -123456;
before(async () => {
({ contract: integers, storageLayout } = contracts.TestIntegers);
@ -217,7 +226,16 @@ describe('Get value from storage', () => {
const transactions = await Promise.all([
integers.setInt1(int1Value),
integers.setInt2(int2Value),
integers.setInt3(int3Value)
integers.setInt3(int3Value),
integers.setInt4(int4Value),
integers.setInt5(int5Value),
integers.setInt6(int6Value),
integers.setInt7(int7Value),
integers.setInt9(int9Value),
integers.setInt10(int10Value),
integers.setInt11(int11Value),
integers.setInt12(int12Value),
integers.setInt13(int13Value)
]);
await Promise.all(transactions.map(transaction => transaction.wait()));
@ -248,6 +266,71 @@ describe('Get value from storage', () => {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
});
it('get value for integer type variables with negative values', async () => {
let { value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int4');
expect(value).to.equal(BigInt(int4Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int5'));
expect(value).to.equal(BigInt(int5Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int6'));
expect(value).to.equal(BigInt(int6Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int7'));
expect(value).to.equal(BigInt(int7Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int9'));
expect(value).to.equal(BigInt(int9Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int10'));
expect(value).to.equal(BigInt(int10Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int11'));
expect(value).to.equal(BigInt(int11Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int12'));
expect(value).to.equal(BigInt(int12Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int13'));
expect(value).to.equal(BigInt(int13Value));
if (isIpldGql) {
assertProofData(blockHash, integers.address, JSON.parse(proofData));
}
});
});
describe('unsigned integer type', () => {

View File

@ -79,9 +79,17 @@ export const getValueByType = (storageValue: string, typeLabel: string): bigint
return !BigNumber.from(storageValue).isZero();
}
const [isIntegerOrEnum, isInteger, isUnsigned, integerSize] = typeLabel.match(/^enum|((u?)int([0-9]+))/) || [false];
// Parse value for uint/int type or enum type.
if (typeLabel.match(/^enum|u?int[0-9]+/)) {
return BigInt(storageValue);
if (isIntegerOrEnum) {
let bigNumber = BigNumber.from(storageValue);
if (Boolean(isInteger) && !isUnsigned) {
bigNumber = bigNumber.fromTwos(Number(integerSize));
}
return BigInt(bigNumber.toString());
}
// Parse value for string type.

View File

@ -10,7 +10,16 @@ contract TestIntegers {
int256 int3;
// Integer type variable is stored in the next slot as there is not enough space for it in the previous slot.
int32 int4;
int24 int4;
int32 int5;
int64 int6;
int72 int7;
int96 int9;
int128 int10;
int200 int11;
int232 int12;
int256 int13;
// Set variable int1.
function setInt1(int8 value) external {
@ -28,7 +37,47 @@ contract TestIntegers {
}
// Set variable int4.
function setInt4(int32 value) external {
function setInt4(int24 value) external {
int4 = value;
}
// Set variable int5.
function setInt5(int32 value) external {
int5 = value;
}
// Set variable int6.
function setInt6(int64 value) external {
int6 = value;
}
// Set variable int7.
function setInt7(int72 value) external {
int7 = value;
}
// Set variable int9.
function setInt9(int96 value) external {
int9 = value;
}
// Set variable int10.
function setInt10(int128 value) external {
int10 = value;
}
// Set variable int11.
function setInt11(int200 value) external {
int11 = value;
}
// Set variable int12.
function setInt12(int232 value) external {
int12 = value;
}
// Set variable int13.
function setInt13(int256 value) external {
int13 = value;
}
}