-
Notifications
You must be signed in to change notification settings - Fork 662
/
backend.go
97 lines (81 loc) · 2.88 KB
/
backend.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
96
97
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
)
// Shared fields used by visitors.
type backend struct {
mempool.Mempool
// lastAccepted is the ID of the last block that had Accept() called on it.
lastAccepted ids.ID
// blkIDToState is a map from a block's ID to the state of the block.
// Blocks are put into this map when they are verified.
// Proposal blocks are removed from this map when they are rejected
// or when a child is accepted.
// All other blocks are removed when they are accepted/rejected.
// Note that Genesis block is a commit block so no need to update
// blkIDToState with it upon backend creation (Genesis is already accepted)
blkIDToState map[ids.ID]*blockState
state state.State
ctx *snow.Context
}
func (b *backend) GetState(blkID ids.ID) (state.Chain, bool) {
// If the block is in the map, it is either processing or a proposal block
// that was accepted without an accepted child.
if state, ok := b.blkIDToState[blkID]; ok {
if state.onAcceptState != nil {
return state.onAcceptState, true
}
return nil, false
}
// Note: If the last accepted block is a proposal block, we will have
// returned in the above if statement.
return b.state, blkID == b.state.GetLastAccepted()
}
func (b *backend) getOnAbortState(blkID ids.ID) (state.Diff, bool) {
state, ok := b.blkIDToState[blkID]
if !ok || state.onAbortState == nil {
return nil, false
}
return state.onAbortState, true
}
func (b *backend) getOnCommitState(blkID ids.ID) (state.Diff, bool) {
state, ok := b.blkIDToState[blkID]
if !ok || state.onCommitState == nil {
return nil, false
}
return state.onCommitState, true
}
func (b *backend) GetBlock(blkID ids.ID) (block.Block, error) {
// See if the block is in memory.
if blk, ok := b.blkIDToState[blkID]; ok {
return blk.statelessBlock, nil
}
// The block isn't in memory. Check the database.
return b.state.GetStatelessBlock(blkID)
}
func (b *backend) LastAccepted() ids.ID {
return b.lastAccepted
}
func (b *backend) free(blkID ids.ID) {
delete(b.blkIDToState, blkID)
}
func (b *backend) getTimestamp(blkID ids.ID) time.Time {
// Check if the block is processing.
// If the block is processing, then we are guaranteed to have populated its
// timestamp in its state.
if blkState, ok := b.blkIDToState[blkID]; ok {
return blkState.timestamp
}
// The block isn't processing.
// According to the snowman.Block interface, the last accepted
// block is the only accepted block that must return a correct timestamp,
// so we just return the chain time.
return b.state.GetTimestamp()
}