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

Commit

Permalink
Add START_BLOCK message (#53)
Browse files Browse the repository at this point in the history
- flush preCommit data when receive START_BLOCK message
- if preCommit data exists when receiving CLAIM message, reply 0 IScore
  • Loading branch information
Eunsoo Park authored Jan 7, 2021
1 parent 67dfae0 commit d9f5b7b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 9 deletions.
15 changes: 15 additions & 0 deletions cmd/sendipc/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (cli *CLI) Run() {
commitClaimTXIndex := commitClaimCmd.Uint64("txindex", 0, "TX index")
commitClaimTXHash := commitClaimCmd.String("txhash", "", "TX hash")

startBlockCmd := flag.NewFlagSet("startblock", flag.ExitOnError)
startBlockBlockHeight := startBlockCmd.Uint64("blockheight", 0, "Block height")
startBlockBlockHash := startBlockCmd.String("blockhash", "", "Block hash")

commitBlockCmd := flag.NewFlagSet("commitblock", flag.ExitOnError)
commitBlockFail := commitBlockCmd.Bool("fail", true, "Success")
commitBlockBlockHeight := commitBlockCmd.Uint64("blockheight", 0, "Block height")
Expand Down Expand Up @@ -133,6 +137,12 @@ func (cli *CLI) Run() {
commitClaimCmd.PrintDefaults()
os.Exit(1)
}
case "startblock":
err := startBlockCmd.Parse(os.Args[3:])
if err != nil {
startBlockCmd.PrintDefaults()
os.Exit(1)
}
case "commitblock":
err := commitBlockCmd.Parse(os.Args[3:])
if err != nil {
Expand Down Expand Up @@ -224,6 +234,11 @@ func (cli *CLI) Run() {
*commitClaimTXIndex, *commitClaimTXHash)
}

if startBlockCmd.Parsed() {
// send START_BLOCK message
cli.startBlock(conn, *startBlockBlockHeight, *startBlockBlockHash)
}

if commitBlockCmd.Parsed() {
// send COMMIT_BLOCK message
cli.commitBlock(conn, *commitBlockFail, *commitBlockBlockHeight, *commitBlockBlockHash)
Expand Down
21 changes: 21 additions & 0 deletions cmd/sendipc/cli_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ func (cli *CLI) commitClaim(conn ipc.Connection, success bool, address string, b
fmt.Printf("Get COMMIT_CLAIM ack\n")
}

func (cli *CLI) startBlock(conn ipc.Connection, blockHeight uint64, blockHash string) {
var req core.StartBlock
var resp core.StartBlock
req.BlockHash = make([]byte, core.BlockHashSize)
if len(blockHash) == 0 {
binary.BigEndian.PutUint64(req.BlockHash, blockHeight)
} else {
bh, err := hex.DecodeString(blockHash)
if err != nil {
fmt.Printf("Failed to START_BLOCK. Invalid block hash. %v\n", err)
return
}
copy(req.BlockHash, bh)
}
req.BlockHeight = blockHeight

fmt.Printf("Send START_BLOCK message: %s\n", req.String())
conn.SendAndReceive(core.MsgStartBlock, cli.id, &req, &resp)
fmt.Printf("Get START_BLOCK response: %s\n", resp.String())
}

func (cli *CLI) commitBlock(conn ipc.Connection, success bool, blockHeight uint64, blockHash string) {
var req core.CommitBlock
var resp core.CommitBlock
Expand Down
25 changes: 25 additions & 0 deletions core/api_ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ func (rc *RCIPC) SendCalculate(iissData string, blockHeight uint64) (*CalculateR
return resp, nil
}

func (rc *RCIPC) SendStartBlock(success bool, blockHeight uint64, blockHash string) (*StartBlock, error) {
var req StartBlock
resp := new(StartBlock)

req.BlockHash = make([]byte, BlockHashSize)
if len(blockHash) == 0 {
binary.BigEndian.PutUint64(req.BlockHash, blockHeight)
} else {
bh, err := hex.DecodeString(blockHash)
if err != nil {
log.Printf("Failed to START_BLOCK. Invalid block hash. %v\n", err)
return resp, err
}
copy(req.BlockHash, bh)
}
req.BlockHeight = blockHeight

log.Printf("Send START_BLOCK message: %s\n", req.String())
rc.id++
err := rc.conn.SendAndReceive(MsgStartBlock, rc.id, &req, &resp)
log.Printf("Get START_BLOCK response: %s\n", resp.String())

return resp, err
}

func (rc *RCIPC) SendCommitBlock(success bool, blockHeight uint64, blockHash string) (*CommitBlock, error) {
var req CommitBlock
resp := new(CommitBlock)
Expand Down
6 changes: 6 additions & 0 deletions core/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
MsgQueryCalculateResult = 7
MsgRollBack = 8
MsgINIT = 9
MsgStartBlock = 10

MsgNotify = 100
MsgReady = MsgNotify + 0
Expand Down Expand Up @@ -61,6 +62,8 @@ func MsgToString(msg uint) string {
return "ROLLBACK"
case MsgINIT:
return "INIT"
case MsgStartBlock:
return "START_BLOCK"
case MsgDebug:
return "DEBUG"
default:
Expand Down Expand Up @@ -96,6 +99,7 @@ func newConnection(m *manager, c ipc.Connection) (*msgHandler, error) {
} else {
c.SetHandler(MsgClaim, handler)
c.SetHandler(MsgCalculate, handler)
c.SetHandler(MsgStartBlock, handler)
c.SetHandler(MsgCommitBlock, handler)
c.SetHandler(MsgCommitClaim, handler)
c.SetHandler(MsgRollBack, handler)
Expand Down Expand Up @@ -125,6 +129,8 @@ func (mh *msgHandler) HandleMessage(c ipc.Connection, msg uint, id uint32, data
go mh.query(c, id, data)
case MsgCalculate:
go mh.calculate(c, id, data)
case MsgStartBlock:
go mh.startBlock(c, id, data)
case MsgCommitBlock:
go mh.commitBlock(c, id, data)
case MsgDebug:
Expand Down
43 changes: 36 additions & 7 deletions core/msg_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ func DoClaim(ctx *Context, req *ClaimMessage) (uint64, *common.HexInt) {
pcDB := ctx.DB.getPreCommitDB()
preCommit := newPreCommit(req.BlockHeight, req.BlockHash, req.TXIndex, req.TXHash, req.Address)
if preCommit.query(pcDB) == true {
if preCommit.Confirmed && req.TXIndex != preCommit.TXIndex {
// already claimed in current block
return preCommit.BlockHeight, nil
} else {
// re-invoke same TX in same Block
return preCommit.BlockHeight, &preCommit.Data.IScore
}
// already claimed in current block
return preCommit.BlockHeight, nil
}

var claim *Claim = nil
Expand Down Expand Up @@ -206,6 +201,40 @@ func DoCommitClaim(ctx *Context, req *CommitClaim) error {
return nil
}

type StartBlock struct {
BlockHeight uint64
BlockHash []byte
}

func (sb *StartBlock) String() string {
return fmt.Sprintf("BlockHeight: %d, BlockHash: %s",
sb.BlockHeight,
hex.EncodeToString(sb.BlockHash))
}

func (mh *msgHandler) startBlock(c ipc.Connection, id uint32, data []byte) error {
var req StartBlock
var err error
mh.mgr.AddMsgTask()
if _, err = codec.MP.UnmarshalFromBytes(data, &req); nil != err {
return err
}
log.Printf("\t START_BLOCK request: %s", req.String())

iDB := mh.mgr.ctx.DB
err = flushPreCommit(iDB.getPreCommitDB(), req.BlockHeight, req.BlockHash)
if err != nil {
log.Printf("Failed to start block. %+v", err)
}

var resp StartBlock
resp = req

mh.mgr.DoneMsgTask()
log.Printf("Send message. (msg:%s, id:%d, data:%s)", MsgToString(MsgStartBlock), id, resp.String())
return c.Send(MsgStartBlock, id, &resp)
}

type CommitBlock struct {
Success bool
BlockHeight uint64
Expand Down
4 changes: 2 additions & 2 deletions core/msg_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func TestMsgClaim_DoClaim(t *testing.T) {
CommitClaim{Success:true, Address: claim.Address, BlockHeight:claim.BlockHeight, BlockHash:claim.BlockHash}
DoCommitClaim(ctx, &commit)

// re-invoke same claim TX
// claim again in same TX
blockHeight, iScore = DoClaim(ctx, &claim)
assert.Equal(t, claim.BlockHeight, blockHeight)
assert.Equal(t, uint64(db1IScore - (db1IScore % claimMinIScore)), iScore.Uint64())
assert.Nil(t, iScore)

// already claimed in current block
blockHeight, iScore = DoClaim(ctx, &alreadyClaimedInCurrentBlockClaim)
Expand Down

0 comments on commit d9f5b7b

Please sign in to comment.