Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
IS-873: send last commit block height with VERSION, READY message
Browse files Browse the repository at this point in the history
- refactor DBInfoData with DBInfoDataV2
  • Loading branch information
eunsoopark committed Nov 19, 2019
1 parent 773a4ea commit a19f67e
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 189 deletions.
62 changes: 60 additions & 2 deletions cmd/iissdata/cli_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"encoding/hex"
"fmt"
"github.com/icon-project/rewardcalculator/common"
"log"
"os"
"path/filepath"

Expand All @@ -24,8 +26,8 @@ func (cli *CLI) read(dbDir string, dbName string) {

iissDB := core.OpenIISSData(path)
core.LoadIISSData(iissDB)
core.ReadIISSBP(iissDB)
core.ReadIISSTX(iissDB)
ReadIISSBP(iissDB)
ReadIISSTX(iissDB)

//checkIISSTX(iissDB, lossDB)

Expand All @@ -34,6 +36,62 @@ func (cli *CLI) read(dbDir string, dbName string) {
iissDB.Close()
}

func ReadIISSBP(iissDB db.Database) {
var bpInfo core.IISSBlockProduceInfo

iter, _ := iissDB.GetIterator()
prefix := util.BytesPrefix([]byte(db.PrefixIISSBPInfo))
iter.New(prefix.Start, prefix.Limit)
var entries, startBH, miss uint64
for entries = 0; iter.Next(); entries++ {
err := bpInfo.SetBytes(iter.Value())
if err != nil {
log.Printf("Failed to load IISS Block Produce information.")
continue
}
bpInfo.BlockHeight = common.BytesToUint64(iter.Key()[len(db.PrefixIISSBPInfo):])
if entries == 0 {
startBH = bpInfo.BlockHeight
}
if startBH + entries + miss != bpInfo.BlockHeight {
fmt.Printf("Miss BP block height %d\n", startBH + entries + miss)
miss++
for ; startBH + entries + miss != bpInfo.BlockHeight; miss++ {
fmt.Printf("Miss BP block height %d\n", startBH + entries + miss)
}
}
}
log.Printf(">> BP total count %d, miss %d", entries, miss)
iter.Release()
err := iter.Error()
if err != nil {
log.Printf("There is error while read IISS BP iteration. %+v", err)
}
}

func ReadIISSTX(iissDB db.Database) {
var tx core.IISSTX

iter, _ := iissDB.GetIterator()
prefix := util.BytesPrefix([]byte(db.PrefixIISSTX))
iter.New(prefix.Start, prefix.Limit)
entries := 0
for entries = 0; iter.Next(); entries++ {
err := tx.SetBytes(iter.Value())
if err != nil {
log.Printf("Failed to load IISS TX data")
continue
}
//log.Printf("[IISSTX] TX : %s", tx.String())
}
log.Printf(">> TX total count %d", entries)
iter.Release()
err := iter.Error()
if err != nil {
log.Printf("There is error while read IISS TX iteration. %+v", err)
}
}

func checkIISSTX(iissDB db.Database, result db.Database) {
var tx core.IISSTX

Expand Down
72 changes: 44 additions & 28 deletions core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func (idb *IScoreDB) getCalculateResultDB() db.Database {
return idb.calcResult
}


func (idb *IScoreDB) resetAccountDB(blockHeight uint64) error {
idb.accountLock.Lock()
defer idb.accountLock.Unlock()
Expand Down Expand Up @@ -182,43 +181,60 @@ func (idb *IScoreDB) resetAccountDB(blockHeight uint64) error {
return nil
}

func (idb *IScoreDB) setCalculateBlockHeight(blockHeight uint64) {
idb.info.CalcBlockHeight = blockHeight
func (idb *IScoreDB) setCalculatingBH(blockHeight uint64) {
idb.info.Calculating = blockHeight

idb.writeToDB()
}

func (idb *IScoreDB) resetCalculateBlockHeight() {
idb.info.CalcBlockHeight = idb.info.BlockHeight
func (idb *IScoreDB) resetCalculatingBH() {
idb.info.Calculating = idb.info.CalcDone

idb.writeToDB()
}

func (idb *IScoreDB) isCalculating() bool {
return idb.info.CalcBlockHeight > idb.info.BlockHeight
return idb.getCalculatingBH() > idb.getCalcDoneBH()
}

func (idb *IScoreDB) setBlockInfo(blockHeight uint64, blockHash []byte) {
tempBlockHash := make([]byte, BlockHashSize)
copy(tempBlockHash, blockHash)
func (idb *IScoreDB) setCurrentBlockInfo(blockHeight uint64, blockHash []byte) {
idb.info.Current.set(blockHeight, blockHash)

// backup to prev Info.
if idb.info.PrevBlockHeight < blockHeight {
idb.info.PrevBlockHeight = idb.info.BlockHeight
copy(idb.info.PrevBlockHash, idb.info.BlockHash)
}
idb.writeToDB()
}

func (idb *IScoreDB) setCalcDoneBH(blockHeight uint64) {
idb.info.PrevCalcDone = idb.info.CalcDone
idb.info.CalcDone = blockHeight

idb.writeToDB()
}

func (idb *IScoreDB) getCurrentBlockInfo() *BlockInfo {
return &idb.info.Current
}

// set current Info.
idb.info.BlockHeight = blockHeight
copy(idb.info.BlockHash, tempBlockHash)
func (idb *IScoreDB) getCalcDoneBH() uint64 {
return idb.info.CalcDone
}

func (idb *IScoreDB) getPrevCalcDoneBH() uint64 {
return idb.info.PrevCalcDone
}

func (idb *IScoreDB) getCalculatingBH() uint64 {
return idb.info.Calculating
}

func (idb *IScoreDB) rollbackCurrentBlockInfo(blockHeight uint64, blockHash []byte) {
idb.setCurrentBlockInfo(blockHeight, blockHash)

idb.writeToDB()
}

func (idb *IScoreDB) rollbackBlockInfo() {
idb.info.BlockHeight = idb.info.PrevBlockHeight
copy(idb.info.BlockHash, idb.info.PrevBlockHash)
idb.setCalculateBlockHeight(idb.info.BlockHeight)
func (idb *IScoreDB) rollbackAccountDBBlockInfo() {
idb.info.CalcDone = idb.info.PrevCalcDone
idb.info.Calculating = idb.info.PrevCalcDone

idb.writeToDB()
}
Expand Down Expand Up @@ -286,11 +302,11 @@ func (idb *IScoreDB) rollbackAccountDB(blockHeight uint64) error {
// toggle query DB switch
idb.toggleAccountDB()

// Rollback block height and block hash
idb.rollbackBlockInfo()

// delete calculation result
DeleteCalculationResult(idb.getCalculateResultDB(), idb.info.CalcBlockHeight)
DeleteCalculationResult(idb.getCalculateResultDB(), idb.getCalcDoneBH())

// Rollback block height and block hash
idb.rollbackAccountDBBlockInfo()

log.Printf("End rollblack account DB to %d", blockHeight)
return nil
Expand Down Expand Up @@ -342,7 +358,7 @@ func (ctx *Context) UpdateGovernanceVariable(gvList []*IISSGovernanceVariable) {
deleteOld := false
deleteIndex := -1
for i := gvLen - 1; i >= 0 ; i-- {
if ctx.GV[i].BlockHeight < ctx.DB.info.BlockHeight {
if ctx.GV[i].BlockHeight < ctx.DB.getCalcDoneBH() {
if deleteOld {
// delete from management DB
bucket.Delete(ctx.GV[i].ID())
Expand Down Expand Up @@ -377,7 +393,7 @@ func (ctx *Context) UpdatePRep(prepList []*PRep) {
deleteOld := false
deleteIndex := -1
for i := prepLen - 1; i >= 0 ; i-- {
if ctx.PRep[i].BlockHeight < ctx.DB.info.BlockHeight {
if ctx.PRep[i].BlockHeight < ctx.DB.getCalcDoneBH() {
if deleteOld {
// delete from management DB
bucket.Delete(ctx.PRep[i].ID())
Expand Down Expand Up @@ -494,7 +510,7 @@ func NewContext(dbPath string, dbType string, dbName string, dbCount int) (*Cont
}

// read Governance variable
ctx.GV, err = LoadGovernanceVariable(mngDB, ctx.DB.info.BlockHeight)
ctx.GV, err = LoadGovernanceVariable(mngDB, ctx.DB.getCalcDoneBH())
if err != nil {
log.Printf("Failed to load GV structure\n")
return nil, err
Expand Down
72 changes: 44 additions & 28 deletions core/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func TestContext_NewContext(t *testing.T) {
assert.NotNil(t, ctx.DB)
assert.NotNil(t, ctx.DB.info)
assert.Equal(t, dbCount, ctx.DB.info.DBCount)
assert.Equal(t, uint64(0), ctx.DB.info.BlockHeight)
assert.True(t, ctx.DB.getCurrentBlockInfo().checkValue(uint64(0), zeroBlockHash))
assert.Equal(t, uint64(0), ctx.DB.getCalcDoneBH())
assert.Equal(t, uint64(0), ctx.DB.getPrevCalcDoneBH())
assert.Equal(t, uint64(0), ctx.DB.getCalculatingBH())
assert.False(t, ctx.DB.info.QueryDBIsZero)
assert.Equal(t, dbCount, len(ctx.DB.Account0))
assert.Equal(t, dbCount, len(ctx.DB.Account1))
Expand All @@ -64,7 +67,9 @@ func TestContext_UpdateGovernanceVariable(t *testing.T) {
bucket, _ := ctx.DB.management.GetBucket(db.PrefixGovernanceVariable)

// Set Block height
ctx.DB.info.BlockHeight = ctxBlockHeight
blockHash := make([]byte, BlockHashSize)
copy(blockHash, []byte(string(ctxBlockHeight)))
ctx.DB.setCalcDoneBH(ctxBlockHeight)

// Insert initial GV
gv := new(GovernanceVariable)
Expand Down Expand Up @@ -135,7 +140,9 @@ func TestContext_UpdatePRep(t *testing.T) {
bucket, _ := ctx.DB.management.GetBucket(db.PrefixPRep)

// Set Block height
ctx.DB.info.BlockHeight = ctxBlockHeight
blockHash := make([]byte, BlockHashSize)
copy(blockHash, []byte(string(ctxBlockHeight)))
ctx.DB.setCalcDoneBH(ctxBlockHeight)

// Insert initial PRep
pRep := new(PRep)
Expand Down Expand Up @@ -437,7 +444,7 @@ func TestContext_ResetAccountDB(t *testing.T) {
backupDB.Close()
}

func TestContext_rollbackBlockInfo(t *testing.T) {
func TestContext_CurrentBlockInfo(t *testing.T) {
ctx := initTest(1)
defer finalizeTest(ctx)

Expand All @@ -451,16 +458,32 @@ func TestContext_rollbackBlockInfo(t *testing.T) {
blockHash2 := make([]byte, BlockHashSize)
copy(blockHash2, []byte(string(blockHeight2)))

ctx.DB.setBlockInfo(blockHeight1, blockHash1)
ctx.DB.setBlockInfo(blockHeight2, blockHash2)
ctx.DB.setCurrentBlockInfo(blockHeight2, blockHash2)
assert.True(t, ctx.DB.getCurrentBlockInfo().checkValue(blockHeight2, blockHash2))

ctx.DB.rollbackBlockInfo()
ctx.DB.rollbackCurrentBlockInfo(blockHeight1, blockHash1)
assert.True(t, ctx.DB.getCurrentBlockInfo().checkValue(blockHeight1, blockHash1))
}

func TestContext_AccountDBBlockHeight(t *testing.T) {
ctx := initTest(1)
defer finalizeTest(ctx)

const (
blockHeight1 uint64 = 100
blockHeight2 uint64 = 200
)

ctx.DB.setCalcDoneBH(blockHeight1)
ctx.DB.setCalcDoneBH(blockHeight2)
assert.Equal(t, blockHeight2, ctx.DB.getCalcDoneBH())
assert.Equal(t, blockHeight1, ctx.DB.getPrevCalcDoneBH())

ctx.DB.rollbackAccountDBBlockInfo()

assert.Equal(t, blockHeight1, ctx.DB.info.BlockHeight)
assert.Equal(t, blockHash1, ctx.DB.info.BlockHash)
assert.Equal(t, blockHeight1, ctx.DB.info.PrevBlockHeight)
assert.Equal(t, blockHash1, ctx.DB.info.PrevBlockHash)
assert.Equal(t, blockHeight1, ctx.DB.info.CalcBlockHeight)
assert.Equal(t, blockHeight1, ctx.DB.getCalcDoneBH())
assert.Equal(t, blockHeight1, ctx.DB.getPrevCalcDoneBH())
assert.Equal(t, blockHeight1, ctx.DB.getCalculatingBH())
}

func TestContext_RollbackAccountDB(t *testing.T) {
Expand All @@ -482,11 +505,9 @@ func TestContext_RollbackAccountDB(t *testing.T) {

// emulate calculation process
prevBlockHeight := uint64(5)
prevBlockHash := testBlockHash0
ctx.DB.setBlockInfo(prevBlockHeight, prevBlockHash)
ctx.DB.setCalcDoneBH(prevBlockHeight)
blockHeight := uint64(10)
blockHash := testBlockHash
ctx.DB.setCalculateBlockHeight(blockHeight)
ctx.DB.setCalculatingBH(blockHeight)
ctx.DB.writeToDB()
ctx.DB.toggleAccountDB()

Expand All @@ -498,20 +519,18 @@ func TestContext_RollbackAccountDB(t *testing.T) {
err = ctx.DB.resetAccountDB(blockHeight)
assert.Nil(t, err)
WriteCalculationResult(crDB, blockHeight, nil, nil)
ctx.DB.setBlockInfo(blockHeight, blockHash)
ctx.DB.setCalcDoneBH(blockHeight)
ctx.DB.writeToDB()
assert.Equal(t, prevBlockHeight, ctx.DB.info.PrevBlockHeight)
assert.Equal(t, prevBlockHash, ctx.DB.info.PrevBlockHash)
assert.Equal(t, blockHeight, ctx.DB.info.BlockHeight)
assert.Equal(t, blockHash, ctx.DB.info.BlockHash)
assert.Equal(t, prevBlockHeight, ctx.DB.getPrevCalcDoneBH())
assert.Equal(t, blockHeight, ctx.DB.getCalcDoneBH())

// read from query DB
qDB = ctx.DB.getQueryDB(ia.Address)
bucket, _ = qDB.GetBucket(db.PrefixIScore)
bs, _ := bucket.Get(ia.ID())
assert.Nil(t, bs)

// no need to Rollback with blockHeigh >= ctx.DB.Info.CalcBlockHeight
// no need to Rollback with blockHeight >= ctx.DB.Info.CalcBlockHeight
err = ctx.DB.rollbackAccountDB(blockHeight)

// backup account DB remains
Expand All @@ -521,8 +540,7 @@ func TestContext_RollbackAccountDB(t *testing.T) {
assert.True(t, stat.IsDir())

// check block height and block hash
assert.Equal(t, blockHeight, ctx.DB.info.BlockHeight)
assert.Equal(t, blockHash, ctx.DB.info.BlockHash)
assert.Equal(t, blockHeight, ctx.DB.getCalcDoneBH())

// valid Rollback
err = ctx.DB.rollbackAccountDB(0)
Expand All @@ -539,10 +557,8 @@ func TestContext_RollbackAccountDB(t *testing.T) {
assert.Nil(t, bs)

// check Rollback block height and block hash
assert.Equal(t, prevBlockHeight, ctx.DB.info.PrevBlockHeight)
assert.Equal(t, prevBlockHash, ctx.DB.info.PrevBlockHash)
assert.Equal(t, prevBlockHeight, ctx.DB.info.BlockHeight)
assert.Equal(t, prevBlockHash, ctx.DB.info.BlockHash)
assert.Equal(t, prevBlockHeight, ctx.DB.getCalcDoneBH())
assert.Equal(t, prevBlockHeight, ctx.DB.getPrevCalcDoneBH())

// read from query DB
qDB = ctx.DB.getQueryDB(ia.Address)
Expand Down
8 changes: 7 additions & 1 deletion core/db_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,16 @@ func checkClaimDBRollback(cbInfo *ClaimBackupInfo, rollback uint64) (bool, error
return true, nil
}

func rollbackClaimDB(cDB db.Database, cbDB db.Database, to uint64) error {
func rollbackClaimDB(ctx *Context, to uint64, blockHash []byte) error {
log.Printf("Start Rollback claim DB to %d", to)
idb := ctx.DB
cDB := idb.getClaimDB()
cbDB := idb.getClaimBackupDB()
bucket, err := cbDB.GetBucket(db.PrefixManagement)
if err != nil {
return err
}

var cbInfo ClaimBackupInfo
bs, _ := bucket.Get(cbInfo.ID())
cbInfo.SetBytes(bs)
Expand Down Expand Up @@ -465,6 +469,8 @@ func rollbackClaimDB(cDB db.Database, cbDB db.Database, to uint64) error {
cbInfo.LastBlockHeight = to
bucket.Set(cbInfo.ID(), cbInfo.Bytes())

idb.rollbackCurrentBlockInfo(to, blockHash)

log.Printf("End Rollback claim DB from %d to %d", from, to)
return nil
}
Expand Down
Loading

0 comments on commit a19f67e

Please sign in to comment.