-
Notifications
You must be signed in to change notification settings - Fork 646
/
backend.go
66 lines (56 loc) · 2.08 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"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
bootstrapped *utils.AtomicBool
}
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) GetBlock(blkID ids.ID) (blocks.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.
statelessBlk, _, err := b.state.GetStatelessBlock(blkID)
return statelessBlk, err
}
func (b *backend) LastAccepted() ids.ID {
return b.lastAccepted
}
func (b *backend) free(blkID ids.ID) {
delete(b.blkIDToState, blkID)
}