go.dedis.ch/onet/v4@v4.0.0-pre1/simul/manage/simulation/simul.go (about)

     1  package main
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/BurntSushi/toml"
     7  	"go.dedis.ch/onet/v4"
     8  	"go.dedis.ch/onet/v4/log"
     9  	"go.dedis.ch/onet/v4/simul"
    10  	"go.dedis.ch/onet/v4/simul/manage"
    11  	"go.dedis.ch/onet/v4/simul/monitor"
    12  	"golang.org/x/xerrors"
    13  )
    14  
    15  /*
    16  Defines the simulation for the count-protocol
    17  */
    18  
    19  func init() {
    20  	onet.SimulationRegister("Count", NewSimulation)
    21  }
    22  
    23  // Simulation only holds the BFTree simulation
    24  type simulation struct {
    25  	onet.SimulationBFTree
    26  }
    27  
    28  // NewSimulation returns the new simulation, where all fields are
    29  // initialised using the config-file
    30  func NewSimulation(config string) (onet.Simulation, error) {
    31  	es := &simulation{}
    32  	// Set defaults before toml.Decode
    33  	es.Suite = "Ed25519"
    34  
    35  	_, err := toml.Decode(config, es)
    36  	if err != nil {
    37  		return nil, xerrors.Errorf("decoding: %v", err)
    38  	}
    39  	return es, nil
    40  }
    41  
    42  // Setup creates the tree used for that simulation
    43  func (e *simulation) Setup(dir string, hosts []string) (
    44  	*onet.SimulationConfig, error) {
    45  	sc := &onet.SimulationConfig{}
    46  	e.CreateRoster(sc, hosts, 2000)
    47  	err := e.CreateTree(sc)
    48  	if err != nil {
    49  		return nil, xerrors.Errorf("creating tree: %v", err)
    50  	}
    51  	return sc, nil
    52  }
    53  
    54  // Run is used on the destination machines and runs a number of
    55  // rounds
    56  func (e *simulation) Run(config *onet.SimulationConfig) error {
    57  	size := config.Tree.Size()
    58  	log.Lvl2("Size is:", size, "rounds:", e.Rounds)
    59  	for round := 0; round < e.Rounds; round++ {
    60  		log.Lvl1("Starting round", round)
    61  		round := monitor.NewTimeMeasure("round")
    62  		p, err := config.Overlay.CreateProtocol("Count", config.Tree, onet.NilServiceID)
    63  		if err != nil {
    64  			return xerrors.Errorf("creating protocol: %v", err)
    65  		}
    66  		go p.Start()
    67  		children := <-p.(*manage.ProtocolCount).Count
    68  		round.Record()
    69  		if children != size {
    70  			return xerrors.New("Didn't get " + strconv.Itoa(size) +
    71  				" children")
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func main() {
    78  	simul.Start()
    79  }