diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7cd49f97..12ee6323 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: - name: Install Go uses: actions/setup-go@v1 with: - go-version: 1.16 + go-version: 1.17 - name: "Build binaries" run: make build - name: "Run tests" diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..506a2cf6 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,18 @@ +name: Lint Check + +on: [pull_request] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Go + uses: actions/setup-go@v1 + with: + go-version: 1.17 + - name: "Run lint" + uses: golangci/golangci-lint-action@v3 + with: + version: v1.41 + args: --timeout=10m + only-new-issues: true \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..3230b6e0 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,28 @@ +linters: + enable-all: true + disable: + - golint + - scopelint + - interfacer + - maligned + +service: + golangci-lint-version: 1.41 + +run: + skip-dirs: + - gov/simulation + - gov/client + - simulation + + skip-files: + - _test.go + - "test_.*" + +issues: + new: true + +linters-settings: + errcheck: + check-type-assertions: true + check-blank: false \ No newline at end of file diff --git a/Makefile b/Makefile index 35555901..7ee234dd 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ start-all: LINT_COMMAND := $(shell command -v golangci-lint 2> /dev/null) lint: ifndef LINT_COMMAND - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.23.8 + go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.41.1 endif golangci-lint run diff --git a/bridge/setu/listener/base.go b/bridge/setu/listener/base.go index 3dc142c8..51724a6f 100644 --- a/bridge/setu/listener/base.go +++ b/bridge/setu/listener/base.go @@ -3,6 +3,7 @@ package listener import ( "context" "math/big" + "sync" "time" "github.com/cosmos/cosmos-sdk/client" @@ -172,11 +173,15 @@ func (bl *BaseListener) StartPolling(ctx context.Context, pollInterval time.Dura // the ending of the interval ticker := time.NewTicker(firstInterval) + var tickerOnce sync.Once // start listening for { select { case <-ticker.C: - ticker.Reset(interval) + tickerOnce.Do(func() { + ticker.Reset(interval) + }) + header, err := bl.chainClient.HeaderByNumber(ctx, nil) if err == nil && header != nil { // send data to channel diff --git a/bridge/setu/listener/heimdall.go b/bridge/setu/listener/heimdall.go index c405e06e..2c031118 100644 --- a/bridge/setu/listener/heimdall.go +++ b/bridge/setu/listener/heimdall.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "strconv" + "sync" "time" stakingTypes "github.com/maticnetwork/heimdall/staking/types" @@ -73,6 +74,7 @@ func (hl *HeimdallListener) StartPolling(ctx context.Context, pollInterval time. // the ending of the interval ticker := time.NewTicker(firstInterval) + var tickerOnce sync.Once // var eventTypes []string // eventTypes = append(eventTypes, "message.action='checkpoint'") // eventTypes = append(eventTypes, "message.action='event-record'") @@ -83,7 +85,9 @@ func (hl *HeimdallListener) StartPolling(ctx context.Context, pollInterval time. for { select { case <-ticker.C: - ticker.Reset(interval) + tickerOnce.Do(func() { + ticker.Reset(interval) + }) fromBlock, toBlock, err := hl.fetchFromAndToBlock() if err != nil { hl.Logger.Error("Error fetching fromBlock and toBlock...skipping events query", "error", err) diff --git a/bridge/setu/listener/tron.go b/bridge/setu/listener/tron.go index c35b0da6..9eac7262 100644 --- a/bridge/setu/listener/tron.go +++ b/bridge/setu/listener/tron.go @@ -6,6 +6,7 @@ import ( "encoding/json" "math/big" "strconv" + "sync" "time" "github.com/RichardKnop/machinery/v1/tasks" @@ -93,11 +94,14 @@ func (tl *TronListener) StartPolling(ctx context.Context, pollInterval time.Dura // the ending of the interval ticker := time.NewTicker(firstInterval) + var tickerOnce sync.Once // start listening for { select { case <-ticker.C: - ticker.Reset(interval) + tickerOnce.Do(func() { + ticker.Reset(interval) + }) headerNum, err := tl.contractConnector.GetTronLatestBlockNumber() if err == nil { // send data to channel diff --git a/bridge/setu/processor/checkpoint.go b/bridge/setu/processor/checkpoint.go index dbcd22d8..2ea7d09f 100644 --- a/bridge/setu/processor/checkpoint.go +++ b/bridge/setu/processor/checkpoint.go @@ -10,6 +10,7 @@ import ( "math" "math/big" "strconv" + "sync" "time" cliContext "github.com/cosmos/cosmos-sdk/client/context" @@ -94,18 +95,30 @@ func (cp *CheckpointProcessor) RegisterTasks() { } func (cp *CheckpointProcessor) startPolling(ctx context.Context) { - tickerForNoAck := time.NewTicker(helper.GetConfig().NoACKPollInterval) + now := time.Now() + baseTime := time.Unix(0, 0) + // no-ack ticker interval keep same with checkpoint interval + noAckInterval := helper.GetConfig().CheckpointerPollInterval + // adjust no-ack ticker to tick at the middle of checkpoint interval + firstIntervalForNoAck := noAckInterval - (now.UTC().Sub(baseTime) % noAckInterval) - noAckInterval/2 // nolint: gomnd + if firstIntervalForNoAck <= 0 { + firstIntervalForNoAck += noAckInterval + } + tickerForNoAck := time.NewTicker(firstIntervalForNoAck) syncInterval := helper.GetConfig().CheckpointerPollInterval / 2 tickerForSync := time.NewTicker(syncInterval) // stop ticker when everything done defer tickerForNoAck.Stop() defer tickerForSync.Stop() - cp.Logger.Info("Start polling", "no-ack-interval", helper.GetConfig().NoACKPollInterval, "checkpoint-sync-interval", syncInterval) + cp.Logger.Info("Start polling", "no-ack-interval", noAckInterval, "checkpoint-sync-interval", syncInterval) + + adjustOnce := sync.Once{} for { select { case <-tickerForNoAck.C: + adjustOnce.Do(func() { tickerForNoAck.Reset(noAckInterval) }) go cp.handleCheckpointNoAck() case <-tickerForSync.C: go cp.handleCheckpointSync() @@ -392,11 +405,11 @@ func (cp *CheckpointProcessor) handleCheckpointNoAck() { return } - isNoAckRequired, count := cp.checkIfNoAckIsRequired(checkpointContext, lastCreatedAt) + isNoAckRequired, _ := cp.checkIfNoAckIsRequired(checkpointContext, lastCreatedAt) if isNoAckRequired { var isProposer bool - if isProposer, err = util.IsProposerByIndex(cp.cliCtx, count); err != nil { + if isProposer, err = util.IsValidator(cp.cliCtx); err != nil { cp.Logger.Error("Error checking IsProposer while proposing Checkpoint No-Ack ", "error", err) return } @@ -797,10 +810,10 @@ func (cp *CheckpointProcessor) checkIfNoAckIsRequired(checkpointContext *Checkpo checkpointCreationTime := time.Unix(lastCreatedAt, 0) currentTime := time.Now().UTC() + timeDiff := currentTime.Sub(checkpointCreationTime) - // check if last checkpoint was < NoACK wait time - if timeDiff.Seconds() >= helper.GetConfig().NoACKWaitTime.Seconds() && index == 0 { - index = math.Floor(timeDiff.Seconds() / helper.GetConfig().NoACKWaitTime.Seconds()) + if timeDiff.Seconds() >= helper.GetConfig().CheckpointerPollInterval.Seconds() && index == 0 { + index = math.Floor(timeDiff.Seconds() / helper.GetConfig().CheckpointerPollInterval.Seconds()) } if index == 0 { @@ -812,7 +825,6 @@ func (cp *CheckpointProcessor) checkIfNoAckIsRequired(checkpointContext *Checkpo // check if difference between no-ack time and current time lastNoAck := cp.getLastNoAckTime() - lastNoAckTime := time.Unix(int64(lastNoAck), 0) // if last no ack == 0 , first no-ack to be sent if currentTime.Sub(lastNoAckTime).Seconds() < checkpointParams.CheckpointBufferTime.Seconds() && lastNoAck != 0 { diff --git a/bridge/setu/util/common.go b/bridge/setu/util/common.go index 96ded4a9..1451f9ae 100644 --- a/bridge/setu/util/common.go +++ b/bridge/setu/util/common.go @@ -120,6 +120,34 @@ func IsProposerByIndex(cliCtx cliContext.CLIContext, index uint64) (bool, error) return false, nil } +func IsValidator(cliCtx cliContext.CLIContext) (bool, error) { + var validatorSet hmtypes.ValidatorSet + + result, err := helper.FetchFromAPI(cliCtx, + helper.GetHeimdallServerEndpoint(CurrentValidatorSetURL), + ) + if err != nil { + logger.Error("Error fetching proposers", "url", CurrentValidatorSetURL, "error", err) + + return false, fmt.Errorf("failed to query heimdall server: %w", err) + } + + err = json.Unmarshal(result.Result, &validatorSet) + if err != nil { + logger.Error("error unmarshalling proposer slice", "error", err) + + return false, fmt.Errorf("failed to unmarshal validatorSet: %w", err) + } + + for _, validator := range validatorSet.Validators { + if bytes.Equal(validator.Signer.Bytes(), helper.GetAddress()) { + return true, nil + } + } + + return false, nil +} + // IsInProposerList checks if we are in current proposer func IsInProposerList(cliCtx cliContext.CLIContext, count uint64) (bool, error) { logger.Debug("Skipping proposers", "count", strconv.FormatUint(count, 10)) diff --git a/go.mod b/go.mod index f23f6337..d5c4e432 100644 --- a/go.mod +++ b/go.mod @@ -1,43 +1,23 @@ module github.com/maticnetwork/heimdall -go 1.16 +go 1.17 require ( github.com/RichardKnop/machinery v1.10.6 - github.com/allegro/bigcache v1.2.1 // indirect - github.com/aristanetworks/goarista v0.0.0-20191206003309-5d8d36c240c9 // indirect - github.com/btcsuite/btcd v0.20.1-beta // indirect github.com/cbergoon/merkletree v0.2.0 - github.com/cespare/cp v1.1.1 // indirect github.com/cosmos/cosmos-sdk v0.37.4 - github.com/deckarep/golang-set v1.7.1 // indirect - github.com/edsrzf/mmap-go v1.0.0 // indirect - github.com/elastic/gosigar v0.10.5 // indirect - github.com/fatih/color v1.9.0 // indirect - github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect - github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect github.com/go-kit/kit v0.9.0 github.com/gogo/protobuf v1.3.0 github.com/golang/protobuf v1.5.0 github.com/gorilla/mux v1.7.3 github.com/hashicorp/golang-lru v0.5.3 - github.com/huin/goupnp v1.0.0 // indirect - github.com/jackpal/go-nat-pmp v1.0.2 // indirect - github.com/karalabe/usb v0.0.0-20191104083709-911d15fe12a9 // indirect github.com/maticnetwork/bor v0.1.7-0.20200507151553-e03cd94ed12b - github.com/olekukonko/tablewriter v0.0.4 // indirect github.com/pborman/uuid v1.2.0 - github.com/peterh/liner v1.2.0 // indirect github.com/pkg/errors v0.9.1 - github.com/prometheus/tsdb v0.10.0 // indirect github.com/prysmaticlabs/prysm v0.0.0-20190507024903-1be950f90cad github.com/rakyll/statik v0.1.6 - github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 // indirect - github.com/rjeczalik/notify v0.9.2 // indirect - github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.4.0 - github.com/status-im/keycard-go v0.0.0-20191119114148-6dd40a46baa0 // indirect github.com/streadway/amqp v1.0.0 github.com/stretchr/testify v1.7.0 github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d @@ -45,7 +25,6 @@ require ( github.com/tendermint/go-amino v0.15.0 github.com/tendermint/tendermint v0.32.7 github.com/tendermint/tm-db v0.2.0 - github.com/tyler-smith/go-bip39 v1.0.2 // indirect golang.org/x/sync v0.0.0-20201207232520-09787c993a3a google.golang.org/genproto v0.0.0-20210207032614-bba0dbe2a9ea google.golang.org/grpc v1.37.0 @@ -53,6 +32,121 @@ require ( gopkg.in/yaml.v2 v2.4.0 ) +require ( + cloud.google.com/go v0.76.0 // indirect + cloud.google.com/go/pubsub v1.10.0 // indirect + github.com/RichardKnop/logging v0.0.0-20190827224416-1a693bdd4fae // indirect + github.com/allegro/bigcache v1.2.1 // indirect + github.com/aristanetworks/goarista v0.0.0-20191206003309-5d8d36c240c9 // indirect + github.com/aws/aws-sdk-go v1.37.16 // indirect + github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/bgentry/speakeasy v0.1.0 // indirect + github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect + github.com/btcsuite/btcd v0.20.1-beta // indirect + github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d // indirect + github.com/cespare/cp v1.1.1 // indirect + github.com/cespare/xxhash/v2 v2.1.1 // indirect + github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 // indirect + github.com/cosmos/ledger-cosmos-go v0.10.3 // indirect + github.com/cosmos/ledger-go v0.9.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/deckarep/golang-set v1.7.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/edsrzf/mmap-go v1.0.0 // indirect + github.com/elastic/gosigar v0.10.5 // indirect + github.com/etcd-io/bbolt v1.3.3 // indirect + github.com/fatih/color v1.9.0 // indirect + github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect + github.com/fsnotify/fsnotify v1.4.9 // indirect + github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect + github.com/go-logfmt/logfmt v0.4.0 // indirect + github.com/go-redis/redis/v8 v8.6.0 // indirect + github.com/go-redsync/redsync/v4 v4.0.4 // indirect + github.com/go-stack/stack v1.8.0 // indirect + github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect + github.com/golang/snappy v0.0.2 // indirect + github.com/gomodule/redigo v2.0.0+incompatible // indirect + github.com/google/go-cmp v0.5.5 // indirect + github.com/google/uuid v1.2.0 // indirect + github.com/googleapis/gax-go/v2 v2.0.5 // indirect + github.com/gorilla/websocket v1.4.1 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/huin/goupnp v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/jackpal/go-nat-pmp v1.0.2 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/jmhodges/levigo v1.0.0 // indirect + github.com/jstemmer/go-junit-report v0.9.1 // indirect + github.com/karalabe/usb v0.0.0-20191104083709-911d15fe12a9 // indirect + github.com/kelseyhightower/envconfig v1.4.0 // indirect + github.com/klauspost/compress v1.11.7 // indirect + github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect + github.com/libp2p/go-buffer-pool v0.0.2 // indirect + github.com/magiconair/properties v1.8.1 // indirect + github.com/mattn/go-colorable v0.1.4 // indirect + github.com/mattn/go-isatty v0.0.11 // indirect + github.com/mattn/go-runewidth v0.0.7 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/olekukonko/tablewriter v0.0.4 // indirect + github.com/opentracing/opentracing-go v1.2.0 // indirect + github.com/pelletier/go-toml v1.7.0 // indirect + github.com/peterh/liner v1.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_golang v1.1.0 // indirect + github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect + github.com/prometheus/common v0.6.0 // indirect + github.com/prometheus/procfs v0.0.3 // indirect + github.com/prometheus/tsdb v0.10.0 // indirect + github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 // indirect + github.com/rjeczalik/notify v0.9.2 // indirect + github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff // indirect + github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/rs/cors v1.7.0 // indirect + github.com/spf13/afero v1.2.2 // indirect + github.com/spf13/cast v1.3.0 // indirect + github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.3 // indirect + github.com/status-im/keycard-go v0.0.0-20191119114148-6dd40a46baa0 // indirect + github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 // indirect + github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 // indirect + github.com/stretchr/objx v0.1.1 // indirect + github.com/stumble/gorocksdb v0.0.3 // indirect + github.com/tendermint/btcd v0.1.1 // indirect + github.com/tendermint/iavl v0.12.4 // indirect + github.com/tyler-smith/go-bip39 v1.0.2 // indirect + github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 // indirect + github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect + github.com/xdg/stringprep v1.0.0 // indirect + github.com/xsleonard/go-merkle v1.1.0 // indirect + github.com/zondax/hid v0.9.0 // indirect + go.mongodb.org/mongo-driver v1.4.6 // indirect + go.opencensus.io v0.22.6 // indirect + go.opentelemetry.io/otel v0.17.0 // indirect + go.opentelemetry.io/otel/metric v0.17.0 // indirect + go.opentelemetry.io/otel/trace v0.17.0 // indirect + golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect + golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect + golang.org/x/mod v0.4.1 // indirect + golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect + golang.org/x/oauth2 v0.0.0-20210201163806-010130855d6c // indirect + golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect + golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect + golang.org/x/text v0.3.5 // indirect + golang.org/x/tools v0.1.0 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + google.golang.org/api v0.39.0 // indirect + google.golang.org/appengine v1.6.7 // indirect + gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect + gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200316214253-d7b0ff38cac9 // indirect + gopkg.in/sourcemap.v1 v1.0.5 // indirect + gopkg.in/urfave/cli.v1 v1.20.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) + replace github.com/tendermint/tendermint => github.com/maticnetwork/tendermint v0.26.0-dev0.0.20200429080413-edc079e7d4c9 replace github.com/cosmos/cosmos-sdk => github.com/maticnetwork/cosmos-sdk v0.37.5-0.20200503092858-55131f25dd9d