Skip to content

Commit 84ee759

Browse files
committed
Simplify return signature for tx validation / application
1 parent 1379554 commit 84ee759

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

rolling-shutter/collator/batch/batch.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ func (b *Batch) NewPendingTransaction(tx *txtypes.Transaction, txRaw []byte, rec
146146
// b) has enough balance at the senders account in order to pay the
147147
// tansactions gas fees, also considering all previous locally
148148
// included transactions in that batch
149-
func (b *Batch) ValidateTx(ctx context.Context, p *PendingTransaction) (bool, error) {
149+
func (b *Batch) ValidateTx(ctx context.Context, p *PendingTransaction) error {
150150
currentNonce, err := b.state.GetNonce(ctx, p.sender)
151151
if err != nil {
152-
return false, err
152+
return err
153153
}
154154
if p.tx.Nonce() != currentNonce {
155-
return false, errors.Errorf("nonce mismatch, want: %d,got: %d", currentNonce, p.tx.Nonce())
155+
return errors.Errorf("nonce mismatch, want: %d,got: %d", currentNonce, p.tx.Nonce())
156156
}
157157

158158
if err := ValidateGasParams(p.tx, b.block.BaseFee()); err != nil {
@@ -162,40 +162,40 @@ func (b *Batch) ValidateTx(ctx context.Context, p *PendingTransaction) (bool, er
162162
p.minerFee = CalculatePriorityFee(p.tx, b.block.BaseFee())
163163
balance, err := b.state.GetBalance(ctx, p.sender)
164164
if err != nil {
165-
return false, err
165+
return err
166166
}
167167
if balance.Cmp(p.gasCost) < 0 {
168-
return false, errors.New("not enough funds to pay gas fee")
168+
return errors.New("not enough funds to pay gas fee")
169169
}
170-
return true, nil
170+
return nil
171171
}
172172

173173
// ApplyTx will include the transaction `p` in the local batch-state and
174174
// will modify the batches local state to include the nonce and balance changes.
175175
// ApplyTx can fail when the transaction's inclusion would surpass the batches
176176
// gas limit.
177-
func (b *Batch) ApplyTx(ctx context.Context, p *PendingTransaction) (bool, error) {
177+
func (b *Batch) ApplyTx(ctx context.Context, p *PendingTransaction) error {
178178
b.mux.Lock()
179179
defer b.mux.Unlock()
180180

181181
err := b.gasPool.SubGas(p.tx.Gas())
182182
if err != nil {
183183
// gas limit reached
184-
return false, err
184+
return err
185185
}
186186
err = b.state.SubBalance(ctx, p.sender, p.gasCost)
187187
if err != nil {
188-
return false, err
188+
return err
189189
}
190190
// not really necessary, only to e.g. observe the total gained fee
191191
err = b.state.AddBalance(ctx, b.block.Coinbase(), p.minerFee)
192192
if err != nil {
193-
return false, err
193+
return err
194194
}
195195
b.state.SetNonce(p.sender, p.tx.Nonce()+1)
196196

197197
b.transactions.Enqueue(p)
198-
return true, nil
198+
return nil
199199
}
200200

201201
func (b *Batch) Transactions() *TransactionQueue {

rolling-shutter/collator/batch/batchhandler.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,12 @@ func (bh *BatchHandler) ProcessTx(ctx context.Context, tx *PendingTransaction) e
248248
if batch == nil {
249249
return errors.New("batch is not submittable")
250250
}
251-
ok, err := batch.ValidateTx(ctx, tx)
252-
if !ok {
251+
err := batch.ValidateTx(ctx, tx)
252+
if err != nil {
253253
return errors.Wrap(err, "tx not valid")
254254
}
255-
255+
err = batch.ApplyTx(ctx, tx)
256256
if err != nil {
257-
return err
258-
}
259-
ok, err = batch.ApplyTx(ctx, tx)
260-
if !ok {
261257
return errors.Wrap(err, "can't apply tx")
262258
}
263259

@@ -438,7 +434,7 @@ func (bh *BatchHandler) reconstructBatchFromDB(ctx context.Context, db *cltrdb.Q
438434
var tx txtypes.Transaction
439435
err := tx.UnmarshalBinary(txBytes)
440436
if err != nil {
441-
// This shouldn't happen, since the tx was once instantiated and validated
437+
// This shouldn't happen, since the tx was once unmarshalled
442438
// before being written to DB
443439
err = errors.Wrap(err, "error during unmarshaling transactions from DB")
444440
fmt.Println(err)
@@ -451,24 +447,18 @@ func (bh *BatchHandler) reconstructBatchFromDB(ctx context.Context, db *cltrdb.Q
451447
recoverTime := time.Now()
452448
p, err := batch.NewPendingTransaction(&tx, txBytes, recoverTime)
453449
if err != nil {
454-
// This shouldn't happen, since the tx was once instantiated and validated
455-
// before being written to DB
456450
err = errors.Wrap(err, "error during recovering transactions from DB")
457451
fmt.Println(err)
458452
continue
459453
}
460-
ok, err := batch.ValidateTx(ctx, p)
461-
if err != nil || !ok {
462-
// This shouldn't happen, since the tx was once instantiated and validated
463-
// before being written to DB
454+
err = batch.ValidateTx(ctx, p)
455+
if err != nil {
464456
err = errors.Wrap(err, "validation error during recovering transactions from DB")
465457
fmt.Println(err)
466458
continue
467459
}
468-
ok, err = batch.ApplyTx(ctx, p)
469-
if err != nil || !ok {
470-
// This shouldn't happen, since the tx was once instantiated and validated
471-
// before being written to DB
460+
err = batch.ApplyTx(ctx, p)
461+
if err != nil {
472462
err = errors.Wrap(err, "error during applying recovered transactions from DB")
473463
fmt.Println(err)
474464
continue

0 commit comments

Comments
 (0)