Skip to content

Commit a79c413

Browse files
Merge branch 'dev' into health_checks_cached_entries
2 parents 140ff3b + 158c320 commit a79c413

File tree

9 files changed

+85
-61
lines changed

9 files changed

+85
-61
lines changed

database/leveldb/db.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/ava-labs/avalanchego/database"
1717
"github.com/ava-labs/avalanchego/utils"
18+
"github.com/ava-labs/avalanchego/utils/logging"
1819
)
1920

2021
const (
@@ -36,6 +37,7 @@ const (
3637
// in binary-alphabetical order.
3738
type Database struct {
3839
*leveldb.DB
40+
log logging.Logger
3941

4042
// True if there was previously an error other than "not found" or "closed"
4143
// while performing a db operation. If [errored] == true, Has, Get, Put,
@@ -45,7 +47,7 @@ type Database struct {
4547
}
4648

4749
// New returns a wrapped LevelDB object.
48-
func New(file string, blockCacheSize, writeBufferSize, handleCap int) (*Database, error) {
50+
func New(file string, log logging.Logger, blockCacheSize, writeBufferSize, handleCap int) (*Database, error) {
4951
// Enforce minimums
5052
if blockCacheSize < minBlockCacheSize {
5153
blockCacheSize = minBlockCacheSize
@@ -71,7 +73,10 @@ func New(file string, blockCacheSize, writeBufferSize, handleCap int) (*Database
7173
if err != nil {
7274
return nil, err
7375
}
74-
return &Database{DB: db}, nil
76+
return &Database{
77+
DB: db,
78+
log: log,
79+
}, nil
7580
}
7681

7782
// Has returns if the key is set in the database
@@ -169,6 +174,7 @@ func (db *Database) handleError(err error) error {
169174
// If we get an error other than "not found" or "closed", disallow future
170175
// database operations to avoid possible corruption
171176
if err != nil && err != database.ErrNotFound && err != database.ErrClosed {
177+
db.log.Fatal("leveldb error: %w", err)
172178
db.errored = true
173179
}
174180
return err

database/leveldb/db_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"testing"
1010

1111
"github.com/ava-labs/avalanchego/database"
12+
"github.com/ava-labs/avalanchego/utils/logging"
1213
)
1314

1415
func TestInterface(t *testing.T) {
1516
for i, test := range database.Tests {
1617
folder := fmt.Sprintf("db%d", i)
1718

18-
db, err := New(folder, 0, 0, 0)
19+
db, err := New(folder, logging.NoLog{}, 0, 0, 0)
1920
if err != nil {
2021
t.Fatalf("leveldb.New(%s, 0, 0) errored with %s", folder, err)
2122
}

main/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
assertionsEnabledKey = "assertions-enabled"
2323
signatureVerificationEnabledKey = "signature-verification-enabled"
2424
dbEnabledKey = "db-enabled"
25-
dbDirKey = "db-dir"
25+
dbPathKey = "db-dir"
2626
publicIPKey = "public-ip"
2727
dynamicUpdateDurationKey = "dynamic-update-duration"
2828
dynamicPublicIPResolverKey = "dynamic-public-ip"

main/main.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package main
66
import (
77
"fmt"
88

9+
"github.com/ava-labs/avalanchego/database"
10+
"github.com/ava-labs/avalanchego/database/leveldb"
11+
"github.com/ava-labs/avalanchego/database/memdb"
912
"github.com/ava-labs/avalanchego/nat"
1013
"github.com/ava-labs/avalanchego/node"
1114
"github.com/ava-labs/avalanchego/utils"
@@ -48,14 +51,25 @@ func main() {
4851
}
4952
fmt.Println(header)
5053

54+
var db database.Database
55+
if Config.DBEnabled {
56+
db, err = leveldb.New(Config.DBPath, log, 0, 0, 0)
57+
if err != nil {
58+
log.Error("couldn't open database at %s: %s", Config.DBPath, err)
59+
return
60+
}
61+
} else {
62+
db = memdb.New()
63+
}
64+
5165
defer func() {
5266
if r := recover(); r != nil {
5367
fmt.Println("Recovered panic from", r)
5468
}
5569
}()
5670

5771
defer func() {
58-
if err := Config.DB.Close(); err != nil {
72+
if err := db.Close(); err != nil {
5973
log.Warn("failed to close the node's DB: %s", err)
6074
}
6175
log.StopOnPanic()
@@ -134,7 +148,7 @@ func main() {
134148
log.Info("this node's IP is set to: %s", Config.StakingIP.IP())
135149

136150
for {
137-
shouldRestart, err := run(log, logFactory)
151+
shouldRestart, err := run(db, log, logFactory)
138152
if err != nil {
139153
break
140154
}
@@ -148,14 +162,14 @@ func main() {
148162

149163
// Initialize and run the node.
150164
// Returns true if the node should restart after this function returns.
151-
func run(log logging.Logger, logFactory logging.Factory) (bool, error) {
165+
func run(db database.Database, log logging.Logger, logFactory logging.Factory) (bool, error) {
152166
log.Info("initializing node")
153167
node := node.Node{}
154168
restarter := &restarter{
155169
node: &node,
156170
shouldRestart: &utils.AtomicBool{},
157171
}
158-
if err := node.Initialize(&Config, log, logFactory, restarter); err != nil {
172+
if err := node.Initialize(&Config, db, log, logFactory, restarter); err != nil {
159173
log.Error("error initializing node: %s", err)
160174
return restarter.shouldRestart.GetValue(), err
161175
}

main/params.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020

2121
"github.com/kardianos/osext"
2222

23-
"github.com/ava-labs/avalanchego/database/leveldb"
24-
"github.com/ava-labs/avalanchego/database/memdb"
2523
"github.com/ava-labs/avalanchego/genesis"
2624
"github.com/ava-labs/avalanchego/ids"
2725
"github.com/ava-labs/avalanchego/ipcs"
@@ -94,9 +92,12 @@ func avalancheFlagSet() *flag.FlagSet {
9492
fs.String(pluginDirKey, defaultString, "Plugin directory for Avalanche VMs")
9593
// Network ID
9694
fs.String(networkNameKey, defaultNetworkName, "Network ID this node will connect to")
95+
// AVAX fees
96+
fs.Uint64(txFeeKey, units.MilliAvax, "Transaction fee, in nAVAX")
97+
fs.Uint64(creationTxFeeKey, units.MilliAvax, "Transaction fee, in nAVAX, for transactions that create new state")
9798
// Database
9899
fs.Bool(dbEnabledKey, true, "Turn on persistent storage")
99-
fs.String(dbDirKey, defaultString, "Database directory for Avalanche state")
100+
fs.String(dbPathKey, defaultDbDir, "Path to database directory")
100101
// Coreth Config
101102
fs.String(corethConfigKey, defaultString, "Specifies config to pass into coreth")
102103
// Logging
@@ -108,9 +109,6 @@ func avalancheFlagSet() *flag.FlagSet {
108109
fs.Bool(assertionsEnabledKey, true, "Turn on assertion execution")
109110
// Signature Verification
110111
fs.Bool(signatureVerificationEnabledKey, true, "Turn on signature verification")
111-
// Fees
112-
fs.Uint64(txFeeKey, units.MilliAvax, "Transaction fee, in nAVAX")
113-
fs.Uint64(creationTxFeeKey, units.MilliAvax, "Transaction fee, in nAVAX, for transactions that create new state")
114112

115113
// Networking
116114
// Public IP Resolution
@@ -179,7 +177,7 @@ func avalancheFlagSet() *flag.FlagSet {
179177
fs.Duration(healthCheckAveragerHalflifeKey, 10*time.Second, "Halflife of averager when calculating a running average in a health check")
180178
// Network Layer Health
181179
fs.Duration(networkHealthMaxTimeSinceMsgSentKey, time.Minute, "Network layer returns unhealthy if haven't received a message for at least this much time")
182-
fs.Duration(networkHealthMaxTimeSinceMsgReceivedKey, time.Minute, "Netowork layer returns unhealthy if haven't received a message for at least this much time")
180+
fs.Duration(networkHealthMaxTimeSinceMsgReceivedKey, time.Minute, "Network layer returns unhealthy if haven't received a message for at least this much time")
183181
fs.Float64(networkHealthMaxPortionSendQueueFillKey, 0.9, "Network layer returns unhealthy if more than this portion of the pending send queue is full")
184182
fs.Uint(networkHealthMinPeersKey, 1, "Network layer returns unhealthy if connected to less than this many peers")
185183
fs.Float64(networkHealthMaxSendFailRateKey, .25, "Network layer reports unhealthy if more than this portion of attempted message sends fail")
@@ -317,21 +315,10 @@ func setNodeConfig(v *viper.Viper) error {
317315
Config.NetworkID = networkID
318316

319317
// DB:
320-
if v.GetBool(dbEnabledKey) {
321-
dbDir := v.GetString(dbDirKey)
322-
if dbDir == defaultString {
323-
dbDir = defaultDbDir
324-
}
325-
dbDir = os.ExpandEnv(dbDir) // parse any env variables
326-
dbPath := path.Join(dbDir, constants.NetworkName(Config.NetworkID), dbVersion)
327-
db, err := leveldb.New(dbPath, 0, 0, 0)
328-
if err != nil {
329-
return fmt.Errorf("couldn't create db at %s: %w", dbPath, err)
330-
}
331-
Config.DB = db
332-
} else {
333-
Config.DB = memdb.New()
334-
}
318+
Config.DBEnabled = v.GetBool(dbEnabledKey)
319+
Config.DBPath = v.GetString(dbPathKey)
320+
Config.DBPath = os.ExpandEnv(Config.DBPath) // parse any env variables
321+
Config.DBPath = path.Join(Config.DBPath, constants.NetworkName(Config.NetworkID), dbVersion)
335322

336323
// IP Configuration
337324
// Resolves our public IP, or does nothing

node/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package node
66
import (
77
"time"
88

9-
"github.com/ava-labs/avalanchego/database"
109
"github.com/ava-labs/avalanchego/genesis"
1110
"github.com/ava-labs/avalanchego/ids"
1211
"github.com/ava-labs/avalanchego/nat"
@@ -43,8 +42,11 @@ type Config struct {
4342
// Crypto configuration
4443
EnableCrypto bool
4544

46-
// Database to use for the node
47-
DB database.Database
45+
// Path to database
46+
DBPath string
47+
48+
// If false, uses an in memory database
49+
DBEnabled bool
4850

4951
// Staking configuration
5052
StakingIP utils.DynamicIPDesc

node/node.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ func (n *Node) Dispatch() error {
377377
******************************************************************************
378378
*/
379379

380-
func (n *Node) initDatabase() error {
381-
n.DB = n.Config.DB
380+
func (n *Node) initDatabase(db database.Database) error {
381+
n.DB = db
382382

383383
rawExpectedGenesisHash := hashing.ComputeHash256(n.Config.GenesisBytes)
384384

@@ -824,6 +824,7 @@ func (n *Node) initAliases(genesisBytes []byte) error {
824824
// Initialize this node
825825
func (n *Node) Initialize(
826826
config *Config,
827+
db database.Database,
827828
logger logging.Logger,
828829
logFactory logging.Factory,
829830
restarter utils.Restarter,
@@ -841,7 +842,7 @@ func (n *Node) Initialize(
841842
}
842843
n.HTTPLog = httpLog
843844

844-
if err := n.initDatabase(); err != nil { // Set up the node's database
845+
if err := n.initDatabase(db); err != nil { // Set up the node's database
845846
return fmt.Errorf("problem initializing database: %w", err)
846847
}
847848
if err = n.initNodeID(); err != nil { // Derive this node's ID

snow/engine/common/queue/jobs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (j *Jobs) Execute(job Job) error {
124124
}
125125
}
126126

127-
return nil
127+
return j.state.DeleteJob(j.db, jobID)
128128
}
129129

130130
// Commit ...

0 commit comments

Comments
 (0)