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

Cache contract wrappers #334

Merged
merged 12 commits into from
Aug 16, 2018
Merged

Cache contract wrappers #334

merged 12 commits into from
Aug 16, 2018

Conversation

dkent600
Copy link
Contributor

@dkent600 dkent600 commented Aug 15, 2018

Resolves: #324

Cache contract wrappers obtained using the contract wrapper factory methods .at and .new. The cache is local, it does not persist across application instances.

The feature is controlled by the cacheContractWrappers config setting which is set to true by default.

@@ -1,6 +1,7 @@
{
"autoApproveTokenTransfers": true,
"defaultVotingMachine": "AbsoluteVote",
"cacheContractWrappers": false,
Copy link
Contributor

Choose a reason for hiding this comment

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

why not true? seems like a no-brainer to me

Copy link
Contributor Author

@dkent600 dkent600 Aug 15, 2018

Choose a reason for hiding this comment

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

Just being conservative: This feature consumes a technically limitless amount of memory. Maybe you're right though?

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, only as much memory as there are contracts to cache

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, that's why I said "technically"

@@ -8,6 +10,18 @@ import { Web3EventService } from "./web3EventService";
export class ContractWrapperFactory<TWrapper extends IContractWrapperBase>
implements IContractWrapperFactory<TWrapper> {

public static setConfigService(configService: IConfigService): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

in general would like a description of all the changes that are happening to the config service in the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did describe the change in the description

Copy link
Contributor Author

Choose a reason for hiding this comment

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

... there is otherwise no change noticible to users of arc.js.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But yes, I did make an internal change and could've mentioned that here

@@ -49,9 +88,50 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapperBase>
*/
public async deployed(): Promise<TWrapper> {
await this.ensureSolidityContract();
return new this.wrapper(this.solidityContract, this.web3EventService).hydrateFromDeployed();

const getWrapper = (): Promise<TWrapper> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

almost all this code is a duplicate of .at, any easy way to merge/reuse?

Copy link
Contributor Author

@dkent600 dkent600 Aug 15, 2018

Choose a reason for hiding this comment

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

I initially tried that but they are each a bit different. I decided it wasn't worth it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the end it was possible

}
addressMap.set(wrapper.address, wrapper);
if (!at) {
// so we can also get the deployed version of this contract without the address
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just use the deployed address as the name? otherwise you could have two copies of the same contract in the cache depending on if you call .deployed or .at

Copy link
Contributor Author

@dkent600 dkent600 Aug 15, 2018

Choose a reason for hiding this comment

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

The "${name}_deployed" key is for when users call deployed(), which supplies no address, only the name.

Copy link
Contributor

Choose a reason for hiding this comment

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

right, but the deployed contract has an address, which you could use as the name of the key so that you dont end up with two copies of the same cached contract

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found a way to do it

}

if (address) {
hydratedWrapper = this.getCachedContract(this.solidityContractName, address) as TWrapper;
Copy link
Contributor

Choose a reason for hiding this comment

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

should maybe log if the returned contract is coming from the cache or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I was wanting to do that too. done.

@dkent600 dkent600 merged commit 77f7f51 into master Aug 16, 2018
@dkent600 dkent600 deleted the cacheWrappers branch August 16, 2018 23:35
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.

2 participants