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

Add golangci-lint to CI #67

Merged
merged 9 commits into from
Nov 12, 2020
Merged
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
9 changes: 9 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ on:
branches: [ master ]

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29

build:
name: Build
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ build: clean

buidl: build

lint:
golangci-lint run ./...

init:
./build/hubble init

Expand Down
5 changes: 4 additions & 1 deletion aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func NewAggregator() *Aggregator {

// OnStart starts new block subscription
func (a *Aggregator) OnStart() error {
a.BaseService.OnStart() // Always call the overridden method.
err := a.BaseService.OnStart() // Always call the overridden method.
if err != nil {
return err
}

ctx, cancelAggregating := context.WithCancel(context.Background())
a.cancelAggregating = cancelAggregating
Expand Down
18 changes: 7 additions & 11 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

const (
FlagFromID = "from"
FlagToID = "to"
FlagPrivKey = "privkey"
FlagPubKey = "pubkey"
FlagAmount = "amount"
FlagFee = "fee"
FlagSignature = "sig"
FlagNonce = "nonce"
FlagTokenID = "token"
FlagDatabaseName = "dbname"
FlagNumberOfUsers = "count"
FlagFromID = "from"
FlagToID = "to"
FlagPrivKey = "privkey"
FlagPubKey = "pubkey"
FlagAmount = "amount"
FlagFee = "fee"
FlagDatabaseName = "dbname"
)
9 changes: 7 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ func main() {
)

// bind with-heimdall-config config with root cmd
viper.BindPFlag(
err := viper.BindPFlag(
WithConfigPathFlag,
rootCmd.Flags().Lookup(WithConfigPathFlag),
)
if err != nil {
fmt.Println("BindPFlag Error", err)
return
}

rootCmd.AddCommand(initCmd())
rootCmd.AddCommand(startCmd())
rootCmd.AddCommand(resetCmd())
Expand All @@ -56,7 +61,7 @@ func main() {
rootCmd.AddCommand(migrationCmd)

executor := Executor{rootCmd, os.Exit}
if err := executor.Command.Execute(); err != nil {
if err = executor.Command.Execute(); err != nil {
fmt.Println("Error while executing command", err)
return
}
Expand Down
23 changes: 16 additions & 7 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ func startCmd() *cobra.Command {
go func() {
// sig is a ^C, handle it
for range catchSignal {
aggregator.Stop()
syncer.Stop()
if err := aggregator.Stop(); err != nil {
log.Fatalln("Unable to stop aggregator", "error", err)
}
if err := syncer.Stop(); err != nil {
log.Fatalln("Unable to stop syncer", "error", err)
}

core.DBInstance.Close()
// exit
os.Exit(1)
}
}()

if err := syncer.Start(); err != nil {
log.Fatalln("Unable to start syncer", "error")
log.Fatalln("Unable to start syncer", "error", err)
}

if err := aggregator.Start(); err != nil {
Expand Down Expand Up @@ -143,10 +148,14 @@ func loadGenesisData(genesis config.Genesis) {

// load params
newParams := core.Params{StakeAmount: genesis.StakeAmount, MaxDepth: genesis.MaxTreeDepth, MaxDepositSubTreeHeight: genesis.MaxDepositSubTreeHeight}
core.DBInstance.UpdateStakeAmount(newParams.StakeAmount)
core.DBInstance.UpdateMaxDepth(newParams.MaxDepth)
core.DBInstance.UpdateDepositSubTreeHeight(newParams.MaxDepositSubTreeHeight)
core.DBInstance.UpdateFinalisationTimePerBatch(40320)
err = core.DBInstance.UpdateStakeAmount(newParams.StakeAmount)
common.PanicIfError(err)
err = core.DBInstance.UpdateMaxDepth(newParams.MaxDepth)
common.PanicIfError(err)
err = core.DBInstance.UpdateDepositSubTreeHeight(newParams.MaxDepositSubTreeHeight)
common.PanicIfError(err)
err = core.DBInstance.UpdateFinalisationTimePerBatch(40320)
common.PanicIfError(err)

// load sync status
err = core.DBInstance.InitSyncStatus(genesis.StartEthBlock)
Expand Down
30 changes: 22 additions & 8 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ func sendTransferTx() *cobra.Command {
cmd.Flags().StringP(FlagPubKey, "", "", "--pubkey=<pubkey>")
cmd.Flags().StringP(FlagPrivKey, "", "", "--privkey=<privkey>")
cmd.Flags().StringP(FlagAmount, "", "", "--amount=<amount>")
cmd.MarkFlagRequired(FlagToID)
cmd.MarkFlagRequired(FlagFromID)
cmd.MarkFlagRequired(FlagPubKey)
cmd.MarkFlagRequired(FlagPrivKey)
cmd.MarkFlagRequired(FlagAmount)
err := cmd.MarkFlagRequired(FlagToID)
common.PanicIfError(err)
err = cmd.MarkFlagRequired(FlagFromID)
common.PanicIfError(err)
err = cmd.MarkFlagRequired(FlagPubKey)
common.PanicIfError(err)
err = cmd.MarkFlagRequired(FlagPrivKey)
common.PanicIfError(err)
err = cmd.MarkFlagRequired(FlagAmount)
common.PanicIfError(err)
return cmd
}

Expand Down Expand Up @@ -172,9 +177,18 @@ func validateAndTransfer(db core.DB, bazooka core.Bazooka, fromIndex, toIndex, a
return
}

tx := core.NewPendingTx(fromIndex, toIndex, core.TX_TRANSFER_TYPE, []byte(""), txData)
tx.SignTx(priv, pub, common.Keccak256(tx.GetSignBytes()))
tx.AssignHash()
tx, err := core.NewPendingTx(fromIndex, toIndex, core.TX_TRANSFER_TYPE, []byte(""), txData)
if err != nil {
return
}
err = tx.SignTx(priv, pub, common.Keccak256(tx.GetSignBytes()))
if err != nil {
return
}
err = tx.AssignHash()
if err != nil {
return
}

fmt.Println("Sending new tx", tx.String())

Expand Down
11 changes: 0 additions & 11 deletions common/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,6 @@ func capitalise(input string) string {
return toCamelCase(strings.ToUpper(input[:1]) + input[1:])
}

// decapitalise makes a camel-case string which starts with a lower case character.
func decapitalise(input string) string {
for len(input) > 0 && input[0] == '_' {
input = input[1:]
}
if len(input) == 0 {
return ""
}
return toCamelCase(strings.ToLower(input[:1]) + input[1:])
}

// toCamelCase converts an under-score string to a camel-case string
func toCamelCase(input string) string {
toupper := false
Expand Down
8 changes: 5 additions & 3 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ func KeccakFromString(data string) (hash common.Hash, err error) {

}

func RlpHash(x interface{}) (h common.Hash) {
func RlpHash(x interface{}) (h common.Hash, err error) {
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, x)
if err = rlp.Encode(hw, x); err != nil {
return
}
hw.Sum(h[:0])
return h
return h, nil
}
10 changes: 8 additions & 2 deletions core/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func (db *DB) AddNewAccount(acc Account) error {
// UpdateAccount updates the account
func (db *DB) UpdateAccount(leaf Account) error {
db.Logger.Info("Updated account pubkey", "ID", leaf.ID)
leaf.PopulateHash()
err := leaf.PopulateHash()
if err != nil {
return err
}
siblings, err := db.GetAccountSiblings(leaf.Path)
if err != nil {
return err
Expand Down Expand Up @@ -232,7 +235,10 @@ func (db *DB) storeAccountLeaf(pdaLeaf Account, path string, siblings []Account)
// InsertCoordinatorPubkeyAccounts inserts the coordinator accounts
func (db *DB) InsertCoordinatorPubkeyAccounts(coordinatorAccount *Account, depth uint64) error {
coordinatorAccount.UpdatePath(GenCoordinatorPath(depth))
coordinatorAccount.PopulateHash()
err := coordinatorAccount.PopulateHash()
if err != nil {
return err
}
coordinatorAccount.Type = TYPE_TERMINAL
return db.Instance.Create(&coordinatorAccount).Error
}
Expand Down
7 changes: 6 additions & 1 deletion core/bazooka.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,12 @@ func (b *Bazooka) DecodeTransferTx(txBytes []byte) (from, to, nonce, txType, amo

func (b *Bazooka) EncodeState(id, balance, nonce, token uint64) (accountBytes []byte, err error) {
opts := bind.CallOpts{From: config.OperatorAddress}
accountBytes, err = b.Frontend.Encode(&opts, rollupclient.TypesUserState{big.NewInt(int64(id)), big.NewInt(int64(token)), big.NewInt(int64(balance)), big.NewInt(int64(nonce))})
accountBytes, err = b.Frontend.Encode(&opts, rollupclient.TypesUserState{
PubkeyIndex: big.NewInt(int64(id)),
TokenType: big.NewInt(int64(token)),
Balance: big.NewInt(int64(balance)),
Nonce: big.NewInt(int64(nonce)),
})
if err != nil {
return
}
Expand Down
26 changes: 17 additions & 9 deletions core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ func NewTx(from, to, txType uint64, sig, data []byte) Tx {
}

// NewPendingTx creates a new transaction
func NewPendingTx(from, to, txType uint64, sig, data []byte) Tx {
tx := Tx{
func NewPendingTx(from, to, txType uint64, sig, data []byte) (tx Tx, err error) {
tx = Tx{
To: to,
From: from,
Data: data,
Signature: sig,
Status: TX_STATUS_PENDING,
Type: txType,
}
tx.AssignHash()
return tx

if err = tx.AssignHash(); err != nil {
return
}
return
}

// GetSignBytes returns the transaction data that has to be signed
Expand Down Expand Up @@ -87,12 +90,16 @@ func (tx *Tx) SignTx(key string, pubkey string, txBytes [32]byte) (err error) {
}

// AssignHash creates a tx hash and add it to the tx
func (t *Tx) AssignHash() {
func (t *Tx) AssignHash() (err error) {
if t.TxHash != "" {
return nil
}
hash, err := common.RlpHash(t)
if err != nil {
return
}
hash := common.RlpHash(t)
t.TxHash = hash.String()
return nil
}

func (t *Tx) String() string {
Expand Down Expand Up @@ -186,8 +193,10 @@ func (tx *Tx) GetWitnessTranfer() (fromMerkleProof, toMerkleProof StateMerklePro
dbCopy, _ := NewDB()

// fetch from state MP
DBInstance.FetchMPWithID(tx.From, &fromMerkleProof)

err = DBInstance.FetchMPWithID(tx.From, &fromMerkleProof)
if err != nil {
return
}
toState, err := DBInstance.GetStateByIndex(tx.To)
if err != nil {
return
Expand Down Expand Up @@ -326,7 +335,6 @@ func ProcessTxs(db DB, bz Bazooka, txs []Tx, isSyncing bool) (commitments []Comm
commitment := Commitment{Txs: txInCommitment, UpdatedRoot: newRoot, BatchType: tx.Type, AggregatedSignature: aggregatedSig.ToBytes()}
commitments = append(commitments, commitment)
}
currentRoot = newRoot
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is an ineffectual assignment, but you might want to take a closer look to check if something is missing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, thanks!

}

return commitments, nil
Expand Down
15 changes: 0 additions & 15 deletions core/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"fmt"
"math/big"
"strconv"
"strings"
Expand Down Expand Up @@ -52,16 +51,6 @@ func UintToString(a uint64) string {
return strconv.FormatUint(a, 2)
}

func isRight(path string) bool {
dataRune := []rune(path)
// if the bit is 0
if dataRune[len(path)-1] == 48 {
return false
} else {
return true
}
}

func GetNthBitFromRight(path string, index int) int {
dataRune := []rune(path)
// if the bit is 0
Expand Down Expand Up @@ -112,10 +101,6 @@ func GetAdjacentNodePath(path string) (string, error) {
return pad.Left(UintToString(adjacentNodePath), len(path), "0"), nil
}

func padNumberWithZero(value string, depth uint64) string {
return fmt.Sprintf("%03v", value)
}

// goes from 3 to 000000000011
func SolidityPathToNodePath(path uint64, depth uint64) (string, error) {
pathWithoutPrefix := UintToString(path)
Expand Down
3 changes: 3 additions & 0 deletions core/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func TestBasicPathMutations(t *testing.T) {
panic(err)
}
data, err := StringToUint(newPath)
if err != nil {
panic(err)
}
fmt.Println("path generated", newPath, "data", data)
}

Expand Down
3 changes: 3 additions & 0 deletions listener/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (s *Syncer) SendDepositFinalisationTx() {
}

err = s.loadedBazooka.FireDepositFinalisation(nodeToBeReplaced, siblings, params.MaxDepositSubTreeHeight)
if err != nil {
return
}
}

func (s *Syncer) applyTxsFromBatch(txsBytes []byte, txType uint64, isSyncing bool) (newRoot core.ByteArray, err error) {
Expand Down
5 changes: 3 additions & 2 deletions listener/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewSyncer() Syncer {
if err != nil {
panic(err)
}

//nolint:govet // will fix later in #76
return *syncerService
Comment on lines +69 to 70
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think I can fix this in the short run, created #76 for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I know the issue, I should just move the wait group outside the struct.

}

Expand Down Expand Up @@ -161,7 +161,8 @@ func (s *Syncer) startSubscription(ctx context.Context, subscription ethereum.Su
case err := <-subscription.Err():
// stop service
s.Logger.Error("Error while subscribing new blocks", "error", err)
s.Stop()
err = s.Stop()
s.Logger.Error("Error while stopping", "error", err)

// cancel subscription
s.cancelSubscription()
Expand Down
Loading