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 all 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[methodID]*method, error) {
precompileABI := si.ABIMethods()
contractImplType := contractImpl.Type()
idsToMethods := make(map[string]*method)
idsToMethods := make(map[methodID]*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[methodID(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[methodID(abiMethod.ID)]; !found {
return nil, errorslib.Wrap(ErrNoPrecompileMethodForABIMethod, abiMethod.Name)
}
}
Expand Down
3 changes: 3 additions & 0 deletions eth/core/precompile/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"pkg.berachain.dev/polaris/lib/utils"
)

// methodID is a fixed length byte array that represents the method ID of a precompile method.
type methodID [NumBytesMethodID]byte

/**
* Welcome to Stateful Precompiled Contracts! To build a stateful precompile, you must implement
* the `StatefulImpl` interface in `interfaces.go`; below are the suggested steps to
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(methodID(x)).To(Equal(methodID{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
7 changes: 3 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[methodID]*method
// 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[methodID]*method,
) (vm.PrecompileContainer, error) {
if idsToMethods == nil {
return nil, ErrContainerHasNoMethods
Expand All @@ -78,7 +77,7 @@ func (sc *statefulContainer) Run(
}

// Extract the method ID from the input and load the method.
method, found := sc.idsToMethods[utils.UnsafeBytesToStr(input[:NumBytesMethodID])]
method, found := sc.idsToMethods[methodID(input)]
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[methodID]*method{
methodID(getOutputABI.ID): newMethod(
mockStatefulDummy,
getOutputABI,
getOutputFunc,
),
utils.UnsafeBytesToStr(getOutputPartialABI.ID): newMethod(
methodID(getOutputPartialABI.ID): newMethod(
mockStatefulDummy,
getOutputPartialABI,
getOutputPartialFunc,
),
utils.UnsafeBytesToStr(contractFuncAddrABI.ID): newMethod(
methodID(contractFuncAddrABI.ID): newMethod(
mockStatefulDummy,
contractFuncAddrABI,
contractFuncAddrInputFunc,
),
utils.UnsafeBytesToStr(contractFuncStrABI.ID): newMethod(
methodID(contractFuncStrABI.ID): newMethod(
mockStatefulDummy,
contractFuncStrABI,
contractFuncStrInputFunc,
),
utils.UnsafeBytesToStr(overloadedFuncABI.ID): newMethod(
methodID(overloadedFuncABI.ID): newMethod(
mockStatefulDummy,
overloadedFuncABI,
overloadedFunc,
),
utils.UnsafeBytesToStr(contractFuncStrABI.ID): newMethod(
methodID(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