Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Enable more linters #861

Merged
merged 12 commits into from
Aug 27, 2020
12 changes: 9 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
linters:
disable-all: true
disable-all: false
enable:
- errcheck
- govet
# Finds repeated strings that could be made constants
- goconst
# Ensures that the code was formatted with `gofmt -s`
- gofmt
# Identifies commonly misspelled words
- misspell
# Identifies unused function parameters
- unparam
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ deps-ts-no-lockfile:
yarn install --frozen-lockfile


# Provides a pre-commit convenience command that runs all of the tests and the linters
.PHONY: check
check: test-all lint


.PHONY: test-all
test-all: test-go test-wasm-browser test-ts test-browser-conversion test-browser-integration

Expand Down
36 changes: 13 additions & 23 deletions cmd/cut-release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func generateTypescriptDocs() {
}
}

const (
captureVersionString = `"version": "(.*)"`
captureMeshBrowserVersionString = `"@0x/mesh-browser": "(.*)"`
captureMeshBrowserLiteVersionString = `"@0x/mesh-browser-lite": "(.*)"`
)

// Update the version string in all files that must be updated for a new release
func updateHardCodedVersions(version string) {
newVersionString := fmt.Sprintf(`"version": "%s"`, version)
Expand All @@ -105,37 +111,37 @@ func updateHardCodedVersions(version string) {

jalextowle marked this conversation as resolved.
Show resolved Hide resolved
// Update `packages/mesh-browser-lite/package.json`
browserLitePackageJSONPath := "packages/mesh-browser-lite/package.json"
regex = `"version": "(.*)"`
regex = captureVersionString
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
updateFileWithRegex(browserLitePackageJSONPath, regex, newVersionString)

// Update `packages/mesh-browser/package.json`
browserPackageJSONPath := "packages/mesh-browser/package.json"
regex = `"version": "(.*)"`
regex = captureVersionString
updateFileWithRegex(browserPackageJSONPath, regex, newVersionString)
// NOTE(jalextowle): `@0x/mesh-browser` uses the local version of `@0x/mesh-browser-lite`
// on the `development` branch. Once the `@0x/mesh-browser-lite` package has been published,
// we need to update dependency in `@0x/mesh-browser` to published version.
regex = `"@0x/mesh-browser-lite": "(.*)"`
regex = captureMeshBrowserLiteVersionString
updateFileWithRegex(browserPackageJSONPath, regex, newBrowserLiteDependencyString)

// Update `packages/mesh-webpack-example-lite/package.json`
webpackExampleLitePackageJSONPath := "packages/mesh-webpack-example-lite/package.json"
regex = `"@0x/mesh-browser-lite": "(.*)"`
regex = captureMeshBrowserLiteVersionString
updateFileWithRegex(webpackExampleLitePackageJSONPath, regex, newBrowserLiteDependencyString)

// Update `packages/mesh-webpack-example/package.json`
webpackExamplePackageJSONPath := "packages/mesh-webpack-example/package.json"
regex = `"@0x/mesh-browser": "(.*)"`
regex = captureMeshBrowserVersionString
updateFileWithRegex(webpackExamplePackageJSONPath, regex, newBrowserDependencyString)

// Update `packages/mesh-integration-tests/package.json`
integrationTestsPackageJSONPath := "packages/mesh-integration-tests/package.json"
regex = `"@0x/mesh-browser": "(.*)"`
regex = captureMeshBrowserVersionString
updateFileWithRegex(integrationTestsPackageJSONPath, regex, newBrowserDependencyString)

// Update `packages/mesh-browser-shim/package.json`
testWasmPackageJSONPath := "packages/mesh-browser-shim/package.json"
regex = `"@0x/mesh-browser-lite": "(.*)"`
regex = captureMeshBrowserLiteVersionString
updateFileWithRegex(testWasmPackageJSONPath, regex, newBrowserLiteDependencyString)

// Update `core.go`
Expand Down Expand Up @@ -194,19 +200,3 @@ func getDocsCommitHash(docsPath string) (string, error) {
}
return matches[1], nil
}

func getFileContentsWithRegex(filePath string, regex string) (string, error) {
dat, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}

var re = regexp.MustCompile(regex)
matches := re.FindAllStringSubmatch(string(dat), -1)

if len(matches) < 1 || len(matches[0]) < 3 {
return "", errors.New("No contents found")
}

return matches[0][2], nil
}
44 changes: 1 addition & 43 deletions cmd/mesh-bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ package main
import (
"context"
"fmt"
mathrand "math/rand"
"strings"
"time"

"github.com/0xProject/0x-mesh/loghooks"
"github.com/0xProject/0x-mesh/p2p"
"github.com/0xProject/0x-mesh/p2p/banner"
"github.com/ipfs/go-datastore"
leveldbStore "github.com/ipfs/go-ds-leveldb"
libp2p "github.com/libp2p/go-libp2p"
autonat "github.com/libp2p/go-libp2p-autonat-svc"
Expand All @@ -40,23 +38,6 @@ const (
// peerGraceDuration is the amount of time a newly opened connection is given
// before it becomes subject to pruning.
peerGraceDuration = 10 * time.Second
// defaultNetworkTimeout is the default timeout for network requests (e.g.
// connecting to a new peer).
defaultNetworkTimeout = 30 * time.Second
// checkBandwidthLoopInterval is how often to potentially check bandwidth usage
// for peers.
checkBandwidthLoopInterval = 5 * time.Second
// chanceToCheckBandwidthUsage is the approximate ratio of (number of check
// bandwidth loop iterations in which we check bandwidth usage) to (total
// number of check bandwidth loop iterations). We check bandwidth
// non-deterministically in order to prevent spammers from avoiding detection
// by carefully timing their bandwidth usage. So on each iteration of the
// check bandwidth loop we generate a number between 0 and 1. If its less than
// chanceToCheckBandiwdthUsage, we perform a bandwidth check.
chanceToCheckBandwidthUsage = 0.1
// DataStoreType constants
leveldbDataStore = "leveldb"
sqlDataStore = "sqldb"
)

// Config contains configuration options for a Node.
Expand Down Expand Up @@ -135,9 +116,7 @@ func main() {
// We need to declare the newDHT function ahead of time so we can use it in
// the libp2p.Routing option.
var kadDHT *dht.IpfsDHT
var newDHT func(h host.Host) (routing.PeerRouting, error)

newDHT = func(h host.Host) (routing.PeerRouting, error) {
newDHT := func(h host.Host) (routing.PeerRouting, error) {
var err error
dhtDir := getDHTDir(config)
// Set up the DHT to use LevelDB.
Expand Down Expand Up @@ -309,24 +288,3 @@ func parseAddrs(commaSeparatedAddrs string) ([]ma.Multiaddr, error) {
}
return maddrs, nil
}

func continuoslyCheckBandwidth(ctx context.Context, banner *banner.Banner) error {
ticker := time.NewTicker(checkBandwidthLoopInterval)
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
// Check bandwidth usage non-deterministically
if mathrand.Float64() <= chanceToCheckBandwidthUsage {
banner.CheckBandwidthUsage()
}
}
}
}

// NewDHTWithDatastore returns a new Kademlia DHT instance configured with store
// as the persistant storage interface.
func NewDHTWithDatastore(ctx context.Context, store datastore.Batching, host host.Host) (*dht.IpfsDHT, error) {
return dht.New(ctx, host, dhtopts.Datastore(store), dhtopts.Protocols(p2p.DHTProtocolID))
}
5 changes: 0 additions & 5 deletions cmd/mesh-bootstrap/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
_ "github.com/lib/pq" // postgres driver
)

const (
dhtTableName = "dhtkv"
peerStoreTableName = "peerStore"
)

func getPrivateKeyPath(config Config) string {
return filepath.Join(config.LevelDBDataDir, "keys", "privkey")
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/mesh/graphql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ func serveGraphQL(ctx context.Context, app *core.App, addr string, enableGraphiQ
// Start the server
server := &http.Server{Addr: addr, Handler: handler}
go func() {
select {
case <-ctx.Done():
shutdownContext, cancel := context.WithTimeout(context.Background(), gracefulShutdownTimeout)
defer cancel()
_ = server.Shutdown(shutdownContext)
}
<-ctx.Done()
shutdownContext, cancel := context.WithTimeout(context.Background(), gracefulShutdownTimeout)
defer cancel()
_ = server.Shutdown(shutdownContext)
}()
return server.ListenAndServe()
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/peer-id-to-pub-key/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
log.Fatal("expects exactly one argument")
}
peerIDString := os.Args[1]
peerID, err := peer.IDB58Decode(peerIDString)
peerID, err := peer.Decode(peerIDString)
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/0xProject/0x-mesh/zeroex"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
)
Expand Down Expand Up @@ -197,5 +198,5 @@ func BytesToHex(b []byte) string {
if len(b) == 0 {
return "0x"
}
return common.ToHex(b)
return hexutil.Encode(b)
}
30 changes: 12 additions & 18 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/rpc"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
peerstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
log "github.com/sirupsen/logrus"
)
Expand All @@ -48,7 +47,6 @@ const (
ethereumRPCRequestTimeout = 30 * time.Second
peerConnectTimeout = 60 * time.Second
checkNewAddrInterval = 20 * time.Second
expirationPollingInterval = 50 * time.Millisecond
rateLimiterCheckpointInterval = 1 * time.Minute
// estimatedNonPollingEthereumRPCRequestsPer24Hrs is an estimate of the
// minimum number of RPC requests Mesh needs to send (not including block
Expand Down Expand Up @@ -285,14 +283,14 @@ func newWithPrivateConfig(ctx context.Context, config Config, pConfig privateCon
}

// Initialize metadata and check stored chain id (if any).
_, err = initMetadata(config.EthereumChainID, database)
err = initMetadata(config.EthereumChainID, database)
if err != nil {
return nil, err
}

// Initialize ETH JSON-RPC RateLimiter
var ethRPCRateLimiter ratelimit.RateLimiter
if config.EnableEthereumRPCRateLimiting == false {
if !config.EnableEthereumRPCRateLimiting {
ethRPCRateLimiter = ratelimit.NewUnlimited()
} else {
clock := clock.New()
Expand Down Expand Up @@ -460,7 +458,7 @@ func initPrivateKey(path string) (p2pcrypto.PrivKey, error) {
return nil, err
}

func initMetadata(chainID int, database *db.DB) (*types.Metadata, error) {
func initMetadata(chainID int, database *db.DB) error {
metadata, err := database.GetMetadata()
if err != nil {
if err == db.ErrNotFound {
Expand All @@ -469,20 +467,20 @@ func initMetadata(chainID int, database *db.DB) (*types.Metadata, error) {
EthereumChainID: chainID,
}
if err := database.SaveMetadata(metadata); err != nil {
return nil, err
return err
}
return metadata, nil
return nil
}
return nil, err
return err
}

// on subsequent startups, verify we are on the same chain
if metadata.EthereumChainID != chainID {
err := fmt.Errorf("expected chainID to be %d but got %d", metadata.EthereumChainID, chainID)
log.WithError(err).Error("Mesh previously started on different Ethereum chain; switch chainId or remove DB")
return nil, err
return err
}
return metadata, nil
return nil
}

func (app *App) Start() error {
Expand Down Expand Up @@ -951,12 +949,8 @@ func (app *App) AddOrdersRaw(ctx context.Context, signedOrdersRaw []*json.RawMes
return nil, err
}

for _, orderInfo := range validationResults.Accepted {
allValidationResults.Accepted = append(allValidationResults.Accepted, orderInfo)
}
for _, orderInfo := range validationResults.Rejected {
allValidationResults.Rejected = append(allValidationResults.Rejected, orderInfo)
}
allValidationResults.Accepted = append(allValidationResults.Accepted, validationResults.Accepted...)
allValidationResults.Rejected = append(allValidationResults.Rejected, validationResults.Rejected...)

for _, acceptedOrderInfo := range allValidationResults.Accepted {
// If the order isn't new, we don't add to OrderWatcher, log it's receipt
Expand Down Expand Up @@ -990,7 +984,7 @@ func (app *App) shareOrder(order *zeroex.SignedOrder) error {
}

// AddPeer can be used to manually connect to a new peer.
func (app *App) AddPeer(peerInfo peerstore.PeerInfo) error {
func (app *App) AddPeer(peerInfo peer.AddrInfo) error {
<-app.started

return app.node.Connect(peerInfo, peerConnectTimeout)
Expand Down
12 changes: 3 additions & 9 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const (
// blockProcessingWaitTime is the amount of time to wait for Mesh to process
// new blocks that have been mined.
blockProcessingWaitTime = 1 * time.Second
// ordersyncWaitTime is the amount of time to wait for ordersync to run.
ordersyncWaitTime = 2 * time.Second
)

func TestEthereumChainDetection(t *testing.T) {
Expand All @@ -41,15 +39,15 @@ func TestEthereumChainDetection(t *testing.T) {
require.NoError(t, err)

// simulate starting up on mainnet
_, err = initMetadata(1, database)
err = initMetadata(1, database)
require.NoError(t, err)

// simulate restart on same chain
_, err = initMetadata(1, database)
err = initMetadata(1, database)
require.NoError(t, err)

// should error when attempting to start on different chain
_, err = initMetadata(2, database)
err = initMetadata(2, database)
assert.Error(t, err)
}

Expand Down Expand Up @@ -94,10 +92,6 @@ func TestConfigChainIDAndRPCMatchDetection(t *testing.T) {
wg.Wait()
}

func newTestApp(t *testing.T, ctx context.Context) *App {
return newTestAppWithPrivateConfig(t, ctx, defaultOrderFilter, defaultPrivateConfig())
}

func newTestAppWithPrivateConfig(t *testing.T, ctx context.Context, customOrderFilter string, pConfig privateConfig) *App {
if customOrderFilter == "" {
customOrderFilter = defaultOrderFilter
Expand Down
Loading