-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathavs_reader.go
More file actions
148 lines (121 loc) · 5.34 KB
/
avs_reader.go
File metadata and controls
148 lines (121 loc) · 5.34 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package chainio
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
servicemanager "github.com/yetanotherco/aligned_layer/contracts/bindings/AlignedLayerServiceManager"
contractERC20Mock "github.com/yetanotherco/aligned_layer/contracts/bindings/ERC20Mock"
"github.com/yetanotherco/aligned_layer/core/config"
"github.com/Layr-Labs/eigensdk-go/chainio/clients"
sdkavsregistry "github.com/Layr-Labs/eigensdk-go/chainio/clients/avsregistry"
"github.com/Layr-Labs/eigensdk-go/logging"
)
type AvsReader struct {
*sdkavsregistry.ChainReader
AvsContractBindings *AvsServiceBindings
AlignedLayerServiceManagerAddr ethcommon.Address
logger logging.Logger
}
func NewAvsReaderFromConfig(baseConfig *config.BaseConfig, ecdsaConfig *config.EcdsaConfig) (*AvsReader, error) {
buildAllConfig := clients.BuildAllConfig{
EthHttpUrl: baseConfig.EthRpcUrl,
EthWsUrl: baseConfig.EthWsUrl,
RegistryCoordinatorAddr: baseConfig.AlignedLayerDeploymentConfig.AlignedLayerRegistryCoordinatorAddr.String(),
OperatorStateRetrieverAddr: baseConfig.AlignedLayerDeploymentConfig.AlignedLayerOperatorStateRetrieverAddr.String(),
AvsName: "AlignedLayer",
PromMetricsIpPortAddress: baseConfig.EigenMetricsIpPortAddress,
}
clients, err := clients.BuildAll(buildAllConfig, ecdsaConfig.PrivateKey, baseConfig.Logger)
if err != nil {
return nil, err
}
chainReader := clients.AvsRegistryChainReader
avsServiceBindings, err := NewAvsServiceBindings(baseConfig.AlignedLayerDeploymentConfig.AlignedLayerServiceManagerAddr, baseConfig.AlignedLayerDeploymentConfig.AlignedLayerOperatorStateRetrieverAddr, baseConfig.EthRpcClient, baseConfig.EthRpcClientFallback, baseConfig.Logger)
if err != nil {
return nil, err
}
return &AvsReader{
ChainReader: chainReader,
AvsContractBindings: avsServiceBindings,
AlignedLayerServiceManagerAddr: baseConfig.AlignedLayerDeploymentConfig.AlignedLayerServiceManagerAddr,
logger: baseConfig.Logger,
}, nil
}
func (r *AvsReader) GetErc20Mock(tokenAddr ethcommon.Address) (*contractERC20Mock.ContractERC20Mock, error) {
erc20Mock, err := contractERC20Mock.NewContractERC20Mock(tokenAddr, &r.AvsContractBindings.ethClient)
if err != nil {
// Retry with fallback client
erc20Mock, err = contractERC20Mock.NewContractERC20Mock(tokenAddr, &r.AvsContractBindings.ethClientFallback)
if err != nil {
r.logger.Error("Failed to fetch ERC20Mock contract", "err", err)
}
}
return erc20Mock, nil
}
func (r *AvsReader) DisabledVerifiers() (*big.Int, error) {
return r.AvsContractBindings.ServiceManager.ContractAlignedLayerServiceManagerCaller.DisabledVerifiers(&bind.CallOpts{})
}
// Returns all the "NewBatchV3" logs that have not been responded starting from the given block number
func (r *AvsReader) GetNotRespondedTasksFrom(fromBlock uint64) ([]servicemanager.ContractAlignedLayerServiceManagerNewBatchV3, error) {
logs, err := r.AvsContractBindings.ServiceManager.FilterNewBatchV3(&bind.FilterOpts{Start: fromBlock, End: nil, Context: context.Background()}, nil)
if err != nil {
return nil, err
}
var tasks []servicemanager.ContractAlignedLayerServiceManagerNewBatchV3
for logs.Next() {
task, err := r.AvsContractBindings.ServiceManager.ParseNewBatchV3(logs.Event.Raw)
if err != nil {
return nil, err
}
// now check if its finalized or not before appending
batchIdentifier := append(task.BatchMerkleRoot[:], task.SenderAddress[:]...)
batchIdentifierHash := *(*[32]byte)(crypto.Keccak256(batchIdentifier))
state, err := r.AvsContractBindings.ServiceManager.ContractAlignedLayerServiceManagerCaller.BatchesState(nil, batchIdentifierHash)
if err != nil {
return nil, err
}
// append the task if not responded yet
if !state.Responded {
tasks = append(tasks, *task)
}
}
return tasks, nil
}
// This function is a helper to get a task hash of aproximately nBlocksOld blocks ago
func (r *AvsReader) GetOldTaskHash(nBlocksOld uint64, interval uint64) (*[32]byte, error) {
latestBlock, err := r.AvsContractBindings.ethClient.BlockNumber(context.Background())
if err != nil {
latestBlock, err = r.AvsContractBindings.ethClientFallback.BlockNumber(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get latest block number: %w", err)
}
}
if latestBlock < nBlocksOld {
return nil, fmt.Errorf("latest block is less than nBlocksOld")
}
// Define block number limits to query the rpc
var fromBlock uint64
toBlock := latestBlock - nBlocksOld
fromBlock = toBlock - interval
logs, err := r.AvsContractBindings.ServiceManager.FilterNewBatchV3(&bind.FilterOpts{Start: fromBlock, End: &toBlock, Context: context.Background()}, nil)
if err != nil {
return nil, err
}
if err := logs.Error(); err != nil {
return nil, err
}
if !logs.Next() {
return nil, nil //not an error, but no tasks found
}
// Any log from the list is good enough.
task, err := r.AvsContractBindings.ServiceManager.ParseNewBatchV3(logs.Event.Raw)
if err != nil {
return nil, err
}
batchIdentifier := append(task.BatchMerkleRoot[:], task.SenderAddress[:]...)
batchIdentifierHash := *(*[32]byte)(crypto.Keccak256(batchIdentifier))
return &batchIdentifierHash, nil
}