2022-01-18 14:40:02 +00:00
|
|
|
# Nimbus
|
2024-02-15 02:57:05 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2022-01-18 14:40:02 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## Transaction Pool Item Container & Wrapper
|
|
|
|
## =========================================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2024-02-16 09:08:07 +00:00
|
|
|
std/[hashes, times],
|
2023-01-30 22:10:23 +00:00
|
|
|
../../utils/ec_recover,
|
2024-02-16 09:08:07 +00:00
|
|
|
../../utils/utils,
|
2024-07-31 07:33:30 +00:00
|
|
|
../../transaction,
|
2022-01-18 14:40:02 +00:00
|
|
|
./tx_info,
|
2024-10-04 14:34:31 +00:00
|
|
|
eth/common/transaction_utils,
|
2024-05-30 12:54:03 +00:00
|
|
|
results
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TxItemStatus* = enum ##\
|
|
|
|
## Current status of a transaction as seen by the pool.
|
|
|
|
txItemPending = 0
|
|
|
|
txItemStaged
|
|
|
|
txItemPacked
|
|
|
|
|
|
|
|
TxItemRef* = ref object of RootObj ##\
|
|
|
|
## Data container with transaction and meta data. Entries are *read-only*\
|
|
|
|
## by default, for some there is a setter available.
|
2024-05-15 03:07:59 +00:00
|
|
|
tx: PooledTransaction ## Transaction data
|
2024-10-16 01:34:12 +00:00
|
|
|
itemID: Hash32 ## Transaction hash
|
2022-01-18 14:40:02 +00:00
|
|
|
timeStamp: Time ## Time when added
|
2024-10-16 01:34:12 +00:00
|
|
|
sender: Address ## Sender account address
|
2022-01-18 14:40:02 +00:00
|
|
|
info: string ## Whatever
|
|
|
|
status: TxItemStatus ## Transaction status (setter available)
|
|
|
|
reject: TxInfo ## Reason for moving to waste basket
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private, helpers for debugging and pretty printing
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc utcTime: Time =
|
|
|
|
getTime().utc.toTime
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, Constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc init*(item: TxItemRef; status: TxItemStatus; info: string) =
|
|
|
|
## Update item descriptor.
|
|
|
|
item.info = info
|
|
|
|
item.status = status
|
|
|
|
item.timeStamp = utcTime()
|
|
|
|
item.reject = txInfoOk
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc new*(T: type TxItemRef; tx: PooledTransaction; itemID: Hash32;
|
2023-01-30 22:10:23 +00:00
|
|
|
status: TxItemStatus; info: string): Result[T,void] {.gcsafe,raises: [].} =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Create item descriptor.
|
2024-10-04 14:34:31 +00:00
|
|
|
let rc = tx.tx.recoverSender()
|
2022-01-18 14:40:02 +00:00
|
|
|
if rc.isErr:
|
|
|
|
return err()
|
|
|
|
ok(T(itemID: itemID,
|
|
|
|
tx: tx,
|
|
|
|
sender: rc.value,
|
|
|
|
timeStamp: utcTime(),
|
|
|
|
info: info,
|
|
|
|
status: status))
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc new*(T: type TxItemRef; tx: PooledTransaction;
|
2023-01-30 22:10:23 +00:00
|
|
|
reject: TxInfo; status: TxItemStatus; info: string): T {.gcsafe,raises: [].} =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Create incomplete item descriptor, so meta-data can be stored (e.g.
|
|
|
|
## for holding in the waste basket to be investigated later.)
|
|
|
|
T(tx: tx,
|
|
|
|
timeStamp: utcTime(),
|
|
|
|
info: info,
|
|
|
|
status: status)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, Table ID helper
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc hash*(item: TxItemRef): Hash =
|
|
|
|
## Needed if `TxItemRef` is used as hash-`Table` index.
|
|
|
|
cast[pointer](item).hash
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, transaction getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc itemID*(tx: Transaction): Hash32 =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter, transaction ID
|
|
|
|
tx.rlpHash
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc itemID*(tx: PooledTransaction): Hash32 =
|
2024-05-15 03:07:59 +00:00
|
|
|
## Getter, transaction ID
|
2024-10-16 01:34:12 +00:00
|
|
|
tx.rlpHash
|
2024-05-15 03:07:59 +00:00
|
|
|
|
2022-01-18 14:40:02 +00:00
|
|
|
# core/types/transaction.go(297): func (tx *Transaction) Cost() *big.Int {
|
|
|
|
proc cost*(tx: Transaction): UInt256 =
|
|
|
|
## Getter (go/ref compat): gas * gasPrice + value.
|
|
|
|
(tx.gasPrice * tx.gasLimit).u256 + tx.value
|
|
|
|
|
2024-07-31 07:33:30 +00:00
|
|
|
func effectiveGasTip*(tx: Transaction; baseFee: GasInt): GasInt =
|
|
|
|
effectiveGasTip(tx, Opt.some(baseFee.u256))
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, item getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc dup*(item: TxItemRef): TxItemRef =
|
|
|
|
## Getter, provide contents copy
|
2024-02-15 02:57:05 +00:00
|
|
|
TxItemRef(
|
|
|
|
tx: item.tx,
|
|
|
|
itemID: item.itemID,
|
|
|
|
timeStamp: item.timeStamp,
|
|
|
|
sender: item.sender,
|
|
|
|
info: item.info,
|
|
|
|
status: item.status,
|
|
|
|
reject: item.reject
|
|
|
|
)
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
proc info*(item: TxItemRef): string =
|
|
|
|
## Getter
|
|
|
|
item.info
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc itemID*(item: TxItemRef): Hash32 =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
item.itemID
|
|
|
|
|
|
|
|
proc reject*(item: TxItemRef): TxInfo =
|
|
|
|
## Getter
|
|
|
|
item.reject
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc sender*(item: TxItemRef): Address =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
item.sender
|
|
|
|
|
|
|
|
proc status*(item: TxItemRef): TxItemStatus =
|
|
|
|
## Getter
|
|
|
|
item.status
|
|
|
|
|
|
|
|
proc timeStamp*(item: TxItemRef): Time =
|
|
|
|
## Getter
|
|
|
|
item.timeStamp
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc pooledTx*(item: TxItemRef): PooledTransaction =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
item.tx
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc tx*(item: TxItemRef): Transaction =
|
|
|
|
## Getter
|
|
|
|
item.tx.tx
|
|
|
|
|
2023-10-20 08:30:05 +00:00
|
|
|
func rejectInfo*(item: TxItemRef): string =
|
|
|
|
## Getter
|
|
|
|
result = $item.reject
|
|
|
|
if item.info.len > 0:
|
|
|
|
result.add ": "
|
|
|
|
result.add item.info
|
|
|
|
|
2022-01-18 14:40:02 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `status=`*(item: TxItemRef; val: TxItemStatus) =
|
|
|
|
## Setter
|
|
|
|
item.status = val
|
|
|
|
|
|
|
|
proc `reject=`*(item: TxItemRef; val: TxInfo) =
|
|
|
|
## Setter
|
|
|
|
item.reject = val
|
|
|
|
|
2023-10-20 08:30:05 +00:00
|
|
|
proc `info=`*(item: TxItemRef; val: string) =
|
|
|
|
## Setter
|
|
|
|
item.info = val
|
|
|
|
|
2022-01-18 14:40:02 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, pretty printing and debugging
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `$`*(w: TxItemRef): string =
|
|
|
|
## Visualise item ID (use for debugging)
|
2024-02-16 09:08:07 +00:00
|
|
|
"<" & w.itemID.short & ">"
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|