-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Feature: StakeHub Contract Interface Implementation #3045
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
Changes from all commits
0f51ae6
0f2dcf7
e098e5e
da8bdb9
7883bf6
259fc7c
0bd3ac5
619dfc0
f4f3a40
5f6f7fc
e651fe4
5f66db7
7bcb02e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,224 @@ | ||||||||||
| package parlia | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "context" | ||||||||||
| "fmt" | ||||||||||
| "math" | ||||||||||
| "math/big" | ||||||||||
|
|
||||||||||
| "github.com/ethereum/go-ethereum/accounts" | ||||||||||
| "github.com/ethereum/go-ethereum/common" | ||||||||||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||||||||||
| "github.com/ethereum/go-ethereum/core/systemcontracts" | ||||||||||
| "github.com/ethereum/go-ethereum/core/types" | ||||||||||
| "github.com/ethereum/go-ethereum/internal/ethapi" | ||||||||||
| "github.com/ethereum/go-ethereum/log" | ||||||||||
| "github.com/ethereum/go-ethereum/p2p/enode" | ||||||||||
| "github.com/ethereum/go-ethereum/rpc" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // GetValidators retrieves validators from the StakeHubContract | ||||||||||
| // It returns operator addresses, credit addresses, and total length of validators | ||||||||||
| func (p *Parlia) GetValidators(blockNumber uint64, offset, limit *big.Int) ([]common.Address, []common.Address, *big.Int, error) { | ||||||||||
| log.Debug("Getting validators", "block", blockNumber, "offset", offset, "limit", limit) | ||||||||||
|
|
||||||||||
| // Create the call data for getValidators | ||||||||||
| data, err := p.stakeHubABI.Pack("getValidators", offset, limit) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to pack getValidators", "error", err) | ||||||||||
| return nil, nil, nil, fmt.Errorf("failed to pack getValidators: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Make the call | ||||||||||
| blockNr := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(blockNumber)) | ||||||||||
| msgData := (hexutil.Bytes)(data) | ||||||||||
| toAddress := common.HexToAddress(systemcontracts.StakeHubContract) | ||||||||||
| gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) | ||||||||||
|
|
||||||||||
| log.Debug("Calling getValidators", "block", blockNumber, "to", toAddress) | ||||||||||
| result, err := p.ethAPI.Call(context.Background(), ethapi.TransactionArgs{ | ||||||||||
| Gas: &gas, | ||||||||||
| To: &toAddress, | ||||||||||
| Data: &msgData, | ||||||||||
| }, &blockNr, nil, nil) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to call getValidators", "error", err) | ||||||||||
| return nil, nil, nil, fmt.Errorf("failed to call getValidators: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Unpack the result | ||||||||||
| var operatorAddrs []common.Address | ||||||||||
| var creditAddrs []common.Address | ||||||||||
| var totalLength *big.Int | ||||||||||
| if err := p.stakeHubABI.UnpackIntoInterface(&[]interface{}{&operatorAddrs, &creditAddrs, &totalLength}, "getValidators", result); err != nil { | ||||||||||
| log.Error("Failed to unpack getValidators result", "error", err) | ||||||||||
| return nil, nil, nil, fmt.Errorf("failed to unpack getValidators result: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| log.Debug("Successfully retrieved validators", "operators", len(operatorAddrs), "credits", len(creditAddrs), "total", totalLength) | ||||||||||
| return operatorAddrs, creditAddrs, totalLength, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // getNodeIDsForValidators retrieves node IDs for the given validators | ||||||||||
| // It returns a map of consensus addresses to their node IDs | ||||||||||
| func (p *Parlia) getNodeIDsForValidators(blockNumber uint64, validatorsToQuery []common.Address) (map[common.Address][]enode.ID, error) { | ||||||||||
| log.Debug("Listing node IDs for validators", "block", blockNumber, "validators", len(validatorsToQuery)) | ||||||||||
|
|
||||||||||
| // Create the call data for getNodeIDs | ||||||||||
| data, err := p.stakeHubABI.Pack("getNodeIDs", validatorsToQuery) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to pack getNodeIDs", "error", err) | ||||||||||
| return nil, fmt.Errorf("failed to pack getNodeIDs: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Make the call | ||||||||||
| blockNr := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(blockNumber)) | ||||||||||
| msgData := (hexutil.Bytes)(data) | ||||||||||
| toAddress := common.HexToAddress(systemcontracts.StakeHubContract) | ||||||||||
| gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) | ||||||||||
|
|
||||||||||
| log.Debug("Calling getNodeIDs", "block", blockNumber, "to", toAddress) | ||||||||||
| result, err := p.ethAPI.Call(context.Background(), ethapi.TransactionArgs{ | ||||||||||
| Gas: &gas, | ||||||||||
| To: &toAddress, | ||||||||||
| Data: &msgData, | ||||||||||
| }, &blockNr, nil, nil) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to call getNodeIDs", "error", err) | ||||||||||
| return nil, fmt.Errorf("failed to call getNodeIDs: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Unpack the result | ||||||||||
| var consensusAddresses []common.Address | ||||||||||
| var nodeIDsList [][]enode.ID | ||||||||||
| if err := p.stakeHubABI.UnpackIntoInterface(&[]interface{}{&consensusAddresses, &nodeIDsList}, "getNodeIDs", result); err != nil { | ||||||||||
| log.Error("Failed to unpack getNodeIDs result", "error", err) | ||||||||||
| return nil, fmt.Errorf("failed to unpack getNodeIDs result: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Create a map of addresses to node IDs | ||||||||||
| addressToNodeIDs := make(map[common.Address][]enode.ID) | ||||||||||
| for i, addr := range consensusAddresses { | ||||||||||
| if i < len(nodeIDsList) { | ||||||||||
| addressToNodeIDs[addr] = nodeIDsList[i] | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| log.Debug("Successfully retrieved node IDs", "addresses", len(addressToNodeIDs)) | ||||||||||
| return addressToNodeIDs, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // GetNodeIDs returns a flattened array of all node IDs for current validators | ||||||||||
| func (p *Parlia) GetNodeIDs() ([]enode.ID, error) { | ||||||||||
| // Get latest block number | ||||||||||
| block := p.ethAPI.BlockNumber() | ||||||||||
| log.Debug("Getting all node IDs", "block", block) | ||||||||||
|
|
||||||||||
| // Call GetValidators with latest block number | ||||||||||
| operatorAddrs, _, _, err := p.GetValidators(uint64(block), big.NewInt(0), big.NewInt(1000)) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to get validators", "error", err) | ||||||||||
| return nil, fmt.Errorf("failed to get validators: %v", err) | ||||||||||
| } | ||||||||||
| log.Debug("Retrieved validators", "count", len(operatorAddrs)) | ||||||||||
|
|
||||||||||
| // Get node IDs for validators | ||||||||||
| nodeIDs, err := p.getNodeIDsForValidators(uint64(block), operatorAddrs) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to get node IDs", "error", err) | ||||||||||
| return nil, fmt.Errorf("failed to get node IDs: %v", err) | ||||||||||
| } | ||||||||||
| log.Debug("Retrieved node IDs map", "addresses", len(nodeIDs)) | ||||||||||
|
|
||||||||||
| // Flatten the array of arrays into a single array | ||||||||||
| flatNodeIDs := make([]enode.ID, 0) | ||||||||||
| for addr, nodeIDArray := range nodeIDs { | ||||||||||
| flatNodeIDs = append(flatNodeIDs, nodeIDArray...) | ||||||||||
| log.Debug("Processing node IDs", "address", addr, "count", len(nodeIDArray)) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| log.Debug("Successfully flattened node IDs", "total", len(flatNodeIDs)) | ||||||||||
| return flatNodeIDs, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // AddNodeIDs creates a signed transaction to add node IDs to the StakeHub contract | ||||||||||
| func (p *Parlia) AddNodeIDs(nodeIDs []enode.ID, nonce uint64) (*types.Transaction, error) { | ||||||||||
| log.Debug("Adding node IDs", "count", len(nodeIDs), "nonce", nonce) | ||||||||||
|
|
||||||||||
| p.lock.RLock() | ||||||||||
| signTxFn := p.signTxFn | ||||||||||
| val := p.val | ||||||||||
| p.lock.RUnlock() | ||||||||||
|
|
||||||||||
| if signTxFn == nil { | ||||||||||
| log.Error("Signing function not set") | ||||||||||
| return nil, fmt.Errorf("signing function not set, call Authorize first") | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Create the call data for addNodeIDs | ||||||||||
| data, err := p.stakeHubABI.Pack("addNodeIDs", nodeIDs) | ||||||||||
| if err != nil { | ||||||||||
| log.Error("Failed to pack addNodeIDs", "error", err) | ||||||||||
| return nil, fmt.Errorf("failed to pack addNodeIDs: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| to := common.HexToAddress(systemcontracts.StakeHubContract) | ||||||||||
| hexData := hexutil.Bytes(data) | ||||||||||
| hexNonce := hexutil.Uint64(nonce) | ||||||||||
| gas, err := p.ethAPI.EstimateGas(context.Background(), ethapi.TransactionArgs{ | ||||||||||
|
||||||||||
| gas, err := p.ethAPI.EstimateGas(context.Background(), ethapi.TransactionArgs{ | |
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | |
| defer cancel() | |
| gas, err := p.ethAPI.EstimateGas(ctx, ethapi.TransactionArgs{ |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package maxwell | ||
|
|
||
| import _ "embed" | ||
|
|
||
| // contract codes for Mainnet upgrade | ||
| var ( | ||
| //go:embed mainnet/StakeHubContract | ||
| MainnetStakeHubContract string | ||
| ) | ||
|
|
||
| // contract codes for Chapel upgrade | ||
| var ( | ||
| //go:embed chapel/StakeHubContract | ||
| ChapelStakeHubContract string | ||
| ) | ||
|
|
||
| // contract codes for Rialto upgrade | ||
| var ( | ||
| //go:embed rialto/StakeHubContract | ||
| RialtoStakeHubContract string | ||
| ) |
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.