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

Oracle: Move query methods to linked library #749

Merged
merged 3 commits into from
Jul 26, 2021

Conversation

nventuro
Copy link
Contributor

This reduces the bytecode size of MetaStablePools enough that they can be deployed. With 200 runs, they end up at 23.919kB.

The way bytecode is reduced is by removing all oracle query functionality outside of the pool contract itself, and into a linked library. This adds a slight overhead of ~3k gas (2600 from the delegatecall), but gets rid of the binary search code and exponentiation (from fromLowResLog).

Ideally, we'd be able to deploy the library at construction time in either the Pool or the Factory. Unfortunately, Solidity does not allow for this (though I'll create a language proposal soon with this idea). Some available options are:

  1. use linked libraries, which requires external (i.e. outside of Solidity itself) linking (bytecode modification)
  2. use delegatecall directly, which can only be done in assembly (as delegatecall is not considered view) - essentially implementing a kind of proxy pattern. This could work, but it requires a way to keep the storage slots of the base and library contracts in sync
  3. drop usage of delegate calls and make view calls back into the main contract for storage reads. Even if these repeated calls only cost 100 gas, the overhead of call encoding, decoding, function dispatch, return value encoding and decoding quickly adds up.

In the end, I went with 1), as this is the most 'standard' approach. I modified the deploy function to allow for libraries, which are manually linked. Unfortunately, Hardhat does not support linking an artifact directly (see NomicFoundation/hardhat#1716), so this will have to make do for now.

This PR is split into two commits. The first moves functionality from the pool and oracle into the QueryProcessor library, while the second moves the query tests from the pools into the pool oracle, which is where they are implemented. No funtionality should change as a result of this.

Comment on lines 42 to 43
using Buffer for uint256;
using Samples for bytes32;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need these?

@facuspagnuolo facuspagnuolo changed the title Stable linked library Oracle: Move query methods to linked library Jul 26, 2021
@facuspagnuolo facuspagnuolo merged commit 70d02f9 into master Jul 26, 2021
@facuspagnuolo facuspagnuolo deleted the stable-linked-library branch July 26, 2021 19:39
TomAFrench added a commit that referenced this pull request Aug 10, 2021
* master:
  renamed RewardsCallback -> DistributorCallback, and callback -> distributorCallback for added specificity (#776)
  Distributors: Exiter (#719)
  de-overload stake function to stakeFor a recipient (#767)
  MultiRewards: use authorizer pattern for access control (#752)
  Distributors:  IRewardsCallback (#761)
  meta: emit price rate provider event in consturctor (#764)
  docs: fix _priceRate natspec to refer to a single token rather than list (#762)
  feat: add AssetHelpers class (#728)
  Investment pool v1 (#738)
  Deployments: Implement meta stable pool factory task (#759)
  Meta: Reduce bytecodesize below limit (#758)
  Chore: Fix deployment bytecode benchmark (#757)
  Meta: Index and test events (#755)
  Meta: Add factory tests (#754)
  Oracle: Fix linter (#753)
  Meta: Implement factory (#751)
  Oracle: Move query methods to linked library (#749)
  Meta: Add update cache event (#750)
  Fix WeightedPool2Tokens ABI
  docs: comment changes in MetaStablePool (#747)
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.

None yet

2 participants