Skip to content

Commit 081642e

Browse files
holimankaralabe
authored andcommitted
Eip 1344 (ChainID opcode) (#19921)
* core/vm: implement EIP 1344 (ChainID opcode) * core/vm: formatting
1 parent 17589aa commit 081642e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

core/vm/eips.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package vm
1818

1919
import (
2020
"fmt"
21-
21+
2222
"github.com/ethereum/go-ethereum/params"
2323
)
2424

@@ -29,6 +29,8 @@ func EnableEIP(eipNum int, jt *JumpTable) error {
2929
switch eipNum {
3030
case 1884:
3131
enable1884(jt)
32+
case 1344:
33+
enable1344(jt)
3234
default:
3335
return fmt.Errorf("undefined eip %d", eipNum)
3436
}
@@ -61,3 +63,23 @@ func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract,
6163
stack.push(balance)
6264
return nil, nil
6365
}
66+
67+
// enable1344 applies EIP-1344 (ChainID Opcode)
68+
// - Adds an opcode that returns the current chain’s EIP-155 unique identifier
69+
func enable1344(jt *JumpTable) {
70+
// New opcode
71+
jt[CHAINID] = operation{
72+
execute: opChainID,
73+
constantGas: GasQuickStep,
74+
minStack: minStack(0, 1),
75+
maxStack: maxStack(0, 1),
76+
valid: true,
77+
}
78+
}
79+
80+
// opChainID implements CHAINID opcode
81+
func opChainID(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
82+
chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID)
83+
stack.push(chainId)
84+
return nil, nil
85+
}

core/vm/opcodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const (
101101
NUMBER
102102
DIFFICULTY
103103
GASLIMIT
104+
CHAINID = 0x46
104105
SELFBALANCE = 0x47
105106
)
106107

@@ -278,6 +279,7 @@ var opCodeToString = map[OpCode]string{
278279
NUMBER: "NUMBER",
279280
DIFFICULTY: "DIFFICULTY",
280281
GASLIMIT: "GASLIMIT",
282+
CHAINID: "CHAINID",
281283
SELFBALANCE: "SELFBALANCE",
282284

283285
// 0x50 range - 'storage' and execution.
@@ -430,6 +432,7 @@ var stringToOp = map[string]OpCode{
430432
"CALLDATALOAD": CALLDATALOAD,
431433
"CALLDATASIZE": CALLDATASIZE,
432434
"CALLDATACOPY": CALLDATACOPY,
435+
"CHAINID": CHAINID,
433436
"DELEGATECALL": DELEGATECALL,
434437
"STATICCALL": STATICCALL,
435438
"CODESIZE": CODESIZE,

0 commit comments

Comments
 (0)