Skip to content

Commit

Permalink
multi: Simplify code per gosimple linter.
Browse files Browse the repository at this point in the history
This simplifies the code based on the recommendations of the gosimple
lint tool.
  • Loading branch information
davecgh committed Nov 3, 2016
1 parent af524fb commit 915fa66
Show file tree
Hide file tree
Showing 43 changed files with 122 additions and 412 deletions.
4 changes: 2 additions & 2 deletions addrmgr/addrmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestNeedMoreAddresses(t *testing.T) {
n := addrmgr.New("testneedmoreaddresses", lookupFunc)
addrsToAdd := 1500
b := n.NeedMoreAddresses()
if b == false {
if !b {
t.Errorf("Expected that we need more addresses")
}
addrs := make([]*wire.NetAddress, addrsToAdd)
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestNeedMoreAddresses(t *testing.T) {
}

b = n.NeedMoreAddresses()
if b == true {
if b {
t.Errorf("Expected that we don't need more addresses")
}
}
Expand Down
11 changes: 4 additions & 7 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,14 +1527,11 @@ func (b *BlockChain) IsCurrent() bool {

// Not current if the latest best block has a timestamp before 24 hours
// ago.
minus24Hours := b.timeSource.AdjustedTime().Add(-24 * time.Hour)
if b.bestNode.timestamp.Before(minus24Hours) {
return false
}

// The chain appears to be current if the above checks did not report
//
// The chain appears to be current if none of the checks reported
// otherwise.
return true
minus24Hours := b.timeSource.AdjustedTime().Add(-24 * time.Hour)
return !b.bestNode.timestamp.Before(minus24Hours)
}

// BestSnapshot returns information about the current best chain block and
Expand Down
10 changes: 2 additions & 8 deletions blockchain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ func TestHaveBlock(t *testing.T) {
t.Errorf("Error loading file: %v\n", err)
return
}
for _, block := range blockTmp {
blocks = append(blocks, block)
}
blocks = append(blocks, blockTmp...)
}

// Create a new database and chain instance to run tests against.
Expand Down Expand Up @@ -119,15 +117,11 @@ func TestHaveBlock(t *testing.T) {
// the returned SequenceLocks are correct for each test instance.
func TestCalcSequenceLock(t *testing.T) {
fileName := "blk_0_to_4.dat.bz2"
blockTmp, err := loadBlocks(fileName)
blocks, err := loadBlocks(fileName)
if err != nil {
t.Errorf("Error loading file: %v\n", err)
return
}
var blocks []*btcutil.Block
for _, block := range blockTmp {
blocks = append(blocks, block)
}

// Create a new database and chain instance to run tests against.
chain, teardownFunc, err := chainSetup("haveblock", &chaincfg.MainNetParams)
Expand Down
5 changes: 1 addition & 4 deletions blockchain/indexers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ func (m *Manager) Init(chain *blockchain.BlockChain) error {
err := m.db.View(func(dbTx database.Tx) error {
idxKey := indexer.Key()
hash, height, err = dbFetchIndexerTip(dbTx, idxKey)
if err != nil {
return err
}
return nil
return err
})
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions blockchain/reorganization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ func TestReorganization(t *testing.T) {
if err != nil {
t.Errorf("Error loading file: %v\n", err)
}
for _, block := range blockTmp {
blocks = append(blocks, block)
}
blocks = append(blocks, blockTmp...)
}

t.Logf("Number of blocks: %v\n", len(blocks))
Expand Down
12 changes: 2 additions & 10 deletions blockchain/scriptval.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,7 @@ func ValidateTransactionScripts(tx *btcutil.Tx, utxoView *UtxoViewpoint, flags t

// Validate all of the inputs.
validator := newTxValidator(utxoView, flags, sigCache)
if err := validator.Validate(txValItems); err != nil {
return err
}

return nil
return validator.Validate(txValItems)
}

// checkBlockScripts executes and validates the scripts for all transactions in
Expand Down Expand Up @@ -248,9 +244,5 @@ func checkBlockScripts(block *btcutil.Block, utxoView *UtxoViewpoint, scriptFlag

// Validate all of the inputs.
validator := newTxValidator(utxoView, scriptFlags, sigCache)
if err := validator.Validate(txValItems); err != nil {
return err
}

return nil
return validator.Validate(txValItems)
}
4 changes: 1 addition & 3 deletions btcjson/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ func resultStructHelp(xT descLookupFunc, rt reflect.Type, indentLevel int) []str
result := fmt.Sprintf("%s\"%s\": %s\t(%s)\t%s", indent,
fieldName, brace, fieldType, xT(fieldDescKey))
results = append(results, result)
for _, example := range fieldExamples {
results = append(results, example)
}
results = append(results, fieldExamples...)
} else {
result := fmt.Sprintf("%s\"%s\": %s,\t(%s)\t%s", indent,
fieldName, fieldExamples[0], fieldType,
Expand Down
2 changes: 1 addition & 1 deletion database/cmd/dbtool/fetchblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (cmd *fetchBlockCmd) Execute(args []string) error {
if err != nil {
return err
}
log.Infof("Loaded block in %v", time.Now().Sub(startTime))
log.Infof("Loaded block in %v", time.Since(startTime))
log.Infof("Block Hex: %s", hex.EncodeToString(blockBytes))
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion database/cmd/dbtool/fetchblockregion.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (cmd *blockRegionCmd) Execute(args []string) error {
if err != nil {
return err
}
log.Infof("Loaded block region in %v", time.Now().Sub(startTime))
log.Infof("Loaded block region in %v", time.Since(startTime))
log.Infof("Double Hash: %s", chainhash.DoubleHashH(regionBytes))
log.Infof("Region Hex: %s", hex.EncodeToString(regionBytes))
return nil
Expand Down
10 changes: 2 additions & 8 deletions database/cmd/dbtool/insecureimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ func (bi *blockImporter) processBlock(serializedBlock []byte) (bool, error) {
var exists bool
err = bi.db.View(func(tx database.Tx) error {
exists, err = tx.HasBlock(block.Hash())
if err != nil {
return err
}
return nil
return err
})
if err != nil {
return false, err
Expand All @@ -139,10 +136,7 @@ func (bi *blockImporter) processBlock(serializedBlock []byte) (bool, error) {
var exists bool
err := bi.db.View(func(tx database.Tx) error {
exists, err = tx.HasBlock(prevHash)
if err != nil {
return err
}
return nil
return err
})
if err != nil {
return false, err
Expand Down
16 changes: 4 additions & 12 deletions database/cmd/dbtool/loadheaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ func (cmd *headersCmd) Execute(args []string) error {
return nil
})
log.Infof("Loaded %d headers in %v", numLoaded,
time.Now().Sub(startTime))
time.Since(startTime))
return nil
})
if err != nil {
return err
}

return nil
return err
}

// Bulk load headers.
Expand All @@ -90,12 +86,8 @@ func (cmd *headersCmd) Execute(args []string) error {
return err
}
log.Infof("Loaded %d headers in %v", len(hdrs),
time.Now().Sub(startTime))
time.Since(startTime))
return nil
})
if err != nil {
return err
}

return nil
return err
}
10 changes: 2 additions & 8 deletions database/ffldb/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ func BenchmarkBlockHeader(b *testing.B) {
defer db.Close()
err = db.Update(func(tx database.Tx) error {
block := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
if err := tx.StoreBlock(block); err != nil {
return err
}
return nil
return tx.StoreBlock(block)
})
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -73,10 +70,7 @@ func BenchmarkBlock(b *testing.B) {
defer db.Close()
err = db.Update(func(tx database.Tx) error {
block := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
if err := tx.StoreBlock(block); err != nil {
return err
}
return nil
return tx.StoreBlock(block)
})
if err != nil {
b.Fatal(err)
Expand Down
8 changes: 2 additions & 6 deletions database/ffldb/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func (c *dbCache) flush() error {
func (c *dbCache) needsFlush(tx *transaction) bool {
// A flush is needed when more time has elapsed than the configured
// flush interval.
if time.Now().Sub(c.lastFlush) > c.flushInterval {
if time.Since(c.lastFlush) > c.flushInterval {
return true
}

Expand All @@ -545,11 +545,7 @@ func (c *dbCache) needsFlush(tx *transaction) bool {
snap := tx.snapshot
totalSize := snap.pendingKeys.Size() + snap.pendingRemove.Size()
totalSize = uint64(float64(totalSize) * 1.5)
if totalSize > c.maxSize {
return true
}

return false
return totalSize > c.maxSize
}

// commitTx atomically adds all of the pending keys to add and remove into the
Expand Down
42 changes: 10 additions & 32 deletions database/ffldb/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,7 @@ func testNestedBucket(tc *testContext, testBucket database.Bucket) bool {
defer func() {
tc.bucketDepth--
}()
if !testBucketInterface(tc, testBucket) {
return false
}

return true
return testBucketInterface(tc, testBucket)
}

// testBucketInterface ensures the bucket interface is working properly by
Expand Down Expand Up @@ -1504,11 +1500,7 @@ func testFetchBlockIO(tc *testContext, tx database.Tx) bool {
}
wantErrCode = database.ErrBlockRegionInvalid
_, err = tx.FetchBlockRegions(badBlockRegions)
if !checkDbError(tc.t, testName, err, wantErrCode) {
return false
}

return true
return checkDbError(tc.t, testName, err, wantErrCode)
}

// testBlockIOTxInterface ensures that the block IO interface works as expected
Expand Down Expand Up @@ -1942,11 +1934,7 @@ func testClosedTxInterface(tc *testContext, tx database.Tx) bool {
return false
}
err = tx.Commit()
if !checkDbError(tc.t, "closed tx commit", err, wantErrCode) {
return false
}

return true
return checkDbError(tc.t, "closed tx commit", err, wantErrCode)
}

// testTxClosed ensures that both the metadata and block IO API functions behave
Expand Down Expand Up @@ -2016,16 +2004,13 @@ func testConcurrecy(tc *testContext) bool {
startTime := time.Now()
err := tc.db.View(func(tx database.Tx) error {
_, err := tx.FetchBlock(tc.blocks[0].Hash())
if err != nil {
return err
}
return nil
return err
})
if err != nil {
tc.t.Errorf("Unexpected error in view: %v", err)
return false
}
elapsed := time.Now().Sub(startTime)
elapsed := time.Since(startTime)
if sleepTime < elapsed {
sleepTime = elapsed
}
Expand All @@ -2041,10 +2026,7 @@ func testConcurrecy(tc *testContext) bool {
err := tc.db.View(func(tx database.Tx) error {
time.Sleep(sleepTime)
_, err := tx.FetchBlock(tc.blocks[blockNum].Hash())
if err != nil {
return err
}
return nil
return err
})
if err != nil {
tc.t.Errorf("Unexpected error in concurrent view: %v",
Expand All @@ -2065,7 +2047,7 @@ func testConcurrecy(tc *testContext) bool {
return false
}
}
elapsed = time.Now().Sub(startTime)
elapsed = time.Since(startTime)
tc.t.Logf("%d concurrent reads of same block elapsed: %v", numReaders,
elapsed)

Expand All @@ -2088,7 +2070,7 @@ func testConcurrecy(tc *testContext) bool {
return false
}
}
elapsed = time.Now().Sub(startTime)
elapsed = time.Since(startTime)
tc.t.Logf("%d concurrent reads of different blocks elapsed: %v",
numReaders, elapsed)

Expand Down Expand Up @@ -2142,11 +2124,7 @@ func testConcurrecy(tc *testContext) bool {
// Set some data the readers are expecting to not find and signal the
// readers the write is done by closing the writeComplete channel.
err = tc.db.Update(func(tx database.Tx) error {
err := tx.Metadata().Put(concurrentKey, concurrentVal)
if err != nil {
return err
}
return nil
return tx.Metadata().Put(concurrentKey, concurrentVal)
})
if err != nil {
tc.t.Errorf("Unexpected error in update: %v", err)
Expand Down Expand Up @@ -2187,7 +2165,7 @@ func testConcurrecy(tc *testContext) bool {
return false
}
}
elapsed = time.Now().Sub(startTime)
elapsed = time.Since(startTime)
tc.t.Logf("%d concurrent writers elapsed using sleep time %v: %v",
numWriters, writeSleepTime, elapsed)

Expand Down
15 changes: 6 additions & 9 deletions database/internal/treap/immutable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestImmutableEmpty(t *testing.T) {

// Ensure there are no errors with requesting keys from an empty treap.
key := serializeUint32(0)
if gotVal := testTreap.Has(key); gotVal != false {
if gotVal := testTreap.Has(key); gotVal {
t.Fatalf("Has: unexpected result - got %v, want false", gotVal)
}
if gotVal := testTreap.Get(key); gotVal != nil {
Expand Down Expand Up @@ -348,8 +348,8 @@ func TestImmutableDuplicatePut(t *testing.T) {
testTreap = testTreap.Put(key, expectedVal)

// Ensure the key still exists and is the new value.
if gotVal := testTreap.Has(key); gotVal != true {
t.Fatalf("Has: unexpected result - got %v, want false",
if gotVal := testTreap.Has(key); !gotVal {
t.Fatalf("Has: unexpected result - got %v, want true",
gotVal)
}
if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, expectedVal) {
Expand Down Expand Up @@ -379,8 +379,8 @@ func TestImmutableNilValue(t *testing.T) {
testTreap = testTreap.Put(key, nil)

// Ensure the key exists and is an empty byte slice.
if gotVal := testTreap.Has(key); gotVal != true {
t.Fatalf("Has: unexpected result - got %v, want false", gotVal)
if gotVal := testTreap.Has(key); !gotVal {
t.Fatalf("Has: unexpected result - got %v, want true", gotVal)
}
if gotVal := testTreap.Get(key); gotVal == nil {
t.Fatalf("Get: unexpected result - got nil, want empty slice")
Expand Down Expand Up @@ -408,10 +408,7 @@ func TestImmutableForEachStopIterator(t *testing.T) {
var numIterated int
testTreap.ForEach(func(k, v []byte) bool {
numIterated++
if numIterated == numItems/2 {
return false
}
return true
return numIterated != numItems/2
})
if numIterated != numItems/2 {
t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",
Expand Down
Loading

0 comments on commit 915fa66

Please sign in to comment.