-
Notifications
You must be signed in to change notification settings - Fork 175
/
block.go
72 lines (58 loc) · 1.64 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package flow
func Genesis(chainID ChainID) *Block {
// create the raw content for the genesis block
payload := Payload{}
// create the header
header := Header{
ChainID: chainID,
ParentID: ZeroID,
Height: 0,
PayloadHash: payload.Hash(),
Timestamp: GenesisTime,
View: 0,
}
// combine to block
genesis := Block{
Header: &header,
Payload: &payload,
}
return &genesis
}
// Block (currently) includes the header, the payload hashes as well as the
// payload contents.
type Block struct {
Header *Header
Payload *Payload
}
// SetPayload sets the payload and updates the payload hash.
func (b *Block) SetPayload(payload Payload) {
b.Payload = &payload
b.Header.PayloadHash = b.Payload.Hash()
}
// Valid will check whether the block is valid bottom-up.
func (b Block) Valid() bool {
return b.Header.PayloadHash == b.Payload.Hash()
}
// ID returns the ID of the header.
func (b Block) ID() Identifier {
return b.Header.ID()
}
// Checksum returns the checksum of the header.
func (b Block) Checksum() Identifier {
return b.Header.Checksum()
}
// BlockStatus represents the status of a block.
type BlockStatus int
const (
// BlockStatusUnknown indicates that the block status is not known.
BlockStatusUnknown BlockStatus = iota
// BlockStatusFinalized is the status of a finalized block.
BlockStatusFinalized
// BlockStatusSealed is the status of a sealed block.
BlockStatusSealed
)
// String returns the string representation of a transaction status.
func (s BlockStatus) String() string {
return [...]string{"BLOCK_UNKNOWN", "BLOCK_FINALIZED", "BLOCK_SEALED"}[s]
}