on master help sign in
Merge pull request #184 from urbit/gas-limits

Add tool for finding real-world gas usage

commit60d3cb76e3311708fb2889fbcf581f382fd3dbea authorFang <Fang-@users.noreply.github.com> dateMon 8 Jul 2019, 19:23 (7 years ago) parent1a0993d8e87674
2 files changed+129−5
find-gas-usage.js+114 src/lib/constants.js+15−5
find-gas-usage.jsnew file+114
@@ -0,0 +1,114 @@
1+// usage: tweak constants at head of file, then run "node find-gas-usage.js"
2+// for very common events/calls, you may need to use a smaller TO-FROM
3+// range to keep web3 from yelling at you
4+
5+const ORIGIN = 6784800; // launch ceremony begins
6+const PUBLIC = 7033765; // azimuth becomes public
7+const LATEST = 'latest';
8+const AZIMUTH_ADDRESS = '0x223c067f8cf28ae173ee5cafea60ca44c335fecb';
9+const ENDPOINT =
10+ 'https://mainnet.infura.io/v3/196a7f37c7d54211b4a07904ec73ad87';
11+
12+const FROM = ORIGIN;
13+const TO = LATEST;
14+
15+const VENT = 'ChangedKeys';
16+const FUNC = '0x4447e48c'; // configureKeys
17+const ARGS = 5;
18+
19+// const VENT = 'Spawned';
20+// const FUNC = '0xa0d3253f'; // spawn
21+// const ARGS = 2;
22+
23+// const VENT = 'OwnerChanged';
24+// const FUNC = '0x1e79a85b'; // transferPoint
25+// const ARGS = 3;
26+
27+// const VENT = 'ChangedSpawnProxy';
28+// const FUNC = '0xae326221'; // setSpawnProxy
29+// const ARGS = 2;
30+
31+// const VENT = 'ChangedManagementProxy';
32+// const FUNC = '0x8866bb2c'; // setManagementProxy
33+// const ARGS = 2;
34+
35+// const VENT = 'ChangedTransferProxy';
36+// const FUNC = '0x2c7ba564'; // setTransferProxy
37+// const ARGS = 2;
38+
39+// const VENT = 'ChangedVotingProxy';
40+// const FUNC = '0xa60e8bd6'; // setVotingProxy
41+// const ARGS = 2;
42+
43+// const VENT = 'EscapeRequested';
44+// const FUNC = '0xbf5772b9'; // escape
45+// const ARGS = 2;
46+
47+// const VENT = 'EscapeCanceled';
48+// const FUNC = '0xc6d761d4'; // cancelEscape
49+// const ARGS = 1;
50+
51+// const VENT = 'EscapeAccepted';
52+// const FUNC = '0xc1b9d98b'; // adopt
53+// const ARGS = 1;
54+
55+// const VENT = 'EscapeCanceled';
56+// const FUNC = '0xbbe21ca5'; // reject
57+// const ARGS = 1;
58+
59+// const VENT = 'LostSponsor';
60+// const FUNC = '0x073a7804'; // detach
61+// const ARGS = 1;
62+
63+const LENT = 64 * ARGS + FUNC.length;
64+
65+const Web3 = require('web3');
66+const azimuth = require('azimuth-js');
67+
68+(async () => {
69+ const web3 = new Web3(new Web3.providers.HttpProvider(ENDPOINT));
70+ const contracts = await azimuth.initContractsPartial(web3, AZIMUTH_ADDRESS);
71+
72+ const keyEvents = await contracts.azimuth.getPastEvents(VENT, {
73+ fromBlock: FROM,
74+ toBlock: TO,
75+ });
76+
77+ const txsBatch = new web3.BatchRequest();
78+ keyEvents.map(vent => {
79+ txsBatch.add(
80+ web3.eth.getTransaction.request(vent.transactionHash, (err, res) => {})
81+ );
82+ });
83+ const txs = await txsBatch.execute();
84+ const keyTxs = txs.response.filter(tx => {
85+ const head = tx.input.slice(0, 10) === FUNC;
86+ const size = tx.input.length === LENT;
87+ if (head && !size) {
88+ console.log('weird inputs', tx.hash);
89+ }
90+ return head && size;
91+ });
92+
93+ const receiptsBatch = new web3.BatchRequest();
94+ keyTxs.map(tx => {
95+ receiptsBatch.add(
96+ web3.eth.getTransactionReceipt.request(tx.hash, (err, res) => {})
97+ );
98+ });
99+ const recs = await receiptsBatch.execute();
100+
101+ let usage = {};
102+ recs.response.map(rec => {
103+ if (rec.status !== true) {
104+ console.log('rejecting', rec.gasUsed);
105+ return;
106+ }
107+ if (rec.gasUsed in usage) {
108+ usage[rec.gasUsed]++;
109+ } else {
110+ usage[rec.gasUsed] = 1;
111+ }
112+ });
113+ console.log(usage);
114+})();
src/lib/constants.js+15−5
@@ -13,12 +13,22 @@ const GALAXY_ENTROPY_BITS = 384;
1313
1414const SEED_ENTROPY_BITS = 128;
1515
16−//TODO properly research these, move into azimuth-js
16+//TODO move into azimuth-js
1717const GAS_LIMITS = {
18− TRANSFER: 500000,
19− CONFIGURE_KEYS: 150000,
20− SET_PROXY: 200000,
21− GIFT_PLANET: 400000, //TODO can maybe do 370000, see also commit 8c0ecaaa
18+ SPAWN: 250000,
19+ TRANSFER: 490000,
20+ CONFIGURE_KEYS: 100000,
21+ SET_PROXY: 119000,
22+ //
23+ ESCAPE: 115000, //NOTE low sample size
24+ ADOPT: 100000, //NOTE low sample size
25+ CANCEL_ESCAPE: 200000, //NOTE no samples
26+ REJECT: 200000, //NOTE no samples
27+ DETACH: 200000, //NOTE no samples
28+ //
29+ GIFT_PLANET: 400000, //NOTE low sample size
30+ //
31+ DEFAULT: 500000,
2232};
2333
2434// TODO: this is walletgen-ui specific, move into a wallet router later