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

Unexpected behaviour difference between setProvider and setRpcProvider in TezosToolkit #2845

Open
ac10n opened this issue Feb 28, 2024 · 0 comments
Labels
breaking change A change that will break API or smart contract interactions bug Something isn't working triage Issue requires triage ux/dx Related to Developer Experience

Comments

@ac10n
Copy link
Contributor

ac10n commented Feb 28, 2024

Consider the following code:

console.log(this.tezosToolkit.rpc.getRpcUrl())
this.tezosToolkit.setRpcProvider(this.rpcNodes[1])
console.log(this.tezosToolkit.rpc.getRpcUrl())
this.contracts[address] = await this.tezosToolkit.wallet.at(address)

This will output the following:

https://mainnet.smartpy.io/
https://mainnet.api.tez.ie/

but the network request from the final line is still sent to https://mainnet.smartpy.io/, ie the previous rpc before we called setRpcProvider

Interestingly, this.tezosToolkit.setProvider({ rpc: this.rpcNodes[0] }) works as expected.

Here we have an unexpected difference between the two APIs.
By looking at the source code of setProvider, we can see that this difference is indeed unexpected:

  setProvider({
    rpc,
    stream,
    signer,
    protocol,
    config,
    forger,
    wallet,
    packer,
    globalConstantsProvider,
    readProvider,
    parserProvider,
    injectorProvider,
  }: SetProviderOptions) {
    this.setRpcProvider(rpc);
    this.setStreamProvider(stream);
    this.setSignerProvider(signer);
    this.setForgerProvider(forger);
    this.setWalletProvider(wallet);
    this.setPackerProvider(packer);
    this.setGlobalConstantsProvider(globalConstantsProvider);
    this.setReadProvider(readProvider);
    this.setParserProvider(parserProvider);
    this.setInjectorProvider(injectorProvider);

    this._context.proto = protocol;
    if (config) {
      this._context.setPartialConfig(config);
    }
  }

The field rpc of the input parameter is only used in this.setRpcProvider(rpc);
The reason we have this difference is in the call to: this.setReadProvider(readProvider);:

  setReadProvider(readProvider?: SetProviderOptions['readProvider']) {
    const readP = readProvider ? readProvider : new RpcReadAdapter(this._context.rpc);
    this._options.readProvider = readP;
    this._context.readProvider = readP;
  }

When a readProvider is not provided, TezosToolkit will use the rpc.
In general, any call to the setProvider will call all the set***Providers, even if the calling code has not passed anything.

In my opinion, when someone passes only rpc to the setProvider, they expect only rpcProvider to be updated, and no other side effect.

How we can solve this problem?
One possible solution is to update the individual calls in setProvider like:

+    if (readProvider !== undefined) {
       this.setReadProvider(readProvider);
+    }

But this will be a breaking change: If an existing dApp called setProvider with only rpc and relied (without knowing) on the behavior that readProvder is also updated, their implementation will be broken.

@ac10n ac10n added triage Issue requires triage breaking change A change that will break API or smart contract interactions bug Something isn't working ux/dx Related to Developer Experience labels Feb 28, 2024
@hui-an-yang hui-an-yang added this to the v20+ milestone May 13, 2024
@dsawali dsawali modified the milestones: v20+, 21, v21 May 29, 2024
@ac10n ac10n moved this to To do in Taquito Dev Jun 21, 2024
@hui-an-yang hui-an-yang modified the milestones: v21, v20.1 Oct 2, 2024
@hui-an-yang hui-an-yang removed this from Taquito Dev Oct 4, 2024
@github-project-automation github-project-automation bot moved this to Needs triage in Taquito Triage Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change A change that will break API or smart contract interactions bug Something isn't working triage Issue requires triage ux/dx Related to Developer Experience
Projects
Status: Needs triage
Development

No branches or pull requests

3 participants