Skip to content

Commit

Permalink
fix access list (#809)
Browse files Browse the repository at this point in the history
* polish

* use new types

* update

* update

* update

* update

* update

* runtime api works

* fix build

* add types bundle

* add api derive

* update

* fix query old block

* fix

* fix

* fix

* update

* temp set acesslist as any

* test remove substrate runtime

* update

* fix accessList

* update

* fix lint

* update

* update
  • Loading branch information
shunjizhan authored Sep 1, 2023
1 parent e7c4064 commit a31619f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dotenv.config();

const ACALA_NODE_URL = 'wss://acala-rpc.aca-api.network';
const ACALA_SUBQL = 'https://subql-query-acala.aca-api.network';
const endpoint = process.env.ENDPOINT_URL || 'ws://127.0.0.1:9944';

describe('connect random', () => {
it('should throw error', async () => {
Expand All @@ -15,7 +16,7 @@ describe('connect random', () => {
});
});

describe('getReceiptAtBlock', async () => {
describe.concurrent('getReceiptAtBlock', async () => {
const provider = EvmRpcProvider.from(ACALA_NODE_URL, { subqlUrl: ACALA_SUBQL });
await provider.isReady();

Expand Down Expand Up @@ -95,3 +96,55 @@ describe.skip('all cache', async () => {
expect(blockData1).to.deep.equal(blockData2);
});
});

describe.concurrent('rpc test', async () => {
const provider = EvmRpcProvider.from(endpoint);
await provider.isReady();

afterAll(async () => {
await sleep(5000);
await provider.disconnect();
});

it('chainId', async () => {
const result = await provider.chainId();
expect(result).to.equal(595);
});

it('getTransactionCount', async () => {
const result = await provider.getTransactionCount('0x33f9440ff970496a09e391f3773a66f1e98eb13c', 'latest');
expect(result).not.toBeUndefined();
});

it('getCode', async () => {
const result1 = await provider.getCode('0x1000000000000000000000000000000000000802');

expect(result1).to.equal('0x');

const result2 = await provider.getCode('0x0000000000000000000000000000000000000802');

expect(result2.length > 2).to.be.true;
});

it('call', async () => {
const result = await provider.call({
data: '0x70a0823100000000000000000000000033f9440ff970496a09e391f3773a66f1e98eb13c',
from: '0x33f9440ff970496a09e391f3773a66f1e98eb13c',
to: '0xbffb25b73c6a0581a28988ce34c9f240d525b152',
});

expect(result).not.toBeUndefined();
});

it('getBalance', async () => {
const result = await provider.getBalance('0x33f9440ff970496a09e391f3773a66f1e98eb13c');

expect(result.toString()).to.equal('0');
});

it('getBlockByNumber', async () => {
await expect(
provider._getBlockHeader('0xff2d5d74f16df09b810225ffd9e1442250914ae6de9459477118d675713c732c')
).rejects.toThrow('header not found');
});
});
34 changes: 26 additions & 8 deletions packages/eth-providers/src/__tests__/e2e/json-rpc-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { sleep } from '../../utils';
import echoJson from '../abis/Echo.json';
import erc20Json from '../abis/IERC20.json';

const gasOverride = {
gasPrice: '0x616dc303ea',
gasLimit: '0x329b140',
};

const localEthRpc = process.env.ETH_RPC || 'http://localhost:8545';

describe('JsonRpcProvider', async () => {
Expand Down Expand Up @@ -143,7 +138,7 @@ describe('JsonRpcProvider', async () => {

it('deploy and call contract', async () => {
const echoFactory = new ContractFactory(echoJson.abi, echoJson.bytecode, wallet);
const echo = await echoFactory.deploy(gasOverride);
const echo = await echoFactory.deploy();
await echo.deployed();

expect(await echo.echo()).to.equal('Deployed successfully!');
Expand All @@ -154,6 +149,29 @@ describe('JsonRpcProvider', async () => {
await (await echo.scream('hello Vegito!')).wait();
expect(await echo.echo()).to.equal('hello Vegito!');
});

// TODO: enable me when we support type 1 tx
it.skip('call contract with access list', async () => {
const echoFactory = new ContractFactory(echoJson.abi, echoJson.bytecode, wallet);
const echo = await echoFactory.deploy();
await echo.deployed();

expect(await echo.echo()).to.equal('Deployed successfully!');

const receipt1 = await (await echo.scream('hello Gogeta!')).wait();
expect(await echo.echo()).to.equal('hello Gogeta!');

const accessList = [{
address: echo.address,
storageKeys: [
// ...
],
}];
const receipt2 = await (await echo.scream('hello Vegito!', { accessList, type: 1 })).wait();
expect(await echo.echo()).to.equal('hello Vegito!');

expect(receipt1.gasUsed).to.be.gt(receipt2.gasUsed);
});
});

describe('subscription', () => {
Expand All @@ -180,7 +198,7 @@ describe('JsonRpcProvider', async () => {
// TODO: need to setup whole stack
it.skip('subscribe to filter', async () => {
// const tokenFactory = new ContractFactory(erc20Json.abi, erc20Json.bytecode, wallet);
// const token = await tokenFactory.deploy(gasOverride);
// const token = await tokenFactory.deploy();
// await token.deployed();

// const _data = new Promise(resolve => {
Expand All @@ -192,7 +210,7 @@ describe('JsonRpcProvider', async () => {
// providerLocal.on(filter, resolve);
// });

// await token.transfer(someOne, parseEther('0.01'), gasOverride);
// await token.transfer(someOne, parseEther('0.01'));

// const data = await _data;

Expand Down
59 changes: 0 additions & 59 deletions packages/eth-providers/src/__tests__/e2e/rpc.test.ts

This file was deleted.

30 changes: 14 additions & 16 deletions packages/eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TransactionResponse,
} from '@ethersproject/abstract-provider';
import { AcalaEvmTX, checkSignatureType, parseTransaction } from '@acala-network/eth-transactions';
import { AccessListish } from 'ethers/lib/utils';
import { AccessList, accessListify } from 'ethers/lib/utils';
import { AccountId, H160, Header } from '@polkadot/types/interfaces';
import { ApiPromise } from '@polkadot/api';
import { AsyncAction } from 'rxjs/internal/scheduler/AsyncAction';
Expand Down Expand Up @@ -149,7 +149,7 @@ export interface SubstrateEvmCallRequest {
storageLimit?: Numberish;
value?: Numberish;
data?: string;
accessList?: AccessListish;
accessList?: AccessList;
}

export interface partialTX {
Expand Down Expand Up @@ -823,8 +823,8 @@ export abstract class BaseProvider extends AbstractProvider {
const estimate = true;

const res = to
? await api.call.evmRuntimeRPCApi.call(from, to, data, value, gasLimit, storageLimit, accessList as any, estimate) // FIXME: fix and test accessList
: await api.call.evmRuntimeRPCApi.create(from, data, value, gasLimit, storageLimit, accessList as any, estimate);
? await api.call.evmRuntimeRPCApi.call(from, to, data, value, gasLimit, storageLimit, accessList, estimate)
: await api.call.evmRuntimeRPCApi.create(from, data, value, gasLimit, storageLimit, accessList, estimate);

const { ok, err } = res.toJSON() as CallInfo;
if (!ok) {
Expand Down Expand Up @@ -933,7 +933,7 @@ export abstract class BaseProvider extends AbstractProvider {
toBN(BigNumber.from(tx.value ?? 0)),
toBN(gasLimit),
toBN(usedStorage.isNegative() ? 0 : usedStorage),
(tx.accessList as any) || [],
accessListify(tx.accessList ?? []),
] as const;

const callParams = [tx.to!, ...createParams] as const;
Expand Down Expand Up @@ -1199,7 +1199,7 @@ export abstract class BaseProvider extends AbstractProvider {
storageLimit: bigint;
validUntil: bigint;
tip: bigint;
accessList?: [string, string[]][];
accessList: AccessList;
v2: boolean;
} => {
let gasLimit = 0n;
Expand Down Expand Up @@ -1272,7 +1272,7 @@ export abstract class BaseProvider extends AbstractProvider {
}
} else if (ethTx.type === 1 || ethTx.type === 2) {
return logger.throwError(
`unsupported transaction type: ${ethTx.type}, please use legacy or EIP-712 instead. More info about EVM+ gas: <TODO: insert link here>`,
`unsupported transaction type: ${ethTx.type}, please use legacy or EIP-712 instead.`,
Logger.errors.UNSUPPORTED_OPERATION,
{
operation: '_getSubstrateGasParams',
Expand All @@ -1281,14 +1281,12 @@ export abstract class BaseProvider extends AbstractProvider {
);
}

const accessList = ethTx.accessList?.map((set) => [set.address, set.storageKeys] as [string, string[]]);

return {
gasLimit,
storageLimit,
validUntil,
tip,
accessList,
accessList: ethTx.accessList ?? [],
v2,
};
};
Expand Down Expand Up @@ -1328,16 +1326,16 @@ export abstract class BaseProvider extends AbstractProvider {
ethTx.value.toString(),
ethTx.gasPrice?.toBigInt(),
ethTx.gasLimit.toBigInt(),
accessList as any || [] // FIXME: fix and test accessList
accessList,
)
: this.api.tx.evm.ethCall(
ethTx.to ? { Call: ethTx.to } : { Create: null },
ethTx.data,
ethTx.value.toString(),
gasLimit,
storageLimit,
accessList as any || [],
validUntil
accessList,
validUntil,
);

const subAddr = await this.getSubstrateAddress(ethTx.from);
Expand Down Expand Up @@ -1977,15 +1975,15 @@ export abstract class BaseProvider extends AbstractProvider {
_pollBlocks = async (filterInfo: BlockPollFilter): Promise<string[]> => {
const curBlockNumber = await this.getBlockNumber();

const newBlockHashes = [];
const newBlockHashesPending: Promise<string>[] = [];
for (let blockNum = filterInfo.lastPollBlockNumber + 1; blockNum <= curBlockNumber; blockNum++) {
newBlockHashes.push(this._getBlockHash(blockNum));
newBlockHashesPending.push(this._getBlockHash(blockNum));
}

filterInfo.lastPollBlockNumber = curBlockNumber;
filterInfo.lastPollTimestamp = Date.now();

return Promise.all(newBlockHashes);
return Promise.all(newBlockHashesPending);
};

poll = async (id: string, logsOnly = false): Promise<string[] | Log[]> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/eth-providers/src/utils/parseBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
WeightV2,
} from '@polkadot/types/interfaces';
import { Formatter } from '@ethersproject/providers';
import { FrameSupportDispatchDispatchInfo, FrameSystemEventRecord } from '@polkadot/types/lookup';
import { FrameSystemEventRecord } from '@polkadot/types/lookup';
import {
FullReceipt,
findEvmEvent,
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a31619f

Please sign in to comment.