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

Add simple gas metering on PVM #69

Merged
merged 3 commits into from
Oct 4, 2024
Merged

Add simple gas metering on PVM #69

merged 3 commits into from
Oct 4, 2024

Conversation

pantrif
Copy link
Contributor

@pantrif pantrif commented Oct 3, 2024

This PR introduces basic gas metering, where each instruction consumes 1 gas, since a gas cost model has not been defined yet.

Part of this #39

@pantrif pantrif requested a review from a team October 3, 2024 14:01
internal/polkavm/instructions.go Outdated Show resolved Hide resolved
internal/polkavm/interpreter/mutator.go Outdated Show resolved Hide resolved
internal/polkavm/interfaces.go Outdated Show resolved Hide resolved
@danielvladco
Copy link
Member

could we use a wrapper pattern for the mutator?

we could have gasMutator struct that wraps the mutator and returns another mutator with gas functionality added
for example:

func NewGasMutator(next Mutator) Mutator {
    return &gasMutator{next: next}
}

func (m *gasMutator) Trap() error {
    m.remainingGas -= 1
    return m.next.Trap()
}

// and so on for all the operatios

then we pass it like this

instruction.StepOnce(NewGasMutator(mutator))

This approach might have a bit more boilerplate, but it seems more idiomatic and doesn't require one map lookup for each instruction, which could add up quite a lot

@pantrif
Copy link
Contributor Author

pantrif commented Oct 4, 2024

could we use a wrapper pattern for the mutator?

we could have gasMutator struct that wraps the mutator and returns another mutator with gas functionality added for example:

func NewGasMutator(next Mutator) Mutator {
    return &gasMutator{next: next}
}

func (m *gasMutator) Trap() error {
    m.remainingGas -= 1
    return m.next.Trap()
}

// and so on for all the operatios

then we pass it like this

instruction.StepOnce(NewGasMutator(mutator))

This approach might have a bit more boilerplate, but it seems more idiomatic and doesn't require one map lookup for each instruction, which could add up quite a lot

I understand your point, but the main reason for using a map is to have a centralized, easily modifiable place once the cost model is defined. However if you have a strong opinion about it i will change it.

Copy link
Member

@danielvladco danielvladco left a comment

Choose a reason for hiding this comment

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

LGTM

@pantrif pantrif merged commit af2c0c2 into main Oct 4, 2024
2 checks passed
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