protocol

package
v0.0.0-...-1b43c1f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2020 License: AGPL-3.0 Imports: 2 Imported by: 0

README

Navigation: DEDIS :: Cothority Template :: Protocol Template

Protocol Template

In the this directory, you will find the implementation of a toy protocol where nodes work together to count how many instances of the protocol there are. It demonstrates how to send messages and how to handle incoming messages.

To implement a new protocol, you must create the NewProtocol function and register it.

func init() {
	_, err := onet.GlobalProtocolRegister(Name, NewProtocol)
	if err != nil {
		panic(err)
	}
}

Writing NewProtocol - the protocol constructor

Inside NewProtocol you must register the channels that are used for receiving messages, e.g., announceChan chan announceWrapper. Any state that is needed by the protocol (for example, the ChildCount channel) should be initialized here too.

The messages are defined in the file struct.go. For each message, you need to define the message itself (e.g., Announce), and the message as it will arrive to you from the cothority server (e.g., announceWrapper).

Writing the protocol logic

After registering, define a struct that implements the onet.ProtocolInstance interface:

type TemplateProtocol struct {
  *onet.TreeNodeInstance
  ...
}

// Check that *TemplateProtocol implements onet.ProtocolInstance
var _ onet.ProtocolInstance = (*TemplateProtocol)(nil)

Usually, an implementation for Start and Dispatch is needed, the others are optional and the default implementation will be used if they are not implemented. Dispatch is where the main protocol logic is implemented and it is called automatically by onet when the protocol is initiated. Start is the entry-point of the protocol, it needs to be called manually, typically by the root node.

Documentation

Overview

Package protocol contains an example demonstrating how to write a protocol and a simulation.

The protocol has two messages:

  • Announce which is sent from the root down the tree
  • Reply which is sent back up to the root

If you want to add other messages, be sure to follow the way Announce and StructAnnounce are set up.

A simple protocol uses four files: - struct.go defines the messages sent around - protocol.go defines the actions for each message - protocol_test.go tests the protocol in a local test - simulation.go tests the protocol on distant platforms like deterlab

Index

Constants

View Source
const Name = "Template"

Name can be used from other packages to refer to this protocol.

Variables

This section is empty.

Functions

func NewProtocol

func NewProtocol(n *onet.TreeNodeInstance) (onet.ProtocolInstance, error)

NewProtocol initialises the structure for use in one round

Types

type Announce

type Announce struct {
	Message string
}

Announce is used to pass a message to all children.

type Reply

type Reply struct {
	ChildrenCount int
}

Reply returns the count of all children.

type TemplateProtocol

type TemplateProtocol struct {
	*onet.TreeNodeInstance

	ChildCount chan int
	// contains filtered or unexported fields
}

TemplateProtocol holds the state of a given protocol.

For this example, it defines a channel that will receive the number of children. Only the root-node will write to the channel.

func (*TemplateProtocol) Dispatch

func (p *TemplateProtocol) Dispatch() error

Dispatch implements the main logic of the protocol. The function is only called once. The protocol is considered finished when Dispatch returns and Done is called.

func (*TemplateProtocol) Start

func (p *TemplateProtocol) Start() error

Start sends the Announce-message to all children

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL