Skip to content
This repository was archived by the owner on May 13, 2022. It is now read-only.

Commit a1bec44

Browse files
author
Silas Davis
committed
Implement CHAINID and DIFFICULTY opcodes
We have to adopt our own convention for CHAINID, which is now Keccak(ChainID). DIFFICULTY, which doesn't really make sense for Tendermint, is fixed to one to represent 'no hashing misses' when proposing. Signed-off-by: Silas Davis <[email protected]>
1 parent 60a86d3 commit a1bec44

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

crypto/signature.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (sig *Signature) String() string {
118118
}
119119

120120
func GetEthChainID(chainID string) *big.Int {
121-
return new(big.Int).SetBytes([]byte(chainID))
121+
return new(big.Int).SetBytes(Keccak256([]byte(chainID)))
122122
}
123123

124124
func GetEthSignatureRecoveryID(chainID string, parity *big.Int) *big.Int {

execution/engine/blockchain.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type TestBlockchain struct {
1212
BlockTime time.Time
1313
}
1414

15+
var _ Blockchain = (*TestBlockchain)(nil)
16+
1517
func (b *TestBlockchain) LastBlockHeight() uint64 {
1618
return b.BlockHeight
1719
}
@@ -28,3 +30,7 @@ func (b *TestBlockchain) BlockHash(height uint64) ([]byte, error) {
2830
binary.BigEndian.PutUint64(bs[24:], height)
2931
return bs, nil
3032
}
33+
34+
func (V *TestBlockchain) ChainID() string {
35+
return "TestChain"
36+
}

execution/engine/callable.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Blockchain interface {
1212
LastBlockHeight() uint64
1313
LastBlockTime() time.Time
1414
BlockHash(height uint64) ([]byte, error)
15+
ChainID() string
1516
}
1617

1718
type CallParams struct {

execution/evm/asm/opcodes.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ const (
7171
COINBASE
7272
TIMESTAMP
7373
BLOCKHEIGHT
74-
DIFFICULTY_DEPRECATED
74+
DIFFICULTY
7575
GASLIMIT
76+
CHAINID
7677
)
7778

7879
const (
@@ -236,12 +237,13 @@ var opCodeNames = map[OpCode]string{
236237
EXTCODEHASH: "EXTCODEHASH",
237238

238239
// 0x40 range - block operations
239-
BLOCKHASH: "BLOCKHASH",
240-
COINBASE: "COINBASE",
241-
TIMESTAMP: "TIMESTAMP",
242-
BLOCKHEIGHT: "BLOCKHEIGHT",
243-
DIFFICULTY_DEPRECATED: "DIFFICULTY_DEPRECATED",
244-
GASLIMIT: "GASLIMIT",
240+
BLOCKHASH: "BLOCKHASH",
241+
COINBASE: "COINBASE",
242+
TIMESTAMP: "TIMESTAMP",
243+
BLOCKHEIGHT: "BLOCKHEIGHT",
244+
DIFFICULTY: "DIFFICULTY",
245+
GASLIMIT: "GASLIMIT",
246+
CHAINID: "CHAINID",
245247

246248
// 0x50 range - 'storage' and execution
247249
POP: "POP",

execution/evm/contract.go

+10
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,20 @@ func (c *Contract) execute(st engine.State, params engine.CallParams) ([]byte, e
478478
stack.Push64(number)
479479
c.debugf(" => %d\n", number)
480480

481+
case DIFFICULTY: // 0x44
482+
// ~ hashes per solution - by convention we'll use unity since there are no misses if you are proposer
483+
stack.Push(One256)
484+
c.debugf(" => %v\n", One256)
485+
481486
case GASLIMIT: // 0x45
482487
stack.PushBigInt(params.Gas)
483488
c.debugf(" => %v\n", *params.Gas)
484489

490+
case CHAINID: // 0x46
491+
id := crypto.GetEthChainID(st.Blockchain.ChainID())
492+
stack.PushBigInt(id)
493+
c.debugf(" => %X\n", id)
494+
485495
case POP: // 0x50
486496
popped := stack.Pop()
487497
c.debugf(" => 0x%v\n", popped)

execution/wasm/contract.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,7 @@ func (ctx *context) ResolveFunc(module, field string) lifeExec.FunctionImport {
8484
}
8585

8686
switch field {
87-
case "call":
88-
fallthrough
89-
case "callCode":
90-
fallthrough
91-
case "callDelegate":
92-
fallthrough
93-
case "callStatic":
87+
case "call", "callCode", "callDelegate", "callStatic":
9488
return func(vm *lifeExec.VirtualMachine) int64 {
9589
gasLimit := big.NewInt(vm.GetCurrentFrame().Locals[0])
9690
addressPtr := uint32(vm.GetCurrentFrame().Locals[1])

0 commit comments

Comments
 (0)