Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

refactor(pc): string -> [4]bytes #1059

Merged
merged 10 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions eth/core/precompile/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func (sf *StatefulFactory) Build(
// This function matches each Go implementation of the precompile to the ABI's respective function.
// It searches for the ABI function in the Go precompile contract and performs basic validation on
// the implemented function.
func buildIdsToMethods(si StatefulImpl, contractImpl reflect.Value) (map[string]*method, error) {
func buildIdsToMethods(si StatefulImpl, contractImpl reflect.Value) (map[[4]byte]*method, error) {
precompileABI := si.ABIMethods()
contractImplType := contractImpl.Type()
idsToMethods := make(map[string]*method)
idsToMethods := make(map[[4]byte]*method)
for m := 0; m < contractImplType.NumMethod(); m++ {
implMethod := contractImplType.Method(m)

Expand All @@ -127,12 +127,12 @@ func buildIdsToMethods(si StatefulImpl, contractImpl reflect.Value) (map[string]
}

method := newMethod(si, precompileABI[methodName], implMethod)
idsToMethods[utils.UnsafeBytesToStr(precompileABI[methodName].ID)] = method
idsToMethods[[4]byte(precompileABI[methodName].ID)] = method
}

// verify that every abi method has a corresponding precompile implementation
for _, abiMethod := range precompileABI {
if _, found := idsToMethods[utils.UnsafeBytesToStr(abiMethod.ID)]; !found {
if _, found := idsToMethods[[4]byte(abiMethod.ID)]; !found {
return nil, errorslib.Wrap(ErrNoPrecompileMethodForABIMethod, abiMethod.Name)
}
}
Expand Down
11 changes: 11 additions & 0 deletions eth/core/precompile/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ var _ = Describe("Method", func() {
})
})

var _ = Describe("Test MethoID", func() {
It("should work", func() {
x := make([]byte, 0)
x = append(x, 0x12)
x = append(x, 0x34)
x = append(x, 0x56)
x = append(x, 0x78)
Expect([4]byte(x)).To(Equal([4]byte{0x12, 0x34, 0x56, 0x78}))
})
})

// MOCKS BELOW.

type mockStatefulWithMethod struct {
Expand Down
3 changes: 1 addition & 2 deletions eth/core/precompile/method_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func validateArg(implMethodVar reflect.Value, abiMethodVar reflect.Value) error
implMethodVarType := implMethodVar.Type()
abiMethodVarType := abiMethodVar.Type()

//nolint:exhaustive // only the ones we need.
switch implMethodVarType.Kind() {
switch implMethodVarType.Kind() { //nolint:exhaustive // todo verify its okay.
case reflect.Array, reflect.Slice:
if implMethodVarType.Elem() != abiMethodVarType.Elem() {
// If the array is not a slice/array of structs, return an error.
Expand Down
9 changes: 5 additions & 4 deletions eth/core/precompile/stateful_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"pkg.berachain.dev/polaris/eth/common"
"pkg.berachain.dev/polaris/eth/core/vm"
"pkg.berachain.dev/polaris/lib/utils"
)

// NumBytesMethodID is the number of bytes used to represent a ABI method's ID.
Expand All @@ -42,7 +41,7 @@ type statefulContainer struct {
// method signatures) to native precompile functions. The signature key is provided by the
// precompile creator and must exactly match the signature in the geth abi.Method.Sig field
// (geth abi format). Please check core/precompile/container/method.go for more information.
idsToMethods map[string]*method
idsToMethods map[[4]byte]*method
itsdevbear marked this conversation as resolved.
Show resolved Hide resolved
// receive *Method // TODO: implement
// fallback *Method // TODO: implement

Expand All @@ -51,7 +50,7 @@ type statefulContainer struct {
// NewStatefulContainer creates and returns a new `statefulContainer` with the given method ids
// precompile functions map.
func NewStatefulContainer(
si StatefulImpl, idsToMethods map[string]*method,
si StatefulImpl, idsToMethods map[[4]byte]*method,
) (vm.PrecompileContainer, error) {
if idsToMethods == nil {
return nil, ErrContainerHasNoMethods
Expand All @@ -78,7 +77,9 @@ func (sc *statefulContainer) Run(
}

// Extract the method ID from the input and load the method.
method, found := sc.idsToMethods[utils.UnsafeBytesToStr(input[:NumBytesMethodID])]

methodID := input[:NumBytesMethodID]
method, found := sc.idsToMethods[[4]byte(methodID)]
if !found {
return nil, ErrMethodNotFound
}
Expand Down
15 changes: 7 additions & 8 deletions eth/core/precompile/stateful_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"pkg.berachain.dev/polaris/eth/common"
"pkg.berachain.dev/polaris/eth/core/vm"
vmmock "pkg.berachain.dev/polaris/eth/core/vm/mock"
"pkg.berachain.dev/polaris/lib/utils"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -159,33 +158,33 @@ var (
contractFuncStrInputFunc, _ = reflect.TypeOf(mockStatefulDummy).MethodByName("ContractFuncStrInput")
overloadedFunc, _ = reflect.TypeOf(mockStatefulDummy).MethodByName("OverloadedFunc")
overloadedFunc0, _ = reflect.TypeOf(mockStatefulDummy).MethodByName("OverloadedFunc0")
mockIdsToMethods = map[string]*method{
utils.UnsafeBytesToStr(getOutputABI.ID): newMethod(
mockIdsToMethods = map[[4]byte]*method{
[4]byte(getOutputABI.ID): newMethod(
mockStatefulDummy,
getOutputABI,
getOutputFunc,
),
utils.UnsafeBytesToStr(getOutputPartialABI.ID): newMethod(
[4]byte(getOutputPartialABI.ID): newMethod(
mockStatefulDummy,
getOutputPartialABI,
getOutputPartialFunc,
),
utils.UnsafeBytesToStr(contractFuncAddrABI.ID): newMethod(
[4]byte(contractFuncAddrABI.ID): newMethod(
mockStatefulDummy,
contractFuncAddrABI,
contractFuncAddrInputFunc,
),
utils.UnsafeBytesToStr(contractFuncStrABI.ID): newMethod(
[4]byte(contractFuncStrABI.ID): newMethod(
mockStatefulDummy,
contractFuncStrABI,
contractFuncStrInputFunc,
),
utils.UnsafeBytesToStr(overloadedFuncABI.ID): newMethod(
[4]byte(overloadedFuncABI.ID): newMethod(
mockStatefulDummy,
overloadedFuncABI,
overloadedFunc,
),
utils.UnsafeBytesToStr(contractFuncStrABI.ID): newMethod(
[4]byte(contractFuncStrABI.ID): newMethod(
mockStatefulDummy,
overloadedFunc0ABI,
overloadedFunc0,
Expand Down
53 changes: 0 additions & 53 deletions lib/utils/unsafe_bytes.go

This file was deleted.

65 changes: 0 additions & 65 deletions lib/utils/unsafe_bytes_test.go

This file was deleted.

Loading