forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add signer extraction adapter to prio-nonce mempool (backport c…
…osmos#18991) (cosmos#19042) Co-authored-by: Eric Warehime <[email protected]> Co-authored-by: Julien Robert <[email protected]>
- Loading branch information
1 parent
8ac0492
commit 3e9a3e9
Showing
5 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package mempool_test | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/mempool" | ||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" | ||
txsigning "github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
) | ||
|
||
type nonVerifiableTx struct{} | ||
|
||
func (n nonVerifiableTx) GetMsgs() []sdk.Msg { | ||
panic("not implemented") | ||
} | ||
|
||
func (n nonVerifiableTx) GetMsgsV2() ([]proto.Message, error) { | ||
panic("not implemented") | ||
} | ||
|
||
func TestDefaultSignerExtractor(t *testing.T) { | ||
accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 1) | ||
sa := accounts[0].Address | ||
ext := mempool.NewDefaultSignerExtractionAdapter() | ||
goodTx := testTx{id: 0, priority: 0, nonce: 0, address: sa} | ||
badTx := &sigErrTx{getSigs: func() ([]txsigning.SignatureV2, error) { | ||
return nil, fmt.Errorf("error") | ||
}} | ||
nonSigVerify := nonVerifiableTx{} | ||
|
||
tests := []struct { | ||
name string | ||
tx sdk.Tx | ||
sea mempool.SignerExtractionAdapter | ||
err error | ||
}{ | ||
{name: "valid tx extracts sigs", tx: goodTx, sea: ext, err: nil}, | ||
{name: "invalid tx fails on sig", tx: badTx, sea: ext, err: fmt.Errorf("err")}, | ||
{name: "non-verifiable tx fails on conversion", tx: nonSigVerify, sea: ext, err: fmt.Errorf("tx of type %T does not implement SigVerifiableTx", nonSigVerify)}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
sigs, err := test.sea.GetSigners(test.tx) | ||
if test.err != nil { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, sigs[0].String(), mempool.SignerData{Signer: sa, Sequence: 0}.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package mempool | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
) | ||
|
||
// SignerData contains canonical useful information about the signer of a transaction | ||
type SignerData struct { | ||
Signer sdk.AccAddress | ||
Sequence uint64 | ||
} | ||
|
||
// NewSignerData returns a new SignerData instance. | ||
func NewSignerData(signer sdk.AccAddress, sequence uint64) SignerData { | ||
return SignerData{ | ||
Signer: signer, | ||
Sequence: sequence, | ||
} | ||
} | ||
|
||
// String implements the fmt.Stringer interface. | ||
func (s SignerData) String() string { | ||
return fmt.Sprintf("SignerData{Signer: %s, Sequence: %d}", s.Signer, s.Sequence) | ||
} | ||
|
||
// SignerExtractionAdapter is an interface used to determine how the signers of a transaction should be extracted | ||
// from the transaction. | ||
type SignerExtractionAdapter interface { | ||
GetSigners(sdk.Tx) ([]SignerData, error) | ||
} | ||
|
||
var _ SignerExtractionAdapter = DefaultSignerExtractionAdapter{} | ||
|
||
// DefaultSignerExtractionAdapter is the default implementation of SignerExtractionAdapter. It extracts the signers | ||
// from a cosmos-sdk tx via GetSignaturesV2. | ||
type DefaultSignerExtractionAdapter struct{} | ||
|
||
// NewDefaultSignerExtractionAdapter constructs a new DefaultSignerExtractionAdapter instance | ||
func NewDefaultSignerExtractionAdapter() DefaultSignerExtractionAdapter { | ||
return DefaultSignerExtractionAdapter{} | ||
} | ||
|
||
// GetSigners implements the Adapter interface | ||
func (DefaultSignerExtractionAdapter) GetSigners(tx sdk.Tx) ([]SignerData, error) { | ||
sigTx, ok := tx.(signing.SigVerifiableTx) | ||
if !ok { | ||
return nil, fmt.Errorf("tx of type %T does not implement SigVerifiableTx", tx) | ||
} | ||
|
||
sigs, err := sigTx.GetSignaturesV2() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signers := make([]SignerData, len(sigs)) | ||
for i, sig := range sigs { | ||
signers[i] = NewSignerData( | ||
sig.PubKey.Address().Bytes(), | ||
sig.Sequence, | ||
) | ||
} | ||
|
||
return signers, nil | ||
} |