Skip to content

Commit c41b638

Browse files
TinyChain --> baseFeePerGas position fixed.
1 parent 91b7d75 commit c41b638

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/TinyChain/Block.mjs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
2121
* @typedef {Object} NewTransactionData
2222
* @property {bigint | number | string} gasLimit - Max gas allowed for transactions.
2323
* @property {bigint | number | string} gasUsed - Actual gas used.
24-
* @property {bigint | number | string} baseFeePerGas - Base fee per gas unit.
2524
* @property {bigint | number | string} maxPriorityFeePerGas - Priority fee paid to the miner.
2625
* @property {bigint | number | string} maxFeePerGas - Max total fee per gas unit allowed.
2726
* @property {string} address - Hex address that created the tx.
@@ -34,7 +33,6 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
3433
* @typedef {Object} TransactionData
3534
* @property {bigint} gasLimit - Max gas allowed for transactions.
3635
* @property {bigint} gasUsed - Actual gas used.
37-
* @property {bigint} baseFeePerGas - Base fee per gas unit.
3836
* @property {bigint} maxPriorityFeePerGas - Priority fee paid to the miner.
3937
* @property {bigint} maxFeePerGas - Max total fee per gas unit allowed.
4038
* @property {string} address - Hex address that created the block.
@@ -57,6 +55,7 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
5755
* @property {bigint | number | string} [reward=0n] - Block reward.
5856
* @property {bigint | number | string} [nonce=0n] - Starting nonce.
5957
* @property {bigint | number | string} [chainId] - The chain ID.
58+
* @property {bigint | number | string} [baseFeePerGas] - Base fee per gas unit.
6059
* @property {TxIndexMap} [txs] - A map where each key is a transaction index.
6160
* @property {SignIndexMap} [sigs] - A map where each key is a transaction signature.
6261
* @property {number} [time=Date.now()] - Unix timestamp of the block.
@@ -76,6 +75,7 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
7675
* reward: bigint,
7776
* miner: string|null,
7877
* chainId: bigint,
78+
* baseFeePerGas: bigint,
7979
* txs: TxIndexMap,
8080
* sigs: SignIndexMap,
8181
* }} GetTransactionData
@@ -184,13 +184,7 @@ class TinyChainBlock {
184184
throw new Error(`"payload" in data entry at index ${index} must be a string.`);
185185

186186
/** @type {Array<string>} */
187-
const bigintFields = [
188-
'gasLimit',
189-
'gasUsed',
190-
'baseFeePerGas',
191-
'maxFeePerGas',
192-
'maxPriorityFeePerGas',
193-
];
187+
const bigintFields = ['gasLimit', 'gasUsed', 'maxFeePerGas', 'maxPriorityFeePerGas'];
194188

195189
for (const field of bigintFields) {
196190
// @ts-ignore
@@ -203,8 +197,6 @@ class TinyChainBlock {
203197

204198
const gasLimit = typeof t.gasLimit === 'bigint' ? t.gasLimit : BigInt(t.gasLimit);
205199
const gasUsed = typeof t.gasUsed === 'bigint' ? t.gasUsed : BigInt(t.gasUsed);
206-
const baseFeePerGas =
207-
typeof t.baseFeePerGas === 'bigint' ? t.baseFeePerGas : BigInt(t.baseFeePerGas);
208200
const maxFeePerGas =
209201
typeof t.maxFeePerGas === 'bigint' ? t.maxFeePerGas : BigInt(t.maxFeePerGas);
210202
const maxPriorityFeePerGas =
@@ -219,7 +211,6 @@ class TinyChainBlock {
219211
payload: this.#payloadString ? t.payload : undefined,
220212
gasLimit,
221213
gasUsed,
222-
baseFeePerGas,
223214
maxFeePerGas,
224215
maxPriorityFeePerGas,
225216
};
@@ -286,6 +277,7 @@ class TinyChainBlock {
286277
index = 0n,
287278
prevHash = '',
288279
diff = 1n,
280+
baseFeePerGas = 0n,
289281
reward = 0n,
290282
nonce = 0n,
291283
hash = null,
@@ -307,6 +299,12 @@ class TinyChainBlock {
307299
if (typeof index !== 'bigint' && !(typeof index === 'string' && /^[0-9]+$/.test(index)))
308300
throw new Error('index must be a bigint or a numeric string.');
309301
this.index = typeof index === 'bigint' ? index : BigInt(index);
302+
if (
303+
typeof baseFeePerGas !== 'bigint' &&
304+
!(typeof baseFeePerGas === 'string' && /^[0-9]+$/.test(baseFeePerGas))
305+
)
306+
throw new Error('baseFeePerGas must be a bigint or a numeric string.');
307+
this.baseFeePerGas = typeof baseFeePerGas === 'bigint' ? baseFeePerGas : BigInt(baseFeePerGas);
310308
if (typeof prevHash !== 'string') throw new Error('prevHash must be a hash string.');
311309
this.prevHash = prevHash;
312310
if (typeof diff !== 'bigint' && !(typeof diff === 'string' && /^[0-9]+$/.test(diff)))
@@ -393,6 +391,7 @@ class TinyChainBlock {
393391
index: this.getIndex(),
394392
time: this.getTime(),
395393
data: this.getData(),
394+
baseFeePerGas: this.getBaseFeePerGas(),
396395
prevHash: this.getPrevHash(),
397396
diff: this.getDiff(),
398397
nonce: this.getNonce(),
@@ -424,6 +423,7 @@ class TinyChainBlock {
424423
value += this.#parser.serializeDeep(this.data);
425424
value += this.#parser.serialize(this.sigs);
426425
value += this.getIndex().toString();
426+
value += this.getBaseFeePerGas().toString();
427427
value += this.getNonce().toString();
428428
value += this.getChainId().toString();
429429
return createHash('sha256').update(Buffer.from(value, 'utf-8')).digest('hex');
@@ -664,6 +664,15 @@ class TinyChainBlock {
664664
if (typeof this.hash !== 'string') throw new Error('Hash must be a string');
665665
return this.hash;
666666
}
667+
668+
/**
669+
* Returns the base fee per gas (in gwei).
670+
* @returns {bigint}
671+
*/
672+
getBaseFeePerGas() {
673+
if (typeof this.baseFeePerGas !== 'bigint') throw new Error('baseFeePerGas must be a bigint');
674+
return this.baseFeePerGas;
675+
}
667676
}
668677

669678
export default TinyChainBlock;

src/TinyChain/Instance.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ class TinyChainInstance {
585585
payload: 'Genesis Block',
586586
gasLimit: 0n,
587587
gasUsed: 0n,
588-
baseFeePerGas: 0n,
589588
maxFeePerGas: 0n,
590589
maxPriorityFeePerGas: 0n,
591590
};
@@ -595,6 +594,7 @@ class TinyChainInstance {
595594
const block = this.#createBlockInstance({
596595
index: 0n,
597596
prevHash: '0',
597+
baseFeePerGas: 0n,
598598
firstValidation: false,
599599
data: [data],
600600
sigs: [sig.toString('hex')],
@@ -636,6 +636,7 @@ class TinyChainInstance {
636636
*/
637637
#createBlockInstance(options) {
638638
return new TinyChainBlock({
639+
baseFeePerGas: this.isCurrencyMode() ? this.getBaseFeePerGas() : 0n,
639640
payloadString: this.#payloadString,
640641
parser: this.#parser,
641642
signer: this.#signer,
@@ -889,7 +890,6 @@ class TinyChainInstance {
889890
payload,
890891
gasLimit: isCurrencyMode ? gasLimit : 0n,
891892
gasUsed,
892-
baseFeePerGas: isCurrencyMode ? this.getBaseFeePerGas() : 0n,
893893
maxFeePerGas: isCurrencyMode ? maxFeePerGas : 0n,
894894
maxPriorityFeePerGas: isCurrencyMode ? maxPriorityFeePerGas : 0n,
895895
};
@@ -939,7 +939,6 @@ class TinyChainInstance {
939939
if (!Buffer.isBuffer(sig) && typeof sig !== 'string')
940940
throw new Error(`Signature at index ${index} must be a Buffer or hex string`);
941941

942-
data.baseFeePerGas = isCurrencyMode ? this.getBaseFeePerGas() : 0n;
943942
dataList.push(data);
944943
sigs.push(typeof sig === 'string' ? sig : sig.toString('hex'));
945944
}
@@ -1130,12 +1129,15 @@ class TinyChainInstance {
11301129
updateBalance(block, balances = this.#getBalances(), emitEvents = true) {
11311130
const reward = block.reward;
11321131
const minerAddress = block.miner;
1132+
const baseFeePerGas = block.baseFeePerGas;
11331133
const isMinerAddress = typeof minerAddress === 'string' && minerAddress.length > 0;
11341134

11351135
if (isMinerAddress && !this.#signer.validateAddress(minerAddress, 'guess').valid)
11361136
throw new Error('Invalid miner address!');
11371137
if (typeof reward !== 'bigint')
11381138
throw new Error(`Invalid reward: expected a BigInt, got "${typeof reward}"`);
1139+
if (typeof baseFeePerGas !== 'bigint')
1140+
throw new Error(`Invalid baseFeePerGas: expected a BigInt, got "${typeof baseFeePerGas}"`);
11391141
if (typeof minerAddress !== 'string' && minerAddress !== null)
11401142
throw new Error(
11411143
`Invalid minerAddress: expected a string or null, got "${typeof minerAddress}"`,
@@ -1166,7 +1168,6 @@ class TinyChainInstance {
11661168
`Gas limit exceeded: used ${data.gasUsed} > limit ${data.gasLimit} for sender "${execAddress}"`,
11671169
);
11681170

1169-
const baseFeePerGas = data.baseFeePerGas;
11701171
const maxPriorityFeePerGas = data.maxPriorityFeePerGas;
11711172
const maxFeePerGas = data.maxFeePerGas; // user sets this
11721173

0 commit comments

Comments
 (0)