Skip to content

Commit

Permalink
statedb and vochain/state: small refactor
Browse files Browse the repository at this point in the history
  * export statedb.mainTreeCfg -> statedb.MainTreeCfg
  * rename statedb.NewStateDB() -> statedb.New()
  * rename state.NewState() -> state.New()
  * statedb: replace t.tx.Commit() with equivalent t.SaveWithoutCommit()
  * state: replace hardcoded strings with Tree* consts
  * state: add TreeMain const, and allow calling StateTreeCfg(TreeMain)
  * state: deduplicate code in initStateDB with a for range loop
  • Loading branch information
altergui committed Mar 6, 2024
1 parent 33535f4 commit 8dfe265
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 106 deletions.
2 changes: 1 addition & 1 deletion cmd/tools/vochaininspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func openStateAtHeight(height int64, stateDir string) *statedb.TreeView {
if err != nil {
log.Fatalf("Can't open DB: %v", err)
}
sdb := statedb.NewStateDB(database)
sdb := statedb.New(database)
lastHeight, err := sdb.Version()
if err != nil {
log.Fatal("Can't get last height")
Expand Down
16 changes: 8 additions & 8 deletions statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ func (c *TreeConfig) HashFunc() arbo.HashFunction {
return c.hashFunc
}

// mainTreeCfg is the subTree configuration of the mainTree. It doesn't have a
// MainTreeCfg is the subTree configuration of the mainTree. It doesn't have a
// kindID because it's the top level tree. For the same reason, it doesn't
// contain functions to work with the parent leaf: it doesn't have a parent.
var mainTreeCfg = NewTreeSingletonConfig(TreeParams{
var MainTreeCfg = NewTreeSingletonConfig(TreeParams{
HashFunc: arbo.HashFunctionSha256,
KindID: "",
MaxLevels: 256,
Expand All @@ -209,10 +209,10 @@ type StateDB struct {
NoStateReadTx db.Reader
}

// NewStateDB returns an instance of the StateDB.
func NewStateDB(db db.Database) *StateDB {
// New returns an instance of the StateDB.
func New(db db.Database) *StateDB {
return &StateDB{
hashLen: mainTreeCfg.hashFunc.Len(),
hashLen: MainTreeCfg.hashFunc.Len(),
db: db,
}
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (s *StateDB) Hash() ([]byte, error) {
// either call treeTx.Commit or treeTx.Discard if BeginTx doesn't return an
// error. Calling treeTx.Discard after treeTx.Commit is ok.
func (s *StateDB) BeginTx() (treeTx *TreeTx, err error) {
cfg := mainTreeCfg
cfg := MainTreeCfg
// NOTE(Edu): The introduction of Batched Txs here came from the fact
// that Badger takes a lot of memory and as a preconfigured maximum
// memory allocated for a Tx, which we were easily reaching. But by
Expand Down Expand Up @@ -379,7 +379,7 @@ func (*readOnlyWriteTx) Discard() {}
// TreeView returns the mainTree opened at root as a TreeView for read-only.
// If root is nil, the last version's root is used.
func (s *StateDB) TreeView(root []byte) (*TreeView, error) {
cfg := mainTreeCfg
cfg := MainTreeCfg

if root == nil {
var err error
Expand All @@ -403,6 +403,6 @@ func (s *StateDB) TreeView(root []byte) (*TreeView, error) {
return &TreeView{
db: s.db,
tree: tree,
cfg: mainTreeCfg,
cfg: MainTreeCfg,
}, nil
}
12 changes: 6 additions & 6 deletions statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var emptyHash = make([]byte, 32)

func TestVersion(t *testing.T) {
sdb := NewStateDB(metadb.NewTest(t))
sdb := New(metadb.NewTest(t))
version, err := sdb.Version()
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, version, qt.Equals, uint32(0))
Expand All @@ -43,7 +43,7 @@ func TestVersion(t *testing.T) {
}

func TestStateDB(t *testing.T) {
sdb := NewStateDB(metadb.NewTest(t))
sdb := New(metadb.NewTest(t))
version, err := sdb.Version()
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, version, qt.Equals, uint32(0))
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestSubTree(t *testing.T) {
// - an instance of two non-singleton subTrees (multiACfg, multiBCfg)
// in a single leaf at path `id`. The leaf of the mainTree at path
// `id` == multiA_id.Root | multiB_id.Root
sdb := NewStateDB(metadb.NewTest(t))
sdb := New(metadb.NewTest(t))

mainTree, err := sdb.BeginTx()
qt.Assert(t, err, qt.IsNil)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestSubTree(t *testing.T) {
}

func TestNoState(t *testing.T) {
sdb := NewStateDB(metadb.NewTest(t))
sdb := New(metadb.NewTest(t))

mainTree, err := sdb.BeginTx()
qt.Assert(t, err, qt.IsNil)
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestTree(t *testing.T) {
}

func TestBigUpdate(t *testing.T) {
sdb := NewStateDB(metadb.NewTest(t))
sdb := New(metadb.NewTest(t))

mainTree, err := sdb.BeginTx()
qt.Assert(t, err, qt.IsNil)
Expand All @@ -468,7 +468,7 @@ func TestBigUpdate(t *testing.T) {
}

func TestBigUpdateDiscard(t *testing.T) {
sdb := NewStateDB(metadb.NewTest(t))
sdb := New(metadb.NewTest(t))

mainTree, err := sdb.BeginTx()
qt.Assert(t, err, qt.IsNil)
Expand Down
2 changes: 1 addition & 1 deletion statedb/treeupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (t *TreeTx) Commit(version uint32) error {
if err := t.CommitOnTx(version); err != nil {
return err
}
return t.tx.Commit()
return t.SaveWithoutCommit()
}

// CommitOnTx do as Commit but without committing the transaction to database.
Expand Down
2 changes: 1 addition & 1 deletion statedb/treeview.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (v *TreeView) SubTree(cfg TreeConfig) (treeView TreeViewer, err error) {
return &TreeView{
db: db,
tree: tree,
cfg: mainTreeCfg,
cfg: MainTreeCfg,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion test/testcommon/vochain.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
)

func NewVochainState(tb testing.TB) *state.State {
s, err := state.NewState(db.TypePebble, tb.TempDir())
s, err := state.New(db.TypePebble, tb.TempDir())
if err != nil {
tb.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion vochain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type ExecuteBlockResponse struct {
// Node still needs to be initialized with SetNode.
// Callback functions still need to be initialized.
func NewBaseApplication(vochainCfg *config.VochainCfg) (*BaseApplication, error) {
state, err := vstate.NewState(vochainCfg.DBType, vochainCfg.DataDir)
state, err := vstate.New(vochainCfg.DBType, vochainCfg.DataDir)
if err != nil {
return nil, fmt.Errorf("cannot create state: (%v)", err)
}
Expand Down
4 changes: 2 additions & 2 deletions vochain/ist/ist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestISTCschedule(t *testing.T) {
rng := testutil.NewRandom(0)
s, err := state.NewState(db.TypePebble, t.TempDir())
s, err := state.New(db.TypePebble, t.TempDir())
qt.Assert(t, err, qt.IsNil)
defer s.Close()
err = s.SetTimestamp(0)
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestISTCschedule(t *testing.T) {

func TestISTCsyncing(t *testing.T) {
rng := testutil.NewRandom(0)
s, err := state.NewState(db.TypePebble, t.TempDir())
s, err := state.New(db.TypePebble, t.TempDir())
qt.Assert(t, err, qt.IsNil)
defer s.Close()

Expand Down
8 changes: 4 additions & 4 deletions vochain/state/processblockregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSetStartBlock(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
// set a start block for a random electionId
pid := util.RandomBytes(32)
Expand All @@ -44,7 +44,7 @@ func TestDeleteStartBlock(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
// set a start block for a random electionId and delete it then
pid := util.RandomBytes(32)
Expand All @@ -60,7 +60,7 @@ func TestMinStartBlock(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
fromBlock := uint32(100)
// check min start block without create any election, fromBlock value
Expand All @@ -85,7 +85,7 @@ func TestMaxEndBlock(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
fromBlock := uint32(100)
// check max endBlock without create any election, fromBlock value
Expand Down
12 changes: 6 additions & 6 deletions vochain/state/sik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestSetAddressSIK(t *testing.T) {
c := qt.New(t)
// create a tree for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
// create a valid leaf
address := common.HexToAddress("0xF3668000B66c61aAa08aBC559a8C78Ae7E007C2e")
Expand All @@ -40,7 +40,7 @@ func TestDelSIK(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
// create a valid leaf
address := common.HexToAddress("0xF3668000B66c61aAa08aBC559a8C78Ae7E007C2e")
Expand Down Expand Up @@ -68,7 +68,7 @@ func Test_registerSIKCounter(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
c.Assert(err, qt.IsNil)

pid := util.RandomBytes(32)
Expand All @@ -93,7 +93,7 @@ func Test_sikRoots(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
// mock height and new sik and update the valid roots
address1 := common.HexToAddress("0xF3668000B66c61aAa08aBC559a8C78Ae7E007C2e")
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAssignSIKToElectionAndPurge(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)
// mock an account
testAccount := ethereum.NewSignKeys()
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestSIKDataRace(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
s, err := New(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)

// create some siks
Expand Down
65 changes: 13 additions & 52 deletions vochain/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ type State struct {
mtxValidSIKRoots sync.Mutex
}

// NewState creates a new State
func NewState(dbType, dataDir string) (*State, error) {
// New creates a new State
func New(dbType, dataDir string) (*State, error) {
database, err := metadb.New(dbType, filepath.Join(dataDir, storageDirectory))
if err != nil {
return nil, err
Expand Down Expand Up @@ -136,7 +136,7 @@ func NewState(dbType, dataDir string) (*State, error) {
// initStateDB initializes the StateDB with the default subTrees
func initStateDB(database db.Database) (*statedb.StateDB, error) {
log.Infof("initializing StateDB")
sdb := statedb.NewStateDB(database)
sdb := statedb.New(database)
startTime := time.Now()
defer func() { log.Infof("state database load took %s", time.Since(startTime)) }()
root, err := sdb.Hash()
Expand All @@ -152,59 +152,20 @@ func initStateDB(database db.Database) (*statedb.StateDB, error) {
return nil, err
}
defer update.Discard()
// Create the Extra, Validators and Processes subtrees (from
// Create the all the Main subtrees (from
// mainTree) by adding leaves in the mainTree that contain the
// corresponding tree roots, and opening the subTrees for the first
// time.
treeCfg := StateTreeCfg(TreeExtra)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
}
treeCfg = StateTreeCfg(TreeValidators)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
}
treeCfg = StateTreeCfg(TreeProcess)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
}
treeCfg = StateTreeCfg(TreeAccounts)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
}
treeCfg = StateTreeCfg(TreeFaucet)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
}
treeCfg = StateTreeCfg(TreeSIK)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
for name := range MainTrees {
treeCfg := StateTreeCfg(name)
if err := update.Add(treeCfg.Key(),
make([]byte, treeCfg.HashFunc().Len())); err != nil {
return nil, err
}
if _, err := update.SubTree(treeCfg); err != nil {
return nil, err
}
}

return sdb, update.Commit(0)
}

Expand Down
2 changes: 1 addition & 1 deletion vochain/state/state_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func newEmptyTreeForTest(t *testing.T) *tree.Tree {
}

func newStateForTest(t *testing.T) *State {
state, err := NewState(db.TypePebble, t.TempDir())
state, err := New(db.TypePebble, t.TempDir())
qt.Assert(t, err, qt.IsNil)
return state
}
Loading

0 comments on commit 8dfe265

Please sign in to comment.