Skip to content

Commit

Permalink
feat: expose sequence number correctly to users of the signer (celest…
Browse files Browse the repository at this point in the history
…iaorg#2992)

It's probably misleading that `GetSequence` should not only return the
sequence number but increment it. The incrementing sequence should be
internal and the user should just be able to use `Sequence` if they wish
to query the sequence number of the signer
  • Loading branch information
cmwaters authored Jan 10, 2024
1 parent 181ee40 commit dc8b7f5
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/user/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,30 @@ func (s *Signer) PubKey() cryptotypes.PubKey {
return s.pk
}

// Sequence returns the last signed sequence number of the signer
func (s *Signer) Sequence() uint64 {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.lastSignedSequence
}

// GetSequence gets the latest signed sequence and increments the local sequence number
// Deprecated: Use Sequence if you want to get the latest signed sequence number
func (s *Signer) GetSequence() uint64 {
s.mtx.Lock()
defer s.mtx.Unlock()
defer func() { s.lastSignedSequence++ }()
return s.lastSignedSequence
}

// getAndIncrementSequence gets the latest signed sequence and increments the local sequence number
func (s *Signer) getAndIncrementSequence() uint64 {
s.mtx.Lock()
defer s.mtx.Unlock()
defer func() { s.lastSignedSequence++ }()
return s.lastSignedSequence
}

// ForceSetSequence manually overrides the current sequence number. Be careful when
// invoking this as it may cause the transactions to reject the sequence if
// it doesn't match the one in state
Expand All @@ -284,6 +300,11 @@ func (s *Signer) ForceSetSequence(seq uint64) {
s.lastSignedSequence = seq
}

// Keyring exposes the signers underlying keyring
func (s *Signer) Keyring() keyring.Keyring {
return s.keys
}

func (s *Signer) signTransaction(builder client.TxBuilder) error {
signers := builder.GetTx().GetSigners()
if len(signers) != 1 {
Expand All @@ -294,7 +315,7 @@ func (s *Signer) signTransaction(builder client.TxBuilder) error {
return fmt.Errorf("expected signer %s, got %s", s.address.String(), signers[0].String())
}

sequence := s.GetSequence()
sequence := s.getAndIncrementSequence()

// To ensure we have the correct bytes to sign over we produce
// a dry run of the signing data
Expand Down

0 comments on commit dc8b7f5

Please sign in to comment.