github.com/amazechain/amc@v0.1.3/internal/sync/subscriber_blocks.go (about)

     1  package sync
     2  
     3  import (
     4  	"context"
     5  	block2 "github.com/amazechain/amc/common/block"
     6  	"github.com/amazechain/amc/log"
     7  	"google.golang.org/protobuf/proto"
     8  )
     9  
    10  func (s *Service) blockSubscriber(ctx context.Context, msg proto.Message) error {
    11  
    12  	iBlock := new(block2.Block)
    13  	if err := iBlock.FromProtoMessage(msg); err != nil {
    14  		return err
    15  	}
    16  
    17  	blocks := make([]block2.IBlock, 0)
    18  	blocks = append(blocks, iBlock)
    19  
    20  	log.Info("Subscriber new Block", "hash", iBlock.Header().Hash(), "blockNr", iBlock.Header().Number64().Uint64())
    21  
    22  	if iBlock.Number64().Uint64() > s.cfg.chain.CurrentBlock().Number64().Uint64()+1 {
    23  		if err := s.cfg.chain.AddFutureBlock(iBlock); err != nil {
    24  			return err
    25  		}
    26  	} else if _, err := s.cfg.chain.InsertChain(blocks); err != nil {
    27  		// todo bad block
    28  		//if errors.Is(err, Badblock) {
    29  		s.setBadBlock(ctx, iBlock.Hash())
    30  		return err
    31  	}
    32  	return nil
    33  }