-
Notifications
You must be signed in to change notification settings - Fork 672
/
context.go
128 lines (107 loc) · 3.73 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snow
import (
"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/crypto/bls"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
)
// 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
PublicKey *bls.PublicKey
XChainID ids.ID
CChainID ids.ID
AVAXAssetID ids.ID
Log logging.Logger
Lock sync.RWMutex
Keystore keystore.BlockchainKeystore
SharedMemory atomic.SharedMemory
BCLookup ids.AliaserReader
Metrics metrics.OptionalGatherer
WarpSigner warp.Signer
// snowman++ attributes
ValidatorState validators.State // interface for P-Chain validators
// Chain-specific directory where arbitrary data can be written
ChainDataDir string
}
// Expose gatherer interface for unit testing.
type Registerer interface {
prometheus.Registerer
prometheus.Gatherer
}
type ConsensusContext struct {
*Context
// Registers all common and snowman consensus metrics. Unlike the avalanche
// consensus engine metrics, we do not prefix the name with the engine name,
// as snowman is used for all chains by default.
Registerer Registerer
// Only used to register Avalanche consensus metrics. Previously, all
// metrics were prefixed with "avalanche_{chainID}_". Now we add avalanche
// to the prefix, "avalanche_{chainID}_avalanche_", to differentiate
// consensus operations after the DAG linearization.
AvalancheRegisterer Registerer
// BlockAcceptor is the callback that will be fired whenever a VM is
// notified that their block was accepted.
BlockAcceptor Acceptor
// TxAcceptor is the callback that will be fired whenever a VM is notified
// that their transaction was accepted.
TxAcceptor Acceptor
// VertexAcceptor is the callback that will be fired whenever a vertex was
// accepted.
VertexAcceptor Acceptor
// State indicates the current state of this consensus instance.
State utils.Atomic[EngineState]
// True iff this chain is executing transactions as part of bootstrapping.
Executing utils.Atomic[bool]
// True iff this chain is currently state-syncing
StateSyncing utils.Atomic[bool]
}
func DefaultContextTest() *Context {
sk, err := bls.NewSecretKey()
if err != nil {
panic(err)
}
pk := bls.PublicFromSecretKey(sk)
return &Context{
NetworkID: 0,
SubnetID: ids.Empty,
ChainID: ids.Empty,
NodeID: ids.EmptyNodeID,
PublicKey: pk,
Log: logging.NoLog{},
BCLookup: ids.NewAliaser(),
Metrics: metrics.NewOptionalGatherer(),
ChainDataDir: "",
}
}
func DefaultConsensusContextTest() *ConsensusContext {
return &ConsensusContext{
Context: DefaultContextTest(),
Registerer: prometheus.NewRegistry(),
AvalancheRegisterer: prometheus.NewRegistry(),
BlockAcceptor: noOpAcceptor{},
TxAcceptor: noOpAcceptor{},
VertexAcceptor: noOpAcceptor{},
}
}