Skip to content

Commit

Permalink
[AVAX] Merge Cortina 15 (v1.10.15)
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht authored Mar 8, 2024
2 parents 1872d0a + 03bc2b0 commit 2c23886
Show file tree
Hide file tree
Showing 212 changed files with 3,080 additions and 2,816 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ linters:
- nakedret
- noctx
- nolintlint
- perfsprint
- prealloc
- revive
- staticcheck
- stylecheck
- typecheck
- tagalign
- typecheck
- unconvert
- unparam
- unused
Expand Down
29 changes: 15 additions & 14 deletions api/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
hashedPassword = password.Hash{}
unAuthorizedResponseRegex = `^{"jsonrpc":"2.0","error":{"code":-32600,"message":"(.*)"},"id":1}`
errTest = errors.New("non-nil error")
hostName = "http://127.0.0.1:9650"
)

func init() {
Expand Down Expand Up @@ -161,8 +162,8 @@ func TestWrapHandlerHappyPath(t *testing.T) {
wrappedHandler := auth.WrapHandler(dummyHandler)

for _, endpoint := range endpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusOK, rr.Code)
Expand All @@ -184,8 +185,8 @@ func TestWrapHandlerRevokedToken(t *testing.T) {
wrappedHandler := auth.WrapHandler(dummyHandler)

for _, endpoint := range endpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusUnauthorized, rr.Code)
Expand All @@ -209,8 +210,8 @@ func TestWrapHandlerExpiredToken(t *testing.T) {
wrappedHandler := auth.WrapHandler(dummyHandler)

for _, endpoint := range endpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusUnauthorized, rr.Code)
Expand Down Expand Up @@ -250,8 +251,8 @@ func TestWrapHandlerUnauthorizedEndpoint(t *testing.T) {

wrappedHandler := auth.WrapHandler(dummyHandler)
for _, endpoint := range unauthorizedEndpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusUnauthorized, rr.Code)
Expand All @@ -272,7 +273,7 @@ func TestWrapHandlerAuthEndpoint(t *testing.T) {

wrappedHandler := auth.WrapHandler(dummyHandler)
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650/ext/auth", strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusOK, rr.Code)
Expand All @@ -290,8 +291,8 @@ func TestWrapHandlerAccessAll(t *testing.T) {

wrappedHandler := auth.WrapHandler(dummyHandler)
for _, endpoint := range endpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusOK, rr.Code)
Expand Down Expand Up @@ -322,7 +323,7 @@ func TestWrapHandlerMutatedRevokedToken(t *testing.T) {
wrappedHandler := auth.WrapHandler(dummyHandler)

for _, endpoint := range endpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s=", tokenStr)) // The appended = at the end looks like padding
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
Expand Down Expand Up @@ -356,8 +357,8 @@ func TestWrapHandlerInvalidSigningMethod(t *testing.T) {
wrappedHandler := auth.WrapHandler(dummyHandler)

for _, endpoint := range endpoints {
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:9650"+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", "Bearer "+tokenStr)
req := httptest.NewRequest(http.MethodPost, hostName+endpoint, strings.NewReader(""))
req.Header.Add("Authorization", headerValStart+tokenStr)
rr := httptest.NewRecorder()
wrappedHandler.ServeHTTP(rr, req)
require.Equal(http.StatusUnauthorized, rr.Code)
Expand Down
8 changes: 3 additions & 5 deletions api/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/encdb"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/json"
Expand Down Expand Up @@ -105,13 +104,12 @@ type keystore struct {
bcDB database.Database
}

func New(log logging.Logger, dbManager manager.Manager) Keystore {
currentDB := dbManager.Current()
func New(log logging.Logger, db database.Database) Keystore {
return &keystore{
log: log,
usernameToPassword: make(map[string]*password.Hash),
userDB: prefixdb.New(usersPrefix, currentDB.Database),
bcDB: prefixdb.New(bcsPrefix, currentDB.Database),
userDB: prefixdb.New(usersPrefix, db),
bcDB: prefixdb.New(bcsPrefix, db),
}
}

Expand Down
17 changes: 0 additions & 17 deletions api/keystore/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/version"
)

type service struct {
Expand Down Expand Up @@ -115,17 +112,3 @@ func (s *service) ExportUser(_ *http.Request, args *ExportUserArgs, reply *Expor
reply.Encoding = args.Encoding
return nil
}

// CreateTestKeystore returns a new keystore that can be utilized for testing
func CreateTestKeystore() (Keystore, error) {
dbManager, err := manager.NewManagerFromDBs([]*manager.VersionedDatabase{
{
Database: memdb.New(),
Version: version.Semantic1_0_0,
},
})
if err != nil {
return nil, err
}
return New(logging.NoLog{}, dbManager), nil
}
40 changes: 16 additions & 24 deletions api/keystore/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
package keystore

import (
"fmt"
"encoding/hex"
"math/rand"
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/password"
)

Expand All @@ -23,8 +25,7 @@ var strongPassword = "N_+=_jJ;^(<;{4,:*m6CET}'&N;83FYK.wtNpwp-Jt" // #nosec G101
func TestServiceListNoUsers(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

reply := ListUsersReply{}
Expand All @@ -35,8 +36,7 @@ func TestServiceListNoUsers(t *testing.T) {
func TestServiceCreateUser(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

{
Expand All @@ -58,16 +58,15 @@ func TestServiceCreateUser(t *testing.T) {
func genStr(n int) string {
b := make([]byte, n)
rand.Read(b) // #nosec G404
return fmt.Sprintf("%x", b)[:n]
return hex.EncodeToString(b)[:n]
}

// TestServiceCreateUserArgsCheck generates excessively long usernames or
// passwords to assure the sanity checks on string length are not exceeded
func TestServiceCreateUserArgsCheck(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

{
Expand Down Expand Up @@ -100,8 +99,7 @@ func TestServiceCreateUserArgsCheck(t *testing.T) {
func TestServiceCreateUserWeakPassword(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

{
Expand All @@ -117,8 +115,7 @@ func TestServiceCreateUserWeakPassword(t *testing.T) {
func TestServiceCreateDuplicate(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

{
Expand All @@ -140,12 +137,11 @@ func TestServiceCreateDuplicate(t *testing.T) {
func TestServiceCreateUserNoName(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

reply := api.EmptyReply{}
err = s.CreateUser(nil, &api.UserPass{
err := s.CreateUser(nil, &api.UserPass{
Password: strongPassword,
}, &reply)
require.ErrorIs(err, errEmptyUsername)
Expand All @@ -154,8 +150,7 @@ func TestServiceCreateUserNoName(t *testing.T) {
func TestServiceUseBlockchainDB(t *testing.T) {
require := require.New(t)

ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

{
Expand Down Expand Up @@ -185,8 +180,7 @@ func TestServiceExportImport(t *testing.T) {

encodings := []formatting.Encoding{formatting.Hex}
for _, encoding := range encodings {
ks, err := CreateTestKeystore()
require.NoError(err)
ks := New(logging.NoLog{}, memdb.New())
s := service{ks: ks.(*keystore)}

{
Expand All @@ -212,8 +206,7 @@ func TestServiceExportImport(t *testing.T) {
exportReply := ExportUserReply{}
require.NoError(s.ExportUser(nil, &exportArgs, &exportReply))

newKS, err := CreateTestKeystore()
require.NoError(err)
newKS := New(logging.NoLog{}, memdb.New())
newS := service{ks: newKS.(*keystore)}

{
Expand Down Expand Up @@ -324,16 +317,15 @@ func TestServiceDeleteUser(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
require := require.New(t)

ksIntf, err := CreateTestKeystore()
require.NoError(err)
ksIntf := New(logging.NoLog{}, memdb.New())
ks := ksIntf.(*keystore)
s := service{ks: ks}

if tt.setup != nil {
require.NoError(tt.setup(ks))
}
got := &api.EmptyReply{}
err = s.DeleteUser(nil, tt.request, got)
err := s.DeleteUser(nil, tt.request, got)
require.ErrorIs(err, tt.expectedErr)
if tt.expectedErr != nil {
return
Expand Down
7 changes: 3 additions & 4 deletions api/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/utils"
)

type metrics struct {
Expand Down Expand Up @@ -46,13 +46,12 @@ func newMetrics(namespace string, registerer prometheus.Registerer) (*metrics, e
),
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
registerer.Register(m.numProcessing),
registerer.Register(m.numCalls),
registerer.Register(m.totalDuration),
)
return m, errs.Err
return m, err
}

func (m *metrics) wrapHandler(chainName string, handler http.Handler) http.Handler {
Expand Down
9 changes: 4 additions & 5 deletions chains/linearizable_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"context"

"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"

dbManager "github.com/ava-labs/avalanchego/database/manager"
)

var (
Expand All @@ -32,7 +31,7 @@ type initializeOnLinearizeVM struct {

registerer metrics.OptionalGatherer
ctx *snow.Context
dbManager dbManager.Manager
db database.Database
genesisBytes []byte
upgradeBytes []byte
configBytes []byte
Expand All @@ -47,7 +46,7 @@ func (vm *initializeOnLinearizeVM) Linearize(ctx context.Context, stopVertexID i
return vm.vmToInitialize.Initialize(
ctx,
vm.ctx,
vm.dbManager,
vm.db,
vm.genesisBytes,
vm.upgradeBytes,
vm.configBytes,
Expand All @@ -74,7 +73,7 @@ func NewLinearizeOnInitializeVM(vm vertex.LinearizableVMWithEngine) *linearizeOn
func (vm *linearizeOnInitializeVM) Initialize(
ctx context.Context,
_ *snow.Context,
_ dbManager.Manager,
_ database.Database,
_ []byte,
_ []byte,
_ []byte,
Expand Down
Loading

0 comments on commit 2c23886

Please sign in to comment.