|
| 1 | +/** |
| 2 | + * auth precompile (0x…100D) — account queries against a live Sei chain. |
| 3 | + * |
| 4 | + * All four methods are views. The parity oracle is the auth module's own Query |
| 5 | + * service; the Go executor has no delegatecall guard (unlike staking/gov), so |
| 6 | + * CALL, STATICCALL and DELEGATECALL all succeed. |
| 7 | + * |
| 8 | + * `nextAccountNumber` queries a keeper method that increments the persisted |
| 9 | + * counter, which the executor neutralises by branching a CacheContext. That |
| 10 | + * discard is not observable from here — an `eth_call` commits nothing either |
| 11 | + * way — so it stays pinned by auth_test.go:TestNextAccountNumber, and this |
| 12 | + * spec only asserts the value the chain actually reports. |
| 13 | + */ |
| 14 | +import { ethers } from 'ethers'; |
| 15 | +import { expect } from 'chai'; |
| 16 | +import { seiRpc, rawSei } from '../utils/chainUtils'; |
| 17 | +import { EvmAccount } from '../utils/evmUtils'; |
| 18 | +import { |
| 19 | + authAccount, |
| 20 | + authAccounts, |
| 21 | + authNextAccountNumber, |
| 22 | + authParams, |
| 23 | + baseAccount, |
| 24 | +} from '../utils/moduleQueries'; |
| 25 | +import { |
| 26 | + PRECOMPILE_ADDRESSES, |
| 27 | + precompileContract, |
| 28 | + precompileInterface, |
| 29 | + callerContract, |
| 30 | + expectExecutionReverted, |
| 31 | +} from '../utils/precompileUtils'; |
| 32 | +import { readRuntimeState, RuntimeState } from '../utils/testUtils'; |
| 33 | + |
| 34 | +const empty = new Uint8Array(); |
| 35 | + |
| 36 | +const uint64 = (v: string | number | bigint | undefined): bigint => BigInt(v ?? 0); |
| 37 | + |
| 38 | +describe('auth precompile (0x100D)', function () { |
| 39 | + this.timeout(120 * 1000); |
| 40 | + |
| 41 | + const provider = seiRpc(); |
| 42 | + const authIface = precompileInterface('auth'); |
| 43 | + |
| 44 | + let runtime: RuntimeState; |
| 45 | + let admin: EvmAccount; |
| 46 | + let auth: ethers.Contract; |
| 47 | + let caller: ethers.Contract; |
| 48 | + |
| 49 | + before(() => { |
| 50 | + runtime = readRuntimeState(); |
| 51 | + admin = EvmAccount.fromMnemonic(runtime.funded.adminMnemonic, provider); |
| 52 | + auth = precompileContract('auth', admin.wallet); |
| 53 | + caller = callerContract(runtime, admin.wallet); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('happy path & state parity', () => { |
| 57 | + it('account(admin) matches the module address, account_number and sequence', async () => { |
| 58 | + const sei = admin.seiAddress(); |
| 59 | + const [via, stored] = await Promise.all([ |
| 60 | + auth.account(admin.address), |
| 61 | + authAccount(sei), |
| 62 | + ]); |
| 63 | + expect(stored, 'the auth module must know the admin account').to.not.equal(undefined); |
| 64 | + const expected = baseAccount(stored!); |
| 65 | + expect(via.accountAddress).to.equal(sei); |
| 66 | + expect(via.accountAddress).to.equal(expected.address); |
| 67 | + expect(via.accountNumber).to.equal(uint64(expected.account_number)); |
| 68 | + expect(via.sequence).to.equal(uint64(expected.sequence)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('accounts(empty) returns the same first page as the auth module', async () => { |
| 72 | + const [page, stored] = await Promise.all([auth.accounts(empty), authAccounts()]); |
| 73 | + expect(page.accounts.length, 'first page of auth accounts').to.be.greaterThan(0); |
| 74 | + // Both read the first page with the module's default page size, so the |
| 75 | + // rows must line up in order, not merely in count. |
| 76 | + expect( |
| 77 | + [...page.accounts].map((a: { accountAddress: string }) => a.accountAddress), |
| 78 | + 'accounts() first page vs the auth module', |
| 79 | + ).to.deep.equal(stored.map(a => baseAccount(a).address)); |
| 80 | + }); |
| 81 | + |
| 82 | + it('params() matches the auth module', async () => { |
| 83 | + const [via, p] = await Promise.all([auth.params(), authParams()]); |
| 84 | + expect(p, 'the auth module must report params').to.not.equal(undefined); |
| 85 | + expect(via.maxMemoCharacters).to.equal(uint64(p!.max_memo_characters)); |
| 86 | + expect(via.txSigLimit).to.equal(uint64(p!.tx_sig_limit)); |
| 87 | + expect(via.txSizeCostPerByte).to.equal(uint64(p!.tx_size_cost_per_byte)); |
| 88 | + expect(via.sigVerifyCostEd25519).to.equal(uint64(p!.sig_verify_cost_ed25519)); |
| 89 | + expect(via.sigVerifyCostSecp256k1).to.equal(uint64(p!.sig_verify_cost_secp256k1)); |
| 90 | + expect(via.disableSeqnoCheck).to.equal(Boolean(p!.disable_seqno_check)); |
| 91 | + }); |
| 92 | + |
| 93 | + it('nextAccountNumber() matches the auth module and is past the admin account', async () => { |
| 94 | + const [count, next, account] = await Promise.all([ |
| 95 | + auth.nextAccountNumber() as Promise<bigint>, |
| 96 | + authNextAccountNumber(), |
| 97 | + auth.account(admin.address), |
| 98 | + ]); |
| 99 | + expect(count, 'nextAccountNumber vs the auth module').to.equal(uint64(next)); |
| 100 | + expect(count > account.accountNumber, 'next number is past every issued number').to.equal( |
| 101 | + true, |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('error handling', () => { |
| 107 | + it('account of a never-associated random EVM address reverts (eth_call)', async () => { |
| 108 | + await expectExecutionReverted( |
| 109 | + auth.account(EvmAccount.random(provider).address), |
| 110 | + 'auth.account of a never-associated EVM address', |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('view methods reject value (eth_call with value 0x1)', async () => { |
| 115 | + const cases: Array<[string, unknown[]]> = [ |
| 116 | + ['account', [admin.address]], |
| 117 | + ['accounts', [empty]], |
| 118 | + ['params', []], |
| 119 | + ['nextAccountNumber', []], |
| 120 | + ]; |
| 121 | + for (const [method, args] of cases) { |
| 122 | + const envelope = await rawSei('eth_call', [ |
| 123 | + { |
| 124 | + from: admin.address, |
| 125 | + to: PRECOMPILE_ADDRESSES.auth, |
| 126 | + data: authIface.encodeFunctionData(method, args), |
| 127 | + value: '0x1', |
| 128 | + }, |
| 129 | + 'latest', |
| 130 | + ]); |
| 131 | + expect(envelope.error, `${method} with value must revert`).to.not.equal(undefined); |
| 132 | + expect(envelope.error!.message).to.match(/execution reverted|revert/i); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('dispatch semantics (via PrecompileCaller)', () => { |
| 138 | + it('account and params respond through a real CALL from contract bytecode', async () => { |
| 139 | + const accountData = authIface.encodeFunctionData('account', [admin.address]); |
| 140 | + const paramsData = authIface.encodeFunctionData('params', []); |
| 141 | + const [accountRet, paramsRet, directAccount, directParams] = await Promise.all([ |
| 142 | + caller.callTarget.staticCall(PRECOMPILE_ADDRESSES.auth, accountData) as Promise<string>, |
| 143 | + caller.callTarget.staticCall(PRECOMPILE_ADDRESSES.auth, paramsData) as Promise<string>, |
| 144 | + auth.account(admin.address), |
| 145 | + auth.params(), |
| 146 | + ]); |
| 147 | + const [decodedAccount] = authIface.decodeFunctionResult('account', accountRet); |
| 148 | + const [decodedParams] = authIface.decodeFunctionResult('params', paramsRet); |
| 149 | + expect(decodedAccount.accountAddress).to.equal(directAccount.accountAddress); |
| 150 | + expect(decodedAccount.accountNumber).to.equal(directAccount.accountNumber); |
| 151 | + expect(decodedAccount.sequence).to.equal(directAccount.sequence); |
| 152 | + expect(decodedParams.maxMemoCharacters).to.equal(directParams.maxMemoCharacters); |
| 153 | + expect(decodedParams.txSigLimit).to.equal(directParams.txSigLimit); |
| 154 | + }); |
| 155 | + |
| 156 | + it('account and params respond under STATICCALL', async () => { |
| 157 | + const accountData = authIface.encodeFunctionData('account', [admin.address]); |
| 158 | + const paramsData = authIface.encodeFunctionData('params', []); |
| 159 | + const [accountRet, paramsRet, directAccount, directParams] = await Promise.all([ |
| 160 | + caller.staticcallTarget.staticCall( |
| 161 | + PRECOMPILE_ADDRESSES.auth, |
| 162 | + accountData, |
| 163 | + ) as Promise<string>, |
| 164 | + caller.staticcallTarget.staticCall( |
| 165 | + PRECOMPILE_ADDRESSES.auth, |
| 166 | + paramsData, |
| 167 | + ) as Promise<string>, |
| 168 | + auth.account(admin.address), |
| 169 | + auth.params(), |
| 170 | + ]); |
| 171 | + const [decodedAccount] = authIface.decodeFunctionResult('account', accountRet); |
| 172 | + const [decodedParams] = authIface.decodeFunctionResult('params', paramsRet); |
| 173 | + expect(decodedAccount.accountAddress).to.equal(directAccount.accountAddress); |
| 174 | + expect(decodedParams.maxMemoCharacters).to.equal(directParams.maxMemoCharacters); |
| 175 | + }); |
| 176 | + |
| 177 | + it('responds under DELEGATECALL (auth has no delegatecall guard)', async () => { |
| 178 | + const data = authIface.encodeFunctionData('account', [admin.address]); |
| 179 | + const ret: string = await caller.delegatecallTarget.staticCall( |
| 180 | + PRECOMPILE_ADDRESSES.auth, |
| 181 | + data, |
| 182 | + ); |
| 183 | + const [decoded] = authIface.decodeFunctionResult('account', ret); |
| 184 | + expect(decoded.accountAddress).to.equal(admin.seiAddress()); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments