Skip to content

Commit

Permalink
Add blockID field to ExecutedBlock type
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald committed Oct 1, 2024
1 parent 9ce793a commit 3aa92cd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
20 changes: 16 additions & 4 deletions chain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,22 +897,34 @@ func (b *StatefulBlock) FeeManager() *internalfees.Manager {
}

type ExecutedBlock struct {
BlockID ids.ID `json:"blockID"`
Block *StatelessBlock `json:"block"`
Results []*Result `json:"results"`
UnitPrices fees.Dimensions `json:"unitPrices"`
}

// NewExecutedBlock returns the execution results of a stateful block.
// This should only be called on a StatefulBlock that has already been
// executed.
func NewExecutedBlock(b *StatefulBlock) *ExecutedBlock {
func NewExecutedBlockFromStateful(b *StatefulBlock) *ExecutedBlock {
return &ExecutedBlock{
BlockID: b.ID(),
Block: b.StatelessBlock,
Results: b.results,
UnitPrices: b.feeManager.UnitPrices(),
}
}

func NewExecutedBlock(statelessBlock *StatelessBlock, results []*Result, unitPrices fees.Dimensions) (*ExecutedBlock, error) {
blkID, err := statelessBlock.ID()
if err != nil {
return nil, err
}
return &ExecutedBlock{
BlockID: blkID,
Block: statelessBlock,
Results: results,
UnitPrices: unitPrices,
}, nil
}

func (b *ExecutedBlock) Marshal() ([]byte, error) {
blockBytes, err := b.Block.Marshal()
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions extension/externalsubscriber/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ func (e *ExternalSubscriberClient) Accept(blk *chain.ExecutedBlock) error {
req := &pb.BlockRequest{
BlockData: blockBytes,
}
blkID, err := blk.Block.ID()
if err != nil {
return err
}
e.log.Debug("sending accepted block to server",
zap.Stringer("blockID", blkID),
zap.Stringer("blockID", blk.BlockID),
zap.Uint64("blockHeight", blk.Block.Hght),
)
_, err = e.client.AcceptBlock(context.TODO(), req)
Expand Down
7 changes: 1 addition & 6 deletions tests/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package integration
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -136,11 +135,7 @@ func setInstances() {
externalSubscriber0 := externalsubscriber.NewExternalSubscriberServer(log, createParserFromBytes, []event.Subscription[*chain.ExecutedBlock]{
event.SubscriptionFunc[*chain.ExecutedBlock]{
AcceptF: func(blk *chain.ExecutedBlock) error {
blockID, err := blk.Block.ID()
if err != nil {
return fmt.Errorf("failed to get block ID: %w", err)
}
externalSubscriberAcceptedBlocksCh <- blockID
externalSubscriberAcceptedBlocksCh <- blk.BlockID
return nil
},
},
Expand Down
2 changes: 1 addition & 1 deletion vm/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (vm *VM) processAcceptedBlock(b *chain.StatefulBlock) {

// Subscriptions must be updated before setting the last processed height
// key to guarantee at-least-once delivery semantics
executedBlock := chain.NewExecutedBlock(b)
executedBlock := chain.NewExecutedBlockFromStateful(b)
for _, subscription := range vm.blockSubscriptions {
if err := subscription.Accept(executedBlock); err != nil {
vm.Fatal("subscription failed to process block", zap.Error(err))
Expand Down

0 comments on commit 3aa92cd

Please sign in to comment.