preprocess

package
v0.0.0-...-08f901d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2024 License: GPL-3.0 Imports: 31 Imported by: 0

Documentation

Overview

Example (SortTransactionsBySenderAndNonce)
txs := []*txcache.WrappedTransaction{
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: []byte("bbbb")}, TxHash: []byte("w")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: []byte("aaaa")}, TxHash: []byte("x")},
	{Tx: &transaction.Transaction{Nonce: 5, SndAddr: []byte("bbbb")}, TxHash: []byte("y")},
	{Tx: &transaction.Transaction{Nonce: 2, SndAddr: []byte("aaaa")}, TxHash: []byte("z")},
	{Tx: &transaction.Transaction{Nonce: 7, SndAddr: []byte("aabb")}, TxHash: []byte("t")},
	{Tx: &transaction.Transaction{Nonce: 6, SndAddr: []byte("aabb")}, TxHash: []byte("a")},
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: []byte("ffff")}, TxHash: []byte("b")},
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: []byte("eeee")}, TxHash: []byte("c")},
}

sortTransactionsBySenderAndNonceLegacy(txs)

for _, item := range txs {
	fmt.Println(item.Tx.GetNonce(), string(item.Tx.GetSndAddr()), string(item.TxHash))
}
Output:

1 aaaa x
2 aaaa z
6 aabb a
7 aabb t
3 bbbb w
5 bbbb y
3 eeee c
3 ffff b
Example (SortTransactionsBySenderAndNonce2)
txs := []*txcache.WrappedTransaction{
	{Tx: &transaction.Transaction{Nonce: 2, SndAddr: []byte("bbbb")}, TxHash: []byte("w")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: []byte("aaaa")}, TxHash: []byte("x")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: []byte("bbbb")}, TxHash: []byte("y")},
	{Tx: &transaction.Transaction{Nonce: 2, SndAddr: []byte("aaaa")}, TxHash: []byte("z")},
	{Tx: &transaction.Transaction{Nonce: 7, SndAddr: []byte("aabb")}, TxHash: []byte("t")},
	{Tx: &transaction.Transaction{Nonce: 6, SndAddr: []byte("aabb")}, TxHash: []byte("a")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: []byte("ffff")}, TxHash: []byte("b")},
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: []byte("eeee")}, TxHash: []byte("c")},
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: []byte("bbbb")}, TxHash: []byte("c")},
}

sortTransactionsBySenderAndNonceLegacy(txs)

for _, item := range txs {
	fmt.Println(item.Tx.GetNonce(), string(item.Tx.GetSndAddr()), string(item.TxHash))
}
Output:

1 aaaa x
2 aaaa z
6 aabb a
7 aabb t
1 bbbb y
2 bbbb w
3 bbbb c
3 eeee c
1 ffff b
Example (SortTransactionsBySenderAndNonceWithFrontRunningProtection)
randomness := "randomness"
txPreproc := transactions{
	basePreProcess: &basePreProcess{
		hasher: &mock.HasherStub{
			ComputeCalled: func(s string) []byte {
				if s == randomness {
					return []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
				}

				return []byte(s)
			},
		},
	},
}

nbSenders := 5

usedRandomness := txPreproc.hasher.Compute(randomness)
senders := make([][]byte, 0)
for i := 0; i < nbSenders; i++ {
	sender := make([]byte, len(usedRandomness))
	copy(sender, usedRandomness)
	sender[len(usedRandomness)-1-i] = 0
	senders = append(senders, sender)
}

txs := []*txcache.WrappedTransaction{
	{Tx: &transaction.Transaction{Nonce: 2, SndAddr: senders[2]}, TxHash: []byte("w")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: senders[0]}, TxHash: []byte("x")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: senders[2]}, TxHash: []byte("y")},
	{Tx: &transaction.Transaction{Nonce: 2, SndAddr: senders[0]}, TxHash: []byte("z")},
	{Tx: &transaction.Transaction{Nonce: 7, SndAddr: senders[1]}, TxHash: []byte("t")},
	{Tx: &transaction.Transaction{Nonce: 6, SndAddr: senders[1]}, TxHash: []byte("a")},
	{Tx: &transaction.Transaction{Nonce: 1, SndAddr: senders[4]}, TxHash: []byte("b")},
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: senders[3]}, TxHash: []byte("c")},
	{Tx: &transaction.Transaction{Nonce: 3, SndAddr: senders[2]}, TxHash: []byte("c")},
}

txPreproc.sortTransactionsBySenderAndNonceWithFrontRunningProtection(txs, []byte(randomness))

for _, item := range txs {
	fmt.Println(item.Tx.GetNonce(), hex.EncodeToString(item.Tx.GetSndAddr()), string(item.TxHash))
}
Output:

1 ffffffffffffffffffffffffffffff00 x
2 ffffffffffffffffffffffffffffff00 z
6 ffffffffffffffffffffffffffff00ff a
7 ffffffffffffffffffffffffffff00ff t
1 ffffffffffffffffffffffffff00ffff y
2 ffffffffffffffffffffffffff00ffff w
3 ffffffffffffffffffffffffff00ffff c
3 ffffffffffffffffffffffff00ffffff c
1 ffffffffffffffffffffff00ffffffff b

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBalanceComputation

func NewBalanceComputation() (*balanceComputation, error)

NewBalanceComputation creates a new object which computes the addresses balances

func NewBlockSizeComputation

func NewBlockSizeComputation(
	marshalizer marshal.Marshalizer,
	blockSizeThrottler BlockSizeThrottler,
	maxSize uint32,
) (*blockSizeComputation, error)

NewBlockSizeComputation creates a blockSizeComputation instance

func NewGasComputation

func NewGasComputation(
	economicsFee process.FeeHandler,
	txTypeHandler process.TxTypeHandler,
	enableEpochsHandler common.EnableEpochsHandler,
) (*gasComputation, error)

NewGasComputation creates a new object which computes the gas consumption

func NewRewardTxPreprocessor

func NewRewardTxPreprocessor(
	rewardTxDataPool dataRetriever.ShardedDataCacherNotifier,
	store dataRetriever.StorageService,
	hasher hashing.Hasher,
	marshalizer marshal.Marshalizer,
	rewardProcessor process.RewardTransactionProcessor,
	shardCoordinator sharding.Coordinator,
	accounts state.AccountsAdapter,
	onRequestRewardTransaction func(shardID uint32, txHashes [][]byte),
	gasHandler process.GasHandler,
	pubkeyConverter core.PubkeyConverter,
	blockSizeComputation BlockSizeComputationHandler,
	balanceComputation BalanceComputationHandler,
	processedMiniBlocksTracker process.ProcessedMiniBlocksTracker,
	txExecutionOrderHandler common.TxExecutionOrderHandler,
) (*rewardTxPreprocessor, error)

NewRewardTxPreprocessor creates a new reward transaction preprocessor object

func NewScheduledTxsExecution

func NewScheduledTxsExecution(
	txProcessor process.TransactionProcessor,
	txCoordinator process.TransactionCoordinator,
	storer storage.Storer,
	marshaller marshal.Marshalizer,
	hasher hashing.Hasher,
	shardCoordinator sharding.Coordinator,
	txExecutionOrderHandler common.TxExecutionOrderHandler,
) (*scheduledTxsExecution, error)

NewScheduledTxsExecution creates a new object which handles the execution of scheduled transactions

func NewSmartContractResultPreprocessor

func NewSmartContractResultPreprocessor(
	scrDataPool dataRetriever.ShardedDataCacherNotifier,
	store dataRetriever.StorageService,
	hasher hashing.Hasher,
	marshalizer marshal.Marshalizer,
	scrProcessor process.SmartContractResultProcessor,
	shardCoordinator sharding.Coordinator,
	accounts state.AccountsAdapter,
	onRequestSmartContractResult func(shardID uint32, txHashes [][]byte),
	gasHandler process.GasHandler,
	economicsFee process.FeeHandler,
	pubkeyConverter core.PubkeyConverter,
	blockSizeComputation BlockSizeComputationHandler,
	balanceComputation BalanceComputationHandler,
	enableEpochsHandler common.EnableEpochsHandler,
	processedMiniBlocksTracker process.ProcessedMiniBlocksTracker,
	txExecutionOrderHandler common.TxExecutionOrderHandler,
) (*smartContractResults, error)

NewSmartContractResultPreprocessor creates a new smartContractResult preprocessor object

func NewTransactionPreprocessor

func NewTransactionPreprocessor(
	args ArgsTransactionPreProcessor,
) (*transactions, error)

NewTransactionPreprocessor creates a new transaction preprocessor object

func NewValidatorInfoPreprocessor

func NewValidatorInfoPreprocessor(
	hasher hashing.Hasher,
	marshalizer marshal.Marshalizer,
	blockSizeComputation BlockSizeComputationHandler,
	validatorsInfoPool dataRetriever.ShardedDataCacherNotifier,
	store dataRetriever.StorageService,
	enableEpochsHandler common.EnableEpochsHandler,
) (*validatorInfoPreprocessor, error)

NewValidatorInfoPreprocessor creates a new validatorInfo preprocessor object

Types

type ArgsTransactionPreProcessor

type ArgsTransactionPreProcessor struct {
	TxDataPool                   dataRetriever.ShardedDataCacherNotifier
	Store                        dataRetriever.StorageService
	Hasher                       hashing.Hasher
	Marshalizer                  marshal.Marshalizer
	TxProcessor                  process.TransactionProcessor
	ShardCoordinator             sharding.Coordinator
	Accounts                     state.AccountsAdapter
	OnRequestTransaction         func(shardID uint32, txHashes [][]byte)
	EconomicsFee                 process.FeeHandler
	GasHandler                   process.GasHandler
	BlockTracker                 BlockTracker
	BlockType                    block.Type
	PubkeyConverter              core.PubkeyConverter
	BlockSizeComputation         BlockSizeComputationHandler
	BalanceComputation           BalanceComputationHandler
	EnableEpochsHandler          common.EnableEpochsHandler
	TxTypeHandler                process.TxTypeHandler
	ScheduledTxsExecutionHandler process.ScheduledTxsExecutionHandler
	ProcessedMiniBlocksTracker   process.ProcessedMiniBlocksTracker
	TxExecutionOrderHandler      common.TxExecutionOrderHandler
}

ArgsTransactionPreProcessor holds the arguments to create a txs pre processor

type BalanceComputationHandler

type BalanceComputationHandler interface {
	Init()
	SetBalanceToAddress(address []byte, value *big.Int)
	AddBalanceToAddress(address []byte, value *big.Int) bool
	SubBalanceFromAddress(address []byte, value *big.Int) bool
	IsAddressSet(address []byte) bool
	AddressHasEnoughBalance(address []byte, value *big.Int) bool
	IsInterfaceNil() bool
}

BalanceComputationHandler defines the functionality for addresses balances computation, used in preventing executing too many debit transactions, after the proposer executed a credit transaction on the same account in the same block

type BlockSizeComputationHandler

type BlockSizeComputationHandler interface {
	Init()
	AddNumMiniBlocks(numMiniBlocks int)
	AddNumTxs(numTxs int)
	IsMaxBlockSizeReached(numNewMiniBlocks int, numNewTxs int) bool
	IsMaxBlockSizeWithoutThrottleReached(numNewMiniBlocks int, numNewTxs int) bool
	IsInterfaceNil() bool
}

BlockSizeComputationHandler defines the functionality for block size computation

type BlockSizeThrottler

type BlockSizeThrottler interface {
	GetCurrentMaxSize() uint32
	IsInterfaceNil() bool
}

BlockSizeThrottler defines the functionality of adapting the node to the network speed/latency when it should send a block to its peers which should be received in a limited time frame

type BlockTracker

type BlockTracker interface {
	IsShardStuck(shardID uint32) bool
	ShouldSkipMiniBlocksCreationFromSelf() bool
	IsInterfaceNil() bool
}

BlockTracker defines the functionality for node to track the blocks which are received from network

type SortedTransactionsProvider

type SortedTransactionsProvider interface {
	GetSortedTransactions() []*txcache.WrappedTransaction
	NotifyAccountNonce(accountKey []byte, nonce uint64)
	IsInterfaceNil() bool
}

SortedTransactionsProvider defines the public API of the transactions cache

type TxCache

type TxCache interface {
	SelectTransactionsWithBandwidth(numRequested int, batchSizePerSender int, bandwidthPerSender uint64) []*txcache.WrappedTransaction
	NotifyAccountNonce(accountKey []byte, nonce uint64)
	IsInterfaceNil() bool
}

TxCache defines the functionality for the transactions cache

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL