From c1fe978e74c0092bd83138dd9cf60b6986d3b2dd Mon Sep 17 00:00:00 2001 From: Andrew Gouin Date: Mon, 11 Dec 2023 14:54:35 -0700 Subject: [PATCH 1/2] mitigate unexpected state remove ClearAllNonces --- signer/cosigner_nonce_cache_test.go | 16 ++++++++++++++++ signer/threshold_validator.go | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/signer/cosigner_nonce_cache_test.go b/signer/cosigner_nonce_cache_test.go index 756f5960..ca666674 100644 --- a/signer/cosigner_nonce_cache_test.go +++ b/signer/cosigner_nonce_cache_test.go @@ -98,7 +98,23 @@ func TestClearNonces(t *testing.T) { for _, n := range cnc.cache.cache { require.Len(t, n.Nonces, 2) + oneFound := false + twoFound := false + for _, cnr := range n.Nonces { + if cnr.Cosigner == cosigners[1] { + oneFound = true + } + if cnr.Cosigner == cosigners[2] { + twoFound = true + } + } + require.True(t, oneFound) + require.True(t, twoFound) } + + cnc.ClearNonces(cosigners[1]) + + require.Equal(t, 0, cnc.cache.Size()) } type mockPruner struct { diff --git a/signer/threshold_validator.go b/signer/threshold_validator.go index f9dede6d..8caaf214 100644 --- a/signer/threshold_validator.go +++ b/signer/threshold_validator.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os" + "strings" "sync" "time" @@ -734,6 +735,10 @@ func (pv *ThresholdValidator) Sign(ctx context.Context, chainID string, block Bl "err", err.Error(), ) + if strings.Contains(err.Error(), "unexpected state, metadata does not exist for U:") { + pv.nonceCache.ClearNonces(cosigner) + } + if cosigner.GetID() == pv.myCosigner.GetID() { return err } From 67dbdd1dde89fcadc2c2fd2578feeeae2d64ee9e Mon Sep 17 00:00:00 2001 From: Andrew Gouin Date: Mon, 11 Dec 2023 23:18:01 -0700 Subject: [PATCH 2/2] use string constant --- signer/local_cosigner.go | 5 ++++- signer/threshold_validator.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/signer/local_cosigner.go b/signer/local_cosigner.go index bc3ee517..d8955371 100644 --- a/signer/local_cosigner.go +++ b/signer/local_cosigner.go @@ -435,6 +435,8 @@ func (cosigner *LocalCosigner) getNonce( return nonce, nil } +const errUnexpectedState = "unexpected state, metadata does not exist for U:" + // setNonce stores a nonce provided by another cosigner func (cosigner *LocalCosigner) setNonce(uuid uuid.UUID, nonce CosignerNonce) error { // Verify the source signature @@ -456,7 +458,8 @@ func (cosigner *LocalCosigner) setNonce(uuid uuid.UUID, nonce CosignerNonce) err // generate metadata placeholder if !ok { return fmt.Errorf( - "unexpected state, metadata does not exist for U: %s", + "%s %s", + errUnexpectedState, uuid, ) } diff --git a/signer/threshold_validator.go b/signer/threshold_validator.go index 8caaf214..891fb3c8 100644 --- a/signer/threshold_validator.go +++ b/signer/threshold_validator.go @@ -735,7 +735,7 @@ func (pv *ThresholdValidator) Sign(ctx context.Context, chainID string, block Bl "err", err.Error(), ) - if strings.Contains(err.Error(), "unexpected state, metadata does not exist for U:") { + if strings.Contains(err.Error(), errUnexpectedState) { pv.nonceCache.ClearNonces(cosigner) }