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

action fee changes #854

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,6 @@ function storageFeeCalculator() {

```

## Forward fees

Internal messages define an `ihr_fee` in Toncoins, which is subtracted from the value attached to the message and awarded to the validators of the destination shardchain if they include the message by the IHR mechanism. The `fwd_fee` is the original total forwarding fee paid for using the HR mechanism; it is automatically computed from some configuration parameters and the size of the message at the time the message is generated. Notice that the total value carried by a newly-created internal outbound message equals the sum of value, `ihr_fee`, and `fwd_fee`. This sum is deducted from the balance of the source account. Of these components, only value is always credited to the destination account on message delivery. The `fwd_fee` is collected by the validators on the HR path from the source to the destination, and the `ihr_fee` is either collected by the validators of the destination shardchain (if the message is delivered via IHR), or credited to the destination account.

:::info
`fwd_fee` covers 2/3 of the cost, as 1/3 is allocated to the `action_fee` when the message is created.

```cpp
auto fwd_fee_mine = msg_prices.get_first_part(fwd_fee);
auto fwd_fee_remain = fwd_fee - fwd_fee_mine;

fees_total = fwd_fee + ihr_fee;
fees_collected = fwd_fee_mine;

ap.total_action_fees += fees_collected;
ap.total_fwd_fees += fees_total;
```

:::

## Computation fees

### Gas
Expand Down Expand Up @@ -233,18 +213,51 @@ will be translated into a few instructions which changes the order of elements o

When the number of stack entries is substantial (10+), and they are actively used in different orders, stack operations fees may become non-negligible.


## Forward fees

Internal messages define an `ihr_fee` in Toncoins, which is subtracted from the value attached to the message and awarded to the validators of the destination shardchain if they include the message through the IHR mechanism. The `fwd_fee` is the original total forwarding fee paid for using the HR mechanism; it is automatically computed from the [24 and 25 configuration parameters](/v3/documentation/network/configs/blockchain-configs#param-24-and-25) and the size of the message at the time the message is generated. Note that the total value carried by a newly created internal outbound message equals the sum of the value, `ihr_fee`, and `fwd_fee`. This sum is deducted from the balance of the source account. Of these components, only the value is always credited to the destination account upon message delivery. The `fwd_fee` is collected by the validators on the HR path from the source to the destination, and the `ihr_fee` is either collected by the validators of the destination shardchain (if the message is delivered via IHR) or credited to the destination account.

Choose a reason for hiding this comment

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

Однако сейчас ihr не имплементирован, и если выставить его ненулевым, то ihr fee просто добавится к value сообщения при получении

Copy link
Contributor Author

@Alexey-Ostrovsky Alexey-Ostrovsky Nov 21, 2024

Choose a reason for hiding this comment

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

"Is either collected by the validators of the destination shardchain (if the message is delivered via IHR) or credited to the destination account."

I guess there is no contradiction here, but if you want so, i can add a tip.

Choose a reason for hiding this comment

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

yes, there are no contradictions, but considering that ihr is not working now, and ihr_fee can still be set, I would add a tip with the clarification that this is the state as of October 2024

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tip added.


```cpp
msg_fwd_fees = (lump_price
+ ceil(
(bit_price * msg.bits + cell_price * msg.cells) / 2^16)
);

ihr_fwd_fees = ceil((msg_fwd_fees * ihr_price_factor) / 2^16);

total_fwd_fees = msg_fwd_fees + ihr_fwd_fees; // ihr_fwd_fees - is 0 for external messages
```

## Action fee

Action fee is deducted from the balance of the source account during processing action list which is perfomed after Computing phase.
These are the actions that lead to pay fees:
The action fee is deducted from the balance of the source account during the processing of the action list, which occurs after the Computing phase. Practically, the only action for which you pay an action fee is `SENDRAWMSG`. Other actions, such as `RAWRESERVE` or `SETCODE`, do not incur any fee during the action phase.

Choose a reason for hiding this comment

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

Формально за SENDMSG тоже нужно платить action_fee, но я думаю это не столь обязательно уточнять

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is SENDMSG action? I see only SENDRAWMSG both in TVM whitepapers - https://docs.ton.org/tvm.pdf,
and in official implementation transaction.cpp - https://github.com/ton-blockchain/ton/blob/master/crypto/block/transaction.cpp

Copy link
Contributor Author

@Alexey-Ostrovsky Alexey-Ostrovsky Nov 21, 2024

Choose a reason for hiding this comment

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

I guess the misconception here is the existence of two TVM instructions - SENDMSG and SENDRAWMSG, that create output actions. Nevertheless, in relation to the c5 register and action phase, we are talking about actions
themselves, one of which is the namesake SENDRAWMSG. Those are different entities. But if you think this will be a widespread misconception, we can add a WARN section describing that.

Choose a reason for hiding this comment

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

Yes, my bad, indeed, I mixed up action and tvm instruction. I think there is no need to add anything here


* `SENDRAWMSG` sends a raw message.
* `RAWRESERVE` creates an output action which would reserve N Nanotons.
* `RAWRESERVEX` similar to `RAWRESERVE`, but also accepts a dictionary with extra currencies.
* `SETCODE` creates an output action that would change this smart contract code.
* `SETLIBCODE` creates an output action that would modify the collection of this smart contract libraries by adding or removing library with given code.
* `CHANGELIB` creates an output action similarly to `SETLIBCODE`, but instead of the library code accepts its hash.
* `FB08–FB3F` reserved for output action primitives.
```cpp
action_fee = floor((msg_fwd_fees * first_frac)/ 2^16); //internal

action_fee = msg_fwd_fees; //external
```

[`first_frac`](/v3/documentation/network/configs/blockchain-configs#param-24-and-25) is part of the 24 and 25 parameters (for master chain and work chain) of the TON Blockchain. Currently, both are set to a value of 21845, which means that the `action_fee` is approximately a third of the `msg_fwd_fees`. In the case of an external message action, `SENDRAWMSG`, the `action_fee` is equal to the `msg_fwd_fees`.

:::tip
Remember that an action register can contain up to 255 actions, which means that all formulas related to `fwd_fee` and `action_fee` will be computed for each `SENDRAWMSG` action, resulting in the following sum:

```cpp
total_fees = sum(action_fee) + sum(total_fwd_fees);
```
:::

Starting from the fourth [global version](https://github.com/ton-blockchain/ton/blob/master/doc/GlobalVersions.md) of TON, if a "send message" action fails, the account is required to pay for processing the cells of the message, referred to as the `action_fine`.

```cpp
fine_per_cell = floor((cell_price >> 16) / 4)

max_cells = floor(remaining_balance / fine_per_cell)

action_fine = fine_per_cell * min(max_cells, cells_in_msg);
```

## Fee's calculation formulas

Expand All @@ -257,16 +270,6 @@ storage_fees = ceil(
* period / 2 ^ 16)
```

### in_fwd_fees, out_fwd_fees
```cpp
msg_fwd_fees = (lump_price
+ ceil(
(bit_price * msg.bits + cell_price * msg.cells) / 2^16)
)

ihr_fwd_fees = ceil((msg_fwd_fees * ihr_price_factor) / 2^16)
```

:::info
Only unique hash cells are counted for storage and fwd fees i.e. 3 identical hash cells are counted as one.

Expand All @@ -277,12 +280,6 @@ Read more about [deduplication](/v3/documentation/data-formats/tlb/library-cells

// bits in the root cell of a message are not included in msg.bits (lump_price pays for them)

### action_fees

```cpp
action_fees = sum(out_ext_msg_fwd_fee) + sum(int_msg_mine_fee)
```

## Fee's config file

All fees are nominated in nanotons or nanotons multiplied by 2^16 to [maintain accuracy while using integer](/v3/documentation/smart-contracts/transaction-fees/fees-low-level#forward-fees) and may be changed. The config file represents the current fee cost.
Expand Down
Loading