-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
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}}" |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current contract implementation reverts if there are zero subscriptions. https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/functions/dev/1_0_0/FunctionsSubscriptions.sol#L269
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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}}" |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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"` |
There was a problem hiding this comment.
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"` |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
There was a problem hiding this 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.
@@ -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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"` |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: ignore
No description provided.