-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
External signer Altair block (#4282)
* External signer Altair support -- send milestone and Altair specific block to external signer Signed-off-by: Usman Saleem <usman@usmans.info>
- Loading branch information
1 parent
6c859fa
commit 2737b80
Showing
9 changed files
with
207 additions
and
12 deletions.
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
...ator/client/src/main/java/tech/pegasys/teku/validator/client/signer/BlockRequestBody.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,42 @@ | ||
/* | ||
* Copyright 2020 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.teku.validator.client.signer; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import tech.pegasys.teku.api.schema.BeaconBlock; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
|
||
public class BlockRequestBody { | ||
private final SpecMilestone version; | ||
private final BeaconBlock beaconBlock; | ||
|
||
@JsonCreator | ||
public BlockRequestBody( | ||
@JsonProperty("version") final SpecMilestone version, | ||
@JsonProperty("block") final BeaconBlock beaconBlock) { | ||
this.version = version; | ||
this.beaconBlock = beaconBlock; | ||
} | ||
|
||
@JsonProperty("version") | ||
public SpecMilestone getVersion() { | ||
return version; | ||
} | ||
|
||
@JsonProperty("block") | ||
public BeaconBlock getBeaconBlock() { | ||
return beaconBlock; | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
...in/java/tech/pegasys/teku/validator/client/signer/ExternalSignerBlockRequestProvider.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,60 @@ | ||
/* | ||
* Copyright 2021 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.teku.validator.client.signer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import tech.pegasys.teku.api.SchemaObjectProvider; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock; | ||
|
||
public class ExternalSignerBlockRequestProvider { | ||
private final Spec spec; | ||
private final SchemaObjectProvider schemaObjectProvider; | ||
private final BeaconBlock block; | ||
private final SignType signType; | ||
|
||
public ExternalSignerBlockRequestProvider(final Spec spec, final BeaconBlock block) { | ||
this.spec = spec; | ||
this.block = block; | ||
schemaObjectProvider = new SchemaObjectProvider(spec); | ||
// backward compatible with phase 0 | ||
if (spec.atSlot(block.getSlot()).getMilestone().equals(SpecMilestone.PHASE0)) { | ||
signType = SignType.BLOCK; | ||
} else { | ||
signType = SignType.BLOCK_V2; | ||
} | ||
} | ||
|
||
public Map<String, Object> getBlockMetadata(final Map<String, Object> additionalEntries) { | ||
final Map<String, Object> metadata = new HashMap<>(additionalEntries); | ||
|
||
final tech.pegasys.teku.api.schema.BeaconBlock beaconBlock = | ||
schemaObjectProvider.getBeaconBlock(block); | ||
if (signType == SignType.BLOCK) { | ||
metadata.put("block", beaconBlock); // backward compatible with phase0 | ||
} else { | ||
metadata.put( | ||
"beacon_block", | ||
new BlockRequestBody(spec.atSlot(block.getSlot()).getMilestone(), beaconBlock)); | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
public SignType getSignType() { | ||
return signType; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...ava/tech/pegasys/teku/validator/client/signer/ExternalSignerBlockRequestProviderTest.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,57 @@ | ||
/* | ||
* Copyright 2021 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.teku.validator.client.signer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.TestSpecFactory; | ||
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock; | ||
import tech.pegasys.teku.spec.util.DataStructureUtil; | ||
|
||
class ExternalSignerBlockRequestProviderTest { | ||
|
||
@Test | ||
void phase0BlockGeneratesCorrectSignTypeAndMetadata() { | ||
final Spec spec = TestSpecFactory.createMinimalPhase0(); | ||
final BeaconBlock block = new DataStructureUtil(spec).randomBeaconBlock(10); | ||
|
||
final ExternalSignerBlockRequestProvider externalSignerBlockRequestProvider = | ||
new ExternalSignerBlockRequestProvider(spec, block); | ||
final SignType signType = externalSignerBlockRequestProvider.getSignType(); | ||
final Map<String, Object> blockMetadata = | ||
externalSignerBlockRequestProvider.getBlockMetadata(Map.of()); | ||
|
||
assertThat(signType).isEqualTo(SignType.BLOCK); | ||
assertThat(blockMetadata).containsKey("block"); | ||
} | ||
|
||
@Test | ||
void altairBlockGeneratesCorrectSignTypeAndMetadata() { | ||
final Spec spec = TestSpecFactory.createMinimalAltair(); | ||
final BeaconBlock block = new DataStructureUtil(spec).randomBeaconBlock(10); | ||
|
||
final ExternalSignerBlockRequestProvider externalSignerBlockRequestProvider = | ||
new ExternalSignerBlockRequestProvider(spec, block); | ||
final SignType signType = externalSignerBlockRequestProvider.getSignType(); | ||
final Map<String, Object> blockMetadata = | ||
externalSignerBlockRequestProvider.getBlockMetadata(Map.of()); | ||
|
||
assertThat(signType).isEqualTo(SignType.BLOCK_V2); | ||
assertThat(blockMetadata).containsKey("beacon_block"); | ||
assertThat(blockMetadata.get("beacon_block")).isExactlyInstanceOf(BlockRequestBody.class); | ||
} | ||
} |