Skip to content

Commit

Permalink
fix procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Apr 10, 2024
1 parent a4b2af3 commit 0eee83d
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 239 deletions.
77 changes: 0 additions & 77 deletions internal/handler/arweave.go

This file was deleted.

104 changes: 14 additions & 90 deletions internal/handler/checkstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,107 +9,31 @@ import (
"github.com/pkg/errors"
)

// CheckStatusVoting check proposal vote status
func CheckStatusVoting(ctx *svc.ServiceContext) {
// CheckStatusByContract sync check proposal status
func CheckStatusByContract(ctx *svc.ServiceContext) {
for {
var dbProposal schema.Proposal
err := ctx.DB.Where("status=?", schema.ProposalVotingStatus).Order("end_batch_num asc").First(&dbProposal).Error
proposal, err := ctx.NodeClient.QueryLastProposal()
if err != nil {
log.Errorf("[Handler.CheckStatus] find Voting proposal err: %s\n", errors.WithStack(err).Error())
time.Sleep(5 * time.Second)
log.Errorf("[Handler.CheckStatusByContract][QueryLastProposalID] error info: %s", errors.WithStack(err).Error())
continue
}
proposal, err := ctx.NodeClient.QueryProposalByID(dbProposal.ProposalID)
proposalContract, err := ctx.NodeClient.QueryProposalByID(proposal.Id)
if err != nil {
log.Errorf("[Handler.CheckStatus] QueryProposalByID err: %s\n", errors.WithStack(err).Error())
log.Errorf("[Handler.CheckStatusByContract][QueryProposalByID] error info: %s", errors.WithStack(err).Error())
continue
}
if proposal.Status != schema.ProposalVotingStatus && proposal.Winner.String() != "" {
dbProposal.Winner = proposal.Winner.String()
dbProposal.Status = uint64(proposal.Status)
ctx.DB.Save(dbProposal)
}
time.Sleep(3 * time.Second)
}
}

func CheckStatusPending(ctx *svc.ServiceContext) {
for {
var dbProposal schema.Proposal
err := ctx.DB.Where("status = ?", schema.ProposalPendingStatus).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)
continue
}
proposal, err := ctx.NodeClient.QueryProposalByID(dbProposal.ProposalID)
err = ctx.DB.Where("proposal_id=?", proposal.Id).First(&dbProposal).Error
if err != nil {
log.Errorf("[Handler.CheckStatusTimeOut] QueryProposalByID err: %s\n", errors.WithStack(err))
log.Errorf("[Handler.CheckStatusByContract] find proposal err: %s\n", errors.WithStack(err).Error())
continue
}
if proposal.Status == schema.ProposalCommitting {
dbProposal.Status = uint64(proposal.Status)
dbProposal.Winner = proposal.Winner.String()
dbProposal.BtcTxHash = proposal.BtcTxHash
ctx.DB.Save(dbProposal)
continue
}
time.Sleep(2 * time.Second)
dbProposal.BtcTxHash = proposalContract.BtcTxHash
dbProposal.ArTxHash = proposalContract.ArweaveTxHash
dbProposal.Winner = proposalContract.Winner.String()
dbProposal.Status = uint64(proposalContract.Status)
ctx.DB.Save(&dbProposal)
time.Sleep(20 * time.Second)
}
}

Check failure on line 38 in internal/handler/checkstatus.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
func CheckStatusCommitting(ctx *svc.ServiceContext) {
for {
var dbProposal schema.Proposal
err := ctx.DB.Where("status = ?", schema.ProposalCommitting).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)
continue
}
proposal, err := ctx.NodeClient.QueryProposalByID(dbProposal.ProposalID)
if err != nil {
log.Errorf("[Handler.CheckStatusTimeOut] QueryProposalByID err: %s\n", errors.WithStack(err))
continue
}
if proposal.Status == schema.ProposalSucceedStatus {
dbProposal.Status = uint64(proposal.Status)
dbProposal.Winner = proposal.Winner.String()
dbProposal.ArTxHash = proposal.ArweaveTxHash
ctx.DB.Save(dbProposal)
continue
}
time.Sleep(2 * time.Second)
}
}

func CheckStatusPendingTimeOut(ctx *svc.ServiceContext) {
for {
time.Sleep(30 * time.Second)
var dbProposal schema.Proposal
err := ctx.DB.Where("status = ? or status = ?", schema.ProposalPendingStatus,
schema.ProposalCommitting).Order("end_batch_num asc").First(&dbProposal).Error
if err != nil {
log.Errorf("[Handler.CheckStatusTimeOut] find Voting proposal and Committing proposal err: %s\n", errors.WithStack(err).Error())
time.Sleep(5 * time.Second)
continue
}
proposal, err := ctx.NodeClient.QueryProposalByID(dbProposal.ProposalID)
if err != nil {
log.Errorf("[Handler.CheckStatusTimeOut] QueryProposalByID err: %s\n", errors.WithStack(err))
continue
}
if (proposal.BtcTxHash == "" || proposal.ArweaveTxHash == "") && proposal.Winner.String() != ctx.B2NodeConfig.Address {
res, err := ctx.NodeClient.IsProposalTimeout(proposal.Id)
if err != nil {
log.Errorf("[Handler.CheckStatusTimeOut] TimeoutProposal err: %s\n", errors.WithStack(err))
continue
}
if res {
dbProposal.Status = schema.ProposalTimeoutStatus
ctx.DB.Save(dbProposal)
}
time.Sleep(2 * time.Second)
}
}
}
38 changes: 27 additions & 11 deletions internal/handler/committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,59 @@ func Committer(ctx *svc.ServiceContext) {
time.Sleep(10 * time.Second)
continue
}
if lastProposalID == 0 {
lastProposalID = 1
}

if proposal.Status == schema.ProposalSucceedStatus {
lastProposalID++
proposal, err = ctx.NodeClient.QueryProposalByID(lastProposalID)
if err != nil {
log.Errorf("[Handler.Committer][QueryProposalByID] error info: %s", errors.WithStack(err).Error())
time.Sleep(10 * time.Second)
continue
}
}

proposalLatest, err := ctx.NodeClient.QueryProposalByID(lastProposalID)
res, err := ctx.NodeClient.CheckProposalTimeout(lastProposalID)
if err != nil {
log.Errorf("[Handler.Committer][QueryLastProposalID] error info: %s", errors.WithStack(err).Error())
return
log.Errorf("[Handler.Committer][CheckProposalTimeout] error info: %s", errors.WithStack(err).Error())
continue
}

if proposalLatest.Status != schema.ProposalTimeoutStatus {
log.Infof("[Handler.Committer] proposal status is processing, proposalID: %d, proposal status: %d", lastProposalID, proposalLatest.Status)
if !res && proposal.Status != schema.ProposalVotingStatus {
log.Infof("[Handler.Committer] proposal status is processing, proposalID: %d", lastProposalID)
time.Sleep(10 * time.Second)
continue
}
if res && lastProposalID == 1 {
lastFinalBatchNum = 0
}

verifyBatchInfo, err := GetVerifyBatchInfoByLastBatchNum(ctx, lastFinalBatchNum)
if err != nil {
log.Errorf("[Handler.Committer] error info: %s", errors.WithStack(err).Error())
time.Sleep(10 * time.Second)
continue
}

err = committerProposal(ctx, verifyBatchInfo, lastProposalID)
if err != nil {
log.Errorf("[Handler.Committer] error info: %s", errors.WithStack(err).Error())
time.Sleep(10 * time.Second)
continue
}
time.Sleep(3 * time.Second)
time.Sleep(30 * time.Second)
}
}

func GetVerifyBatchInfoByLastBatchNum(ctx *svc.ServiceContext, lastFinalBatchNum uint64) (*VerifyRangBatchInfo, error) {
verifyBatchesAndTxHashs, err := GetVerifyBatchesFromStartBatchNum(ctx, lastFinalBatchNum, ctx.Config.LimitNum)
if err != nil || len(verifyBatchesAndTxHashs) != ctx.Config.LimitNum {
verifyBatchesAndTxHashes, err := GetVerifyBatchesFromStartBatchNum(ctx, lastFinalBatchNum, ctx.Config.LimitNum)
if err != nil || len(verifyBatchesAndTxHashes) != ctx.Config.LimitNum {
return nil, fmt.Errorf("[GetVerifyBatchInfoByLastBatchNum] error info: %s", errors.WithStack(err))
}
verifyBatchesParams := make([]*VerifyBatchesTrustedAggregatorParams, 0, ctx.Config.LimitNum)
for _, verifyBatch := range verifyBatchesAndTxHashs {
for _, verifyBatch := range verifyBatchesAndTxHashes {
verifyBatchParam, err := GetVerifyBatchesParamsByTxHash(ctx, common.HexToHash(verifyBatch.txHash))
if err != nil {
return nil, fmt.Errorf("[GetVerifyBatchInfoByLastBatchNum] error info: %s", errors.WithStack(err))
Expand All @@ -102,8 +119,7 @@ func GetVerifyBatchInfoByLastBatchNum(ctx *svc.ServiceContext, lastFinalBatchNum

// CommitterProposal committer transaction to b2-node
func committerProposal(ctx *svc.ServiceContext, verifyBatchInfo *VerifyRangBatchInfo, lastProposalID uint64) error {
proposalID := lastProposalID + 1
_, err := ctx.NodeClient.SubmitProof(proposalID, verifyBatchInfo.proofRootHash, verifyBatchInfo.stateRootHash,
_, err := ctx.NodeClient.SubmitProof(lastProposalID, verifyBatchInfo.proofRootHash, verifyBatchInfo.stateRootHash,
verifyBatchInfo.startBatchNum, verifyBatchInfo.endBatchNum)
if err != nil {
return fmt.Errorf("[committerProposal] submit proof error info: %s, %d", errors.WithStack(err), verifyBatchInfo.startBatchNum)
Expand Down
18 changes: 6 additions & 12 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@ import (
func Run(ctx *svc.ServiceContext) {

// query last block number
go LatestBlackNumber(ctx)
// sync blocks
go SyncBlock(ctx)
// sync events
go SyncEvent(ctx)
//go LatestBlackNumber(ctx)

Check failure on line 10 in internal/handler/handler.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
//// sync blocks
//go SyncBlock(ctx)
//// sync events
//go SyncEvent(ctx)
// execute committer
go Committer(ctx)
// check and inscribe
go Inscribe(ctx)
// check status
go CheckStatusVoting(ctx)
// check pending
go CheckStatusPending(ctx)
// check time out
go CheckStatusPendingTimeOut(ctx)
go CheckStatusByContract(ctx)
// sync proposal
go SyncProposal(ctx)
// sequence batches
go SequenceBatches(ctx)
// upload batch detail to ar
go BatchDetailsToAr(ctx)
}
Loading

0 comments on commit 0eee83d

Please sign in to comment.