Skip to content

Commit

Permalink
improve: adding logging to Worker
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
  • Loading branch information
Psykepro committed Feb 24, 2023
1 parent 5994a08 commit c16b85a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion sequencer/addrqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sequencer
import (
"math/big"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -38,7 +39,7 @@ func (a *addrQueue) addTx(tx *TxTracker) (newReadyTx, prevReadyTx *TxTracker) {
if a.currentBalance.Cmp(tx.Cost) >= 0 { //
a.readyTx = tx
return tx, oldReadyTx
} else { // If there is not enought balance we set the new tx as notReadyTxs
} else { // If there is not enough balance we set the new tx as notReadyTxs
a.readyTx = nil
a.notReadyTxs[tx.Nonce] = tx
return nil, oldReadyTx
Expand All @@ -61,12 +62,14 @@ func (a *addrQueue) deleteTx(txHash common.Hash) (deletedReadyTx *TxTracker) {
txHashStr := txHash.String()

if (a.readyTx != nil) && (a.readyTx.HashStr == txHashStr) {
log.Infof("Deleting readyTx %s from addrQueue %s", txHashStr, a.fromStr)
prevReadyTx := a.readyTx
a.readyTx = nil
return prevReadyTx
} else {
for _, txTracker := range a.notReadyTxs {
if txTracker.HashStr == txHashStr {
log.Infof("Deleting notReadyTx %s from addrQueue %s", txHashStr, a.fromStr)
delete(a.notReadyTxs, txTracker.Nonce)
break
}
Expand All @@ -80,6 +83,7 @@ func (a *addrQueue) updateCurrentNonceBalance(nonce *uint64, balance *big.Int) (
var oldReadyTx *TxTracker = nil

if balance != nil {
log.Infof("Updating balance for addrQueue %s from %s to %s", a.fromStr, a.currentBalance.String(), balance.String())
a.currentBalance = balance
}

Expand All @@ -95,6 +99,7 @@ func (a *addrQueue) updateCurrentNonceBalance(nonce *uint64, balance *big.Int) (
}
}
for _, delTxNonce := range txToDelete {
log.Infof("Deleting notReadyTx with nonce %d from addrQueue %s", delTxNonce, a.fromStr)
delete(a.notReadyTxs, delTxNonce)
}
}
Expand All @@ -116,13 +121,15 @@ func (a *addrQueue) updateCurrentNonceBalance(nonce *uint64, balance *big.Int) (
if found {
if a.currentBalance.Cmp(nrTx.Cost) >= 0 {
a.readyTx = nrTx
log.Infof("Moving notReadyTx %s to readyTx for addrQueue %s", nrTx.HashStr, a.fromStr)
delete(a.notReadyTxs, a.currentNonce)
}
}
}

// We add the oldReadyTx to notReadyTxs (if it has a valid nonce) at this point to avoid check it again in the previous if statement
if oldReadyTx != nil && oldReadyTx.Nonce > a.currentNonce {
log.Infof("Marking readyTx %s as notReadyTx from addrQueue %s", oldReadyTx.HashStr, a.fromStr)
a.notReadyTxs[oldReadyTx.Nonce] = oldReadyTx
}

Expand All @@ -142,11 +149,13 @@ func (a *addrQueue) UpdateTxZKCounters(txHash common.Hash, counters state.ZKCoun
newReadyTx := *a.readyTx
newReadyTx.updateZKCounters(counters, constraints, weights)
a.readyTx = &newReadyTx
log.Debugf("Updating readyTx %s with new ZKCounters from addrQueue %s", txHashStr, a.fromStr)
return a.readyTx, prevReadyTx
} else { // TODO: This makes sense or we need only to check the readyTx
txHashStr := txHash.String()
for _, txTracker := range a.notReadyTxs {
if txTracker.HashStr == txHashStr {
log.Debugf("Updating notReadyTx %s with new ZKCounters from addrQueue %s", txHashStr, a.fromStr)
txTracker.updateZKCounters(counters, constraints, weights)
break
}
Expand Down
14 changes: 13 additions & 1 deletion sequencer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ func (w *Worker) AddTx(ctx context.Context, tx *TxTracker) {
root, err := w.state.GetLastStateRoot(ctx, nil)
if err != nil {
// TODO: How to manage this
log.Errorf("AddTx GetLastStateRoot error: %v", err)
return
}
nonce, err := w.state.GetNonceByStateRoot(ctx, tx.From, root)
if err != nil {
log.Errorf("AddTx GetNonceByStateRoot error: %v", err)
// TODO: How to manage this
return
}
balance, err := w.state.GetBalanceByStateRoot(ctx, tx.From, root)
if err != nil {
log.Errorf("AddTx GetBalanceByStateRoot error: %v", err)
// TODO: How to manage this
return
}
Expand All @@ -76,16 +79,20 @@ func (w *Worker) AddTx(ctx context.Context, tx *TxTracker) {
w.workerMutex.Lock()

w.pool[tx.FromStr] = addr
log.Infof("AddTx new addrQueue created for addr(%s)", tx.FromStr)
}

// Add the txTracker to Addr and get the newReadyTx and prevReadyTx
log.Infof("AddTx new tx(%s) to addrQueue(%s)", tx.Hash.String(), tx.FromStr)
newReadyTx, prevReadyTx := addr.addTx(tx)

// Update the EfficiencyList (if needed)
if prevReadyTx != nil {
log.Infof("AddTx prevReadyTx(%s) deleted from EfficiencyList", prevReadyTx.Hash.String())
w.efficiencyList.delete(prevReadyTx)
}
if newReadyTx != nil {
log.Infof("AddTx newReadyTx(%s) added to EfficiencyList", newReadyTx.Hash.String())
w.efficiencyList.add(newReadyTx)
}
}
Expand All @@ -99,9 +106,11 @@ func (w *Worker) applyAddressUpdate(from common.Address, fromNonce *uint64, from

// Update the EfficiencyList (if needed)
if prevReadyTx != nil {
log.Infof("applyAddressUpdate prevReadyTx(%s) deleted from EfficiencyList", prevReadyTx.Hash.String())
w.efficiencyList.delete(prevReadyTx)
}
if newReadyTx != nil {
log.Infof("applyAddressUpdate newReadyTx(%s) added to EfficiencyList", newReadyTx.Hash.String())
w.efficiencyList.add(newReadyTx)
}

Expand Down Expand Up @@ -140,7 +149,6 @@ func (w *Worker) MoveTxToNotReady(txHash common.Hash, from common.Address, actua
defer w.workerMutex.Unlock()

addrQueue, found := w.pool[from.String()]

if found {
// Sanity check. The txHash must be the readyTx
if addrQueue.readyTx == nil || txHash.String() != addrQueue.readyTx.HashStr {
Expand All @@ -165,6 +173,7 @@ func (w *Worker) DeleteTx(txHash common.Hash, addr common.Address) {
if found {
deletedReadyTx := addrQueue.deleteTx(txHash)
if deletedReadyTx != nil {
log.Infof("DeleteTx tx(%s) deleted from EfficiencyList", deletedReadyTx.Hash.String())
w.efficiencyList.delete(deletedReadyTx)
}
} else {
Expand All @@ -184,9 +193,11 @@ func (w *Worker) UpdateTx(txHash common.Hash, addr common.Address, counters stat

// Resort the newReadyTx in efficiencyList
if prevReadyTx != nil {
log.Infof("UpdateTx prevReadyTx(%s) deleted from EfficiencyList", prevReadyTx.Hash.String())
w.efficiencyList.delete(prevReadyTx)
}
if newReadyTx != nil {
log.Infof("UpdateTx newReadyTx(%s) added to EfficiencyList", newReadyTx.Hash.String())
w.efficiencyList.add(newReadyTx)
}
} else {
Expand Down Expand Up @@ -233,6 +244,7 @@ func (w *Worker) GetBestFittingTx(resources batchResources) *TxTracker {
if foundAt == -1 || foundAt > i {
foundAt = i
tx = txCandidate
log.Infof("GetBestFittingTx found tx(%s) at index(%d)", tx.Hash.String(), i)
}
foundMutex.Unlock()

Expand Down

0 comments on commit c16b85a

Please sign in to comment.