Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Events Plugin - Add initial "NewBlock" event message #1463

Merged
merged 7 commits into from
May 20, 2019

Conversation

shemnon
Copy link
Contributor

@shemnon shemnon commented May 17, 2019

PR description

  • It returns for any block we would broadcast to other peers, when
    we would broadcast them.
  • It returns a JSON String containing hask, number, and timestamp
  • This event data is not set in stone, it may change in type or content.
  • Acceptance tests and unit tests got a re-work away from the assumption
    that there is only one plugin type.

Fixed Issue(s)

shemnon added 3 commits May 17, 2019 16:35
* It returns for any block we would broadcast to other peers, when
  we would broadcast them.
* It returns a JSON String containing hask, number, and timestamp
* This event data is not set in stone, it may change in type or content.
* Acceptance tests and unit tests got a re-work away from the assumption
  that there is only one plugin type.
make picocli plugin not listen to events.
public void propagate(final Block block, final UInt256 totalDifficulty) {
newBlockSubscribers.forEach(listener -> listener.accept(block));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to add this functionality here. Blockchain.observeBlockAdded already supports observation of new block events.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This event needs to be fired between when we validate the header and when we validate the entire block.

The stack for this code is in part

Breakpoint reached
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockBroadcaster.propagate(BlockBroadcaster.java:46)
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockPropagationManager.broadcastBlock(BlockPropagationManager.java:248)
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockPropagationManager.lambda$validateAndProcessPendingBlock$6(BlockPropagationManager.java:306)
	  at ...

And the stack for using the Blockchain.observeBlockAdded event is

...
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockPropagationManager.runImportTask(BlockPropagationManager.java:323)
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockPropagationManager.validateAndProcessPendingBlock(BlockPropagationManager.java:307)
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockPropagationManager.lambda$importOrSavePendingBlock$5(BlockPropagationManager.java:297)
	  at tech.pegasys.pantheon.ethereum.eth.sync.BlockPropagationManager$$Lambda$1088.740466537.get(Unknown Source:-1)
...

Like 307 for BlockPropagationManager runs the full block validation, which is later than the requirements we have for when this information should be pushed to the events plugin. We don't want full block validation for this event just header (e.g. proof of work) validation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense. I think we just need to change the naming to make it clear what this event is. In the future if we handle NEW_BLOCK_HASHES / NEW_BLOCK messages differently, we may want to expose more events to get all of the new blocks on the network. Most clients don't propagate out NEW_BLOCK messages in response to receiving NEW_BLOCK_HASHES from the network, for example.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class BlockBroadcaster {
private static final Logger LOG = LogManager.getLogger();

private final EthContext ethContext;
private final Subscribers<Consumer<Block>> newBlockSubscribers = new Subscribers<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final Subscribers<Consumer<Block>> newBlockSubscribers = new Subscribers<>();
private final Subscribers<Consumer<Block>> blockPropagatedSubscribers = new Subscribers<>();

}

@Override
public Object addBlockAddedListener(final Consumer<String> blockJSONListener) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Object addBlockAddedListener(final Consumer<String> blockJSONListener) {
public Object observeNewBlockPropagated(final Consumer<String> blockJSONListener) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listener is more prevalent in our code base than observer. So I added the Propagated but kept the Listener.

}

@Override
public void removeBlockAddedObserver(final Object listenerIdentifier) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void removeBlockAddedObserver(final Object listenerIdentifier) {
public void removeNewBlockPropagatedObserver(final Object listenerIdentifier) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listener is more prevalent in our code base than observer. So I added the Propagated but kept the Listener.

}
}

private void dispatchNewBlockMessage(final Block block, final Consumer<String> callback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private void dispatchNewBlockMessage(final Block block, final Consumer<String> callback) {
private void dispatchNewBlockPropagatedMessage(final Block block, final Consumer<String> callback) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listener is more prevalent in our code base than observer. So I added the Propagated but kept the Listener.

public void propagate(final Block block, final UInt256 totalDifficulty) {
newBlockSubscribers.forEach(listener -> listener.accept(block));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense. I think we just need to change the naming to make it clear what this event is. In the future if we handle NEW_BLOCK_HASHES / NEW_BLOCK messages differently, we may want to expose more events to get all of the new blocks on the network. Most clients don't propagate out NEW_BLOCK messages in response to receiving NEW_BLOCK_HASHES from the network, for example.

}

@Override
public Object addBlockAddedListener(final Consumer<String> blockJSONListener) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) Consider adding a functional interface in place of Consumer<String>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@mbaxter mbaxter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending the typo fix

}

@Override
public Object addNewBlockPropagatedLister(final NewBlockPropagatedListener listener) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Object addNewBlockPropagatedLister(final NewBlockPropagatedListener listener) {
public Object addNewBlockPropagatedListener(final NewBlockPropagatedListener listener) {

Typo

build.gradle Outdated
@@ -105,7 +105,7 @@ allprojects {
java {
// This path needs to be relative to each project
target fileTree('.') {
include '**/*.java'
include '**/src/*/java/**/*.java'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are these changes for??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meant for another PR. Spotless was catching intellij generated files from .../out/classes/.... By limiting src to having to live in src directories generated and transient java files don't get style checked.

@shemnon shemnon merged commit 9ab506b into PegaSysEng:master May 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants