-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement creating future transactions #5390
Merged
ikem-legend
merged 23 commits into
release/3.0.0
from
5317-enable-creating-future-transactions
Oct 25, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
04016c2
Implemented initial account nonce sync
ikem-legend b7bbd4b
Optimized nonce hook and minor Redux fix
ikem-legend c656a9a
Implemented nonce hook in transaction signing process
ikem-legend e8226e5
:test_tube: Fix failing tests
ikem-legend 30483d5
:test_tube: Fixed remaining failing tests
ikem-legend e8b51bc
Refactored nonce hook, reducer and minor changes
ikem-legend 2faef57
Implemented nonce sync test
ikem-legend 1e73365
Merge branch 'release/3.0.0' of github.com:LiskHQ/lisk-desktop into 5…
ikem-legend 2044aa9
Implemented changes based on PR review
ikem-legend 938d2fa
Minor refactor to fix default account nonce and failing tests
ikem-legend 70235e6
Refactored nonce hook logic
ikem-legend f1b852a
Fixed nonce bug during transaction signing
ikem-legend 2475a52
:test_tube: Fixed failing test
ikem-legend eb172de
Fixed coverage issues
ikem-legend e456f67
Implemented PR feedback
ikem-legend d3a7e18
Merge branch 'release/3.0.0' of github.com:LiskHQ/lisk-desktop into 5…
ikem-legend fc4a70c
Implemented nonce reset feature
ikem-legend f7888a9
Updated nonce reset icon and fixed failing tests
ikem-legend a5aa841
Minor DeepScan fix
ikem-legend 4c5cc31
Fixed coverage issue and minor changes
ikem-legend b273f6a
Implementing Redux state clear on nonce reset
ikem-legend c7190c7
Fixed bugs regarding reset of nonce
oskarleonard 9e3a707
PR feedback and coverage issue fixes
ikem-legend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,14 +58,39 @@ export const list = (state = {}, { type, encryptedAccount, accountDetail, addres | |
} | ||
}; | ||
|
||
export const localNonce = (state = {}, { type, address, nonce, transactionHex }) => { | ||
switch (type) { | ||
case actionTypes.setAccountNonce: | ||
if (state[address]?.[transactionHex]) { | ||
return state; | ||
} | ||
return { | ||
...state, | ||
[address]: { | ||
...state[address], | ||
[transactionHex]: nonce, | ||
}, | ||
}; | ||
Comment on lines
+67
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a follow up to my above statement this create a history of transactions created, though there is not clean up strategy. |
||
|
||
case actionTypes.resetAccountNonce: | ||
return { | ||
...state, | ||
[address]: { defaultNonce: nonce }, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const persistConfig = { | ||
key: 'account', | ||
storage, | ||
whitelist: ['list', 'current'], // only navigation will be persisted | ||
whitelist: ['list', 'current', 'localNonce'], // only navigation will be persisted | ||
blacklist: [], | ||
}; | ||
|
||
const accountReducer = combineReducers({ current, list }); | ||
const accountReducer = combineReducers({ current, list, localNonce }); | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const account = persistReducer(persistConfig, accountReducer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const selectCurrentAccount = (state) => state.account.current; | ||
export const selectAccounts = (state) => state.account.list || []; | ||
export const selectAccountNonce = (state) => state.account.localNonce || {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||||||||||||||||||||||||||||||||||||
import { useEffect, useState, useCallback } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||
import { useQueryClient } from '@tanstack/react-query'; | ||||||||||||||||||||||||||||||||||||||||||
import { useCurrentApplication } from '@blockchainApplication/manage/hooks'; | ||||||||||||||||||||||||||||||||||||||||||
import { useCurrentAccount, useAccounts } from '@account/hooks'; | ||||||||||||||||||||||||||||||||||||||||||
import useSettings from '@settings/hooks/useSettings'; | ||||||||||||||||||||||||||||||||||||||||||
import { AUTH } from 'src/const/queries'; | ||||||||||||||||||||||||||||||||||||||||||
import { useAuthConfig } from './queries'; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// eslint-disable-next-line max-statements | ||||||||||||||||||||||||||||||||||||||||||
const useNonceSync = () => { | ||||||||||||||||||||||||||||||||||||||||||
const queryClient = useQueryClient(); | ||||||||||||||||||||||||||||||||||||||||||
oskarleonard marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||
const [currentApplication] = useCurrentApplication(); | ||||||||||||||||||||||||||||||||||||||||||
const [currentAccount] = useCurrentAccount(); | ||||||||||||||||||||||||||||||||||||||||||
const { setNonceByAccount, getNonceByAccount, resetNonceByAccount } = useAccounts(); | ||||||||||||||||||||||||||||||||||||||||||
const currentAccountAddress = currentAccount.metadata.address; | ||||||||||||||||||||||||||||||||||||||||||
const { mainChainNetwork } = useSettings('mainChainNetwork'); | ||||||||||||||||||||||||||||||||||||||||||
const chainID = currentApplication.chainID; | ||||||||||||||||||||||||||||||||||||||||||
const customConfig = { | ||||||||||||||||||||||||||||||||||||||||||
params: { | ||||||||||||||||||||||||||||||||||||||||||
address: currentAccountAddress, | ||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
const serviceUrl = mainChainNetwork?.serviceUrl; | ||||||||||||||||||||||||||||||||||||||||||
const config = useAuthConfig(customConfig); | ||||||||||||||||||||||||||||||||||||||||||
const authData = queryClient.getQueryData([AUTH, chainID, config, serviceUrl]); | ||||||||||||||||||||||||||||||||||||||||||
const onChainNonce = authData?.data?.nonce ? BigInt(authData?.data.nonce) : BigInt('0'); | ||||||||||||||||||||||||||||||||||||||||||
const authNonce = typeof onChainNonce === 'bigint' ? onChainNonce.toString() : onChainNonce; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
const [accountNonce, setAccountNonce] = useState(onChainNonce.toString()); | ||||||||||||||||||||||||||||||||||||||||||
const currentAccountNonce = getNonceByAccount(currentAccountAddress); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Store nonce by address in accounts store | ||||||||||||||||||||||||||||||||||||||||||
const handleLocalNonce = (currentNonce) => { | ||||||||||||||||||||||||||||||||||||||||||
const storedNonce = BigInt(currentAccountNonce || 0); | ||||||||||||||||||||||||||||||||||||||||||
const localNonce = storedNonce < currentNonce ? currentNonce : storedNonce; | ||||||||||||||||||||||||||||||||||||||||||
const localNonceStr = localNonce.toString(); | ||||||||||||||||||||||||||||||||||||||||||
setNonceByAccount(currentAccountAddress, localNonceStr, 'defaultNonce'); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
setAccountNonce(localNonceStr); | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||
handleLocalNonce(onChainNonce); | ||||||||||||||||||||||||||||||||||||||||||
}, [onChainNonce]); | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+33
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be done in a memo eg
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Increment nonce after transaction signing | ||||||||||||||||||||||||||||||||||||||||||
const incrementNonce = useCallback((transactionHex) => { | ||||||||||||||||||||||||||||||||||||||||||
const localNonce = BigInt(Math.max(currentAccountNonce, Number(accountNonce))) + BigInt(1); | ||||||||||||||||||||||||||||||||||||||||||
setNonceByAccount(currentAccountAddress, localNonce.toString(), transactionHex); | ||||||||||||||||||||||||||||||||||||||||||
}, []); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
const resetNonce = () => { | ||||||||||||||||||||||||||||||||||||||||||
resetNonceByAccount(currentAccountAddress, authNonce); | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
return { accountNonce, onChainNonce: authNonce, incrementNonce, resetNonce }; | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
export default useNonceSync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import * as ReactQuery from '@tanstack/react-query'; | ||
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper'; | ||
import mockSavedAccounts from '@tests/fixtures/accounts'; | ||
import { useAccounts } from 'src/modules/account/hooks'; | ||
import { mockAuth } from '@auth/__fixtures__'; | ||
import useNonceSync from './useNonceSync'; | ||
|
||
const mockedCurrentAccount = mockSavedAccounts[0]; | ||
const mockSetNonceByAccount = jest.fn(); | ||
const mockModifiedMockAuth = { ...mockAuth, data: { ...mockAuth.data, nonce: '2' } }; | ||
|
||
jest.mock('@account/hooks/useCurrentAccount', () => ({ | ||
useCurrentAccount: jest.fn(() => [mockedCurrentAccount, jest.fn()]), | ||
})); | ||
jest.mock('@account/hooks/useAccounts'); | ||
jest.mock('@auth/hooks/queries/useAuth'); | ||
|
||
describe('useNonceSync', () => { | ||
it('renders properly', async () => { | ||
jest | ||
.spyOn(ReactQuery, 'useQueryClient') | ||
.mockReturnValue({ getQueryData: () => mockModifiedMockAuth }); | ||
useAccounts.mockReturnValue({ | ||
accounts: mockedCurrentAccount, | ||
setNonceByAccount: mockSetNonceByAccount, | ||
getNonceByAccount: jest.fn().mockReturnValue(3), | ||
}); | ||
const { result } = renderHook(() => useNonceSync(), { wrapper }); | ||
expect(result.current.accountNonce).toEqual('3'); | ||
result.current.incrementNonce(); | ||
expect(mockSetNonceByAccount).toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders properly if auth nonce is undefined and no local nonce has been previously stored', () => { | ||
jest.spyOn(ReactQuery, 'useQueryClient').mockReturnValue({ getQueryData: () => {} }); | ||
useAccounts.mockReturnValue({ | ||
accounts: mockedCurrentAccount, | ||
setNonceByAccount: mockSetNonceByAccount, | ||
getNonceByAccount: jest.fn().mockReturnValue(undefined), | ||
}); | ||
const { result } = renderHook(() => useNonceSync(), { wrapper }); | ||
expect(result.current.accountNonce).toEqual('0'); | ||
}); | ||
|
||
it("updates local nonce if it's less than on-chain nonce", () => { | ||
jest | ||
.spyOn(ReactQuery, 'useQueryClient') | ||
.mockReturnValue({ getQueryData: () => mockModifiedMockAuth }); | ||
useAccounts.mockReturnValue({ | ||
accounts: mockedCurrentAccount, | ||
setNonceByAccount: mockSetNonceByAccount, | ||
getNonceByAccount: jest.fn().mockReturnValue(1), | ||
}); | ||
const { result } = renderHook(() => useNonceSync(), { wrapper }); | ||
expect(result.current.accountNonce).toEqual('2'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The present implementation of the nonce caching is scary in the sense that there is no history clean up strategy.
Since for every multi sig transaction a user creates, we are always keeping a record of it at a point a user would have like eg 200 transactions possibly from different accounts on that device and since there is no way a transaction done at index 0 would have a nonce greater than the transaction done at index 100.
I think there should be a sanitisation of this states so as not to fill up local storage space (remember it has a max size of 10mb) since transactions HEX can be quite very lengthy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that a transaction might most likely be completed on a different device than it was initiated on plays a role in why this can be tricky to solve. However, the fix for clearing all stored nonce can play a role in this