Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vote.go « transaction « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a205a37857cd752c22550c45bab7de07c793c4be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package transaction

import (
	"crypto/sha1"
	"fmt"
	"hash"
)

const (
	// voteSize is the number of bytes of a vote.
	voteSize = sha1.Size
)

// Vote is a vote cast by a node.
type Vote [voteSize]byte

// Bytes returns a byte slice containing the hash.
func (v Vote) Bytes() []byte {
	return v[:]
}

// VoteFromHash converts the given byte slice containing a hash into a vote.
func VoteFromHash(bytes []byte) (Vote, error) {
	if len(bytes) != voteSize {
		return Vote{}, fmt.Errorf("invalid vote length %d", len(bytes))
	}

	var vote Vote
	copy(vote[:], bytes)

	return vote, nil
}

// VoteFromData hashes the given data and converts it to a vote.
func VoteFromData(data []byte) Vote {
	return sha1.Sum(data)
}

// VoteHash is the hash structure used to compute a Vote from arbitrary data.
type VoteHash struct {
	hash.Hash
}

// NewVoteHash returns a new VoteHash.
func NewVoteHash() VoteHash {
	return VoteHash{sha1.New()}
}

// Vote hashes all data written into VoteHash and returns the resulting Vote.
func (v VoteHash) Vote() (Vote, error) {
	return VoteFromHash(v.Sum(nil))
}