/
engine.go
176 lines (133 loc) · 4.62 KB
/
engine.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package core
import (
"math/big"
"time"
"github.com/olebedev/emitter"
"github.com/ellcrys/elld/config"
"github.com/ellcrys/elld/elldb"
"github.com/ellcrys/elld/types"
"github.com/ellcrys/elld/util"
"github.com/ellcrys/elld/util/cache"
"github.com/ellcrys/elld/util/queue"
host "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
)
// Engine represents node functionalities not provided by the
// protocol. This can include peer discovery, configuration,
// APIs etc.
type Engine interface {
// SetEventEmitter set the event emitter
// used to broadcast/receive events
SetEventEmitter(*emitter.Emitter)
// DB is the laoded client database
DB() elldb.DB
// GetTxPool returns the transaction pool
GetTxPool() types.TxPool
// StringID is the peer ID of engine's
// network host as a string
StringID() string
// ShortID is the short version the value
// return by StringID. It meant to be
// used for logging.
ShortID() string
// ID returns the peer ID of the engine's
// network knows
ID() peer.ID
// GetLastSeen returns the time the engine
// (or peer) was last seen
GetLastSeen() time.Time
// SetLastSeen sets the time the engine
// (or peer) was last seen
SetLastSeen(time.Time)
// CreatedAt returns the time this engine
// was first created.
CreatedAt() time.Time
// SetCreatedAt sets the time this engine
// was first created
SetCreatedAt(t time.Time)
// IsSame checks whether the engine has
// the same ID as another engine
IsSame(Engine) bool
// IsSameID is like IsSame but accepts a string
IsSameID(id string) bool
// IsHardcodedSeed indicates that the engine
// was not discovered through the gossip protocol
// but injected into the codebase
IsHardcodedSeed() bool
// GetAddress returns the listening address
// of the host network
GetAddress() util.NodeAddr
// Connected checks whether the engine is connected
// to the local node
Connected() bool
// GetBlockchain returns the blockchain instance
GetBlockchain() types.Blockchain
// SetBlockchain stores a reference to the
// blockchain instance
SetBlockchain(bchain types.Blockchain)
// ProdMode checks whether the engine
// is in production mode
ProdMode() bool
// TestMode checks whether the engine
// is in test mode
TestMode() bool
// IsInbound checks whether the engine (or peer) is
// considered an inbound connection to the local node
IsInbound() bool
// IsInbound checks whether the engine (or peer) is
// considered an outbound connection to the local node
SetInbound(v bool)
// HasStopped checks whether the peer has stopped
HasStopped() bool
// GetHost returns the engine's network host
GetHost() host.Host
// Gossip returns the gossip manager
Gossip() Gossip
// NewRemoteNode creates a node that will be used to
// represent a remote peer.
NewRemoteNode(address util.NodeAddr) Engine
// GetCfg returns the engine configuration
GetCfg() *config.EngineConfig
// GetEventEmitter gets the event emitter
GetEventEmitter() *emitter.Emitter
// GetHistory returns the general items cache
GetHistory() *cache.Cache
// SetSyncing sets the sync status
SetSyncing(syncing bool)
// UpdateSyncInfo updates the sync state
UpdateSyncInfo(bi *BestBlockInfo)
// GetBlockHashQueue returns the block hash queue
GetBlockHashQueue() *queue.Queue
// GetSyncStateInfo generates status and progress
// information about the current blockchain sync operation
GetSyncStateInfo() *SyncStateInfo
// AddToPeerStore adds the ID of the engine
// to the peerstore
AddToPeerStore(node Engine) Engine
// GetIntros returns the cache containing received intros
GetIntros() *cache.Cache
// AddTransaction validates and adds a
// transaction to the transaction pool.
AddTransaction(tx types.Transaction) error
// SetHardcodedState sets the hardcoded seed state
// of the engine.
SetHardcodedState(v bool)
// SetGossipManager sets the gossip manager
SetGossipManager(m Gossip)
}
// BestBlockInfo represent best block
// heard by the engine from other peers
type BestBlockInfo struct {
BestBlockHash util.Hash
BestBlockTotalDifficulty *big.Int
BestBlockNumber uint64
}
// SyncStateInfo describes the current state
// and progress of ongoing blockchain synchronization
type SyncStateInfo struct {
TargetTD *big.Int `json:"targetTotalDifficulty"`
TargetChainHeight uint64 `json:"targetChainHeight" msgpack:"targetChainHeight"`
CurrentTD *big.Int `json:"currentTotalDifficulty" msgpack:"currentTotalDifficulty"`
CurrentChainHeight uint64 `json:"currentChainHeight" msgpack:"currentChainHeight"`
ProgressPercent float64 `json:"progressPercent" msgpack:"progressPercent"`
}