@@ -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+ }
0 commit comments