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

Remove database.Manager #2239

Merged
merged 17 commits into from
Oct 31, 2023
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
}
36 changes: 14 additions & 22 deletions api/keystore/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"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 Down Expand Up @@ -66,8 +66,7 @@ func genStr(n int) string {
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
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
37 changes: 17 additions & 20 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/api/server"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/meterdb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/message"
Expand Down Expand Up @@ -57,7 +59,6 @@ import (
"github.com/ava-labs/avalanchego/vms/proposervm"
"github.com/ava-labs/avalanchego/vms/tracedvm"

dbManager "github.com/ava-labs/avalanchego/database/manager"
timetracker "github.com/ava-labs/avalanchego/snow/networking/tracker"

aveng "github.com/ava-labs/avalanchego/snow/engine/avalanche"
Expand Down Expand Up @@ -180,7 +181,7 @@ type ManagerConfig struct {
BlockAcceptorGroup snow.AcceptorGroup
TxAcceptorGroup snow.AcceptorGroup
VertexAcceptorGroup snow.AcceptorGroup
DBManager dbManager.Manager
DB database.Database
MsgCreator message.OutboundMsgBuilder // message creator, shared with network
Router router.Router // Routes incoming messages to the appropriate chain
Net network.Network // Sends consensus messages to other validators
Expand Down Expand Up @@ -596,18 +597,16 @@ func (m *manager) createAvalancheChain(
State: snow.Initializing,
})

meterDBManager, err := m.DBManager.NewMeterDBManager("db", ctx.Registerer)
meterDB, err := meterdb.New("db", ctx.Registerer, m.DB)
if err != nil {
return nil, err
}
prefixDBManager := meterDBManager.NewPrefixDBManager(ctx.ChainID[:])
vmDBManager := prefixDBManager.NewPrefixDBManager(vmDBPrefix)

db := prefixDBManager.Current()
vertexDB := prefixdb.New(vertexDBPrefix, db.Database)
vertexBootstrappingDB := prefixdb.New(vertexBootstrappingDBPrefix, db.Database)
txBootstrappingDB := prefixdb.New(txBootstrappingDBPrefix, db.Database)
blockBootstrappingDB := prefixdb.New(blockBootstrappingDBPrefix, db.Database)
prefixDB := prefixdb.New(ctx.ChainID[:], meterDB)
db := prefixdb.New(vmDBPrefix, prefixDB)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
vertexDB := prefixdb.New(vertexDBPrefix, db)
vertexBootstrappingDB := prefixdb.New(vertexBootstrappingDBPrefix, db)
txBootstrappingDB := prefixdb.New(txBootstrappingDBPrefix, db)
blockBootstrappingDB := prefixdb.New(blockBootstrappingDBPrefix, db)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved

vtxBlocker, err := queue.NewWithMissing(vertexBootstrappingDB, "vtx", ctx.AvalancheRegisterer)
if err != nil {
Expand Down Expand Up @@ -730,7 +729,7 @@ func (m *manager) createAvalancheChain(
err = dagVM.Initialize(
context.TODO(),
ctx.Context,
vmDBManager,
db,
genesisData,
chainConfig.Upgrade,
chainConfig.Config,
Expand Down Expand Up @@ -796,7 +795,7 @@ func (m *manager) createAvalancheChain(

registerer: snowmanRegisterer,
ctx: ctx.Context,
dbManager: vmDBManager,
db: db,
genesisBytes: genesisData,
upgradeBytes: chainConfig.Upgrade,
configBytes: chainConfig.Config,
Expand Down Expand Up @@ -1004,15 +1003,13 @@ func (m *manager) createSnowmanChain(
State: snow.Initializing,
})

meterDBManager, err := m.DBManager.NewMeterDBManager("db", ctx.Registerer)
meterDB, err := meterdb.New("db", ctx.Registerer, m.DB)
if err != nil {
return nil, err
}
prefixDBManager := meterDBManager.NewPrefixDBManager(ctx.ChainID[:])
vmDBManager := prefixDBManager.NewPrefixDBManager(vmDBPrefix)

db := prefixDBManager.Current()
bootstrappingDB := prefixdb.New(bootstrappingDB, db.Database)
prefixDB := prefixdb.New(ctx.ChainID[:], meterDB)
db := prefixdb.New(vmDBPrefix, prefixDB)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
bootstrappingDB := prefixdb.New(bootstrappingDB, db)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved

blocked, err := queue.NewWithMissing(bootstrappingDB, "block", ctx.Registerer)
if err != nil {
Expand Down Expand Up @@ -1145,7 +1142,7 @@ func (m *manager) createSnowmanChain(
if err := vm.Initialize(
context.TODO(),
ctx.Context,
vmDBManager,
db,
genesisData,
chainConfig.Upgrade,
chainConfig.Config,
Expand Down
Loading
Loading