-
Notifications
You must be signed in to change notification settings - Fork 83
RandomBeacon contract for the Beacon Ethereum chain - sortition functions #3067
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
012e362
Instantiate RandomBeacon contract for the Beacon Ethereum chain
pdyraga a3ba22a
Accept single instance of Ethereum chain when creating application ch…
pdyraga fa75502
Global Connect function to create Beacon and TBTC Ethereum chains
pdyraga 388d89f
Dial ethereum inside of ethereum.Connect
pdyraga 8e9602d
sortition.Chain implemented by ethereum.BeaconChain
pdyraga 3b34f3b
Added documentation to Go chain sortition-pool-related functions
pdyraga c2a71f9
Merge branch 'main' into sortition-chain
pdyraga 646b5d7
Use parent context in ethereum.newChain
pdyraga 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 hidden or 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 |
|---|---|---|
| @@ -1,30 +1,154 @@ | ||
| package ethereum | ||
|
|
||
| import ( | ||
| "context" | ||
| "bytes" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/keep-network/keep-common/pkg/chain/ethereum" | ||
| "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" | ||
| "github.com/keep-network/keep-core/pkg/chain" | ||
| "github.com/keep-network/keep-core/pkg/chain/random-beacon/gen/contract" | ||
| ) | ||
|
|
||
| // Definitions of contract names. | ||
| const ( | ||
| RandomBeaconContractName = "RandomBeacon" | ||
| ) | ||
|
|
||
| // BeaconChain represents a beacon-specific chain handle. | ||
| type BeaconChain struct { | ||
| *Chain | ||
|
|
||
| randomBeacon *contract.RandomBeacon | ||
| sortitionPool *contract.SortitionPool | ||
| } | ||
|
|
||
| // NewBeaconChain construct a new instance of the beacon-specific Ethereum | ||
| // newBeaconChain construct a new instance of the beacon-specific Ethereum | ||
| // chain handle. | ||
| func NewBeaconChain( | ||
| ctx context.Context, | ||
| config *ethereum.Config, | ||
| client ethutil.EthereumClient, | ||
| func newBeaconChain( | ||
| config ethereum.Config, | ||
| baseChain *Chain, | ||
| ) (*BeaconChain, error) { | ||
| chain, err := NewChain(ctx, config, client) | ||
| randomBeaconAddress, err := config.ContractAddress(RandomBeaconContractName) | ||
| if err != nil { | ||
| return nil, fmt.Errorf( | ||
| "failed to resolve %s contract address: [%v]", | ||
| RandomBeaconContractName, | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| randomBeacon, err := | ||
| contract.NewRandomBeacon( | ||
| randomBeaconAddress, | ||
| baseChain.chainID, | ||
| baseChain.key, | ||
| baseChain.client, | ||
| baseChain.nonceManager, | ||
| baseChain.miningWaiter, | ||
| baseChain.blockCounter, | ||
| baseChain.transactionMutex, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf( | ||
| "failed to attach to RandomBeacon contract: [%v]", | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| sortitionPoolAddress, err := randomBeacon.SortitionPool() | ||
| if err != nil { | ||
| return nil, fmt.Errorf( | ||
| "failed to get sortition pool address: [%v]", | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| sortitionPool, err := | ||
| contract.NewSortitionPool( | ||
| sortitionPoolAddress, | ||
| baseChain.chainID, | ||
| baseChain.key, | ||
| baseChain.client, | ||
| baseChain.nonceManager, | ||
| baseChain.miningWaiter, | ||
| baseChain.blockCounter, | ||
| baseChain.transactionMutex, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot create base chain handle: [%v]", err) | ||
| return nil, fmt.Errorf( | ||
| "failed to attach to SortitionPool contract: [%v]", | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| return &BeaconChain{ | ||
| Chain: chain, | ||
| Chain: baseChain, | ||
| randomBeacon: randomBeacon, | ||
| sortitionPool: sortitionPool, | ||
| }, nil | ||
| } | ||
|
|
||
| // OperatorToStakingProvider returns the staking provider address for the | ||
| // current operator. If the staking provider has not been registered for the | ||
| // operator, the returned address is empty and the boolean flag is set to false | ||
| // If the staking provider has been registered, the address is not empty and the | ||
| // boolean flag indicates true. | ||
| func (bc *BeaconChain) OperatorToStakingProvider() (chain.Address, bool, error) { | ||
| stakingProvider, err := bc.randomBeacon.OperatorToStakingProvider(bc.key.Address) | ||
| if err != nil { | ||
| return "", false, fmt.Errorf( | ||
| "failed to map operator %v to a staking provider: [%v]", | ||
| bc.key.Address, | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| if bytes.Equal( | ||
| stakingProvider.Bytes(), | ||
| bytes.Repeat([]byte{0}, common.AddressLength), | ||
| ) { | ||
| return "", false, nil | ||
| } | ||
|
|
||
| return chain.Address(stakingProvider.Hex()), true, nil | ||
| } | ||
|
|
||
| // EligibleStake returns the current value of the staking provider's eligible | ||
| // stake. Eligible stake is defined as the currently authorized stake minus the | ||
| // pending authorization decrease. Eligible stake is what is used for operator's | ||
| // weight in the sortition pool. If the authorized stake minus the pending | ||
| // authorization decrease is below the minimum authorization, eligible stake | ||
| // is 0. | ||
| func (bc *BeaconChain) EligibleStake(stakingProvider chain.Address) (*big.Int, error) { | ||
| eligibleStake, err := bc.randomBeacon.EligibleStake(common.HexToAddress(stakingProvider.String())) | ||
| if err != nil { | ||
| return nil, fmt.Errorf( | ||
| "failed to get eligible stake for staking provider %s: [%w]", | ||
| stakingProvider, | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| return eligibleStake, nil | ||
| } | ||
|
|
||
| // IsPoolLocked returns true if the sortition pool is locked and no state | ||
| // changes are allowed. | ||
| func (bc *BeaconChain) IsPoolLocked() (bool, error) { | ||
| return bc.sortitionPool.IsLocked() | ||
| } | ||
|
|
||
| // IsOperatorInPool returns true if the current operator is registered in the | ||
| // sortition pool. | ||
| func (bc *BeaconChain) IsOperatorInPool() (bool, error) { | ||
| return bc.randomBeacon.IsOperatorInPool(bc.key.Address) | ||
| } | ||
|
|
||
| // JoinSortitionPool executes a transaction to have the current operator join | ||
| // the sortition pool. | ||
| func (bc *BeaconChain) JoinSortitionPool() error { | ||
| _, err := bc.randomBeacon.JoinSortitionPool() | ||
| return err | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,30 +1,17 @@ | ||
| package ethereum | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "github.com/keep-network/keep-common/pkg/chain/ethereum" | ||
| "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" | ||
| ) | ||
|
|
||
| // TbtcChain represents a TBTC-specific chain handle. | ||
| type TbtcChain struct { | ||
| *Chain | ||
| } | ||
|
|
||
| // NewTbtcChain construct a new instance of the TBTC-specific Ethereum | ||
| // chain handle. | ||
| func NewTbtcChain( | ||
| ctx context.Context, | ||
| config *ethereum.Config, | ||
| client ethutil.EthereumClient, | ||
| func newTbtcChain( | ||
| baseChain *Chain, | ||
| ) (*TbtcChain, error) { | ||
| chain, err := NewChain(ctx, config, client) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot create base chain handle: [%v]", err) | ||
| } | ||
|
|
||
| return &TbtcChain{ | ||
| Chain: chain, | ||
| Chain: baseChain, | ||
| }, nil | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.