Skip to content

Commit

Permalink
feat: Fix checking for mint subaccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpiotzh committed Mar 21, 2023
1 parent 2475d5e commit 276c30c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 88 deletions.
8 changes: 8 additions & 0 deletions dao/t_task_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ func (d *DbDao) UpdateTaskStatusToRollback(ids []uint64) error {
}).Error
}

func (d *DbDao) UpdateTaskStatusToRollbackWithBalanceErr(taskId string) error {
return d.db.Model(tables.TableTaskInfo{}).
Where("task_id=? AND smt_status=? AND tx_status=?", taskId, tables.SmtStatusNeedToWrite, tables.TxStatusUnSend).
Updates(map[string]interface{}{
"smt_status": tables.SmtStatusNeedToRollback,
}).Error
}

func (d *DbDao) GetTaskInfoByParentAccountIdWithAction(parentAccountId string, action common.DasAction) (task tables.TableTaskInfo, err error) {
taskType := []tables.TaskType{tables.TaskTypeNormal, tables.TaskTypeChain}
err = d.db.Where("parent_account_id=? AND action=? AND task_type IN(?)",
Expand Down
6 changes: 5 additions & 1 deletion http_server/handle/sub_account_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ func (h *HttpHandle) doSubAccountCheckList(req *ReqSubAccountCreate, apiResp *ap
}

accLen := len(req.SubAccountList[i].AccountCharStr)
if uint32(accLen) > maxLength {
if accLen <= 0 {
tmp.Status = CheckStatusFail
tmp.Message = fmt.Sprintf("account length is 0")
isOk = false
} else if uint32(accLen) > maxLength {
tmp.Status = CheckStatusFail
tmp.Message = fmt.Sprintf("account len more than: %d", maxLength)
isOk = false
Expand Down
175 changes: 88 additions & 87 deletions task/update_sub_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,94 +143,95 @@ func (t *SmtTask) doUpdateSubAccountTaskDetail(action common.DasAction, parentAc
return nil
}

// deprecated
func (t *SmtTask) doUpdateSubAccountTask(action common.DasAction) error {
list, err := t.DbDao.GetNeedToDoTaskListByAction(config.Cfg.Slb.SvrName, action)
if err != nil {
return fmt.Errorf("GetNeedToDoTaskListByAction err: %s [%s]", err.Error(), action)
}

// group task list by ParentAccountId
mapTaskList, mapTaskIdList := t.groupByParentAccountIdNew(list)
// do task
for parentAccountId, taskList := range mapTaskList {
taskIdList, _ := mapTaskIdList[parentAccountId]

// check need roll back
var needRollbackIds []uint64
for _, v := range taskList {
if v.SmtStatus != tables.SmtStatusNeedToWrite {
needRollbackIds = append(needRollbackIds, v.Id)
}
}
if len(needRollbackIds) > 0 {
if err := t.DbDao.UpdateTaskStatusToRollback(needRollbackIds); err != nil {
return fmt.Errorf("UpdateTaskStatusToRollback err: %s", err.Error())
}
continue
}

if _, ok := config.Cfg.SuspendMap[parentAccountId]; ok {
log.Warn("SuspendMap:", parentAccountId)
continue
}

// get smt records
taskMap, subAccountIds, err := t.getTaskMap(taskIdList)
if err != nil {
return fmt.Errorf("getTaskMap err: %s", err.Error())
}

// check nonce
hasDiffNonce, err := t.doCheckNonceNew(taskMap, subAccountIds)
if err != nil {
return fmt.Errorf("doCheckNonce err: %s", err.Error())
} else if hasDiffNonce {
log.Warn("doCheckNonce:", parentAccountId)
continue
}

// do check
resCheck, err := t.TxTool.DoCheckBeforeBuildTx(parentAccountId)
if err != nil {
if resCheck != nil && resCheck.Continue {
log.Info("CheckInProgressTask: task in progress", parentAccountId)
continue
}
return fmt.Errorf("DoCheckBeforeBuildTx err: %s", err.Error())
}

// do check custom script
if tId, customScriptOk := t.TxTool.DoCheckCustomScriptHashNew(resCheck.SubAccountLiveCell, taskList); !customScriptOk {
log.Error("DoCheckCustomScriptHash err:", tId)
if err := t.DbDao.UpdateTaskCompleteWithDiffCustomScriptHash(tId, taskMap[tId]); err != nil {
return fmt.Errorf("UpdateTaskCompleteWithDiffCustomScriptHash err: %s", err.Error())
}
return fmt.Errorf("DoCheckCustomScriptHash err: %s", tId)
}

// get account
parentAccount, err := t.DbDao.GetAccountInfoByAccountId(parentAccountId)
if err != nil {
return fmt.Errorf("GetAccountInfoByAccountId err: %s", err.Error())
}

// do task detail
if err := t.doTaskDetail(&paramDoTaskDetail{
action: action,
taskList: taskList,
taskMap: taskMap,
account: &parentAccount,
subAccountLiveCell: resCheck.SubAccountLiveCell,
baseInfo: resCheck.BaseInfo,
subAccountIds: subAccountIds,
}); err != nil {
if err == cache.ErrDistributedLockPreemption {
log.Info("doTaskDetail: task in progress", parentAccountId)
continue
}
return fmt.Errorf("doTaskDetail err: %s", err.Error())
}
}
//list, err := t.DbDao.GetNeedToDoTaskListByAction(config.Cfg.Slb.SvrName, action)
//if err != nil {
// return fmt.Errorf("GetNeedToDoTaskListByAction err: %s [%s]", err.Error(), action)
//}
//
//// group task list by ParentAccountId
//mapTaskList, mapTaskIdList := t.groupByParentAccountIdNew(list)
//// do task
//for parentAccountId, taskList := range mapTaskList {
// taskIdList, _ := mapTaskIdList[parentAccountId]
//
// // check need roll back
// var needRollbackIds []uint64
// for _, v := range taskList {
// if v.SmtStatus != tables.SmtStatusNeedToWrite {
// needRollbackIds = append(needRollbackIds, v.Id)
// }
// }
// if len(needRollbackIds) > 0 {
// if err := t.DbDao.UpdateTaskStatusToRollback(needRollbackIds); err != nil {
// return fmt.Errorf("UpdateTaskStatusToRollback err: %s", err.Error())
// }
// continue
// }
//
// if _, ok := config.Cfg.SuspendMap[parentAccountId]; ok {
// log.Warn("SuspendMap:", parentAccountId)
// continue
// }
//
// // get smt records
// taskMap, subAccountIds, err := t.getTaskMap(taskIdList)
// if err != nil {
// return fmt.Errorf("getTaskMap err: %s", err.Error())
// }
//
// // check nonce
// hasDiffNonce, err := t.doCheckNonceNew(taskMap, subAccountIds)
// if err != nil {
// return fmt.Errorf("doCheckNonce err: %s", err.Error())
// } else if hasDiffNonce {
// log.Warn("doCheckNonce:", parentAccountId)
// continue
// }
//
// // do check
// resCheck, err := t.TxTool.DoCheckBeforeBuildTx(parentAccountId)
// if err != nil {
// if resCheck != nil && resCheck.Continue {
// log.Info("CheckInProgressTask: task in progress", parentAccountId)
// continue
// }
// return fmt.Errorf("DoCheckBeforeBuildTx err: %s", err.Error())
// }
//
// // do check custom script
// if tId, customScriptOk := t.TxTool.DoCheckCustomScriptHashNew(resCheck.SubAccountLiveCell, taskList); !customScriptOk {
// log.Error("DoCheckCustomScriptHash err:", tId)
// if err := t.DbDao.UpdateTaskCompleteWithDiffCustomScriptHash(tId, taskMap[tId]); err != nil {
// return fmt.Errorf("UpdateTaskCompleteWithDiffCustomScriptHash err: %s", err.Error())
// }
// return fmt.Errorf("DoCheckCustomScriptHash err: %s", tId)
// }
//
// // get account
// parentAccount, err := t.DbDao.GetAccountInfoByAccountId(parentAccountId)
// if err != nil {
// return fmt.Errorf("GetAccountInfoByAccountId err: %s", err.Error())
// }
//
// // do task detail
// if err := t.doTaskDetail(&paramDoTaskDetail{
// action: action,
// taskList: taskList,
// taskMap: taskMap,
// account: &parentAccount,
// subAccountLiveCell: resCheck.SubAccountLiveCell,
// baseInfo: resCheck.BaseInfo,
// subAccountIds: subAccountIds,
// }); err != nil {
// if err == cache.ErrDistributedLockPreemption {
// log.Info("doTaskDetail: task in progress", parentAccountId)
// continue
// }
// return fmt.Errorf("doTaskDetail err: %s", err.Error())
// }
//}
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions txtool/update_sub_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (s *SubAccountTxTool) BuildUpdateSubAccountTx(p *ParamBuildUpdateSubAccount
needCapacity: p.CommonFee + registerCapacity,
})
if err != nil {
log.Info("UpdateTaskStatusToRollbackWithBalanceErr:", p.TaskInfo.TaskId)
_ = s.DbDao.UpdateTaskStatusToRollbackWithBalanceErr(p.TaskInfo.TaskId)
return nil, fmt.Errorf("getBalanceCell err: %s", err.Error())
}
change += p.CommonFee
Expand Down

0 comments on commit 276c30c

Please sign in to comment.