Skip to content

Commit d41c24a

Browse files
committed
Implemented most opcodes
1 parent 3ab26ce commit d41c24a

14 files changed

+907
-111
lines changed

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"recommendations": [
33
"firsttris.vscode-jest-runner",
44
"dbaeumer.vscode-eslint",
5-
"eamodio.gitlens"
5+
"eamodio.gitlens",
6+
"Gruntfuggly.todo-tree"
67
]
78
}

src/container/CoreContainer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { AccessSets } from '../evm/gas/AccessSets';
5353
import { EvmKeyValueStorage } from '../evm/EvmKeyValueStorage';
5454
import { MemoryExpansionGas } from '../evm/gas/MemoyExspansionGas';
5555
import { AccountAccessGas } from '../evm/gas/AccountAccessGas';
56+
import { EvmSubContext } from '../evm/EvmSubContext';
5657

5758
export class CoreContainer {
5859
protected create(options?: ContainerOptions) {
@@ -135,7 +136,7 @@ export class CoreContainer {
135136
container.bind(MessageQueue).toSelf();
136137
container.bind(GetRandomBytesInteractor).toSelf();
137138

138-
// container.bind(Evm).toSelf();
139+
container.bind(EvmSubContext).toSelf();
139140
container.bind(EvmStack).toSelf();
140141
container.bind(Network).toSelf();
141142
container.bind(EvmMemory).toSelf();

src/evm/Contract.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,41 @@ import { keccak256 } from '../utils/keccak256';
77
import { RlpEncoder } from '../rlp';
88
import { getBufferFromHex } from '../utils/getBufferFromHex';
99
import { Address } from './Address';
10+
import { convertNumberToPadHex } from '../utils/convertNumberToPadHex';
11+
import { EvmMemory } from './EvmMemory';
12+
import { ExposedEvm } from './ExposedEvm';
13+
import { collapseTextChangeRangesAcrossMultipleVersions } from 'typescript';
1014

1115
export class Contract {
1216
private _address: string;
17+
private _returnData?: Buffer;
1318

1419
constructor(
1520
private bytes: Buffer,
1621
private _value: BigNumber,
17-
private context: TxContext
22+
private context: TxContext,
23+
private salt?: Buffer // private evm?: Evm
1824
) {
1925
// https://ethereum.stackexchange.com/a/101340
2026
// https://www.evm.codes/#f0
2127
const rlp = getClassFromTestContainer(RlpEncoder);
2228
const encoding = rlp.encode({
2329
input: [context.sender.raw || crypto.randomBytes(32), context.nonce],
2430
});
25-
this._address =
26-
'0x' + keccak256(getBufferFromHex(encoding)).slice(12).toString('hex');
31+
if (this.salt) {
32+
const dataHash = keccak256(bytes);
33+
const address = keccak256(
34+
getBufferFromHex(
35+
`0xff${convertNumberToPadHex(
36+
context.sender.raw.toString(16)
37+
)}${salt?.toString('hex')}${dataHash.toString('hex')}`
38+
)
39+
);
40+
this._address = '0x' + address.slice(12).toString('hex');
41+
} else {
42+
this._address =
43+
'0x' + keccak256(getBufferFromHex(encoding)).slice(12).toString('hex');
44+
}
2745
}
2846

2947
public get value() {
@@ -42,24 +60,42 @@ export class Contract {
4260
return new Address(this._address);
4361
}
4462

45-
public execute() {
63+
public execute(
64+
options: Partial<TxContext> & {
65+
evm?: Evm;
66+
}
67+
) {
4668
// This should be done in another way -> Redo this in a better way.
4769
// The result of this execution will also effect the previous context, so we need to be able to continue in same context.
4870
// by having it in a different container this is not the case.
4971
// I think that all access sets done in this new contract also will affect the ones in the current context.
50-
const evm = getClassFromTestContainer(Evm)
51-
.boot(this.bytes, {
52-
value: new Wei(this.value.toNumber()),
53-
data: Buffer.from(''),
54-
nonce: 0,
55-
sender: this.context.sender,
56-
gasLimit: this.context.gasLimit,
57-
})
72+
const data = options.data || Buffer.from('');
73+
const evm = getClassFromTestContainer(ExposedEvm);
74+
if (options.evm) {
75+
evm.copy(options.evm);
76+
}
77+
evm
78+
.boot(
79+
this.bytes,
80+
{
81+
value: new Wei(this.value.toNumber()),
82+
data,
83+
nonce: 0,
84+
sender: this.context.sender,
85+
gasLimit: this.context.gasLimit,
86+
},
87+
{ debug: false }
88+
)
5889
.execute();
5990
if (evm.callingContextReturnData) {
91+
this._returnData = evm.callingContextReturnData;
6092
this.bytes = evm.callingContextReturnData;
6193
}
6294

6395
return this;
6496
}
97+
98+
public get returnData() {
99+
return this._returnData;
100+
}
65101
}

0 commit comments

Comments
 (0)