Skip to content

Commit

Permalink
chore: adding godot, gofumpt and goimports linters (pactus-project#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Aug 17, 2023
1 parent 15ac79d commit af4b8e9
Show file tree
Hide file tree
Showing 119 changed files with 563 additions and 294 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ linters:
- funlen # Consider to enable it
- forbidigo # Consider to enable it
- gci # Consider to enable it (add it to make fmt)
- gofumpt # Consider to enable it (add it to make fmt)
- godot # Consider to enable it
- godox # Consider to enable it (only show warning)
- gocritic # Consider to enable it
- wsl # Consider to enable it
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ devtools:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/bufbuild/buf/cmd/[email protected]
go install mvdan.cc/gofumpt@latest
go install github.com/rakyll/[email protected]

########################################
Expand Down Expand Up @@ -60,6 +61,7 @@ proto:
### Formatting, linting, and vetting
fmt:
gofmt -s -w .
gofumpt -l -w .

check:
golangci-lint run --build-tags "${BUILD_TAG}" --timeout=20m0s
Expand Down
12 changes: 7 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ func TrapSignal(cleanupFunc func()) {

func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
mnemonic string, walletPassword string) (
validatorAddrs []string, rewardAddrs []string, err error) {
validatorAddrs []string, rewardAddrs []string, err error,
) {
// To make process faster, we update the password after creating the addresses
walletPath := PactusDefaultWalletPath(workingDir)
wallet, err := wallet.Create(walletPath, mnemonic, "", chain)
Expand Down Expand Up @@ -323,7 +324,8 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
}

func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*node.Node, *wallet.Wallet, error) {
*node.Node, *wallet.Wallet, error,
) {
gen, err := genesis.LoadFromFile(PactusGenesisPath(workingDir))
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -415,8 +417,7 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string,
return nil, nil, fmt.Errorf("not enough addresses in wallet")
}
for i := 0; i < conf.Node.NumValidators; i++ {
rewardAddrs[i], _ =
crypto.AddressFromString(addrLabels[conf.Node.NumValidators+i].Address)
rewardAddrs[i], _ = crypto.AddressFromString(addrLabels[conf.Node.NumValidators+i].Address)
}
}

Expand All @@ -439,7 +440,8 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
acc := account.NewAccount(0)
acc.AddToBalance(21 * 1e14)
accs := map[crypto.Address]*account.Account{
crypto.TreasuryAddress: acc}
crypto.TreasuryAddress: acc,
}

vals := make([]*validator.Validator, 4)
for i := 0; i < 4; i++ {
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// BuildInitCmd builds the init command for the Pactus blockchain.
func buildInitCmd(parentCmd *cobra.Command) {
var initCmd = &cobra.Command{
initCmd := &cobra.Command{
Use: "init",
Short: "Initialize the Pactus blockchain",
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func main() {
var rootCmd = &cobra.Command{
rootCmd := &cobra.Command{
Use: "pactus-daemon",
Short: "Pactus daemon",
Run: func(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Start starts the pactus node.
func buildStartCmd(parentCmd *cobra.Command) {
var startCmd = &cobra.Command{
startCmd := &cobra.Command{
Use: "start",
Short: "Start the Pactus blockchain",
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Version prints the version of the Pactus node.
func buildVersionCmd(parentCmd *cobra.Command) {
var versionCmd = &cobra.Command{
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the Pactus version",
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/gtk/model_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func (model *walletModel) rebuildModel() {
IDAddressesColumnAddress,
IDAddressesColumnLabel,
IDAddressesColumnBalance,
IDAddressesColumnStake},
IDAddressesColumnStake,
},
[]interface{}{
d[0],
d[1],
Expand Down Expand Up @@ -97,7 +98,8 @@ func (model *walletModel) createAddress() error {
IDAddressesColumnAddress,
IDAddressesColumnLabel,
IDAddressesColumnBalance,
IDAddressesColumnStake},
IDAddressesColumnStake,
},
[]interface{}{
fmt.Sprintf("%v", model.wallet.AddressCount()+1),
address,
Expand Down
6 changes: 4 additions & 2 deletions cmd/gtk/startup_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ func pageSeedRestore(assistant *gtk.Assistant, assistFunc assistantFunc) (*gtk.W
}

func pageSeedConfirm(assistant *gtk.Assistant, assistFunc assistantFunc,
textViewSeed *gtk.TextView) (*gtk.Widget, string) {
textViewSeed *gtk.TextView,
) (*gtk.Widget, string) {
pageWidget := new(gtk.Widget)
textViewConfirmSeed, err := gtk.TextViewNew()
fatalErrorCheck(err)
Expand Down Expand Up @@ -464,7 +465,8 @@ func pagePassword(assistant *gtk.Assistant, assistFunc assistantFunc) (*gtk.Widg
}

func pageNumValidators(assistant *gtk.Assistant,
assistFunc assistantFunc) (*gtk.Widget, *gtk.ListStore, *gtk.ComboBox, string) {
assistFunc assistantFunc,
) (*gtk.Widget, *gtk.ListStore, *gtk.ComboBox, string) {
var pageWidget *gtk.Widget
lsNumValidators, err := gtk.ListStoreNew(glib.TYPE_INT)
fatalErrorCheck(err)
Expand Down
3 changes: 2 additions & 1 deletion cmd/gtk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func updateValidatorHint(lbl *gtk.Label, addr string, w *wallet.Wallet) {
updateHintLabel(lbl, hint)
}
}

func updateAccountHint(lbl *gtk.Label, addr string, w *wallet.Wallet) {
balance, err := w.Balance(addr)
if err != nil {
Expand Down Expand Up @@ -223,7 +224,7 @@ func signAndBroadcastTransaction(parent *gtk.Dialog, msg string, w *wallet.Walle
}
}

// openURLInBrowser open specific url in browser base on os
// openURLInBrowser open specific url in browser base on os.
func openURLInBrowser(address string) error {
cmd := ""
args := make([]string, 0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func buildGenerateCmd(parentCmd *cobra.Command) {

// ChangePassword updates the wallet password.
func buildChangePasswordCmd(parentCmd *cobra.Command) {
var changePasswordCmd = &cobra.Command{
changePasswordCmd := &cobra.Command{
Use: "password",
Short: "Change wallet password",
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func buildAllHistoryCmd(parentCmd *cobra.Command) {
}

func buildAddToHistoryCmd(parentCmd *cobra.Command) {
var addToHistoryCmd = &cobra.Command{
addToHistoryCmd := &cobra.Command{
Use: "add",
Short: "Add a transaction to the wallet history",
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"github.com/spf13/cobra"
)

var pathArg *string
var offlineOpt *bool
var serverAddrOpt *string
var (
pathArg *string
offlineOpt *bool
serverAddrOpt *string
)

func addPasswordOption(c *cobra.Command) *string {
return c.Flags().StringP("password", "p",
Expand Down
3 changes: 2 additions & 1 deletion committee/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func cloneValidator(val *validator.Validator) *validator.Validator {
}

func NewCommittee(validators []*validator.Validator, committeeSize int,
proposerAddress crypto.Address) (Committee, error) {
proposerAddress crypto.Address,
) (Committee, error) {
validatorList := list.New()
var proposerPos *list.Element

Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func SaveTestnetConfig(path string, numValidators int) error {
"/ip4/172.104.46.145/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq",
"/ip4/163.172.178.141/tcp/21777/p2p/12D3KooWDF8a4goNCHriP1y922y4jagaPwHdX4eSrG5WtQpjzS6k",
"/ip4/13.230.117.134/tcp/21777/p2p/12D3KooWBGNEH8NqdK1UddSnPV1yRHGLYpaQUcnujC24s7YNWPiq",
"/ip6/2400:8901::f03c:93ff:fe1c:c3ec/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq"}
"/ip6/2400:8901::f03c:93ff:fe1c:c3ec/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq",
}
conf.Network.Bootstrap.MinThreshold = 4
conf.Network.Bootstrap.MaxThreshold = 8
conf.Network.EnableRelay = true
Expand Down
9 changes: 6 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func TestNodeConfigSanityCheck(t *testing.T) {
t.Run("invalid number of reward addresses", func(t *testing.T) {
conf := DefaultNodeConfig()
conf.RewardAddresses = []string{
ts.RandomAddress().String()}
ts.RandomAddress().String(),
}

assert.Error(t, conf.SanityCheck())
})
Expand All @@ -107,7 +108,8 @@ func TestNodeConfigSanityCheck(t *testing.T) {
conf.NumValidators = 2
conf.RewardAddresses = []string{
ts.RandomAddress().String(),
"abcd"}
"abcd",
}

assert.Error(t, conf.SanityCheck())
})
Expand All @@ -117,7 +119,8 @@ func TestNodeConfigSanityCheck(t *testing.T) {
conf.NumValidators = 2
conf.RewardAddresses = []string{
ts.RandomAddress().String(),
ts.RandomAddress().String()}
ts.RandomAddress().String(),
}

assert.NoError(t, conf.SanityCheck())
})
Expand Down
3 changes: 2 additions & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func NewConsensus(
signer crypto.Signer,
rewardAddr crypto.Address,
broadcastCh chan message.Message,
mediator mediator) Consensus {
mediator mediator,
) Consensus {
cs := &consensus{
config: conf,
state: state,
Expand Down
9 changes: 6 additions & 3 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ func (td *testData) shouldPublishBlockAnnounce(t *testing.T, cons *consensus, ha
}

func (td *testData) shouldPublishProposal(t *testing.T, cons *consensus,
height uint32, round int16) *proposal.Proposal {
height uint32, round int16,
) *proposal.Proposal {
return shouldPublishProposal(t, cons, height, round)
}

func shouldPublishProposal(t *testing.T, cons *consensus,
height uint32, round int16) *proposal.Proposal {
height uint32, round int16,
) *proposal.Proposal {
timeout := time.NewTimer(1 * time.Second)

for {
Expand Down Expand Up @@ -254,7 +256,8 @@ func (td *testData) checkHeightRoundWait(t *testing.T, cons *consensus, height u
}

func (td *testData) addVote(cons *consensus, voteType vote.Type, height uint32, round int16,
blockHash hash.Hash, valID int) *vote.Vote {
blockHash hash.Hash, valID int,
) *vote.Vote {
v := vote.NewVote(voteType, height, round, blockHash, td.signers[valID].Address())
td.signers[valID].SignMsg(v)

Expand Down
1 change: 1 addition & 0 deletions consensus/height_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestNewHeightEntry(t *testing.T) {
assert.True(t, td.consX.active)
assert.NotEqual(t, td.consX.currentState.name(), "new-height")
}

func TestUpdateCertificate(t *testing.T) {
td := setup(t)

Expand Down
3 changes: 2 additions & 1 deletion consensus/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func NewManager(
state state.Facade,
signers []crypto.Signer,
rewardAddrs []crypto.Address,
broadcastCh chan message.Message) Manager {
broadcastCh chan message.Message,
) Manager {
mgr := &manager{
instances: make([]Consensus, len(signers)),
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/mediator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type mediator interface {
Register(cons Consensus)
}

// ConcreteMediator struct
// ConcreteMediator struct.
type ConcreteMediator struct {
instances []Consensus
}
Expand Down Expand Up @@ -47,7 +47,7 @@ func (m *ConcreteMediator) OnBlockAnnounce(from Consensus) {
}
}

// Register a new Consensus instance to the mediator
// Register a new Consensus instance to the mediator.
func (m *ConcreteMediator) Register(cons Consensus) {
m.instances = append(m.instances, cons)
}
11 changes: 11 additions & 0 deletions consensus/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ func MockingConsensus(ts *testsuite.TestSuite, signer crypto.Signer) *MockConsen
Signer: signer,
}
}

func (m *MockConsensus) SignerKey() crypto.PublicKey {
return m.Signer.PublicKey()
}

func (m *MockConsensus) MoveToNewHeight() {
m.lk.Lock()
defer m.lk.Unlock()

m.Height++
}

func (m *MockConsensus) Start() error {
return nil
}
Expand All @@ -65,18 +68,21 @@ func (m *MockConsensus) AddVote(v *vote.Vote) {

m.Votes = append(m.Votes, v)
}

func (m *MockConsensus) AllVotes() []*vote.Vote {
m.lk.Lock()
defer m.lk.Unlock()

return m.Votes
}

func (m *MockConsensus) SetProposal(p *proposal.Proposal) {
m.lk.Lock()
defer m.lk.Unlock()

m.Proposal = p
}

func (m *MockConsensus) HasVote(hash hash.Hash) bool {
m.lk.Lock()
defer m.lk.Unlock()
Expand All @@ -88,6 +94,7 @@ func (m *MockConsensus) HasVote(hash hash.Hash) bool {
}
return false
}

func (m *MockConsensus) RoundProposal(round int16) *proposal.Proposal {
m.lk.Lock()
defer m.lk.Unlock()
Expand All @@ -97,15 +104,18 @@ func (m *MockConsensus) RoundProposal(round int16) *proposal.Proposal {
}
return m.Proposal
}

func (m *MockConsensus) HeightRound() (uint32, int16) {
m.lk.Lock()
defer m.lk.Unlock()

return m.Height, m.Round
}

func (m *MockConsensus) String() string {
return ""
}

func (m *MockConsensus) PickRandomVote(_ int16) *vote.Vote {
m.lk.Lock()
defer m.lk.Unlock()
Expand All @@ -116,6 +126,7 @@ func (m *MockConsensus) PickRandomVote(_ int16) *vote.Vote {
r := m.ts.RandInt32(int32(len(m.Votes)))
return m.Votes[r]
}

func (m *MockConsensus) IsActive() bool {
m.lk.Lock()
defer m.lk.Unlock()
Expand Down
1 change: 1 addition & 0 deletions consensus/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *proposeState) onSetProposal(_ *proposal.Proposal) {
func (s *proposeState) onTimeout(_ *ticker) {
panic("Unreachable")
}

func (s *proposeState) name() string {
return "propose"
}
Loading

0 comments on commit af4b8e9

Please sign in to comment.