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