-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send correct state root with finalization event #13842
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,7 +170,7 @@ func (s *Service) ReceiveBlock(ctx context.Context, block interfaces.ReadOnlySig | |
// Send finalized events and finalized deposits in the background | ||
if newFinalized { | ||
finalized := s.cfg.ForkChoiceStore.FinalizedCheckpoint() | ||
go s.sendNewFinalizedEvent(blockCopy, postState) | ||
go s.sendNewFinalizedEvent(ctx, postState) | ||
depCtx, cancel := context.WithTimeout(context.Background(), depositDeadline) | ||
go func() { | ||
s.insertFinalizedDeposits(depCtx, finalized.Root) | ||
|
@@ -443,16 +443,25 @@ func (s *Service) updateFinalizationOnBlock(ctx context.Context, preState, postS | |
|
||
// sendNewFinalizedEvent sends a new finalization checkpoint event over the | ||
// event feed. It needs to be called on the background | ||
func (s *Service) sendNewFinalizedEvent(signed interfaces.ReadOnlySignedBeaconBlock, postState state.BeaconState) { | ||
func (s *Service) sendNewFinalizedEvent(ctx context.Context, postState state.BeaconState) { | ||
isValidPayload := false | ||
s.headLock.RLock() | ||
if s.head != nil { | ||
isValidPayload = s.head.optimistic | ||
} | ||
s.headLock.RUnlock() | ||
|
||
blk, err := s.cfg.BeaconDB.Block(ctx, bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root)) | ||
if err != nil { | ||
log.WithError(err).Error("Could not retrieve block for finalized checkpoint root. Finalized event will not be emitted") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Not tested.) |
||
return | ||
} | ||
if blk == nil || blk.IsNil() || blk.Block() == nil || blk.Block().IsNil() { | ||
log.WithError(err).Error("Block retrieved for finalized checkpoint root is nil. Finalized event will not be emitted") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Not tested.) |
||
return | ||
} | ||
stateRoot := blk.Block().StateRoot() | ||
// Send an event regarding the new finalized checkpoint over a common event feed. | ||
stateRoot := signed.Block().StateRoot() | ||
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{ | ||
Type: statefeed.FinalizedCheckpoint, | ||
Data: ðpbv1.EventFinalizedCheckpoint{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,14 @@ import ( | |
|
||
blockchainTesting "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/testing" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache" | ||
statefeed "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed/state" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/das" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/voluntaryexits" | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" | ||
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" | ||
ethpbv1 "github.com/prysmaticlabs/prysm/v5/proto/eth/v1" | ||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/v5/testing/assert" | ||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
|
@@ -378,3 +380,38 @@ func TestHandleBlockBLSToExecutionChanges(t *testing.T) { | |
require.Equal(t, false, pool.ValidatorExists(idx)) | ||
}) | ||
} | ||
|
||
func Test_sendNewFinalizedEvent(t *testing.T) { | ||
s, _ := minimalTestService(t) | ||
notifier := &blockchainTesting.MockStateNotifier{RecordEvents: true} | ||
s.cfg.StateNotifier = notifier | ||
finalizedSt, err := util.NewBeaconState() | ||
require.NoError(t, err) | ||
finalizedStRoot, err := finalizedSt.HashTreeRoot(s.ctx) | ||
require.NoError(t, err) | ||
b := util.NewBeaconBlock() | ||
b.Block.StateRoot = finalizedStRoot[:] | ||
sbb, err := blocks.NewSignedBeaconBlock(b) | ||
require.NoError(t, err) | ||
sbbRoot, err := sbb.Block().HashTreeRoot() | ||
require.NoError(t, err) | ||
require.NoError(t, s.cfg.BeaconDB.SaveBlock(s.ctx, sbb)) | ||
st, err := util.NewBeaconState() | ||
require.NoError(t, err) | ||
require.NoError(t, st.SetFinalizedCheckpoint(ðpb.Checkpoint{ | ||
Epoch: 123, | ||
Root: sbbRoot[:], | ||
})) | ||
|
||
s.sendNewFinalizedEvent(s.ctx, st) | ||
|
||
require.Equal(t, 1, len(notifier.ReceivedEvents())) | ||
e := notifier.ReceivedEvents()[0] | ||
assert.Equal(t, statefeed.FinalizedCheckpoint, int(e.Type)) | ||
fc, ok := e.Data.(*ethpbv1.EventFinalizedCheckpoint) | ||
require.Equal(t, true, ok, "event has wrong data type") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's true then it should be okay, why do you have what seems like an error message here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the |
||
assert.Equal(t, primitives.Epoch(123), fc.Epoch) | ||
assert.DeepEqual(t, sbbRoot[:], fc.Block) | ||
assert.DeepEqual(t, finalizedStRoot[:], fc.State) | ||
assert.Equal(t, false, fc.ExecutionOptimistic) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this line has not be modified in this PR, but this case is not tested, and I guess it's quite an important case to test.