Skip to content

How to get the network fee during a test invoke? (inside a smart contract) #3777

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

Closed
lock9 opened this issue Dec 26, 2024 · 4 comments
Closed
Labels
question Further information is requested U3 Regular

Comments

@lock9
Copy link
Contributor

lock9 commented Dec 26, 2024

Problem

I'm validating the transaction fees to ensure they aren't above a specific threshold. I'm using test invoke to validate most of my contract features. It works, except when it comes to network fees. The values are always 0 (during test invoke).

  • Is this expected? Is there any way that I can get the proper value of the net fee inside a smart contract?
  • There are some occasions where I have to attach an extra network fee. I remember this being fixed. Could anyone confirm? Do I need to use another endpoint to calculate this dynamic fee?
  • (Unrelated) When should I use int64 instead of int? I just realized I've been using int everywhere. Could this cause an overflow? Should I replace all int with int64?

Context

This is my contract code:

func ValidateFeeRequirement(feeReq FeeRequirement, ctx ValidationContext) ValidationResult {
	transaction := runtime.GetScriptContainer()

	if transaction.NetFee > feeReq.MaxNetworkFee {
		return ValidationResult{Success: false, ErrorMessage: "Network fee exceeds the maximum allowed fee"}
	}

	if transaction.SysFee > feeReq.MaxSystemFee {
		return ValidationResult{Success: false, ErrorMessage: "System fee exceeds the maximum allowed fee"}
	}

	return ValidationResult{Success: true}
}

It seems that NetFee is always 0. The following test won't work unless I change the net fee requirement to '-1'. This is an indicator that the current netfee of the test invoke is 0:

test("Validate fee requirement with network fee exceeding maximum", async () => {
    const feeRequirement = new FeeRequirement({
        maxNetworkFee: BigInt(0),  // Will work if replaced with -1. 
        maxSystemFee: BigInt(1000000)
    });
    const validationContext = createDummyValidationContext();
    
    const {result, exception} = await PermissionsManager.Test.validateFeeRequirement({
        feeReq: feeRequirement,
        ctx: validationContext
    });

    expect(exception).toBeNull();
    expect(result?.success).toBe(false);
    expect(result?.errorMessage).toBe('Network fee exceeds the maximum allowed fee');
});

Is it possible to 'emulate' the network fees during a test invoke?

@lock9 lock9 added question Further information is requested U3 Regular labels Dec 26, 2024
@lock9 lock9 changed the title How to get the network fee during a test invoke? (inside a smart contract How to get the network fee during a test invoke? (inside a smart contract) Dec 26, 2024
@roman-khimov
Copy link
Member

Is this expected?

Yes.

Is there any way that I can get the proper value of the net fee inside a smart contract?

No. There is no "proper" value, especially when we're doing test invocations. There is no transaction to evaluate its size, it's not yet signed to evaluate GAS required for witness check. And users can legitimately add any amounts to the minimum allowed values.

When should I use int64 instead of int? I just realized I've been using int everywhere. Could this cause an overflow? Should I replace all int with int64?

With the compiler we have now they're all the same 256-bit integers.

Related to neo-project/neo#2568 (comment).

@lock9
Copy link
Contributor Author

lock9 commented Dec 26, 2024

Thanks Roman.

About the dynamic fee calculation - should it work for all cases? Does it need any special config or endpoint?

Related to neo-project/neo#2568 (comment).

Not sure if I understood how they are related, but I suppose it's about differences between testinvoke and a real invoke? Is it related to the dynamic fee calculation?

@roman-khimov
Copy link
Member

roman-khimov commented Dec 26, 2024

About the dynamic fee calculation - should it work for all cases?

I'm not sure what problems you're referring to. RPC estimates work as specified. But

differences between testinvoke and a real invoke

can still happen in some cases (neo-project/neo#2806).

@lock9
Copy link
Contributor Author

lock9 commented Dec 26, 2024

Ah ok. That solves the mystery. Thanks again.

@lock9 lock9 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested U3 Regular
Projects
None yet
Development

No branches or pull requests

2 participants