Skip to content

Commit

Permalink
PRT-217-provider-uses-cache-even-without-blockHash-data (lavanet#204)
Browse files Browse the repository at this point in the history
* issue fixed

* finalized get and set can have block hash nil

* fixed chainSentry loop ending prematurely

* fixed eth_blockNumber parser func, added error handling in cache setEntry

* also eth spec
  • Loading branch information
omerlavanet authored Dec 28, 2022
1 parent b7f5581 commit b0dc249
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cookbook/spec_add_alfajores.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
8 changes: 4 additions & 4 deletions cookbook/spec_add_arbitrum.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down Expand Up @@ -1590,9 +1590,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions cookbook/spec_add_celo.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions cookbook/spec_add_ethereum.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
8 changes: 4 additions & 4 deletions cookbook/spec_add_fantom.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down Expand Up @@ -1706,9 +1706,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions cookbook/spec_add_goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions cookbook/spec_add_polygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@
"name": "eth_blockNumber",
"block_parsing": {
"parser_arg": [
""
"latest"
],
"parser_func": "EMPTY"
"parser_func": "DEFAULT"
},
"compute_units": "10",
"enabled": true,
Expand Down
5 changes: 4 additions & 1 deletion relayer/chainproxy/chainproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ func SendRelay(
}
cache := cp.GetCache()
// TODO: response sanity, check its under an expected format add that format to spec
cache.SetEntry(ctx, relayRequest, cp.GetSentry().ApiInterface, nil, cp.GetSentry().ChainID, dappID, reply, finalized) // caching in the portal doesn't care about hashes
err := cache.SetEntry(ctx, relayRequest, cp.GetSentry().ApiInterface, nil, cp.GetSentry().ChainID, dappID, reply, finalized) // caching in the portal doesn't care about hashes
if err != nil && !performance.NotInitialisedError.Is(err) {
utils.LavaFormatWarning("error updating cache with new entry", err, nil)
}
return reply, nil, relayRequest, currentLatency, false, nil
}
// isSubscription
Expand Down
4 changes: 3 additions & 1 deletion relayer/chainsentry/chainsentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (cs *ChainSentry) GetLatestBlockData(requestedBlock int64) (latestBlock int
}
hashes := make(map[int64]interface{}, len(cs.blocksQueue))

for indexInQueue := 0; indexInQueue < cs.numFinalBlocks; indexInQueue++ {
for indexInQueue := 0; indexInQueue < cs.numFinalBlocks+cs.finalizedBlockDistance; indexInQueue++ {
blockNum := latestBlockNum - int64(cs.finalizedBlockDistance) - int64(cs.numFinalBlocks) + int64(indexInQueue+1)
if blockNum < 0 {
continue
Expand All @@ -60,10 +60,12 @@ func (cs *ChainSentry) GetLatestBlockData(requestedBlock int64) (latestBlock int
hashes[blockNum] = cs.blocksQueue[indexInQueue]
}
// keep iterating on the others to find a match for the request
// utils.LavaFormatDebug("blocksCompare", &map[string]string{"blocksQueue": fmt.Sprintf("%+v", blockNum), "requestedBlock": strconv.FormatInt(requestedBlock, 10), "indexInQueue": strconv.FormatUint(uint64(indexInQueue), 10)})
if blockNum == requestedBlock {
requestedBlockHash = cs.blocksQueue[indexInQueue]
}
}
// utils.LavaFormatDebug("ChainSentry LatestBlockData", &map[string]string{"blocksQueue": fmt.Sprintf("%+v", cs.blocksQueue), "requestedBlock": strconv.FormatInt(requestedBlock, 10), "initialBlocknum": strconv.FormatInt(latestBlockNum-int64(cs.finalizedBlockDistance)-int64(cs.numFinalBlocks)+int64(0+1), 10)})
return latestBlockNum, hashes, requestedBlockHash, nil
}

Expand Down
7 changes: 4 additions & 3 deletions relayer/performance/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ func (cache *Cache) GetEntry(ctx context.Context, request *pairingtypes.RelayReq
return cache.client.GetRelay(ctx, &pairingtypes.RelayCacheGet{Request: request, ApiInterface: apiInterface, BlockHash: blockHash, ChainID: chainID, Finalized: finalized})
}

func (cache *Cache) SetEntry(ctx context.Context, request *pairingtypes.RelayRequest, apiInterface string, blockHash []byte, chainID string, bucketID string, reply *pairingtypes.RelayReply, finalized bool) {
func (cache *Cache) SetEntry(ctx context.Context, request *pairingtypes.RelayRequest, apiInterface string, blockHash []byte, chainID string, bucketID string, reply *pairingtypes.RelayReply, finalized bool) error {
if cache == nil {
// TODO: try to connect again once in a while
return
return NotInitialisedError
}
// TODO: handle disconnections and SetRelay error types here
cache.client.SetRelay(ctx, &pairingtypes.RelayCacheSet{Request: request, ApiInterface: apiInterface, BlockHash: blockHash, ChainID: chainID, Response: reply, Finalized: finalized, BucketID: bucketID})
_, err := cache.client.SetRelay(ctx, &pairingtypes.RelayCacheSet{Request: request, ApiInterface: apiInterface, BlockHash: blockHash, ChainID: chainID, Response: reply, Finalized: finalized, BucketID: bucketID})
return err
}
17 changes: 13 additions & 4 deletions relayer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,17 +803,26 @@ func (s *relayServer) TryRelay(ctx context.Context, request *pairingtypes.RelayR
finalized := g_sentry.IsFinalizedBlock(request.RequestBlock, latestBlock)
cache := g_chainProxy.GetCache()
// TODO: handle cache on fork for dataReliability = false
reply, err := cache.GetEntry(ctx, request, g_sentry.ApiInterface, requestedBlockHash, g_sentry.ChainID, finalized)
var reply *pairingtypes.RelayReply = nil
var err error = nil
if requestedBlockHash != nil || finalized {
reply, err = cache.GetEntry(ctx, request, g_sentry.ApiInterface, requestedBlockHash, g_sentry.ChainID, finalized)
}
if err != nil || reply == nil {
if performance.NotConnectedError.Is(err) {
if err != nil && performance.NotConnectedError.Is(err) {
utils.LavaFormatError("cache not connected", err, nil)
}
// cache miss
// cache miss or invalid
reply, _, _, err = nodeMsg.Send(ctx, nil)
if err != nil {
return nil, utils.LavaFormatError("Sending nodeMsg failed", err, nil)
}
cache.SetEntry(ctx, request, g_sentry.ApiInterface, requestedBlockHash, g_sentry.ChainID, userAddr.String(), reply, finalized)
if requestedBlockHash != nil || finalized {
err := cache.SetEntry(ctx, request, g_sentry.ApiInterface, requestedBlockHash, g_sentry.ChainID, userAddr.String(), reply, finalized)
if err != nil && !performance.NotInitialisedError.Is(err) {
utils.LavaFormatWarning("error updating cache with new entry", err, nil)
}
}
}

apiName := nodeMsg.GetServiceApi().Name
Expand Down

0 comments on commit b0dc249

Please sign in to comment.