This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Events Plugin - Add initial "NewBlockPropagated" event message (#1463)
* It returns for any block we would broadcast to other peers, when we would broadcast them. * It returns a JSON String containing hash, 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.
- Loading branch information
Danno Ferrin
authored
May 20, 2019
1 parent
0ef1d8f
commit 9ab506b
Showing
17 changed files
with
494 additions
and
70 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...rc/test/java/tech/pegasys/pantheon/tests/acceptance/plugins/PantheonEventsPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.plugins; | ||
|
||
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; | ||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Stream; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class PantheonEventsPluginTest extends AcceptanceTestBase { | ||
private PantheonNode pluginNode; | ||
private PantheonNode minerNode; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
minerNode = pantheon.createMinerNode("minerNode"); | ||
pluginNode = | ||
pantheon.createPluginsNode( | ||
"node1", Collections.singletonList("testPlugin"), Collections.emptyList()); | ||
cluster.start(pluginNode, minerNode); | ||
} | ||
|
||
@Test | ||
public void blockIsAnnounded() { | ||
waitForFile(pluginNode.homeDirectory().resolve("plugins/newBlock.2")); | ||
} | ||
|
||
private void waitForFile(final Path path) { | ||
final File file = path.toFile(); | ||
Awaitility.waitAtMost(30, TimeUnit.SECONDS) | ||
.until( | ||
() -> { | ||
if (file.exists()) { | ||
try (final Stream<String> s = Files.lines(path)) { | ||
return s.count() > 0; | ||
} | ||
} else { | ||
return false; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
pantheon/src/main/java/tech/pegasys/pantheon/services/PantheonEventsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.services; | ||
|
||
import tech.pegasys.pantheon.ethereum.core.Block; | ||
import tech.pegasys.pantheon.ethereum.eth.sync.BlockBroadcaster; | ||
import tech.pegasys.pantheon.plugins.services.PantheonEvents; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.vertx.core.json.Json; | ||
|
||
public class PantheonEventsImpl implements PantheonEvents { | ||
private final BlockBroadcaster blockBroadcaster; | ||
|
||
public PantheonEventsImpl(final BlockBroadcaster blockBroadcaster) { | ||
this.blockBroadcaster = blockBroadcaster; | ||
} | ||
|
||
@Override | ||
public Object addNewBlockPropagatedListener(final NewBlockPropagatedListener listener) { | ||
return blockBroadcaster.subscribePropagateNewBlocks( | ||
block -> dispatchNewBlockPropagatedMessage(block, listener)); | ||
} | ||
|
||
@Override | ||
public void removeNewBlockPropagatedListener(final Object listenerIdentifier) { | ||
if (listenerIdentifier instanceof Long) { | ||
blockBroadcaster.unsubscribePropagateNewBlocks((Long) listenerIdentifier); | ||
} | ||
} | ||
|
||
private void dispatchNewBlockPropagatedMessage( | ||
final Block block, final NewBlockPropagatedListener listener) { | ||
final ImmutableMap<Object, Object> result = | ||
new ImmutableMap.Builder<>() | ||
.put("type", "NewBlock") | ||
.put("blockHash", block.getHash().toString()) | ||
.put("blockNumber", block.getHeader().getNumber()) | ||
.put("timestamp", block.getHeader().getTimestamp()) | ||
.build(); | ||
listener.newBlockPropagated(Json.encode(result)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
pantheon/src/main/java/tech/pegasys/pantheon/services/PicoCLIOptionsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.services; | ||
|
||
import tech.pegasys.pantheon.plugins.services.PicoCLIOptions; | ||
|
||
import picocli.CommandLine; | ||
|
||
public class PicoCLIOptionsImpl implements PicoCLIOptions { | ||
|
||
private final CommandLine commandLine; | ||
|
||
public PicoCLIOptionsImpl(final CommandLine commandLine) { | ||
this.commandLine = commandLine; | ||
} | ||
|
||
@Override | ||
public void addPicoCLIOptions(final String namespace, final Object optionObject) { | ||
commandLine.addMixin("Plugin " + namespace, optionObject); | ||
} | ||
} |
Oops, something went wrong.