func NewBlockchainWithInitialValidators(db db.Database, config *config.Config, validators []primitives.InitialValidatorEntry, skipValidation bool, genesisTime uint64) (*Blockchain, error) {
	b := &Blockchain{
		DB:     db,
		config: config,
		View:   NewBlockchainView(),
	}

	sm, err := NewStateManager(config, genesisTime, b, db)
	if err != nil {
		return nil, err
	}

	b.stateManager = sm

	initialState, err := primitives.InitializeState(config, validators, genesisTime, skipValidation)
	if err != nil {
		return nil, err
	}

	stateRoot, err := ssz.HashTreeRoot(initialState)
	if err != nil {
		return nil, err
	}

	block0 := primitives.Block{
		BlockHeader: primitives.BlockHeader{
			SlotNumber:     0,
			StateRoot:      stateRoot,
			ParentRoot:     zeroHash,
			RandaoReveal:   bls.EmptySignature.Serialize(),
			Signature:      bls.EmptySignature.Serialize(),
			ValidatorIndex: 0,
		},
		BlockBody: primitives.BlockBody{
			ProposerSlashings: []primitives.ProposerSlashing{},
			CasperSlashings:   []primitives.CasperSlashing{},
			Attestations:      []primitives.Attestation{},
			Deposits:          []primitives.Deposit{},
			Exits:             []primitives.Exit{},
		},
	}

	blockHash, err := ssz.HashTreeRoot(block0)
	if err != nil {
		return nil, err
	}

	logrus.WithField("genesisHash", chainhash.Hash(blockHash)).Info("initializing blockchain with genesis block")

	err = b.DB.SetBlock(block0)
	if err != nil {
		return nil, err
	}

	// this is a new database, so let's populate it with default values
	node, err := b.View.Index.AddBlockNodeToIndex(&block0, blockHash, stateRoot, proofs.GetValidatorHash(initialState))
	if err != nil {
		return nil, err
	}

	// check if the block index exists in the database
	_, err = b.DB.GetBlockNode(blockHash)
	if err != nil {
		// if it doesn't, initialize the database
		if err := b.initializeDatabase(node, *initialState); err != nil {
			return nil, err
		}
	} else {
		// if it does, load everything needed from disk
		if err := b.loadBlockchainFromDisk(blockHash); err != nil {
			return nil, err
		}
	}

	return b, nil
}
// NewBlockchainWithInitialValidators creates a new blockchain with the specified
// initial validators.
NewBlockchainWithInitialValidators creates a new blockchain with the specified
initial validators.
// NewBlockchainWithInitialValidators creates a new blockchain with the specified // initial validators .
func NewBlockchainWithInitialValidators(db db.Database, config *config.Config, validators []primitives.InitialValidatorEntry, skipValidation bool, genesisTime uint64) (*Blockchain, error) {
	b := &Blockchain{
		DB:     db,
		config: config,
		View:   NewBlockchainView(),
	}
	sm, err := NewStateManager(config, genesisTime, b, db)
	if err != nil {
		return nil, err
	}
	b.stateManager = sm
	initialState, err := primitives.InitializeState(config, validators, genesisTime, skipValidation)
	if err != nil {
		return nil, err
	}
	stateRoot, err := ssz.HashTreeRoot(initialState)
	if err != nil {
		return nil, err
	}
	block0 := primitives.Block{
		BlockHeader: primitives.BlockHeader{
			SlotNumber:     0,
			StateRoot:      stateRoot,
			ParentRoot:     zeroHash,
			RandaoReveal:   bls.EmptySignature.Serialize(),
			Signature:      bls.EmptySignature.Serialize(),
			ValidatorIndex: 0,
		},
		BlockBody: primitives.BlockBody{
			ProposerSlashings: []primitives.ProposerSlashing{},
			CasperSlashings:   []primitives.CasperSlashing{},
			Attestations:      []primitives.Attestation{},
			Deposits:          []primitives.Deposit{},
			Exits:             []primitives.Exit{},
		},
	}
	blockHash, err := ssz.HashTreeRoot(block0)
	if err != nil {
		return nil, err
	}
	logrus.WithField("genesisHash", chainhash.Hash(blockHash)).Info("initializing blockchain with genesis block")
	err = b.DB.SetBlock(block0)
	if err != nil {
		return nil, err
	}
	node, err := b.View.Index.AddBlockNodeToIndex(&block0, blockHash, stateRoot, proofs.GetValidatorHash(initialState))
	if err != nil {
		return nil, err
	}
	_, err = b.DB.GetBlockNode(blockHash)
	if err != nil {
		if err := b.initializeDatabase(node, *initialState); err != nil {
			return nil, err
		}
	} else {
		if err := b.loadBlockchainFromDisk(blockHash); err != nil {
			return nil, err
		}
	}
	return b, nil
}
func NewBlockchainWithInitialValidators ( db db . Database , config * config . Config , validators [ ] primitives . InitialValidatorEntry , skipValidation bool , genesisTime uint64 ) ( * Blockchain , error ) { b := & Blockchain { DB : db , config : config , View : NewBlockchainView ( ) , } sm , err := NewStateManager ( config , genesisTime , b , db ) if err != nil { return nil , err } b . stateManager = sm initialState , err := primitives . InitializeState ( config , validators , genesisTime , skipValidation ) if err != nil { return nil , err } stateRoot , err := ssz . HashTreeRoot ( initialState ) if err != nil { return nil , err } block0 := primitives . Block { BlockHeader : primitives . BlockHeader { SlotNumber : 0 , StateRoot : stateRoot , ParentRoot : zeroHash , RandaoReveal : bls . EmptySignature . Serialize ( ) , Signature : bls . EmptySignature . Serialize ( ) , ValidatorIndex : 0 , } , BlockBody : primitives . BlockBody { ProposerSlashings : [ ] primitives . ProposerSlashing { } , CasperSlashings : [ ] primitives . CasperSlashing { } , Attestations : [ ] primitives . Attestation { } , Deposits : [ ] primitives . Deposit { } , Exits : [ ] primitives . Exit { } , } , } blockHash , err := ssz . HashTreeRoot ( block0 ) if err != nil { return nil , err } logrus . WithField ( " " , chainhash . Hash ( blockHash ) ) . Info ( " " ) err = b . DB . SetBlock ( block0 ) if err != nil { return nil , err } node , err := b . View . Index . AddBlockNodeToIndex ( & block0 , blockHash , stateRoot , proofs . GetValidatorHash ( initialState ) ) if err != nil { return nil , err } _ , err = b . DB . GetBlockNode ( blockHash ) if err != nil { if err := b . initializeDatabase ( node , * initialState ) ; err != nil { return nil , err } } else { if err := b . loadBlockchainFromDisk ( blockHash ) ; err != nil { return nil , err } } return b , nil }
NewBlockchainWithInitialValidators creates a new blockchain with the specified
initial validators.
NewBlockchainWithInitialValidators creates a new blockchain with the specified initial validators .
// this is a new database, so let's populate it with default values // check if the block index exists in the database // if it doesn't, initialize the database // if it does, load everything needed from disk