UNPKG

11.1 kBTypeScriptView Raw
1declare module 'ethereumjs-vm' {
2 import BN = require('bn.js');
3 type Common = any; // from ethereumjs-common
4 type Account = any; // from ethereumjs-account
5 type Blockchain = any; // from ethereumjs-blockchain
6
7 export default class VM {
8 opts: VmOpts;
9 stateManager: StateManager;
10 constructor(opts: VmOpts);
11 runCall(opts: RunCallOpts): Promise<EVMResult>;
12 }
13
14 interface VmOpts {
15 chain?: string;
16 hardfork?: string;
17 stateManager?: StateManager;
18 state?: any;
19 blockchain?: Blockchain;
20 activatePrecompiles?: boolean;
21 allowUnlimitedContractSize?: boolean;
22 common?: Common;
23 }
24
25 interface RunCallOpts {
26 block?: any;
27 gasPrice?: Buffer;
28 origin?: Buffer;
29 caller?: Buffer;
30 gasLimit?: Buffer;
31 to?: Buffer;
32 value?: Buffer;
33 data?: Buffer;
34 code?: Buffer;
35 depth?: number;
36 compiled?: boolean;
37 static?: boolean;
38 salt?: Buffer;
39 selfdestruct?: { [k: string]: boolean };
40 delegatecall?: boolean;
41 }
42
43 interface EVMResult {
44 gasUsed: BN;
45 createdAddress?: Buffer;
46 execResult: ExecResult;
47 }
48
49 interface ExecResult {
50 runState?: RunState;
51 exceptionError?: VmError;
52 gas?: BN;
53 gasUsed: BN;
54 returnValue: Buffer;
55 logs?: any[];
56 gasRefund?: BN;
57 selfdestruct?: { [k: string]: Buffer };
58 }
59 interface RunState {
60 programCounter: number;
61 opCode: number;
62 memory: Memory;
63 memoryWordCount: BN;
64 highestMemCost: BN;
65 stack: Stack;
66 code: Buffer;
67 validJumps: number[];
68 _common: Common;
69 stateManager: StateManager;
70 eei: EEI;
71 }
72
73 class Memory {
74 _store: number[];
75 constructor();
76 extend(offset: number, size: number): void;
77 write(offset: number, size: number, value: Buffer): void;
78 read(offset: number, size: number): Buffer;
79 }
80
81 class Stack {
82 _store: BN[];
83 constructor();
84 length(): number;
85 push(value: BN): void;
86 pop(): BN;
87 popN(num: number): BN[];
88 swap(position: number): void;
89 dup(position: number): void;
90 }
91
92 class StateManager {
93 _common: Common;
94 _trie: any;
95 _storageTries: any;
96 _cache: Cache;
97 _touched: Set<string>;
98 _touchedStack: Set<string>[];
99 _checkpointCount: number;
100 _originalStorageCache: Map<string, Map<string, Buffer>>;
101
102 constructor(opts: StateManagerOpts);
103 copy(): StateManager;
104 getAccount(address: Buffer, cb: any): void;
105 putAccount(address: Buffer, account: Account, cb: any): void;
106 putContractCode(address: Buffer, value: Buffer, cb: any): void;
107 getContractCode(address: Buffer, cb: any): void;
108 _lookupStorageTrie(address: Buffer, cb: any): void;
109 _getStorageTrie(address: Buffer, cb: any): void;
110 getContractStorage(address: Buffer, key: Buffer, cb: any): void;
111 getOriginalContractStorage(address: Buffer, key: Buffer, cb: any): void;
112 _modifyContractStorage(address: Buffer, modifyTrie: any, cb: any): void;
113 putContractStorage(address: Buffer, key: Buffer, value: Buffer, cb: any): void;
114 clearContractStorage(address: Buffer, cb: any): void;
115 checkpoint(cb: any): void;
116 commit(cb: any): void;
117 revert(cb: any): void;
118 getStateRoot(cb: any): void;
119 setStateRoot(stateRoot: Buffer, cb: any): void;
120 dumpStorage(address: Buffer, cb: any): void;
121 hasGenesisState(cb: any): void;
122 generateCanonicalGenesis(cb: any): void;
123 generateGenesis(initState: any, cb: any): void;
124 accountIsEmpty(address: Buffer, cb: any): void;
125 cleanupTouchedAccounts(cb: any): void;
126 _clearOriginalStorageCache(): void;
127 touchAccount(address: Buffer): void;
128 }
129
130 class Cache {
131 _cache: any;
132 _checkpoints: any[];
133 _trie: any;
134 constructor(trie: any);
135 put(key: Buffer, val: Account, fromTrie: boolean): void;
136 get(key: Buffer): Account;
137 lookup(key: Buffer): Account | undefined;
138 _lookupAccount(address: Buffer, cb: any): void;
139 getOrLoad(key: Buffer, cb: any): void;
140 warm(addresses: string[], cb: any): void;
141 flush(cb: any): void;
142 checkpoint(): void;
143 revert(): void;
144 commit(): void;
145 clear(): void;
146 del(key: Buffer): void;
147 _update(key: Buffer, val: Account, modified: boolean, deleted: boolean): void;
148 }
149
150 interface StateManagerOpts {
151 common?: Common;
152 trie?: any;
153 }
154
155 class EEI {
156 _env: Env;
157 _result: RunResult;
158 _state: PStateManager;
159 _evm: EVM;
160 _lastReturned: Buffer;
161 _common: Common;
162 _gasLeft: BN;
163 constructor(env: Env, state: PStateManager, evm: EVM, common: Common, gasLeft: BN);
164 useGas(amount: BN): void;
165 refundGas(amount: BN): void;
166 getAddress(): Buffer;
167 getExternalBalance(address: Buffer): Promise<BN>;
168 getSelfBalance(): BN;
169 getCaller(): BN;
170 getCallValue(): BN;
171 getCallData(): Buffer;
172 getCallDataSize(): BN;
173 getCodeSize(): BN;
174 getCode(): Buffer;
175 isStatic(): boolean;
176 getExternalCodeSize(address: BN): Promise<BN>;
177 getExternalCode(address: BN | Buffer): Promise<Buffer>;
178 getReturnDataSize(): BN;
179 getReturnData(): Buffer;
180 getTxGasPrice(): BN;
181 getTxOrigin(): BN;
182 getBlockNumber(): BN;
183 getBlockCoinbase(): BN;
184 getBlockTimestamp(): BN;
185 getBlockDifficulty(): BN;
186 getBlockGasLimit(): BN;
187 getChainId(): BN;
188 getBlockHash(num: BN): Promise<BN>;
189 storageStore(key: Buffer, value: Buffer): Promise<void>;
190 storageLoad(key: Buffer): Promise<Buffer>;
191 getGasLeft(): BN;
192 finish(returnData: Buffer): void;
193 revert(returnData: Buffer): void;
194 selfDestruct(toAddress: Buffer): Promise<void>;
195 _selfDestruct(toAddress: Buffer): Promise<void>;
196 log(data: Buffer, numberOfTopics: number, topics: Buffer[]): void;
197 call(gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN>;
198 callCode(gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN>;
199 callStatic(gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN>;
200 callDelegate(gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN>;
201 _baseCall(msg: Message): Promise<BN>;
202 create(gasLimit: BN, value: BN, data: Buffer, salt: Buffer | null): Promise<BN>;
203 create2(gasLimit: BN, value: BN, data: Buffer, salt: Buffer): Promise<BN>;
204 isAccountEmpty(address: Buffer): Promise<boolean>;
205 private _getReturnCode(results: EVMResult): any;
206 }
207
208 interface Env {
209 blockchain: Blockchain;
210 address: Buffer;
211 caller: Buffer;
212 callData: Buffer;
213 callValue: BN;
214 code: Buffer;
215 isStatic: boolean;
216 depth: number;
217 gasPrice: Buffer;
218 origin: Buffer;
219 block: any;
220 contract: Account;
221 }
222
223 interface RunResult {
224 logs: any;
225 returnValue?: Buffer;
226 gasRefund: BN;
227 selfdestruct: { [k: string]: Buffer };
228 }
229
230 export class PStateManager {
231 _wrapped: StateManager;
232 constructor(wrapped: StateManager);
233 copy(): PStateManager;
234 getAccount(addr: Buffer): Promise<Account>;
235 putAccount(addr: Buffer, account: Account): Promise<void>;
236 putContractCode(addr: Buffer, code: Buffer): Promise<void>;
237 getContractCode(addr: Buffer): Promise<Buffer>;
238 getContractStorage(addr: Buffer, key: Buffer): Promise<any>;
239 getOriginalContractStorage(addr: Buffer, key: Buffer): Promise<any>;
240 putContractStorage(addr: Buffer, key: Buffer, value: Buffer): Promise<void>;
241 clearContractStorage(addr: Buffer): Promise<void>;
242 checkpoint(): Promise<void>;
243 commit(): Promise<void>;
244 revert(): Promise<void>;
245 getStateRoot(): Promise<Buffer>;
246 setStateRoot(root: Buffer): Promise<void>;
247 dumpStorage(address: Buffer): Promise<StorageDump>;
248 hasGenesisState(): Promise<boolean>;
249 generateCanonicalGenesis(): Promise<void>;
250 generateGenesis(initState: any): Promise<void>;
251 accountIsEmpty(address: Buffer): Promise<boolean>;
252 cleanupTouchedAccounts(): Promise<void>;
253 }
254
255 interface StorageDump {
256 [key: string]: string;
257 }
258
259 class EVM {
260 _vm: any;
261 _state: PStateManager;
262 _tx: TxContext;
263 _block: any;
264 constructor(vm: any, txContext: TxContext, block: any);
265 executeMessage(message: Message): Promise<EVMResult>;
266 _executeCall(message: Message): Promise<EVMResult>;
267 _executeCreate(message: Message): Promise<EVMResult>;
268 runInterpreter(message: Message, opts: InterpreterOpts): Promise<ExecResult>;
269 getPrecompile(address: Buffer): PrecompileFunc;
270 runPrecompile(code: PrecompileFunc, data: Buffer, gasLimit: BN): ExecResult;
271 _loadCode(message: Message): Promise<void>;
272 _generateAddress(message: Message): Promise<Buffer>;
273 _reduceSenderBalance(account: Account, message: Message): Promise<void>;
274 _addToBalance(toAccount: Account, message: Message): Promise<void>;
275 _touchAccount(address: Buffer): Promise<void>;
276 }
277
278 class TxContext {
279 gasPrice: Buffer;
280 origin: Buffer;
281 constructor(gasPrice: Buffer, origin: Buffer);
282 }
283
284 class Message {
285 to: Buffer;
286 value: BN;
287 caller: Buffer;
288 gasLimit: BN;
289 data: Buffer;
290 depth: number;
291 code: Buffer | PrecompileFunc;
292 _codeAddress: Buffer;
293 isStatic: boolean;
294 isCompiled: boolean;
295 salt: Buffer;
296 selfdestruct: any;
297 delegatecall: boolean;
298 constructor(opts: any);
299 codeAddress(): Buffer;
300 }
301
302 interface InterpreterOpts {
303 pc?: number;
304 }
305
306 interface PrecompileFunc {
307 (opts: PrecompileInput): ExecResult;
308 }
309
310 interface PrecompileInput {
311 data: Buffer;
312 gasLimit: BN;
313 _common: Common;
314 }
315
316 class VmError {
317 error: ERROR;
318 errorType: string;
319 constructor(error: ERROR);
320 }
321
322 enum ERROR {
323 OUT_OF_GAS = 'out of gas',
324 STACK_UNDERFLOW = 'stack underflow',
325 STACK_OVERFLOW = 'stack overflow',
326 INVALID_JUMP = 'invalid JUMP',
327 INVALID_OPCODE = 'invalid opcode',
328 OUT_OF_RANGE = 'value out of range',
329 REVERT = 'revert',
330 STATIC_STATE_CHANGE = 'static state change',
331 INTERNAL_ERROR = 'internal error',
332 CREATE_COLLISION = 'create collision',
333 STOP = 'stop',
334 }
335}