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

feat: Allow free request by GasPerToken is 0:0 #2798

Merged
merged 2 commits into from
Aug 17, 2023

Conversation

howjmay
Copy link
Member

@howjmay howjmay commented Aug 12, 2023

closes #2119

@howjmay howjmay force-pushed the gas-per-token branch 9 times, most recently from 0b1d30f to 7263094 Compare August 13, 2023 15:47
@howjmay howjmay marked this pull request as ready for review August 13, 2023 15:56
Comment on lines 102 to 113
func (ratio Ratio32) HasZeroComponent() bool {
return ratio.A == 0 || ratio.B == 0
func (ratio Ratio32) HasInvalidZeroComponent() bool {
return (ratio.A == 0 || ratio.B == 0) && !(ratio.A == 0 && ratio.B == 0)
}
Copy link
Collaborator

@dessaya dessaya Aug 14, 2023

Choose a reason for hiding this comment

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

This funtion IMO now looks too specific for the gas ratio. The Ratio32 type is meant to represent any ratio, not just for gas fee (although right now the only use is for the gas fee).

I sugest moving this logic to the gas package:

package gas

var ZeroGasFee = Ratio32{}

func IsRatioValid(r Ratio32) bool {
    return r == ZeroGasFee || !r.HasZeroComponent()
}

func IsGasFeeCharged(r Ratio32) bool {
    return r != ZeroGasFee
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I have use IsRatioValid() to replace the current usage of HasZeroComponent() . Is it ok?

@@ -140,6 +140,17 @@ func (reqctx *requestContext) checkAllowance() {
}

func (reqctx *requestContext) shouldChargeGasFee() bool {
// freeGasPerToken checks whether we charge token per gas
// If it is free, then we will still burn the gas, but it doesn't charge tokens
// NOT FOR PUBLIC NETWORK
Copy link
Collaborator

Choose a reason for hiding this comment

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

End users will not see this warning. Also there is no way for them to guess that they have to set 0:0 to disable the gas fee. IMO there should be a dedicated command in wasp-cli (like wasp-cli chain disable-gas-policy or whatever), or at least some example in the wiki.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have added the command and relating test

@howjmay howjmay force-pushed the gas-per-token branch 2 times, most recently from 4bad149 to 8045da8 Compare August 15, 2023 08:28
@howjmay howjmay force-pushed the gas-per-token branch 4 times, most recently from 58f4db2 to fbcc725 Compare August 15, 2023 10:54
Comment on lines 106 to 114
var ZeroGasFee = Ratio32{}

func (ratio Ratio32) IsRatioValid() bool {
return ratio == ZeroGasFee || !ratio.HasZeroComponent()
}

func (ratio Ratio32) IsGasFeeCharged() bool {
return ratio != ZeroGasFee
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey, that's better but still has a "separation of concerns" problem: Ratio32 is a generic math type. It can represent a ratio between any two integer values, for example bytes/network packets, cars/people, hamburgers/McDonald's stores...

So it should not know anything about gas, or anything ISC related. IMO these functions should go in the gas package.

Copy link
Member Author

@howjmay howjmay Aug 15, 2023

Choose a reason for hiding this comment

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

It is good idea.

@@ -107,7 +107,7 @@ func (ratio *Ratio32) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
ratio.A = rr.ReadUint32()
ratio.B = rr.ReadUint32()
if rr.Err == nil && ratio.HasZeroComponent() {
if rr.Err == nil && !(ratio == &Ratio32{} || !ratio.HasZeroComponent()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@dessaya here it has a cyclic importing problem. And we need to support 0:0 here too. Therefore, I have to do in this way. Is it ok?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well... I don't know. Again, this validation is specific to gas. From the point of view of Ratio32, 0:0 is invalid (since mathematically 0/0 is undefined). And both 0:x or x:0 are "dangerous" because they might panic withzero division error.

But OK, maybe the simplest solution is to add the concept of a "Zero" Ratio32, so the invariant of Ratio32 can be:

Either A != 0 and B != 0, or both A == 0 and B == 0.

Then:

func (ratio Ratio32) IsZero() bool {
	return ratio.A == 0 && ratio.B == 0
}

func (ratio Ratio32) IsValid() bool {
	return ratio.IsZero() || !ratio.HasZeroComponent()
}

Then:

Suggested change
if rr.Err == nil && !(ratio == &Ratio32{} || !ratio.HasZeroComponent()) {
if rr.Err == nil && ratio.IsValid() {

Or just ignore all my comments and do what you thik is best. This is just me nitpicking... 😛

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me do the above way. That looks fine for me

@howjmay howjmay force-pushed the gas-per-token branch 3 times, most recently from f054de0 to 73f5518 Compare August 15, 2023 23:35
@howjmay howjmay merged commit 59eca3d into iotaledger:develop Aug 17, 2023
5 checks passed
@howjmay howjmay deleted the gas-per-token branch August 17, 2023 11:33
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.

Allow "0 fee" execution
2 participants