Skip to content
Snippets Groups Projects
Commit 6d5f585d authored by Josh Brooks's avatar Josh Brooks
Browse files

Standardize mutex unlocks as defers

parent aa6bc689
Branches
Tags
No related merge requests found
......@@ -78,9 +78,9 @@ func (b *Block) NextBlock() (*Block, error) {
// GetCreated returns a copy of the created coins list
func (b *Block) GetCreated() []coin.Coin {
b.mutex.Lock()
defer b.mutex.Unlock()
cCopy := make([]coin.Coin, len(b.created))
copy(cCopy, b.created)
b.mutex.Unlock()
return cCopy
}
......@@ -102,9 +102,9 @@ func (b *Block) AddCreated(c []coin.Coin) error {
// GetDestroyed returns a copy of the destroyed coins list
func (b *Block) GetDestroyed() []coin.Coin {
b.mutex.Lock()
defer b.mutex.Unlock()
cCopy := make([]coin.Coin, len(b.destroyed))
copy(cCopy, b.destroyed)
b.mutex.Unlock()
return cCopy
}
......@@ -139,8 +139,8 @@ func (b *Block) GetHash() (BlockHash, error) {
func (b *Block) GetPreviousHash() BlockHash {
var rtnBH BlockHash
b.mutex.Lock()
defer b.mutex.Unlock()
copy(rtnBH[:], b.previousHash[:])
b.mutex.Unlock()
return rtnBH
}
......@@ -148,7 +148,7 @@ func (b *Block) GetPreviousHash() BlockHash {
func (b *Block) GetLifecycle() BlockLifecycle {
b.mutex.Lock()
blc := b.lifecycle
b.mutex.Unlock()
defer b.mutex.Unlock()
return blc
}
......@@ -276,7 +276,7 @@ func Deserialize(sBlock []byte) (*Block, error) {
b.id = sb.ID
b.lifecycle = Baked
b.mutex.Unlock()
defer b.mutex.Unlock()
return &b, nil
}
......
......@@ -38,7 +38,7 @@ type Stream struct {
rng csprng.Source
source []byte
numStream uint
mut sync.Mutex
mutex sync.Mutex
fortunaHash hash.Hash
}
......@@ -81,8 +81,8 @@ func (sg *StreamGenerator) newStream() *Stream {
// If the # of open streams exceeds streamCount,
// this function blocks (and prints a log warning) until a stream is available
func (sg *StreamGenerator) GetStream() *Stream {
//Initialize a stream
var retStream *Stream
// If there is a stream waiting to be used, take that from the channel and return in
select {
case retStream = <-sg.waitingStreams:
......@@ -112,9 +112,10 @@ func (sg *StreamGenerator) Close(stream *Stream) {
// BlockSize into AES then run it until blockSize*scalingFactor bytes are read. Every time
// BlockSize*scalingFactor bytes are read this functions blocks until it rereads csprng.Source.
func (s *Stream) Read(b []byte) (int, error) {
s.mut.Lock()
s.mutex.Lock()
defer s.mutex.Unlock()
if len(b)%aes.BlockSize != 0 {
s.mut.Unlock()
return 0, errors.New("requested read length is not byte aligned")
}
......@@ -136,7 +137,6 @@ func (s *Stream) Read(b []byte) (int, error) {
extension = make([]byte, aes.BlockSize)
_, err := s.rng.Read(extension)
if err != nil {
s.mut.Unlock()
return 0, err
}
s.entropyCnt = s.streamGen.scalingFactor
......@@ -149,7 +149,6 @@ func (s *Stream) Read(b []byte) (int, error) {
copy(s.source, dst)
s.mut.Unlock()
return len(b), nil
}
......@@ -161,11 +160,13 @@ func Fortuna(src, ext []byte, fortunaHash hash.Hash) cipher.Stream {
fortunaHash.Write(src)
fortunaHash.Write(ext)
key := fortunaHash.Sum(nil)
// Initialize a block cipher on that key
block, err := aes.NewCipher(key[:aes.BlockSize])
if err != nil {
jww.FATAL.Panicf(err.Error())
}
// Encrypt the counter and place into destination
return cipher.NewCTR(block, key[aes.BlockSize:2*aes.BlockSize])
}
......@@ -174,6 +175,5 @@ func Fortuna(src, ext []byte, fortunaHash hash.Hash) cipher.Stream {
// csprng.Source interface.
func (s *Stream) SetSeed(seed []byte) error {
jww.INFO.Printf("Stream does not utilise SetSeed().")
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment