Skip to content

Commit

Permalink
amen fix VRF bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Aug 10, 2022
1 parent e93ab4d commit 0451823
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
11 changes: 9 additions & 2 deletions relayer/chainproxy/chainproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package chainproxy
import (
"context"
"fmt"
"strconv"
"time"

"github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/relayer/sentry"
"github.com/lavanet/lava/relayer/sigs"
"github.com/lavanet/lava/utils"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
)
Expand Down Expand Up @@ -164,7 +166,7 @@ func SendRelay(
}
callback_send_reliability := func(clientSession *sentry.ClientSession, dataReliability *pairingtypes.VRFData) (*pairingtypes.RelayReply, *pairingtypes.RelayRequest, error) {
//client session is locked here

sentry := cp.GetSentry()
if blockHeight < 0 {
return nil, nil, fmt.Errorf("expected callback_send_relay to be called first and set blockHeight")
}
Expand All @@ -174,7 +176,7 @@ func SendRelay(
ApiUrl: url,
Data: []byte(req),
SessionId: uint64(0), //sessionID for reliability is 0
ChainID: cp.GetSentry().ChainID,
ChainID: sentry.ChainID,
CuSum: clientSession.CuSum,
BlockHeight: blockHeight,
RelayNum: clientSession.RelayNum,
Expand All @@ -184,6 +186,11 @@ func SendRelay(
ConnectionType: connectionType,
}

if (requestedBlock - int64(dataReliability.Epoch)) > int64(sentry.GetEpochSize())+int64(sentry.GetOverlapSize()) {
return nil, nil, utils.LavaFormatError("data reliability message, requested block and data reliability block mismatch", nil,
&map[string]string{"requestedBlock": strconv.FormatInt(requestedBlock, 10), "dataReliability Epoch": strconv.FormatUint(dataReliability.Epoch, 10)})
}

sig, err := sigs.SignRelay(privKey, *relayRequest)
if err != nil {
return nil, nil, err
Expand Down
11 changes: 8 additions & 3 deletions relayer/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ func (s *Sentry) GetProvidersCount() uint64 {
return atomic.LoadUint64(&s.providersCount)
}

func (s *Sentry) GetEpochSize() uint64 {
return atomic.LoadUint64(&s.EpochSize)
}

func (s *Sentry) FetchEpochSize(ctx context.Context) error {
res, err := s.epochStorageQueryClient.Params(ctx, &epochstoragetypes.QueryParamsRequest{})
if err != nil {
Expand Down Expand Up @@ -757,7 +761,7 @@ func (s *Sentry) Start(ctx context.Context) {
s.FetchChainParams(ctx)

if s.newEpochCb != nil {
go s.newEpochCb(data.Block.Height - StaleEpochDistance*int64(s.EpochSize)) // Currently this is only askForRewards
go s.newEpochCb(data.Block.Height - StaleEpochDistance*int64(s.GetEpochSize())) // Currently this is only askForRewards
}

//
Expand Down Expand Up @@ -1596,9 +1600,10 @@ func (s *Sentry) ExpecedBlockHeight() (int64, int) {

// TODO:: Dont calc. get this info from blockchain - if LAVA params change, this calc is obsolete
func (s *Sentry) GetEpochFromBlockHeight(blockHeight int64, isOverlap bool) uint64 {
epoch := uint64(blockHeight - blockHeight%int64(s.EpochSize))
epochSize := s.GetEpochSize()
epoch := uint64(blockHeight - blockHeight%int64(epochSize))
if isOverlap {
epoch = epoch - s.EpochSize
epoch = epoch - epochSize
}
return epoch
}
Expand Down
33 changes: 19 additions & 14 deletions relayer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,31 +483,29 @@ func (s *relayServer) Relay(ctx context.Context, request *pairingtypes.RelayRequ
return nil, utils.LavaFormatError("spec not supported by server", err, &map[string]string{"request.chainID": request.ChainID, "chainID": g_serverChainID})
}

// (ctx, userAddr.String(), uint64(request.BlockHeight), authorizedHeight uint64)

authorizeAndParseMessage := func(ctx context.Context, userAddr sdk.AccAddress, request *pairingtypes.RelayRequest) (*pairingtypes.QueryVerifyPairingResponse, chainproxy.NodeMessage, error) {
authorisedUserResponse, err := g_sentry.IsAuthorizedUser(ctx, userAddr.String(), uint64(request.BlockHeight))
var nodeMsg chainproxy.NodeMessage
authorizeAndParseMessage := func(ctx context.Context, userAddr sdk.AccAddress, request *pairingtypes.RelayRequest, blockHeighToAutherise uint64) (*pairingtypes.QueryVerifyPairingResponse, chainproxy.NodeMessage, error) {
//TODO: cache this client, no need to run the query every time
authorisedUserResponse, err := g_sentry.IsAuthorizedUser(ctx, userAddr.String(), blockHeighToAutherise)
if err != nil {
return nil, nil, utils.LavaFormatError("user not authorized or error occured", err, &map[string]string{"userAddr": userAddr.String()})
return nil, nil, utils.LavaFormatError("user not authorized or error occured", err, &map[string]string{"userAddr": userAddr.String(), "block": strconv.FormatUint(blockHeighToAutherise, 10)})
}

// Parse message, check valid api, etc
nodeMsg, err := g_chainProxy.ParseMsg(request.ApiUrl, request.Data, request.ConnectionType)
if err != nil {
return nil, nil, utils.LavaFormatError("failed parsing request message", err, &map[string]string{"apiInterface": g_sentry.ApiInterface, "request URL": request.ApiUrl, "request data": string(request.Data), "userAddr": userAddr.String()})
}
return authorisedUserResponse, nodeMsg, nil
}

//TODO: cache this client, no need to run the query every time
//
// Parse message, check valid api, etc
authorisedUserResponse, nodeMsg, err := authorizeAndParseMessage(ctx, userAddr, request, epoch)
if err != nil {
return nil, err
}

if request.DataReliability != nil {
epoch := request.DataReliability.GetEpoch()
var authorisedUserResponse *pairingtypes.QueryVerifyPairingResponse
authorisedUserResponse, nodeMsg, err = authorizeAndParseMessage(ctx, userAddr, request, epoch)
if err != nil {
utils.LavaFormatError("data reliability failed autherising user request", nil, nil)
return nil, err
}

// client blockheight can only be at at prev epoch but not ealier
if epoch < uint64(prevEpochStart) {
Expand Down Expand Up @@ -578,6 +576,13 @@ func (s *relayServer) Relay(ctx context.Context, request *pairingtypes.RelayRequ
userSessions.Lock.Unlock()

} else {
var authorisedUserResponse *pairingtypes.QueryVerifyPairingResponse
authorisedUserResponse, nodeMsg, err = authorizeAndParseMessage(ctx, userAddr, request, uint64(request.BlockHeight))

if err != nil {
return nil, err
}

relaySession, err := getOrCreateSession(ctx, userAddr.String(), request, authorisedUserResponse.GetOverlap())
if err != nil {
return nil, err
Expand Down

0 comments on commit 0451823

Please sign in to comment.