Skip to content

Commit

Permalink
Add Etna P-chain metrics (#3458)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Nov 13, 2024
1 parent 4c2f285 commit 899f3cc
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 61 deletions.
38 changes: 20 additions & 18 deletions vms/platformvm/block/executor/acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func (a *acceptor) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {
blkID := b.ID()
defer a.free(blkID)

if err := a.commonAccept(b); err != nil {
return err
}

blkState, ok := a.blkIDToState[blkID]
if !ok {
return fmt.Errorf("%w %s", errMissingBlockState, blkID)
}

if err := a.commonAccept(blkState); err != nil {
return err
}

// Update the state to reflect the changes made in [onAcceptState].
if err := blkState.onAcceptState.Apply(a.state); err != nil {
return err
Expand Down Expand Up @@ -132,11 +132,7 @@ func (a *acceptor) optionBlock(b block.Block, blockType string) error {
}()

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

if err := a.commonAccept(b); err != nil {
if err := a.commonAccept(parentState); err != nil {
return err
}

Expand All @@ -150,6 +146,11 @@ func (a *acceptor) optionBlock(b block.Block, blockType string) error {
if !ok {
return fmt.Errorf("%w %s", errMissingBlockState, blkID)
}

if err := a.commonAccept(blkState); err != nil {
return err
}

if err := blkState.onAcceptState.Apply(a.state); err != nil {
return err
}
Expand Down Expand Up @@ -219,15 +220,15 @@ func (a *acceptor) standardBlock(b block.Block, blockType string) error {
blkID := b.ID()
defer a.free(blkID)

if err := a.commonAccept(b); err != nil {
return err
}

blkState, ok := a.blkIDToState[blkID]
if !ok {
return fmt.Errorf("%w %s", errMissingBlockState, blkID)
}

if err := a.commonAccept(blkState); err != nil {
return err
}

// Update the state to reflect the changes made in [onAcceptState].
if err := blkState.onAcceptState.Apply(a.state); err != nil {
return err
Expand Down Expand Up @@ -264,17 +265,18 @@ func (a *acceptor) standardBlock(b block.Block, blockType string) error {
return nil
}

func (a *acceptor) commonAccept(b block.Block) error {
blkID := b.ID()
func (a *acceptor) commonAccept(b *blockState) error {
blk := b.statelessBlock
blkID := blk.ID()

if err := a.metrics.MarkAccepted(b); err != nil {
if err := a.metrics.MarkAccepted(b.metrics); err != nil {
return fmt.Errorf("failed to accept block %s: %w", blkID, err)
}

a.backend.lastAccepted = blkID
a.state.SetLastAccepted(blkID)
a.state.SetHeight(b.Height())
a.state.AddStatelessBlock(b)
a.state.SetHeight(blk.Height())
a.state.AddStatelessBlock(blk)
a.validators.OnAcceptedBlockID(blkID)
return nil
}
46 changes: 21 additions & 25 deletions vms/platformvm/block/executor/acceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ func TestAcceptorVisitAtomicBlock(t *testing.T) {
)
require.NoError(err)

// Set expected calls on the state.
// We should error after [commonAccept] is called.
s.EXPECT().SetLastAccepted(blk.ID()).Times(1)
s.EXPECT().SetHeight(blk.Height()).Times(1)
s.EXPECT().AddStatelessBlock(blk).Times(1)

err = acceptor.ApricotAtomicBlock(blk)
require.ErrorIs(err, errMissingBlockState)

Expand All @@ -127,8 +121,12 @@ func TestAcceptorVisitAtomicBlock(t *testing.T) {
childID := ids.GenerateTestID()
atomicRequests := make(map[ids.ID]*atomic.Requests)
acceptor.backend.blkIDToState[blk.ID()] = &blockState{
statelessBlock: blk,
onAcceptState: onAcceptState,
atomicRequests: atomicRequests,
metrics: metrics.Block{
Block: blk,
},
}
// Give [blk] a child.
childOnAcceptState := state.NewMockDiff(ctrl)
Expand Down Expand Up @@ -196,12 +194,6 @@ func TestAcceptorVisitStandardBlock(t *testing.T) {
)
require.NoError(err)

// Set expected calls on the state.
// We should error after [commonAccept] is called.
s.EXPECT().SetLastAccepted(blk.ID()).Times(1)
s.EXPECT().SetHeight(blk.Height()).Times(1)
s.EXPECT().AddStatelessBlock(blk).Times(1)

err = acceptor.BanffStandardBlock(blk)
require.ErrorIs(err, errMissingBlockState)

Expand All @@ -211,12 +203,16 @@ func TestAcceptorVisitStandardBlock(t *testing.T) {
atomicRequests := make(map[ids.ID]*atomic.Requests)
calledOnAcceptFunc := false
acceptor.backend.blkIDToState[blk.ID()] = &blockState{
onAcceptState: onAcceptState,
statelessBlock: blk,
onAcceptState: onAcceptState,
onAcceptFunc: func() {
calledOnAcceptFunc = true
},

atomicRequests: atomicRequests,
metrics: metrics.Block{
Block: blk,
},
}
// Give [blk] a child.
childOnAcceptState := state.NewMockDiff(ctrl)
Expand Down Expand Up @@ -308,10 +304,6 @@ func TestAcceptorVisitCommitBlock(t *testing.T) {
parentStatelessBlk.EXPECT().Height().Return(blk.Height()-1).Times(1),
s.EXPECT().SetHeight(blk.Height()-1).Times(1),
s.EXPECT().AddStatelessBlock(parentState.statelessBlock).Times(1),

s.EXPECT().SetLastAccepted(blkID).Times(1),
s.EXPECT().SetHeight(blk.Height()).Times(1),
s.EXPECT().AddStatelessBlock(blk).Times(1),
)

err = acceptor.ApricotCommitBlock(blk)
Expand All @@ -322,12 +314,16 @@ func TestAcceptorVisitCommitBlock(t *testing.T) {
// Set [blk]'s state in the map as though it had been verified.
acceptor.backend.blkIDToState[parentID] = parentState
acceptor.backend.blkIDToState[blkID] = &blockState{
onAcceptState: parentState.onCommitState,
onAcceptFunc: parentState.onAcceptFunc,
statelessBlock: blk,
onAcceptState: parentState.onCommitState,
onAcceptFunc: parentState.onAcceptFunc,

inputs: parentState.inputs,
timestamp: parentOnCommitState.GetTimestamp(),
atomicRequests: parentState.atomicRequests,
metrics: metrics.Block{
Block: blk,
},
}

batch := databasemock.NewBatch(ctrl)
Expand Down Expand Up @@ -418,10 +414,6 @@ func TestAcceptorVisitAbortBlock(t *testing.T) {
parentStatelessBlk.EXPECT().Height().Return(blk.Height()-1).Times(1),
s.EXPECT().SetHeight(blk.Height()-1).Times(1),
s.EXPECT().AddStatelessBlock(parentState.statelessBlock).Times(1),

s.EXPECT().SetLastAccepted(blkID).Times(1),
s.EXPECT().SetHeight(blk.Height()).Times(1),
s.EXPECT().AddStatelessBlock(blk).Times(1),
)

err = acceptor.ApricotAbortBlock(blk)
Expand All @@ -432,12 +424,16 @@ func TestAcceptorVisitAbortBlock(t *testing.T) {
// Set [blk]'s state in the map as though it had been verified.
acceptor.backend.blkIDToState[parentID] = parentState
acceptor.backend.blkIDToState[blkID] = &blockState{
onAcceptState: parentState.onAbortState,
onAcceptFunc: parentState.onAcceptFunc,
statelessBlock: blk,
onAcceptState: parentState.onAbortState,
onAcceptFunc: parentState.onAcceptFunc,

inputs: parentState.inputs,
timestamp: parentOnAbortState.GetTimestamp(),
atomicRequests: parentState.atomicRequests,
metrics: metrics.Block{
Block: blk,
},
}

batch := databasemock.NewBatch(ctrl)
Expand Down
2 changes: 2 additions & 0 deletions vms/platformvm/block/executor/block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
)

Expand All @@ -32,4 +33,5 @@ type blockState struct {
timestamp time.Time
atomicRequests map[ids.ID]*atomic.Requests
verifiedHeights set.Set[uint64]
metrics metrics.Block
}
Loading

0 comments on commit 899f3cc

Please sign in to comment.