-
Notifications
You must be signed in to change notification settings - Fork 12
/
block.go
95 lines (75 loc) · 1.67 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package vm
import (
"context"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/consideritdone/landslidecore/types"
)
var (
_ snowman.Block = &Block{}
)
// Block implements the snowman.Block interface
type Block struct {
id ids.ID
tmBlock *types.Block
vm *VM
status choices.Status
}
// newBlock returns a new Block wrapping the Tendermint Block type and implementing the snowman.Block interface
func (vm *VM) newBlock(tmBlock *types.Block) (*Block, error) {
var id ids.ID
copy(id[:], tmBlock.Hash())
return &Block{
id: id,
tmBlock: tmBlock,
vm: vm,
}, nil
}
func (b *Block) ID() ids.ID {
return b.id
}
func (b *Block) Accept(ctx context.Context) error {
b.SetStatus(choices.Accepted)
return b.vm.applyBlock(b)
}
func (b *Block) Reject(ctx context.Context) error {
b.SetStatus(choices.Rejected)
return nil
}
func (b *Block) SetStatus(status choices.Status) {
b.status = status
}
func (b *Block) Status() choices.Status {
return b.status
}
func (b *Block) Parent() ids.ID {
var id ids.ID
parentHash := b.tmBlock.Header.LastBlockID.Hash
copy(id[:], parentHash)
return id
}
func (b *Block) Verify(context.Context) error {
if b == nil || b.tmBlock == nil {
return errInvalidBlock
}
return b.tmBlock.ValidateBasic()
}
func (b *Block) Bytes() []byte {
block, err := b.tmBlock.ToProto()
if err != nil {
panic(err)
}
data, err := block.Marshal()
if err != nil {
panic(err)
}
return data
}
func (b *Block) Height() uint64 {
return uint64(b.tmBlock.Height)
}
func (b *Block) Timestamp() time.Time {
return b.tmBlock.Time
}