github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/testutils/misc.go (about) 1 package testutils 2 3 import ( 4 cryptoRand "crypto/rand" 5 "math/big" 6 "math/rand" 7 "testing" 8 9 gethCommon "github.com/onflow/go-ethereum/common" 10 gethTypes "github.com/onflow/go-ethereum/core/types" 11 "github.com/stretchr/testify/require" 12 13 "github.com/onflow/flow-go/fvm/evm/types" 14 ) 15 16 func RandomCommonHash(t testing.TB) gethCommon.Hash { 17 ret := gethCommon.Hash{} 18 _, err := cryptoRand.Read(ret[:gethCommon.HashLength]) 19 require.NoError(t, err) 20 return ret 21 } 22 23 func RandomBigInt(limit int64) *big.Int { 24 return big.NewInt(rand.Int63n(limit) + 1) 25 } 26 27 func RandomAddress(t testing.TB) types.Address { 28 return types.NewAddress(RandomCommonAddress(t)) 29 } 30 31 func RandomCommonAddress(t testing.TB) gethCommon.Address { 32 ret := gethCommon.Address{} 33 _, err := cryptoRand.Read(ret[:gethCommon.AddressLength]) 34 require.NoError(t, err) 35 return ret 36 } 37 38 func RandomGas(limit int64) uint64 { 39 return uint64(rand.Int63n(limit) + 1) 40 } 41 42 func RandomData(t testing.TB) []byte { 43 // byte size [1, 100] 44 size := rand.Intn(100) + 1 45 ret := make([]byte, size) 46 _, err := cryptoRand.Read(ret[:]) 47 require.NoError(t, err) 48 return ret 49 } 50 51 func GetRandomLogFixture(t testing.TB) *gethTypes.Log { 52 return &gethTypes.Log{ 53 Address: RandomCommonAddress(t), 54 Topics: []gethCommon.Hash{ 55 RandomCommonHash(t), 56 RandomCommonHash(t), 57 }, 58 Data: RandomData(t), 59 } 60 } 61 62 func COAOwnershipProofFixture(t testing.TB) *types.COAOwnershipProof { 63 return &types.COAOwnershipProof{ 64 Address: types.FlowAddress{1, 2, 3}, 65 CapabilityPath: "path", 66 KeyIndices: types.KeyIndices{1, 2}, 67 Signatures: types.Signatures{ 68 types.Signature("sig1"), 69 types.Signature("sig2"), 70 }, 71 } 72 } 73 74 func COAOwnershipProofInContextFixture(t testing.TB) *types.COAOwnershipProofInContext { 75 signedMsg := RandomCommonHash(t) 76 return &types.COAOwnershipProofInContext{ 77 COAOwnershipProof: *COAOwnershipProofFixture(t), 78 SignedData: types.SignedData(signedMsg[:]), 79 EVMAddress: RandomAddress(t), 80 } 81 }