Skip to content

Commit d8161ce

Browse files
rpc/jsonrpc: fixed duplicate and incorrect state-sync transaction receipts post Madhugiri (#97)
* rpc/jsonrpc: fixed duplicate and incorrect state-sync transaction receipts post Madhugiri * rpc/jsonrpc: removed commented code * rpc/jsonrpc: moved the check only when we have >0 events
1 parent c382bda commit d8161ce

File tree

3 files changed

+26
-44
lines changed

3 files changed

+26
-44
lines changed

rpc/jsonrpc/erigon_receipts.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,15 @@ func (api *ErigonImpl) GetBlockReceiptsByBlockHash(ctx context.Context, cannonic
410410
return nil, err
411411
}
412412

413-
result = append(result, ethutils.MarshalReceipt(borReceipt, bortypes.NewBorTransaction(), chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false, false))
413+
var borTx types.Transaction = bortypes.NewBorTransaction()
414+
if chainConfig.Bor.IsMadhugiri(blockNum) {
415+
borTx = block.Transactions()[len(block.Transactions())-1]
416+
if borTx.Type() != types.StateSyncTxType {
417+
return nil, fmt.Errorf("the last transaction is not a state-sync transaction %x", block.Hash())
418+
}
419+
}
420+
421+
result = append(result, ethutils.MarshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false, false))
414422
}
415423
}
416424

rpc/jsonrpc/eth_receipts.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -576,34 +576,30 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, numberOrHash rpc.Block
576576
if err != nil {
577577
return nil, err
578578
}
579+
580+
// Get normal transaction receipts.
581+
// This DOES NOT generate state-sync transaction receipt for bor.
579582
receipts, err := api.getReceipts(ctx, tx, block)
580583
if err != nil {
581584
return nil, fmt.Errorf("getReceipts error: %w", err)
582585
}
583586

584587
numReceipts := len(receipts)
585-
numTxs := len(block.Transactions())
586588
result := make([]map[string]interface{}, 0, numReceipts)
587589

588-
// track if we already have a state-sync receipt coming from the generator
589-
hasBorStateSyncReceipt := false
590-
590+
// Marshal normal transaction receipts and append to result
591591
for _, receipt := range receipts {
592-
// State-sync receipts' TransactionIndex is equal to numTxs.
593-
if int(receipt.TransactionIndex) == numTxs {
594-
// This is a state-sync transaction receipt.
595-
hasBorStateSyncReceipt = true
596-
result = append(result, ethutils.MarshalReceipt(receipt, bortypes.NewBorTransaction(), chainConfig, block.HeaderNoCopy(), receipt.TxHash, false, true))
597-
} else {
598-
// This is a normal transaction receipt.
599-
txn := block.Transactions()[receipt.TransactionIndex]
600-
result = append(result, ethutils.MarshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true, true))
601-
}
592+
txn := block.Transactions()[receipt.TransactionIndex]
593+
result = append(result, ethutils.MarshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true, true))
594+
}
595+
596+
// check for state sync event logs and generate state-sync transaction receipt for bor.
597+
events, err := api.bridgeReader.Events(ctx, block.Hash(), blockNum)
598+
if err != nil {
599+
return nil, err
602600
}
603601

604-
// if we've already included the state-sync
605-
// receipt from the receipts generator, don't generate and append another one.
606-
if chainConfig.Bor != nil && hasBorStateSyncReceipt {
602+
if len(events) == 0 {
607603
return result, nil
608604
}
609605

@@ -615,19 +611,12 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, numberOrHash rpc.Block
615611
}
616612
}
617613

618-
events, err := api.bridgeReader.Events(ctx, block.Hash(), blockNum)
614+
borReceipt, err := api.borReceiptGenerator.GenerateBorReceipt(ctx, tx, block, events, chainConfig)
619615
if err != nil {
620616
return nil, err
621617
}
622618

623-
if len(events) != 0 {
624-
borReceipt, err := api.borReceiptGenerator.GenerateBorReceipt(ctx, tx, block, events, chainConfig)
625-
if err != nil {
626-
return nil, err
627-
}
628-
629-
result = append(result, ethutils.MarshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false, true))
630-
}
619+
result = append(result, ethutils.MarshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false, true))
631620

632621
return result, nil
633622
}

rpc/jsonrpc/receipts/receipts_generator.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ func (g *Generator) GetReceipt(ctx context.Context, cfg *chain.Config, tx kv.Tem
286286
return receipt, nil
287287
}
288288

289-
// GetReceipts regenerates or loads receipts for a given block (including state-sync synthetic receipt if needed).
289+
// GetReceipts regenerates or loads receipts for a given block
290+
// This DOES NOT generate state-sync transaction receipt for bor.
290291
func (g *Generator) GetReceipts(ctx context.Context, cfg *chain.Config, tx kv.TemporalTx, block *types.Block) (types.Receipts, error) {
291292
blockHash := block.Hash()
292293

@@ -383,22 +384,6 @@ func (g *Generator) GetReceipts(ctx context.Context, cfg *chain.Config, tx kv.Te
383384
}
384385
}
385386

386-
// PIP-74: state-sync receipt handling.
387-
if g.borGenerator != nil && cfg.Bor != nil {
388-
// Extract state-sync events from block.
389-
events, err := g.extractBorEvents(ctx, block)
390-
if err != nil {
391-
return nil, fmt.Errorf("ReceiptGen.GetReceipts: failed to extract bor events for block %d: %w", block.NumberU64(), err)
392-
}
393-
if len(events) > 0 {
394-
borReceipt, err := g.borGenerator.GenerateBorReceipt(ctx, tx, block, events, cfg)
395-
if err != nil {
396-
return nil, fmt.Errorf("ReceiptGen.GetReceipts: failed to generate bor receipt for block %d: %w", block.NumberU64(), err)
397-
}
398-
receipts = append(receipts, borReceipt)
399-
}
400-
}
401-
402387
g.addToCacheReceipts(block.HeaderNoCopy(), receipts)
403388
return receipts, nil
404389
}

0 commit comments

Comments
 (0)