From b9804620196d0c52b82bc618e457ae446657fbf4 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Thu, 26 Jan 2023 17:04:24 +0200 Subject: [PATCH 1/3] fix validation for estimateGas Signed-off-by: georgi-l95 --- packages/server/src/validator/objectTypes.ts | 40 +++++-- packages/server/tests/acceptance/rpc.spec.ts | 109 +++++++++++++++++++ 2 files changed, 137 insertions(+), 12 deletions(-) diff --git a/packages/server/src/validator/objectTypes.ts b/packages/server/src/validator/objectTypes.ts index 8ccefd38f0..acc3ec61ea 100644 --- a/packages/server/src/validator/objectTypes.ts +++ b/packages/server/src/validator/objectTypes.ts @@ -31,40 +31,56 @@ export const OBJECTS_VALIDATIONS = { }, "transaction": { "from": { - type: "address" + type: "address", + require: false }, "to": { - type: "address" + type: "address", + require: false }, "gas": { - type: "hex" + type: "hex", + require: false }, "gasPrice": { - type: "hex" + type: "hex", + require: false }, "maxPriorityFeePerGas": { - type: "hex" + type: "hex", + require: false }, "maxFeePerGas": { - type: "hex" + type: "hex", + require: false }, "value": { - type: "hex" + type: "hex", + require: false }, "data": { - type: "hex" + type: "hex", + require: false }, "type": { - type: "hex" + type: "hex", + require: false }, "chainId": { - type: "hex" + type: "hex", + require: false }, "nonce": { - type: "hex" + type: "hex", + require: false }, "input": { - type: "hex" + type: "hex", + require: false + }, + "accessList": { + type: "array", + require: false } } }; diff --git a/packages/server/tests/acceptance/rpc.spec.ts b/packages/server/tests/acceptance/rpc.spec.ts index 1ff6bf1b19..bdee5c5169 100644 --- a/packages/server/tests/acceptance/rpc.spec.ts +++ b/packages/server/tests/acceptance/rpc.spec.ts @@ -920,6 +920,115 @@ describe('@api RPC Server Acceptance Tests', function () { expect(res).to.not.be.equal('0x'); expect(res).to.not.be.equal('0x0'); }); + + it('should execute "eth_estimateGas" with to, from, value and gas filed', async function () { + const res = await relay.call('eth_estimateGas', [{ + from: '0x114f60009ee6b84861c0cdae8829751e517bc4d7', + to: '0xae410f34f7487e2cd03396499cebb09b79f45d6e', + value: '0xa688906bd8b00000', + gas: '0xd97010' + }], requestId); + expect(res).to.contain('0x'); + expect(res).to.not.be.equal('0x'); + expect(res).to.not.be.equal('0x0'); + }); + + it('should execute "eth_estimateGas" with to, from, value,accessList gas filed', async function () { + const res = await relay.call('eth_estimateGas', [{ + from: '0x114f60009ee6b84861c0cdae8829751e517bc4d7', + to: '0xae410f34f7487e2cd03396499cebb09b79f45d6e', + value: '0xa688906bd8b00000', + gas: '0xd97010', + accessList: [] + }], requestId); + expect(res).to.contain('0x'); + expect(res).to.not.be.equal('0x'); + expect(res).to.not.be.equal('0x0'); + }); + + it('should not be able to execute "eth_estimateGas" with no transaction object', async function () { + try { + await relay.call('eth_estimateGas', [], requestId); + Assertions.expectedError(); + } catch (error) { + const err = JSON.parse(error.body); + expect(error).to.not.be.null; + expect(err.error.name).to.be.equal('Missing required parameters'); + expect(err.error.message.endsWith('Missing value for required parameter 0')).to.be.true; + } + }); + + it('should not be able to execute "eth_estimateGas" with wrong from field', async function () { + try { + await relay.call('eth_estimateGas', [{ + from: '0x114f60009ee6b84861c0cdae8829751e517bc4d7', + to: '0xae410f34f7487e2cd03396499cebb09b79f45', + value: '0xa688906bd8b00000', + gas: '0xd97010', + accessList: [] + }], requestId); + Assertions.expectedError(); + } catch (error) { + const err = JSON.parse(error.body); + expect(error).to.not.be.null; + expect(err.error.name).to.be.equal('Invalid parameter'); + expect(err.error.message.endsWith(`Invalid parameter 'to' for TransactionObject: Expected 0x prefixed string representing the address (20 bytes)`)).to.be.true; + } + }); + + it('should not be able to execute "eth_estimateGas" with wrong to field', async function () { + try { + await relay.call('eth_estimateGas', [{ + from: '0x114f60009ee6b84861c0cdae8829751e517bc4d7', + to: '0xae410f34f7487e2cd03396499cebb09b79f45', + value: '0xa688906bd8b00000', + gas: '0xd97010', + accessList: [] + }], requestId); + Assertions.expectedError(); + } catch (error) { + const err = JSON.parse(error.body); + expect(error).to.not.be.null; + expect(err.error.name).to.be.equal('Invalid parameter'); + expect(err.error.message.endsWith(`Invalid parameter 'to' for TransactionObject: Expected 0x prefixed string representing the address (20 bytes)`)).to.be.true; + } + }); + + it('should not be able to execute "eth_estimateGas" with wrong value field', async function () { + try { + await relay.call('eth_estimateGas', [{ + from: '0x114f60009ee6b84861c0cdae8829751e517bc4d7', + to: '0xae410f34f7487e2cd03396499cebb09b79f45d6e', + value: '123', + gas: '0xd97010', + accessList: [] + }], requestId); + Assertions.expectedError(); + } catch (error) { + const err = JSON.parse(error.body); + expect(error).to.not.be.null; + expect(err.error.name).to.be.equal('Invalid parameter'); + expect(err.error.message.endsWith(`Invalid parameter 'value' for TransactionObject: Expected 0x prefixed hexadecimal value`)).to.be.true; + } + }); + + it('should not be able to execute "eth_estimateGas" with wrong gas field', async function () { + try { + await relay.call('eth_estimateGas', [{ + from: '0x114f60009ee6b84861c0cdae8829751e517bc4d7', + to: '0xae410f34f7487e2cd03396499cebb09b79f45d6e', + value: '0xa688906bd8b00000', + gas: '123', + accessList: [] + }], requestId); + Assertions.expectedError(); + } catch (error) { + const err = JSON.parse(error.body); + expect(error).to.not.be.null; + expect(err.error.name).to.be.equal('Invalid parameter'); + expect(err.error.message.endsWith(`Invalid parameter 'gas' for TransactionObject: Expected 0x prefixed hexadecimal value`)).to.be.true; + } + }); }); describe('eth_gasPrice', async function () { From aee36a954d97274453ca930996fc7d307198c865 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Thu, 26 Jan 2023 17:08:54 +0200 Subject: [PATCH 2/3] fix duplication Signed-off-by: georgi-l95 --- packages/server/src/validator/objectTypes.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/packages/server/src/validator/objectTypes.ts b/packages/server/src/validator/objectTypes.ts index acc3ec61ea..bb89973649 100644 --- a/packages/server/src/validator/objectTypes.ts +++ b/packages/server/src/validator/objectTypes.ts @@ -32,55 +32,42 @@ export const OBJECTS_VALIDATIONS = { "transaction": { "from": { type: "address", - require: false }, "to": { type: "address", - require: false }, "gas": { type: "hex", - require: false }, "gasPrice": { type: "hex", - require: false }, "maxPriorityFeePerGas": { type: "hex", - require: false }, "maxFeePerGas": { type: "hex", - require: false }, "value": { type: "hex", - require: false }, "data": { type: "hex", - require: false }, "type": { type: "hex", - require: false }, "chainId": { type: "hex", - require: false }, "nonce": { type: "hex", - require: false }, "input": { type: "hex", - require: false }, "accessList": { type: "array", - require: false } } }; From 985e15b6b60097e74ac8feafe2054c7525e109af Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Thu, 26 Jan 2023 17:11:39 +0200 Subject: [PATCH 3/3] fix duplication Signed-off-by: georgi-l95 --- packages/server/src/validator/objectTypes.ts | 26 ++++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/server/src/validator/objectTypes.ts b/packages/server/src/validator/objectTypes.ts index bb89973649..c52d7609bf 100644 --- a/packages/server/src/validator/objectTypes.ts +++ b/packages/server/src/validator/objectTypes.ts @@ -31,43 +31,43 @@ export const OBJECTS_VALIDATIONS = { }, "transaction": { "from": { - type: "address", + type: "address" }, "to": { - type: "address", + type: "address" }, "gas": { - type: "hex", + type: "hex" }, "gasPrice": { - type: "hex", + type: "hex" }, "maxPriorityFeePerGas": { - type: "hex", + type: "hex" }, "maxFeePerGas": { - type: "hex", + type: "hex" }, "value": { - type: "hex", + type: "hex" }, "data": { - type: "hex", + type: "hex" }, "type": { - type: "hex", + type: "hex" }, "chainId": { - type: "hex", + type: "hex" }, "nonce": { - type: "hex", + type: "hex" }, "input": { - type: "hex", + type: "hex" }, "accessList": { - type: "array", + type: "array" } } };