-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define a selection session wrapper to track accounts state, across al…
…l "heap items" (within a selection session).
- Loading branch information
1 parent
2759489
commit ec0db29
Showing
6 changed files
with
141 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package txcache | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/multiversx/mx-chain-core-go/data" | ||
) | ||
|
||
// After moving "mx-chain-storage-go/txcache" into "mx-chain-go", maybe merge this component into "SelectionSession". | ||
type selectionSessionWrapper struct { | ||
session SelectionSession | ||
recordByAddress map[string]*accountBalanceRecord | ||
} | ||
|
||
type accountBalanceRecord struct { | ||
initialNonce uint64 | ||
initialBalance *big.Int | ||
consumedBalance *big.Int | ||
} | ||
|
||
func newSelectionSessionWrapper(session SelectionSession) *selectionSessionWrapper { | ||
return &selectionSessionWrapper{ | ||
session: session, | ||
recordByAddress: make(map[string]*accountBalanceRecord), | ||
} | ||
} | ||
|
||
func (sessionWrapper *selectionSessionWrapper) getAccountRecord(address []byte) *accountBalanceRecord { | ||
record, ok := sessionWrapper.recordByAddress[string(address)] | ||
if ok { | ||
return record | ||
} | ||
|
||
state, err := sessionWrapper.session.GetAccountState(address) | ||
if err != nil { | ||
logSelect.Debug("selectionSessionWrapper.getAccountRecord, could not retrieve account state", "address", address, "err", err) | ||
|
||
record = &accountBalanceRecord{ | ||
initialNonce: 0, | ||
initialBalance: big.NewInt(0), | ||
consumedBalance: big.NewInt(0), | ||
} | ||
} else { | ||
record = &accountBalanceRecord{ | ||
initialNonce: state.Nonce, | ||
initialBalance: state.Balance, | ||
consumedBalance: big.NewInt(0), | ||
} | ||
} | ||
|
||
sessionWrapper.recordByAddress[string(address)] = record | ||
return record | ||
} | ||
|
||
func (sessionWrapper *selectionSessionWrapper) getNonce(address []byte) uint64 { | ||
record := sessionWrapper.getAccountRecord(address) | ||
return record.initialNonce | ||
} | ||
|
||
func (sessionWrapper *selectionSessionWrapper) accumulateConsumedBalance(tx *WrappedTransaction) { | ||
sender := tx.Tx.GetSndAddr() | ||
feePayer := tx.FeePayer | ||
|
||
senderRecord := sessionWrapper.getAccountRecord(sender) | ||
feePayerRecord := sessionWrapper.getAccountRecord(feePayer) | ||
|
||
transferredValue := tx.TransferredValue | ||
if transferredValue != nil { | ||
senderRecord.consumedBalance.Add(senderRecord.consumedBalance, transferredValue) | ||
} | ||
|
||
fee := tx.Fee | ||
if fee != nil { | ||
feePayerRecord.consumedBalance.Add(feePayerRecord.consumedBalance, fee) | ||
} | ||
} | ||
|
||
func (sessionWrapper *selectionSessionWrapper) detectWillFeeExceedBalance(tx *WrappedTransaction) bool { | ||
fee := tx.Fee | ||
if fee == nil { | ||
return false | ||
} | ||
|
||
// Here, we are not interested into an eventual transfer of value (we only check if there's enough balance to pay the transaction fee). | ||
feePayer := tx.FeePayer | ||
feePayerRecord := sessionWrapper.getAccountRecord(feePayer) | ||
|
||
futureConsumedBalance := new(big.Int).Add(feePayerRecord.consumedBalance, fee) | ||
feePayerBalance := feePayerRecord.initialBalance | ||
|
||
willFeeExceedBalance := futureConsumedBalance.Cmp(feePayerBalance) > 0 | ||
if willFeeExceedBalance { | ||
logSelect.Trace("selectionSessionWrapper.detectWillFeeExceedBalance", | ||
"tx", tx.TxHash, | ||
"feePayer", feePayer, | ||
"initialBalance", feePayerRecord.initialBalance, | ||
"consumedBalance", feePayerRecord.consumedBalance, | ||
) | ||
} | ||
|
||
return willFeeExceedBalance | ||
} | ||
|
||
func (sessionWrapper *selectionSessionWrapper) isIncorrectlyGuarded(tx data.TransactionHandler) bool { | ||
return sessionWrapper.session.IsIncorrectlyGuarded(tx) | ||
} |
Oops, something went wrong.