github.com/amazechain/amc@v0.1.3/internal/p2p/fork.go (about)

     1  package p2p
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/amazechain/amc/common/types"
     7  	"github.com/amazechain/amc/internal/p2p/enode"
     8  	"github.com/amazechain/amc/internal/p2p/enr"
     9  	"github.com/amazechain/amc/utils"
    10  	"github.com/holiman/uint256"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  const amtENRKey = "amcEnr"
    15  
    16  // ForkDigest returns the current fork digest of
    17  // the node according to the local clock.
    18  func (s *Service) currentForkDigest() ([4]byte, error) {
    19  	return utils.CreateForkDigest(new(uint256.Int), s.genesisHash)
    20  }
    21  
    22  // Compares fork ENRs between an incoming peer's record and our node's
    23  // local record values for current and next fork version/epoch.
    24  func (s *Service) compareForkENR(record *enr.Record) error {
    25  	remoteForkDigest := make([]byte, 4)
    26  	entry := enr.WithEntry(amtENRKey, &remoteForkDigest)
    27  	err := record.Load(entry)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	enrString, err := SerializeENR(record)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	currentForkDigest, err := s.currentForkDigest()
    37  	if err != nil {
    38  		err := errors.Wrap(err, "could not retrieve fork digest")
    39  		//tracing.AnnotateError(span, err)
    40  		return err
    41  	}
    42  
    43  	if !bytes.Equal(remoteForkDigest, currentForkDigest[:]) {
    44  		return fmt.Errorf(
    45  			"fork digest of peer with ENR %s: %v, does not match local value: %v",
    46  			enrString,
    47  			currentForkDigest,
    48  			remoteForkDigest,
    49  		)
    50  	}
    51  	return nil
    52  }
    53  
    54  // Adds a fork entry as an ENR record under the Ethereum consensus EnrKey for
    55  // the local node. The fork entry is an ssz-encoded enrForkID type
    56  // which takes into account the current fork version from the current
    57  // epoch to create a fork digest, the next fork version,
    58  // and the next fork epoch.
    59  func addForkEntry(node *enode.LocalNode, genesisHash types.Hash) (*enode.LocalNode, error) {
    60  	//todo
    61  	enc, err := utils.CreateForkDigest(new(uint256.Int), genesisHash)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	forkEntry := enr.WithEntry(amtENRKey, enc[:])
    66  	node.Set(forkEntry)
    67  	return node, nil
    68  }