github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/precompiles/arch_test.go (about) 1 package precompiles_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/onflow/flow-go/fvm/evm/precompiles" 9 "github.com/onflow/flow-go/fvm/evm/testutils" 10 "github.com/onflow/flow-go/fvm/evm/types" 11 ) 12 13 func TestArchContract(t *testing.T) { 14 15 t.Run("test block height", func(t *testing.T) { 16 address := testutils.RandomAddress(t) 17 height := uint64(12) 18 pc := precompiles.ArchContract( 19 address, 20 func() (uint64, error) { 21 return height, nil 22 }, 23 nil, 24 nil, 25 nil, 26 ) 27 28 input := precompiles.FlowBlockHeightFuncSig.Bytes() 29 require.Equal(t, address, pc.Address()) 30 require.Equal(t, precompiles.FlowBlockHeightFixedGas, pc.RequiredGas(input)) 31 ret, err := pc.Run(input) 32 require.NoError(t, err) 33 34 expected := make([]byte, 32) 35 expected[31] = 12 36 require.Equal(t, expected, ret) 37 38 _, err = pc.Run([]byte{1, 2, 3}) 39 require.Error(t, err) 40 }) 41 42 t.Run("test get random source", func(t *testing.T) { 43 address := testutils.RandomAddress(t) 44 rand := uint64(1337) 45 pc := precompiles.ArchContract( 46 address, 47 nil, 48 nil, 49 func(u uint64) (uint64, error) { 50 return rand, nil 51 }, 52 nil, 53 ) 54 55 require.Equal(t, address, pc.Address()) 56 57 height := make([]byte, 32) 58 require.NoError(t, precompiles.EncodeUint64(13, height, 0)) 59 60 input := append(precompiles.RandomSourceFuncSig.Bytes(), height...) 61 require.Equal(t, precompiles.RandomSourceGas, pc.RequiredGas(input)) 62 63 ret, err := pc.Run(input) 64 require.NoError(t, err) 65 66 resultRand, err := precompiles.ReadUint64(ret, 0) 67 require.NoError(t, err) 68 require.Equal(t, rand, resultRand) 69 }) 70 71 t.Run("test revertible random", func(t *testing.T) { 72 address := testutils.RandomAddress(t) 73 rand := uint64(1337) 74 pc := precompiles.ArchContract( 75 address, 76 nil, 77 nil, 78 nil, 79 func() (uint64, error) { 80 return rand, nil 81 }, 82 ) 83 84 require.Equal(t, address, pc.Address()) 85 86 input := precompiles.RevertibleRandomFuncSig.Bytes() 87 require.Equal(t, precompiles.RevertibleRandomGas, pc.RequiredGas(input)) 88 89 ret, err := pc.Run(input) 90 require.NoError(t, err) 91 92 resultRand, err := precompiles.ReadUint64(ret, 0) 93 require.NoError(t, err) 94 require.Equal(t, rand, resultRand) 95 }) 96 97 t.Run("test proof verification", func(t *testing.T) { 98 proof := testutils.COAOwnershipProofInContextFixture(t) 99 pc := precompiles.ArchContract( 100 testutils.RandomAddress(t), 101 nil, 102 func(p *types.COAOwnershipProofInContext) (bool, error) { 103 require.Equal(t, proof, p) 104 return true, nil 105 }, 106 nil, 107 nil, 108 ) 109 110 abiEncodedData, err := precompiles.ABIEncodeProof(proof) 111 require.NoError(t, err) 112 113 // add function selector to the input 114 input := append(precompiles.ProofVerifierFuncSig.Bytes(), abiEncodedData...) 115 116 expectedGas := precompiles.ProofVerifierBaseGas + 117 uint64(len(proof.KeyIndices))*precompiles.ProofVerifierGasMultiplerPerSignature 118 require.Equal(t, expectedGas, pc.RequiredGas(input)) 119 120 ret, err := pc.Run(input) 121 require.NoError(t, err) 122 123 expected := make([]byte, 32) 124 expected[31] = 1 125 require.Equal(t, expected, ret) 126 }) 127 }