/
context.go
34 lines (27 loc) · 1.08 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) 2021 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package genesis
import (
"context"
"github.com/iotexproject/iotex-core/pkg/log"
)
type genesisKey struct{}
// WithGenesisContext attachs genesis into context
func WithGenesisContext(ctx context.Context, g Genesis) context.Context {
return context.WithValue(ctx, genesisKey{}, g)
}
// ExtractGenesisContext extracts genesis from context if available
func ExtractGenesisContext(ctx context.Context) (Genesis, bool) {
gc, ok := ctx.Value(genesisKey{}).(Genesis)
return gc, ok
}
// MustExtractGenesisContext extracts genesis from context if available, else panic
func MustExtractGenesisContext(ctx context.Context) Genesis {
gc, ok := ctx.Value(genesisKey{}).(Genesis)
if !ok {
log.S().Panic("Miss genesis context")
}
return gc
}