Skip to content

Commit 4a75f92

Browse files
core/vm: add test for random opcode
1 parent 5cf5c71 commit 4a75f92

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

core/vm/instructions_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io/ioutil"
24+
"math/big"
2425
"testing"
2526

2627
"github.com/ethereum/go-ethereum/common"
@@ -650,3 +651,36 @@ func TestCreate2Addreses(t *testing.T) {
650651
}
651652
}
652653
}
654+
655+
func TestRandom(t *testing.T) {
656+
type testcase struct {
657+
name string
658+
random common.Hash
659+
}
660+
661+
for _, tt := range []testcase{
662+
{name: "empty hash", random: common.Hash{}},
663+
{name: "1", random: common.Hash{0}},
664+
{name: "emptyCodeHash", random: emptyCodeHash},
665+
{name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})},
666+
} {
667+
var (
668+
env = NewEVM(BlockContext{Random: tt.random}, TxContext{}, nil, params.TestChainConfig, Config{})
669+
stack = newstack()
670+
pc = uint64(0)
671+
evmInterpreter = env.interpreter
672+
)
673+
opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
674+
if len(stack.data) != 1 {
675+
t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data))
676+
}
677+
actual := stack.pop()
678+
expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes()))
679+
if overflow {
680+
t.Errorf("Testcase %v: invalid overflow", tt.name)
681+
}
682+
if actual.Cmp(expected) != 0 {
683+
t.Errorf("Testcase %v: expected %x, got %x", tt.name, expected, actual)
684+
}
685+
}
686+
}

core/vm/opcodes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ var opCodeToString = map[OpCode]string{
278278
COINBASE: "COINBASE",
279279
TIMESTAMP: "TIMESTAMP",
280280
NUMBER: "NUMBER",
281-
DIFFICULTY: "DIFFICULTY",
281+
DIFFICULTY: "DIFFICULTY", // TODO (MariusVanDerWijden) rename to RANDOM post merge
282282
GASLIMIT: "GASLIMIT",
283283
CHAINID: "CHAINID",
284284
SELFBALANCE: "SELFBALANCE",

0 commit comments

Comments
 (0)