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

Commit

Permalink
Merge pull request #33 from icon-project/IS-873-add-rollback-function
Browse files Browse the repository at this point in the history
IS-873: add rollback function
  • Loading branch information
eunsoo-icon authored Nov 22, 2019
2 parents 428067f + 8305d2e commit 83d265b
Show file tree
Hide file tree
Showing 28 changed files with 1,897 additions and 373 deletions.
2 changes: 1 addition & 1 deletion cmd/dbtest/cli_calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func (cli *CLI) calculate(dbName string, blockHeight uint64, batchCount uint64)
req.BlockHeight = blockHeight
req.Path = "noiissdata"

core.DoCalculate(ctx, &req, nil, 0)
core.DoCalculate(ctx.Rollback.GetChannel(), ctx, &req, nil, 0)
}
2 changes: 1 addition & 1 deletion cmd/icon_rc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func main() {

rcm, err := core.InitManager(&cfg)
if err != nil {
log.Panicf("Failed to start RewardCalculator manager %+v", err)
log.Panicf("Failed to start RewardCalculator manager. %+v", err)
}

forever := make(chan bool)
Expand Down
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
18 changes: 18 additions & 0 deletions cmd/sendipc/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (cli *CLI) Run() {
queryCRCmd := flag.NewFlagSet("query_calculate_result", flag.ExitOnError)
queryCRBlockHeight := queryCRCmd.Uint64("blockheight", 0, "Block height(Required)")

rollbackCmd := flag.NewFlagSet("rollback", flag.ExitOnError)
rollbackBlockHeight := rollbackCmd.Uint64("blockheight", 0, "Rollback block height(Required)")
rollbackBlockHash := rollbackCmd.String("blockhash", "", "Rollback block hash(Required)")

// Parse the CLI
switch cmd {
case "version":
Expand Down Expand Up @@ -116,6 +120,12 @@ func (cli *CLI) Run() {
queryCRCmd.PrintDefaults()
os.Exit(1)
}
case "rollback":
err := rollbackCmd.Parse(os.Args[3:])
if err != nil {
rollbackCmd.PrintDefaults()
os.Exit(1)
}
default:
cli.printUsage()
os.Exit(1)
Expand Down Expand Up @@ -202,4 +212,12 @@ func (cli *CLI) Run() {
if monitorCmd.Parsed() {
cli.monitor(conn, *monitorConfig, *monitorURL)
}

if rollbackCmd.Parsed() {
if *rollbackBlockHeight == 0 || *rollbackBlockHash == "" {
rollbackCmd.PrintDefaults()
os.Exit(1)
}
cli.rollback(conn, *rollbackBlockHeight, *rollbackBlockHash)
}
}
12 changes: 7 additions & 5 deletions cmd/sendipc/cli_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ func (cli *CLI) claim(conn ipc.Connection, address string, blockHeight uint64) {
// Send CLAIM and get response
req.Address.SetString(address)
req.BlockHeight = blockHeight
req.BlockHash = make([]byte, 8)
req.BlockHash = make([]byte, core.BlockHashSize)
binary.BigEndian.PutUint64(req.BlockHash, blockHeight)

fmt.Printf("send CLAIM message: %s\n", req.String())
conn.SendAndReceive(core.MsgClaim, cli.id, &req, &resp)
cli.id++
fmt.Printf("CLAIM command get response: %s\n", resp.String())
fmt.Printf("CLAIM message get response: %s\n", resp.String())

// send COMMIT_CLAIM and get ack
var commitClaim core.CommitClaim
commitClaim.Success = true
commitClaim.Address = req.Address
commitClaim.BlockHeight = req.BlockHeight
commitClaim.BlockHash = req.BlockHash
commitClaim.Success = true
commitClaim.BlockHash = make([]byte, core.BlockHashSize)
copy(commitClaim.BlockHash, req.BlockHash)

fmt.Printf("Send COMMIT_CLAIM message: %s\n", commitClaim.String())
conn.SendAndReceive(core.MsgClaim, cli.id, &req, &resp)
conn.SendAndReceive(core.MsgCommitClaim, cli.id, &commitClaim, &resp)
cli.id++
fmt.Printf("COMMIT_CLAIM message get ack\n")

Expand Down
27 changes: 27 additions & 0 deletions cmd/sendipc/cli_rollback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"encoding/hex"
"fmt"

"github.com/icon-project/rewardcalculator/common/ipc"
"github.com/icon-project/rewardcalculator/core"
)

func (cli *CLI) rollback(conn ipc.Connection, blockHeight uint64, blockHash string) {
var req core.RollBackRequest
var resp core.RollBackResponse

hash, err := hex.DecodeString(blockHash)
if err != nil {
fmt.Printf("Failed to decode blockHash. %v\n", err)
return
}

req.BlockHeight = blockHeight
req.BlockHash = make([]byte, core.BlockHashSize)
copy(req.BlockHash, hash[0:core.BlockHashSize])

conn.SendAndReceive(core.MsgRollBack, cli.id, &req, &resp)
fmt.Printf("ROLLBACK command get response: %s\n", resp.String())
}
Loading

0 comments on commit 83d265b

Please sign in to comment.