Skip to content

Commit

Permalink
Added missing 2.4 Firehose methods (system_call_start/end), renamed s…
Browse files Browse the repository at this point in the history
…ome unclear calls
  • Loading branch information
maoueh committed Apr 17, 2024
1 parent 4235d7f commit 2da7341
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
// Process block using the parent state as reference point
firehoseContext := firehose.NoOpContext
if firehose.Enabled {
firehoseContext = firehose.NewSpeculativeExecutionContextWithBuffer(firehose.BlockSyncBuffer)
firehoseContext = firehose.NewBlockContextWithBuffer(firehose.BlockSyncBuffer)
}

substart := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg

txFirehoseContext := firehoseContext
if txFirehoseContext.Enabled() {
txFirehoseContext = firehose.NewSpeculativeExecutionContextWithBuffer(firehose.TxSyncBuffer)
txFirehoseContext = firehose.NewTransactionContextWithBuffer(firehose.TxSyncBuffer)
}

blockContext := NewEVMBlockContext(header, p.bc, nil)
Expand Down
67 changes: 45 additions & 22 deletions firehose/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func SyncContext() *Context {
return syncContext
}

func NewContext(printer Printer, speculative bool) *Context {
func NewContext(printer Printer, transactionScopedContext bool) *Context {
ctx := &Context{
printer: printer,

isSpeculativeContext: speculative,
inBlock: atomic.NewBool(false),
inTransaction: atomic.NewBool(false),
totalOrderingCounter: atomic.NewUint64(0),
transactionScopedContext: transactionScopedContext,
inBlock: atomic.NewBool(false),
inTransaction: atomic.NewBool(false),
totalOrderingCounter: atomic.NewUint64(0),
}

ctx.resetBlock()
Expand All @@ -72,8 +72,8 @@ type Context struct {
printer Printer

// Global state
isSpeculativeContext bool
flushTxLock sync.Mutex
transactionScopedContext bool
flushTxLock sync.Mutex

// Block state
inBlock *atomic.Bool
Expand Down Expand Up @@ -112,7 +112,16 @@ func NewSpeculativeExecutionContext(initialAllocationInBytes int) *Context {
return NewContext(NewToBufferPrinter(initialAllocationInBytes), true)
}

func NewSpeculativeExecutionContextWithBuffer(buffer *bytes.Buffer) *Context {
// NewBlockContextWithBuffer creates a new block context with a buffer to accumulate the
// firehose logs. This should be used when tracing a block.
func NewBlockContextWithBuffer(buffer *bytes.Buffer) *Context {
return NewContext(NewToBufferPrinterWithBuffer(buffer), false)
}

// NewTransactionContextWithBuffer creates a new transaction context with a buffer to accumulate the
// firehose logs. This should be used when tracing a standalone transaction that should later be
// either emitted or flushed to a block context.
func NewTransactionContextWithBuffer(buffer *bytes.Buffer) *Context {
return NewContext(NewToBufferPrinterWithBuffer(buffer), true)
}

Expand Down Expand Up @@ -153,6 +162,7 @@ func (ctx *Context) RecordGenesisBlock(block *types.Block, recordGenesisAlloc fu
ctx.EndTransaction(&types.Receipt{PostState: root[:]})
ctx.FinalizeBlock(block)
ctx.EndBlock(block, block.Difficulty())
ctx.FlushBlock()
}

func (ctx *Context) StartBlock(block *types.Block) {
Expand Down Expand Up @@ -198,7 +208,7 @@ func (ctx *Context) FlushBlock() {
}

// exitBlock is used when an abnormal condition is encountered while processing
// transactions and we must end the block processing right away, resetting the start
// transactions and we must end the block processing right away, resetting the state
// along the way.
func (ctx *Context) exitBlock() {
if !ctx.inBlock.Load() {
Expand All @@ -211,24 +221,37 @@ func (ctx *Context) exitBlock() {
ctx.resetTransaction()
}

// CancelBlock emit a Firehose CANCEL_BLOCK event that tells the console reader to discard any
// accumulated block's data and start over. This happens on certains error conditions where the block
// is actually invalid and will be re-processed by the chain so we should not record it.
func (ctx *Context) CancelBlock(block *types.Block, err error) {
func (ctx *Context) StartSystemCall() {
if ctx == nil {
return
}

// There is some particular runtime code path that could trigger a CANCEL_BLOCK without having started
// one, it's ok, the reader is resistant to such and here, we simply don't call `ExitBlock`.
if ctx.inBlock.Load() {
ctx.exitBlock()
if !ctx.inBlock.Load() {
panic("starting system call while not already within a block scope")
}

ctx.printer.Print("CANCEL_BLOCK",
Uint64(block.NumberU64()),
err.Error(),
)
if !ctx.inTransaction.CAS(false, true) {
panic("entering a system call while already in a transaction scope")
}

ctx.printer.Print("SYSTEM_CALL_START")
}

func (ctx *Context) EndSystemCall() {
if ctx == nil {
return
}

if !ctx.inBlock.Load() {
panic("ending system call while not already within a block scope")
}

if !ctx.inTransaction.CAS(true, false) {
panic("ending a system call while not in a transaction scope")
}

ctx.resetTransaction()
ctx.printer.Print("SYSTEM_CALL_END")
}

// Transaction methods
Expand Down Expand Up @@ -437,7 +460,7 @@ func (ctx *Context) openCall() string {
}

func (ctx *Context) callIndex() string {
if !ctx.isSpeculativeContext && !ctx.inBlock.Load() {
if !ctx.transactionScopedContext && !ctx.inBlock.Load() {
debug.PrintStack()
panic("should have been call in a block or in speculative context, something is deeply wrong")
}
Expand Down
3 changes: 1 addition & 2 deletions firehose/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"strconv"
Expand Down Expand Up @@ -67,7 +66,7 @@ func flushToFirehose(in []byte, writer io.Writer) {
}

errstr := fmt.Sprintf("\nFIREHOSE FAILED WRITING %dx: %s\n", loops, err)
ioutil.WriteFile("/tmp/firehose_writer_failed_print.log", []byte(errstr), 0644)
os.WriteFile("/tmp/firehose_writer_failed_print.log", []byte(errstr), 0644)
fmt.Fprint(writer, errstr)
}

Expand Down

0 comments on commit 2da7341

Please sign in to comment.