Skip to content

Commit

Permalink
Revert "add FORCE_FINALIZED_BLOCK_ABOVE_THRESHOLD to prevent infinite…
Browse files Browse the repository at this point in the history
… lagging when beacon misbehaves, ex. on testnet"

* This has been implemented in the firehose-core reader instead

This reverts commit de6dbf1.
  • Loading branch information
sduchesneau committed Jan 29, 2024
1 parent de6dbf1 commit 604db95
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 129 deletions.
21 changes: 18 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1792,8 +1792,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
td := new(big.Int).Add(block.Difficulty(), ptd)

finalBlockHeader := firehose.LastFinalBlock(block, bc.CurrentFinalBlock(), bc.GetBlockByNumber)
firehoseContext.EndBlock(block, finalBlockHeader, td)
var curFinalBlock *types.Header
if firehose.ReprocessingWithSyncTarget {
curFinalBlock = block.Header()
} else {
curFinalBlock = bc.CurrentFinalBlock()
}
firehoseContext.EndBlock(block, curFinalBlock, td)

firehoseContext.FlushBlock()
}
Expand Down Expand Up @@ -1874,7 +1879,17 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
td = new(big.Int).Add(difficulty, ptd)
}

finalBlockHeader := firehose.LastFinalBlock(block, bc.CurrentFinalBlock(), bc.GetBlockByNumber)
var finalBlockHeader *types.Header
if firehose.ReprocessingWithSyncTarget {
finalBlockHeader = block.Header()
} else {
if cur := bc.CurrentFinalBlock(); cur != nil && !firehose.SyncingBehindFinalized() {
// If beaconFinalizedBlockNum is in the future, the 'finalizedBlock' will not progress until we reach it.
// we don't want to advertise a super old finalizedBlock when reprocessing.
finalBlockHeader = cur
}
}

firehoseContext.EndBlock(block, finalBlockHeader, td)
}

Expand Down
29 changes: 0 additions & 29 deletions firehose/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,35 +171,6 @@ func (ctx *Context) FinalizeBlock(block *types.Block) {
ctx.printer.Print("FINALIZE_BLOCK", Uint64(block.NumberU64()))
}

type blockGetter func(uint64) *types.Block

func LastFinalBlock(currentBlock *types.Block, candidateLIB *types.Header, getBlockByNum blockGetter) *types.Header {
if ReprocessingWithSyncTarget {
return currentBlock.Header()
}

currentNum := currentBlock.NumberU64()
if candidateLIB != nil &&
candidateLIB.Number.Uint64() <= currentNum &&
(ForceFinalizedBlockAboveThreshold == 0 || candidateLIB.Number.Uint64()+ForceFinalizedBlockAboveThreshold > currentNum) {
return candidateLIB
}

if ForceFinalizedBlockAboveThreshold == 0 {
return nil
}

if currentNum < ForceFinalizedBlockAboveThreshold {
return nil
}

blk := getBlockByNum(currentNum - 200)
if blk != nil {
return blk.Header()
}
return nil
}

func (ctx *Context) EndBlock(block *types.Block, finalBlockHeader *types.Header, totalDifficulty *big.Int) {
endData := map[string]interface{}{
"header": block.Header(),
Expand Down
86 changes: 0 additions & 86 deletions firehose/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestLastFinalBlock(t *testing.T) {

getBlockGetter := func(expectedBlockNum uint64) func(uint64) *types.Block {
return func(in uint64) *types.Block {
if in != expectedBlockNum {
panic("unexpected block number")
}
header := &types.Header{Number: big.NewInt(int64(expectedBlockNum))}
return types.NewBlockWithHeader(header)
}
}

tests := []struct {
name string
current *types.Block
candidate *types.Header
threshold uint64
getBlockNum func(uint64) *types.Block
reprocessing bool
expect uint64
expectNil bool
}{
{
name: "candidate is good",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(10)}),
candidate: &types.Header{Number: big.NewInt(5)},
expect: 5,
},
{
name: "candidate is good, with forceThreshold",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(10)}),
candidate: &types.Header{Number: big.NewInt(5)},
threshold: 200,
expect: 5,
},
{
name: "is reprocessing",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(10)}),
candidate: &types.Header{Number: big.NewInt(5)},
reprocessing: true,
expect: 10,
},
{
name: "no candidate, no threshold",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(10)}),
expectNil: true,
},
{
name: "no candidate, below threshold",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(10)}),
threshold: 200,
expectNil: true,
},
{
name: "no candidate, above threshold",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(1000)}),
threshold: 200,
getBlockNum: getBlockGetter(800),
expect: 800,
},
{
name: "candidate too old",
current: types.NewBlockWithHeader(&types.Header{Number: big.NewInt(1000)}),
candidate: &types.Header{Number: big.NewInt(5)},
threshold: 200,
getBlockNum: getBlockGetter(800),
expect: 800,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ReprocessingWithSyncTarget = tt.reprocessing
ForceFinalizedBlockAboveThreshold = tt.threshold

if tt.expectNil {
assert.Nil(t, LastFinalBlock(tt.current, tt.candidate, tt.getBlockNum))
} else {
lib := LastFinalBlock(tt.current, tt.candidate, tt.getBlockNum)
require.NotNil(t, lib)
assert.Equal(t, tt.expect, lib.Number.Uint64())
}
})
}
}

func TestAccessList_marshal(t *testing.T) {
tests := []struct {
name string
Expand Down
11 changes: 0 additions & 11 deletions firehose/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"reflect"
"strconv"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -57,9 +56,6 @@ var GenesisConfig interface{}
// This setting will cause the "Last Finalized Block" (LIB) set to follow each block that we process.
var ReprocessingWithSyncTarget = false

// ForceFinalizedBlockAboveThreshold can be used to assume that a part of the chain is finalized when a certain number of confirmations have passed, even when the beacon chain is drifting.
var ForceFinalizedBlockAboveThreshold = uint64(0)

var MissingGenesisPanicMessage = "Firehose requires to have the genesis config to properly emit genesis block for this chain " +
"but it appears it was not set properly. Ensure you are using either chain's specific flag like " +
"'--mainnet' or if using a custom network, you can use '--firehose-genesis' flag to provide. Firehose " +
Expand Down Expand Up @@ -110,13 +106,6 @@ func Init(
}
}

if forceFinalized := os.Getenv("FORCE_FINALIZED_BLOCK_ABOVE_THRESHOLD"); forceFinalized != "" {
thresh, err := strconv.ParseUint(forceFinalized, 10, 64)
if err == nil && thresh > 0 {
ForceFinalizedBlockAboveThreshold = thresh
}
}

if Enabled {
AllocateBuffers()
}
Expand Down

0 comments on commit 604db95

Please sign in to comment.