Add Last db view in kvdb and statedb

Last db view is an opened pebble db which always contains a checkpoint from the
last batch.  Methods to access this last batch are thread safe so that views of
the last checkpoint can be made anywhere and with a consistent view of the
state.
This commit is contained in:
Eduard S
2021-02-02 15:42:52 +01:00
parent ca7fa8ae2e
commit 6590c47a9a
12 changed files with 422 additions and 160 deletions

View File

@@ -55,7 +55,7 @@ func (a *DebugAPI) handleAccount(c *gin.Context) {
badReq(err, c)
return
}
account, err := a.stateDB.GetAccount(common.Idx(uri.Idx))
account, err := a.stateDB.LastGetAccount(common.Idx(uri.Idx))
if err != nil {
badReq(err, c)
return
@@ -64,8 +64,12 @@ func (a *DebugAPI) handleAccount(c *gin.Context) {
}
func (a *DebugAPI) handleAccounts(c *gin.Context) {
accounts, err := a.stateDB.GetAccounts()
if err != nil {
var accounts []common.Account
if err := a.stateDB.LastRead(func(sdb *statedb.Last) error {
var err error
accounts, err = sdb.GetAccounts()
return err
}); err != nil {
badReq(err, c)
return
}
@@ -73,7 +77,7 @@ func (a *DebugAPI) handleAccounts(c *gin.Context) {
}
func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
batchNum, err := a.stateDB.GetCurrentBatch()
batchNum, err := a.stateDB.LastGetCurrentBatch()
if err != nil {
badReq(err, c)
return
@@ -82,7 +86,11 @@ func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
}
func (a *DebugAPI) handleMTRoot(c *gin.Context) {
root := a.stateDB.MTGetRoot()
root, err := a.stateDB.LastMTGetRoot()
if err != nil {
badReq(err, c)
return
}
c.JSON(http.StatusOK, root)
}

View File

@@ -66,6 +66,9 @@ func TestDebugAPI(t *testing.T) {
_, err = sdb.CreateAccount(account.Idx, account)
require.Nil(t, err)
}
// Make a checkpoint (batchNum 2) to make the accounts available in Last
err = sdb.MakeCheckpoint()
require.Nil(t, err)
url := fmt.Sprintf("http://%v/debug/", addr)
@@ -73,7 +76,7 @@ func TestDebugAPI(t *testing.T) {
req, err := sling.New().Get(url).Path("sdb/batchnum").ReceiveSuccess(&batchNum)
require.Equal(t, http.StatusOK, req.StatusCode)
require.Nil(t, err)
assert.Equal(t, common.BatchNum(1), batchNum)
assert.Equal(t, common.BatchNum(2), batchNum)
var mtroot *big.Int
req, err = sling.New().Get(url).Path("sdb/mtroot").ReceiveSuccess(&mtroot)