protocol.go 3.88 KB
Newer Older
1
// Package sign handles contract and signature operations.
2 3 4 5
package sign

import (
	"fmt"
6
	"time"
7 8 9

	cAPI "dfss/dfssc/api"
	"dfss/dfssc/common"
Richer Maximilien's avatar
Richer Maximilien committed
10
	dAPI "dfss/dfssd/api"
Loïck Bonniot's avatar
Loïck Bonniot committed
11
	"github.com/spf13/viper"
12 13
)

14
// Sign performs all the message exchanges for the contract to be signed
15 16 17 18 19 20
//
// * Initialize the SignatureManager from starter.go
// * Compute the reversed map [mail -> ID] of signers
// * Make channels for handlers
// * Promises rounds
// * Signature round
21
func (m *SignatureManager) Sign() error {
22 23 24 25 26 27

	defer func() {
		m.finished = true
		m.closeConnections()
	}()

28
	myID, nextIndex, err := m.Initialize()
29 30 31 32
	if err != nil {
		return err
	}

33
	m.makeSignersHashToIDMap()
34 35
	m.cServerIface.incomingPromises = make(chan interface{}, chanBufferSize)
	m.cServerIface.incomingSignatures = make(chan interface{}, chanBufferSize)
36

37 38
	// Cooldown delay, let other clients wake-up their channels
	time.Sleep(time.Second)
39

Loïck Bonniot's avatar
Loïck Bonniot committed
40 41
	seqLen := len(m.sequence)

42
	// Promess rounds
43
	// Follow the sequence until there is no next occurence of me
44
	for m.currentIndex >= 0 {
Loïck Bonniot's avatar
Loïck Bonniot committed
45
		m.OnProgressUpdate(m.currentIndex, seqLen+1)
Loïck Bonniot's avatar
Loïck Bonniot committed
46
		time.Sleep(viper.GetDuration("slowdown"))
47
		dAPI.DLog("starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "] with nextIndex=" + fmt.Sprintf("%d", nextIndex))
Richer Maximilien's avatar
Richer Maximilien committed
48

Loïck Bonniot's avatar
Loïck Bonniot committed
49
		// Set of promises we are waiting for
50 51 52 53
		var pendingSet []uint32
		pendingSet, err = common.GetPendingSet(m.sequence, myID, m.currentIndex)
		if err != nil {
			return err
54 55
		}

56
		// Set of the promises we must send
57 58 59 60
		var sendSet []uint32
		sendSet, err = common.GetSendSet(m.sequence, myID, m.currentIndex)
		if err != nil {
			return err
61 62
		}

63 64
		// Exchange messages
		m.promiseRound(pendingSet, sendSet, myID)
65

66
		m.currentIndex = nextIndex
67 68 69
		nextIndex, err = common.FindNextIndex(m.sequence, myID, m.currentIndex)
		if err != nil {
			return err
70 71 72
		}
	}

Loïck Bonniot's avatar
Loïck Bonniot committed
73
	m.OnProgressUpdate(seqLen, seqLen+1)
74
	dAPI.DLog("entering signature round")
75
	// Signature round
76
	err = m.ExchangeAllSignatures()
77 78 79
	if err != nil {
		return err
	}
80
	dAPI.DLog("exiting signature round")
Loïck Bonniot's avatar
Loïck Bonniot committed
81
	m.OnProgressUpdate(seqLen+1, seqLen+1)
82

83
	return nil
84 85 86
}

// GetClient retrieves the Client to the specified sequence id provided it exists
87 88 89 90
func (m *SignatureManager) GetClient(to uint32) (client *cAPI.ClientClient, mail string) {
	mail = m.contract.Signers[to].Email
	client = m.peers[mail]
	return
91 92
}

93
// makeSignersHashToIDMap builds an association to reverse a hash to the sequence ID
94 95
func (m *SignatureManager) makeSignersHashToIDMap() {
	m.hashToID = make(map[string]uint32)
96 97
	signers := m.contract.Signers
	for id, signer := range signers {
98
		m.hashToID[signer.Hash] = uint32(id)
99 100 101
	}
}

102
// promiseRound describes a promise round: reception and sending
103 104 105 106
func (m *SignatureManager) promiseRound(pendingSet, sendSet []uint32, myID uint32) {

	// Reception of the due promises
	for len(pendingSet) > 0 {
107 108 109 110 111 112 113 114 115 116 117
		select {
		case promiseIface := <-m.cServerIface.incomingPromises:
			promise := (promiseIface).(*cAPI.Promise)
			senderID, exist := m.hashToID[fmt.Sprintf("%x", promise.Context.SenderKeyHash)]
			if exist {
				var err error
				pendingSet, err = common.Remove(pendingSet, senderID)
				if err != nil {
					continue
				}
				m.archives.receivedPromises = append(m.archives.receivedPromises, promise)
118
			}
119 120 121 122

		case <-time.After(time.Minute):
			// TODO contact TTP
			return
123
		}
124 125
	}

126
	c := make(chan *cAPI.Promise, chanBufferSize)
127 128 129 130
	// Sending of due promises
	for _, id := range sendSet {
		go func(id uint32, m *SignatureManager) {
			promise, err := m.CreatePromise(myID, id)
131 132
			if err == nil {
				_ = m.SendEvidence(promise, nil, id)
133 134 135
			}
			c <- promise
		}(id, m)
136 137
	}

138
	// Verifying we sent all the due promises
139
	for range sendSet {
Loïck Bonniot's avatar
Loïck Bonniot committed
140
		<-c
141 142
	}
}
143

144 145 146 147 148 149
// closeConnections tries to close all established connection with other peers and platform.
// It also stops the local server.
func (m *SignatureManager) closeConnections() {
	_ = m.platformConn.Close()
	for k, peer := range m.peersConn {
		_ = peer.Close()
150 151
		delete(m.peers, k)
	}
152
	m.cServer.Stop()
153
}