github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/genesis/context.go (about)

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