Skip to content

Commit

Permalink
Fix mutex issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pouya-eghbali committed Mar 24, 2024
1 parent f158e27 commit a8a6649
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
56 changes: 36 additions & 20 deletions src/plugins/uniswap/uniswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ var crossPrices map[string]big.Int
var crossTokens map[string]datasets.TokenKey
var lastPrice big.Int

func CheckAndCacheSignature(
reportedValues *xsync.MapOf[bls12381.G1Affine, big.Int],
signature bls12381.G1Affine, signer bls.Signer,
hash bls12381.G1Affine,
totalVoted *big.Int) error {

signatureMutex.Lock()
defer signatureMutex.Unlock()

cached, _ := signatureCache.Get(hash)

packed := bls.Signature{
Signature: signature,
Signer: signer,
Processed: false,
}

for _, item := range cached {
if item.Signer.PublicKey == signer.PublicKey {
log.Logger.
With("Address", address.Calculate(signer.PublicKey[:])).
Debug("Duplicated signature")
return fmt.Errorf("duplicated signature")
}
}

reportedValues.Store(hash, *totalVoted)
cached = append(cached, packed)
signatureCache.Add(hash, cached)

return nil
}

// TODO: This needs to work with different datasets
// TODO: Can we turn this into a library func?
func RecordSignature(
Expand Down Expand Up @@ -146,29 +179,12 @@ func RecordSignature(
return isMajority
})

signatureMutex.Lock()
cached, _ := signatureCache.Get(hash)

packed := bls.Signature{
Signature: signature,
Signer: signer,
Processed: false,
}
err = CheckAndCacheSignature(&reportedValues, signature, signer, hash, totalVoted)

for _, item := range cached {
if item.Signer.PublicKey == signer.PublicKey {
log.Logger.
With("Address", address.Calculate(signer.PublicKey[:])).
Debug("Duplicated signature")
return
}
if err != nil {
return
}

reportedValues.Store(hash, *totalVoted)
cached = append(cached, packed)
signatureCache.Add(hash, cached)
signatureMutex.Unlock()

if isMajority {
reportLog := log.Logger.
With("Block", info.Asset.Block).
Expand Down
2 changes: 1 addition & 1 deletion src/utils/debounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func Debounce[KeyType comparable, ArgType any](
debounce.Lock()

go func() {
defer debounce.Unlock()
delete(debounce.timers, key)
function(arg)
debounce.Unlock()
}()
})
}
Expand Down

0 comments on commit a8a6649

Please sign in to comment.