Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions: tracking subscriptions #10613

Merged
merged 19 commits into from
Sep 18, 2023
Merged

Conversation

pinebit
Copy link
Contributor

@pinebit pinebit commented Sep 13, 2023

No description provided.

@github-actions
Copy link
Contributor

I see that you haven't updated any README files. Would it make sense to do so?

@@ -43,6 +43,13 @@ maxSecretsSizesList = [10_240, 20_480, 51_200, 102_400, 307_200, 512_000, 1_048_
updateFrequencySec = 30
updateTimeoutSec = 10

[pluginConfig.OnchainSubscriptions]
blockConfirmations = 1
routerAddress = "{{router_contract_address}}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have multiple places in the jobspec where there are duplicate definitions of the router contract address. How hard would it be to exclusively use the router address specified under contractID everywhere?
(This is out of scope for this PR, but may be a good backlog ticket as it may add misconfiguration risk)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this would be a good improvement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a ticket to tech debt epic please!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FUN-921

utils.StartStopOnce

config OnchainSubscriptionsConfig
subscriptions map[common.Address]*functions_router.IFunctionsSubscriptionsSubscription
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One user could have many subscriptions. Therefore, how are we planning to approach this if we are only mapping each user's address to 1 subscription? Are we only going to keep the subscription with the maximum balance for each user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, İ need to improve this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should store all of them.

return
}
if count == 0 {
s.lggr.Info("Router has no subscriptions yet")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will log errors when that happens right?

defer s.rwMutex.Unlock()
for _, subscription := range subscriptions {
if subscription.Owner == utils.ZeroAddress {
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this case happen? Should we log an error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If subscription is cancelled I guess. I need to test it better though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is for cancelled subs. But we shouldn't continue, we need to still store it as cancelled. Otherwise we'll keep using previous values.

continue
}
subscription := subscription
s.subscriptions[subscription.Owner] = &subscription
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should check the balance and only save the subscription with the highest balance here? Although maybe we should also re-work this as we are currently holding the rwMutext here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's save all but have an API to retrieve highest balance. In the future we are also going to look at flags stored within subscriptions.

@@ -43,6 +43,13 @@ maxSecretsSizesList = [10_240, 20_480, 51_200, 102_400, 307_200, 512_000, 1_048_
updateFrequencySec = 30
updateTimeoutSec = 10

[pluginConfig.OnchainSubscriptions]
blockConfirmations = 1
routerAddress = "{{router_contract_address}}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a ticket to tech debt epic please!

@@ -68,6 +70,10 @@ func (h *functionsConnectorHandler) HandleGatewayMessage(ctx context.Context, ga
h.lggr.Errorw("request rate-limited", "id", gatewayId, "address", fromAddr)
return
}
if h.subscriptions.GetSubscription(fromAddr) == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to check against sub balance. So we need required balance in the config and probably a more specific API than GetSubscription().

type OnchainSubscriptionsConfig struct {
RouterAddress common.Address `json:"routerAddress"`
BlockConfirmations uint `json:"blockConfirmations"`
QueryFrequencySec uint `json:"queryFrequencySec"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow allowlist config fields, I'd suggest "UpdateFrequencySec", "UpdateTimeoutSec" and "UpdateRangeSec".

)

type OnchainSubscriptionsConfig struct {
RouterAddress common.Address `json:"routerAddress"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowlist has "ContractAddress" but "Router" is slightly better. So up to you if you prefer consistency or a slightly more specific name.

utils.StartStopOnce

config OnchainSubscriptionsConfig
subscriptions map[common.Address]*functions_router.IFunctionsSubscriptionsSubscription
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should store all of them.


count, err := s.getSubscriptionsCount(ctx, blockNumber)
if err != nil {
s.lggr.Errorw("Error getting subscriptions count", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could cache this and use previous value if fetching fails.

defer s.rwMutex.Unlock()
for _, subscription := range subscriptions {
if subscription.Owner == utils.ZeroAddress {
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is for cancelled subs. But we shouldn't continue, we need to still store it as cancelled. Otherwise we'll keep using previous values.

continue
}
subscription := subscription
s.subscriptions[subscription.Owner] = &subscription
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's save all but have an API to retrieve highest balance. In the future we are also going to look at flags stored within subscriptions.

func NewFunctionsConnectorHandler(nodeAddress string, signerKey *ecdsa.PrivateKey, storage s4.Storage, allowlist functions.OnchainAllowlist, rateLimiter *hc.RateLimiter, lggr logger.Logger) (*functionsConnectorHandler, error) {
if signerKey == nil || storage == nil || allowlist == nil || rateLimiter == nil {
return nil, fmt.Errorf("signerKey, storage, allowlist and rateLimiter must be non-nil")
func NewFunctionsConnectorHandler(nodeAddress string, signerKey *ecdsa.PrivateKey, storage s4.Storage, allowlist functions.OnchainAllowlist, rateLimiter *hc.RateLimiter, subscriptions functions.OnchainSubscriptions, lggr logger.Logger) (*functionsConnectorHandler, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning to also add to the Gateway handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it needs to be enabled in GW handler too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a must. But it should be very easy to add there as optional and the more bad requests we can stop in Gateway, the less unnecessary load we put on the nodes.

maxBalance = sub.Balance
}
}
fmt.Println(hex.EncodeToString(maxBalance.Bytes()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

KuphJr
KuphJr previously approved these changes Sep 14, 2023
Copy link
Contributor

@KuphJr KuphJr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One missed log line.

Other that that, my concerns have been addressed, so it LGTM. Bolek should review & make sure his comments have been addressed before we merge.

core/services/functions/connector_handler.go Show resolved Hide resolved
@@ -68,6 +71,10 @@ func (h *functionsConnectorHandler) HandleGatewayMessage(ctx context.Context, ga
h.lggr.Errorw("request rate-limited", "id", gatewayId, "address", fromAddr)
return
}
if balance, err := h.subscriptions.GetMaxUserBalance(fromAddr); err != nil || balance.Cmp(big.NewInt(0)) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to check against a configured minimum balance, not 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
minBalanceWei := new(big.Int)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not Wei but Juels.

pendingRequests hc.RequestCache[PendingSecretsRequest]
allowlist OnchainAllowlist
subscriptions OnchainSubscriptions
minimumBalanceWei *big.Int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Juels

}
}
minimumBalanceWei := new(big.Int)
big.NewFloat(0).Mul(big.NewFloat(cfg.MinimumSubscriptionBalanceLink), big.NewFloat(1e18)).Int(minimumBalanceWei)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to have Juels in config? I'm worried about precision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to what we use in other cases like this: assets.Link.

return
}
if count == 0 {
s.lggr.Info("Router has no subscriptions yet")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will log errors when that happens right?

@@ -69,6 +76,11 @@ func (h *functionsConnectorHandler) HandleGatewayMessage(ctx context.Context, ga
return
}

if balance, err := h.subscriptions.GetMaxUserBalance(fromAddr); err != nil || balance.Cmp(h.minBalanceWei) < 0 {
h.lggr.Errorw("request is not backed with a funded subscription", "id", gatewayId, "address", fromAddr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also log user balance and min required balance.

@@ -20,7 +21,11 @@ import (
type FunctionsHandlerConfig struct {
OnchainAllowlistChainID string `json:"onchainAllowlistChainId"`
// Not specifying OnchainAllowlist config disables allowlist checks
OnchainAllowlist *OnchainAllowlistConfig `json:"onchainAllowlist"`
OnchainAllowlist *OnchainAllowlistConfig `json:"onchainAllowlist"`
OnchainSubscriptionsChainID string `json:"onchainSubscriptionsChainId"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have a single chainID config field for both the allowlist and subscriptions. Maybe rename the existing one to just "ChainId"?

if h.subscriptions != nil {
if balance, err := h.subscriptions.GetMaxUserBalance(sender); err != nil || balance.Cmp(h.minimumBalanceWei) < 0 {
h.lggr.Debug("received a message from a user having insufficient balance", "sender", msg.Body.Sender, "balance", balance.String())
return errors.New("sender has insufficient balance")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the message to "insufficient subscription balance: [balance] [unit]"

type OnchainSubscriptions interface {
job.ServiceCtx

// GetMaxUserBalance returns a maximum subscription balance, or error if user has no subscriptions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify unit (juels)

@@ -35,6 +35,8 @@ requestTimeoutCheckFrequencySec = 10
requestTimeoutSec = 300
maxRequestSizesList = [30_720, 51_200, 102_400, 204_800, 512_000, 1_048_576, 2_097_152, 3_145_728, 5_242_880, 10_485_760]
maxSecretsSizesList = [10_240, 20_480, 51_200, 102_400, 307_200, 512_000, 1_048_576, 2_097_152]
minimumSubscriptionBalanceLink = 0.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outdated

allowlist functions.OnchainAllowlist
rateLimiter *hc.RateLimiter
subscriptions functions.OnchainSubscriptions
minBalance assets.Link
Copy link
Contributor

@bolekk bolekk Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: ignore

@pinebit pinebit enabled auto-merge September 18, 2023 05:38
@pinebit pinebit added this pull request to the merge queue Sep 18, 2023
@cl-sonarqube-production
Copy link

SonarQube Quality Gate

Quality Gate failed

Failed condition 77.2% 77.2% Coverage on New Code (is less than 80%)

See analysis details on SonarQube

Merged via the queue into develop with commit 1bb0c1a Sep 18, 2023
105 of 106 checks passed
@pinebit pinebit deleted the functions/subscriptions-tracking branch September 18, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants