Skip to content

Commit

Permalink
feat(connector-besu): customizable nonce and gas
Browse files Browse the repository at this point in the history
Signed-off-by: jordigiam <jordi.giron.amezcua@accenture.com>
  • Loading branch information
jordigiam authored and petermetz committed Mar 11, 2021
1 parent aa070ad commit 89c0060
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@
}
]
},
"nonce": {
"type": "number"
},
"data": {
"oneOf": [
{
Expand Down Expand Up @@ -491,6 +494,9 @@
}
]
},
"nonce": {
"type": "number"
},
"timeoutMs": {
"type": "number",
"description": "The amount of milliseconds to wait for a transaction receipt beforegiving up and crashing. Only has any effect if the invocation type is SEND",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export interface BesuTransactionConfig {
* @memberof BesuTransactionConfig
*/
gasPrice?: string | number;
/**
*
* @type {number}
* @memberof BesuTransactionConfig
*/
nonce?: number;
/**
*
* @type {string}
Expand Down Expand Up @@ -190,6 +196,12 @@ export interface InvokeContractV1Request {
* @memberof InvokeContractV1Request
*/
gasPrice?: string | number;
/**
*
* @type {number}
* @memberof InvokeContractV1Request
*/
nonce?: number;
/**
* The amount of milliseconds to wait for a transaction receipt beforegiving up and crashing. Only has any effect if the invocation type is SEND
* @type {number}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,14 @@ export class PluginLedgerConnectorBesu
const payload = (method.send as any).request();
const { params } = payload;
const [transactionConfig] = params;
if (req.gas == undefined) {
req.gas = await this.web3.eth.estimateGas(transactionConfig);
}
transactionConfig.from = web3SigningCredential.ethAccount;
transactionConfig.gas = req.gas;
transactionConfig.gasPrice = req.gasPrice;
transactionConfig.value = req.value;
transactionConfig.nonce = req.nonce;

const txReq: RunTransactionRequest = {
transactionConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,38 @@ test("deploys contract via .json file", async (t: Test) => {
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PRIVATEKEYHEX,
},
nonce: 1,
});
t2.ok(setNameOut, "setName() invocation #1 output is truthy OK");

try {
const setNameOutInvalid = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PRIVATEKEYHEX,
},
nonce: 1,
});
t2.ifError(setNameOutInvalid);
} catch (error) {
t2.notStrictEqual(
error,
"Nonce too low",
"setName() invocation with invalid nonce",
);
}
const { callOutput: getNameOut } = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
Expand Down Expand Up @@ -258,9 +281,30 @@ test("deploys contract via .json file", async (t: Test) => {
params: [newName],
gas: 1000000,
web3SigningCredential,
nonce: 4,
});
t2.ok(setNameOut, "setName() invocation #1 output is truthy OK");

try {
const setNameOutInvalid = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential,
nonce: 4,
});
t2.ifError(setNameOutInvalid);
} catch (error) {
t2.notStrictEqual(
error,
"Nonce too low",
"setName() invocation with invalid nonce",
);
}

const { callOutput: getNameOut } = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@
}
]
},
"nonce": {
"type": "number"
},
"data": {
"oneOf": [
{
Expand Down Expand Up @@ -520,6 +523,9 @@
}
]
},
"nonce": {
"type": "number"
},
"timeoutMs": {
"type": "number",
"description": "The amount of milliseconds to wait for a transaction receipt beforegiving up and crashing. Only has any effect if the invocation type is SEND",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ export interface InvokeContractV1Request {
* @memberof InvokeContractV1Request
*/
value?: string | number;
/**
*
* @type {number}
* @memberof InvokeContractV1Request
*/
nonce?: number;
/**
* The amount of milliseconds to wait for a transaction receipt beforegiving up and crashing. Only has any effect if the invocation type is SEND
* @type {number}
Expand Down Expand Up @@ -209,6 +215,12 @@ export interface QuorumTransactionConfig {
* @memberof QuorumTransactionConfig
*/
gasPrice?: string | number;
/**
*
* @type {number}
* @memberof QuorumTransactionConfig
*/
nonce?: number;
/**
*
* @type {string}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,14 @@ export class PluginLedgerConnectorQuorum
const payload = (method.send as any).request();
const { params } = payload;
const [transactionConfig] = params;
if (req.gas == undefined) {
req.gas = await this.web3.eth.estimateGas(transactionConfig);
}
transactionConfig.from = web3SigningCredential.ethAccount;
transactionConfig.gas = req.gas;
transactionConfig.gasPrice = req.gasPrice;
transactionConfig.value = req.value;
transactionConfig.nonce = req.nonce;

const txReq: RunTransactionRequest = {
transactionConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,34 @@ test("Quorum Ledger Connector Plugin", async (t: Test) => {
secret: "",
type: Web3SigningCredentialType.GETHKEYCHAINPASSWORD,
},
nonce: 2,
});
t2.ok(setNameOut, "setName() invocation #1 output is truthy OK");

try {
const setNameOutInvalid = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: "",
type: Web3SigningCredentialType.GETHKEYCHAINPASSWORD,
},
nonce: 2,
});
t2.ifError(setNameOutInvalid);
} catch (error) {
t2.notStrictEqual(
error,
"Nonce too low",
"setName() invocation with invalid nonce",
);
}

const getNameOut = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
Expand Down Expand Up @@ -227,15 +252,38 @@ test("Quorum Ledger Connector Plugin", async (t: Test) => {
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PRIVATEKEYHEX,
},
nonce: 1,
});
t2.ok(setNameOut, "setName() invocation #1 output is truthy OK");

try {
const setNameOutInvalid = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PRIVATEKEYHEX,
},
nonce: 1,
});
t2.ifError(setNameOutInvalid);
} catch (error) {
t2.notStrictEqual(
error,
"Nonce too low",
"setName() invocation with invalid nonce",
);
}
const { callOutput: getNameOut } = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
Expand Down Expand Up @@ -287,9 +335,33 @@ test("Quorum Ledger Connector Plugin", async (t: Test) => {
params: [newName],
gas: 1000000,
web3SigningCredential,
nonce: 3,
});
t2.ok(setNameOut, "setName() invocation #1 output is truthy OK");

try {
const setNameOutInvalid = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
invocationType: EthContractInvocationType.SEND,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: "",
type: Web3SigningCredentialType.GETHKEYCHAINPASSWORD,
},
nonce: 3,
});
t2.ifError(setNameOutInvalid);
} catch (error) {
t2.notStrictEqual(
error,
"Nonce too low",
"setName() invocation with invalid nonce",
);
}
const { callOutput: getNameOut } = await connector.invokeContract({
contractAddress,
contractAbi: HelloWorldContractJson.abi,
Expand Down

0 comments on commit 89c0060

Please sign in to comment.