Skip to content

Commit

Permalink
feat: ListStakerShares rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Jan 21, 2025
1 parent 5e174b5 commit 13cdf2d
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 37 deletions.
4 changes: 3 additions & 1 deletion cmd/debugger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/Layr-Labs/sidecar/pkg/rewardsCalculatorQueue"
"github.com/Layr-Labs/sidecar/pkg/rpcServer"
"github.com/Layr-Labs/sidecar/pkg/service/protocolDataService"
"github.com/Layr-Labs/sidecar/pkg/sidecar"
pgStorage "github.com/Layr-Labs/sidecar/pkg/storage/postgres"
"log"
Expand Down Expand Up @@ -104,6 +105,7 @@ func main() {

p := pipeline.NewPipeline(fetchr, idxr, mds, sm, msm, rc, rcq, cfg, sdc, eb, l)
rps := proofs.NewRewardsProofsStore(rc, l)
pds := protocolDataService.NewProtocolDataService(grm, l, cfg)

// Create new sidecar instance
// Create new sidecar instance
Expand All @@ -114,7 +116,7 @@ func main() {
rpc := rpcServer.NewRpcServer(&rpcServer.RpcServerConfig{
GrpcPort: cfg.RpcConfig.GrpcPort,
HttpPort: cfg.RpcConfig.HttpPort,
}, mds, sm, rc, rcq, eb, rps, l)
}, mds, sm, rc, rcq, eb, rps, pds, l)

// RPC channel to notify the RPC server to shutdown gracefully
rpcChannel := make(chan bool)
Expand Down
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/Layr-Labs/sidecar/pkg/rewardsCalculatorQueue"
"github.com/Layr-Labs/sidecar/pkg/rpcServer"
"github.com/Layr-Labs/sidecar/pkg/service/protocolDataService"
"github.com/Layr-Labs/sidecar/pkg/shutdown"
"github.com/Layr-Labs/sidecar/pkg/sidecar"
pgStorage "github.com/Layr-Labs/sidecar/pkg/storage/postgres"
Expand Down Expand Up @@ -121,6 +122,8 @@ var runCmd = &cobra.Command{

rps := proofs.NewRewardsProofsStore(rc, l)

pds := protocolDataService.NewProtocolDataService(grm, l, cfg)

go rcq.Process()

p := pipeline.NewPipeline(fetchr, idxr, mds, sm, msm, rc, rcq, cfg, sdc, eb, l)
Expand All @@ -133,7 +136,7 @@ var runCmd = &cobra.Command{
rpc := rpcServer.NewRpcServer(&rpcServer.RpcServerConfig{
GrpcPort: cfg.RpcConfig.GrpcPort,
HttpPort: cfg.RpcConfig.HttpPort,
}, mds, sm, rc, rcq, eb, rps, l)
}, mds, sm, rc, rcq, eb, rps, pds, l)

// RPC channel to notify the RPC server to shutdown gracefully
rpcChannel := make(chan bool)
Expand Down
8 changes: 4 additions & 4 deletions examples/rewardsProofs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"context"
"crypto/tls"
"fmt"
v1 "github.com/Layr-Labs/protocol-apis/gen/protos/eigenlayer/sidecar/v1"
rewardsV1 "github.com/Layr-Labs/protocol-apis/gen/protos/eigenlayer/sidecar/v1/rewards"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"log"
"strings"
)

func NewSidecarClient(url string, insecureConn bool) (v1.RewardsClient, error) {
func NewSidecarClient(url string, insecureConn bool) (rewardsV1.RewardsClient, error) {
var creds grpc.DialOption
if strings.Contains(url, "localhost:") || strings.Contains(url, "127.0.0.1:") || insecureConn {
creds = grpc.WithTransportCredentials(insecure.NewCredentials())
Expand All @@ -25,7 +25,7 @@ func NewSidecarClient(url string, insecureConn bool) (v1.RewardsClient, error) {
return nil, err
}

return v1.NewRewardsClient(grpcClient), nil
return rewardsV1.NewRewardsClient(grpcClient), nil
}

func main() {
Expand All @@ -37,7 +37,7 @@ func main() {
log.Fatal(err)
}

res, err := client.GenerateClaimProof(context.Background(), &v1.GenerateClaimProofRequest{
res, err := client.GenerateClaimProof(context.Background(), &rewardsV1.GenerateClaimProofRequest{
EarnerAddress: earnerAddress,
Tokens: tokens,
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/Layr-Labs/eigenlayer-contracts v0.4.1-holesky-pepe.0.20240813143901-00fc4b95e9c1
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115220323-135176acb92b
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115230325-93c4ebccbeb7
github.com/ethereum/go-ethereum v1.14.9
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/google/uuid v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/Layr-Labs/protocol-apis v1.1.1-0.20250114181701-acb87ef4eeb5 h1:0PLxb
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250114181701-acb87ef4eeb5/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115220323-135176acb92b h1:eJmPAq3s+AwOrQUjSFXILCzUstDZobwYEraOZ2NFC1M=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115220323-135176acb92b/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115230325-93c4ebccbeb7 h1:zTOIFjJcCzOZ1PBk9jtoW/bsKSyRzvQyTG2Beutpiuk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115230325-93c4ebccbeb7/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
Expand Down
20 changes: 18 additions & 2 deletions pkg/rpcServer/protocolHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ func (rpc *RpcServer) GetDelegatedStakersForOperator(ctx context.Context, reques
}

func (rpc *RpcServer) GetStakerShares(ctx context.Context, request *protocolV1.GetStakerSharesRequest) (*protocolV1.GetStakerSharesResponse, error) {
//TODO implement me
panic("implement me")
shares, err := rpc.protocolDataService.ListStakerShares(request.GetStakerAddress(), request.GetBlockHeight())
if err != nil {
return nil, err
}

stakerShares := make([]*protocolV1.StakerShare, 0, len(shares))
for _, share := range shares {
stakerShares = append(stakerShares, &protocolV1.StakerShare{
Strategy: share.Strategy,
Shares: share.Shares,
OperatorAddress: share.Operator,
AvsAddresses: share.AvsAddresses,
})
}

return &protocolV1.GetStakerSharesResponse{
Shares: stakerShares,
}, nil
}
36 changes: 20 additions & 16 deletions pkg/rpcServer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Layr-Labs/sidecar/pkg/proofs"
"github.com/Layr-Labs/sidecar/pkg/rewards"
"github.com/Layr-Labs/sidecar/pkg/rewardsCalculatorQueue"
"github.com/Layr-Labs/sidecar/pkg/service/protocolDataService"
"github.com/Layr-Labs/sidecar/pkg/storage"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
Expand All @@ -34,14 +35,15 @@ type RpcServerConfig struct {
}

type RpcServer struct {
Logger *zap.Logger
rpcConfig *RpcServerConfig
blockStore storage.BlockStore
stateManager *stateManager.EigenStateManager
rewardsCalculator *rewards.RewardsCalculator
rewardsQueue *rewardsCalculatorQueue.RewardsCalculatorQueue
eventBus eventBusTypes.IEventBus
rewardsProofs *proofs.RewardsProofsStore
Logger *zap.Logger
rpcConfig *RpcServerConfig
blockStore storage.BlockStore
stateManager *stateManager.EigenStateManager
rewardsCalculator *rewards.RewardsCalculator
rewardsQueue *rewardsCalculatorQueue.RewardsCalculatorQueue
eventBus eventBusTypes.IEventBus
rewardsProofs *proofs.RewardsProofsStore
protocolDataService *protocolDataService.ProtocolDataService
}

func NewRpcServer(
Expand All @@ -52,17 +54,19 @@ func NewRpcServer(
rcq *rewardsCalculatorQueue.RewardsCalculatorQueue,
eb eventBusTypes.IEventBus,
rp *proofs.RewardsProofsStore,
pds *protocolDataService.ProtocolDataService,
l *zap.Logger,
) *RpcServer {
server := &RpcServer{
rpcConfig: config,
blockStore: bs,
stateManager: sm,
rewardsCalculator: rc,
rewardsQueue: rcq,
eventBus: eb,
rewardsProofs: rp,
Logger: l,
rpcConfig: config,
blockStore: bs,
stateManager: sm,
rewardsCalculator: rc,
rewardsQueue: rcq,
eventBus: eb,
rewardsProofs: rp,
protocolDataService: pds,
Logger: l,
}

return server
Expand Down
86 changes: 74 additions & 12 deletions pkg/service/protocolDataService/protocol.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package protocolDataService

import (
"database/sql"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/pkg/eigenState/stakerShares"
"github.com/Layr-Labs/sidecar/pkg/service/types"
"github.com/Layr-Labs/sidecar/pkg/storage"
"go.uber.org/zap"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -42,20 +43,81 @@ func (pds *ProtocolDataService) ListDelegatedStakersForOperator(operator string,
return nil, nil
}

func (pds *ProtocolDataService) ListStakerShares(staker string, blockHeight uint64) ([]*stakerShares.StakerShareDeltas, error) {
shares := make([]*stakerShares.StakerShareDeltas, 0)
type StakerShares struct {
Staker string
Strategy string
Shares string
BlockHeight uint64
Operator *string
Delegated *bool
AvsAddresses []string
}

whereParams := []interface{}{staker}
where := "staker = ?"
if blockHeight > 0 {
where += " AND block_height <= ?"
whereParams = append(whereParams, blockHeight)
}
// ListStakerShares returns the shares of a staker at a given block height, including the operator they were delegated to
// and the addresses of the AVSs the operator was registered to.
//
// If not blockHeight is provided, the most recently indexed block will be used.
func (pds *ProtocolDataService) ListStakerShares(staker string, blockHeight uint64) ([]*StakerShares, error) {
shares := make([]*StakerShares, 0)

res := pds.db.Model(&stakerShares.StakerShareDeltas{}).
Where(where, whereParams...).
Find(&shares)
if blockHeight == 0 {
var currentBlock *storage.Block
res := pds.db.Model(&storage.Block{}).Order("number desc").First(&currentBlock)
if res.Error != nil {
return nil, res.Error
}
blockHeight = currentBlock.Number
}

query := `
with distinct_staker_strategies as (
select
ssd.staker,
ssd.strategy,
ssd.shares,
ssd.block_number,
row_number() over (partition by ssd.staker, ssd.strategy order by ssd.block_number desc) as rn
from sidecar_mainnet_ethereum.staker_shares as ssd
where
ssd.staker = @staker
and block_number <= @blockHeight
order by block_number desc
)
select
dss.*,
dsc.operator,
dsc.delegated,
aosc.avs_list as avs_addresses
from distinct_staker_strategies as dss
left join lateral (
select
sdc.staker,
sdc.operator,
sdc.delegated,
row_number() over (partition by sdc.staker order by sdc.block_number desc, sdc.log_index) as rn
from sidecar_mainnet_ethereum.staker_delegation_changes as sdc
where
sdc.staker = dss.staker
and sdc.block_number <= dss.block_number
order by block_number desc
) as dsc on (dsc.rn = 1)
left join lateral (
select
jsonb_agg(distinct aosc.avs) as avs_list
from sidecar_mainnet_ethereum.avs_operator_state_changes aosc
where
aosc.operator = dsc.operator
and aosc.block_number <= dss.block_number
and aosc.registered = true
) as aosc on true
where
dss.rn = 1
order by block_number desc;
`
res := pds.db.Raw(query,
sql.Named("staker", staker),
sql.Named("blockHeight", blockHeight),
).Scan(&shares)
if res.Error != nil {
return nil, res.Error
}
Expand Down

0 comments on commit 13cdf2d

Please sign in to comment.