Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

(Bugfix) Fails to create transaction#1332

Closed
fernandomg wants to merge 2 commits intodevelopmentfrom
fix/newTx-not-working
Closed

(Bugfix) Fails to create transaction#1332
fernandomg wants to merge 2 commits intodevelopmentfrom
fix/newTx-not-working

Conversation

@fernandomg
Copy link
Contributor

This PR fixes an issue in development where transactions are not being properly created due to nonce not being properly calculated.

@fernandomg fernandomg added Bug 🐛 Something isn't working Critical Only for bugs in released apps, needs to be fixed asap and hotfix needs to be shipped. labels Sep 8, 2020
@fernandomg fernandomg self-assigned this Sep 8, 2020
@github-actions
Copy link

github-actions bot commented Sep 8, 2020

CLA Assistant Lite All Contributors have signed the CLA.

@github-actions
Copy link

github-actions bot commented Sep 8, 2020

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@ghost
Copy link

ghost commented Sep 8, 2020

Travis automatic deployment:
https://pr1332--safereact.review.gnosisdev.com/app

@mmv08
Copy link
Contributor

mmv08 commented Sep 9, 2020

@fernandomg Agustine made a solution for this in his PR #1230, maybe you can take it from here to avoid conflicts in the future

@mmv08
Copy link
Contributor

mmv08 commented Sep 9, 2020

On the other hand, why did it stop working at development? This function has been working fine for ages. May it be that we're dealing with the consequences instead of solving the real problem?

Copy link
Contributor

@rmeissner rmeissner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this to make sure that this doesn't happen again

@fernandomg
Copy link
Contributor Author

@mikheevm,

Agustine made a solution for this in his PR #1230, maybe you can take it from here to avoid conflicts in the future

I didn't know he was working on a particular bug that affects all the other PRs, in that particular one. Good to know.

On the other hand, why did it stop working at development? This function has been working fine for ages. May it be that we're dealing with the consequences instead of solving the real problem?

That's the exact same question I asked myself. The bug was introduced here: https://github.com/gnosis/safe-react/pull/1301/files#diff-6c3bab67fc37510237bb32f6523ef7f9R19-R32

If you see, I think that this fix is more likely to keep the function behavior from before. The as string part was unnecessary and forced an invalid type, thus, the returned value was wrong.

I just added the !txNonce because if it's undefined or '' will ask the safeInstance for the nonce.

@rmeissner,

Please add a test for this to make sure that this doesn't happen again

Agree, but I'm still waiting for the #1230 to be merged to have the mocked services that help to test.

@mmv08
Copy link
Contributor

mmv08 commented Sep 9, 2020

The as string part was unnecessary and forced an invalid type

The return type for the function is string, how could it force an invalid type?

@fernandomg
Copy link
Contributor Author

The as string part was unnecessary and forced an invalid type

The return type for the function is string, how could it force an invalid type?

you're returning undefined as string.

@Agupane
Copy link
Contributor

Agupane commented Sep 9, 2020

I do have tests for this on my test branch, don't lose time adding them here

@Agupane Agupane self-requested a review September 9, 2020 12:22
@mmv08
Copy link
Contributor

mmv08 commented Sep 9, 2020

@fernandomg function's return type is Promise<string>, in this case, it needs to be adjusted

@fernandomg
Copy link
Contributor Author

fernandomg commented Sep 9, 2020

@fernandomg function's return type is Promise<string>, in this case, it needs to be adjusted

That comment isn't adding any significant value to the discussion. Being a Promise or not, does not change the fact that txNonce as string is just wrong.

Thing is that the function "worked well for ages", and a bug was introduced due to enforced types.

Check the old code:

export const getNewTxNonce = async (txNonce, lastTx, safeInstance) => {
  if (!Number.isInteger(Number.parseInt(txNonce, 10))) {
    return lastTx === null
      ? // use current's safe nonce as fallback
        (await safeInstance.nonce()).toString()
      : `${lastTx.nonce + 1}`
  }
  return txNonce
}

the bug was introduced here:

export const getNewTxNonce = async (
  txNonce: string | undefined,
  lastTx: TxServiceModel | null,
  safeInstance: GnosisSafe,
): Promise<string> => {
  if (typeof txNonce === 'string' && !Number.isInteger(Number.parseInt(txNonce, 10))) {
    return lastTx === null
      ? // use current's safe nonce as fallback
        (await safeInstance.methods.nonce().call()).toString()
      : `${lastTx.nonce + 1}`
  }

  return txNonce as string
}

the solution I'm proposing is: if txNonce is falsy (that is undefined or ''), then enter the if, if not, return the txNonce.

We can't keep the original code as Number.isInteger doesn't accept undefined as a argument.

to avoid confusions, what I'm proposing in this PR is:

export const getNewTxNonce = async (
  txNonce: string | undefined,
  lastTx: TxServiceModel | null,
  safeInstance: GnosisSafe,
): Promise<string> => {
  if (!txNonce || !Number.isInteger(Number.parseInt(txNonce, 10))) {
    return lastTx === null
      ? // use current's safe nonce as fallback
        (await safeInstance.methods.nonce().call()).toString()
      : `${lastTx.nonce + 1}`
  }

  return txNonce
}

@mmv08
Copy link
Contributor

mmv08 commented Sep 9, 2020

@fernandomg sorry, I mixed it up a little bit. But nevertheless, I like Agustine's solution more because it's easier to understand.

@fernandomg fernandomg requested a review from rmeissner September 9, 2020 12:56
@fernandomg
Copy link
Contributor Author

fernandomg commented Sep 9, 2020

@mikheevm, updated with Agus' code.

@rmeissner, can you approve this? See: #1332 (comment)

This bug is blocking those PRs who already merged development

Except that if #1230 is getting merged right away, this PR makes no sense after all and we can close it.

@ghost
Copy link

ghost commented Sep 9, 2020

Travis automatic deployment:
https://pr1332--safereact.review.gnosisdev.com/app

@lukasschor lukasschor changed the title (Fix) Fails to create transaction (Bugfix) Fails to create transaction Sep 9, 2020
@lukasschor lukasschor removed Bug 🐛 Something isn't working Critical Only for bugs in released apps, needs to be fixed asap and hotfix needs to be shipped. labels Sep 9, 2020
@fernandomg
Copy link
Contributor Author

closed, as it's no longer needed due to the revert at: #1335

@fernandomg fernandomg closed this Sep 9, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Sep 9, 2020
@dasanra dasanra deleted the fix/newTx-not-working branch September 10, 2020 13:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants