|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { Address } from '@hyperlane-xyz/utils'; |
| 4 | + |
| 5 | +import type { EthersLikeProvider } from '../deploy/proxy.js'; |
| 6 | + |
| 7 | +import { isAddressActive } from './contracts.js'; |
| 8 | + |
| 9 | +const ADDRESS: Address = '0xa7eccdb9be08178f896c26b7bbd8c3d4e844d9ba'; |
| 10 | +const CONTRACT_CODE = '0x60806040'; |
| 11 | + |
| 12 | +interface NonceProviderCalls { |
| 13 | + getTransactionCount: number; |
| 14 | +} |
| 15 | + |
| 16 | +interface ActivationProviderCalls { |
| 17 | + isAccountActive: number; |
| 18 | +} |
| 19 | + |
| 20 | +/** Provider double for chains whose liveness is inferred from the nonce. */ |
| 21 | +function nonceProvider( |
| 22 | + code: string, |
| 23 | + txnCount: number, |
| 24 | +): { provider: EthersLikeProvider; calls: NonceProviderCalls } { |
| 25 | + const calls: NonceProviderCalls = { getTransactionCount: 0 }; |
| 26 | + const double = { |
| 27 | + getCode: async () => code, |
| 28 | + getTransactionCount: async () => { |
| 29 | + calls.getTransactionCount += 1; |
| 30 | + return txnCount; |
| 31 | + }, |
| 32 | + }; |
| 33 | + // CAST: explicit test double exercising only the two methods isAddressActive calls. |
| 34 | + return { provider: double as unknown as EthersLikeProvider, calls }; |
| 35 | +} |
| 36 | + |
| 37 | +/** Provider double for chains that answer activation directly (Tron). */ |
| 38 | +function activationProvider( |
| 39 | + code: string, |
| 40 | + activation: boolean | (() => Promise<boolean>), |
| 41 | +): { provider: EthersLikeProvider; calls: ActivationProviderCalls } { |
| 42 | + const calls: ActivationProviderCalls = { isAccountActive: 0 }; |
| 43 | + const double = { |
| 44 | + getCode: async () => code, |
| 45 | + getTransactionCount: async () => { |
| 46 | + throw new Error('getTransactionCount must not be consulted'); |
| 47 | + }, |
| 48 | + isAccountActive: async () => { |
| 49 | + calls.isAccountActive += 1; |
| 50 | + return typeof activation === 'boolean' ? activation : activation(); |
| 51 | + }, |
| 52 | + }; |
| 53 | + // CAST: explicit test double exercising only the methods isAddressActive calls. |
| 54 | + return { provider: double as unknown as EthersLikeProvider, calls }; |
| 55 | +} |
| 56 | + |
| 57 | +describe('isAddressActive', () => { |
| 58 | + describe('providers without an activation check', () => { |
| 59 | + it('returns false for an EOA with no code and a zero nonce', async () => { |
| 60 | + const { provider } = nonceProvider('0x', 0); |
| 61 | + |
| 62 | + expect(await isAddressActive(provider, ADDRESS)).to.be.false; |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns true for an EOA with a non-zero nonce', async () => { |
| 66 | + const { provider } = nonceProvider('0x', 1); |
| 67 | + |
| 68 | + expect(await isAddressActive(provider, ADDRESS)).to.be.true; |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns true for a contract regardless of nonce', async () => { |
| 72 | + const { provider } = nonceProvider(CONTRACT_CODE, 0); |
| 73 | + |
| 74 | + expect(await isAddressActive(provider, ADDRESS)).to.be.true; |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('providers with an activation check', () => { |
| 79 | + it('returns true for an activated EOA with no code and no nonce', async () => { |
| 80 | + const { provider, calls } = activationProvider('0x', true); |
| 81 | + |
| 82 | + expect(await isAddressActive(provider, ADDRESS)).to.be.true; |
| 83 | + expect(calls.isAccountActive).to.equal(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns false for an unactivated EOA', async () => { |
| 87 | + const { provider, calls } = activationProvider('0x', false); |
| 88 | + |
| 89 | + expect(await isAddressActive(provider, ADDRESS)).to.be.false; |
| 90 | + expect(calls.isAccountActive).to.equal(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns true for a contract without consulting the activation check', async () => { |
| 94 | + const { provider, calls } = activationProvider(CONTRACT_CODE, false); |
| 95 | + |
| 96 | + expect(await isAddressActive(provider, ADDRESS)).to.be.true; |
| 97 | + expect(calls.isAccountActive).to.equal(0); |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns true for a contract even when the activation check would fail', async () => { |
| 101 | + const { provider, calls } = activationProvider( |
| 102 | + CONTRACT_CODE, |
| 103 | + async () => { |
| 104 | + throw new Error('the method wallet/getaccount is not available'); |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + expect(await isAddressActive(provider, ADDRESS)).to.be.true; |
| 109 | + expect(calls.isAccountActive).to.equal(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it('propagates an activation-check failure instead of reporting inactive', async () => { |
| 113 | + const failure = new Error('bad response (status=503)'); |
| 114 | + const { provider } = activationProvider('0x', async () => { |
| 115 | + throw failure; |
| 116 | + }); |
| 117 | + |
| 118 | + let thrown: unknown; |
| 119 | + try { |
| 120 | + await isAddressActive(provider, ADDRESS); |
| 121 | + } catch (error: unknown) { |
| 122 | + thrown = error; |
| 123 | + } |
| 124 | + |
| 125 | + expect(thrown).to.equal(failure); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments