-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathstate.go
91 lines (72 loc) · 2.31 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package eth
import (
"context"
"math/big"
"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/core"
)
type ChainState struct {
Client common.EthClient
Tx core.Reader
}
func NewChainState(tx core.Reader, client common.EthClient) *ChainState {
return &ChainState{
Client: client,
Tx: tx,
}
}
var _ core.ChainState = (*ChainState)(nil)
func (cs *ChainState) GetOperatorStateByOperator(ctx context.Context, blockNumber uint, operator core.OperatorID) (*core.OperatorState, error) {
operatorsByQuorum, _, err := cs.Tx.GetOperatorStakes(ctx, operator, uint32(blockNumber))
if err != nil {
return nil, err
}
return getOperatorState(operatorsByQuorum, uint32(blockNumber))
}
func (cs *ChainState) GetOperatorState(ctx context.Context, blockNumber uint, quorums []core.QuorumID) (*core.OperatorState, error) {
operatorsByQuorum, err := cs.Tx.GetOperatorStakesForQuorums(ctx, quorums, uint32(blockNumber))
if err != nil {
return nil, err
}
return getOperatorState(operatorsByQuorum, uint32(blockNumber))
}
func (cs *ChainState) GetCurrentBlockNumber() (uint, error) {
ctx := context.Background()
header, err := cs.Client.HeaderByNumber(ctx, nil)
if err != nil {
return 0, err
}
return uint(header.Number.Uint64()), nil
}
func (cs *ChainState) GetOperatorSocket(ctx context.Context, blockNumber uint, operator core.OperatorID) (string, error) {
socket, err := cs.Tx.GetOperatorSocket(ctx, operator)
if err != nil {
return "", err
}
return socket, nil
}
func getOperatorState(operatorsByQuorum core.OperatorStakes, blockNumber uint32) (*core.OperatorState, error) {
operators := make(map[core.QuorumID]map[core.OperatorID]*core.OperatorInfo)
totals := make(map[core.QuorumID]*core.OperatorInfo)
for quorumID, quorum := range operatorsByQuorum {
totalStake := big.NewInt(0)
operators[quorumID] = make(map[core.OperatorID]*core.OperatorInfo)
for ind, op := range quorum {
operators[quorumID][op.OperatorID] = &core.OperatorInfo{
Stake: op.Stake,
Index: core.OperatorIndex(ind),
}
totalStake.Add(totalStake, op.Stake)
}
totals[quorumID] = &core.OperatorInfo{
Stake: totalStake,
Index: core.OperatorIndex(len(quorum)),
}
}
state := &core.OperatorState{
Operators: operators,
Totals: totals,
BlockNumber: uint(blockNumber),
}
return state, nil
}