Skip to content

Commit

Permalink
opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
6boris committed Aug 29, 2023
1 parent 6699c9e commit 6367a23
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 285 deletions.
64 changes: 64 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This file configures github.com/golangci/golangci-lint.

run:
timeout: 20m
tests: true
# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true
skip-files:
- core/genesis_alloc.go

linters:
disable-all: true
enable:
- goconst
- goimports
- gosimple
- govet
- ineffassign
- misspell
- unconvert
- typecheck
- unused
- staticcheck
- bidichk
- durationcheck
- exportloopref
- whitespace

# - structcheck # lots of false positives
# - errcheck #lot of false positives
# - contextcheck
# - errchkjson # lots of false positives
# - errorlint # this check crashes
# - exhaustive # silly check
# - makezero # false positives
# - nilerr # several intentional

linters-settings:
gofmt:
simplify: true
goconst:
min-len: 3 # minimum length of string constant
min-occurrences: 6 # minimum number of occurrences

issues:
exclude-rules:
- path: crypto/bn256/cloudflare/optate.go
linters:
- deadcode
- staticcheck
- path: internal/build/pgp.go
text: 'SA1019: "golang.org/x/crypto/openpgp" is deprecated: this package is unmaintained except for security fixes.'
- path: core/vm/contracts.go
text: 'SA1019: "golang.org/x/crypto/ripemd160" is deprecated: RIPEMD-160 is a legacy hash and should not be used for new applications.'
- path: accounts/usbwallet/trezor.go
text: 'SA1019: "github.com/golang/protobuf/proto" is deprecated: Use the "google.golang.org/protobuf/proto" package instead.'
- path: accounts/usbwallet/trezor/
text: 'SA1019: "github.com/golang/protobuf/proto" is deprecated: Use the "google.golang.org/protobuf/proto" package instead.'
exclude:
- 'SA1019: event.TypeMux is deprecated: use Feed'
- 'SA1019: strings.Title is deprecated'
- 'SA1019: strings.Title has been deprecated since Go 1.18 and an alternative has been available since Go 1.0: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.'
- 'SA1029: should not use built-in type string as key for value'
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ node:

console:
geth attach test/geth/data/geth.ipc

lint:
golangci-lint run ./...
clean:
rm -rf test/geth/data
init:
Expand Down
18 changes: 9 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package client
import (
"context"
"encoding/json"
"math/big"
"time"

"github.com/6boris/web3-go/consts"
"github.com/6boris/web3-go/pkg/otel"
"github.com/ethereum/go-ethereum"
Expand All @@ -12,8 +15,6 @@ import (
"github.com/ethereum/go-ethereum/rpc"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"math/big"
"time"
)

type Client struct {
Expand Down Expand Up @@ -54,8 +55,8 @@ func NewClient(conf *ConfClient) (*Client, error) {
}

func (ec *Client) _beforeHooks(ctx context.Context, meta *Metadata) {
_ = ctx
meta.StartAt = time.Now()

}
func (ec *Client) _afterHooks(ctx context.Context, meta *Metadata) {
otel.MetricsWeb3RequestCounter.Add(ctx, 1, metric.WithAttributes(
Expand All @@ -70,7 +71,7 @@ func (ec *Client) _afterHooks(ctx context.Context, meta *Metadata) {
attribute.Key("abi_method").String(meta.AbiMethod),
attribute.Key("status").String(meta.Status),
))
otel.MetricsWeb3RequestHistogram.Record(ctx, time.Now().Sub(meta.StartAt).Milliseconds(), metric.WithAttributes(
otel.MetricsWeb3RequestHistogram.Record(ctx, time.Since(meta.StartAt).Milliseconds(), metric.WithAttributes(
attribute.Key("client_id").String(ec.ClientID),
attribute.Key("app_id").String(ec.AppID),
attribute.Key("zone").String(ec.AppID),
Expand All @@ -85,7 +86,6 @@ func (ec *Client) _afterHooks(ctx context.Context, meta *Metadata) {

func (ec *Client) Close() {
ec.ethClient.Close()
return
}

// Account Information
Expand Down Expand Up @@ -426,8 +426,8 @@ func (ec *Client) GetUncleCountByBlockHash(ctx context.Context, blockHash common
ec._afterHooks(ctx, meta)
}()
var result string
var err error
err = ec.rpcClient.CallContext(ctx, &result, abiMethod, blockHash)

err := ec.rpcClient.CallContext(ctx, &result, abiMethod, blockHash)
if err != nil {
meta.Status = consts.AbiCallStatusFail
}
Expand All @@ -447,8 +447,8 @@ func (ec *Client) GetUncleCountByBlockNumber(ctx context.Context, number *big.In
ec._afterHooks(ctx, meta)
}()
var result string
var err error
err = ec.rpcClient.CallContext(ctx, &result, abiMethod, number)

err := ec.rpcClient.CallContext(ctx, &result, abiMethod, number)
if err != nil {
meta.Status = consts.AbiCallStatusFail
}
Expand Down
9 changes: 4 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package client

import (
"context"
"github.com/6boris/web3-go/pkg/otel"
"math/big"
"os"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"math/big"
"os"
"testing"
)

var (
Expand All @@ -24,7 +24,6 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
otel.InitProvider()
testPool = NewPool(GetDefaultConfPool())
testCtx = context.TODO()
//beforeTest(config.Conf)
Expand Down
13 changes: 4 additions & 9 deletions client/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package client

import (
"fmt"
"github.com/6boris/web3-go/consts"
"github.com/ethereum/go-ethereum/common"
"github.com/gin-gonic/gin"
"math/big"
"net/http"
"strconv"

"github.com/6boris/web3-go/consts"
"github.com/ethereum/go-ethereum/common"
"github.com/gin-gonic/gin"
)

type GinGinMethodConvert struct {
Expand All @@ -33,11 +34,6 @@ func (g *GinGinMethodConvert) ConvertGinHandler(ctx *gin.Context) {
ctx.JSON(400, ErrReply{Code: 400, Reason: "PARAMS_ERR", Message: err.Error()})
return
}
req.ChainID, err = strconv.ParseInt(ctx.Param("chain_id"), 10, 64)
if err != nil {
ctx.JSON(400, ErrReply{Code: 400, Reason: "PARAMS_ERR", Message: err.Error()})
return
}
client := g.pool.GetClient(req.ChainID)
if client == nil {
ctx.JSON(400, ErrReply{Code: 400, Reason: "PARAMS_ERR", Message: "Not have enable client"})
Expand All @@ -59,7 +55,6 @@ func (g *GinGinMethodConvert) ConvertGinHandler(ctx *gin.Context) {
ctx.JSON(400, ErrReply{Code: 400, Reason: "PARAMS_ERR", Message: fmt.Sprintf("Not support this method:%s", req.Method)})
return
}

}

// eth_chainId https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_chainId
Expand Down
7 changes: 4 additions & 3 deletions client/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package client

import (
"testing"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"testing"
)

func Test_Unite_Convert(t *testing.T) {
t.Run("", func(t *testing.T) {
t.Run("TestServer", func(t *testing.T) {
app := gin.New()
app.GET("/metrics", PromHandler(promhttp.Handler()))
app.POST("/eth_proxy/:chain_id", NewGinMethodConvert(GetDefaultConfPool()).ConvertGinHandler)
app.POST("/", NewGinMethodConvert(GetDefaultConfPool()).ConvertGinHandler)
_ = app.Run(":8545")
})
}
2 changes: 1 addition & 1 deletion client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Metadata struct {
}

type EthCallProxyRequest struct {
ChainID int64 `json:"CHainID"`
ChainID int64 `json:"chain_id"`
ID int64 `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Expand Down
5 changes: 3 additions & 2 deletions client/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package client

import (
"fmt"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_Unite_Pool(t *testing.T) {
Expand Down Expand Up @@ -41,7 +42,7 @@ func Test_Unite_Pool(t *testing.T) {
for _, c := range chainCase {
resp, err := testPool.GetClient(c).BlockNumber(testCtx)
assert.Nil(t, err)
fmt.Println(fmt.Sprintf("ChainID:%d BlockNumber:%d", c, resp))
fmt.Println(fmt.Printf("ChainID:%d BlockNumber:%d", c, resp))
}
})
}
54 changes: 28 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
module github.com/6boris/web3-go

go 1.20
go 1.21

require (
github.com/davecgh/go-spew v1.1.1
github.com/ethereum/go-ethereum v1.12.0
github.com/ethereum/go-ethereum v1.12.2
github.com/gin-gonic/gin v1.9.1
github.com/go-kratos/aegis v0.2.0
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.3
go.opentelemetry.io/otel v1.16.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.17.0
go.opentelemetry.io/otel/exporters/prometheus v0.39.0
go.opentelemetry.io/otel/metric v1.16.0
go.opentelemetry.io/otel/sdk/metric v0.39.0
go.opentelemetry.io/otel/metric v1.17.0
go.opentelemetry.io/otel/sdk v1.17.0
go.opentelemetry.io/otel/sdk/metric v0.40.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/bytedance/sonic v1.10.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand All @@ -30,38 +32,38 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.15.2 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/otel/trace v1.17.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 6367a23

Please sign in to comment.