Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #225 from radixdlt/feature/proof-endpoints
Browse files Browse the repository at this point in the history
Proof endpoints
  • Loading branch information
talekhinezh authored Apr 25, 2021
2 parents a32bea5 + 26b5eb4 commit 031ddb2
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,79 @@
import com.radixdlt.consensus.bft.PacemakerTimeout;
import com.radixdlt.consensus.epoch.EpochViewUpdate;
import com.radixdlt.counters.SystemCountersImpl;
import com.radixdlt.environment.EventProcessor;
import com.radixdlt.environment.EventProcessorOnRunner;
import com.radixdlt.environment.LocalEvents;
import com.radixdlt.environment.ProcessWithSystemInfoRunner;
import com.radixdlt.environment.Runners;
import com.radixdlt.epochs.EpochsLedgerUpdate;
import com.radixdlt.systeminfo.InMemorySystemInfo;
import com.radixdlt.systeminfo.SystemInfoRunner;
import com.radixdlt.consensus.liveness.EpochLocalTimeoutOccurrence;
import com.radixdlt.consensus.QuorumCertificate;
import com.radixdlt.consensus.epoch.EpochView;
import com.radixdlt.middleware2.InfoSupplier;
import com.radixdlt.counters.SystemCounters;
import com.radixdlt.properties.RuntimeProperties;

import org.radix.Radix;

/**
* Module which manages system info
*/
public class SystemInfoModule extends AbstractModule {
private static final int DEFAULT_VERTEX_BUFFER_SIZE = 16;

@Override
protected void configure() {
bind(SystemCounters.class).to(SystemCountersImpl.class).in(Scopes.SINGLETON);
bind(SystemInfoRunner.class).in(Scopes.SINGLETON);

bind(InMemorySystemInfo.class).in(Scopes.SINGLETON);
var eventBinder = Multibinder.newSetBinder(binder(), new TypeLiteral<Class<?>>() { }, LocalEvents.class)
.permitDuplicates();
eventBinder.addBinding().toInstance(EpochViewUpdate.class);
eventBinder.addBinding().toInstance(EpochLocalTimeoutOccurrence.class);
eventBinder.addBinding().toInstance(BFTCommittedUpdate.class);
eventBinder.addBinding().toInstance(BFTHighQCUpdate.class);
eventBinder.addBinding().toInstance(EpochsLedgerUpdate.class);
}

@ProvidesIntoSet
private EventProcessor<EpochViewUpdate> epochViewEventProcessor(InMemorySystemInfo inMemorySystemInfo) {
return v -> inMemorySystemInfo.processView(v.getEpochView());
private EventProcessorOnRunner<?> epochsLedgerUpdateProcessor(InMemorySystemInfo inMemorySystemInfo) {
return new EventProcessorOnRunner<>(
Runners.SYSTEM_INFO,
EpochsLedgerUpdate.class,
inMemorySystemInfo.ledgerUpdateEventProcessor()
);
}

@ProvidesIntoSet
private EventProcessor<EpochLocalTimeoutOccurrence> timeoutEventProcessor(InMemorySystemInfo inMemorySystemInfo) {
return inMemorySystemInfo::processTimeout;
private EventProcessorOnRunner<?> epochViewEventProcessor(InMemorySystemInfo inMemorySystemInfo) {
return new EventProcessorOnRunner<>(
Runners.SYSTEM_INFO,
EpochViewUpdate.class,
v -> inMemorySystemInfo.processView(v.getEpochView())
);
}

@ProvidesIntoSet
private EventProcessor<BFTCommittedUpdate> committedUpdateEventProcessor(InMemorySystemInfo inMemorySystemInfo) {
return inMemorySystemInfo.bftCommittedUpdateEventProcessor();
private EventProcessorOnRunner<?> timeoutEventProcessor(InMemorySystemInfo inMemorySystemInfo) {
return new EventProcessorOnRunner<>(
Runners.SYSTEM_INFO,
EpochLocalTimeoutOccurrence.class,
inMemorySystemInfo::processTimeout
);
}

@ProvidesIntoSet
@ProcessWithSystemInfoRunner
private EventProcessor<BFTHighQCUpdate> highQCProcessor(InMemorySystemInfo inMemorySystemInfo) {
return inMemorySystemInfo.bftHighQCEventProcessor();
private EventProcessorOnRunner<?> committedUpdateEventProcessor(InMemorySystemInfo inMemorySystemInfo) {
return new EventProcessorOnRunner<>(
Runners.SYSTEM_INFO,
BFTCommittedUpdate.class,
inMemorySystemInfo.bftCommittedUpdateEventProcessor()
);
}

@Provides
@Singleton
private InMemorySystemInfo inMemorySystemInfo(RuntimeProperties runtimeProperties) {
final int vertexBufferSize = runtimeProperties.get("api.debug.vertex_buffer_size", DEFAULT_VERTEX_BUFFER_SIZE);
return new InMemorySystemInfo(vertexBufferSize);
@ProvidesIntoSet
private EventProcessorOnRunner<?> highQCProcessor(InMemorySystemInfo inMemorySystemInfo) {
return new EventProcessorOnRunner<>(
Runners.SYSTEM_INFO,
BFTHighQCUpdate.class,
inMemorySystemInfo.bftHighQCEventProcessor()
);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableSet;
import com.radixdlt.client.ValidatorAddress;
import com.radixdlt.consensus.bft.BFTValidator;
import com.radixdlt.consensus.bft.BFTValidatorSet;
import com.radixdlt.consensus.bft.View;
Expand All @@ -30,6 +31,10 @@
import com.radixdlt.serialization.SerializerConstants;
import com.radixdlt.serialization.SerializerDummy;
import com.radixdlt.serialization.SerializerId2;
import com.radixdlt.utils.Bytes;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Objects;
import java.util.Optional;
import javax.annotation.concurrent.Immutable;
Expand Down Expand Up @@ -88,6 +93,29 @@ private LedgerHeader(
this.timestamp = timestamp;
}

public JSONObject asJSONObject() {
var json = new JSONObject()
.put("epoch", epoch)
.put("view", view.number())
.put("version", accumulatorState.getStateVersion())
.put("accumulator", Bytes.toHexString(accumulatorState.getAccumulatorHash().asBytes()))
.put("timestamp", timestamp);

if (nextValidators != null) {
var validators = new JSONArray();
for (var v : nextValidators) {
var validatorAddress = ValidatorAddress.of(v.getNode().getKey());
validators.put(new JSONObject()
.put("address", validatorAddress)
.put("stake", v.getPower())
);
}
json.put("nextValidators", validators);
}

return json;
}


public static LedgerHeader mocked() {
return new LedgerHeader(0, View.genesis(), new AccumulatorState(0, HashUtils.zero256()), 0, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import com.radixdlt.serialization.SerializerConstants;
import com.radixdlt.serialization.SerializerDummy;
import com.radixdlt.serialization.SerializerId2;
import com.radixdlt.utils.Bytes;
import org.json.JSONObject;

import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -72,6 +75,13 @@ public LedgerProof(
this.signatures = Objects.requireNonNull(signatures);
}

public JSONObject asJSON() {
return new JSONObject()
.put("opaque", Bytes.toHexString(opaque.asBytes()))
.put("header", ledgerHeader.asJSONObject())
.put("sigs", signatures.asJSON());
}

public static LedgerProof genesis(AccumulatorState accumulatorState, BFTValidatorSet nextValidators, long timestamp) {
LedgerHeader genesisLedgerHeader = LedgerHeader.genesis(accumulatorState, nextValidators, timestamp);
return new LedgerProof(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.radixdlt.client.ValidatorAddress;
import com.radixdlt.consensus.bft.BFTNode;
import com.radixdlt.crypto.exception.PublicKeyException;
import com.radixdlt.serialization.DsonOutput;
Expand All @@ -28,6 +29,8 @@
import com.radixdlt.utils.Bytes;
import com.radixdlt.utils.Pair;
import com.radixdlt.utils.UInt256;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -66,6 +69,19 @@ public static TimestampedECDSASignatures from(@JsonProperty("signatures") Map<St
return new TimestampedECDSASignatures(signaturesByNode);
}

public JSONArray asJSON() {
var json = new JSONArray();
nodeToTimestampedSignature.forEach((node, sig) -> {
var obj = new JSONObject()
.put("address", ValidatorAddress.of(node.getKey()))
.put("signature", sig.signature().toHexString())
.put("timestamp", sig.timestamp());
json.put(obj);
});

return json;
}

/**
* Returns a new empty instance.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public final class Runners {
public static final String APPLICATION = "application";
public static final String CHAOS = "chaos";
public static final String CONSENSUS = "consensus";
public static final String SYSTEM_INFO = "info";

private Runners() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.Duration;
import java.util.List;
Expand All @@ -43,7 +45,7 @@
* Executes chaos related events
*/
public final class ModuleRunnerImpl implements ModuleRunner {

private static final Logger logger = LogManager.getLogger();
private final Scheduler singleThreadScheduler;
private final ScheduledExecutorService executorService;
private final String threadName;
Expand Down Expand Up @@ -142,6 +144,8 @@ public void start() {
return;
}

logger.info("Starting Runner: {}", this.threadName);

final var disposables = this.subscriptions.stream()
.map(s -> s.subscribe(singleThreadScheduler))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ private RxEnvironment rxEnvironment(
);
}

@ProvidesIntoMap
@StringMapKey(Runners.SYSTEM_INFO)
@Singleton
public ModuleRunner systemInfoRunner(
@Self BFTNode self,
Set<EventProcessorOnRunner<?>> processors,
RxEnvironment rxEnvironment
) {
final var runnerName = Runners.SYSTEM_INFO;
final var builder = ModuleRunnerImpl.builder();
addProcessorsOnRunner(processors, rxEnvironment, runnerName, builder);
return builder.build("SystemInfo " + self);
}

@ProvidesIntoMap
@StringMapKey(Runners.CHAOS)
@Singleton
Expand Down Expand Up @@ -290,7 +304,10 @@ private void addProcessorsOnRunner(
.map(EventProcessorOnRunner::getEventClass)
.collect(Collectors.toSet());
eventClasses.forEach(eventClass ->
allProcessors.forEach(p -> addToBuilder(eventClass, rxEnvironment, p, builder))
allProcessors
.stream()
.filter(p -> p.getRunnerName().equals(runnerName))
.forEach(p -> addToBuilder(eventClass, rxEnvironment, p, builder))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void sendLedgerUpdate(LedgerUpdate ledgerUpdate) {
return new EpochChange(header, bftConfiguration);
});

EpochsLedgerUpdate epochsLedgerUpdate = new EpochsLedgerUpdate(ledgerUpdate, epochChangeOptional.orElse(null));
var epochsLedgerUpdate = new EpochsLedgerUpdate(ledgerUpdate, epochChangeOptional.orElse(null));
this.epochsLedgerUpdateSender.dispatch(epochsLedgerUpdate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,32 @@

package com.radixdlt.systeminfo;

import com.radixdlt.consensus.LedgerProof;
import com.radixdlt.consensus.QuorumCertificate;
import com.radixdlt.consensus.bft.BFTHighQCUpdate;
import com.radixdlt.consensus.liveness.EpochLocalTimeoutOccurrence;
import com.radixdlt.consensus.bft.BFTCommittedUpdate;
import com.radixdlt.consensus.bft.View;
import com.radixdlt.consensus.epoch.EpochView;
import com.radixdlt.environment.EventProcessor;
import com.radixdlt.epochs.EpochsLedgerUpdate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.atomic.AtomicReference;

/**
* Manages system information to be consumed by clients such as the api.
*/
public final class InMemorySystemInfo {
private static final Logger logger = LogManager.getLogger();
private final AtomicReference<EpochLocalTimeoutOccurrence> lastTimeout = new AtomicReference<>();
private final AtomicReference<EpochView> currentView = new AtomicReference<>(EpochView.of(0L, View.genesis()));
private final AtomicReference<QuorumCertificate> highQC = new AtomicReference<>();
private final AtomicReference<LedgerProof> ledgerProof = new AtomicReference<>();
private final AtomicReference<LedgerProof> epochsLedgerProof = new AtomicReference<>();

public InMemorySystemInfo(int vertexBufferSize) {
if (vertexBufferSize < 0) {
throw new IllegalArgumentException("vertexBufferSize must be >= 0 but was " + vertexBufferSize);
}
public InMemorySystemInfo() {
}

public void processTimeout(EpochLocalTimeoutOccurrence timeout) {
Expand All @@ -48,6 +53,13 @@ public void processView(EpochView epochView) {
currentView.set(epochView);
}

public EventProcessor<EpochsLedgerUpdate> ledgerUpdateEventProcessor() {
return update -> {
this.ledgerProof.set(update.getBase().getTail());
update.getEpochChange().ifPresent(e -> epochsLedgerProof.set(update.getBase().getTail()));
};
}

public EventProcessor<BFTHighQCUpdate> bftHighQCEventProcessor() {
return update -> this.highQC.set(update.getHighQC().highestQC());
}
Expand All @@ -58,6 +70,14 @@ public EventProcessor<BFTCommittedUpdate> bftCommittedUpdateEventProcessor() {
};
}

public LedgerProof getCurrentProof() {
return ledgerProof.get();
}

public LedgerProof getEpochProof() {
return epochsLedgerProof.get();
}

public EpochView getCurrentView() {
return this.currentView.get();
}
Expand Down
Loading

0 comments on commit 031ddb2

Please sign in to comment.