Skip to content

Commit

Permalink
Tighten linter errcheck; Fix outstanding linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
placer14 committed Aug 22, 2019
1 parent ad9abd6 commit 24dc0f6
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ linters:
- goconst
- govet
- megacheck
- errcheck
disable:
- goimports
- errcheck
- golint
- prealloc
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_install:
install:
- echo "No external dependencies required. Skipping travis default library dependency setup to use vendors..."
script:
- $GOPATH/bin/golangci-lint run --deadline 10m
- $GOPATH/bin/golangci-lint run --deadline 10m --new-from-rev=master
- cd $TRAVIS_BUILD_DIR && go test -i && ./test_compile.sh
- goveralls -coverprofile=coverage.out -service travis-ci -repotoken $COVERALLS_TOKEN
after_success:
Expand Down
20 changes: 4 additions & 16 deletions core/disputes.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,7 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
outPercentage := new(big.Float).Quo(new(big.Float).SetInt(&output.Value), new(big.Float).SetInt(totalOut))
outputShareOfFee := new(big.Float).Mul(outPercentage, new(big.Float).SetInt(&txFee))
valF := new(big.Float).Sub(new(big.Float).SetInt(&output.Value), outputShareOfFee)
val, accuracy := valF.Int(nil)
if accuracy != 0 {
//return errors.New("problem rounding the fee")
}
val, _ := valF.Int(nil)
if !wal.IsDust(*val) {
o := wallet.TransactionOutput{
Value: *val,
Expand Down Expand Up @@ -699,10 +696,7 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
if _, ok := outMap["buyer"]; ok {
f := new(big.Float).Quo(new(big.Float).SetInt(buyerValue), new(big.Float).SetInt(totalOut))
outputShareOfFeeF := new(big.Float).Mul(f, new(big.Float).SetInt(&txFee))
outputShareOfFeeInt, accuracy := outputShareOfFeeF.Int(nil)
if accuracy != 0 {
//return errors.New("problem rounding the fee")
}
outputShareOfFeeInt, _ := outputShareOfFeeF.Int(nil)
amt := new(big.Int).Sub(buyerValue, outputShareOfFeeInt)
if amt.Cmp(big.NewInt(0)) < 0 {
amt = big.NewInt(0)
Expand All @@ -718,10 +712,7 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
if _, ok := outMap["vendor"]; ok {
f := new(big.Float).Quo(new(big.Float).SetInt(vendorValue), new(big.Float).SetInt(totalOut))
outputShareOfFeeF := new(big.Float).Mul(f, new(big.Float).SetInt(&txFee))
outputShareOfFeeInt, accuracy := outputShareOfFeeF.Int(nil)
if accuracy != 0 {
//return errors.New("problem rounding the fee")
}
outputShareOfFeeInt, _ := outputShareOfFeeF.Int(nil)
amt := new(big.Int).Sub(vendorValue, outputShareOfFeeInt)
if amt.Cmp(big.NewInt(0)) < 0 {
amt = big.NewInt(0)
Expand All @@ -737,10 +728,7 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
if _, ok := outMap["moderator"]; ok {
f := new(big.Float).Quo(new(big.Float).SetInt(&modValue), new(big.Float).SetInt(totalOut))
outputShareOfFeeF := new(big.Float).Mul(f, new(big.Float).SetInt(&txFee))
outputShareOfFeeInt, accuracy := outputShareOfFeeF.Int(nil)
if accuracy != 0 {
//return errors.New("problem rounding the fee")
}
outputShareOfFeeInt, _ := outputShareOfFeeF.Int(nil)
amt := new(big.Int).Sub(&modValue, outputShareOfFeeInt)
if amt.Cmp(big.NewInt(0)) < 0 {
amt = big.NewInt(0)
Expand Down
10 changes: 2 additions & 8 deletions core/moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ func (n *OpenBazaarNode) GetModeratorFee(transactionTotal big.Int, paymentCoin,
f := big.NewFloat(float64(profile.ModeratorInfo.Fee.Percentage))
f.Mul(f, big.NewFloat(0.01))
t.Mul(t, f)
total, accuracy := t.Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("inaccurate fee amount rounding")
}
total, _ := t.Int(nil)
return *total, nil
case pb.Moderator_Fee_FIXED:
fixedFee, ok := new(big.Int).SetString(profile.ModeratorInfo.Fee.FixedFeeValue.Amount, 10)
Expand Down Expand Up @@ -196,10 +193,7 @@ func (n *OpenBazaarNode) GetModeratorFee(transactionTotal big.Int, paymentCoin,
f := big.NewFloat(float64(profile.ModeratorInfo.Fee.Percentage))
f.Mul(f, big.NewFloat(0.01))
t.Mul(t, f)
total, accuracy := t.Int(&transactionTotal)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("inaccurate transaction total rounding")
}
total, _ := t.Int(&transactionTotal)
if fixed.Add(fixed, total).Cmp(&transactionTotal) > 0 {
return *big.NewInt(0), errors.New("Fixed moderator fee exceeds transaction amount")
}
Expand Down
45 changes: 9 additions & 36 deletions core/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,10 +1026,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (bi
if l.Metadata.Format == pb.Listing_Metadata_MARKET_PRICE {
satoshis, err = n.getMarketPriceInSatoshis(contract.BuyerOrder.Payment.AmountValue.Currency.Code, l.Metadata.PricingCurrencyDefn.Code, *big.NewInt(int64(itemQuantity)))
t0 := new(big.Float).Mul(big.NewFloat(float64(l.Metadata.PriceModifier)), new(big.Float).SetInt(&satoshis))
t1, accuracy := new(big.Float).Mul(t0, big.NewFloat(0.01)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in price")
}
t1, _ := new(big.Float).Mul(t0, big.NewFloat(0.01)).Int(nil)
satoshis = *new(big.Int).Add(&satoshis, t1)
itemQuantity = 1
} else {
Expand Down Expand Up @@ -1100,10 +1097,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (bi
itemTotal = *new(big.Int).Sub(&itemTotal, &satoshis)
} else if discountF := vendorCoupon.GetPercentDiscount(); discountF > 0 {
d := new(big.Float).Mul(big.NewFloat(float64(discountF)), big.NewFloat(0.01))
totalDiscount, accuracy := new(big.Float).Mul(d, new(big.Float).SetInt(&itemTotal)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in discount total")
}
totalDiscount, _ := new(big.Float).Mul(d, new(big.Float).SetInt(&itemTotal)).Int(nil)
itemTotal = *new(big.Int).Sub(&itemTotal, totalDiscount)
}
}
Expand All @@ -1114,10 +1108,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (bi
for _, taxRegion := range tax.TaxRegions {
if contract.BuyerOrder.Shipping.Country == taxRegion {
t := new(big.Float).Mul(big.NewFloat(float64(tax.Percentage)), big.NewFloat(0.01))
totalTax, accuracy := new(big.Float).Mul(t, new(big.Float).SetInt(&itemTotal)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in tax total")
}
totalTax, _ := new(big.Float).Mul(t, new(big.Float).SetInt(&itemTotal)).Int(nil)
itemTotal = *new(big.Int).Add(&itemTotal, totalTax)
break
}
Expand Down Expand Up @@ -1245,21 +1236,15 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
if len(is) == 1 {
s := int64(((1 + is[0].shippingTaxPercentage) * 100) + .5)
shippingTotalPrimary := new(big.Int).Mul(&is[0].primary, big.NewInt(s))
stp, accuracy := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalPrimary)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in shipping total")
}
stp, _ := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalPrimary)).Int(nil)
shippingTotal = *stp
if is[0].quantity > 1 {
if is[0].version == 1 {
t1 := new(big.Int).Mul(stp, big.NewInt(int64(is[0].quantity-1)))
shippingTotal = *new(big.Int).Add(stp, t1)
} else if is[0].version >= 2 {
shippingTotalSecondary := new(big.Int).Mul(&is[0].secondary, big.NewInt(s))
sts, accuracy := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalSecondary)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in shipping total")
}
sts, _ := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalSecondary)).Int(nil)

t1 := new(big.Int).Mul(sts, big.NewInt(int64(is[0].quantity-1)))
shippingTotal = *new(big.Int).Add(stp, t1)
Expand All @@ -1280,26 +1265,17 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
}
s0 := int64(((1 + s.shippingTaxPercentage) * 100) + .5)
shippingTotalSec := new(big.Int).Mul(&s.secondary, big.NewInt(s0))
sts0, accuracy := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalSec)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in shipping total")
}
sts0, _ := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalSec)).Int(nil)
shippingTotal0 := new(big.Int).Mul(sts0, big.NewInt(int64(s.quantity)))
shippingTotal = *new(big.Int).Add(&shippingTotal, shippingTotal0)
}
sp := int64(((1 + is[i].shippingTaxPercentage) * 100) + .5)
shippingTotalPrimary0 := new(big.Int).Mul(&is[i].primary, big.NewInt(sp))
stp0, accuracy := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalPrimary0)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in shipping total")
}
stp0, _ := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalPrimary0)).Int(nil)
shippingTotal = *new(big.Int).Sub(&shippingTotal, stp0)

shippingTotalSecondary0 := new(big.Int).Mul(&is[i].secondary, big.NewInt(sp))
sts0, accuracy := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalSecondary0)).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in shipping total")
}
sts0, _ := new(big.Float).Mul(big.NewFloat(0.01), new(big.Float).SetInt(shippingTotalSecondary0)).Int(nil)
shippingTotal = *new(big.Int).Add(&shippingTotal, sts0)

return shippingTotal, nil
Expand Down Expand Up @@ -1393,10 +1369,7 @@ func (n *OpenBazaarNode) getMarketPriceInSatoshis(pricingCurrency, currencyCode
if err != nil {
return *big.NewInt(0), err
}
r, accuracy := big.NewFloat(rate).Int(nil)
if accuracy != 0 {
//return *big.NewInt(0), errors.New("rounding error in rate")
}
r, _ := big.NewFloat(rate).Int(nil)
if r.Int64() == 0 {
return *big.NewInt(0), errors.New("invalid rate of zero value")
}
Expand Down
4 changes: 3 additions & 1 deletion repo/db/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ func TestImportKey(t *testing.T) {

func TestPutDuplicateKey(t *testing.T) {
b := make([]byte, 32)
kdb.Put(b, wallet.KeyPath{Purpose: wallet.EXTERNAL, Index: 0})
if err := kdb.Put(b, wallet.KeyPath{Purpose: wallet.EXTERNAL, Index: 0}); err != nil {
t.Fatal(err)
}
err := kdb.Put(b, wallet.KeyPath{Purpose: wallet.EXTERNAL, Index: 0})
if err == nil {
t.Error("Expected duplicate key error")
Expand Down
9 changes: 5 additions & 4 deletions repo/migrations/Migration027.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import (
crypto "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"

"github.com/OpenBazaar/jsonpb"
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/davecgh/go-spew/spew"
"github.com/golang/protobuf/proto"
timestamp "github.com/golang/protobuf/ptypes/timestamp"
ipfscore "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/fsrepo"

"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/pb"
)

type Migration027 struct{}
Expand Down Expand Up @@ -317,7 +316,9 @@ func (Migration027) Up(repoPath, databasePassword string, testnetEnabled bool) e
b, _ := json.Marshal(listingAbstract)
//indexBytes0 = append(indexBytes0, b...)
var n Migration027_ListingDatav5
json.Unmarshal(b, &n)
if err := json.Unmarshal(b, &n); err != nil {
return fmt.Errorf("failed unmarshaling (%s): %s", listSlug, err.Error())
}
indexv5 = append(indexv5, n)
continue
}
Expand Down
17 changes: 13 additions & 4 deletions repo/migrations/Migration028.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrations

import (
"database/sql"
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -45,7 +46,9 @@ func (AM02) Up(repoPath string, dbPassword string, testnet bool) error {
}
if dbPassword != "" {
p := "pragma key='" + dbPassword + "';"
db.Exec(p)
if _, err := db.Exec(p); err != nil {
return err
}
}

upSequence := strings.Join([]string{
Expand All @@ -64,7 +67,9 @@ func (AM02) Up(repoPath string, dbPassword string, testnet bool) error {
return err
}
if _, err = tx.Exec(upSequence); err != nil {
tx.Rollback()
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("failed rollback: (%s) due to (%s)", rErr.Error(), err.Error())
}
return err
}
if err = tx.Commit(); err != nil {
Expand Down Expand Up @@ -95,7 +100,9 @@ func (AM02) Down(repoPath string, dbPassword string, testnet bool) error {
}
if dbPassword != "" {
p := "pragma key='" + dbPassword + "';"
db.Exec(p)
if _, err := db.Exec(p); err != nil {
return err
}
}
downSequence := strings.Join([]string{
AM02_temp_sales,
Expand All @@ -113,7 +120,9 @@ func (AM02) Down(repoPath string, dbPassword string, testnet bool) error {
return err
}
if _, err = tx.Exec(downSequence); err != nil {
tx.Rollback()
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("failed rollback: (%s) due to (%s)", rErr.Error(), err.Error())
}
return err
}
if err = tx.Commit(); err != nil {
Expand Down
67 changes: 35 additions & 32 deletions repo/migrations/Migration028_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,47 @@ var stmt = `PRAGMA key = 'letmein';

func TestMigration028(t *testing.T) {
var dbPath string
os.Mkdir("./datastore", os.ModePerm)
if err := os.Mkdir("./datastore", os.ModePerm); err != nil {
t.Fatal(err)
}
dbPath = path.Join("./", "datastore", "mainnet.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Error(err)
t.Fatal(err)
}
db.Exec(stmt)
_, err = db.Exec("INSERT INTO sales (orderID, total) values (?,?)", "asdf", 3)
if err != nil {
t.Error(err)
return
if _, err := db.Exec(stmt); err != nil {
t.Fatal(err)
}
_, err = db.Exec("INSERT INTO purchases (orderID, total) values (?,?)", "asdf", 3)
if err != nil {
t.Error(err)
return
if _, err := db.Exec("INSERT INTO sales (orderID, total) values (?,?)", "asdf", 3); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO purchases (orderID, total) values (?,?)", "asdf", 3); err != nil {
t.Fatal(err)
}
var m migrations.Migration028
err = m.Up("./", "letmein", false)
if err != nil {
t.Error(err)
if err := m.Up("./", "letmein", false); err != nil {
t.Fatal(err)
}

var orderID string
var total string
var total1 int
var (
orderID string
total string
total1 int
)

r := db.QueryRow("select orderID, total from sales where orderID=?", "asdf")

if err := r.Scan(&orderID, &total); err != nil || total != "3" {
if err := r.Scan(&orderID, &total); err != nil {
t.Error(err)
return
}

if total != "3" {
t.Errorf("expected total to be 3, but was %s", total)
}
r = db.QueryRow("select orderID, total from purchases where orderID=?", "asdf")

if err := r.Scan(&orderID, &total); err != nil || total != "3" {
if err := r.Scan(&orderID, &total); err != nil {
t.Error(err)
return
}
if total != "3" {
t.Errorf("expected total to be 3, but was %s", total)
}

repoVer, err := ioutil.ReadFile("./repover")
Expand All @@ -83,22 +85,23 @@ func TestMigration028(t *testing.T) {

err = m.Down("./", "letmein", false)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

r = db.QueryRow("select orderID, total from sales where orderID=?", "asdf")

if err := r.Scan(&orderID, &total1); err != nil || total1 != 3 {
if err := r.Scan(&orderID, &total1); err != nil {
t.Error(err)
return
}
if total1 != 3 {
t.Errorf("expected total to be 3, but was %d", total1)
}

r = db.QueryRow("select orderID, total from purchases where orderID=?", "asdf")

if err := r.Scan(&orderID, &total1); err != nil || total1 != 3 {
if err := r.Scan(&orderID, &total1); err != nil {
t.Error(err)
return
}
if total1 != 3 {
t.Errorf("expected total to be 3, but was %d", total1)
}

repoVer, err = ioutil.ReadFile("./repover")
Expand Down
Loading

0 comments on commit 24dc0f6

Please sign in to comment.