Skip to content

Commit

Permalink
Remove db manager (#367)
Browse files Browse the repository at this point in the history
* remove database.Manager

* remove database.Manager
  • Loading branch information
Dan Laine authored Nov 1, 2023
1 parent 0fda487 commit 57451b0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/VictoriaMetrics/fastcache v1.10.0
github.com/ava-labs/avalanchego v1.10.15-0.20231027221648-efd633e292ee
github.com/ava-labs/avalanchego v1.10.15-rc.1
github.com/cespare/cp v0.1.0
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811
github.com/davecgh/go-spew v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/avalanchego v1.10.15-0.20231027221648-efd633e292ee h1:eSjxL/eA6/dj+tL1lT5zDWF2ECcYrZVwR4nbReiGV0A=
github.com/ava-labs/avalanchego v1.10.15-0.20231027221648-efd633e292ee/go.mod h1:AoVh5J/z6Mv5+Gfx3rvnF79ukO4HU6LM7vhXIcFp/MI=
github.com/ava-labs/avalanchego v1.10.15-rc.1 h1:5MQ6JAojk1oxTkD/Fj833+X3/4ARM1oOuLwNOuOFjKc=
github.com/ava-labs/avalanchego v1.10.15-rc.1/go.mod h1:IT0UDCv0JjeAog/wSC8eFbr/wsmiUhdDZ2wpYVGIFE0=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
12 changes: 6 additions & 6 deletions plugin/evm/syncervm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestStateSyncToggleEnabledToDisabled(t *testing.T) {
if err := syncDisabledVM.Initialize(
context.Background(),
vmSetup.syncerVM.ctx,
vmSetup.syncerDBManager,
vmSetup.syncerDB,
[]byte(genesisJSONLatest),
nil,
[]byte(stateSyncDisabledConfigJSON),
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestStateSyncToggleEnabledToDisabled(t *testing.T) {
if err := syncReEnabledVM.Initialize(
context.Background(),
vmSetup.syncerVM.ctx,
vmSetup.syncerDBManager,
vmSetup.syncerDB,
[]byte(genesisJSONLatest),
nil,
[]byte(configJSON),
Expand Down Expand Up @@ -342,7 +342,7 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest) *syncVMSetup {

// initialise [syncerVM] with blank genesis state
stateSyncEnabledJSON := fmt.Sprintf(`{"state-sync-enabled":true, "state-sync-min-blocks": %d}`, test.stateSyncMinBlocks)
syncerEngineChan, syncerVM, syncerDBManager, syncerAtomicMemory, syncerAppSender := GenesisVMWithUTXOs(
syncerEngineChan, syncerVM, syncerDB, syncerAtomicMemory, syncerAppSender := GenesisVMWithUTXOs(
t, false, "", stateSyncEnabledJSON, "", alloc,
)
shutdownOnceSyncerVM := &shutdownOnceVM{VM: syncerVM}
Expand Down Expand Up @@ -395,7 +395,7 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest) *syncVMSetup {
},
fundedAccounts: accounts,
syncerVM: syncerVM,
syncerDBManager: syncerDBManager,
syncerDB: syncerDB,
syncerEngineChan: syncerEngineChan,
syncerAtomicMemory: syncerAtomicMemory,
shutdownOnceSyncerVM: shutdownOnceSyncerVM,
Expand All @@ -412,7 +412,7 @@ type syncVMSetup struct {
fundedAccounts map[*keystore.Key]*types.StateAccount

syncerVM *VM
syncerDBManager manager.Manager
syncerDB database.Database
syncerEngineChan <-chan commonEng.Message
syncerAtomicMemory *atomic.Memory
shutdownOnceSyncerVM *shutdownOnceVM
Expand Down
8 changes: 3 additions & 5 deletions plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import (
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -358,7 +357,7 @@ func (vm *VM) GetActivationTime() time.Time {
func (vm *VM) Initialize(
_ context.Context,
chainCtx *snow.Context,
dbManager manager.Manager,
db database.Database,
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
Expand Down Expand Up @@ -414,11 +413,10 @@ func (vm *VM) Initialize(

vm.toEngine = toEngine
vm.shutdownChan = make(chan struct{}, 1)
baseDB := dbManager.Current().Database
// Use NewNested rather than New so that the structure of the database
// remains the same regardless of the provided baseDB type.
vm.chaindb = Database{prefixdb.NewNested(ethDBPrefix, baseDB)}
vm.db = versiondb.New(baseDB)
vm.chaindb = Database{prefixdb.NewNested(ethDBPrefix, db)}
vm.db = versiondb.New(db)
vm.acceptedBlockDB = prefixdb.New(acceptedPrefix, vm.db)
vm.metadataDB = prefixdb.New(metadataPrefix, vm.db)

Expand Down
38 changes: 15 additions & 23 deletions plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import (

"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
Expand All @@ -50,7 +51,6 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/chain"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand Down Expand Up @@ -176,7 +176,7 @@ func NewContext() *snow.Context {
func setupGenesis(t *testing.T,
genesisJSON string,
) (*snow.Context,
manager.Manager,
database.Database,
[]byte,
chan engCommon.Message,
*atomic.Memory) {
Expand All @@ -186,13 +186,9 @@ func setupGenesis(t *testing.T,
genesisBytes := BuildGenesisTest(t, genesisJSON)
ctx := NewContext()

baseDBManager := manager.NewMemDB(&version.Semantic{
Major: 1,
Minor: 4,
Patch: 5,
})
baseDB := memdb.New()

m := atomic.NewMemory(prefixdb.New([]byte{0}, baseDBManager.Current().Database))
m := atomic.NewMemory(prefixdb.New([]byte{0}, baseDB))
ctx.SharedMemory = m.NewSharedMemory(ctx.ChainID)

// NB: this lock is intentionally left locked when this function returns.
Expand All @@ -201,20 +197,16 @@ func setupGenesis(t *testing.T,

userKeystore := keystore.New(
logging.NoLog{},
manager.NewMemDB(&version.Semantic{
Major: 1,
Minor: 4,
Patch: 5,
}),
memdb.New(),
)
if err := userKeystore.CreateUser(username, password); err != nil {
t.Fatal(err)
}
ctx.Keystore = userKeystore.NewBlockchainKeyStore(ctx.ChainID)

issuer := make(chan engCommon.Message, 1)
prefixedDBManager := baseDBManager.NewPrefixDBManager([]byte{1})
return ctx, prefixedDBManager, genesisBytes, issuer, m
prefixedDB := prefixdb.New([]byte{1}, baseDB)
return ctx, prefixedDB, genesisBytes, issuer, m
}

// GenesisVM creates a VM instance with the genesis test bytes and returns
Expand All @@ -226,18 +218,18 @@ func GenesisVM(t *testing.T,
configJSON string,
upgradeJSON string,
) (chan engCommon.Message,
*VM, manager.Manager,
*VM, database.Database,
*atomic.Memory,
*engCommon.SenderTest) {
vm := &VM{}
ctx, dbManager, genesisBytes, issuer, m := setupGenesis(t, genesisJSON)
ctx, db, genesisBytes, issuer, m := setupGenesis(t, genesisJSON)
appSender := &engCommon.SenderTest{T: t}
appSender.CantSendAppGossip = true
appSender.SendAppGossipF = func(context.Context, []byte) error { return nil }
if err := vm.Initialize(
context.Background(),
ctx,
dbManager,
db,
genesisBytes,
[]byte(upgradeJSON),
[]byte(configJSON),
Expand All @@ -253,7 +245,7 @@ func GenesisVM(t *testing.T,
assert.NoError(t, vm.SetState(context.Background(), snow.NormalOp))
}

return issuer, vm, dbManager, m, appSender
return issuer, vm, db, m, appSender
}

func addUTXO(sharedMemory *atomic.Memory, ctx *snow.Context, txID ids.ID, index uint32, assetID ids.ID, amount uint64, addr ids.ShortID) (*avax.UTXO, error) {
Expand Down Expand Up @@ -294,8 +286,8 @@ func addUTXO(sharedMemory *atomic.Memory, ctx *snow.Context, txID ids.ID, index
// GenesisVMWithUTXOs creates a GenesisVM and generates UTXOs in the X-Chain Shared Memory containing AVAX based on the [utxos] map
// Generates UTXOIDs by using a hash of the address in the [utxos] map such that the UTXOs will be generated deterministically.
// If [genesisJSON] is empty, defaults to using [genesisJSONLatest]
func GenesisVMWithUTXOs(t *testing.T, finishBootstrapping bool, genesisJSON string, configJSON string, upgradeJSON string, utxos map[ids.ShortID]uint64) (chan engCommon.Message, *VM, manager.Manager, *atomic.Memory, *engCommon.SenderTest) {
issuer, vm, dbManager, sharedMemory, sender := GenesisVM(t, finishBootstrapping, genesisJSON, configJSON, upgradeJSON)
func GenesisVMWithUTXOs(t *testing.T, finishBootstrapping bool, genesisJSON string, configJSON string, upgradeJSON string, utxos map[ids.ShortID]uint64) (chan engCommon.Message, *VM, database.Database, *atomic.Memory, *engCommon.SenderTest) {
issuer, vm, db, sharedMemory, sender := GenesisVM(t, finishBootstrapping, genesisJSON, configJSON, upgradeJSON)
for addr, avaxAmount := range utxos {
txID, err := ids.ToID(hashing.ComputeHash256(addr.Bytes()))
if err != nil {
Expand All @@ -306,7 +298,7 @@ func GenesisVMWithUTXOs(t *testing.T, finishBootstrapping bool, genesisJSON stri
}
}

return issuer, vm, dbManager, sharedMemory, sender
return issuer, vm, db, sharedMemory, sender
}

func TestVMConfig(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/versions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

# Don't export them as they're used in the context of other calls
avalanche_version=${AVALANCHE_VERSION:-'826f9415'}
avalanche_version=${AVALANCHE_VERSION:-'v1.10.15-rc.1'}

0 comments on commit 57451b0

Please sign in to comment.