@ -21,19 +21,19 @@ var ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a
// already exists
// already exists
var ErrAccountAlreadyExists = errors . New ( "Can not CreateAccount because Account already exists" )
var ErrAccountAlreadyExists = errors . New ( "Can not CreateAccount because Account already exists" )
// KEYCURRENTBATCH is used as key in the db to store the current BatchNum
var KEYCURRENTBATCH = [ ] byte ( "currentbatch" )
// KeyCurrentBatch is used as key in the db to store the current BatchNum
var KeyCurrentBatch = [ ] byte ( "currentbatch" )
// PATHSTATE DB defines the subpath of the StateDB
const PATHSTATE DB = "/statedb"
// PathState DB defines the subpath of the StateDB
const PathState DB = "/statedb"
// PATHBATCHNUM defines the subpath of the Batch Checkpoint in the subpath of
// PathBatchNum defines the subpath of the Batch Checkpoint in the subpath of
// the StateDB
// the StateDB
const PATHBATCHNUM = "/BatchNum"
const PathBatchNum = "/BatchNum"
// PATHCURRENT defines the subpath of the current Batch in the subpath of the
// PathCurrent defines the subpath of the current Batch in the subpath of the
// StateDB
// StateDB
const PATHCURRENT = "/current"
const PathCurrent = "/current"
// StateDB represents the StateDB object
// StateDB represents the StateDB object
type StateDB struct {
type StateDB struct {
@ -50,7 +50,7 @@ type StateDB struct {
func NewStateDB ( path string , withMT bool , nLevels int ) ( * StateDB , error ) {
func NewStateDB ( path string , withMT bool , nLevels int ) ( * StateDB , error ) {
var sto * pebble . PebbleStorage
var sto * pebble . PebbleStorage
var err error
var err error
sto , err = pebble . NewPebbleStorage ( path + PATHSTATEDB + PATHCURRENT , false )
sto , err = pebble . NewPebbleStorage ( path + PathStateDB + PathCurrent , false )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -64,7 +64,7 @@ func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) {
}
}
sdb := & StateDB {
sdb := & StateDB {
path : path + PATHSTATE DB ,
path : path + PathState DB ,
db : sto ,
db : sto ,
mt : mt ,
mt : mt ,
}
}
@ -85,7 +85,7 @@ func (s *StateDB) DB() *pebble.PebbleStorage {
// GetCurrentBatch returns the current BatchNum stored in the StateDB
// GetCurrentBatch returns the current BatchNum stored in the StateDB
func ( s * StateDB ) GetCurrentBatch ( ) ( common . BatchNum , error ) {
func ( s * StateDB ) GetCurrentBatch ( ) ( common . BatchNum , error ) {
cbBytes , err := s . db . Get ( KEYCURRENTBATCH )
cbBytes , err := s . db . Get ( KeyCurrentBatch )
if err == db . ErrNotFound {
if err == db . ErrNotFound {
return 0 , nil
return 0 , nil
}
}
@ -101,7 +101,7 @@ func (s *StateDB) setCurrentBatch() error {
if err != nil {
if err != nil {
return err
return err
}
}
tx . Put ( KEYCURRENTBATCH , s . currentBatch . Bytes ( ) )
tx . Put ( KeyCurrentBatch , s . currentBatch . Bytes ( ) )
if err := tx . Commit ( ) ; err != nil {
if err := tx . Commit ( ) ; err != nil {
return err
return err
}
}
@ -113,7 +113,7 @@ func (s *StateDB) MakeCheckpoint() error {
// advance currentBatch
// advance currentBatch
s . currentBatch ++
s . currentBatch ++
checkpointPath := s . path + PATHBATCHNUM + strconv . Itoa ( int ( s . currentBatch ) )
checkpointPath := s . path + PathBatchNum + strconv . Itoa ( int ( s . currentBatch ) )
err := s . setCurrentBatch ( )
err := s . setCurrentBatch ( )
if err != nil {
if err != nil {
@ -139,7 +139,7 @@ func (s *StateDB) MakeCheckpoint() error {
// DeleteCheckpoint removes if exist the checkpoint of the given batchNum
// DeleteCheckpoint removes if exist the checkpoint of the given batchNum
func ( s * StateDB ) DeleteCheckpoint ( batchNum common . BatchNum ) error {
func ( s * StateDB ) DeleteCheckpoint ( batchNum common . BatchNum ) error {
checkpointPath := s . path + PATHBATCHNUM + strconv . Itoa ( int ( batchNum ) )
checkpointPath := s . path + PathBatchNum + strconv . Itoa ( int ( batchNum ) )
if _ , err := os . Stat ( checkpointPath ) ; os . IsNotExist ( err ) {
if _ , err := os . Stat ( checkpointPath ) ; os . IsNotExist ( err ) {
return fmt . Errorf ( "Checkpoint with batchNum %d does not exist in DB" , batchNum )
return fmt . Errorf ( "Checkpoint with batchNum %d does not exist in DB" , batchNum )
@ -158,8 +158,8 @@ func (s *StateDB) Reset(batchNum common.BatchNum) error {
return nil
return nil
}
}
checkpointPath := s . path + PATHBATCHNUM + strconv . Itoa ( int ( batchNum ) )
currentPath := s . path + PATHCURRENT
checkpointPath := s . path + PathBatchNum + strconv . Itoa ( int ( batchNum ) )
currentPath := s . path + PathCurrent
// remove 'current'
// remove 'current'
err := os . RemoveAll ( currentPath )
err := os . RemoveAll ( currentPath )
@ -217,7 +217,7 @@ func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error)
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
var b [ 32 * common . NLEAFELEMS ] byte
var b [ 32 * common . NLeafElems ] byte
copy ( b [ : ] , accBytes )
copy ( b [ : ] , accBytes )
return common . AccountFromBytes ( b )
return common . AccountFromBytes ( b )
}
}
@ -345,9 +345,9 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er
return nil
return nil
}
}
synchronizerCheckpointPath := l . synchronizerStateDB . path + PATHBATCHNUM + strconv . Itoa ( int ( batchNum ) )
checkpointPath := l . path + PATHBATCHNUM + strconv . Itoa ( int ( batchNum ) )
currentPath := l . path + PATHCURRENT
synchronizerCheckpointPath := l . synchronizerStateDB . path + PathBatchNum + strconv . Itoa ( int ( batchNum ) )
checkpointPath := l . path + PathBatchNum + strconv . Itoa ( int ( batchNum ) )
currentPath := l . path + PathCurrent
if fromSynchronizer {
if fromSynchronizer {
// use checkpoint from SynchronizerStateDB
// use checkpoint from SynchronizerStateDB