Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7 quorums fix #141

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
APIGetTokenNumber string = "/api/get-token-number"
APIGetMigratedTokenStatus string = "/api/get-Migrated-token-status"
APISyncDIDArbitration string = "/api/sync-did-arbitration"
APIUnlockTokens string = "/api/unlock-tokens"
)

const (
Expand Down
91 changes: 81 additions & 10 deletions core/quorum_initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ type ArbitaryStatus struct {
status bool
}

type TokenList struct {
Tokens []string
DID string
}

// PingSetup will setup the ping route
func (c *Core) QuroumSetup() {
c.l.AddRoute(APICreditStatus, "GET", c.creditStatus)
Expand All @@ -149,6 +154,7 @@ func (c *Core) QuroumSetup() {
c.l.AddRoute(APIUpdatePledgeToken, "POST", c.updatePledgeToken)
c.l.AddRoute(APISignatureRequest, "POST", c.signatureRequest)
c.l.AddRoute(APISendReceiverToken, "POST", c.updateReceiverToken)
c.l.AddRoute(APIUnlockTokens, "POST", c.unlockTokens)
gklps marked this conversation as resolved.
Show resolved Hide resolved
if c.arbitaryMode {
c.l.AddRoute(APIMapDIDArbitration, "POST", c.mapDIDArbitration)
c.l.AddRoute(APICheckDIDArbitration, "GET", c.chekDIDArbitration)
Expand Down Expand Up @@ -320,6 +326,10 @@ func (c *Core) initiateConsensus(cr *ConensusRequest, sc *contract.Contract, dc
}
}
if err != nil {
unlockErr := c.checkLokedTokens(cr, ql)
if unlockErr != nil {
c.log.Error(unlockErr.Error() + "Locked tokens could not be unlocked")
}
return nil, nil, err
}

Expand Down Expand Up @@ -381,7 +391,7 @@ func (c *Core) initiateConsensus(cr *ConensusRequest, sc *contract.Contract, dc
c.log.Debug("String: token is ", token, " issuetype is ", issueType)
issueTypeInt, err1 := strconv.Atoi(issueType)
if err1 != nil {
errMsg := fmt.Sprintf("Consensus failed due to token chain sync issue, issueType string conversion, err %v", err1)
errMsg := fmt.Sprintf("Consensus failed due to token chain sync issue, issueType string conversion, err %v", err1)
c.log.Error(errMsg)
return nil, nil, fmt.Errorf(errMsg)
}
Expand All @@ -397,7 +407,7 @@ func (c *Core) initiateConsensus(cr *ConensusRequest, sc *contract.Contract, dc
syncIssueTokenDetails.TokenStatus = wallet.TokenChainSyncIssue
c.log.Debug("sync issue token details status updated", syncIssueTokenDetails)
c.w.UpdateToken(syncIssueTokenDetails)
return nil, nil, errors.New(br.Message)
return nil, nil, errors.New(br.Message)
}
}
if !br.Status {
Expand Down Expand Up @@ -656,29 +666,55 @@ func (c *Core) finishConsensus(id string, qt int, p *ipfsport.Peer, status bool,
defer c.qlock.Unlock()
cs, ok := c.quorumRequest[id]
if !ok {
fmt.Println("failed to get quorum consensus")
if p != nil {
p.Close()
}
return
}
pd, ok := c.pd[id] //getting details of quorums who pledged
if !ok {
fmt.Println("failed to get pledged token details")
if p != nil {
p.Close()
}
return
}
pledgingQuorumDID := make([]string, 0, len(pd.PledgedTokens))
for k := range pd.PledgedTokens {
pledgingQuorumDID = append(pledgingQuorumDID, k)
}
var pledgingDID string
if len(pledgingQuorumDID) > 0 {
pledgingDID = pledgingQuorumDID[0]
}

switch qt {
case 0:
cs.Result.RunningCount--
if status {
did := p.GetPeerDID()
if cs.Result.SuccessCount < MinConsensusRequired {
csig := CreditSignature{
Signature: util.HexToStr(ss),
PrivSignature: util.HexToStr(ps),
DID: did,
Hash: hash,
}
csig := CreditSignature{
thewebchap marked this conversation as resolved.
Show resolved Hide resolved
Signature: util.HexToStr(ss),
PrivSignature: util.HexToStr(ps),
DID: did,
Hash: hash,
}
if cs.Result.SuccessCount < MinConsensusRequired-1 {
cs.P[did] = p
cs.Credit.Credit = append(cs.Credit.Credit, csig)
cs.Result.SuccessCount++
} else if did == pledgingDID && cs.Result.SuccessCount == MinConsensusRequired-1 {
cs.P[did] = p
cs.Credit.Credit = append(cs.Credit.Credit, csig)
cs.Result.SuccessCount++
} else if cs.Result.RunningCount == 0 && cs.Result.SuccessCount == MinConsensusRequired-1 {
thewebchap marked this conversation as resolved.
Show resolved Hide resolved
cs.P[did] = p
cs.Credit.Credit = append(cs.Credit.Credit, csig)
cs.Result.SuccessCount++
} else {
p.Close()
}
cs.Result.SuccessCount++
} else {
cs.Result.FailedCount++
if p != nil {
Expand Down Expand Up @@ -1243,3 +1279,38 @@ func (c *Core) createCommitedTokensBlock(newBlock *block.Block, smartContractTok
}
return nil
}

func (c *Core) checkLokedTokens(cr *ConensusRequest, quorumList []string) error {
// var err error
pd := c.pd[cr.ReqID]

pledgingQuorumDID := make([]string, 0, len(pd.PledgedTokens))
for k := range pd.PledgedTokens {
pledgingQuorumDID = append(pledgingQuorumDID, k)
}
var pledgingDID string
if len(pledgingQuorumDID) > 0 {
pledgingDID = pledgingQuorumDID[0]
}
var br model.BasicResponse
for _, addr := range quorumList {
peerID, did, _ := util.ParseAddress(addr)
if did == pledgingDID {
p, err := c.pm.OpenPeerConn(peerID, did, c.getCoreAppName(peerID))
if err != nil {
c.log.Error("Failed to get peer connection", "err", err)
return err
}
tokenList := TokenList{
Tokens: pd.PledgedTokens[pledgingDID],
DID: pledgingDID,
}
err = p.SendJSONRequest("POST", APIUnlockTokens, nil, &tokenList, &br, true)
if err != nil {
c.log.Error("Invalid response for pledge request", "err", err)
return err
}
}
}
return nil
}
31 changes: 31 additions & 0 deletions core/quorum_recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,14 @@ func (c *Core) reqPledgeToken(req *ensweb.Request) *ensweb.Result {
crep.Message = "Failed to parse json request"
return c.l.RenderJSON(req, &crep, http.StatusOK)
}

_, ok := c.qc[did]
if !ok {
c.log.Error("Quorum is not setup")
crep.Message = "Quorum is not setup"
return c.l.RenderJSON(req, &crep, http.StatusOK)
}

if (pr.TokensRequired) < MinTrnxAmt {
c.log.Error("Pledge amount is less than ", MinTrnxAmt)
crep.Message = "Pledge amount is less than minimum transcation amount"
Expand Down Expand Up @@ -1314,3 +1322,26 @@ func (c *Core) getProofverificationDetails(tokenID string, senderAddr string) (s
}
return receiverDID, txnId, nil
}

func (c *Core) unlockTokens(req *ensweb.Request) *ensweb.Result {
var tokenList TokenList
err := c.l.ParseJSON(req, &tokenList)
crep := model.BasicResponse{
Status: false,
}
if err != nil {
c.log.Error("Failed to parse json request", "err", err)
crep.Message = "Failed to parse json request"
return c.l.RenderJSON(req, &crep, http.StatusOK)
}
err = c.w.UnlockLockedTokens(tokenList.DID, tokenList.Tokens)
if err != nil {
c.log.Error("Failed to update token status", "err", err)
return c.l.RenderJSON(req, &crep, http.StatusOK)
}
crep.Status = true
crep.Message = "Tokens Unlocked Successfully."
c.log.Info("Tokens Unclocked")
return c.l.RenderJSON(req, &crep, http.StatusOK)

}
18 changes: 18 additions & 0 deletions core/wallet/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,21 @@ func (w *Wallet) ReleaseAllLockedTokens() error {
}
return nil
}

func (w *Wallet) UnlockLockedTokens(did string, tokenList []string) error {
for _, tid := range tokenList {
var t Token
err := w.s.Read(TokenStorage, &t, "did=? AND token_id=?", did, tid)
if err != nil {
w.log.Error("Failed to update token status", "err", err)
return err
}
t.TokenStatus = TokenIsFree
err = w.s.Update(TokenStorage, &t, "did=? AND token_id=?", did, tid)
if err != nil {
w.log.Error("Failed to update token status", "err", err)
return err
}
}
return nil
}