diff --git a/internal/handler/checkstatus.go b/internal/handler/checkstatus.go index 6976275..539ca46 100644 --- a/internal/handler/checkstatus.go +++ b/internal/handler/checkstatus.go @@ -1,18 +1,19 @@ package handler import ( + "time" + "github.com/b2network/b2committer/internal/schema" "github.com/b2network/b2committer/internal/svc" "github.com/b2network/b2committer/pkg/log" "github.com/pkg/errors" - "time" ) // CheckStatus check proposal vote status func CheckStatus(ctx *svc.ServiceContext) { for { var dbProposal schema.Proposal - err := ctx.DB.Where("status=?", schema.Voting_Status).Order("end_batch_num asc").First(&dbProposal).Error + err := ctx.DB.Where("status=?", schema.VotingStatus).Order("end_batch_num asc").First(&dbProposal).Error if err != nil { log.Errorf("[Handler.CheckStatus] find Voting proposal err: %s\n", errors.WithStack(err)) time.Sleep(5 * time.Second) @@ -23,7 +24,7 @@ func CheckStatus(ctx *svc.ServiceContext) { log.Errorf("[Handler.CheckStatus] QueryProposalByID err: %s\n", errors.WithStack(err)) continue } - if proposal.Status != schema.Voting_Status && proposal.Winner != "" { + if proposal.Status != schema.VotingStatus && proposal.Winner != "" { dbProposal.Winner = proposal.Winner dbProposal.Status = proposal.Status ctx.DB.Save(dbProposal) @@ -35,7 +36,7 @@ func CheckStatus(ctx *svc.ServiceContext) { func CheckStatusTimeOut(ctx *svc.ServiceContext) { for { var dbProposal schema.Proposal - err := ctx.DB.Where("status!=?", schema.Succeed_Status).Order("end_batch_num asc").First(&dbProposal).Error + err := ctx.DB.Where("status!=?", schema.SucceedStatus).Order("end_batch_num asc").First(&dbProposal).Error if err != nil { log.Errorf("[Handler.CheckStatusTimeOut] find Voting proposal err: %s\n", errors.WithStack(err)) time.Sleep(5 * time.Second) diff --git a/internal/handler/committer.go b/internal/handler/committer.go index f73d89e..acb9141 100644 --- a/internal/handler/committer.go +++ b/internal/handler/committer.go @@ -100,7 +100,7 @@ func committerProposal(ctx *svc.ServiceContext, verifyBatchInfo *VerifyRangBatch }, EndBatchNum: verifyBatchInfo.endBatchNum, ProposalID: proposalID, - Status: schema.Voting_Status, + Status: schema.VotingStatus, StateRootHash: verifyBatchInfo.stateRootHash, ProofRootHash: verifyBatchInfo.proofRootHash, StartBatchNum: verifyBatchInfo.startBatchNum, diff --git a/internal/handler/inscribe.go b/internal/handler/inscribe.go index c9c7ad7..482c5b0 100644 --- a/internal/handler/inscribe.go +++ b/internal/handler/inscribe.go @@ -2,9 +2,10 @@ package handler import ( "encoding/json" - "github.com/b2network/b2committer/pkg/inscribe" "time" + "github.com/b2network/b2committer/pkg/inscribe" + "github.com/b2network/b2committer/pkg/log" "github.com/b2network/b2committer/internal/schema" @@ -17,7 +18,7 @@ import ( func Inscribe(ctx *svc.ServiceContext) { for { var dbProposal schema.Proposal - err := ctx.DB.Where("status=?", schema.Pending_Status).Order("end_batch_num asc").First(&dbProposal).Error + err := ctx.DB.Where("status=?", schema.PendingStatus).Order("end_batch_num asc").First(&dbProposal).Error if err != nil { log.Errorf("[Handler.Inscribe] Pending and timeout proposal err: %s\n", errors.WithStack(err)) time.Sleep(5 * time.Second) @@ -28,13 +29,13 @@ func Inscribe(ctx *svc.ServiceContext) { log.Errorf("[CheckProposalPending] QueryProposalByID err: %s\n", errors.WithStack(err)) continue } - if proposal.Status == schema.Succeed_Status { + if proposal.Status == schema.SucceedStatus { dbProposal.Status = proposal.Status dbProposal.Winner = proposal.Winner dbProposal.BitcoinRevealTxHash = proposal.BitcoinTxHash ctx.DB.Save(dbProposal) } - if proposal.Status == schema.Pending_Status && + if proposal.Status == schema.PendingStatus && proposal.Winner == ctx.B2NodeConfig.Address && proposal.BitcoinTxHash == "" { rs, err := inscribe.Inscribe(ctx.BTCConfig.PrivateKey, proposal.StateRootHash, proposal.ProofHash, ctx.BTCConfig.DestinationAddress, ChainParams(ctx.BTCConfig.NetworkName)) if err != nil { @@ -58,9 +59,8 @@ func Inscribe(ctx *svc.ServiceContext) { dbProposal.BitcoinCommitTxHash = rs.CommitTxHash.String() ctx.DB.Save(dbProposal) - } - if proposal.Status == schema.Pending_Status && proposal.BitcoinTxHash != "" { + if proposal.Status == schema.PendingStatus && proposal.BitcoinTxHash != "" { _, err = ctx.NodeClient.BitcoinTx(proposal.Id, proposal.Proposer, proposal.BitcoinTxHash) if err != nil { log.Errorf("[Handler.Inscribe] BitcoinTx err: %s\n", errors.WithStack(err)) @@ -70,13 +70,6 @@ func Inscribe(ctx *svc.ServiceContext) { } } -// sendBTCTxHashToB2Node step7 -func sendBTCTxHashToB2Node(txHash string) { - // TODO send revealTxHash to b2node - // update local proposal btcTxHash - println(txHash) -} - func ChainParams(network string) *chaincfg.Params { switch network { case chaincfg.MainNetParams.Name: diff --git a/internal/schema/proposal.go b/internal/schema/proposal.go index c655427..9b50963 100644 --- a/internal/schema/proposal.go +++ b/internal/schema/proposal.go @@ -1,10 +1,10 @@ package schema const ( - Voting_Status = 0 - Pending_Status = 1 - Succeed_Status = 2 - Timeout_Status = 3 + VotingStatus = 0 + PendingStatus = 1 + SucceedStatus = 2 + TimeoutStatus = 3 ) type Proposal struct { diff --git a/internal/svc/svc.go b/internal/svc/svc.go index 5c3b250..e9841ca 100644 --- a/internal/svc/svc.go +++ b/internal/svc/svc.go @@ -1,9 +1,10 @@ package svc import ( - "github.com/b2network/b2committer/pkg/b2node" "time" + "github.com/b2network/b2committer/pkg/b2node" + "github.com/b2network/b2committer/pkg/log" "github.com/b2network/b2committer/internal/types" diff --git a/pkg/b2node/b2node.go b/pkg/b2node/b2node.go index 39098c8..ef92fac 100644 --- a/pkg/b2node/b2node.go +++ b/pkg/b2node/b2node.go @@ -61,7 +61,8 @@ func (n NodeClient) GetAccountInfo(address string) (*eTypes.EthAccount, error) { } func (n NodeClient) SubmitProof(id uint64, from string, proofHash string, stateRootHash string, - startIndex uint64, endIndex uint64) (uint64, error) { + startIndex uint64, endIndex uint64, +) (uint64, error) { msg := committerTypes.NewMsgSubmitProof(id, from, proofHash, stateRootHash, startIndex, endIndex) msgResponse, err := n.broadcastTx(msg) if err != nil { @@ -72,7 +73,7 @@ func (n NodeClient) SubmitProof(id uint64, from string, proofHash string, stateR if err != nil { return 0, fmt.Errorf("[SubmitProof][hex.DecodeString] err: %s", err) } - var pbMsg = &sdk.TxMsgData{} + pbMsg := &sdk.TxMsgData{} err = pbMsg.Unmarshal(byteData) if err != nil { return 0, fmt.Errorf("[SubmitProof][pbMsg.Unmarshal] err: %s", err) @@ -97,7 +98,7 @@ func (n NodeClient) BitcoinTx(proposalID uint64, from string, bitcoinTxHash stri if err != nil { return 0, fmt.Errorf("[BitcoinTx][hex.DecodeString] err: %s", err) } - var pbMsg = &sdk.TxMsgData{} + pbMsg := &sdk.TxMsgData{} err = pbMsg.Unmarshal(byteData) if err != nil { return 0, fmt.Errorf("[BitcoinTx][pbMsg.Unmarshal] err: %s", err)