archivesManager.go 4.11 KB
Newer Older
1
// Package entities contains database entities and helpers on these entities.
2
package entities
Caro Axel's avatar
Caro Axel committed
3 4 5 6 7 8 9 10 11 12

import (
	cAPI "dfss/dfssc/api"
	"dfss/mgdb"
	"gopkg.in/mgo.v2/bson"
)

// ArchivesManager : handles the structure of a SignatureArchives, with functions suited for the TTP resolve protocol.
type ArchivesManager struct {
	DB       *mgdb.MongoManager
13
	Archives *SignatureArchives
Caro Axel's avatar
Caro Axel committed
14 15 16 17 18 19 20 21 22 23 24 25 26
}

// NewArchivesManager : create a new archivesManager, with the specified mgdb manager, but
// doesn't initialize the signatureArchives. (see function 'InitializeArchives').
func NewArchivesManager(db *mgdb.MongoManager) *ArchivesManager {
	return &ArchivesManager{
		DB: db,
	}
}

// InitializeArchives : if an entry in the database for this signature exists, retrieves it, otherwise creates it.
//
// This function should only be called after function IsRequestValid.
Caro Axel's avatar
Caro Axel committed
27
func (manager *ArchivesManager) InitializeArchives(promise *cAPI.Promise, signatureUUID bson.ObjectId, signers *[]Signer) error {
Caro Axel's avatar
Caro Axel committed
28 29 30
	present, archives := manager.ContainsSignature(signatureUUID)

	if !present {
31
		archives = NewSignatureArchives(signatureUUID, promise.Context.Sequence, *signers, promise.Context.ContractDocumentHash, promise.Context.Seal)
Caro Axel's avatar
Caro Axel committed
32 33 34 35
		ok, err := manager.DB.Get("signatures").Insert(*archives)
		if !ok {
			return err
		}
Caro Axel's avatar
Caro Axel committed
36 37 38
	}

	manager.Archives = archives
Caro Axel's avatar
Caro Axel committed
39
	return nil
Caro Axel's avatar
Caro Axel committed
40 41 42 43
}

// ContainsSignature : checks if the specified signatureUUID matches a SignatureArchives in the database.
// If it exists, returns it.
44 45
func (manager *ArchivesManager) ContainsSignature(signatureUUID bson.ObjectId) (present bool, archives *SignatureArchives) {
	err := manager.DB.Get("signatures").FindByID(SignatureArchives{ID: signatureUUID}, &archives)
Caro Axel's avatar
Caro Axel committed
46 47
	if err != nil {
		present = false
48
		archives = &SignatureArchives{}
Caro Axel's avatar
Caro Axel committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
		return
	}

	present = true
	return
}

// HasReceivedAbortToken : determines if the specified signer has already been sent an abort token in the specified signatureArchives.
func (manager *ArchivesManager) HasReceivedAbortToken(signerIndex uint32) bool {
	for _, s := range manager.Archives.AbortedSigners {
		if signerIndex == s.SignerIndex {
			return true
		}
	}

	return false
}

// WasContractSigned : determines if the ttp already generated a signed contract for this signatureArchives, and returns it if true.
func (manager *ArchivesManager) WasContractSigned() (bool, []byte) {
	signedContract := manager.Archives.SignedContract
	if len(signedContract) != 0 {
		return true, signedContract
	}

	return false, []byte{}
}

Caro Axel's avatar
Caro Axel committed
77
// HasSignerPromised : determines if the specified signer has promised to sign at least one time.
Caro Axel's avatar
Caro Axel committed
78 79
func (manager *ArchivesManager) HasSignerPromised(signer uint32) bool {
	for _, p := range manager.Archives.ReceivedPromises {
Caro Axel's avatar
Caro Axel committed
80
		if p.SenderKeyIndex == signer {
Caro Axel's avatar
Caro Axel committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			return true
		}
	}

	return false
}

// AddToAbort : adds the specified signer to the aborted signers of the signatureArchives.
// If the signer is already present, does nothing.
func (manager *ArchivesManager) AddToAbort(signerIndex uint32) {
	for _, s := range manager.Archives.AbortedSigners {
		if s.SignerIndex == signerIndex {
			return
		}
	}

	// TODO
	// This requires the implementation of promises
	var abortIndex uint32
	abortIndex = 0
101
	abortedSigner := NewAbortedSigner(signerIndex, abortIndex)
Caro Axel's avatar
Caro Axel committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

	manager.Archives.AbortedSigners = append(manager.Archives.AbortedSigners, *abortedSigner)
}

// AddToDishonest : adds the specified signer to the dishonest signers of the signatureArchives.
// If the signer is already present, does nothing.
func (manager *ArchivesManager) AddToDishonest(signerIndex uint32) {
	for _, s := range manager.Archives.DishonestSigners {
		if s == signerIndex {
			return
		}
	}

	manager.Archives.DishonestSigners = append(manager.Archives.DishonestSigners, signerIndex)
}

// AddPromise : adds the specified promises to the list of received promises of the SignatureArchives.
Caro Axel's avatar
Caro Axel committed
119
// If we have already a promise from this client, does not add it.
120
func (manager *ArchivesManager) AddPromise(promise *Promise) {
Caro Axel's avatar
Caro Axel committed
121
	for _, p := range manager.Archives.ReceivedPromises {
Caro Axel's avatar
Caro Axel committed
122
		if p.SenderKeyIndex == promise.SenderKeyIndex {
Caro Axel's avatar
Caro Axel committed
123 124 125 126 127 128
			return
		}
	}

	manager.Archives.ReceivedPromises = append(manager.Archives.ReceivedPromises, *promise)
}