Skip to content
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

Allow calls to Options before Verify #2363

Merged
merged 27 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ffbc96d
wip
StephenButtolph Nov 22, 2023
f6c0949
wip
StephenButtolph Nov 23, 2023
3b817f2
wip
StephenButtolph Nov 23, 2023
743e176
fix unit tests
StephenButtolph Nov 23, 2023
6a35a2c
Merge branch 'dev' into options-before-verify
StephenButtolph Nov 23, 2023
527c22c
fix typo
StephenButtolph Nov 23, 2023
5edf87a
nit
StephenButtolph Nov 23, 2023
7d49953
nit
StephenButtolph Nov 23, 2023
6655c06
fix tests
StephenButtolph Nov 23, 2023
8d266a8
Merge branch 'dev' into options-before-verify
StephenButtolph Nov 25, 2023
f0cfa6d
Merge branch 'dev' into options-before-verify
StephenButtolph Nov 29, 2023
82b4338
Merge branch 'dev' into options-before-verify
StephenButtolph Dec 4, 2023
0e37af9
Merge branch 'dev' into options-before-verify
StephenButtolph Dec 5, 2023
2e863f4
Merge branch 'dev' into options-before-verify
StephenButtolph Dec 5, 2023
92363ec
Allow errors from Options() calls
StephenButtolph Dec 5, 2023
dc6e557
add test
StephenButtolph Dec 5, 2023
877bc76
Revert "Allow errors from Options() calls"
StephenButtolph Dec 5, 2023
fec4276
Revert "add test"
StephenButtolph Dec 5, 2023
70df297
Remove error handling
StephenButtolph Dec 6, 2023
8c5bb37
nits
StephenButtolph Dec 6, 2023
c6c4a8c
merged
StephenButtolph Dec 27, 2023
e1b3547
nits
StephenButtolph Dec 28, 2023
67610a7
Merge branch 'dev' into options-before-verify
StephenButtolph Dec 28, 2023
99dd506
merged
StephenButtolph Jan 18, 2024
5a6bf3d
merged
StephenButtolph Jan 19, 2024
bc9e195
Merge branch 'master' into options-before-verify
StephenButtolph Jan 20, 2024
7de3c5c
Merge branch 'master' into options-before-verify
StephenButtolph Jan 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion snow/consensus/snowman/oracle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ var ErrNotOracle = errors.New("block isn't an oracle")
type OracleBlock interface {
// Options returns the possible children of this block in the order this
// validator prefers the blocks.
// Options is guaranteed to only be called on a verified block.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the whole point of the PR

Options(context.Context) ([2]Block, error)
}
34 changes: 20 additions & 14 deletions snow/engine/snowman/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,22 +515,26 @@ func (t *Transitive) Start(ctx context.Context, startReqID uint32) error {
if oracleBlk, ok := lastAccepted.(snowman.OracleBlock); ok {
options, err := oracleBlk.Options(ctx)
switch {
case err == snowman.ErrNotOracle:
// if there aren't blocks we need to deliver on startup, we need to set
// the preference to the last accepted block
if err := t.VM.SetPreference(ctx, lastAcceptedID); err != nil {
return err
}
case err != nil:
return err
default:
case err == nil:
issuedMetric := t.metrics.issued.WithLabelValues(builtSource)
for _, blk := range options {
// note that deliver will set the VM's preference
if err := t.deliver(ctx, t.Ctx.NodeID, blk, false, issuedMetric); err != nil {
return err
}
}
case err != snowman.ErrNotOracle:
t.Ctx.Log.Warn("failed to set preferences for oracle block",
zap.Stringer("blkID", lastAcceptedID),
zap.Error(err),
)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
fallthrough
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
default:
// if there aren't blocks we need to deliver on startup, we need to
// set the preference to the last accepted block
if err := t.VM.SetPreference(ctx, lastAcceptedID); err != nil {
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
} else if err := t.VM.SetPreference(ctx, lastAcceptedID); err != nil {
return err
Expand Down Expand Up @@ -1015,11 +1019,8 @@ func (t *Transitive) deliver(
dropped := []snowman.Block{}
if blk, ok := blk.(snowman.OracleBlock); ok {
options, err := blk.Options(ctx)
if err != snowman.ErrNotOracle {
if err != nil {
return err
}

switch {
case err == nil:
for _, blk := range options {
blkAdded, err := t.addUnverifiedBlockToConsensus(ctx, nodeID, blk, issuedMetric)
if err != nil {
Expand All @@ -1031,6 +1032,11 @@ func (t *Transitive) deliver(
dropped = append(dropped, blk)
}
}
case err != snowman.ErrNotOracle:
t.Ctx.Log.Debug("failed to set preferences for oracle block",
zap.Stringer("blkID", blkID),
zap.Error(err),
)
}
}

Expand Down
44 changes: 6 additions & 38 deletions vms/platformvm/block/executor/acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type acceptor struct {
}

func (a *acceptor) BanffAbortBlock(b *block.BanffAbortBlock) error {
return a.abortBlock(b, "banff abort")
return a.optionBlock(b, "banff abort")
}

func (a *acceptor) BanffCommitBlock(b *block.BanffCommitBlock) error {
return a.commitBlock(b, "apricot commit")
return a.optionBlock(b, "banff commit")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the wrong block previously

}

func (a *acceptor) BanffProposalBlock(b *block.BanffProposalBlock) error {
Expand All @@ -50,11 +50,11 @@ func (a *acceptor) BanffStandardBlock(b *block.BanffStandardBlock) error {
}

func (a *acceptor) ApricotAbortBlock(b *block.ApricotAbortBlock) error {
return a.abortBlock(b, "apricot abort")
return a.optionBlock(b, "apricot abort")
}

func (a *acceptor) ApricotCommitBlock(b *block.ApricotCommitBlock) error {
return a.commitBlock(b, "apricot commit")
return a.optionBlock(b, "apricot commit")
}

func (a *acceptor) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
Expand Down Expand Up @@ -116,46 +116,14 @@ func (a *acceptor) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {
return nil
}

func (a *acceptor) abortBlock(b block.Block, blockType string) error {
func (a *acceptor) optionBlock(b block.Block, blockType string) error {
parentID := b.Parent()
parentState, ok := a.blkIDToState[parentID]
if !ok {
return fmt.Errorf("%w: %s", state.ErrMissingParentState, parentID)
}

if a.bootstrapped.Get() {
if parentState.initiallyPreferCommit {
a.metrics.MarkOptionVoteLost()
} else {
a.metrics.MarkOptionVoteWon()
}
Comment on lines -127 to -131
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't set this value during execution, so the acceptor doesn't really have access to it anymore.

}

return a.optionBlock(b, parentState.statelessBlock, blockType)
}

func (a *acceptor) commitBlock(b block.Block, blockType string) error {
parentID := b.Parent()
parentState, ok := a.blkIDToState[parentID]
if !ok {
return fmt.Errorf("%w: %s", state.ErrMissingParentState, parentID)
}

if a.bootstrapped.Get() {
if parentState.initiallyPreferCommit {
a.metrics.MarkOptionVoteWon()
} else {
a.metrics.MarkOptionVoteLost()
}
}

return a.optionBlock(b, parentState.statelessBlock, blockType)
}

func (a *acceptor) optionBlock(b, parent block.Block, blockType string) error {
blkID := b.ID()
parentID := parent.ID()

defer func() {
// Note: we assume this block's sibling doesn't
// need the parent's state when it's rejected.
Expand All @@ -164,7 +132,7 @@ func (a *acceptor) optionBlock(b, parent block.Block, blockType string) error {
}()

// Note that the parent must be accepted first.
if err := a.commonAccept(parent); err != nil {
if err := a.commonAccept(parentState.statelessBlock); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions vms/platformvm/block/executor/acceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestAcceptorVisitCommitBlock(t *testing.T) {
// Set expected calls on dependencies.
// Make sure the parent is accepted first.
gomock.InOrder(
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(2),
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(1),
s.EXPECT().SetLastAccepted(parentID).Times(1),
parentStatelessBlk.EXPECT().Height().Return(blk.Height()-1).Times(1),
s.EXPECT().SetHeight(blk.Height()-1).Times(1),
Expand All @@ -319,7 +319,7 @@ func TestAcceptorVisitCommitBlock(t *testing.T) {
// Set expected calls on dependencies.
// Make sure the parent is accepted first.
gomock.InOrder(
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(2),
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(1),
s.EXPECT().SetLastAccepted(parentID).Times(1),
parentStatelessBlk.EXPECT().Height().Return(blk.Height()-1).Times(1),
s.EXPECT().SetHeight(blk.Height()-1).Times(1),
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestAcceptorVisitAbortBlock(t *testing.T) {
// Set expected calls on dependencies.
// Make sure the parent is accepted first.
gomock.InOrder(
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(2),
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(1),
s.EXPECT().SetLastAccepted(parentID).Times(1),
parentStatelessBlk.EXPECT().Height().Return(blk.Height()-1).Times(1),
s.EXPECT().SetHeight(blk.Height()-1).Times(1),
Expand All @@ -411,7 +411,7 @@ func TestAcceptorVisitAbortBlock(t *testing.T) {
// Set expected calls on dependencies.
// Make sure the parent is accepted first.
gomock.InOrder(
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(2),
parentStatelessBlk.EXPECT().ID().Return(parentID).Times(1),
s.EXPECT().SetLastAccepted(parentID).Times(1),
parentStatelessBlk.EXPECT().Height().Return(blk.Height()-1).Times(1),
s.EXPECT().SetHeight(blk.Height()-1).Times(1),
Expand Down
24 changes: 9 additions & 15 deletions vms/platformvm/block/executor/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package executor

import (
"context"
"fmt"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -83,22 +82,17 @@ func (b *Block) Timestamp() time.Time {
}

func (b *Block) Options(context.Context) ([2]snowman.Block, error) {
options := options{}
options := options{
primaryUptimePercentage: b.manager.txExecutorBackend.Config.UptimePercentage,
uptimes: b.manager.txExecutorBackend.Uptimes,
state: b.manager.backend.state,
}
if err := b.Block.Visit(&options); err != nil {
return [2]snowman.Block{}, err
}

commitBlock := b.manager.NewBlock(options.commitBlock)
abortBlock := b.manager.NewBlock(options.abortBlock)

blkID := b.ID()
blkState, ok := b.manager.blkIDToState[blkID]
if !ok {
return [2]snowman.Block{}, fmt.Errorf("block %s state not found", blkID)
}

if blkState.initiallyPreferCommit {
return [2]snowman.Block{commitBlock, abortBlock}, nil
}
return [2]snowman.Block{abortBlock, commitBlock}, nil
return [2]snowman.Block{
b.manager.NewBlock(options.preferredBlock),
b.manager.NewBlock(options.alternateBlock),
}, nil
}
5 changes: 2 additions & 3 deletions vms/platformvm/block/executor/block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type standardBlockState struct {
}

type proposalBlockState struct {
initiallyPreferCommit bool
onCommitState state.Diff
onAbortState state.Diff
onCommitState state.Diff
onAbortState state.Diff
}

// The state of a block.
Expand Down
Loading
Loading