protocol.go 3.62 KB
Newer Older
1 2 3 4
package sign

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

	cAPI "dfss/dfssc/api"
	"dfss/dfssc/common"
Richer Maximilien's avatar
Richer Maximilien committed
9
	dAPI "dfss/dfssd/api"
10 11
)

12
// Sign performs all the message exchanges for the contract to be signed
13 14 15 16 17 18
//
// * Initialize the SignatureManager from starter.go
// * Compute the reversed map [mail -> ID] of signers
// * Make channels for handlers
// * Promises rounds
// * Signature round
19
func (m *SignatureManager) Sign() error {
20
	myID, nextIndex, err := m.Initialize()
21 22 23 24
	if err != nil {
		return err
	}

25
	m.makeSignersHashToIDMap()
26 27
	m.cServerIface.incomingPromises = make(chan interface{}, chanBufferSize)
	m.cServerIface.incomingSignatures = make(chan interface{}, chanBufferSize)
28

29 30
	// Cooldown delay, let other clients wake-up their channels
	time.Sleep(time.Second)
31

32
	// Promess rounds
33
	// Follow the sequence until there is no next occurence of me
34
	for m.currentIndex >= 0 {
35
		dAPI.DLog("starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "] with nextIndex=" + fmt.Sprintf("%d", nextIndex))
Richer Maximilien's avatar
Richer Maximilien committed
36

37
		// Set of the promise we are waiting for
38 39 40 41
		var pendingSet []uint32
		pendingSet, err = common.GetPendingSet(m.sequence, myID, m.currentIndex)
		if err != nil {
			return err
42 43
		}

44
		// Set of the promises we must send
45 46 47 48
		var sendSet []uint32
		sendSet, err = common.GetSendSet(m.sequence, myID, m.currentIndex)
		if err != nil {
			return err
49 50
		}

51 52
		// Exchange messages
		m.promiseRound(pendingSet, sendSet, myID)
53

54
		m.currentIndex = nextIndex
55 56 57
		nextIndex, err = common.FindNextIndex(m.sequence, myID, m.currentIndex)
		if err != nil {
			return err
58 59 60
		}
	}

61
	dAPI.DLog("entering signature round")
62
	// Signature round
63
	err = m.ExchangeAllSignatures()
64 65 66
	if err != nil {
		return err
	}
67
	dAPI.DLog("exiting signature round")
68

69 70
	// Network's job is done, cleaning time
	// Shutdown and platform client and TODO peer server & connections
71
	return m.platformConn.Close()
72 73 74
}

// GetClient retrieves the Client to the specified sequence id provided it exists
75 76 77 78
func (m *SignatureManager) GetClient(to uint32) (client *cAPI.ClientClient, mail string) {
	mail = m.contract.Signers[to].Email
	client = m.peers[mail]
	return
79 80
}

81
// makeSignersHashToIDMap builds an association to reverse a hash to the sequence ID
82 83
func (m *SignatureManager) makeSignersHashToIDMap() {
	m.hashToID = make(map[string]uint32)
84 85
	signers := m.contract.Signers
	for id, signer := range signers {
86
		m.hashToID[signer.Hash] = uint32(id)
87 88 89
	}
}

90
// promiseRound describes a promise round: reception and sending
91 92 93 94
func (m *SignatureManager) promiseRound(pendingSet, sendSet []uint32, myID uint32) {

	// Reception of the due promises
	for len(pendingSet) > 0 {
95 96 97 98 99 100 101 102 103 104 105
		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)
106
			}
107 108 109 110

		case <-time.After(time.Minute):
			// TODO contact TTP
			return
111
		}
112 113
	}

114
	c := make(chan *cAPI.Promise, chanBufferSize)
115 116 117 118
	// Sending of due promises
	for _, id := range sendSet {
		go func(id uint32, m *SignatureManager) {
			promise, err := m.CreatePromise(myID, id)
119 120
			if err == nil {
				_ = m.SendEvidence(promise, nil, id)
121 122 123
			}
			c <- promise
		}(id, m)
124 125
	}

126
	// Verifying we sent all the due promises
127
	for range sendSet {
128
		<-c
129 130
	}
}
131 132 133 134

// closeAllPeerClient tries to close all established connection with other peers
func (m *SignatureManager) closeAllPeerClient() {
	for k, client := range m.peersConn {
135
		_ = client.Close()
136 137 138 139
		// Remove associated grpc client
		delete(m.peers, k)
	}
}