Skip to content

Commit e66ae9d

Browse files
authored
Migrate precompiles, opcodes, eei, message, txContext to typescript (ethereumjs#497)
* Change txContext filetype to ts * Migrate txContext to ts * Change message filetype to ts * Migrate message to ts * Change eei filetype to ts * Change opcodes filetype to ts * Migrate eei and opcodes to ts * Change precompile filetypes to ts * Migrate precompiles to ts
1 parent b75d1da commit e66ae9d

30 files changed

+514
-486
lines changed

lib/evm/eei.js renamed to lib/evm/eei.ts

+55-51
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import BN = require('bn.js')
2+
import PStateManager from '../state/promisified'
3+
import Message from './message'
4+
import { toBuffer } from 'ethereumjs-util'
15
const promisify = require('util.promisify')
2-
const BN = require('bn.js')
36
const { VmError, ERROR } = require('../exceptions')
4-
const PStateManager = require('../state/promisified').default
5-
const Message = require('./message')
67

7-
module.exports = class EEI {
8-
constructor (env) {
8+
export default class EEI {
9+
_env: any
10+
_state: PStateManager
11+
12+
constructor (env: any) {
913
this._env = env
1014
this._state = new PStateManager(this._env.stateManager)
1115
}
@@ -15,7 +19,7 @@ module.exports = class EEI {
1519
* @param {BN} amount - Amount of gas to consume
1620
* @throws if out of gas
1721
*/
18-
useGas (amount) {
22+
useGas (amount: BN): void {
1923
this._env.gasLeft.isub(amount)
2024
if (this._env.gasLeft.ltn(0)) {
2125
this._env.gasLeft = new BN(0)
@@ -27,24 +31,24 @@ module.exports = class EEI {
2731
* Returns address of currently executing account.
2832
* @returns {Buffer}
2933
*/
30-
getAddress () {
34+
getAddress (): Buffer {
3135
return this._env.address
3236
}
3337

3438
/**
3539
* Returns balance of the given account.
3640
* @param {BN} address - Address of account
3741
*/
38-
async getExternalBalance (address) {
39-
address = addressToBuffer(address)
42+
async getExternalBalance (address: BN): Promise<BN> {
43+
const addressBuf = addressToBuffer(address)
4044

4145
// shortcut if current account
42-
if (address.toString('hex') === this._env.address.toString('hex')) {
46+
if (addressBuf.toString('hex') === this._env.address.toString('hex')) {
4347
return new BN(this._env.contract.balance)
4448
}
4549

4650
// otherwise load account then return balance
47-
const account = await this._state.getAccount(address)
51+
const account = await this._state.getAccount(addressBuf)
4852
return new BN(account.balance)
4953
}
5054

@@ -53,7 +57,7 @@ module.exports = class EEI {
5357
* that is directly responsible for this execution.
5458
* @returns {BN}
5559
*/
56-
getCaller () {
60+
getCaller (): BN {
5761
return new BN(this._env.caller)
5862
}
5963

@@ -62,7 +66,7 @@ module.exports = class EEI {
6266
* responsible for this execution.
6367
* @returns {BN}
6468
*/
65-
getCallValue () {
69+
getCallValue (): BN {
6670
return new BN(this._env.callValue)
6771
}
6872

@@ -72,7 +76,7 @@ module.exports = class EEI {
7276
* @param {BN} pos - Offset of calldata
7377
* @returns {Buffer}
7478
*/
75-
getCallData (pos) {
79+
getCallData (pos: BN): Buffer {
7680
return this._env.callData
7781
}
7882

@@ -81,7 +85,7 @@ module.exports = class EEI {
8185
* input data passed with the message call instruction or transaction.
8286
* @returns {BN}
8387
*/
84-
getCallDataSize () {
88+
getCallDataSize (): BN {
8589
if (this._env.callData.length === 1 && this._env.callData[0] === 0) {
8690
return new BN(0)
8791
}
@@ -93,33 +97,33 @@ module.exports = class EEI {
9397
* Returns the size of code running in current environment.
9498
* @returns {BN}
9599
*/
96-
getCodeSize () {
100+
getCodeSize (): BN {
97101
return new BN(this._env.code.length)
98102
}
99103

100104
/**
101105
* Returns the code running in current environment.
102106
* @returns {BN}
103107
*/
104-
getCode () {
108+
getCode (): BN {
105109
return this._env.code
106110
}
107111

108112
/**
109113
* Get size of an account’s code.
110114
* @param {BN} address - Address of account
111115
*/
112-
async getExternalCodeSize (address) {
113-
address = addressToBuffer(address)
114-
const code = await this._state.getContractCode(address)
116+
async getExternalCodeSize (address: BN): Promise<BN> {
117+
const addressBuf = addressToBuffer(address)
118+
const code = await this._state.getContractCode(addressBuf)
115119
return new BN(code.length)
116120
}
117121

118122
/**
119123
* Returns code of an account.
120124
* @param {BN} address - Address of account
121125
*/
122-
async getExternalCode (address) {
126+
async getExternalCode (address: BN | Buffer): Promise<Buffer> {
123127
if (!Buffer.isBuffer(address)) {
124128
address = addressToBuffer(address)
125129
}
@@ -132,7 +136,7 @@ module.exports = class EEI {
132136
* Note: create only fills the return data buffer in case of a failure.
133137
* @returns {BN}
134138
*/
135-
getReturnDataSize () {
139+
getReturnDataSize (): BN {
136140
return new BN(this._env.lastReturned.length)
137141
}
138142

@@ -142,15 +146,15 @@ module.exports = class EEI {
142146
* Note: create only fills the return data buffer in case of a failure.
143147
* @returns {Buffer}
144148
*/
145-
getReturnData () {
149+
getReturnData (): Buffer {
146150
return this._env.lastReturned
147151
}
148152

149153
/**
150154
* Returns price of gas in current environment.
151155
* @returns {BN}
152156
*/
153-
getTxGasPrice () {
157+
getTxGasPrice (): BN {
154158
return new BN(this._env.gasPrice)
155159
}
156160

@@ -160,56 +164,56 @@ module.exports = class EEI {
160164
* non-empty associated code.
161165
* @returns {BN}
162166
*/
163-
getTxOrigin () {
167+
getTxOrigin (): BN {
164168
return new BN(this._env.origin)
165169
}
166170

167171
/**
168172
* Returns the block’s number.
169173
* @returns {BN}
170174
*/
171-
getBlockNumber () {
175+
getBlockNumber (): BN {
172176
return new BN(this._env.block.header.number)
173177
}
174178

175179
/**
176180
* Returns the block's beneficiary address.
177181
* @returns {BN}
178182
*/
179-
getBlockCoinbase () {
183+
getBlockCoinbase (): BN {
180184
return new BN(this._env.block.header.coinbase)
181185
}
182186

183187
/**
184188
* Returns the block's timestamp.
185189
* @returns {BN}
186190
*/
187-
getBlockTimestamp () {
191+
getBlockTimestamp (): BN {
188192
return new BN(this._env.block.header.timestamp)
189193
}
190194

191195
/**
192196
* Returns the block's difficulty.
193197
* @returns {BN}
194198
*/
195-
getBlockDifficulty () {
199+
getBlockDifficulty (): BN {
196200
return new BN(this._env.block.header.difficulty)
197201
}
198202

199203
/**
200204
* Returns the block's gas limit.
201205
* @returns {BN}
202206
*/
203-
getBlockGasLimit () {
207+
getBlockGasLimit (): BN {
204208
return new BN(this._env.block.header.gasLimit)
205209
}
206210

207211
/**
208212
* Returns Gets the hash of one of the 256 most recent complete blocks.
209213
* @param {BN} - Number of block
210214
*/
211-
async getBlockHash (number) {
212-
const block = await promisify(this._env.blockchain.getBlock).bind(this._env.blockchain)(number)
215+
async getBlockHash (num: BN): Promise<BN> {
216+
const block = await promisify(this._env.blockchain.getBlock).bind(this._env.blockchain)(num)
213217
return new BN(block.hash())
214218
}
215219

@@ -218,7 +222,7 @@ module.exports = class EEI {
218222
* @param {Buffer} key
219223
* @param {Buffer} value
220224
*/
221-
async storageStore (key, value) {
225+
async storageStore (key: Buffer, value: Buffer): Promise<void> {
222226
await this._state.putContractStorage(this._env.address, key, value)
223227
const account = await this._state.getAccount(this._env.address)
224228
this._env.contract = account
@@ -229,23 +233,23 @@ module.exports = class EEI {
229233
* @param {Buffer} key - Storage key
230234
* @returns {Buffer}
231235
*/
232-
async storageLoad (key) {
236+
async storageLoad (key: Buffer): Promise<Buffer> {
233237
return this._state.getContractStorage(this._env.address, key)
234238
}
235239

236240
/**
237241
* Returns the current gasCounter.
238242
* @returns {BN}
239243
*/
240-
getGasLeft () {
244+
getGasLeft (): BN {
241245
return new BN(this._env.gasLeft)
242246
}
243247

244248
/**
245249
* Set the returning output data for the execution.
246250
* @param {Buffer} returnData - Output data to return
247251
*/
248-
finish (returnData) {
252+
finish (returnData: Buffer): void {
249253
this._env.returnValue = returnData
250254
}
251255

@@ -254,7 +258,7 @@ module.exports = class EEI {
254258
* execution immediately and set the execution result to "reverted".
255259
* @param {Buffer} returnData - Output data to return
256260
*/
257-
revert (returnData) {
261+
revert (returnData: Buffer): void {
258262
this._env.returnValue = returnData
259263
trap(ERROR.REVERT)
260264
}
@@ -265,11 +269,11 @@ module.exports = class EEI {
265269
* execution will be aborted immediately.
266270
* @param {Buffer} toAddress - Beneficiary address
267271
*/
268-
async selfDestruct (toAddress) {
272+
async selfDestruct (toAddress: Buffer): Promise<void> {
269273
return this._selfDestruct(toAddress)
270274
}
271275

272-
async _selfDestruct (toAddress) {
276+
async _selfDestruct (toAddress: Buffer): Promise<void> {
273277
// TODO: Determine if gas consumption & refund should happen in EEI or opFn
274278
if ((new BN(this._env.contract.balance)).gtn(0)) {
275279
const empty = await this._state.accountIsEmpty(toAddress)
@@ -289,12 +293,12 @@ module.exports = class EEI {
289293
// Add to beneficiary balance
290294
const toAccount = await this._state.getAccount(toAddress)
291295
const newBalance = new BN(this._env.contract.balance).add(new BN(toAccount.balance))
292-
toAccount.balance = newBalance
296+
toAccount.balance = toBuffer(newBalance)
293297
await this._state.putAccount(toAddress, toAccount)
294298

295299
// Subtract from contract balance
296300
const account = await this._state.getAccount(this._env.address)
297-
account.balance = new BN(0)
301+
account.balance = toBuffer(new BN(0))
298302
await this._state.putAccount(this._env.address, account)
299303
}
300304

@@ -304,7 +308,7 @@ module.exports = class EEI {
304308
* @param {Number} numberOfTopics
305309
* @param {BN[]} topics
306310
*/
307-
log (data, numberOfTopics, topics) {
311+
log (data: Buffer, numberOfTopics: number, topics: BN[]): void {
308312
if (numberOfTopics < 0 || numberOfTopics > 4) {
309313
trap(ERROR.OUT_OF_RANGE)
310314
}
@@ -329,7 +333,7 @@ module.exports = class EEI {
329333
* @param {BN} value
330334
* @param {Buffer} data
331335
*/
332-
async call (gasLimit, address, value, data) {
336+
async call (gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN> {
333337
const msg = new Message({
334338
caller: this._env.address,
335339
gasLimit: gasLimit,
@@ -350,7 +354,7 @@ module.exports = class EEI {
350354
* @param {BN} value
351355
* @param {Buffer} data
352356
*/
353-
async callCode (gasLimit, address, value, data) {
357+
async callCode (gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN> {
354358
const msg = new Message({
355359
caller: this._env.address,
356360
gasLimit: gasLimit,
@@ -374,7 +378,7 @@ module.exports = class EEI {
374378
* @param {BN} value
375379
* @param {Buffer} data
376380
*/
377-
async callStatic (gasLimit, address, value, data) {
381+
async callStatic (gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN> {
378382
const msg = new Message({
379383
caller: this._env.address,
380384
gasLimit: gasLimit,
@@ -396,7 +400,7 @@ module.exports = class EEI {
396400
* @param {BN} value
397401
* @param {Buffer} data
398402
*/
399-
async callDelegate (gasLimit, address, value, data) {
403+
async callDelegate (gasLimit: BN, address: Buffer, value: BN, data: Buffer): Promise<BN> {
400404
const msg = new Message({
401405
caller: this._env.caller,
402406
gasLimit: gasLimit,
@@ -412,7 +416,7 @@ module.exports = class EEI {
412416
return this._baseCall(msg)
413417
}
414418

415-
async _baseCall (msg) {
419+
async _baseCall (msg: Message): Promise<BN> {
416420
const selfdestruct = Object.assign({}, this._env.selfdestruct)
417421
msg.selfdestruct = selfdestruct
418422

@@ -460,7 +464,7 @@ module.exports = class EEI {
460464
* @param {BN} value
461465
* @param {Buffer} data
462466
*/
463-
async create (gasLimit, value, data, salt = null) {
467+
async create (gasLimit: BN, value: BN, data: Buffer, salt: Buffer | null = null): Promise<BN> {
464468
const selfdestruct = Object.assign({}, this._env.selfdestruct)
465469
const msg = new Message({
466470
caller: this._env.address,
@@ -530,16 +534,16 @@ module.exports = class EEI {
530534
* @param {Buffer} data
531535
* @param {Buffer} salt
532536
*/
533-
async create2 (gasLimit, value, data, salt) {
537+
async create2 (gasLimit: BN, value: BN, data: Buffer, salt: Buffer): Promise<BN> {
534538
return this.create(gasLimit, value, data, salt)
535539
}
536540
}
537541

538-
function trap (err) {
542+
function trap (err: string) {
539543
throw new VmError(err)
540544
}
541545

542546
const MASK_160 = new BN(1).shln(160).subn(1)
543-
function addressToBuffer (address) {
547+
function addressToBuffer (address: BN) {
544548
return address.and(MASK_160).toArrayLike(Buffer, 'be', 20)
545549
}

0 commit comments

Comments
 (0)