@ -23,8 +23,10 @@ var ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account
// KEYCURRENTBATCH is used as key in the db to store the current BatchNum
// KEYCURRENTBATCH is used as key in the db to store the current BatchNum
var KEYCURRENTBATCH = [ ] byte ( "currentbatch" )
var KEYCURRENTBATCH = [ ] byte ( "currentbatch" )
// STATEDBPATH defines the subpath of the StateDB
const STATEDBPATH = "/statedb"
// PATHSTATEDB defines the subpath of the StateDB
const PATHSTATEDB = "/statedb"
const PATHBATCHNUM = "/BatchNum"
const PATHCURRENT = "/current"
// StateDB represents the StateDB object
// StateDB represents the StateDB object
type StateDB struct {
type StateDB struct {
@ -39,7 +41,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 + STATEDBPATH + "/current" , false )
sto , err = pebble . NewPebbleStorage ( path + PATHSTATEDB + PATHCURRENT , false )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -53,7 +55,7 @@ func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) {
}
}
sdb := & StateDB {
sdb := & StateDB {
path : path + STATEDBPATH ,
path : path + PATH STATEDB,
db : sto ,
db : sto ,
mt : mt ,
mt : mt ,
}
}
@ -100,14 +102,17 @@ func (s *StateDB) setCurrentBatch() error {
return nil
return nil
}
}
// CheckPointA t does a checkpoint at the given batchNum in the defined path
// MakeCheckpoin t does a checkpoint at the given batchNum in the defined path. Internally this advances & stores the current BatchNum, and then stores a Checkpoint of the current state of the StateDB.
func ( s * StateDB ) MakeCheckpoint ( ) error {
func ( s * StateDB ) MakeCheckpoint ( ) error {
// advance currentBatch
// advance currentBatch
s . currentBatch ++
s . currentBatch ++
checkpointPath := s . path + "/BatchNum" + strconv . Itoa ( int ( s . currentBatch ) )
checkpointPath := s . path + PATHBATCHNUM + strconv . Itoa ( int ( s . currentBatch ) )
s . setCurrentBatch ( )
err := s . setCurrentBatch ( )
if err != nil {
return err
}
// if checkpoint BatchNum already exist in disk, delete it
// if checkpoint BatchNum already exist in disk, delete it
if _ , err := os . Stat ( checkpointPath ) ; ! os . IsNotExist ( err ) {
if _ , err := os . Stat ( checkpointPath ) ; ! os . IsNotExist ( err ) {
@ -118,7 +123,7 @@ func (s *StateDB) MakeCheckpoint() error {
}
}
// execute Checkpoint
// execute Checkpoint
err : = s . db . Pebble ( ) . Checkpoint ( checkpointPath )
err = s . db . Pebble ( ) . Checkpoint ( checkpointPath )
if err != nil {
if err != nil {
return err
return err
}
}
@ -128,7 +133,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 uint64 ) error {
func ( s * StateDB ) DeleteCheckpoint ( batchNum uint64 ) error {
checkpointPath := s . path + "/BatchNum" + 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 )
@ -142,8 +147,8 @@ func (s *StateDB) DeleteCheckpoint(batchNum uint64) error {
// those checkpoints will remain in the storage, and eventually will be
// those checkpoints will remain in the storage, and eventually will be
// deleted when MakeCheckpoint overwrites them.
// deleted when MakeCheckpoint overwrites them.
func ( s * StateDB ) Reset ( batchNum uint64 ) error {
func ( s * StateDB ) Reset ( batchNum uint64 ) error {
checkpointPath := s . path + "/BatchNum" + strconv . Itoa ( int ( batchNum ) )
currentPath := s . path + "/current"
checkpointPath := s . path + PATHBATCHNUM + strconv . Itoa ( int ( batchNum ) )
currentPath := s . path + PATHCURRENT
// remove 'current'
// remove 'current'
err := os . RemoveAll ( currentPath )
err := os . RemoveAll ( currentPath )
@ -311,9 +316,9 @@ func NewLocalStateDB(path string, synchronizerDB *StateDB, withMT bool, nLevels
// gets the state from LocalStateDB.synchronizerStateDB for the given batchNum. If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
// gets the state from LocalStateDB.synchronizerStateDB for the given batchNum. If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
func ( l * LocalStateDB ) Reset ( batchNum uint64 , fromSynchronizer bool ) error {
func ( l * LocalStateDB ) Reset ( batchNum uint64 , fromSynchronizer bool ) error {
synchronizerCheckpointPath := l . synchronizerStateDB . path + "/BatchNum" + strconv . Itoa ( int ( batchNum ) )
checkpointPath := l . path + "/BatchNum" + strconv . Itoa ( int ( batchNum ) )
currentPath := l . path + "/current"
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