Skip to content

Commit 63dbf2f

Browse files
dajohimarcopeereboom
authored andcommitted
multi: various cleanups and error checking (decred#688)
* multi: more error checking * multi: remove unused * multi: add missing copyright
1 parent 90bcf50 commit 63dbf2f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+370
-144
lines changed

politeiad/api/v1/identity/identity.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2016-2017 Company 0, LLC.
2+
// Copyright (c) 2017-2019 The Decred developers
23
// Use of this source code is governed by an ISC
34
// license that can be found in the LICENSE file.
45

politeiad/api/v1/identity/identity_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2016-2017 Company 0, LLC.
2+
// Copyright (c) 2017-2019 The Decred developers
23
// Use of this source code is governed by an ISC
34
// license that can be found in the LICENSE file.
45

politeiad/api/v1/v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiad/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiad/backend/gitbe/anchors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -201,7 +201,7 @@ func (g *gitBackEnd) readAnchorRecord(key [sha256.Size]byte) (*Anchor, error) {
201201
if err != nil {
202202
return nil, err
203203
}
204-
currLine = currLine + linesUsed
204+
currLine += linesUsed
205205

206206
// Check the first line to see if the commit matches the target
207207
firstLine := commit.Message[0]
@@ -248,7 +248,7 @@ func (g *gitBackEnd) readLastAnchorRecord() (*LastAnchor, error) {
248248
if err != nil {
249249
return nil, err
250250
}
251-
currLine = currLine + linesUsed
251+
currLine += linesUsed
252252

253253
// Check the first line of the commit message
254254
// Make sure it is an anchor, not an anchor confirmation
@@ -298,7 +298,7 @@ func (g *gitBackEnd) readUnconfirmedAnchorRecord() (*UnconfirmedAnchor, error) {
298298
if err != nil {
299299
return nil, err
300300
}
301-
currLine = currLine + linesUsed
301+
currLine += linesUsed
302302

303303
// Check the first line of the commit message to see if it is an
304304
// anchor confirmation or an anchor.

politeiad/backend/gitbe/decred.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -228,7 +228,7 @@ func (g *gitBackEnd) replayAllJournals() error {
228228
for _, f := range files {
229229
name := f.Name()
230230
// replay ballot for all props
231-
g.replayBallot(name)
231+
err := g.replayBallot(name)
232232
if err != nil {
233233
return fmt.Errorf("replayAllJournals replayBallot %s %v", name, err)
234234
}
@@ -1069,18 +1069,6 @@ func (g *gitBackEnd) pluginNewComment(payload string) (string, error) {
10691069
return string(ncrb), nil
10701070
}
10711071

1072-
func replyLikeCommentReplyError(failure error) (string, error) {
1073-
lcr := decredplugin.LikeCommentReply{
1074-
Error: failure.Error(),
1075-
}
1076-
lcrb, err := decredplugin.EncodeLikeCommentReply(lcr)
1077-
if err != nil {
1078-
return "", fmt.Errorf("EncodeLikeCommentReply: %v", err)
1079-
}
1080-
1081-
return string(lcrb), nil
1082-
}
1083-
10841072
// pluginLikeComment handles up and down votes of comments.
10851073
func (g *gitBackEnd) pluginLikeComment(payload string) (string, error) {
10861074
log.Tracef("pluginLikeComment")

politeiad/backend/gitbe/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiad/backend/gitbe/git_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -246,7 +246,7 @@ func TestFsck(t *testing.T) {
246246
copy(blobObject, xxx) // restore blob object
247247
corruptBuf := buf.Bytes()
248248
location = len(corruptBuf) - 2 // account for \n
249-
corruptBuf[location] = corruptBuf[location] & 0xdf
249+
corruptBuf[location] &= 0xdf
250250

251251
var bc bytes.Buffer
252252
w2, err := zlib.NewWriterLevel(&bc, 1) // git uses zlib level 1

politeiad/backend/gitbe/gitbe.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ func pijoin(elements ...string) string {
138138
return filepath.Join(elements...)
139139
}
140140

141-
func join(elements ...string) string {
142-
panic(filepath.Join(elements...))
143-
}
144-
145141
// getLatest returns the latest version as a string.
146142
// This function must be called with the lock held.
147143
func getLatest(dir string) (string, error) {

politeiad/backend/gitbe/journal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiad/backend/gitbe/journal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiad/backend/gitbe/readme.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiad/log.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -19,8 +19,7 @@ type logWriter struct{}
1919

2020
func (logWriter) Write(p []byte) (n int, err error) {
2121
os.Stdout.Write(p)
22-
logRotator.Write(p)
23-
return len(p), nil
22+
return logRotator.Write(p)
2423
}
2524

2625
// Loggers per subsystem. A single backend logger is created and all subsytem

politeiad/politeiad.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiavoter/config.go

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2013-2014 The btcsuite developers
2-
// Copyright (c) 2015-2018 The Decred developers
2+
// Copyright (c) 2015-2019 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -18,21 +18,16 @@ import (
1818

1919
"github.com/btcsuite/go-socks/socks"
2020
"github.com/decred/dcrd/dcrutil"
21-
"github.com/decred/politeia/util"
2221
"github.com/decred/politeia/util/version"
2322
flags "github.com/jessevdk/go-flags"
2423
)
2524

2625
const (
27-
defaultConfigFilename = "politeiavoter.conf"
28-
defaultLogLevel = "info"
29-
defaultLogDirname = "logs"
30-
defaultLogFilename = "politeiavoter.log"
31-
defaultIdentityFilename = "identity.json"
32-
defaultWalletHost = "127.0.0.1"
33-
34-
defaultMainnetPort = "49374"
35-
defaultTestnetPort = "59374"
26+
defaultConfigFilename = "politeiavoter.conf"
27+
defaultLogLevel = "info"
28+
defaultLogDirname = "logs"
29+
defaultLogFilename = "politeiavoter.log"
30+
defaultWalletHost = "127.0.0.1"
3631

3732
defaultWalletMainnetPort = "9111"
3833
defaultWalletTestnetPort = "19111"
@@ -42,7 +37,6 @@ var (
4237
defaultHomeDir = dcrutil.AppDataDir("politeiavoter", false)
4338
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename)
4439
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
45-
defaultIdentityFile = filepath.Join(defaultHomeDir, defaultIdentityFilename)
4640
dcrwalletHomeDir = dcrutil.AppDataDir("dcrwallet", false)
4741
defaultWalletCertFile = filepath.Join(dcrwalletHomeDir, "rpc.cert")
4842
)
@@ -184,40 +178,6 @@ func parseAndSetDebugLevels(debugLevel string) error {
184178
return nil
185179
}
186180

187-
// removeDuplicateAddresses returns a new slice with all duplicate entries in
188-
// addrs removed.
189-
func removeDuplicateAddresses(addrs []string) []string {
190-
result := make([]string, 0, len(addrs))
191-
seen := map[string]struct{}{}
192-
for _, val := range addrs {
193-
if _, ok := seen[val]; !ok {
194-
result = append(result, val)
195-
seen[val] = struct{}{}
196-
}
197-
}
198-
return result
199-
}
200-
201-
// normalizeAddresses returns a new slice with all the passed peer addresses
202-
// normalized with the given default port, and all duplicates removed.
203-
func normalizeAddresses(addrs []string, defaultPort string) []string {
204-
for i, addr := range addrs {
205-
addrs[i] = util.NormalizeAddress(addr, defaultPort)
206-
}
207-
208-
return removeDuplicateAddresses(addrs)
209-
}
210-
211-
// filesExists reports whether the named file or directory exists.
212-
func fileExists(name string) bool {
213-
if _, err := os.Stat(name); err != nil {
214-
if os.IsNotExist(err) {
215-
return false
216-
}
217-
}
218-
return true
219-
}
220-
221181
// newConfigParser returns a new command line flags parser.
222182
func newConfigParser(cfg *config, so *serviceOptions, options flags.Options) *flags.Parser {
223183
parser := flags.NewParser(cfg, options)

politeiavoter/log.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 The Decred developers
1+
// Copyright (c) 2017-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -19,8 +19,7 @@ type logWriter struct{}
1919

2020
func (logWriter) Write(p []byte) (n int, err error) {
2121
os.Stdout.Write(p)
22-
logRotator.Write(p)
23-
return len(p), nil
22+
return logRotator.Write(p)
2423
}
2524

2625
// Loggers per subsystem. A single backend logger is created and all subsytem

politeiavoter/params.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2013-2014 The btcsuite developers
2-
// Copyright (c) 2015-2018 The Decred developers
2+
// Copyright (c) 2015-2019 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

politeiavoter/politeiavoter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018 The Decred developers
1+
// Copyright (c) 2018-2019 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

politeiawww/admin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2017-2019 The Decred developers
2+
// Use of this source code is governed by an ISC
3+
// license that can be found in the LICENSE file.
4+
15
package main
26

37
import (

politeiawww/admin_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2017-2019 The Decred developers
2+
// Use of this source code is governed by an ISC
3+
// license that can be found in the LICENSE file.
4+
15
package main
26

37
import (

politeiawww/backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2017-2019 The Decred developers
2+
// Use of this source code is governed by an ISC
3+
// license that can be found in the LICENSE file.
4+
15
package main
26

37
import (

politeiawww/backend_user_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2017-2019 The Decred developers
2+
// Use of this source code is governed by an ISC
3+
// license that can be found in the LICENSE file.
4+
15
package main
26

37
import (

politeiawww/cmd/politeiawww_dataload/config.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ func cleanAndExpandPath(path string) string {
6767
return filepath.Clean(os.ExpandEnv(path))
6868
}
6969

70-
// filesExists reports whether the named file or directory exists.
71-
func fileExists(name string) bool {
72-
if _, err := os.Stat(name); err != nil {
73-
if os.IsNotExist(err) {
74-
return false
75-
}
76-
}
77-
return true
78-
}
79-
8070
// newConfigParser returns a new command line flags parser.
8171
func newConfigParser(cfg *config, options flags.Options) *flags.Parser {
8272
return flags.NewParser(cfg, options)

politeiawww/cmd/politeiawww_dataload/main.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2017-2019 The Decred developers
2+
// Use of this source code is governed by an ISC
3+
// license that can be found in the LICENSE file.
4+
15
package main
26

37
import (
@@ -600,13 +604,6 @@ func updateUserKey() error {
600604
"updateuserkey")
601605
}
602606

603-
func handleError(err error) {
604-
if err != nil {
605-
fmt.Fprintf(os.Stderr, "%v\n", err)
606-
os.Exit(1)
607-
}
608-
}
609-
610607
func deleteExistingData() error {
611608
fmt.Printf("Deleting existing data\n")
612609

@@ -629,15 +626,19 @@ func deleteExistingData() error {
629626
func stopPoliteiad() {
630627
if politeiadCmd != nil {
631628
fmt.Printf("Stopping politeiad\n")
632-
politeiadCmd.Process.Kill()
629+
if err := politeiadCmd.Process.Kill(); err != nil {
630+
fmt.Fprintf(os.Stderr, "unable to kill politeiad: %v", err)
631+
}
633632
politeiadCmd = nil
634633
}
635634
}
636635

637636
func stopPoliteiawww() {
638637
if politeiawwwCmd != nil {
639638
fmt.Printf("Stopping politeiawww\n")
640-
politeiawwwCmd.Process.Kill()
639+
if err := politeiawwwCmd.Process.Kill(); err != nil {
640+
fmt.Fprintf(os.Stderr, "unable to kill politeiawww: %v", err)
641+
}
641642
politeiawwwCmd = nil
642643
}
643644
}

0 commit comments

Comments
 (0)