Skip to content

Commit

Permalink
Add a function to generate frc42 method number (#129)
Browse files Browse the repository at this point in the history
* Add a function to generate frc42 method number
  • Loading branch information
shrenujbansal authored Dec 12, 2022
1 parent 74ec38c commit b3046c0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
5 changes: 3 additions & 2 deletions abi/actor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package abi

import "strconv"
import (
"strconv"
)

// A sequential number assigned to an actor when created by the InitActor.
// This ID is embedded in ID-type addresses.
Expand Down Expand Up @@ -35,4 +37,3 @@ type Multiaddrs = []byte

// PeerID is a byte array representing a Libp2p PeerID
type PeerID = []byte

46 changes: 46 additions & 0 deletions builtin/methods.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package builtin

import (
"encoding/binary"
"github.com/filecoin-project/go-state-types/abi"
"github.com/minio/blake2b-simd"
"golang.org/x/xerrors"
"unicode"
)

const (
Expand Down Expand Up @@ -161,3 +165,45 @@ var MethodsEAM = struct {
Create abi.MethodNum
Create2 abi.MethodNum
}{MethodConstructor, 2, 3}

// Generates a standard FRC-42 compliant method number
// Reference: https://github.com/filecoin-project/FIPs/blob/master/FRCs/frc-0042.md
func GenerateMethodNum(name string) (abi.MethodNum, error) {
err := validateMethodName(name)
if err != nil {
return 0, err
}

if name == "Constructor" {
return MethodConstructor, nil
}

digest := blake2b.Sum512([]byte("1|" + name))

for i := 0; i < 64; i += 4 {
methodId := binary.BigEndian.Uint32(digest[i : i+4])
if methodId >= (1 << 24) {
return abi.MethodNum(methodId), nil
}
}

return abi.MethodNum(0), xerrors.Errorf("Could not generate method num from method name %s:", name)
}

func validateMethodName(name string) error {
if name == "" {
return xerrors.Errorf("empty name string")
}

if !unicode.IsUpper(rune(name[0])) {
return xerrors.Errorf("Method name first letter must be uppercase, method name: %s", name)
}

for _, c := range name {
if !(unicode.IsLetter(c) || unicode.IsDigit(c) || c == '_') {
return xerrors.Errorf("method name has illegal characters, method name: %s", name)
}
}

return nil
}
14 changes: 14 additions & 0 deletions builtin/methods_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package builtin

import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/stretchr/testify/require"
"testing"
)

func TestGenerateMethodNum(t *testing.T) {

methodNum, err := GenerateMethodNum("Receive")
require.NoError(t, err)
require.Equal(t, methodNum, abi.MethodNum(3726118371))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-cid v0.2.0
github.com/ipfs/go-ipld-cbor v0.0.6
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/multiformats/go-multibase v0.0.3
github.com/multiformats/go-multihash v0.0.15
github.com/multiformats/go-varint v0.0.6
Expand Down

0 comments on commit b3046c0

Please sign in to comment.