Skip to content

Commit

Permalink
apply PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ramtinms committed Jan 23, 2024
1 parent 55f2a23 commit a930a00
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions fvm/evm/handler/addressAllocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
const (
ledgerAddressAllocatorKey = "AddressAllocator"
uint64ByteSize = 8
addressPrefixLen = 12
)

var (
// prefixes:
// the first 12 bytes of addresses allocation
// leading zeros helps with storage and all zero is reserved for the EVM precompiles
FlowEVMPrecompileAddressPrefix = [...]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
FlowEVMCOAAddressPrefix = [...]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
FlowEVMPrecompileAddressPrefix = [addressPrefixLen]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
FlowEVMCOAAddressPrefix = [addressPrefixLen]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
)

type AddressAllocator struct {
Expand Down Expand Up @@ -75,7 +76,7 @@ func MakePrecompileAddress(index uint64) types.Address {
return makePrefixedAddress(index, FlowEVMPrecompileAddressPrefix)
}

func makePrefixedAddress(index uint64, prefix [12]byte) types.Address {
func makePrefixedAddress(index uint64, prefix [addressPrefixLen]byte) types.Address {
var addr types.Address
prefixIndex := types.AddressLength - uint64ByteSize
copy(addr[:prefixIndex], prefix[:])
Expand Down
4 changes: 2 additions & 2 deletions fvm/evm/precompiles/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *precompile) Address() types.Address {

// RequiredGas calculates the contract gas use
func (p *precompile) RequiredGas(input []byte) uint64 {
if len(input) < 4 {
if len(input) < FunctionSelectorLength {
return InvalidMethodCallGasUsage
}
sig, data := SplitFunctionSelector(input)
Expand All @@ -63,7 +63,7 @@ func (p *precompile) RequiredGas(input []byte) uint64 {

// Run runs the precompiled contract
func (p *precompile) Run(input []byte) ([]byte, error) {
if len(input) < 4 {
if len(input) < FunctionSelectorLength {
return nil, ErrInvalidMethodCall
}
sig, data := SplitFunctionSelector(input)
Expand Down
2 changes: 2 additions & 0 deletions fvm/evm/precompiles/precompile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestMutiFunctionContract(t *testing.T) {
t.Parallel()

address := testutils.RandomAddress(t)
sig := precompiles.FunctionSelector{1, 2, 3, 4}
data := "data"
Expand Down
2 changes: 2 additions & 0 deletions fvm/evm/precompiles/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestFunctionSelector(t *testing.T) {
t.Parallel()

expected := gethCrypto.Keccak256([]byte("test()"))[:4]
require.Equal(t, expected, precompiles.ComputeFunctionSelector("test", nil).Bytes())

Expand Down

0 comments on commit a930a00

Please sign in to comment.