-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
46 lines (37 loc) · 995 Bytes
/
block.go
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
package capusta
import (
"bytes"
"fmt"
)
// Block contain information about Block
type Block struct {
index int64
timestamp int64
data []Transaction
proof int64
hash [32]byte
previousHash [32]byte
}
func (b *Block) GetTransactions() []Transaction {
return b.data
}
// Block.validate check hash of Block
func (b *Block) validate() bool {
hash := Hash(b)
return bytes.Equal(hash[:], b.hash[:])
}
func (b *Block) checkSum() bool {
return bytes.HasPrefix(b.hash[:], defaultProof)
}
// Block.info return string with info about Block
func (b *Block) String() string {
transactions := ""
for _, t := range b.data {
transactions += t.String()
}
if transactions == "" {
transactions = "<Empty transactions>"
}
template := "Block %v \nTimestamp: %v Proof: %v \nHash: %x\nPreviousHash: %x\nValidated: %v\nTransactions: %v\n\n"
return fmt.Sprintf(template, b.index, b.timestamp, b.proof, b.hash, b.previousHash, b.validate(), transactions)
}