-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(adapters): Add SignerExtractionAdapter [ENG-1916] (#114)
* add a signer-extraction-adapter * linting * feat(adapters/mev-lane): Use the SignerExtractionAdapter in the Mev-Lane [ENG-1917] (#115) * use SignerExtractionAdapter in the Factory * feat(e2e): block sdk integration updates (#122) * cherry-pick from injective * remove transactions from app-side mempool on failed re-checktx
- Loading branch information
Showing
23 changed files
with
675 additions
and
300 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
51 changes: 51 additions & 0 deletions
51
adapters/signer_extraction_adapter/signer_extraction_adapter.go
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,51 @@ | ||
package signerextraction | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
) | ||
|
||
type SignerData struct { | ||
Signer sdk.AccAddress | ||
Sequence uint64 | ||
} | ||
|
||
// SignerExtractionAdapter is an interface used to determine how the signers of a transaction should be extracted | ||
// from the transaction. | ||
type Adapter interface { | ||
GetSigners(sdk.Tx) ([]SignerData, error) | ||
} | ||
|
||
var _ Adapter = DefaultAdapter{} | ||
|
||
// DefaultSignerExtractionAdapter is the default implementation of SignerExtractionAdapter. It extracts the signers | ||
// from a cosmos-sdk tx via GetSignaturesV2. | ||
type DefaultAdapter struct{} | ||
|
||
func NewDefaultAdapter() DefaultAdapter { | ||
return DefaultAdapter{} | ||
} | ||
|
||
func (DefaultAdapter) 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] = SignerData{ | ||
Signer: sig.PubKey.Address().Bytes(), | ||
Sequence: sig.Sequence, | ||
} | ||
} | ||
|
||
return signers, nil | ||
} |
53 changes: 53 additions & 0 deletions
53
adapters/signer_extraction_adapter/signer_extraction_adapter_test.go
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,53 @@ | ||
package signerextraction_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
signer_extraction "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter" | ||
testutils "github.com/skip-mev/block-sdk/testutils" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type SignerExtractionAdapterTestSuite struct { | ||
suite.Suite | ||
txConfig client.TxConfig | ||
accts []testutils.Account | ||
adapter signer_extraction.DefaultAdapter | ||
} | ||
|
||
func TestSignerExtractionAdapterTestSuite(t *testing.T) { | ||
suite.Run(t, new(SignerExtractionAdapterTestSuite)) | ||
} | ||
|
||
func (s *SignerExtractionAdapterTestSuite) SetupTest() { | ||
encodingConfig := testutils.CreateTestEncodingConfig() | ||
s.txConfig = encodingConfig.TxConfig | ||
|
||
accts := testutils.RandomAccounts(rand.New(rand.NewSource(1)), 2) | ||
|
||
s.accts = accts | ||
} | ||
|
||
func (s *SignerExtractionAdapterTestSuite) TestGetSigners() { | ||
acct := s.accts[0] | ||
tx, err := testutils.CreateTx(s.txConfig, acct, 1, 1, []sdk.Msg{ | ||
&banktypes.MsgSend{ | ||
FromAddress: acct.Address.String(), | ||
ToAddress: acct.Address.String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 1)), | ||
}, | ||
}, sdk.NewCoins(sdk.NewCoin("test", math.NewInt(1)))...) | ||
s.Require().NoError(err) | ||
|
||
signers, err := s.adapter.GetSigners(tx) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Len(signers, 1) | ||
s.Require().Equal(acct.Address.String(), signers[0].Signer.String()) | ||
s.Require().Equal(uint64(1), signers[0].Sequence) | ||
} |
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
Oops, something went wrong.