Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/spf13/pflag v1.0.6
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/unicitynetwork/bft-go-base v1.0.3-0.20260316092951-afcfbc83f42f
github.com/unicitynetwork/bft-go-base v1.1.0
go.etcd.io/bbolt v1.4.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/unicitynetwork/bft-go-base v1.0.3-0.20260316092951-afcfbc83f42f h1:HiqLjm+aM0VX5oLJLAEiqZCUnQhf1KrL6rCpco9FMFA=
github.com/unicitynetwork/bft-go-base v1.0.3-0.20260316092951-afcfbc83f42f/go.mod h1:hBnOG52VRy/vpgIBUulTgk7PBTwODZ2xkVjCEu5yRcQ=
github.com/unicitynetwork/bft-go-base v1.1.0 h1:x1+kX0X+n4CmNibBs0f8oWwoZ5UCW40Gaq6WYhUcUZQ=
github.com/unicitynetwork/bft-go-base v1.1.0/go.mod h1:hBnOG52VRy/vpgIBUulTgk7PBTwODZ2xkVjCEu5yRcQ=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
Expand Down
5 changes: 3 additions & 2 deletions rootchain/consensus/storage/block_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestExecutedBlock_Extend(t *testing.T) {
func TestExecutedBlock_GenerateCertificates(t *testing.T) {
const partitionID1 types.PartitionID = 1
const partitionID2 types.PartitionID = 2
rh, err := hex.DecodeString("51592107828763663BE3378AD1F4BAE7D9C1A921DEEC1A6B28247770A8B4F526")
rh, err := hex.DecodeString("CC44C256E1AC47E941FCA307FABFE7BF27F318175A865D6891D31988AE90DDF7")
require.NoError(t, err)

validBlock := func() *ExecutedBlock {
Expand Down Expand Up @@ -454,7 +454,8 @@ func Test_ExecutedBlock_serialization(t *testing.T) {
Leader: "ldr",
},
UC: types.UnicityCertificate{
Version: 1,
Version: 1,
ShardTreeCertificate: types.NewShardTreeCertificate(),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion rootchain/consensus/storage/block_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func Test_BlockTree_Add(t *testing.T) {
require.NoError(t, err)
k := types.PartitionShardID{PartitionID: 1, ShardID: types.ShardID{}.Key()}
b.ShardState.States[k] = &ShardInfo{PartitionID: 1, IR: &types.InputRecord{}}
b.RootHash = hexToBytes("F8C1F929F9E718FE5B19DD72BFD23802FFFE5FAC21711BF425548548262942E5")
b.RootHash = hexToBytes("02D6563BFE92C38E0FD37A647F982B49C5B8980A441BCD395E5CC304E2217FCD")

commitQc := &drctypes.QuorumCert{
VoteInfo: &drctypes.RoundInfo{
Expand Down
74 changes: 40 additions & 34 deletions rootchain/consensus/storage/db_bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type BoltDB struct {
db *bbolt.DB
}

const currentDBVersion uint64 = 1
Comment thread
MastaP marked this conversation as resolved.

func NewBoltStorage(file string) (db BoltDB, err error) {
_, err = os.Stat(file)
newDB := err != nil && errors.Is(err, fs.ErrNotExist)
Expand All @@ -53,18 +55,50 @@ func NewBoltStorage(file string) (db BoltDB, err error) {
}()

if newDB {
if err := db.db.Update(initVersion1Buckets); err != nil {
if err := db.db.Update(initBuckets); err != nil {
return db, fmt.Errorf("initializing new database: %w", err)
}
} else {
if err := db.migrateTo(1); err != nil {
return db, fmt.Errorf("upgrading DB version: %w", err)
}
return db, nil
}

ver, err := db.getVersion()
if err != nil {
return db, fmt.Errorf("reading database version: %w", err)
}
if ver != currentDBVersion {
return db, fmt.Errorf("unsupported database version %d, expected %d", ver, currentDBVersion)
}
return db, nil
}

// initBuckets creates the bucket layout and writes the current version marker
// into a fresh bbolt database.
func initBuckets(tx *bbolt.Tx) error {
if _, err := tx.CreateBucket(bucketBlocks); err != nil {
return fmt.Errorf("creating bucket for blocks: %w", err)
}
if _, err := tx.CreateBucket(bucketCertificates); err != nil {
return fmt.Errorf("creating bucket for certificates: %w", err)
}
if _, err := tx.CreateBucket(bucketVotes); err != nil {
return fmt.Errorf("creating bucket for votes: %w", err)
}
b, err := tx.CreateBucket(bucketSafety)
if err != nil {
return fmt.Errorf("creating bucket for safety: %w", err)
}
if err := writeUint64(b, keyHighestQc, rctypes.GenesisRootRound); err != nil {
return fmt.Errorf("storing highest QC round: %w", err)
}
if err := writeUint64(b, keyHighestVoted, rctypes.GenesisRootRound); err != nil {
return fmt.Errorf("storing highest voted round: %w", err)
}
if _, err := tx.CreateBucket(bucketMetadata); err != nil {
return fmt.Errorf("creating bucket for metadata: %w", err)
}
return setVersion(tx, currentDBVersion)
}

func (db BoltDB) Close() error { return db.db.Close() }

var errNoBlocksBucket = errors.New("blocks bucket not found")
Expand Down Expand Up @@ -321,39 +355,11 @@ func (db BoltDB) SetHighestQcRound(qcRound, votedRound uint64) error {
})
}

/*
migrateTo upgrades database to version "ver" if the current version is older.
*/
func (db BoltDB) migrateTo(ver uint64) error {
curVer, err := db.getVersion()
if err != nil {
return fmt.Errorf("determining current version of the database: %w", err)
}
if curVer > ver {
return fmt.Errorf("downgrading database version not supported, current is %d, asking for %d", curVer, ver)
}

for ; curVer < ver; curVer++ {
switch curVer {
case 0:
err = db.migrate_0_to_1()
default:
return fmt.Errorf("migration from version %d to the next version not implemented", curVer)
}

if err != nil {
return fmt.Errorf("migrating from version %d: %w", curVer, err)
}
}
return nil
}

func (db BoltDB) getVersion() (ver uint64, _ error) {
return ver, db.db.View(func(tx *bbolt.Tx) (err error) {
b := tx.Bucket(bucketMetadata)
if b == nil {
// no bucket, must be version 0 database
return nil
return errors.New("metadata bucket not found")
}
ver, err = readUint64(b, keyDbVersion)
return err
Expand Down
152 changes: 0 additions & 152 deletions rootchain/consensus/storage/db_migrate_0_1.go

This file was deleted.

Loading
Loading