Progress towards validator signing attestations.
- Added a 'beacon_attester' RPC endpoint, so the BeaconNode can supply attestation data. - Renamed 'attestation_data' to just 'attestation' throughout (except where it is actually just the data structure).
This commit is contained in:
		
							parent
							
								
									2f49289c28
								
							
						
					
					
						commit
						4cdeb6abe5
					
				| @ -280,8 +280,8 @@ where | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
 |     /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
 | ||||||
|     pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> { |     pub fn produce_attestation(&self, shard: u64) -> Result<AttestationData, Error> { | ||||||
|         trace!("BeaconChain::produce_attestation_data: shard: {}", shard); |         trace!("BeaconChain::produce_attestation: shard: {}", shard); | ||||||
|         let source_epoch = self.state.read().current_justified_epoch; |         let source_epoch = self.state.read().current_justified_epoch; | ||||||
|         let source_root = *self.state.read().get_block_root( |         let source_root = *self.state.read().get_block_root( | ||||||
|             source_epoch.start_slot(self.spec.slots_per_epoch), |             source_epoch.start_slot(self.spec.slots_per_epoch), | ||||||
|  | |||||||
| @ -50,18 +50,18 @@ impl<T: ClientDB, U: SlotClock, F: ForkChoice> DirectBeaconNode<T, U, F> { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<T: ClientDB, U: SlotClock, F: ForkChoice> AttesterBeaconNode for DirectBeaconNode<T, U, F> { | impl<T: ClientDB, U: SlotClock, F: ForkChoice> AttesterBeaconNode for DirectBeaconNode<T, U, F> { | ||||||
|     fn produce_attestation_data( |     fn produce_attestation( | ||||||
|         &self, |         &self, | ||||||
|         _slot: Slot, |         _slot: Slot, | ||||||
|         shard: u64, |         shard: u64, | ||||||
|     ) -> Result<Option<AttestationData>, NodeError> { |     ) -> Result<Option<AttestationData>, NodeError> { | ||||||
|         match self.beacon_chain.produce_attestation_data(shard) { |         match self.beacon_chain.produce_attestation(shard) { | ||||||
|             Ok(attestation_data) => Ok(Some(attestation_data)), |             Ok(attestation_data) => Ok(Some(attestation_data)), | ||||||
|             Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))), |             Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn publish_attestation_data( |     fn publish_attestation( | ||||||
|         &self, |         &self, | ||||||
|         free_attestation: FreeAttestation, |         free_attestation: FreeAttestation, | ||||||
|     ) -> Result<AttestationPublishOutcome, NodeError> { |     ) -> Result<AttestationPublishOutcome, NodeError> { | ||||||
|  | |||||||
							
								
								
									
										61
									
								
								beacon_node/rpc/src/beacon_attester.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								beacon_node/rpc/src/beacon_attester.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,61 @@ | |||||||
|  | use futures::Future; | ||||||
|  | use grpcio::{RpcContext, UnarySink}; | ||||||
|  | use protos::services::{ | ||||||
|  |     Attestation as AttestationProto, ProduceAttestation, ProduceAttestationResponse, | ||||||
|  |     ProduceAttestationRequest, PublishAttestationResponse, PublishAttestationRequest, | ||||||
|  |     PublishAttestation | ||||||
|  | }; | ||||||
|  | use protos::services_grpc::BeaconBlockService; | ||||||
|  | use slog::Logger; | ||||||
|  | 
 | ||||||
|  | #[derive(Clone)] | ||||||
|  | pub struct AttestationServiceInstance { | ||||||
|  |     pub log: Logger, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl AttestationService for AttestationServiceInstance { | ||||||
|  |     /// Produce a `BeaconBlock` for signing by a validator.
 | ||||||
|  |     fn produce_attestation( | ||||||
|  |         &mut self, | ||||||
|  |         ctx: RpcContext, | ||||||
|  |         req: ProduceAttestationRequest, | ||||||
|  |         sink: UnarySink<ProduceAttestationResponse>, | ||||||
|  |     ) { | ||||||
|  |         println!("producing attestation at slot {}", req.get_slot()); | ||||||
|  | 
 | ||||||
|  |         // TODO: build a legit block.
 | ||||||
|  |         let mut attestation = AttestationProto::new(); | ||||||
|  |         attestation.set_slot(req.get_slot()); | ||||||
|  |         // TODO Set the shard to something legit.
 | ||||||
|  |         attestation.set_shard(0); | ||||||
|  |         attestation.set_block_root(b"cats".to_vec()); | ||||||
|  | 
 | ||||||
|  |         let mut resp = ProduceAttestationResponse::new(); | ||||||
|  |         resp.set_attestation_data(attestation); | ||||||
|  | 
 | ||||||
|  |         let f = sink | ||||||
|  |             .success(resp) | ||||||
|  |             .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); | ||||||
|  |         ctx.spawn(f) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Accept some fully-formed `BeaconBlock`, process and publish it.
 | ||||||
|  |     fn publish_attestation( | ||||||
|  |         &mut self, | ||||||
|  |         ctx: RpcContext, | ||||||
|  |         req: PublishAttestationRequest, | ||||||
|  |         sink: UnarySink<PublishAttestationResponse>, | ||||||
|  |     ) { | ||||||
|  |         println!("publishing attestation {:?}", req.get_block()); | ||||||
|  | 
 | ||||||
|  |         // TODO: actually process the block.
 | ||||||
|  |         let mut resp = PublishAttestationResponse::new(); | ||||||
|  | 
 | ||||||
|  |         resp.set_success(true); | ||||||
|  | 
 | ||||||
|  |         let f = sink | ||||||
|  |             .success(resp) | ||||||
|  |             .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); | ||||||
|  |         ctx.spawn(f) | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -94,7 +94,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> Attester<T, U, V, | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn produce_attestation(&mut self, slot: Slot, shard: u64) -> Result<PollOutcome, Error> { |     fn produce_attestation(&mut self, slot: Slot, shard: u64) -> Result<PollOutcome, Error> { | ||||||
|         let attestation_data = match self.beacon_node.produce_attestation_data(slot, shard)? { |         let attestation_data = match self.beacon_node.produce_attestation(slot, shard)? { | ||||||
|             Some(attestation_data) => attestation_data, |             Some(attestation_data) => attestation_data, | ||||||
|             None => return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(slot)), |             None => return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(slot)), | ||||||
|         }; |         }; | ||||||
| @ -120,7 +120,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> Attester<T, U, V, | |||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         self.beacon_node |         self.beacon_node | ||||||
|             .publish_attestation_data(free_attestation)?; |             .publish_attestation(free_attestation)?; | ||||||
|         Ok(PollOutcome::AttestationProduced(slot)) |         Ok(PollOutcome::AttestationProduced(slot)) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -26,7 +26,7 @@ impl SimulatedBeaconNode { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl BeaconNode for SimulatedBeaconNode { | impl BeaconNode for SimulatedBeaconNode { | ||||||
|     fn produce_attestation_data(&self, slot: Slot, shard: u64) -> ProduceResult { |     fn produce_attestation(&self, slot: Slot, shard: u64) -> ProduceResult { | ||||||
|         *self.produce_input.write().unwrap() = Some((slot, shard)); |         *self.produce_input.write().unwrap() = Some((slot, shard)); | ||||||
|         match *self.produce_result.read().unwrap() { |         match *self.produce_result.read().unwrap() { | ||||||
|             Some(ref r) => r.clone(), |             Some(ref r) => r.clone(), | ||||||
| @ -34,7 +34,7 @@ impl BeaconNode for SimulatedBeaconNode { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn publish_attestation_data(&self, free_attestation: FreeAttestation) -> PublishResult { |     fn publish_attestation(&self, free_attestation: FreeAttestation) -> PublishResult { | ||||||
|         *self.publish_input.write().unwrap() = Some(free_attestation.clone()); |         *self.publish_input.write().unwrap() = Some(free_attestation.clone()); | ||||||
|         match *self.publish_result.read().unwrap() { |         match *self.publish_result.read().unwrap() { | ||||||
|             Some(ref r) => r.clone(), |             Some(ref r) => r.clone(), | ||||||
|  | |||||||
| @ -14,13 +14,13 @@ pub enum PublishOutcome { | |||||||
| 
 | 
 | ||||||
| /// Defines the methods required to produce and publish blocks on a Beacon Node.
 | /// Defines the methods required to produce and publish blocks on a Beacon Node.
 | ||||||
| pub trait BeaconNode: Send + Sync { | pub trait BeaconNode: Send + Sync { | ||||||
|     fn produce_attestation_data( |     fn produce_attestation( | ||||||
|         &self, |         &self, | ||||||
|         slot: Slot, |         slot: Slot, | ||||||
|         shard: u64, |         shard: u64, | ||||||
|     ) -> Result<Option<AttestationData>, BeaconNodeError>; |     ) -> Result<Option<AttestationData>, BeaconNodeError>; | ||||||
| 
 | 
 | ||||||
|     fn publish_attestation_data( |     fn publish_attestation( | ||||||
|         &self, |         &self, | ||||||
|         free_attestation: FreeAttestation, |         free_attestation: FreeAttestation, | ||||||
|     ) -> Result<PublishOutcome, BeaconNodeError>; |     ) -> Result<PublishOutcome, BeaconNodeError>; | ||||||
|  | |||||||
| @ -33,8 +33,8 @@ service ValidatorService { | |||||||
| 
 | 
 | ||||||
| /// Service that handles validator attestations | /// Service that handles validator attestations | ||||||
| service AttestationService { | service AttestationService { | ||||||
| 	rpc ProduceAttestationData (ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); | 	rpc ProduceAttestation(ProduceAttestationRequest) returns (ProduceAttestationResponse); | ||||||
| 	rpc PublishAttestationData (PublishAttestationDataRequest) returns (PublishAttestationDataResponse); | 	rpc PublishAttestation(PublishAttestationRequest) returns (PublishAttestationResponse); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
| @ -138,20 +138,20 @@ message ProposeBlockSlotResponse { | |||||||
|  * Attestation Service Messages |  * Attestation Service Messages | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| message ProduceAttestationDataRequest { | message ProduceAttestationRequest { | ||||||
| 	uint64 slot = 1; | 	uint64 slot = 1; | ||||||
| 	uint64 shard = 2; | 	uint64 shard = 2; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| message ProduceAttestationDataResponse { | message ProduceAttestationResponse { | ||||||
| 	AttestationData attestation_data = 1; | 	Attestation attestation_data = 1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| message PublishAttestationDataRequest { | message PublishAttestationRequest { | ||||||
| 	FreeAttestation free_attestation = 1; | 	FreeAttestation free_attestation = 1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| message PublishAttestationDataResponse { | message PublishAttestationResponse { | ||||||
| 	bool success = 1; | 	bool success = 1; | ||||||
| 	bytes msg = 2; | 	bytes msg = 2; | ||||||
| } | } | ||||||
| @ -162,7 +162,7 @@ message Crosslink { | |||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| message AttestationData { | message Attestation { | ||||||
| 	uint64 slot = 1; | 	uint64 slot = 1; | ||||||
| 	uint64 shard = 2; | 	uint64 shard = 2; | ||||||
| 	bytes beacon_block_root = 3; | 	bytes beacon_block_root = 3; | ||||||
| @ -175,7 +175,7 @@ message AttestationData { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| message FreeAttestation { | message FreeAttestation { | ||||||
| 	AttestationData attestation_data = 1; | 	Attestation attestation_data = 1; | ||||||
| 	bytes signature = 2; | 	bytes signature = 2; | ||||||
| 	uint64 validator_index = 3; | 	uint64 validator_index = 3; | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ use protos::services_grpc::AttestationServiceClient; | |||||||
| use std::sync::Arc; | use std::sync::Arc; | ||||||
| 
 | 
 | ||||||
| use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; | use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; | ||||||
| use protos::services::ProduceAttestationDataRequest; | use protos::services::ProduceAttestationRequest; | ||||||
| use types::{AttestationData, FreeAttestation, Slot}; | use types::{AttestationData, FreeAttestation, Slot}; | ||||||
| 
 | 
 | ||||||
| pub struct AttestationGrpcClient { | pub struct AttestationGrpcClient { | ||||||
| @ -16,25 +16,25 @@ impl AttestationGrpcClient { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl BeaconNode for AttestationGrpcClient { | impl BeaconNode for AttestationGrpcClient { | ||||||
|     fn produce_attestation_data( |     fn produce_attestation( | ||||||
|         &self, |         &self, | ||||||
|         slot: Slot, |         slot: Slot, | ||||||
|         shard: u64, |         shard: u64, | ||||||
|     ) -> Result<Option<AttestationData>, BeaconNodeError> { |     ) -> Result<Option<AttestationData>, BeaconNodeError> { | ||||||
|         let mut req = ProduceAttestationDataRequest::new(); |         let mut req = ProduceAttestationRequest::new(); | ||||||
|         req.set_slot(slot.as_u64()); |         req.set_slot(slot.as_u64()); | ||||||
|         req.set_shard(shard); |         req.set_shard(shard); | ||||||
| 
 | 
 | ||||||
|         let reply = self |         let reply = self | ||||||
|             .client |             .client | ||||||
|             .produce_attestation_data(&req) |             .produce_attestation(&req) | ||||||
|             .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; |             .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; | ||||||
| 
 | 
 | ||||||
|         // TODO: return correct AttestationData
 |         // TODO: return correct Attestation
 | ||||||
|         Err(BeaconNodeError::DecodeFailure) |         Err(BeaconNodeError::DecodeFailure) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn publish_attestation_data( |     fn publish_attestation( | ||||||
|         &self, |         &self, | ||||||
|         free_attestation: FreeAttestation, |         free_attestation: FreeAttestation, | ||||||
|     ) -> Result<PublishOutcome, BeaconNodeError> { |     ) -> Result<PublishOutcome, BeaconNodeError> { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user