Skip to content

Commit

Permalink
Update web3_clientVersion endpoint to return a correct value (#1965)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman committed Oct 9, 2023
1 parent 027538d commit bbb52d5
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ protoc: check-protoc

.PHONY: build
build: check-go check-git
$(eval LATEST_VERSION = $(shell git describe --tags --abbrev=0))
$(eval COMMIT_HASH = $(shell git rev-parse HEAD))
$(eval VERSION = $(shell git tag --points-at $COMMIT_HASH))
$(eval BRANCH = $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n'))
$(eval TIME = $(shell date))
go build -o polygon-edge -ldflags="\
-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)' \
-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(VERSION)' \
-X 'github.com/0xPolygon/polygon-edge/versioning.Commit=$(COMMIT_HASH)'\
-X 'github.com/0xPolygon/polygon-edge/versioning.Branch=$(BRANCH)'\
-X 'github.com/0xPolygon/polygon-edge/versioning.BuildTime=$(TIME)'" \
Expand Down
2 changes: 1 addition & 1 deletion consensus/ibft/signer/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func verifyIBFTExtraSize(header *types.Header) error {
return nil
}

// UseIstanbulHeaderHash is a helper function for the test
// UseIstanbulHeaderHashInTest is a helper function for the test
func UseIstanbulHeaderHashInTest(t *testing.T, signer Signer) {
t.Helper()

Expand Down
1 change: 1 addition & 0 deletions consensus/polybft/signer/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestPrivate_Marshal(t *testing.T) {
// marshal public key
privateKeyMarshalled, err := blsKey.Marshal()
require.NoError(t, err)

// recover private and public key
blsKeyUnmarshalled, err := UnmarshalPrivateKey(privateKeyMarshalled)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ func generateBLSKeyAndMarshal() ([]byte, error) {
return buf, nil
}

// BytesToECDSAPrivateKey reads the input byte array and constructs a private key if possible
// BytesToBLSSecretKey reads the input byte array and constructs a private key if possible
func BytesToBLSSecretKey(input []byte) (*bls_sig.SecretKey, error) {
// The key file on disk should be encoded in Base64,
// The key file on disk should be encoded in hex,
// so it must be decoded before it can be parsed by ParsePrivateKey
decoded, err := hex.DecodeString(string(input))
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion docker/local/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
FROM golang:1.20-alpine AS builder

RUN apk add make git

WORKDIR /polygon-edge

ADD go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build -o polygon-edge main.go
RUN make build

FROM alpine:latest AS runner

Expand Down
6 changes: 2 additions & 4 deletions e2e-polybft/e2e/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestE2E_JsonRPC(t *testing.T) {

jsonRPC := cluster.Servers[0].JSONRPC()
client := jsonRPC.Eth()
debug := jsonRPC.Debug()

// Test eth_call with override in state diff
t.Run("eth_call state override", func(t *testing.T) {
Expand Down Expand Up @@ -380,10 +381,7 @@ func TestE2E_JsonRPC(t *testing.T) {
txReceipt := txn.Receipt()

// Use a wrapper function from "jsonrpc" package when the config is introduced.
var trace *jsonrpc.TransactionTrace
err = jsonRPC.Call("debug_traceTransaction", &trace, txReceipt.TransactionHash, map[string]interface{}{
"tracer": "callTracer",
})
trace, err := debug.TraceTransaction(txReceipt.TransactionHash, jsonrpc.TraceTransactionOptions{})
require.NoError(t, err)
require.Equal(t, txReceipt.GasUsed, trace.Gas)
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mitchellh/mapstructure v1.5.0
github.com/umbracle/ethgo v0.1.4-0.20230810113823-c9c19bcd8a1e
github.com/umbracle/ethgo v0.1.4-0.20231006072852-6b068360fc97
github.com/valyala/fastjson v1.6.3 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,8 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/umbracle/ethgo v0.1.4-0.20230810113823-c9c19bcd8a1e h1:CPUqjupBwC5EpV/eY/K+E65ZvFZkcXYgWf6KzypBQDY=
github.com/umbracle/ethgo v0.1.4-0.20230810113823-c9c19bcd8a1e/go.mod h1:J+OZNfRCtbaYW3AEc0m47GhwAzlNJjcr9vO86nzOr6E=
github.com/umbracle/ethgo v0.1.4-0.20231006072852-6b068360fc97 h1:BAVsVHkgvBlQcE+/vUG58w/p7vAhgYaY/yUo4eHTfc8=
github.com/umbracle/ethgo v0.1.4-0.20231006072852-6b068360fc97/go.mod h1:J+OZNfRCtbaYW3AEc0m47GhwAzlNJjcr9vO86nzOr6E=
github.com/umbracle/fastrlp v0.1.1-0.20230504065717-58a1b8a9929d h1:HAg1Kpr9buwRxEiC2UXU9oT2AU8uCU7o3/WTH+Lt5wo=
github.com/umbracle/fastrlp v0.1.1-0.20230504065717-58a1b8a9929d/go.mod h1:5RHgqiFjd4vLJESMWagP/E7su+5Gzk0iqqmrotR8WdA=
github.com/umbracle/go-eth-bn256 v0.0.0-20230125114011-47cb310d9b0b h1:5/xofhZiOG0I9DQXqDSPxqYObk6QI7mBGMJI+ngyIgc=
Expand Down
4 changes: 2 additions & 2 deletions helper/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CreateIfNotExists(path string, create createFn) ([]byte, error) {
return nil, fmt.Errorf("unable to generate private key, %w", err)
}

// Encode it to a readable format (Base64) and write to disk
// Encode it to a readable format (hex) and write to disk
keyBuff = []byte(hex.EncodeToString(keyBuff))
if err = common.SaveFileSafe(path, keyBuff, 0440); err != nil {
return nil, fmt.Errorf("unable to write private key to disk (%s), %w", path, err)
Expand All @@ -50,6 +50,6 @@ func CreatePrivateKey(create createFn) ([]byte, error) {
return nil, fmt.Errorf("unable to generate private key, %w", err)
}

// Encode it to a readable format (Base64) and return
// Encode it to a readable format (hex) and return
return []byte(hex.EncodeToString(keyBuff)), nil
}
1 change: 0 additions & 1 deletion jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (d *Dispatcher) registerEndpoints(store JSONRPCStore) error {
d.params.chainID,
}
d.endpoints.Web3 = &Web3{
d.params.chainID,
d.params.chainName,
}
d.endpoints.TxPool = &TxPool{
Expand Down
19 changes: 15 additions & 4 deletions jsonrpc/web3_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@ package jsonrpc

import (
"fmt"
"runtime"

"github.com/0xPolygon/polygon-edge/helper/keccak"
"github.com/0xPolygon/polygon-edge/versioning"
)

// Web3 is the web3 jsonrpc endpoint
type Web3 struct {
chainID uint64
chainName string
}

var clientVersionTemplate = "%s [chain-id: %d] [version: %s]"
var clientVersionTemplate = "%s/%s/%s-%s/%s"

// ClientVersion returns the version of the web3 client (web3_clientVersion)
// Example: "polygon-edge-53105/v1.1.0/linux-amd64/go1.20.0"
// Spec: https://ethereum.org/en/developers/docs/apis/json-rpc/#web3_clientversion
func (w *Web3) ClientVersion() (interface{}, error) {
var version string
if versioning.Version != "" {
version = versioning.Version
} else if versioning.Commit != "" {
version = versioning.Commit[:8]
}

return fmt.Sprintf(
clientVersionTemplate,
w.chainName,
w.chainID,
versioning.Version,
version,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
), nil
}

Expand Down
8 changes: 5 additions & 3 deletions jsonrpc/web3_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonrpc

import (
"fmt"
"runtime"
"testing"

"github.com/0xPolygon/polygon-edge/versioning"
Expand Down Expand Up @@ -61,11 +62,12 @@ func TestWeb3EndpointClientVersion(t *testing.T) {

assert.NoError(t, expectJSONResult(resp, &res))
assert.Contains(t, res,
fmt.Sprintf(
clientVersionTemplate,
fmt.Sprintf("%s/%s/%s-%s/%s",
chainName,
chainID,
versioning.Version,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
),
)
}
10 changes: 4 additions & 6 deletions secrets/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"errors"
"fmt"

"github.com/hashicorp/go-hclog"
libp2pCrypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/network"
Expand All @@ -13,14 +17,8 @@ import (
"github.com/0xPolygon/polygon-edge/secrets/hashicorpvault"
"github.com/0xPolygon/polygon-edge/secrets/local"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
libp2pCrypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/umbracle/ethgo/abi"
)

var addressTypeABI = abi.MustNewType("address")

// SetupLocalSecretsManager is a helper method for boilerplate local secrets manager setup
func SetupLocalSecretsManager(dataDir string) (secrets.SecretsManager, error) {
return local.SecretsManagerFactory(
Expand Down
3 changes: 2 additions & 1 deletion versioning/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package versioning

var (
// Version is the main version at the moment.
// Commit is the git commit that the binary was built on
// Branch is the current branch name.
// Commit is the git commit that the binary was built on.
// BuildTime is the timestamp of the build
// Embedded by --ldflags on build time
// Versioning should follow the SemVer guidelines
Expand Down

0 comments on commit bbb52d5

Please sign in to comment.