Skip to content

Commit

Permalink
Merge pull request #152 from commerceblock/fix/refactor/getUnconfirmedTx
Browse files Browse the repository at this point in the history
fix: refactor getUnconfirmedTx
  • Loading branch information
tomt1664 authored Mar 20, 2024
2 parents be5ec0a + 8bd211d commit 61fe0bf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
29 changes: 29 additions & 0 deletions attestation/attestclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"encoding/hex"
"errors"
"math"
"fmt"

confpkg "mainstay/config"
"mainstay/crypto"
"mainstay/log"
"mainstay/models"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcjson"
Expand Down Expand Up @@ -577,3 +579,30 @@ func (w *AttestClient) getUnconfirmedTx() (bool, chainhash.Hash, error) {
}
return false, chainhash.Hash{}, nil
}

func (w *AttestClient) getUnconfirmedTxFromCommitments(latestAttestations []models.Attestation) (bool, chainhash.Hash, error) {
mempool, err := w.MainClient.GetRawMempool()
if err != nil {
return false, chainhash.Hash{}, err
}
fmt.Printf("mempool %v", mempool)
fmt.Printf("latest Attestaions %v", latestAttestations)
if (len(latestAttestations) > 0) {
for _, attest := range latestAttestations {
for _, mem := range mempool {
if attest.Txid.IsEqual(mem) {
if w.verifyTxOnSubchain(*mem) {
return true, *mem, nil
}
}
}
}
} else {
for _, hash := range mempool {
if w.verifyTxOnSubchain(*hash) {
return true, *hash, nil
}
}
}
return false, chainhash.Hash{}, nil
}
8 changes: 6 additions & 2 deletions attestation/attestservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,13 @@ func (s *AttestService) stateInitWalletFailure() {
// - If no attestation found, check last unconfirmed from db
func (s *AttestService) doStateInit() {
log.Infoln("*AttestService* INITIATING ATTESTATION PROCESS")

latestAttestations, err := s.server.dbInterface.GetUnconfirmedAttestations()
log.Infof("latest commitment %v", latestAttestations)
if err != nil {
return
}
// find the state of the attestation
unconfirmed, unconfirmedTxid, unconfirmedErr := s.attester.getUnconfirmedTx()
unconfirmed, unconfirmedTxid, unconfirmedErr := s.attester.getUnconfirmedTxFromCommitments(latestAttestations)
if s.setFailure(unconfirmedErr) {
return // will rebound to init
} else if unconfirmed { // check mempool for unconfirmed - added check in case something gets rejected
Expand Down
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Db interface {
getAttestationMerkleRoot(chainhash.Hash) (string, error)

// get methods required by server
GetUnconfirmedAttestations() ([]models.Attestation, error)
GetLatestAttestationMerkleRoot(bool) (string, error)
GetClientCommitments() ([]models.ClientCommitment, error)
GetAttestationMerkleCommitments(chainhash.Hash) ([]models.CommitmentMerkleCommitment, error)
Expand Down
8 changes: 7 additions & 1 deletion db/db_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type DbFake struct {
MerkleCommitments []models.CommitmentMerkleCommitment
MerkleProofs []models.CommitmentMerkleProof
latestCommitments []models.ClientCommitment
latestAttestations []models.Attestation
}

// Return new DbFake instance
Expand All @@ -30,7 +31,8 @@ func NewDbFake() *DbFake {
[]models.AttestationInfo{},
[]models.CommitmentMerkleCommitment{},
[]models.CommitmentMerkleProof{},
[]models.ClientCommitment{}}
[]models.ClientCommitment{},
[]models.Attestation{}}
}

// Save latest attestation to Attestations
Expand Down Expand Up @@ -145,6 +147,10 @@ func (d *DbFake) getAttestationMerkleRoot(txid chainhash.Hash) (string, error) {
return "", nil
}

func (d *DbFake) GetUnconfirmedAttestations() ([]models.Attestation, error) {
return d.latestAttestations, nil
}

// Return commitment for attestation with given txid
func (d *DbFake) GetAttestationMerkleCommitments(txid chainhash.Hash) ([]models.CommitmentMerkleCommitment, error) {
// get merkle root of attestation
Expand Down
39 changes: 39 additions & 0 deletions db/db_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,45 @@ func (d *DbMongo) getAttestationMerkleRoot(txid chainhash.Hash) (string, error)
return attestationDoc.Lookup(models.CommitmentMerkleRootName).StringValue(), nil
}

func (d *DbMongo) GetUnconfirmedAttestations() ([]models.Attestation, error) {
// Filter for unconfirmed attestations
confirmedFilter := bsonx.Doc{{models.AttestationConfirmedName, bsonx.Boolean(false)}}

// Find all unconfirmed attestations
cursor, err := d.db.Collection(ColNameAttestation).Find(d.ctx, confirmedFilter)
if err != nil {
return nil, errors.New(fmt.Sprintf("%s %v", ErrorAttestationGet, err))
}
defer func() {
if err := cursor.Close(d.ctx); err != nil {
// Log or handle closing error
}
}()

// Iterate over cursor and collect unconfirmed attestations
var attestations []models.Attestation
for {
// Call cursor.Next with only context
moreDocs := cursor.Next(d.ctx)
if !moreDocs {
// No more documents found, break the loop
break
}
if err != nil {
return nil, errors.New(fmt.Sprintf("%s %v", ErrorAttestationGet, err))
}

// Decode document into an attestation object (optional if using All)
var attestation models.Attestation
if err := cursor.Decode(d.ctx); err != nil {
return nil, errors.New(fmt.Sprintf("%s %v", ErrorAttestationGet, err))
}
attestations = append(attestations, attestation)
}

return attestations, nil
}

// Return Commitment from MerkleCommitment commitments for attestation with given txid hash
func (d *DbMongo) GetAttestationMerkleCommitments(txid chainhash.Hash) ([]models.CommitmentMerkleCommitment, error) {
// get merkle root of attestation
Expand Down

0 comments on commit 61fe0bf

Please sign in to comment.