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

refactor: exclude encoding of reading state results in chain extension #101

Closed
Tracked by #132
Daanvdplas opened this issue Jul 5, 2024 · 1 comment · Fixed by #122
Closed
Tracked by #132

refactor: exclude encoding of reading state results in chain extension #101

Daanvdplas opened this issue Jul 5, 2024 · 1 comment · Fixed by #122
Assignees

Comments

@Daanvdplas
Copy link
Collaborator

Daanvdplas commented Jul 5, 2024

Because we funnelled the read state functions through read_state on the contract size the result had to be raw bytes, therefore encoded on the runtime and decoded in the contract. The read state functions are now called specifically and therefore the result can be its type (e.g. Balance). By refactoring it prevents the pop api having to decode the read state results which can decrease the contract size.

With the current contract it decreases it from 7.1K to 6.8K (don't compare with the psp22 contract as the logic implemented is not equal).

In stead of:

fn read_fungibles_state<T, E>(
	key: Keys<T>,
	env: &mut Environment<E, BufInBufOutState>,
) -> Result<Vec<u8>, DispatchError>
where
	T: pallet_contracts::Config
		+ pallet_assets::Config<TrustBackedAssetsInstance, AssetId = AssetId>
		+ fungibles::Config,
	E: Ext<T = T>,
	T: frame_system::Config<AccountId = sp_runtime::AccountId32>,
{
	env.charge_weight(T::DbWeight::get().reads(1_u64))?;
	match key {
		TotalSupply(id) => Ok(fungibles::Pallet::<T>::total_supply(id).encode()),
	}
	...
}

We do:

fn read_fungibles_state<T, E>(
	key: Keys<T>,
	env: &mut Environment<E, BufInBufOutState>,
) -> Result<(), DispatchError>
where
	T: pallet_contracts::Config
		+ pallet_assets::Config<TrustBackedAssetsInstance, AssetId = AssetId>
		+ fungibles::Config,
	E: Ext<T = T>,
	T: frame_system::Config<AccountId = sp_runtime::AccountId32>,
{
	env.charge_weight(T::DbWeight::get().reads(1_u64))?;
	match key {
		TotalSupply(id) => env.write(&fungibles::Pallet::<T>::total_supply(id), false, None),
	}
	...
	Ok(())
}
@Daanvdplas
Copy link
Collaborator Author

Closed by #122

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 a pull request may close this issue.

1 participant