From 2cb5e034c17dd74089fadcaa0b06cba0945a5402 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Date: Mon, 11 Dec 2023 15:38:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20change=20`BLOCKHASH`=20opcode=20behavio?= =?UTF-8?q?r=20to=20`keccak(chain=5Fid=20||=20block=E2=80=A6=20(#582)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: change `BLOCKHASH` opcode behavior to `keccak(chain_id || block_number)` * fix: conflicts in library for blockhash pr (#591) * feat: change `BLOCKHASH` opcode behavior to `keccak(chain_id || block_number)` * fix conflicts in library --------- Co-authored-by: HAOYUatHZ Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> --------- Co-authored-by: Nazarii Denha --- build/ci.go | 2 +- core/vm/instructions.go | 20 +++++++++++++++++++- go.mod | 2 +- go.sum | 1 - 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/build/ci.go b/build/ci.go index 46f1ac2812..f3f49bc847 100644 --- a/build/ci.go +++ b/build/ci.go @@ -285,7 +285,7 @@ func doTest(cmdline []string) { coverage = flag.Bool("coverage", false, "Whether to record code coverage") verbose = flag.Bool("v", false, "Whether to log verbosely") race = flag.Bool("race", false, "Execute the race detector") - short = flag.Bool("short", false, "Pass the 'short'-flag to go test") + short = flag.Bool("short", false, "Pass the 'short'-flag to go test") cachedir = flag.String("cachedir", "./build/cache", "directory for caching downloads") ) flag.CommandLine.Parse(cmdline) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index becc48dc1d..2e1a5f7c3d 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -17,11 +17,14 @@ package vm import ( + "encoding/binary" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" + "golang.org/x/crypto/sha3" ) func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { @@ -447,7 +450,22 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ( lower = upper - 256 } if num64 >= lower && num64 < upper { - num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes()) + chainId := interpreter.evm.ChainConfig().ChainID + chainIdBuf := make([]byte, 8) + binary.BigEndian.PutUint64(chainIdBuf, chainId.Uint64()) + num64Buf := make([]byte, 8) + binary.BigEndian.PutUint64(num64Buf, num64) + + if interpreter.hasher == nil { + interpreter.hasher = sha3.NewLegacyKeccak256().(crypto.KeccakState) + } else { + interpreter.hasher.Reset() + } + interpreter.hasher.Write(chainIdBuf) + interpreter.hasher.Write(num64Buf) + interpreter.hasher.Read(interpreter.hasherBuf[:]) + + num.SetBytes(interpreter.hasherBuf[:]) } else { num.Clear() } diff --git a/go.mod b/go.mod index 46878dbde1..68cb194c86 100644 --- a/go.mod +++ b/go.mod @@ -42,6 +42,7 @@ require ( github.com/holiman/bloomfilter/v2 v2.0.3 github.com/holiman/uint256 v1.2.3 github.com/huin/goupnp v1.3.0 + github.com/iden3/go-iden3-crypto v0.0.15 github.com/influxdata/influxdb-client-go/v2 v2.4.0 github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c github.com/jackpal/go-nat-pmp v1.0.2 @@ -112,7 +113,6 @@ require ( github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.4 // indirect - github.com/iden3/go-iden3-crypto v0.0.15 // indirect github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/kilic/bls12-381 v0.1.0 // indirect diff --git a/go.sum b/go.sum index fa8d06e274..7fc5de3a6a 100644 --- a/go.sum +++ b/go.sum @@ -561,7 +561,6 @@ github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobt github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=