2023-03-20 16:32:32 +00:00
package beacon_api
import (
"context"
"net/http"
2023-03-28 11:36:41 +00:00
"strconv"
2023-03-20 16:32:32 +00:00
"time"
2023-03-28 11:36:41 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
2023-03-20 16:32:32 +00:00
"github.com/golang/protobuf/ptypes/empty"
2023-03-28 11:36:41 +00:00
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/apimiddleware"
2023-03-20 16:32:32 +00:00
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/validator/client/iface"
2023-03-28 11:36:41 +00:00
"google.golang.org/protobuf/types/known/timestamppb"
2023-03-20 16:32:32 +00:00
)
type beaconApiNodeClient struct {
fallbackClient iface . NodeClient
jsonRestHandler jsonRestHandler
2023-03-28 11:36:41 +00:00
genesisProvider genesisProvider
2023-03-20 16:32:32 +00:00
}
2023-03-29 13:38:31 +00:00
func ( c * beaconApiNodeClient ) GetSyncStatus ( ctx context . Context , _ * empty . Empty ) ( * ethpb . SyncStatus , error ) {
syncingResponse := apimiddleware . SyncingResponseJson { }
if _ , err := c . jsonRestHandler . GetRestJsonResponse ( ctx , "/eth/v1/node/syncing" , & syncingResponse ) ; err != nil {
return nil , errors . Wrap ( err , "failed to get sync status" )
2023-03-20 16:32:32 +00:00
}
2023-03-29 13:38:31 +00:00
if syncingResponse . Data == nil {
return nil , errors . New ( "syncing data is nil" )
}
return & ethpb . SyncStatus {
Syncing : syncingResponse . Data . IsSyncing ,
} , nil
2023-03-20 16:32:32 +00:00
}
2023-03-28 11:36:41 +00:00
func ( c * beaconApiNodeClient ) GetGenesis ( ctx context . Context , _ * empty . Empty ) ( * ethpb . Genesis , error ) {
genesisJson , _ , err := c . genesisProvider . GetGenesis ( ctx )
if err != nil {
return nil , errors . Wrap ( err , "failed to get genesis" )
2023-03-20 16:32:32 +00:00
}
2023-03-28 11:36:41 +00:00
genesisValidatorRoot , err := hexutil . Decode ( genesisJson . GenesisValidatorsRoot )
if err != nil {
return nil , errors . Wrapf ( err , "failed to decode genesis validator root `%s`" , genesisJson . GenesisValidatorsRoot )
}
genesisTime , err := strconv . ParseInt ( genesisJson . GenesisTime , 10 , 64 )
if err != nil {
return nil , errors . Wrapf ( err , "failed to parse genesis time `%s`" , genesisJson . GenesisTime )
}
depositContractJson := apimiddleware . DepositContractResponseJson { }
if _ , err = c . jsonRestHandler . GetRestJsonResponse ( ctx , "/eth/v1/config/deposit_contract" , & depositContractJson ) ; err != nil {
return nil , errors . Wrapf ( err , "failed to query deposit contract information" )
}
if depositContractJson . Data == nil {
return nil , errors . New ( "deposit contract data is nil" )
}
depositContactAddress , err := hexutil . Decode ( depositContractJson . Data . Address )
if err != nil {
return nil , errors . Wrapf ( err , "failed to decode deposit contract address `%s`" , depositContractJson . Data . Address )
}
return & ethpb . Genesis {
GenesisTime : & timestamppb . Timestamp {
Seconds : genesisTime ,
} ,
DepositContractAddress : depositContactAddress ,
GenesisValidatorsRoot : genesisValidatorRoot ,
} , nil
2023-03-20 16:32:32 +00:00
}
func ( c * beaconApiNodeClient ) GetVersion ( ctx context . Context , in * empty . Empty ) ( * ethpb . Version , error ) {
if c . fallbackClient != nil {
return c . fallbackClient . GetVersion ( ctx , in )
}
// TODO: Implement me
panic ( "beaconApiNodeClient.GetVersion is not implemented. To use a fallback client, pass a fallback client as the last argument of NewBeaconApiNodeClientWithFallback." )
}
func ( c * beaconApiNodeClient ) ListPeers ( ctx context . Context , in * empty . Empty ) ( * ethpb . Peers , error ) {
if c . fallbackClient != nil {
return c . fallbackClient . ListPeers ( ctx , in )
}
// TODO: Implement me
panic ( "beaconApiNodeClient.ListPeers is not implemented. To use a fallback client, pass a fallback client as the last argument of NewBeaconApiNodeClientWithFallback." )
}
func NewNodeClientWithFallback ( host string , timeout time . Duration , fallbackClient iface . NodeClient ) iface . NodeClient {
jsonRestHandler := beaconApiJsonRestHandler {
httpClient : http . Client { Timeout : timeout } ,
host : host ,
}
return & beaconApiNodeClient {
jsonRestHandler : jsonRestHandler ,
fallbackClient : fallbackClient ,
2023-03-28 11:36:41 +00:00
genesisProvider : beaconApiGenesisProvider { jsonRestHandler : jsonRestHandler } ,
2023-03-20 16:32:32 +00:00
}
}