-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathgas.ts
244 lines (209 loc) · 6.94 KB
/
gas.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { bn } from '@fuel-ts/math';
import type { BN, BNInput } from '@fuel-ts/math';
import { ReceiptType, type Input } from '@fuel-ts/transactions';
import { arrayify } from '@fuel-ts/utils';
import type {
GqlDependentCost,
GqlHeavyOperation,
GqlLightOperation,
} from '../__generated__/operations';
import type { GasCosts } from '../provider';
import type { TransactionRequestInput } from '../transaction-request';
import type {
TransactionResultReceipt,
TransactionResultScriptResultReceipt,
} from '../transaction-response';
/** @hidden */
export const getGasUsedFromReceipts = (receipts: Array<TransactionResultReceipt>): BN => {
const scriptResult = receipts.filter(
(receipt) => receipt.type === ReceiptType.ScriptResult
) as TransactionResultScriptResultReceipt[];
const gasUsed = scriptResult.reduce((prev, receipt) => prev.add(receipt.gasUsed), bn(0));
return gasUsed;
};
export function resolveGasDependentCosts(byteSize: BNInput, gasDependentCost: GqlDependentCost) {
const base = bn(gasDependentCost.base);
let dependentValue = bn(0);
if ('unitsPerGas' in gasDependentCost) {
dependentValue = bn(byteSize).div(bn((<GqlLightOperation>gasDependentCost).unitsPerGas));
} else {
dependentValue = bn(byteSize).mul(bn((<GqlHeavyOperation>gasDependentCost).gasPerUnit));
}
return base.add(dependentValue);
}
export function gasUsedByInputs(
inputs: Array<TransactionRequestInput | Input>,
txBytesSize: number,
gasCosts: GasCosts
) {
const witnessCache: Array<number> = [];
const chargeableInputs = inputs.filter((input) => {
const isCoinOrMessage = 'owner' in input || 'sender' in input;
if (isCoinOrMessage) {
if ('predicate' in input && input.predicate && input.predicate !== '0x') {
return true;
}
if (!witnessCache.includes(input.witnessIndex)) {
// should charge only once for each witness
witnessCache.push(input.witnessIndex);
return true;
}
}
return false;
});
const vmInitializationCost = resolveGasDependentCosts(txBytesSize, gasCosts.vmInitialization);
const totalGas = chargeableInputs.reduce((total, input) => {
if ('predicate' in input && input.predicate && input.predicate !== '0x') {
return total.add(
vmInitializationCost
.add(resolveGasDependentCosts(arrayify(input.predicate).length, gasCosts.contractRoot))
.add(bn(input.predicateGasUsed))
);
}
return total.add(gasCosts.ecr1);
}, bn(0));
// Never allow gas to exceed MAX_U64
return totalGas;
}
export interface IGetMinGasParams {
inputs: Array<TransactionRequestInput | Input>;
gasCosts: GasCosts;
txBytesSize: number;
metadataGas: BN;
gasPerByte: BN;
}
export function getMinGas(params: IGetMinGasParams) {
const { gasCosts, gasPerByte, inputs, metadataGas, txBytesSize } = params;
const vmInitGas = resolveGasDependentCosts(txBytesSize, gasCosts.vmInitialization);
const bytesGas = bn(txBytesSize).mul(gasPerByte);
const inputsGas = gasUsedByInputs(inputs, txBytesSize, gasCosts);
const minGas = vmInitGas.add(bytesGas).add(inputsGas).add(metadataGas).maxU64();
return minGas;
}
export interface IGetMaxGasParams {
witnessesLength: number;
witnessLimit?: BN;
gasPerByte: BN;
minGas: BN;
gasLimit?: BN;
blobSize?: BN;
maxGasPerTx: BN;
}
export function getMaxGas(params: IGetMaxGasParams) {
const {
gasPerByte,
witnessesLength,
witnessLimit,
minGas,
gasLimit = bn(0),
maxGasPerTx,
} = params;
let remainingAllowedWitnessGas = bn(0);
if (witnessLimit?.gt(0) && witnessLimit.gte(witnessesLength)) {
remainingAllowedWitnessGas = bn(witnessLimit).sub(witnessesLength).mul(gasPerByte);
}
const maxGas = remainingAllowedWitnessGas.add(minGas).add(gasLimit);
return maxGas.gte(maxGasPerTx) ? maxGasPerTx : maxGas;
}
export function calculateMetadataGasForTxCreate({
gasCosts,
stateRootSize,
txBytesSize,
contractBytesSize,
}: {
gasCosts: GasCosts;
contractBytesSize: BN;
stateRootSize: number;
txBytesSize: number;
}) {
const contractRootGas = resolveGasDependentCosts(contractBytesSize, gasCosts.contractRoot);
const stateRootGas = resolveGasDependentCosts(stateRootSize, gasCosts.stateRoot);
const txIdGas = resolveGasDependentCosts(txBytesSize, gasCosts.s256);
// See https://github.com/FuelLabs/fuel-specs/blob/master/src/identifiers/contract-id.md
const contractIdInputSize = bn(4 + 32 + 32 + 32);
const contractIdGas = resolveGasDependentCosts(contractIdInputSize, gasCosts.s256);
const metadataGas = contractRootGas.add(stateRootGas).add(txIdGas).add(contractIdGas);
return metadataGas.maxU64();
}
export function calculateMetadataGasForTxScript({
gasCosts,
txBytesSize,
}: {
gasCosts: GasCosts;
txBytesSize: number;
}) {
return resolveGasDependentCosts(txBytesSize, gasCosts.s256);
}
export function calculateMetadataGasForTxBlob({
gasCosts,
txBytesSize,
witnessBytesSize,
}: {
gasCosts: GasCosts;
txBytesSize: number;
witnessBytesSize: number;
}) {
const txId = resolveGasDependentCosts(txBytesSize, gasCosts.s256);
const blobLen = resolveGasDependentCosts(witnessBytesSize, gasCosts.s256);
return txId.add(blobLen);
}
export function calculateMetadataGasForTxUpgrade({
gasCosts,
txBytesSize,
consensusSize,
}: {
gasCosts: GasCosts;
txBytesSize: number;
consensusSize?: number;
}) {
const txId = resolveGasDependentCosts(txBytesSize, gasCosts.s256);
if (consensusSize) {
const consensusLen = resolveGasDependentCosts(consensusSize, gasCosts.s256);
txId.add(consensusLen);
}
return txId;
}
export function calculateMetadataGasForTxUpload({
gasCosts,
txBytesSize,
subsectionSize,
subsectionsSize,
}: {
gasCosts: GasCosts;
txBytesSize: number;
subsectionSize: number;
subsectionsSize: number;
}) {
const txId = resolveGasDependentCosts(txBytesSize, gasCosts.s256);
const subsectionLen = resolveGasDependentCosts(subsectionSize, gasCosts.s256);
txId.add(subsectionLen);
const subsectionsLen = resolveGasDependentCosts(subsectionsSize, gasCosts.stateRoot);
txId.add(subsectionsLen);
return txId;
}
export function calculateMinGasForTxUpload({
gasCosts,
baseMinGas,
subsectionSize,
}: {
gasCosts: GasCosts;
baseMinGas: number;
subsectionSize: number;
}) {
// Since the `Upload` transaction occupies much of the storage, we want to
// discourage people from using it too much. For that, we charge additional gas
// for the storage.
// https://github.com/FuelLabs/fuel-vm/blob/6e137db6387bd291b9505d17e15e0f2edda7957e/fuel-tx/src/transaction/types/upload.rs#L135-L150
const additionalStoragePerByte = bn(gasCosts.newStoragePerByte).mul(subsectionSize);
return bn(baseMinGas).add(additionalStoragePerByte);
}
export interface CalculateGasFeeParams {
tip?: BN;
gas: BN;
gasPrice: BN;
priceFactor: BN;
}
export const calculateGasFee = (params: CalculateGasFeeParams) => {
const { gas, gasPrice, priceFactor, tip } = params;
return gas.mul(gasPrice).div(priceFactor).add(bn(tip));
};