-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: addresses module #291
Changes from all commits
a8bcd0e
1814cf1
6bc712c
8d36899
4b374a1
d415759
c62622e
3ed4f58
1dbb93c
abe2f37
87e5901
19729af
426819d
f1f06a8
8a6e97a
9cffc23
7b51fab
cb2fc5a
0b5ec42
b6f11d5
5e35ce0
113970c
7e89e5e
bf31ad9
8e3d78f
34fdfc0
1673648
8ac8e30
eb632dd
3143444
cf334d5
e4cfdbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,7 @@ jobs: | |
"CellarFees", | ||
"Incentives", | ||
"Pubsub", | ||
"Addresses", | ||
] | ||
steps: | ||
- name: Set up Go 1.22 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package integration_tests | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/peggyjv/sommelier/v7/x/addresses/types" | ||
) | ||
|
||
func (s *IntegrationTestSuite) TestAddresses() { | ||
s.Run("Bring up chain, submit, query, and remove address mappings", func() { | ||
s.T().Log("Starting x/addresses tests") | ||
val := s.chain.validators[0] | ||
kb, err := val.keyring() | ||
s.Require().NoError(err) | ||
val0ClientCtx, err := s.chain.clientContext("tcp://localhost:26657", &kb, "val", val.address()) | ||
s.Require().NoError(err) | ||
addressesQueryClient := types.NewQueryClient(val0ClientCtx) | ||
|
||
orch := s.chain.orchestrators[0] | ||
orchClientCtx, err := s.chain.clientContext("tcp://localhost:26657", orch.keyring, "orch", orch.address()) | ||
s.Require().NoError(err) | ||
|
||
evmAddress := common.HexToAddress("0x1234567890123456789012345678901234567890") | ||
cosmosAddress := orch.address() | ||
|
||
addAddressMappingMsg, err := types.NewMsgAddAddressMapping(evmAddress, cosmosAddress) | ||
s.Require().NoError(err) | ||
|
||
s.T().Logf("Submitting mapping from %s to %s", evmAddress.Hex(), cosmosAddress.String()) | ||
_, err = s.chain.sendMsgs(*orchClientCtx, addAddressMappingMsg) | ||
s.Require().NoError(err) | ||
|
||
s.T().Log("Testing queries return expected addresses") | ||
s.Require().Eventually(func() bool { | ||
queryRes, err := addressesQueryClient.QueryAddressMappings(context.Background(), &types.QueryAddressMappingsRequest{}) | ||
if err != nil { | ||
s.T().Logf("Error querying address mappings: %s", err) | ||
return false | ||
} | ||
|
||
return len(queryRes.AddressMappings) == 1 && evmAddress.Hex() == queryRes.AddressMappings[0].EvmAddress | ||
}, 20*time.Second, 4*time.Second, "address mapping never found") | ||
|
||
queryByEvmRes, err := addressesQueryClient.QueryAddressMappingByEVMAddress(context.Background(), &types.QueryAddressMappingByEVMAddressRequest{EvmAddress: evmAddress.Hex()}) | ||
s.Require().NoError(err) | ||
s.Require().Equal(cosmosAddress.String(), queryByEvmRes.CosmosAddress, "Cosmos address does not match") | ||
s.Require().Equal(evmAddress.Hex(), queryByEvmRes.EvmAddress, "EVM address does not match") | ||
|
||
queryByCosmosRes, err := addressesQueryClient.QueryAddressMappingByCosmosAddress(context.Background(), &types.QueryAddressMappingByCosmosAddressRequest{CosmosAddress: cosmosAddress.String()}) | ||
s.Require().NoError(err) | ||
s.Require().Equal(cosmosAddress.String(), queryByCosmosRes.CosmosAddress, "Cosmos address does not match") | ||
s.Require().Equal(evmAddress.Hex(), queryByCosmosRes.EvmAddress, "EVM address does not match") | ||
|
||
s.T().Log("Removing address mapping") | ||
removeAddressMappingMsg, err := types.NewMsgRemoveAddressMapping(cosmosAddress) | ||
s.Require().NoError(err) | ||
|
||
_, err = s.chain.sendMsgs(*orchClientCtx, removeAddressMappingMsg) | ||
s.Require().NoError(err) | ||
|
||
s.T().Log("Testing mappings query returns no addresses") | ||
s.Require().Eventually(func() bool { | ||
queryRes, err := addressesQueryClient.QueryAddressMappings(context.Background(), &types.QueryAddressMappingsRequest{}) | ||
if err != nil { | ||
s.T().Logf("Error querying address mappings: %s", err) | ||
return false | ||
} | ||
|
||
return len(queryRes.AddressMappings) == 0 | ||
}, 20*time.Second, 4*time.Second, "address mapping not deleted") | ||
|
||
_, err = addressesQueryClient.QueryAddressMappingByEVMAddress(context.Background(), &types.QueryAddressMappingByEVMAddressRequest{EvmAddress: evmAddress.Hex()}) | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "code = NotFound") | ||
|
||
_, err = addressesQueryClient.QueryAddressMappingByCosmosAddress(context.Background(), &types.QueryAddressMappingByCosmosAddressRequest{CosmosAddress: cosmosAddress.String()}) | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "code = NotFound") | ||
|
||
// Test error cases | ||
|
||
// Test adding multiple mappings | ||
s.T().Log("Adding multiple mappings") | ||
evmAddress2 := common.HexToAddress("0x2345678901234567890123456789012345678901") | ||
cosmosAddress2 := s.chain.orchestrators[1].address() | ||
orch1 := s.chain.orchestrators[1] | ||
orch1ClientCtx, err := s.chain.clientContext("tcp://localhost:26657", orch1.keyring, "orch", orch1.address()) | ||
s.Require().NoError(err) | ||
|
||
addAddressMappingMsg2, err := types.NewMsgAddAddressMapping(evmAddress2, cosmosAddress2) | ||
s.Require().NoError(err) | ||
|
||
_, err = s.chain.sendMsgs(*orchClientCtx, addAddressMappingMsg) | ||
s.Require().NoError(err) | ||
_, err = s.chain.sendMsgs(*orch1ClientCtx, addAddressMappingMsg2) | ||
s.Require().NoError(err) | ||
|
||
// Query multiple mappings | ||
s.T().Log("Querying multiple mappings") | ||
s.Require().Eventually(func() bool { | ||
queryRes, err := addressesQueryClient.QueryAddressMappings(context.Background(), &types.QueryAddressMappingsRequest{}) | ||
if err != nil { | ||
s.T().Logf("Error querying address mappings: %s", err) | ||
return false | ||
} | ||
|
||
return len(queryRes.AddressMappings) == 2 | ||
}, 20*time.Second, 4*time.Second, "expected two address mappings") | ||
|
||
// Test adding a duplicate mapping | ||
s.T().Log("Adding a duplicate mapping") | ||
duplicateMsg, err := types.NewMsgAddAddressMapping(evmAddress, cosmosAddress) | ||
s.Require().NoError(err) | ||
|
||
_, err = s.chain.sendMsgs(*orchClientCtx, duplicateMsg) | ||
s.Require().NoError(err) | ||
_, err = s.chain.sendMsgs(*orchClientCtx, duplicateMsg) | ||
s.Require().NoError(err) | ||
|
||
// Test removing a non-existent mapping | ||
s.T().Log("Removing a non-existent mapping") | ||
nonExistentAddress := s.chain.orchestrators[2].address() | ||
orch2 := s.chain.orchestrators[2] | ||
orch2ClientCtx, err := s.chain.clientContext("tcp://localhost:26657", orch2.keyring, "orch", orch2.address()) | ||
s.Require().NoError(err) | ||
removeNonExistentMsg, err := types.NewMsgRemoveAddressMapping(nonExistentAddress) | ||
s.Require().NoError(err) | ||
|
||
_, err = s.chain.sendMsgs(*orch2ClientCtx, removeNonExistentMsg) | ||
s.Require().NoError(err) | ||
|
||
// Test querying with invalid addresses | ||
s.T().Log("Querying with invalid addresses") | ||
_, err = addressesQueryClient.QueryAddressMappingByEVMAddress(context.Background(), &types.QueryAddressMappingByEVMAddressRequest{EvmAddress: "invalid"}) | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "invalid EVM address") | ||
|
||
s.T().Log("Querying with invalid cosmos address") | ||
_, err = addressesQueryClient.QueryAddressMappingByCosmosAddress(context.Background(), &types.QueryAddressMappingByCosmosAddressRequest{CosmosAddress: "invalid"}) | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "failed to parse cosmos address") | ||
}) | ||
Comment on lines
+12
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor Test into Sub-Tests for Improved Readability The func (s *IntegrationTestSuite) TestAddresses() {
s.Run("Add and Query Address Mappings", func() {
// Test logic for adding and querying mappings
})
s.Run("Remove Address Mappings", func() {
// Test logic for removing mappings
})
s.Run("Error Cases", func() {
// Test logic for error scenarios
})
} This approach isolates test cases, making it easier to identify and debug failures related to specific functionality. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
syntax = "proto3"; | ||
package addresses.v1; | ||
|
||
option go_package = "github.com/peggyjv/sommelier/v7/x/addresses/types"; | ||
|
||
message AddressMapping { | ||
string cosmos_address = 1; | ||
string evm_address = 2; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
syntax = "proto3"; | ||
package addresses.v1; | ||
|
||
option go_package = "github.com/peggyjv/sommelier/v7/x/addresses/types"; | ||
|
||
import "addresses/v1/addresses.proto"; | ||
import "gogoproto/gogo.proto"; | ||
import "google/api/annotations.proto"; | ||
|
||
message GenesisState { | ||
Params params = 1 [ (gogoproto.nullable) = false ]; | ||
repeated AddressMapping address_mappings = 2; | ||
} | ||
|
||
message Params {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use Context with Timeout for Client Queries
Currently, the code uses
context.Background()
for client queries, which may lead to indefinite blocking if the service is unresponsive. Consider using a context with a timeout to prevent potential hangs:Applying a timeout ensures that the test fails gracefully if a query does not complete within the expected time frame.
Also applies to: 46-46, 51-51, 65-65, 74-74, 136-136, 141-141