Skip to content

Commit 972405a

Browse files
authored
refactor(types): add SignedHeaderFromContext (#2370)
ref: evstack/ev-abci#101 (comment)
1 parent 619bd55 commit 972405a

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

block/manager.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,19 +849,13 @@ func (m *Manager) execCreateBlock(_ context.Context, height uint64, lastSignatur
849849
return header, blockData, nil
850850
}
851851

852-
type headerContextKey struct{}
853-
854-
// HeaderContextKey is used to store the header in the context.
855-
// This is useful if the execution client needs to access the header during transaction execution.
856-
var HeaderContextKey = headerContextKey{}
857-
858852
func (m *Manager) execApplyBlock(ctx context.Context, lastState types.State, header *types.SignedHeader, data *types.Data) (types.State, error) {
859853
rawTxs := make([][]byte, len(data.Txs))
860854
for i := range data.Txs {
861855
rawTxs[i] = data.Txs[i]
862856
}
863857

864-
ctx = context.WithValue(ctx, HeaderContextKey, header)
858+
ctx = context.WithValue(ctx, types.SignedHeaderContextKey, header)
865859
newStateRoot, _, err := m.exec.ExecuteTxs(ctx, rawTxs, header.Height(), header.Time(), lastState.AppHash)
866860
if err != nil {
867861
return types.State{}, fmt.Errorf("failed to execute transactions: %w", err)

types/signed_header.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ package types
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"fmt"
78

89
"github.com/celestiaorg/go-header"
910
)
1011

12+
type signedHeaderContextKey struct{}
13+
14+
// SignedHeaderContextKey is used to store the signed header in the context.
15+
// This is useful if the execution client needs to access the signed header during transaction execution.
16+
var SignedHeaderContextKey = signedHeaderContextKey{}
17+
18+
func SignedHeaderFromContext(ctx context.Context) (*SignedHeader, bool) {
19+
sh, ok := ctx.Value(SignedHeaderContextKey).(*SignedHeader)
20+
if !ok {
21+
return nil, false
22+
}
23+
24+
return sh, true
25+
}
26+
1127
var (
1228
// ErrLastHeaderHashMismatch is returned when the last header hash doesn't match.
1329
ErrLastHeaderHashMismatch = errors.New("last header hash mismatch")

0 commit comments

Comments
 (0)