You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
package blockfactory
|
|
|
|
import (
|
|
"math/big"
|
|
|
|
"github.com/harmony-one/harmony/block"
|
|
"github.com/harmony-one/harmony/block/v0"
|
|
"github.com/harmony-one/harmony/block/v1"
|
|
"github.com/harmony-one/harmony/internal/params"
|
|
)
|
|
|
|
// Factory is a data structure factory for a specific chain configuration.
|
|
type Factory interface {
|
|
// NewHeader creates a new, empty header object for the given epoch.
|
|
NewHeader(epoch *big.Int) *block.Header
|
|
}
|
|
|
|
type factory struct {
|
|
chainConfig *params.ChainConfig
|
|
}
|
|
|
|
// NewFactory creates a new factory for the given chain configuration.
|
|
func NewFactory(chainConfig *params.ChainConfig) Factory {
|
|
return &factory{chainConfig: chainConfig}
|
|
}
|
|
|
|
func (f *factory) NewHeader(epoch *big.Int) *block.Header {
|
|
var impl block.HeaderInterface
|
|
switch {
|
|
case epoch.Cmp(f.chainConfig.CrossLinkEpoch) >= 0:
|
|
impl = v1.NewHeader()
|
|
default:
|
|
impl = v0.NewHeader()
|
|
}
|
|
return &block.Header{impl}
|
|
}
|
|
|
|
// Factories corresponding to well-known chain configurations.
|
|
var (
|
|
ForTest = NewFactory(params.TestChainConfig)
|
|
ForTestnet = NewFactory(params.TestnetChainConfig)
|
|
ForMainnet = NewFactory(params.MainnetChainConfig)
|
|
)
|
|
|
|
// NewTestHeader creates a new, empty header object for epoch 0 using the test
|
|
// factory. Use for unit tests.
|
|
func NewTestHeader() *block.Header {
|
|
return ForTest.NewHeader(new(big.Int))
|
|
}
|
|
|