spacemeshos / svm

SVM - Spacemesh Virtual Machine
https://spacemesh.io
MIT License
85 stars 14 forks source link

Runtime Refactoring #435

Closed YaronWittenstein closed 2 years ago

YaronWittenstein commented 2 years ago

I want to refactor the Runtime of SVM.

Here are the changes done on this one:

Adding RuntimeError::FuncNotCtor

The FuncNotAllowed requires a target. Since we don't really have a target when spawning, I've ended up the FuncNotCtor

Removal of Imports dependency

Initially, the Runtime expected external imports. Back then, we thought that Global-State-related host functions would be implemented within the go-spacemesh. Since then, the Global State has been moved to the Rust land, so we ended up with having setting empty imports as a parameter when initializing the Runtime

Moved Template Pricing to another file

I created TemplatePriceCache to act as the cache for the Template Pricing. The Runtime expects now that cache as a dependency upon initialization. In practice, it'd be ideal to have only a single cache used throughout the process lifetime.

Added GasTank

I thought it would make the code more readable if the repeating pattern of checking whether we've enough gas during will be less explicit and noisy in the code.

I've also added two helpers: check_gas_for_payload and check_gas_for_func. Both expect a paramere named gas_left and its type of GasTank. Each helper checks first whether there is any gas in the tank and returns immediately if the tank is empty.

The GasTank could have been an Option<u64>, but I think it's nicer as it's now.

Other