-
Notifications
You must be signed in to change notification settings - Fork 179
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
Add stake percentage + operator address to ejection reporting (dataapi + cli) #815
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,9 @@ | ||
build: clean | ||
go mod tidy | ||
go build -o ./bin/ejections ./cmd | ||
|
||
clean: | ||
rm -rf ./bin | ||
|
||
run: build | ||
./bin/ejections --help |
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,231 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/Layr-Labs/eigenda/common" | ||
"github.com/Layr-Labs/eigenda/common/geth" | ||
"github.com/Layr-Labs/eigenda/core" | ||
"github.com/Layr-Labs/eigenda/core/eth" | ||
"github.com/Layr-Labs/eigenda/disperser/dataapi" | ||
"github.com/Layr-Labs/eigenda/disperser/dataapi/subgraph" | ||
"github.com/Layr-Labs/eigenda/tools/ejections" | ||
"github.com/Layr-Labs/eigenda/tools/ejections/flags" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/jedib0t/go-pretty/v6/text" | ||
"github.com/urfave/cli" | ||
|
||
gethcommon "github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
var ( | ||
version = "1.0.0" | ||
gitCommit = "" | ||
gitDate = "" | ||
) | ||
|
||
type EjectionTransaction struct { | ||
BlockNumber uint64 `json:"block_number"` | ||
BlockTimestamp string `json:"block_timestamp"` | ||
TransactionHash string `json:"transaction_hash"` | ||
QuorumStakePercentage map[uint8]float64 `json:"stake_percentage"` | ||
QuorumEjections map[uint8]uint8 `json:"ejections"` | ||
} | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Version = fmt.Sprintf("%s,%s,%s", version, gitCommit, gitDate) | ||
app.Name = "ejections report" | ||
app.Description = "operator ejections report" | ||
app.Usage = "" | ||
app.Flags = flags.Flags | ||
app.Action = RunScan | ||
if err := app.Run(os.Args); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func RunScan(ctx *cli.Context) error { | ||
config, err := ejections.NewConfig(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger, err := common.NewLogger(config.LoggerConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, err := geth.NewMultiHomingClient(config.EthClientConfig, gethcommon.Address{}, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tx, err := eth.NewReader(logger, client, config.BLSOperatorStateRetrieverAddr, config.EigenDAServiceManagerAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
chainState := eth.NewChainState(tx, client) | ||
if chainState == nil { | ||
return errors.New("failed to create chain state") | ||
} | ||
subgraphApi := subgraph.NewApi(config.SubgraphEndpoint, config.SubgraphEndpoint) | ||
subgraphClient := dataapi.NewSubgraphClient(subgraphApi, logger) | ||
|
||
ejections, err := subgraphClient.QueryOperatorEjectionsForTimeWindow(context.Background(), int32(config.Days), config.OperatorId, config.First, config.Skip) | ||
if err != nil { | ||
logger.Warn("failed to fetch operator ejections", "operatorId", config.OperatorId, "error", err) | ||
return errors.New("operator ejections not found") | ||
} | ||
|
||
sort.Slice(ejections, func(i, j int) bool { | ||
return ejections[i].BlockTimestamp > ejections[j].BlockTimestamp | ||
}) | ||
|
||
// Create a sorted slice from the set of quorums | ||
quorumSet := make(map[uint8]struct{}) | ||
for _, ejection := range ejections { | ||
quorumSet[ejection.Quorum] = struct{}{} | ||
} | ||
quorums := make([]uint8, 0, len(quorumSet)) | ||
for quorum := range quorumSet { | ||
quorums = append(quorums, quorum) | ||
} | ||
sort.Slice(quorums, func(i, j int) bool { | ||
return quorums[i] < quorums[j] | ||
}) | ||
|
||
stateCache := make(map[uint64]*core.OperatorState) | ||
ejectedOperatorIds := make(map[core.OperatorID]struct{}) | ||
for _, ejection := range ejections { | ||
previouseBlock := ejection.BlockNumber - 1 | ||
if _, exists := stateCache[previouseBlock]; !exists { | ||
state, err := chainState.GetOperatorState(context.Background(), uint(previouseBlock), quorums) | ||
if err != nil { | ||
return err | ||
} | ||
stateCache[previouseBlock] = state | ||
} | ||
|
||
// construct a set of ejected operator ids for later batch address lookup | ||
opID, err := core.OperatorIDFromHex(ejection.OperatorId) | ||
if err != nil { | ||
return err | ||
} | ||
ejectedOperatorIds[opID] = struct{}{} | ||
} | ||
|
||
// resolve operator id to operator addresses mapping | ||
operatorIDs := make([]core.OperatorID, 0, len(ejectedOperatorIds)) | ||
for opID := range ejectedOperatorIds { | ||
operatorIDs = append(operatorIDs, opID) | ||
} | ||
operatorAddresses, err := tx.BatchOperatorIDToAddress(context.Background(), operatorIDs) | ||
if err != nil { | ||
return err | ||
} | ||
operatorIdToAddress := make(map[string]string) | ||
for i := range operatorAddresses { | ||
operatorIdToAddress["0x"+operatorIDs[i].Hex()] = strings.ToLower(operatorAddresses[i].Hex()) | ||
} | ||
|
||
rowConfigAutoMerge := table.RowConfig{AutoMerge: true} | ||
rowConfigNoAutoMerge := table.RowConfig{AutoMerge: false} | ||
operators := table.NewWriter() | ||
operators.AppendHeader(table.Row{"Operator Address", "Quorum", "Stake %", "Timestamp", "Txn"}, rowConfigAutoMerge) | ||
txns := table.NewWriter() | ||
txns.AppendHeader(table.Row{"Txn", "Timestamp", "Operator Address", "Quorum", "Stake %"}, rowConfigAutoMerge) | ||
txnQuorums := table.NewWriter() | ||
txnQuorums.AppendHeader(table.Row{"Txn", "Timestamp", "Quorum", "Stake %", "Operators"}, rowConfigNoAutoMerge) | ||
|
||
ejectionTransactions := make(map[string]*EjectionTransaction) | ||
for _, ejection := range ejections { | ||
state := stateCache[ejection.BlockNumber-1] | ||
opID, err := core.OperatorIDFromHex(ejection.OperatorId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stakePercentage := float64(0) | ||
if stake, ok := state.Operators[ejection.Quorum][opID]; ok { | ||
totalStake := new(big.Float).SetInt(state.Totals[ejection.Quorum].Stake) | ||
operatorStake := new(big.Float).SetInt(stake.Stake) | ||
stakePercentage, _ = new(big.Float).Mul(big.NewFloat(100), new(big.Float).Quo(operatorStake, totalStake)).Float64() | ||
} | ||
|
||
if _, exists := ejectionTransactions[ejection.TransactionHash]; !exists { | ||
ejectionTransactions[ejection.TransactionHash] = &EjectionTransaction{ | ||
BlockNumber: ejection.BlockNumber, | ||
BlockTimestamp: ejection.BlockTimestamp, | ||
TransactionHash: ejection.TransactionHash, | ||
QuorumStakePercentage: make(map[uint8]float64), | ||
QuorumEjections: make(map[uint8]uint8), | ||
} | ||
ejectionTransactions[ejection.TransactionHash].QuorumStakePercentage[ejection.Quorum] = stakePercentage | ||
ejectionTransactions[ejection.TransactionHash].QuorumEjections[ejection.Quorum] = 1 | ||
} else { | ||
ejectionTransactions[ejection.TransactionHash].QuorumStakePercentage[ejection.Quorum] += stakePercentage | ||
ejectionTransactions[ejection.TransactionHash].QuorumEjections[ejection.Quorum] += 1 | ||
} | ||
|
||
operatorAddress := operatorIdToAddress[ejection.OperatorId] | ||
operators.AppendRow(table.Row{operatorAddress, ejection.Quorum, stakePercentage, ejection.BlockTimestamp, ejection.TransactionHash}, rowConfigAutoMerge) | ||
txns.AppendRow(table.Row{ejection.TransactionHash, ejection.BlockTimestamp, operatorAddress, ejection.Quorum, stakePercentage}, rowConfigAutoMerge) | ||
} | ||
|
||
orderedEjectionTransactions := make([]*EjectionTransaction, 0, len(ejectionTransactions)) | ||
for _, txn := range ejectionTransactions { | ||
orderedEjectionTransactions = append(orderedEjectionTransactions, txn) | ||
} | ||
sort.Slice(orderedEjectionTransactions, func(i, j int) bool { | ||
return orderedEjectionTransactions[i].BlockNumber > orderedEjectionTransactions[j].BlockNumber | ||
}) | ||
for _, txn := range orderedEjectionTransactions { | ||
for _, quorum := range quorums { | ||
if _, exists := txn.QuorumEjections[quorum]; exists { | ||
txnQuorums.AppendRow(table.Row{txn.TransactionHash, txn.BlockTimestamp, quorum, txn.QuorumStakePercentage[quorum], txn.QuorumEjections[quorum]}, rowConfigAutoMerge) | ||
} | ||
} | ||
} | ||
|
||
operators.SetAutoIndex(true) | ||
operators.SetColumnConfigs([]table.ColumnConfig{ | ||
{Number: 1, AutoMerge: true}, | ||
{Number: 2, Align: text.AlignCenter}, | ||
}) | ||
operators.SetStyle(table.StyleLight) | ||
operators.Style().Options.SeparateRows = true | ||
|
||
txns.SetAutoIndex(true) | ||
txns.SetColumnConfigs([]table.ColumnConfig{ | ||
{Number: 1, AutoMerge: true}, | ||
{Number: 2, AutoMerge: true}, | ||
{Number: 3, AutoMerge: true}, | ||
{Number: 4, Align: text.AlignCenter}, | ||
}) | ||
txns.SetStyle(table.StyleLight) | ||
txns.Style().Options.SeparateRows = true | ||
|
||
txnQuorums.SetAutoIndex(true) | ||
txnQuorums.SetColumnConfigs([]table.ColumnConfig{ | ||
{Number: 1, AutoMerge: true}, | ||
{Number: 2, AutoMerge: true, Align: text.AlignCenter}, | ||
{Number: 3, Align: text.AlignCenter}, | ||
{Number: 5, Align: text.AlignCenter}, | ||
}) | ||
txnQuorums.SetStyle(table.StyleLight) | ||
txnQuorums.Style().Options.SeparateRows = true | ||
|
||
fmt.Println(operators.Render()) | ||
fmt.Println(txns.Render()) | ||
fmt.Println(txnQuorums.Render()) | ||
return nil | ||
} |
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.
looks like this section of code is highly similar to the codeblock above. Create a new function?