github.com/amazechain/amc@v0.1.3/internal/download/response.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package download
    18  
    19  import (
    20  	"github.com/amazechain/amc/api/protocol/sync_proto"
    21  	"github.com/amazechain/amc/api/protocol/types_pb"
    22  	"github.com/amazechain/amc/common"
    23  	"github.com/amazechain/amc/common/message"
    24  	"github.com/amazechain/amc/log"
    25  	"github.com/amazechain/amc/utils"
    26  	"google.golang.org/protobuf/proto"
    27  
    28  	"github.com/holiman/uint256"
    29  )
    30  
    31  // responseHeaders
    32  func (d *Downloader) responseHeaders(taskID uint64, p common.Peer, task *sync_proto.SyncHeaderRequest) {
    33  
    34  	headers := make([]*types_pb.Header, 0, utils.ConvertH256ToUint256Int(task.Amount).Uint64())
    35  	ok := true
    36  
    37  	origin := task.Number
    38  	for i := 0; i < int(utils.ConvertH256ToUint256Int(task.Amount).Uint64()); i++ {
    39  		fullHeader := d.bc.GetHeaderByNumber(utils.ConvertH256ToUint256Int(origin))
    40  		var header *types_pb.Header
    41  		if fullHeader == nil {
    42  			log.Warnf("cannot fetch header from db the number is:%v", utils.ConvertH256ToUint256Int(origin).Uint64())
    43  			headers = headers[0:0]
    44  			ok = false
    45  			break
    46  		} else {
    47  			header = fullHeader.ToProtoMessage().(*types_pb.Header)
    48  			log.Tracef("fetch header from db the number is:%v", utils.ConvertH256ToUint256Int(header.Number).Uint64())
    49  		}
    50  
    51  		headers = append(headers, header)
    52  		origin = utils.ConvertUint256IntToH256(uint256.NewInt(0).Add(utils.ConvertH256ToUint256Int(origin), uint256.NewInt(1)))
    53  	}
    54  
    55  	log.Tracef("fetch all header from db the count is:%v", len(headers))
    56  
    57  	msg := &sync_proto.SyncTask{
    58  		Id:       taskID,
    59  		Ok:       ok,
    60  		SyncType: sync_proto.SyncType_HeaderRes,
    61  		Payload: &sync_proto.SyncTask_SyncHeaderResponse{
    62  			SyncHeaderResponse: &sync_proto.SyncHeaderResponse{
    63  				Headers: headers,
    64  			},
    65  		},
    66  	}
    67  	payload, err := proto.Marshal(msg)
    68  
    69  	if err != nil {
    70  		log.Errorf("proto Marshal err: %v", err)
    71  		return
    72  	}
    73  
    74  	p.WriteMsg(message.MsgDownloader, payload)
    75  	log.Debugf("response sync task(headersRequest) ok: %v , taskID: %v, header count: %v", ok, taskID, len(headers))
    76  }
    77  
    78  // responseHeaders body
    79  func (d *Downloader) responseBlocks(taskID uint64, p common.Peer, task *sync_proto.SyncBlockRequest) {
    80  
    81  	blocks := make([]*types_pb.Block, 0, len(task.Number))
    82  
    83  	ok := true
    84  
    85  	for _, number := range task.Number {
    86  		block, err := d.bc.GetBlockByNumber(utils.ConvertH256ToUint256Int(number))
    87  		if err != nil {
    88  			log.Infof("cannot fetch block from db the number is:%d, err: %v", utils.ConvertH256ToUint256Int(number).Uint64(), err)
    89  			blocks = blocks[0:0]
    90  			ok = false
    91  			break
    92  		}
    93  		if PBlock, ok := block.ToProtoMessage().(*types_pb.Block); ok {
    94  			blocks = append(blocks, PBlock)
    95  			log.Tracef("fetch body from db the number is:%v", utils.ConvertH256ToUint256Int(number).Uint64())
    96  		}
    97  	}
    98  
    99  	log.Tracef("fetch all blocks from db the count is:%d", len(blocks))
   100  
   101  	msg := &sync_proto.SyncTask{
   102  		Id:       taskID,
   103  		Ok:       ok,
   104  		SyncType: sync_proto.SyncType_BodyRes,
   105  		Payload: &sync_proto.SyncTask_SyncBlockResponse{
   106  			SyncBlockResponse: &sync_proto.SyncBlockResponse{
   107  				Blocks: blocks,
   108  			},
   109  		},
   110  	}
   111  	payload, err := proto.Marshal(msg)
   112  
   113  	if err != nil {
   114  		log.Errorf("proto Marshal err: %v", err)
   115  		return
   116  	}
   117  
   118  	p.WriteMsg(message.MsgDownloader, payload)
   119  	log.Debugf("response sync task(blockRequest) ok: %v , taskID: %v, block count: %v", ok, taskID, len(task.Number))
   120  }