Commit 6c665a0c authored by Richer Maximilien's avatar Richer Maximilien Committed by Loïck Bonniot

[c] Add DLog and error messages

parent 6ae6a780
......@@ -49,7 +49,10 @@ func signContract(args []string) {
fmt.Println("Everybody is ready, starting the signature", signatureUUID)
// Signature
manager.Sign()
err = manager.Sign()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(5)
}
// Persist evidencies, if any
}
......@@ -9,12 +9,6 @@ import (
dAPI "dfss/dfssd/api"
)
var (
hashToID map[string]uint32
incomingPromises chan *cAPI.Promise
incomingSignatures chan *cAPI.Signature
)
// Sign performe all the message exchange for the contract to be signed
//
// * Initialize the SignatureManager from starter.go
......@@ -29,17 +23,16 @@ func (m *SignatureManager) Sign() error {
return err
}
hashToID = m.makeSignersHashToIDMap()
m.makeSignersHashToIDMap()
incomingPromises = make(chan *cAPI.Promise)
incomingSignatures = make(chan *cAPI.Signature)
m.cServerIface.incomingPromises = make(chan *cAPI.Promise)
m.cServerIface.incomingSignatures = make(chan *cAPI.Signature)
// Promess rounds
// Follow the sequence until there is no next occurence of me
for m.currentIndex >= 0 {
dAPI.DLog("Starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "]")
log.Println("Starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "]")
dAPI.DLog(fmt.Sprintf("{%d} ", myID) + "Starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "] with nextIndex=" + fmt.Sprintf("%d", nextIndex))
// Set of the promise we are waiting for
pendingSet, err1 := common.GetPendingSet(m.sequence, myID, m.currentIndex)
......@@ -63,15 +56,15 @@ func (m *SignatureManager) Sign() error {
}
}
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Enter signature round")
// Signature round
err = m.SendAllSigns()
if err != nil {
return err
}
err = m.RecieveAllSigns()
if err != nil {
return err
}
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Exit signature round")
return nil
}
......@@ -88,34 +81,37 @@ func (m *SignatureManager) GetClient(to uint32) (*cAPI.ClientClient, error) {
}
// makeEMailMap build an association to reverse a hash to the sequence ID
func (m *SignatureManager) makeSignersHashToIDMap() map[string]uint32 {
func (m *SignatureManager) makeSignersHashToIDMap() {
hashToID := make(map[string]uint32)
m.hashToID = make(map[string]uint32)
signers := m.contract.Signers
for id, signer := range signers {
hashToID[signer.Hash] = uint32(id)
m.hashToID[signer.Hash] = uint32(id)
}
return hashToID
}
// promiseRound describe a promise round: reception and sending
//
// TODO better error management - this function should return `error` !
func (m *SignatureManager) promiseRound(pendingSet, sendSet []uint32, myID uint32) {
// Reception of the due promises
// TODO this ctx needs a timeout !
for len(pendingSet) > 0 {
promise := <-incomingPromises
senderID, exist := hashToID[fmt.Sprintf("%x", promise.SenderKeyHash)]
promise := <-m.cServerIface.incomingPromises
senderID, exist := m.hashToID[fmt.Sprintf("%x", promise.SenderKeyHash)]
if exist {
var err error
pendingSet, err = common.Remove(pendingSet, senderID)
if err != nil {
// Recieve unexpected promise, ignore ?
_ = fmt.Errorf("Recieve unexpected promise")
}
dAPI.DLog("Recieved promise from [" + fmt.Sprintf("%d", senderID) + "]")
m.archives.recievedPromises = append(m.archives.recievedPromises, promise)
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Recieved promise from [" + fmt.Sprintf("%d", senderID) + "] for index " + fmt.Sprintf("%d", promise.Index))
} else {
// Wrong sender keyHash
log.Println("{" + fmt.Sprintf("%d", myID) + "} Wrong sender keyhash !")
}
}
......@@ -126,11 +122,13 @@ func (m *SignatureManager) promiseRound(pendingSet, sendSet []uint32, myID uint3
go func(id uint32, m *SignatureManager) {
promise, err := m.CreatePromise(myID, id)
if err != nil {
// Failed to create promise
_ = fmt.Errorf("Failed to create promise from %d to %d", myID, id)
}
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Send promise to " + fmt.Sprintf("%d", id))
_, err = m.SendPromise(promise, id)
if err != nil {
// We don't check if the promise has been recieved
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Promise have not been recieved !")
_ = fmt.Errorf("Failed to deliver promise from %d to %d", myID, id)
}
c <- promise
}(id, m)
......
......@@ -10,15 +10,18 @@ import (
"google.golang.org/grpc"
)
type clientServer struct{}
type clientServer struct {
incomingPromises chan *cAPI.Promise
incomingSignatures chan *cAPI.Signature
}
// TreatPromise handler
//
// Handle incoming TreatPromise messages
func (s *clientServer) TreatPromise(ctx context.Context, in *cAPI.Promise) (*pAPI.ErrorCode, error) {
// Pass the message to Sign()
if incomingPromises != nil {
incomingPromises <- in
if s.incomingPromises != nil {
s.incomingPromises <- in
// Maybe we can add another channel here for better error management
return &pAPI.ErrorCode{Code: pAPI.ErrorCode_SUCCESS}, nil
}
......@@ -30,15 +33,13 @@ func (s *clientServer) TreatPromise(ctx context.Context, in *cAPI.Promise) (*pAP
//
// Handle incoming TreatSignature messages
func (s *clientServer) TreatSignature(ctx context.Context, in *cAPI.Signature) (*pAPI.ErrorCode, error) {
if incomingSignatures != nil {
incomingSignatures <- in
if s.incomingSignatures != nil {
s.incomingSignatures <- in
// Maybe we can add another channel here for better error management
return &pAPI.ErrorCode{Code: pAPI.ErrorCode_SUCCESS}, nil
}
return &pAPI.ErrorCode{Code: pAPI.ErrorCode_INVARG}, fmt.Errorf("Cannot pass incoming signature")
return nil, nil
}
// Discover handler
......@@ -51,6 +52,7 @@ func (s *clientServer) Discover(ctx context.Context, in *cAPI.Hello) (*cAPI.Hell
// GetServer create and registers a ClientServer, returning the associated GRPC server
func (m *SignatureManager) GetServer() *grpc.Server {
server := net.NewServer(m.auth.Cert, m.auth.Key, m.auth.CA)
cAPI.RegisterClientServer(server, &clientServer{})
m.cServerIface = clientServer{}
cAPI.RegisterClientServer(server, &m.cServerIface)
return server
}
......@@ -32,16 +32,16 @@ func (m *SignatureManager) SendAllSigns() error {
for _, id := range sendSet {
go func(id uint32) {
signature, err := m.CreateSignature(myID, id)
if err != nil {
errorChan <- err
signature, err2 := m.CreateSignature(myID, id)
if err2 != nil {
errorChan <- err2
return
}
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Send sign to " + fmt.Sprintf("%d", id))
_, err = m.SendSignature(signature, id)
if err != nil {
errorChan <- err
_, err2 = m.SendSignature(signature, id)
if err2 != nil {
errorChan <- err2
return
}
......@@ -119,8 +119,9 @@ func (m *SignatureManager) RecieveAllSigns(out chan error) {
// TODO this ctx needs a timeout !
for len(pendingSet) > 0 {
signature := <-incomingSignatures
senderID, exist := hashToID[fmt.Sprintf("%x", signature.SenderKeyHash)]
signature := <-m.cServerIface.incomingSignatures
senderID, exist := m.hashToID[fmt.Sprintf("%x", signature.SenderKeyHash)]
dAPI.DLog("{" + fmt.Sprintf("%d", myID) + "} Receive sign from " + fmt.Sprintf("%d", senderID))
if exist {
var err error
pendingSet, err = common.Remove(pendingSet, senderID)
......
......@@ -25,8 +25,10 @@ type SignatureManager struct {
contract *contract.JSON // contains the contractUUID, the list of the signers' hashes, the hash of the contract
platform pAPI.PlatformClient
peers map[string]*cAPI.ClientClient
hashToID map[string]uint32
nbReady int
cServer *grpc.Server
cServerIface clientServer
sequence []uint32
currentIndex int
uuid string
......
......@@ -82,8 +82,8 @@ func TestSignContract(t *testing.T) {
time.Sleep(time.Duration(i*2) * time.Second)
c.Stdin = strings.NewReader("password\nyes\n")
c.Stderr = os.Stderr
output, err := c.Output()
if err != nil {
output, err1 := c.Output()
if err1 != nil {
output = nil
}
closeChannel <- output
......
......@@ -89,7 +89,7 @@ func createClient(tmpDir string, ca []byte, port int) (*exec.Cmd, error) {
// Prepare the client command.
// The last argument is up to you!
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "127.0.0.1:"+testPort, "-key", keyPath, "-port", strconv.Itoa(port), "-v", "-d")
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "127.0.0.1:"+testPort, "-key", keyPath, "-port", strconv.Itoa(port), "-v")
return cmd, nil
}
......@@ -127,3 +127,15 @@ func checkStderr(t *testing.T, cmd *exec.Cmd, value string) error {
return cmd.Wait()
}
// runDemonstrator provide a way to lanch a dfssd in the background
//
// It will be binded to the provided output file
func runDemonstrator() (*exec.Cmd, error) {
path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssd")
cmd := exec.Command(path, "start")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
return cmd, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment