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

chore: Db linting #12141

Merged
merged 52 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
6bc71c1
mainly sdk.int to cosmossdk.io/math
faddat Jun 3, 2022
ce2cce2
Merge branch 'main' into linting-math-lib
faddat Jun 3, 2022
2b3991e
staking keys
faddat Jun 3, 2022
afee296
Merge branch 'linting-math-lib' of https://github.com/notional-labs/c…
faddat Jun 3, 2022
9f5cd2b
fumpt
faddat Jun 3, 2022
1b2320c
var-naming linter errors and a fumpt
faddat Jun 3, 2022
2499162
Update CHANGELOG.md
faddat Jun 3, 2022
31b7a55
Merge branch 'main' into var-naming
faddat Jun 3, 2022
1c32467
Merge branch 'main' into linting-math-lib
faddat Jun 3, 2022
9ad16db
Update .golangci.yml
faddat Jun 3, 2022
838ef64
Update CHANGELOG.md
faddat Jun 3, 2022
57c94ae
Merge branch 'main' into linting-math-lib
faddat Jun 3, 2022
f5978a5
Update test_helpers.go
faddat Jun 3, 2022
78a9ae8
Merge branch 'main' into var-naming
faddat Jun 3, 2022
848b2f6
Update test_helpers.go
faddat Jun 3, 2022
11f096f
fumpt and lint
faddat Jun 3, 2022
1bfca40
Merge branch 'var-naming' of https://github.com/notional-labs/cosmos-sdk
faddat Jun 3, 2022
3f72847
Merge branch 'linting-math-lib' of https://github.com/notional-labs/c…
faddat Jun 3, 2022
984e880
this lints the db module, and makes it easier to use. It adds breaki…
faddat Jun 3, 2022
ba145fb
DBConnection -> Connection
faddat Jun 3, 2022
31743e3
previous commit contained a merge error
faddat Jun 3, 2022
1cbd6d2
Merge branch 'main' into db-linting
faddat Jun 3, 2022
98767f4
Merge branch 'main' into db-linting
faddat Jun 3, 2022
631d6be
Update test_helpers.go
faddat Jun 3, 2022
0d566c5
Update test_helpers.go
faddat Jun 3, 2022
e039217
db renamings
faddat Jun 3, 2022
e2122ce
Merge branch 'db-linting' of https://github.com/notional-labs/cosmos-…
faddat Jun 3, 2022
7a1846f
Merge branch 'main' into db-linting
faddat Jun 3, 2022
4ccbdb9
Merge remote-tracking branch 'origin/main' into db-linting
faddat Jun 3, 2022
343e5bf
merge master
faddat Jun 3, 2022
7698645
Merge branch 'db-linting' of https://github.com/notional-labs/cosmos-…
faddat Jun 3, 2022
9560aeb
changelog
faddat Jun 3, 2022
5a7b803
DBWriter -> Writer
faddat Jun 3, 2022
13bb3ef
Merge branch 'main' into db-linting
faddat Jun 3, 2022
520bf86
consistent multistore reciever
faddat Jun 3, 2022
d034aad
Merge branch 'db-linting' of https://github.com/notional-labs/cosmos-…
faddat Jun 3, 2022
8f175a3
standard recievers for multistore v2alpha1
faddat Jun 3, 2022
fcc514e
general cleanup of linting issues
faddat Jun 3, 2022
38d09bc
Merge branch 'main' into db-linting
faddat Jun 3, 2022
3c37a4f
more linter fixes
faddat Jun 3, 2022
cfafb8e
remove prealloc linter
faddat Jun 3, 2022
ee4bb7b
Merge branch 'main' into db-linting
faddat Jun 7, 2022
e611227
Merge branch 'main' into db-linting
faddat Jun 8, 2022
fe74461
Merge branch 'main' into db-linting
faddat Jun 8, 2022
3ddfcc8
nolint the secp256k1 import
faddat Jun 8, 2022
023640d
Merge branch 'db-linting' of https://github.com/notional-labs/cosmos-…
faddat Jun 8, 2022
84fff6d
nolint the secp256k1 package
faddat Jun 8, 2022
b6cbe02
completenolint resulting in a diff that has only nolints
faddat Jun 8, 2022
bbed362
Merge branch 'db-linting' of https://github.com/notional-labs/cosmos-…
faddat Jun 8, 2022
134a09d
Merge branch 'main' into db-linting
faddat Jun 8, 2022
a55034d
Merge branch 'main' into db-linting
faddat Jun 8, 2022
4271893
Merge branch 'main' into db-linting
faddat Jun 8, 2022
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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ linters:
- misspell
- nakedret
- nolintlint
- prealloc
- revive
- staticcheck
- structcheck
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
### API Breaking Changes

* (x/staking) [#12102](https://github.com/cosmos/cosmos-sdk/pull/12102) Staking keeper now is passed by reference instead of copy. Keeper's SetHooks no longer returns keeper. It updates the keeper in place instead.
* (linting) [#12141](https://github.com/cosmos/cosmos-sdk/pull/12141) Fix usability related linting for database. This means removing the infix Prefix from `prefix.NewPrefixWriter` and such so that it is `prefix.NewWriter` and making `db.DBConnection` and such into `db.Connection`


### Bug Fixes

* (linting) [#12135](https://github.com/cosmos/cosmos-sdk/pull/12135/) Fix variable naming issues per enabled linters. Run gofumpt to ensure easy reviews of ongoing linting work.
* (linting) [#12135](https://github.com/cosmos/cosmos-sdk/pull/12135) Fix variable naming issues per enabled linters. Run gofumpt to ensure easy reviews of ongoing linting work.
* (linting) [#12132](https://github.com/cosmos/cosmos-sdk/pull/12132) Change sdk.Int to math.Int, run `gofumpt -w -l .`, and `golangci-lint run ./... --fix`
* (cli) [#12127](https://github.com/cosmos/cosmos-sdk/pull/12127) Fix the CLI not always taking into account `--fee-payer` and `--fee-granter` flags.
* (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations.
Expand Down
27 changes: 15 additions & 12 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import (
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"

legacybech32 "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32"
legacybech32 "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" //nolint:staticcheck // we do old keys, they're keys after all.
)

var flagPubkeyType = "type"
var (
flagPubkeyType = "type"
ed = "ed25519"
)

// Cmd creates a main CLI command
func Cmd() *cobra.Command {
Expand Down Expand Up @@ -69,7 +72,7 @@ $ %s debug pubkey '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfP
}

func bytesToPubkey(bz []byte, keytype string) (cryptotypes.PubKey, bool) {
if keytype == "ed25519" {
if keytype == ed {
if len(bz) == ed25519.PubKeySize {
return &ed25519.PubKey{Key: bz}, true
}
Expand Down Expand Up @@ -102,17 +105,17 @@ func getPubKeyFromRawString(pkstr string, keytype string) (cryptotypes.PubKey, e
}
}

pk, err := legacybech32.UnmarshalPubKey(legacybech32.AccPK, pkstr)
pk, err := legacybech32.UnmarshalPubKey(legacybech32.AccPK, pkstr) //nolint:staticcheck // we do old keys, they're keys after all.
if err == nil {
return pk, nil
}

pk, err = legacybech32.UnmarshalPubKey(legacybech32.ValPK, pkstr)
pk, err = legacybech32.UnmarshalPubKey(legacybech32.ValPK, pkstr) //nolint:staticcheck // we do old keys, they're keys after all.
if err == nil {
return pk, nil
}

pk, err = legacybech32.UnmarshalPubKey(legacybech32.ConsPK, pkstr)
pk, err = legacybech32.UnmarshalPubKey(legacybech32.ConsPK, pkstr) //nolint:staticcheck // we do old keys, they're keys after all.
if err == nil {
return pk, nil
}
Expand All @@ -138,7 +141,7 @@ $ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
return err
}
pubkeyType = strings.ToLower(pubkeyType)
if pubkeyType != "secp256k1" && pubkeyType != "ed25519" {
if pubkeyType != "secp256k1" && pubkeyType != ed {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type, expected oneof ed25519 or secp256k1")
}

Expand All @@ -149,8 +152,8 @@ $ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg

var consensusPub string
edPK, ok := pk.(*ed25519.PubKey)
if ok && pubkeyType == "ed25519" {
consensusPub, err = legacybech32.MarshalPubKey(legacybech32.ConsPK, edPK)
if ok && pubkeyType == ed {
consensusPub, err = legacybech32.MarshalPubKey(legacybech32.ConsPK, edPK) //nolint:staticcheck // we do old keys, they're keys after all.
if err != nil {
return err
}
Expand All @@ -163,11 +166,11 @@ $ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
if err != nil {
return err
}
accPub, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pk)
accPub, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pk) //nolint:staticcheck // we do old keys, they're keys after all.
if err != nil {
return err
}
valPub, err := legacybech32.MarshalPubKey(legacybech32.ValPK, pk)
valPub, err := legacybech32.MarshalPubKey(legacybech32.ValPK, pk) //nolint:staticcheck // we do old keys, they're keys after all.
if err != nil {
return err
}
Expand All @@ -182,7 +185,7 @@ $ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
return nil
},
}
cmd.Flags().StringP(flagPubkeyType, "t", "ed25519", "Pubkey type to decode (oneof secp256k1, ed25519)")
cmd.Flags().StringP(flagPubkeyType, "t", ed, "Pubkey type to decode (oneof secp256k1, ed25519)")
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

legacyproto "github.com/golang/protobuf/proto"
legacyproto "github.com/golang/protobuf/proto" //nolint:staticcheck // we're aware this is deprecated and using it anyhow.
"google.golang.org/grpc/encoding"
"google.golang.org/protobuf/proto"

Expand Down
44 changes: 22 additions & 22 deletions crypto/keys/secp256k1/internal/secp256k1/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,34 +143,34 @@ func (bitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {

// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
// (x2, y2, z2) and returns their sum, also in Jacobian form.
func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
faddat marked this conversation as resolved.
Show resolved Hide resolved
func (bitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
z1z1 := new(big.Int).Mul(z1, z1)
z1z1.Mod(z1z1, BitCurve.P)
z1z1.Mod(z1z1, bitCurve.P)
z2z2 := new(big.Int).Mul(z2, z2)
z2z2.Mod(z2z2, BitCurve.P)
z2z2.Mod(z2z2, bitCurve.P)

u1 := new(big.Int).Mul(x1, z2z2)
u1.Mod(u1, BitCurve.P)
u1.Mod(u1, bitCurve.P)
u2 := new(big.Int).Mul(x2, z1z1)
u2.Mod(u2, BitCurve.P)
u2.Mod(u2, bitCurve.P)
h := new(big.Int).Sub(u2, u1)
if h.Sign() == -1 {
h.Add(h, BitCurve.P)
h.Add(h, bitCurve.P)
}
i := new(big.Int).Lsh(h, 1)
i.Mul(i, i)
j := new(big.Int).Mul(h, i)

s1 := new(big.Int).Mul(y1, z2)
s1.Mul(s1, z2z2)
s1.Mod(s1, BitCurve.P)
s1.Mod(s1, bitCurve.P)
s2 := new(big.Int).Mul(y2, z1)
s2.Mul(s2, z1z1)
s2.Mod(s2, BitCurve.P)
s2.Mod(s2, bitCurve.P)
r := new(big.Int).Sub(s2, s1)
if r.Sign() == -1 {
r.Add(r, BitCurve.P)
r.Add(r, bitCurve.P)
}
r.Lsh(r, 1)
v := new(big.Int).Mul(u1, i)
Expand All @@ -180,41 +180,41 @@ func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int
x3.Sub(x3, j)
x3.Sub(x3, v)
x3.Sub(x3, v)
x3.Mod(x3, BitCurve.P)
x3.Mod(x3, bitCurve.P)

y3 := new(big.Int).Set(r)
v.Sub(v, x3)
y3.Mul(y3, v)
s1.Mul(s1, j)
s1.Lsh(s1, 1)
y3.Sub(y3, s1)
y3.Mod(y3, BitCurve.P)
y3.Mod(y3, bitCurve.P)

z3 := new(big.Int).Add(z1, z2)
z3.Mul(z3, z3)
z3.Sub(z3, z1z1)
if z3.Sign() == -1 {
z3.Add(z3, BitCurve.P)
z3.Add(z3, bitCurve.P)
}
z3.Sub(z3, z2z2)
if z3.Sign() == -1 {
z3.Add(z3, BitCurve.P)
z3.Add(z3, bitCurve.P)
}
z3.Mul(z3, h)
z3.Mod(z3, BitCurve.P)
z3.Mod(z3, bitCurve.P)

return x3, y3, z3
}

// Double returns 2*(x,y)
func (BitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
func (bitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
z1 := new(big.Int).SetInt64(1)
return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z1))
return bitCurve.affineFromJacobian(bitCurve.doubleJacobian(x1, y1, z1))
}

// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
// returns its double, also in Jacobian form.
func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
func (bitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l

a := new(big.Int).Mul(x, x) // X1²
Expand All @@ -232,24 +232,24 @@ func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int,

x3 := new(big.Int).Mul(big.NewInt(2), d) // 2*D
x3.Sub(f, x3) // F-2*D
x3.Mod(x3, BitCurve.P)
x3.Mod(x3, bitCurve.P)

y3 := new(big.Int).Sub(d, x3) // D-X3
y3.Mul(e, y3) // E*(D-X3)
y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) // E*(D-X3)-8*C
y3.Mod(y3, BitCurve.P)
y3.Mod(y3, bitCurve.P)

z3 := new(big.Int).Mul(y, z) // Y1*Z1
z3.Mul(big.NewInt(2), z3) // 3*Y1*Z1
z3.Mod(z3, BitCurve.P)
z3.Mod(z3, bitCurve.P)

return x3, y3, z3
}

// ScalarBaseMult returns k*G, where G is the base point of the group and k is
// an integer in big-endian form.
func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
return BitCurve.ScalarMult(BitCurve.Gx, BitCurve.Gy, k)
func (bitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
return bitCurve.ScalarMult(bitCurve.Gx, bitCurve.Gy, k)
}

// Marshal converts a point into the form specified in section 4.3.6 of ANSI
Expand Down
6 changes: 3 additions & 3 deletions crypto/keys/secp256k1/internal/secp256k1/scalar_mult_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned
*/
import "C"

func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
func (bitCurve *BitCurve) ScalarMult(bx, by *big.Int, scalar []byte) (*big.Int, *big.Int) {
// Ensure scalar is exactly 32 bytes. We pad always, even if
// scalar is 32 bytes long, to avoid a timing side channel.
if len(scalar) > 32 {
Expand All @@ -34,8 +34,8 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int,

// Do the multiplication in C, updating point.
point := make([]byte, 64)
readBits(Bx, point[:32])
readBits(By, point[32:])
readBits(bx, point[:32])
readBits(by, point[32:])

pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
Expand Down
2 changes: 1 addition & 1 deletion db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Databases supporting mappings of arbitrary byte sequences.

The database interface types consist of objects to encapsulate the singular connection to the DB, transactions being made to it, historical version state, and iteration.

### `DBConnection`
### `Connection`

This interface represents a connection to a versioned key-value database. All versioning operations are performed using methods on this type.

Expand Down
4 changes: 2 additions & 2 deletions db/adapter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package db

type readerRWAdapter struct{ DBReader }
type readerRWAdapter struct{ Reader }

// ReaderAsReadWriter returns a ReadWriter that forwards to a reader and errors if writes are
// attempted. Can be used to pass a Reader when a ReadWriter is expected
// but no writes will actually occur.
func ReaderAsReadWriter(r DBReader) DBReadWriter {
func ReaderAsReadWriter(r Reader) ReadWriter {
return readerRWAdapter{r}
}

Expand Down
22 changes: 11 additions & 11 deletions db/badgerdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
var versionsFilename = "versions.csv"

var (
_ db.DBConnection = (*BadgerDB)(nil)
_ db.DBReader = (*badgerTxn)(nil)
_ db.DBWriter = (*badgerWriter)(nil)
_ db.DBReadWriter = (*badgerWriter)(nil)
_ db.Connection = (*BadgerDB)(nil)
_ db.Reader = (*badgerTxn)(nil)
_ db.Writer = (*badgerWriter)(nil)
_ db.ReadWriter = (*badgerWriter)(nil)
)

// BadgerDB is a connection to a BadgerDB key-value database.
Expand Down Expand Up @@ -164,14 +164,14 @@ func writeVersionsFile(vm *versionManager, path string) error {
return w.WriteAll(rows)
}

func (b *BadgerDB) Reader() db.DBReader {
func (b *BadgerDB) Reader() db.Reader {
b.mtx.RLock()
ts := b.vmgr.lastTs
b.mtx.RUnlock()
return &badgerTxn{txn: b.db.NewTransactionAt(ts, false), db: b}
}

func (b *BadgerDB) ReaderAt(version uint64) (db.DBReader, error) {
func (b *BadgerDB) ReaderAt(version uint64) (db.Reader, error) {
b.mtx.RLock()
defer b.mtx.RUnlock()
ts, has := b.vmgr.versionTs(version)
Expand All @@ -181,15 +181,15 @@ func (b *BadgerDB) ReaderAt(version uint64) (db.DBReader, error) {
return &badgerTxn{txn: b.db.NewTransactionAt(ts, false), db: b}, nil
}

func (b *BadgerDB) ReadWriter() db.DBReadWriter {
func (b *BadgerDB) ReadWriter() db.ReadWriter {
atomic.AddInt32(&b.openWriters, 1)
b.mtx.RLock()
ts := b.vmgr.lastTs
b.mtx.RUnlock()
return &badgerWriter{badgerTxn{txn: b.db.NewTransactionAt(ts, true), db: b}, false}
}

func (b *BadgerDB) Writer() db.DBWriter {
func (b *BadgerDB) Writer() db.Writer {
// Badger has a WriteBatch, but it doesn't support conflict detection
return b.ReadWriter()
}
Expand All @@ -201,7 +201,7 @@ func (b *BadgerDB) Close() error {
return b.db.Close()
}

// Versions implements DBConnection.
// Versions implements Connection.
// Returns a VersionSet that is valid until the next call to SaveVersion or DeleteVersion.
func (b *BadgerDB) Versions() (db.VersionSet, error) {
b.mtx.RLock()
Expand All @@ -219,12 +219,12 @@ func (b *BadgerDB) save(target uint64) (uint64, error) {
return b.vmgr.Save(target)
}

// SaveNextVersion implements DBConnection.
// SaveNextVersion implements Connection.
func (b *BadgerDB) SaveNextVersion() (uint64, error) {
return b.save(0)
}

// SaveVersion implements DBConnection.
// SaveVersion implements Connection.
func (b *BadgerDB) SaveVersion(target uint64) error {
if target == 0 {
return db.ErrInvalidVersion
Expand Down
2 changes: 1 addition & 1 deletion db/badgerdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/db/dbtest"
)

func load(t *testing.T, dir string) db.DBConnection {
func load(t *testing.T, dir string) db.Connection {
d, err := NewDB(dir)
require.NoError(t, err)
return d
Expand Down
Loading