Repository navigation
Treat uninitialized wallets as unverified in isConnected
#583
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
Draft
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/prevent-failed-xdc-wallet-init
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+422
−14
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
915c932
Initial plan
Copilot ab1cf06
Handle uninitialized wallet in isConnected so it can't break shared v…
Copilot 192b78f
Use multi-RPC provider for wallets, retry wallet init 3x with one san…
Copilot afaf58e
fix: not keep identity contract when no admin wallets are initialized…
L03TJ3 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
This file contains hidden or 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 hidden or 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,66 @@ | ||
| import Web3 from 'web3' | ||
| import MultiWallet from '../MultiWallet' | ||
| import { CeloAdminWallet } from '../CeloAdminWallet' | ||
| import { XdcAdminWallet } from '../XdcAdminWallet' | ||
| import { Web3Wallet } from '../Web3Wallet' | ||
| import conf from '../../server.config' | ||
|
|
||
| const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000' | ||
| const web3 = new Web3() | ||
| const generateWalletAddress = () => web3.eth.accounts.create().address | ||
|
|
||
| const mockIdentityContract = whitelistedRoot => ({ | ||
| methods: { | ||
| getWhitelistedRoot: () => ({ call: () => Promise.resolve(whitelistedRoot) }) | ||
| } | ||
| }) | ||
|
|
||
| describe('MultiWallet.isConnected', () => { | ||
| let account | ||
| let fuseWallet | ||
| let celoWallet | ||
| let xdcWallet | ||
| let multiWallet | ||
|
|
||
| beforeEach(() => { | ||
| account = generateWalletAddress() | ||
|
|
||
| fuseWallet = new Web3Wallet('FuseTestWallet', conf, { network: 'development' }) | ||
| celoWallet = CeloAdminWallet({}) | ||
| xdcWallet = XdcAdminWallet({}) // never initialized, so has no identityContract | ||
|
|
||
| fuseWallet.identityContract = mockIdentityContract(ADDRESS_ZERO) | ||
|
|
||
| multiWallet = new MultiWallet.constructor({ | ||
| 122: fuseWallet, | ||
| 42220: celoWallet, | ||
| 50: xdcWallet | ||
| }) | ||
| }) | ||
|
|
||
| test('should succeed when XDC is uninitialized and Celo verifies the account', async () => { | ||
| const warnSpy = jest.spyOn(xdcWallet.log, 'warn') | ||
|
|
||
| celoWallet.identityContract = mockIdentityContract(account) | ||
|
|
||
| expect(xdcWallet.identityContract).toBeUndefined() | ||
| await expect(multiWallet.isConnected(account)).resolves.toBe(true) | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('identity contract unavailable'), | ||
| expect.objectContaining({ address: account, wallet: 'XdcAdminWallet' }) | ||
| ) | ||
| }) | ||
|
|
||
| test('should not authorize when XDC is uninitialized and no other wallet verifies the account', async () => { | ||
| const warnSpy = jest.spyOn(xdcWallet.log, 'warn') | ||
|
|
||
| celoWallet.identityContract = mockIdentityContract(ADDRESS_ZERO) | ||
|
|
||
| expect(xdcWallet.identityContract).toBeUndefined() | ||
| await expect(multiWallet.isConnected(account)).resolves.toBe(false) | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('identity contract unavailable'), | ||
| expect.objectContaining({ address: account, wallet: 'XdcAdminWallet' }) | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or 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,89 @@ | ||
| jest.mock('../../db/mongo-db') | ||
| jest.mock('../../../imports/slack', () => ({ sendSlackAlert: jest.fn() })) | ||
| jest.mock('../../utils/tx-manager', () => ({ | ||
| getManager: () => ({ createListIfNotExists: jest.fn().mockResolvedValue() }) | ||
| })) | ||
| jest.mock('web3', () => Object.assign(jest.fn(), { providers: jest.requireActual('web3').providers })) | ||
|
|
||
| import Web3 from 'web3' | ||
| import MultiWallet from '../MultiWallet' | ||
| import { Web3Wallet } from '../Web3Wallet' | ||
| import conf from '../../server.config' | ||
|
|
||
| const ZERO = '0x0000000000000000000000000000000000000000' | ||
| const account = '0x1111111111111111111111111111111111111111' | ||
| const identity = root => ({ methods: { getWhitelistedRoot: () => ({ call: () => Promise.resolve(root) }) } }) | ||
|
|
||
| test('late initialization failure cannot interfere with or authorize verification', async () => { | ||
| let rejectBalance | ||
| let signalLateBalance | ||
| const lateBalance = new Promise(resolve => { | ||
| signalLateBalance = resolve | ||
| }) | ||
| const failedBalance = new Promise((resolve, reject) => { | ||
| rejectBalance = reject | ||
| }) | ||
| const getBalance = jest | ||
| .fn() | ||
| .mockResolvedValue('1000000000000000000') | ||
| .mockImplementationOnce(() => Promise.resolve('1000000000000000000')) | ||
| .mockImplementationOnce(() => Promise.resolve('1000000000000000000')) | ||
| .mockImplementationOnce(() => Promise.resolve('1000000000000000000')) | ||
| .mockImplementationOnce(() => { | ||
| signalLateBalance() | ||
| return failedBalance | ||
| }) | ||
| const contract = jest.fn().mockImplementation(() => ({ | ||
| methods: { | ||
| adminToppingAmount: () => ({ call: () => Promise.resolve('0') }), | ||
| getWhitelistedRoot: () => ({ call: () => Promise.reject(new Error('XDC unavailable')) }) | ||
| } | ||
| })) | ||
| Web3.mockImplementation(() => ({ | ||
| eth: { | ||
| getBalance, | ||
| getTransactionCount: jest.fn().mockResolvedValue(0), | ||
| Contract: contract, | ||
| accounts: { privateKeyToAccount: () => ({ address: account }), wallet: { add: jest.fn() } } | ||
| } | ||
| })) | ||
|
|
||
| const options = { network: 'production-xdc', ethereum: { network_id: 50 }, initRetryDelay: 0 } | ||
| const xdc = new Web3Wallet( | ||
| 'XdcAdminWallet', | ||
| { ...conf, env: 'test', mnemonic: null, privateKey: '0x01', topAdminsOnStartup: false }, | ||
| options, | ||
| false | ||
| ) | ||
| const celo = new Web3Wallet('CeloAdminWallet', { ...conf, env: 'test' }, options, false) | ||
| celo.identityContract = identity(account) | ||
| xdc.identityContract = identity(account) // stale result from a previous initialization | ||
| const multi = new MultiWallet.constructor({ 50: xdc, 42220: celo }) | ||
| jest.spyOn(xdc, 'getWeb3TransportProvider').mockReturnValue({}) | ||
| jest.spyOn(xdc, 'supportsEIP1559').mockResolvedValue(true) | ||
| jest.spyOn(xdc, 'isVerifiedAdmin').mockResolvedValue(true) | ||
|
|
||
| const ready = xdc.ready | ||
| await lateBalance | ||
| expect(contract.mock.calls.length).toBeGreaterThanOrEqual(3) | ||
| expect(xdc.identityContract).toBeUndefined() | ||
| await expect(multi.isConnected(account)).resolves.toBe(true) | ||
| rejectBalance(new Error('late RPC failure')) | ||
| // The subsequent attempts fail earlier, before creating contracts. | ||
| getBalance.mockImplementation(() => Promise.reject(new Error('RPC unavailable'))) | ||
| await expect(ready).resolves.toBe(false) | ||
| await expect(multi.isConnected(account)).resolves.toBe(true) | ||
| celo.identityContract = identity(ZERO) | ||
| await expect(multi.isConnected(account)).resolves.toBe(false) | ||
|
|
||
| // A later successful initialization publishes XDC's identity for verification. | ||
| getBalance.mockResolvedValue('1000000000000000000') | ||
| contract.mockImplementation(() => ({ | ||
| methods: { | ||
| adminToppingAmount: () => ({ call: () => Promise.resolve('0') }), | ||
| getWhitelistedRoot: () => ({ call: () => Promise.resolve(account) }) | ||
| } | ||
| })) | ||
| await expect(xdc.ready).resolves.toBe(true) | ||
| await expect(multi.isConnected(account)).resolves.toBe(true) | ||
| }) |
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.
this shouldnt be restored