-
Notifications
You must be signed in to change notification settings - Fork 465
/
CompareReplacedBlobTx.cs
37 lines (29 loc) · 1.71 KB
/
CompareReplacedBlobTx.cs
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
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System.Collections.Generic;
using Nethermind.Core;
namespace Nethermind.TxPool.Comparison;
/// <summary>
/// Compare fee of newcomer blob transaction with fee of transaction intended to be replaced
/// </summary>
public class CompareReplacedBlobTx : IComparer<Transaction?>
{
public static readonly CompareReplacedBlobTx Instance = new();
private CompareReplacedBlobTx() { }
// To replace old blob transaction, new transaction needs to have fee at least 2x higher than current fee.
// 2x higher must be MaxPriorityFeePerGas, MaxFeePerGas and MaxFeePerDataGas
public int Compare(Transaction? newTx, Transaction? oldTx)
{
if (ReferenceEquals(newTx, oldTx)) return TxComparisonResult.NotDecided;
if (oldTx is null) return TxComparisonResult.KeepOld;
if (newTx is null) return TxComparisonResult.TakeNew;
// do not allow to replace blob tx by the one with lower number of blobs
if (oldTx.BlobVersionedHashes is null || newTx.BlobVersionedHashes is null) return TxComparisonResult.KeepOld;
if (oldTx.BlobVersionedHashes.Length > newTx.BlobVersionedHashes.Length) return TxComparisonResult.KeepOld;
if (oldTx.MaxFeePerGas * 2 > newTx.MaxFeePerGas) return TxComparisonResult.KeepOld;
if (oldTx.MaxPriorityFeePerGas * 2 > newTx.MaxPriorityFeePerGas) return TxComparisonResult.KeepOld;
if (oldTx.MaxFeePerBlobGas * 2 > newTx.MaxFeePerBlobGas) return TxComparisonResult.KeepOld;
// if we are here, it means that all new fees are at least 2x higher than old ones, so replacement is allowed
return TxComparisonResult.TakeNew;
}
}