-
Notifications
You must be signed in to change notification settings - Fork 655
/
context.go
135 lines (113 loc) · 4.19 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snow
import (
"crypto"
"crypto/x509"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/api/keystore"
"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"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)
type EventDispatcher interface {
Issue(ctx *Context, containerID ids.ID, container []byte) error
// If the returned error is non-nil, the chain associated with [ctx] should shut
// down and not commit [container] or any other container to its database as accepted.
// Accept must be called before [containerID] is committed to the VM as accepted.
Accept(ctx *Context, containerID ids.ID, container []byte) error
Reject(ctx *Context, containerID ids.ID, container []byte) error
}
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.ShortID
XChainID ids.ID
AVAXAssetID ids.ID
Log logging.Logger
DecisionDispatcher EventDispatcher
ConsensusDispatcher EventDispatcher
Lock sync.RWMutex
Keystore keystore.BlockchainKeystore
SharedMemory atomic.SharedMemory
BCLookup ids.AliaserReader
SNLookup SubnetLookup
Namespace string
Metrics prometheus.Registerer
// Epoch management
EpochFirstTransition time.Time
EpochDuration time.Duration
Clock mockable.Clock
// Non-zero iff this chain bootstrapped.
bootstrapped utils.AtomicBool
// Indicates this chain is available to only validators.
validatorOnly utils.AtomicBool
// snowman++ attributes
ValidatorState validators.State // interface for P-Chain validators
StakingLeafSigner crypto.Signer // block signer
StakingCertLeaf *x509.Certificate // block certificate
}
// IsBootstrapped returns true iff this chain is done bootstrapping
func (ctx *Context) IsBootstrapped() bool {
return ctx.bootstrapped.GetValue()
}
// Bootstrapped marks this chain as done bootstrapping
func (ctx *Context) Bootstrapped() {
ctx.bootstrapped.SetValue(true)
}
// IsValidatorOnly returns true iff this chain is available only to validators
func (ctx *Context) IsValidatorOnly() bool {
return ctx.validatorOnly.GetValue()
}
// SetValidatorOnly marks this chain as available only to validators
func (ctx *Context) SetValidatorOnly() {
ctx.validatorOnly.SetValue(true)
}
// Epoch this context thinks it's in based on the wall clock time.
func (ctx *Context) Epoch() uint32 {
now := ctx.Clock.Time()
timeSinceFirstEpochTransition := now.Sub(ctx.EpochFirstTransition)
epochsSinceFirstTransition := timeSinceFirstEpochTransition / ctx.EpochDuration
currentEpoch := epochsSinceFirstTransition + 1
if currentEpoch < 0 {
return 0
}
return uint32(currentEpoch)
}
func DefaultContextTest() *Context {
return &Context{
NetworkID: 0,
SubnetID: ids.Empty,
ChainID: ids.Empty,
NodeID: ids.ShortEmpty,
Log: logging.NoLog{},
DecisionDispatcher: emptyEventDispatcher{},
ConsensusDispatcher: emptyEventDispatcher{},
BCLookup: ids.NewAliaser(),
Namespace: "",
Metrics: prometheus.NewRegistry(),
}
}
type emptyEventDispatcher struct{}
func (emptyEventDispatcher) Issue(*Context, ids.ID, []byte) error { return nil }
func (emptyEventDispatcher) Accept(*Context, ids.ID, []byte) error { return nil }
func (emptyEventDispatcher) Reject(*Context, ids.ID, []byte) error { return nil }