From b91db85c2352fe1dabf04703d4d217e681b9cbc2 Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Fri, 26 Mar 2021 03:36:43 -0700 Subject: [PATCH] [sync] add commit signature in peer registeration and new block push --- api/service/legacysync/downloader/client.go | 2 +- .../downloader/proto/downloader.pb.go | 85 +++++++++++-------- .../downloader/proto/downloader.proto | 1 + api/service/legacysync/syncing.go | 2 +- node/node.go | 2 + node/node_syncing.go | 30 +++++-- 6 files changed, 78 insertions(+), 44 deletions(-) diff --git a/api/service/legacysync/downloader/client.go b/api/service/legacysync/downloader/client.go index ac03e2130..5cb786ac2 100644 --- a/api/service/legacysync/downloader/client.go +++ b/api/service/legacysync/downloader/client.go @@ -113,7 +113,7 @@ func (client *Client) GetBlocksAndSigs(hashes [][]byte) *pb.DownloaderResponse { func (client *Client) Register(hash []byte, ip, port string) *pb.DownloaderResponse { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_REGISTER} + request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_REGISTER, RegisterWithSig: true} request.PeerHash = make([]byte, len(hash)) copy(request.PeerHash, hash) request.Ip = ip diff --git a/api/service/legacysync/downloader/proto/downloader.pb.go b/api/service/legacysync/downloader/proto/downloader.pb.go index fd947f6dd..0185c64db 100644 --- a/api/service/legacysync/downloader/proto/downloader.pb.go +++ b/api/service/legacysync/downloader/proto/downloader.pb.go @@ -154,12 +154,13 @@ type DownloaderRequest struct { // Request type. Type DownloaderRequest_RequestType `protobuf:"varint,1,opt,name=type,proto3,enum=downloader.DownloaderRequest_RequestType" json:"type,omitempty"` // The hashes of the blocks we want to download. - Hashes [][]byte `protobuf:"bytes,2,rep,name=hashes,proto3" json:"hashes,omitempty"` - PeerHash []byte `protobuf:"bytes,3,opt,name=peerHash,proto3" json:"peerHash,omitempty"` - BlockHash []byte `protobuf:"bytes,4,opt,name=blockHash,proto3" json:"blockHash,omitempty"` - Ip string `protobuf:"bytes,5,opt,name=ip,proto3" json:"ip,omitempty"` - Port string `protobuf:"bytes,6,opt,name=port,proto3" json:"port,omitempty"` - Size uint32 `protobuf:"varint,7,opt,name=size,proto3" json:"size,omitempty"` + Hashes [][]byte `protobuf:"bytes,2,rep,name=hashes,proto3" json:"hashes,omitempty"` + PeerHash []byte `protobuf:"bytes,3,opt,name=peerHash,proto3" json:"peerHash,omitempty"` + BlockHash []byte `protobuf:"bytes,4,opt,name=blockHash,proto3" json:"blockHash,omitempty"` + Ip string `protobuf:"bytes,5,opt,name=ip,proto3" json:"ip,omitempty"` + Port string `protobuf:"bytes,6,opt,name=port,proto3" json:"port,omitempty"` + Size uint32 `protobuf:"varint,7,opt,name=size,proto3" json:"size,omitempty"` + RegisterWithSig bool `protobuf:"varint,8,opt,name=registerWithSig,proto3" json:"registerWithSig,omitempty"` // Expect to have NEWBLOCK response with signature } func (x *DownloaderRequest) Reset() { @@ -243,6 +244,13 @@ func (x *DownloaderRequest) GetSize() uint32 { return 0 } +func (x *DownloaderRequest) GetRegisterWithSig() bool { + if x != nil { + return x.RegisterWithSig + } + return false +} + // DownloaderResponse is the generic response of DownloaderRequest. type DownloaderResponse struct { state protoimpl.MessageState @@ -313,8 +321,8 @@ var File_downloader_proto protoreflect.FileDescriptor var file_downloader_proto_rawDesc = []byte{ 0x0a, 0x10, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x0a, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x22, 0xf8, - 0x02, 0x0a, 0x11, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, + 0x74, 0x6f, 0x12, 0x0a, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x22, 0xa2, + 0x03, 0x0a, 0x11, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x3d, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x29, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, @@ -327,36 +335,39 @@ var file_downloader_proto_rawDesc = []byte{ 0x6b, 0x48, 0x61, 0x73, 0x68, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a, - 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x22, 0x99, 0x01, - 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0d, 0x0a, - 0x09, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x48, 0x41, 0x53, 0x48, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, - 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x4e, 0x45, 0x57, 0x42, 0x4c, - 0x4f, 0x43, 0x4b, 0x10, 0x02, 0x12, 0x0f, 0x0a, 0x0b, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x48, 0x45, - 0x49, 0x47, 0x48, 0x54, 0x10, 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x52, 0x45, 0x47, 0x49, 0x53, 0x54, - 0x45, 0x52, 0x10, 0x04, 0x12, 0x13, 0x0a, 0x0f, 0x52, 0x45, 0x47, 0x49, 0x53, 0x54, 0x45, 0x52, - 0x54, 0x49, 0x4d, 0x45, 0x4f, 0x55, 0x54, 0x10, 0x05, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, - 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x06, 0x12, 0x0f, 0x0a, 0x0b, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x48, - 0x45, 0x41, 0x44, 0x45, 0x52, 0x10, 0x07, 0x12, 0x10, 0x0a, 0x0c, 0x42, 0x4c, 0x4f, 0x43, 0x4b, - 0x57, 0x49, 0x54, 0x48, 0x53, 0x49, 0x47, 0x10, 0x08, 0x22, 0xd4, 0x01, 0x0a, 0x12, 0x44, 0x6f, + 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x28, 0x0a, + 0x0f, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x57, 0x69, 0x74, 0x68, 0x53, 0x69, 0x67, + 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0f, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, + 0x57, 0x69, 0x74, 0x68, 0x53, 0x69, 0x67, 0x22, 0x99, 0x01, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0d, 0x0a, 0x09, 0x42, 0x4c, 0x4f, 0x43, 0x4b, + 0x48, 0x41, 0x53, 0x48, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x10, + 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x4e, 0x45, 0x57, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x10, 0x02, 0x12, + 0x0f, 0x0a, 0x0b, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x10, 0x03, + 0x12, 0x0c, 0x0a, 0x08, 0x52, 0x45, 0x47, 0x49, 0x53, 0x54, 0x45, 0x52, 0x10, 0x04, 0x12, 0x13, + 0x0a, 0x0f, 0x52, 0x45, 0x47, 0x49, 0x53, 0x54, 0x45, 0x52, 0x54, 0x49, 0x4d, 0x45, 0x4f, 0x55, + 0x54, 0x10, 0x05, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x06, + 0x12, 0x0f, 0x0a, 0x0b, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x48, 0x45, 0x41, 0x44, 0x45, 0x52, 0x10, + 0x07, 0x12, 0x10, 0x0a, 0x0c, 0x42, 0x4c, 0x4f, 0x43, 0x4b, 0x57, 0x49, 0x54, 0x48, 0x53, 0x49, + 0x47, 0x10, 0x08, 0x22, 0xd4, 0x01, 0x0a, 0x12, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, + 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, + 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, + 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x47, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0e, 0x32, 0x33, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, + 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x20, 0x0a, + 0x0b, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x48, 0x65, 0x69, 0x67, 0x68, 0x74, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x04, 0x52, 0x0b, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x48, 0x65, 0x69, 0x67, 0x68, 0x74, 0x22, + 0x39, 0x0a, 0x14, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x53, 0x55, 0x43, 0x43, 0x45, + 0x53, 0x53, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x46, 0x41, 0x49, 0x4c, 0x10, 0x01, 0x12, 0x0a, + 0x0a, 0x06, 0x49, 0x4e, 0x53, 0x59, 0x4e, 0x43, 0x10, 0x02, 0x32, 0x56, 0x0a, 0x0a, 0x44, 0x6f, + 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x12, 0x48, 0x0a, 0x05, 0x51, 0x75, 0x65, 0x72, + 0x79, 0x12, 0x1d, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x44, + 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x1e, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x01, 0x20, 0x03, 0x28, - 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x47, 0x0a, 0x04, 0x74, 0x79, - 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x33, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, - 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, - 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, - 0x79, 0x70, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x48, 0x65, 0x69, 0x67, - 0x68, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0b, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x48, - 0x65, 0x69, 0x67, 0x68, 0x74, 0x22, 0x39, 0x0a, 0x14, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, - 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, - 0x07, 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x46, 0x41, - 0x49, 0x4c, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x49, 0x4e, 0x53, 0x59, 0x4e, 0x43, 0x10, 0x02, - 0x32, 0x56, 0x0a, 0x0a, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x12, 0x48, - 0x0a, 0x05, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x1d, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, - 0x61, 0x64, 0x65, 0x72, 0x2e, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1e, 0x2e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, - 0x64, 0x65, 0x72, 0x2e, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x22, 0x00, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/api/service/legacysync/downloader/proto/downloader.proto b/api/service/legacysync/downloader/proto/downloader.proto index bafed0ff9..2be54682b 100644 --- a/api/service/legacysync/downloader/proto/downloader.proto +++ b/api/service/legacysync/downloader/proto/downloader.proto @@ -31,6 +31,7 @@ message DownloaderRequest { string ip = 5; string port = 6; uint32 size = 7; + bool registerWithSig = 8; // Expect to have NEWBLOCK response of block along with current signature } // DownloaderResponse is the generic response of DownloaderRequest. diff --git a/api/service/legacysync/syncing.go b/api/service/legacysync/syncing.go index 07e17b253..deb962159 100644 --- a/api/service/legacysync/syncing.go +++ b/api/service/legacysync/syncing.go @@ -907,7 +907,7 @@ func (ss *StateSync) generateNewState(bc *core.BlockChain, worker *worker.Worker if block == nil { break } - err = ss.UpdateBlockAndStatus(block, bc, worker, false) + err = ss.UpdateBlockAndStatus(block, bc, worker, true) if err != nil { break } diff --git a/node/node.go b/node/node.go index b3fe10a93..951af2c08 100644 --- a/node/node.go +++ b/node/node.go @@ -66,6 +66,8 @@ const ( type syncConfig struct { timestamp int64 client *downloader.Client + // Determine to send encoded BlockWithSig or Block + withSig bool } // Node represents a protocol-participating node in the network diff --git a/node/node_syncing.go b/node/node_syncing.go index d9e45a294..9e1dd8962 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -357,6 +357,11 @@ func (node *Node) SendNewBlockToUnsync() { utils.Logger().Warn().Msg("[SYNC] unable to encode block to hashes") continue } + blockWithSigBytes, err := node.getEncodedBlockWithSigFromBlock(block) + if err != nil { + utils.Logger().Warn().Err(err).Msg("[SYNC] rlp encode BlockWithSig") + continue + } node.stateMutex.Lock() for peerID, config := range node.peerRegistrationRecord { @@ -367,7 +372,11 @@ func (node *Node) SendNewBlockToUnsync() { delete(node.peerRegistrationRecord, peerID) continue } - response, err := config.client.PushNewBlock(node.GetSyncID(), blockBytes, false) + sendBytes := blockBytes + if config.withSig { + sendBytes = blockWithSigBytes + } + response, err := config.client.PushNewBlock(node.GetSyncID(), sendBytes, false) // close the connection if cannot push new block to unsync node if err != nil { node.peerRegistrationRecord[peerID].client.Close() @@ -470,18 +479,21 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, in response.Type = downloader_pb.DownloaderResponse_INSYNC return response, nil } - var blockObj types.Block - err := rlp.DecodeBytes(request.BlockHash, &blockObj) + var bws legacysync.BlockWithSig + err := rlp.DecodeBytes(request.BlockHash, &bws) if err != nil { utils.Logger().Warn().Msg("[SYNC] unable to decode received new block") return response, err } - node.stateSync.AddNewBlock(request.PeerHash, &blockObj) + blockObj := bws.Block + blockObj.SetCurrentCommitSig(bws.CommitSigAndBitmap) + node.stateSync.AddNewBlock(request.PeerHash, blockObj) case downloader_pb.DownloaderRequest_REGISTER: peerID := string(request.PeerHash[:]) ip := request.Ip port := request.Port + withSig := request.RegisterWithSig node.stateMutex.Lock() defer node.stateMutex.Unlock() if _, ok := node.peerRegistrationRecord[peerID]; ok { @@ -509,7 +521,7 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, in Msg("[SYNC] unable to setup client for peerID") return response, nil } - config := &syncConfig{timestamp: time.Now().UnixNano(), client: client} + config := &syncConfig{timestamp: time.Now().UnixNano(), client: client, withSig: withSig} node.peerRegistrationRecord[peerID] = config utils.Logger().Debug(). Str("ip", ip). @@ -603,6 +615,14 @@ func (node *Node) getEncodedBlockWithSigByHash(hash common.Hash) ([]byte, error) return b, nil } +func (node *Node) getEncodedBlockWithSigFromBlock(block *types.Block) ([]byte, error) { + bwh := legacysync.BlockWithSig{ + Block: block, + CommitSigAndBitmap: block.GetCurrentCommitSig(), + } + return rlp.EncodeToBytes(bwh) +} + func (node *Node) getCommitSigAndBitmap(block *types.Block) ([]byte, error) { child := node.Blockchain().GetBlockByNumber(block.NumberU64() + 1) if child != nil {