protocol.go 3.81 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"
11
12
)

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

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

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

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

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

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

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

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

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

61
62
		// Exchange messages
		m.promiseRound(pendingSet, sendSet, myID)
63

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

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

81
	return nil
82
83
84
}

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

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

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

	// Reception of the due promises
	for len(pendingSet) > 0 {
105
106
107
108
109
110
111
112
113
114
115
		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)
116
			}
117
118
119
120

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

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

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

142
143
144
145
146
147
// 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()
148
149
		delete(m.peers, k)
	}
150
	m.cServer.Stop()
151
}