-
Notifications
You must be signed in to change notification settings - Fork 10
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(delegation): load genesis state #26
Merged
MaxMustermann2
merged 16 commits into
imua-xyz:develop
from
MaxMustermann2:feat/genesis-delegation
Apr 9, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b6f31e4
feat(delegation): set up genesis proto
MaxMustermann2 846ed9f
fix(delegation): use StakerID not StakerId
MaxMustermann2 b59e6ce
refactor(assets): create ValidateID function
MaxMustermann2 52af021
feat(delegation): stateless genesis validation
MaxMustermann2 a2100c7
refactor(delegation): create x-chain param type
MaxMustermann2 f44e12e
fix(assets): accept stakers from known chains only
MaxMustermann2 df2a01e
fix(assets): check deposit and asset chain ids
MaxMustermann2 59f7927
refactor(delegation): validate + load genesis
MaxMustermann2 e9f762e
doc(delegation): add comment for uninit members
MaxMustermann2 fa954eb
Merge branch 'feat/genesis-operator'
MaxMustermann2 da0d954
fix(delegation): do not use genesis map
MaxMustermann2 b1ef8e2
refactor(delegation): remove total amount
MaxMustermann2 7059f78
test(delegation): update genesis test...
MaxMustermann2 83401f9
Merge branch 'feat/genesis-operator'
MaxMustermann2 3fe82fb
doc(delegation): add TODO comments
MaxMustermann2 5a61d4e
Merge branch 'develop'
MaxMustermann2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
syntax = "proto3"; | ||
|
||
package exocore.delegation.v1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
|
||
import "exocore/delegation/v1/tx.proto"; | ||
|
||
option go_package = "github.com/ExocoreNetwork/exocore/x/delegation/types"; | ||
|
||
// GenesisState defines the delegation module's state. It needs to encompass | ||
// all of the state that is required to start the chain from the genesis | ||
// or in the event of a restart. At this point, it is only built with | ||
// the former in mind. There are no params in this module. | ||
message GenesisState { | ||
// delegations is a list of all delegations in the system. | ||
repeated DelegationsByStaker delegations = 1 [(gogoproto.nullable) = false]; | ||
} | ||
|
||
// DelegationsByStaker is a list of delegations for a single staker. | ||
message DelegationsByStaker { | ||
// staker_id is the staker's account address + _ + l0 chain id (hex).`` | ||
string staker_id = 1 [(gogoproto.customname) = "StakerID"]; | ||
// delegations is the list of delegations for the staker, indexed by the | ||
// asset_id. | ||
repeated DelegatedSingleAssetInfo delegations = 2 [(gogoproto.nullable) = false]; | ||
} |
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,60 @@ | ||
package keeper | ||
|
||
import ( | ||
assetstype "github.com/ExocoreNetwork/exocore/x/assets/types" | ||
delegationtype "github.com/ExocoreNetwork/exocore/x/delegation/types" | ||
abci "github.com/cometbft/cometbft/abci/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// InitGenesis initializes the module's state from a provided genesis state. | ||
// Since this action typically occurs on chain starts, this function is allowed to panic. | ||
func (k Keeper) InitGenesis( | ||
ctx sdk.Context, | ||
gs delegationtype.GenesisState, | ||
) []abci.ValidatorUpdate { | ||
// TODO(mm): is it possible to parallelize these without using goroutines? | ||
for _, level1 := range gs.Delegations { | ||
stakerID := level1.StakerID | ||
// #nosec G703 // already validated | ||
stakerAddress, lzID, _ := assetstype.ParseID(stakerID) | ||
// we have checked IsHexAddress already | ||
stakerAddressBytes := common.HexToAddress(stakerAddress) | ||
for _, level2 := range level1.Delegations { | ||
assetID := level2.AssetID | ||
// #nosec G703 // already validated | ||
assetAddress, _, _ := assetstype.ParseID(assetID) | ||
// we have checked IsHexAddress already | ||
assetAddressBytes := common.HexToAddress(assetAddress) | ||
for _, level3 := range level2.PerOperatorAmounts { | ||
operator := level3.Key | ||
wrappedAmount := level3.Value | ||
amount := wrappedAmount.Amount | ||
// #nosec G703 // already validated | ||
accAddress, _ := sdk.AccAddressFromBech32(operator) | ||
delegationParams := &delegationtype.DelegationOrUndelegationParams{ | ||
ClientChainLzID: lzID, | ||
Action: assetstype.DelegateTo, | ||
AssetsAddress: assetAddressBytes.Bytes(), | ||
OperatorAddress: accAddress, | ||
StakerAddress: stakerAddressBytes.Bytes(), | ||
OpAmount: amount, | ||
// the uninitialized members are not used in this context | ||
// they are the LzNonce and TxHash | ||
} | ||
if err := k.delegateTo(ctx, delegationParams, false); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
} | ||
return []abci.ValidatorUpdate{} | ||
} | ||
|
||
// ExportGenesis returns the module's exported genesis | ||
func (Keeper) ExportGenesis(sdk.Context) *delegationtype.GenesisState { | ||
genesis := delegationtype.DefaultGenesis() | ||
// TODO | ||
return genesis | ||
} |
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,20 @@ | ||
package types | ||
|
||
import ( | ||
sdkmath "cosmossdk.io/math" | ||
assetstype "github.com/ExocoreNetwork/exocore/x/assets/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type DelegationOrUndelegationParams struct { | ||
ClientChainLzID uint64 | ||
Action assetstype.CrossChainOpType | ||
AssetsAddress []byte | ||
OperatorAddress sdk.AccAddress | ||
StakerAddress []byte | ||
OpAmount sdkmath.Int | ||
LzNonce uint64 | ||
TxHash common.Hash | ||
// todo: The operator approved signature might be needed here in future | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the genesis state contains a lot of delegate information, the current nested loop may affect the performance of the initialization. Consider whether it is possible to improve efficiency by optimizing data structures or parallel processing.
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.
1. It is not trivial to build something like this because the order ofgoroutine
execution is not guaranteed. This can result in inconsistency across nodes in calculating the state root, since it depends on the order of insertion.2. Within
delegateTo
, there are 6 different function calls, only 4 of which can be batched either on the staker + asset level or on the operator + asset level. Others cannot be batched, hence, batching is not necessarily possible.I have added a TODO for this.