Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4byte signature & geth test server connection issue #217

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 15 additions & 19 deletions 4byte/4byte.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fourbyte

import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
Expand All @@ -12,40 +11,37 @@ const (
)

// Resolve resolves a method/event signature
func Resolve(str string) (string, error) {
return get("/api/v1/signatures/?hex_signature=" + str)
func Resolve(str string) ([]string, error) {
return get("/api/v1/signatures/?hex_signature=" + str + "&ordering=created_at")
}

// ResolveBytes resolves a method/event signature in bytes
func ResolveBytes(b []byte) (string, error) {
return Resolve(hex.EncodeToString(b))
}

func get(path string) (string, error) {
func get(path string) ([]string, error) {
req, err := http.Get(fourByteURL + path)
if err != nil {
return "", err
return nil, err
}
defer req.Body.Close()

data, err := ioutil.ReadAll(req.Body)
if err != nil {
return "", err
return nil, err
}

var result struct {
Results []signatureResult
Results []struct {
TextSignature string `json:"text_signature"`
}
}
if err := json.Unmarshal(data, &result); err != nil {
return "", err
return nil, err
}

if len(result.Results) == 0 {
return "", nil
return nil, nil
}
return result.Results[0].TextSignature, nil
}

type signatureResult struct {
TextSignature string `json:"text_signature"`
signatures := make([]string, 0)
for _, r := range result.Results {
signatures = append(signatures, r.TextSignature)
}
return signatures, nil
}
16 changes: 11 additions & 5 deletions 4byte/4byte_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package fourbyte

import (
"testing"

"github.com/stretchr/testify/assert"
"testing"
)

func Test4Byte(t *testing.T) {
t.Skip("http api not stable, skip for now")

var cases = []struct {
in, out string
}{
Expand All @@ -24,7 +21,16 @@ func Test4Byte(t *testing.T) {
for _, i := range cases {
found, err := Resolve(i.in)
assert.NoError(t, err)
assert.Equal(t, i.out, found)
assert.Equal(t, contains(found, i.out), true)
}

}

func contains[T comparable](s []T, e T) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
google.golang.org/appengine v1.6.5 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc=
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down
5 changes: 3 additions & 2 deletions testutil/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {

opts := &dockertest.RunOptions{
Repository: "ethereum/client-go",
Tag: "v1.10.15",
Tag: "v1.10.25",
Cmd: args,
Mounts: []string{
tmpDir + ":/eth1data",
Expand Down Expand Up @@ -169,7 +169,8 @@ func (t *TestServer) WSAddr() string {

// HTTPAddr returns the http endpoint
func (t *TestServer) HTTPAddr() string {
return fmt.Sprintf("http://%s:8545", t.resource.Container.NetworkSettings.IPAddress)
port := t.resource.GetPort("8545/tcp")
return fmt.Sprintf("http://localhost:%s", port)
}

// ProcessBlock processes a new block
Expand Down