github.com/AlohaMobile/go-ethereum@v1.9.7/les/ulc.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/ethereum/go-ethereum/log"
    23  	"github.com/ethereum/go-ethereum/p2p/enode"
    24  )
    25  
    26  type ulc struct {
    27  	keys     map[string]bool
    28  	fraction int
    29  }
    30  
    31  // newULC creates and returns an ultra light client instance.
    32  func newULC(servers []string, fraction int) (*ulc, error) {
    33  	keys := make(map[string]bool)
    34  	for _, id := range servers {
    35  		node, err := enode.Parse(enode.ValidSchemes, id)
    36  		if err != nil {
    37  			log.Warn("Failed to parse trusted server", "id", id, "err", err)
    38  			continue
    39  		}
    40  		keys[node.ID().String()] = true
    41  	}
    42  	if len(keys) == 0 {
    43  		return nil, errors.New("no trusted servers")
    44  	}
    45  	return &ulc{
    46  		keys:     keys,
    47  		fraction: fraction,
    48  	}, nil
    49  }
    50  
    51  // trusted return an indicator that whether the specified peer is trusted.
    52  func (u *ulc) trusted(p enode.ID) bool {
    53  	return u.keys[p.String()]
    54  }