-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BeaconChainImpl.java
223 lines (176 loc) · 7.79 KB
/
BeaconChainImpl.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.sharding.processing;
import org.ethereum.core.Block;
import org.ethereum.db.DbFlushManager;
import org.ethereum.sharding.processing.consensus.GenesisTransition;
import org.ethereum.sharding.pubsub.Event;
import org.ethereum.sharding.pubsub.Publisher;
import org.ethereum.sharding.processing.consensus.ScoreFunction;
import org.ethereum.sharding.processing.consensus.StateTransition;
import org.ethereum.sharding.processing.state.BeaconState;
import org.ethereum.sharding.processing.state.StateRepository;
import org.ethereum.sharding.processing.validation.BeaconValidator;
import org.ethereum.sharding.processing.validation.StateValidator;
import org.ethereum.sharding.processing.db.BeaconStore;
import org.ethereum.sharding.domain.Beacon;
import org.ethereum.sharding.domain.BeaconGenesis;
import org.ethereum.sharding.processing.validation.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigInteger;
import static org.ethereum.sharding.pubsub.Events.onBeaconBlock;
import static org.ethereum.sharding.pubsub.Events.onBeaconChainLoaded;
import static org.ethereum.sharding.pubsub.Events.onBeaconChainSynced;
/**
* This is default and likely the only implementation of {@link BeaconChain}
*
* @author Mikhail Kalinin
* @since 14.08.2018
*/
public class BeaconChainImpl implements BeaconChain {
private static final Logger logger = LoggerFactory.getLogger("beacon");
DbFlushManager beaconDbFlusher;
BeaconStore store;
StateTransition<BeaconState> transitionFunction;
StateTransition<BeaconState> genesisStateTransition;
BeaconValidator beaconValidator;
StateValidator stateValidator;
StateRepository repository;
ScoreFunction scoreFunction;
Publisher publisher;
private ChainHead canonicalHead;
public BeaconChainImpl(DbFlushManager beaconDbFlusher, BeaconStore store,
StateTransition<BeaconState> transitionFunction, StateRepository repository,
BeaconValidator beaconValidator, StateValidator stateValidator,
ScoreFunction scoreFunction, StateTransition<BeaconState> genesisStateTransition) {
this.beaconDbFlusher = beaconDbFlusher;
this.store = store;
this.transitionFunction = transitionFunction;
this.repository = repository;
this.beaconValidator = beaconValidator;
this.stateValidator = stateValidator;
this.scoreFunction = scoreFunction;
this.genesisStateTransition = genesisStateTransition;
}
@Override
public void init() {
logger.info("Load chain with genesis: {}", BeaconGenesis.instance());
if (store.getCanonicalHead() == null)
insertGenesis();
canonicalHead = new ChainHead(store.getCanonicalHead(), store.getCanonicalHeadScore(),
repository.get(store.getCanonicalHead().getStateHash()));
publish(onBeaconChainLoaded(canonicalHead.block));
publish(onBeaconChainSynced(canonicalHead.block, canonicalHead.state));
logger.info("Chain loaded with head: {}", canonicalHead.block);
}
void insertGenesis() {
BeaconGenesis genesis = BeaconGenesis.instance();
BeaconState genesisState = genesisStateTransition.applyBlock(genesis, repository.getEmpty());
repository.insert(genesisState);
genesis.setStateHash(genesisState.getHash());
store.save(genesis, genesis.getScore(), true);
repository.commit();
beaconDbFlusher.flushSync();
}
@Override
public Beacon getCanonicalHead() {
return canonicalHead.block;
}
@Override
public synchronized ProcessingResult insert(Beacon block) {
ValidationResult vRes;
if ((vRes = beaconValidator.validateAndLog(block)) != ValidationResult.Success)
return ProcessingResult.fromValidation(vRes);
// apply state transition
Beacon parent = pullParent(block);
BeaconState currentState = pullState(parent);
BeaconState newState = transitionFunction.applyBlock(block, currentState);
if ((vRes = stateValidator.validateAndLog(block, newState)) != ValidationResult.Success)
return ProcessingResult.fromValidation(vRes);
// calculate block and chain score
BigInteger blockScore = scoreFunction.apply(block, newState);
BigInteger chainScore = store.getChainScore(parent.getHash()).add(blockScore);
ChainHead newHead = new ChainHead(block, chainScore, newState);
beaconDbFlusher.commit(() -> {
// save state
repository.insert(newHead.state);
repository.commit();
// store block
store.save(newHead.block, newHead.score, canonicalHead.isParentOf(newHead));
// do reorg if canonical chain is beaten
if (canonicalHead.shouldReorgTo(newHead)) {
store.reorgTo(newHead.block);
}
// update head
if (canonicalHead.isParentOf(newHead) || canonicalHead.shouldReorgTo(newHead)) {
canonicalHead = newHead;
}
});
ProcessingResult res = canonicalHead.equals(newHead) ? ProcessingResult.Best : ProcessingResult.NotBest;
// publish beacon block event
publish(onBeaconBlock(block, newState, res == ProcessingResult.Best));
logger.info("Process block {}, status: {}", block.toString(), res);
return res;
}
@Override
public void setBestBlock(Block block) {
((GenesisTransition) genesisStateTransition).withMainChainRef(block.getHash());
}
private Beacon pullParent(Beacon block) {
if (canonicalHead.block.isParentOf(block))
return canonicalHead.block;
return store.getByHash(block.getParentHash());
}
private BeaconState pullState(Beacon block) {
if (canonicalHead.block.equals(block))
return canonicalHead.state;
return repository.get(block.getStateHash());
}
static class ChainHead {
final Beacon block;
final BigInteger score;
final BeaconState state;
public ChainHead(Beacon block, BigInteger score, BeaconState state) {
this.block = block;
this.score = score;
this.state = state;
}
public boolean isParentOf(ChainHead other) {
return this.block.isParentOf(other.block);
}
public boolean shouldReorgTo(ChainHead other) {
return !this.isParentOf(other) && this.score.compareTo(other.score) < 0;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof ChainHead)) return false;
return this.block.equals(((ChainHead) other).block);
}
}
void publish(Event e) {
if (publisher != null)
publisher.publish(e);
}
@Autowired
public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
}