func NewShardApp(options config.Options) (*ShardApp, error) {
	beaconAddr, err := utils.MultiaddrStringToDialString(options.BeaconRPC)
	if err != nil {
		return nil, err
	}

	cc, err := grpc.Dial(beaconAddr, grpc.WithInsecure())
	if err != nil {
		return nil, fmt.Errorf("could not connect to beacon host at %s with error: %s", options.BeaconRPC, err)
	}

	ma, err := multiaddr.NewMultiaddr(options.RPCListen)
	if err != nil {
		return nil, err
	}

	addr, err := manet.ToNetAddr(ma)
	if err != nil {
		return nil, err
	}

	c := config.ShardConfig{
		BeaconConn:  cc,
		RPCProtocol: addr.Network(),
		RPCAddress:  addr.String(),
		TrackShards: options.TrackShards,
		P2PListen:   options.P2PListen,
	}

	a := &ShardApp{Config: c}

	a.beaconRPC = pb.NewBlockchainRPCClient(a.Config.BeaconConn)

	priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
	if err != nil {
		panic(err)
	}

	p2pAddr, err := multiaddr.NewMultiaddr(c.P2PListen)
	if err != nil {
		return nil, err
	}

	hn, err := p2p.NewHostNode(context.TODO(), p2p.HostNodeOptions{
		ListenAddresses: []multiaddr.Multiaddr{p2pAddr},
		PrivateKey:      priv,
		ConnManagerOptions: p2p.ConnectionManagerOptions{
			BootstrapAddresses: nil,
			MDNS:               p2p.MDNSOptions{},
		},
		Timeout: 60 * time.Second,
	})
	if err != nil {
		return nil, err
	}

	a.shardMux = chain.NewShardMux(a.beaconRPC, hn)

	a.hostnode = hn

	genesisTime, err := a.beaconRPC.GetGenesisTime(context.Background(), &empty.Empty{})
	if err != nil {
		return nil, err
	}

	shards, err := utils.ParseRanges(a.Config.TrackShards)
	if err != nil {
		return nil, err
	}

	for _, shard := range shards {
		shardGenesis := primitives.GetGenesisBlockForShard(uint64(shard))

		shardGenesisHash, _ := ssz.HashTreeRoot(shardGenesis)

		_, err := a.shardMux.StartManaging(uint64(shard), chain.ShardChainInitializationParameters{
			RootBlockHash: shardGenesisHash,
			RootSlot:      0,
			GenesisTime:   genesisTime.GenesisTime,
		})
		if err != nil {
			return nil, err
		}
	}

	networkConfig, found := beaconconfig.NetworkIDs[options.NetworkID]
	if !found {
		return nil, fmt.Errorf("could not find network config %s", options.NetworkID)
	}

	go func() {
		err := rpc.Serve(a.Config.RPCProtocol, a.Config.RPCAddress, a.shardMux, &networkConfig, hn)
		if err != nil {
			logrus.Error(err)
		}
	}()

	return a, nil
}
// NewShardApp creates a new shard app given a config.
NewShardApp creates a new shard app given a config.
// NewShardApp creates a new shard app given a config .
func NewShardApp(options config.Options) (*ShardApp, error) {
	beaconAddr, err := utils.MultiaddrStringToDialString(options.BeaconRPC)
	if err != nil {
		return nil, err
	}
	cc, err := grpc.Dial(beaconAddr, grpc.WithInsecure())
	if err != nil {
		return nil, fmt.Errorf("could not connect to beacon host at %s with error: %s", options.BeaconRPC, err)
	}
	ma, err := multiaddr.NewMultiaddr(options.RPCListen)
	if err != nil {
		return nil, err
	}
	addr, err := manet.ToNetAddr(ma)
	if err != nil {
		return nil, err
	}
	c := config.ShardConfig{
		BeaconConn:  cc,
		RPCProtocol: addr.Network(),
		RPCAddress:  addr.String(),
		TrackShards: options.TrackShards,
		P2PListen:   options.P2PListen,
	}
	a := &ShardApp{Config: c}
	a.beaconRPC = pb.NewBlockchainRPCClient(a.Config.BeaconConn)
	priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
	if err != nil {
		panic(err)
	}
	p2pAddr, err := multiaddr.NewMultiaddr(c.P2PListen)
	if err != nil {
		return nil, err
	}
	hn, err := p2p.NewHostNode(context.TODO(), p2p.HostNodeOptions{
		ListenAddresses: []multiaddr.Multiaddr{p2pAddr},
		PrivateKey:      priv,
		ConnManagerOptions: p2p.ConnectionManagerOptions{
			BootstrapAddresses: nil,
			MDNS:               p2p.MDNSOptions{},
		},
		Timeout: 60 * time.Second,
	})
	if err != nil {
		return nil, err
	}
	a.shardMux = chain.NewShardMux(a.beaconRPC, hn)
	a.hostnode = hn
	genesisTime, err := a.beaconRPC.GetGenesisTime(context.Background(), &empty.Empty{})
	if err != nil {
		return nil, err
	}
	shards, err := utils.ParseRanges(a.Config.TrackShards)
	if err != nil {
		return nil, err
	}
	for _, shard := range shards {
		shardGenesis := primitives.GetGenesisBlockForShard(uint64(shard))
		shardGenesisHash, _ := ssz.HashTreeRoot(shardGenesis)
		_, err := a.shardMux.StartManaging(uint64(shard), chain.ShardChainInitializationParameters{
			RootBlockHash: shardGenesisHash,
			RootSlot:      0,
			GenesisTime:   genesisTime.GenesisTime,
		})
		if err != nil {
			return nil, err
		}
	}
	networkConfig, found := beaconconfig.NetworkIDs[options.NetworkID]
	if !found {
		return nil, fmt.Errorf("could not find network config %s", options.NetworkID)
	}
	go func() {
		err := rpc.Serve(a.Config.RPCProtocol, a.Config.RPCAddress, a.shardMux, &networkConfig, hn)
		if err != nil {
			logrus.Error(err)
		}
	}()
	return a, nil
}
func NewShardApp ( options config . Options ) ( * ShardApp , error ) { beaconAddr , err := utils . MultiaddrStringToDialString ( options . BeaconRPC ) if err != nil { return nil , err } cc , err := grpc . Dial ( beaconAddr , grpc . WithInsecure ( ) ) if err != nil { return nil , fmt . Errorf ( " " , options . BeaconRPC , err ) } ma , err := multiaddr . NewMultiaddr ( options . RPCListen ) if err != nil { return nil , err } addr , err := manet . ToNetAddr ( ma ) if err != nil { return nil , err } c := config . ShardConfig { BeaconConn : cc , RPCProtocol : addr . Network ( ) , RPCAddress : addr . String ( ) , TrackShards : options . TrackShards , P2PListen : options . P2PListen , } a := & ShardApp { Config : c } a . beaconRPC = pb . NewBlockchainRPCClient ( a . Config . BeaconConn ) priv , _ , err := crypto . GenerateEd25519Key ( rand . Reader ) if err != nil { panic ( err ) } p2pAddr , err := multiaddr . NewMultiaddr ( c . P2PListen ) if err != nil { return nil , err } hn , err := p2p . NewHostNode ( context . TODO ( ) , p2p . HostNodeOptions { ListenAddresses : [ ] multiaddr . Multiaddr { p2pAddr } , PrivateKey : priv , ConnManagerOptions : p2p . ConnectionManagerOptions { BootstrapAddresses : nil , MDNS : p2p . MDNSOptions { } , } , Timeout : 60 * time . Second , } ) if err != nil { return nil , err } a . shardMux = chain . NewShardMux ( a . beaconRPC , hn ) a . hostnode = hn genesisTime , err := a . beaconRPC . GetGenesisTime ( context . Background ( ) , & empty . Empty { } ) if err != nil { return nil , err } shards , err := utils . ParseRanges ( a . Config . TrackShards ) if err != nil { return nil , err } for _ , shard := range shards { shardGenesis := primitives . GetGenesisBlockForShard ( uint64 ( shard ) ) shardGenesisHash , _ := ssz . HashTreeRoot ( shardGenesis ) _ , err := a . shardMux . StartManaging ( uint64 ( shard ) , chain . ShardChainInitializationParameters { RootBlockHash : shardGenesisHash , RootSlot : 0 , GenesisTime : genesisTime . GenesisTime , } ) if err != nil { return nil , err } } networkConfig , found := beaconconfig . NetworkIDs [ options . NetworkID ] if ! found { return nil , fmt . Errorf ( " " , options . NetworkID ) } go func ( ) { err := rpc . Serve ( a . Config . RPCProtocol , a . Config . RPCAddress , a . shardMux , & networkConfig , hn ) if err != nil { logrus . Error ( err ) } } ( ) return a , nil }
NewShardApp creates a new shard app given a config.
NewShardApp creates a new shard app given a config .