-
Notifications
You must be signed in to change notification settings - Fork 672
/
context.go
140 lines (114 loc) · 3.77 KB
/
context.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snow
import (
"crypto"
"crypto/x509"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
)
type SubnetLookup interface {
SubnetID(chainID ids.ID) (ids.ID, error)
}
// ContextInitializable represents an object that can be initialized
// given a *Context object
type ContextInitializable interface {
// InitCtx initializes an object provided a *Context object
InitCtx(ctx *Context)
}
// Context is information about the current execution.
// [NetworkID] is the ID of the network this context exists within.
// [ChainID] is the ID of the chain this context exists within.
// [NodeID] is the ID of this node
type Context struct {
NetworkID uint32
SubnetID ids.ID
ChainID ids.ID
NodeID ids.NodeID
XChainID ids.ID
AVAXAssetID ids.ID
Log logging.Logger
Lock sync.RWMutex
Keystore keystore.BlockchainKeystore
SharedMemory atomic.SharedMemory
BCLookup ids.AliaserReader
SNLookup SubnetLookup
Metrics metrics.OptionalGatherer
// snowman++ attributes
ValidatorState validators.State // interface for P-Chain validators
StakingLeafSigner crypto.Signer // block signer
StakingCertLeaf *x509.Certificate // block certificate
}
// Expose gatherer interface for unit testing.
type Registerer interface {
prometheus.Registerer
prometheus.Gatherer
}
type ConsensusContext struct {
*Context
Registerer Registerer
// DecisionAcceptor is the callback that will be fired whenever a VM is
// notified that their object, either a block in snowman or a transaction
// in avalanche, was accepted.
DecisionAcceptor Acceptor
// ConsensusAcceptor is the callback that will be fired whenever a
// container, either a block in snowman or a vertex in avalanche, was
// accepted.
ConsensusAcceptor Acceptor
// Non-zero iff this chain bootstrapped.
state utils.AtomicInterface
// Non-zero iff this chain is executing transactions.
executing utils.AtomicBool
// Indicates this chain is available to only validators.
validatorOnly utils.AtomicBool
}
func (ctx *ConsensusContext) SetState(newState State) {
ctx.state.SetValue(newState)
}
func (ctx *ConsensusContext) GetState() State {
stateInf := ctx.state.GetValue()
return stateInf.(State)
}
// IsExecuting returns true iff this chain is still executing transactions.
func (ctx *ConsensusContext) IsExecuting() bool {
return ctx.executing.GetValue()
}
// Executing marks this chain as executing or not.
// Set to "true" if there's an ongoing transaction.
func (ctx *ConsensusContext) Executing(b bool) {
ctx.executing.SetValue(b)
}
// IsValidatorOnly returns true iff this chain is available only to validators
func (ctx *ConsensusContext) IsValidatorOnly() bool {
return ctx.validatorOnly.GetValue()
}
// SetValidatorOnly marks this chain as available only to validators
func (ctx *ConsensusContext) SetValidatorOnly() {
ctx.validatorOnly.SetValue(true)
}
func DefaultContextTest() *Context {
return &Context{
NetworkID: 0,
SubnetID: ids.Empty,
ChainID: ids.Empty,
NodeID: ids.EmptyNodeID,
Log: logging.NoLog{},
BCLookup: ids.NewAliaser(),
Metrics: metrics.NewOptionalGatherer(),
}
}
func DefaultConsensusContextTest() *ConsensusContext {
return &ConsensusContext{
Context: DefaultContextTest(),
Registerer: prometheus.NewRegistry(),
DecisionAcceptor: noOpAcceptor{},
ConsensusAcceptor: noOpAcceptor{},
}
}