WTC

bootnode  

1 修改core/types/block.go中Header的结构体。加上

Signature      []byte      `json:"signature"        gencodec:"required"`

2 在p2p/discover/udp.go中修改

"os"
	"github.com/ethereum/go-ethereum/common"
p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
	// Send neighbors in chunks with at most maxNeighbors per packet
	// to stay below the 1280 byte limit.
	for i, n := range closest {
		if netutil.CheckRelayIP(from.IP, n.IP) != nil {
			continue
		}
		p.Nodes = append(p.Nodes, nodeToRPC(n))
		if len(p.Nodes) == maxNeighbors || i == len(closest)-1 {
			t.send(from, neighborsPacket, &p)
			p.Nodes = p.Nodes[:0]
		}
	}
	fast := make([]*Node, 0, bucketSize)  
    fast = append(fast,MasterNodes()...)  
    // Send neighbors in chunks with at most maxNeighbors per packet  
    // to stay below the 1280 byte limit.  
    for _, n := range fast {  
        if netutil.CheckRelayIP(from.IP, n.IP) != nil {  
            continue  
        }  
        p.Nodes = append(p.Nodes, nodeToRPC(n))  
    }  
    fmt.Println(p.Nodes)  
	return nil
}

func  MasterNodes() []*Node {
	return parsePersistentNodes("/home/flq/Projects/src/github.com/ethereum/go-ethereum/p2p/discover/masternodes.json")
}

func parsePersistentNodes(path string) []*Node {
	// Short circuit if no node config is present
	if path == "" {
		return nil
	}
	if _, err := os.Stat(path); err != nil {
		return nil
	}
	// Load the nodes from the config file.
	var nodelist []string
	if err := common.LoadJSON(path, &nodelist); err != nil {
		log.Error(fmt.Sprintf("Can't load node file %s: %v", path, err))
		return nil
	}
	// Interpret the list as a discovery node array
	var nodes []*Node
	for _, url := range nodelist {
		if url == "" {
			continue
		}
		node, err := ParseNode(url)
		if err != nil {
			log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
			continue
		}
		nodes = append(nodes, node)
	}
	return nodes
}
可以看到bootnode的输出有我们定义的masternodes

在geth上输入geth --identity "qkl" --datadir "qkl" --networkid 1123 --rpc --port 30303 --bootnodes "enode://3ccd479f747ac233649fd6013f6bc1676ce55f9e2ba3d415c16fe663dc20a172a7450f20f96d2c640b6431a9f5bcad34bdb4a287dbe1bcf2d7c658edae810bd6@192.168.70.183:10101" console

flq@ubuntu:~/Projects/bin$ ./bootnode -verbosity=5 -nodekey=boot.key
INFO [01-25|03:29:21] UDP listener up                          self=enode://3ccd479f747ac233649fd6013f6bc1676ce55f9e2ba3d415c16fe663dc20a172a7450f20f96d2c640b6431a9f5bcad34bdb4a287dbe1bcf2d7c658edae810bd6@[::]:10101
TRACE[01-25|03:29:26] >> PONG/v4                               addr=192.168.70.183:30303 err=nil
TRACE[01-25|03:29:26] << PING/v4                               addr=192.168.70.183:30303 err=nil
TRACE[01-25|03:29:26] Starting bonding ping/pong               id=d046dc43aee4ce5c known=false failcount=0 age=421355h29m26.743076253s
TRACE[01-25|03:29:26] >> PING/v4                               addr=192.168.70.183:30303 err=nil
TRACE[01-25|03:29:26] << PONG/v4                               addr=192.168.70.183:30303 err=nil
TRACE[01-25|03:29:44] >> NEIGHBORS/v4                          addr=192.168.70.183:30303 err=nil
[{192.168.1.2 61910 61910 ad307e052d0e04af519b8999fa870800df8a7a0cc2a91e6aea30e879b75c344dfa12c773a63a71677c2a3ea1254cf982815817f7ff58bd79e5837ea44d791a2d} {120.27.164.92 13333 13333 91922b12115c067005c574844c6bbdb114eb262f90b6355cec89e13b483c3e4669c6d63ec66b6e3ca7a3a462d28edb3c659e9fa05ed4c7234524e582a8816743}]
TRACE[01-25|03:29:44] << FINDNODE/v4                           addr=192.168.70.183:30303 err=nil
TRACE[01-25|03:30:10] >> NEIGHBORS/v4                          addr=192.168.70.183:30303 err=nil
[{192.168.1.2 61910 61910 ad307e052d0e04af519b8999fa870800df8a7a0cc2a91e6aea30e879b75c344dfa12c773a63a71677c2a3ea1254cf982815817f7ff58bd79e5837ea44d791a2d} {120.27.164.92 13333 13333 91922b12115c067005c574844c6bbdb114eb262f90b6355cec89e13b483c3e4669c6d63ec66b6e3ca7a3a462d28edb3c659e9fa05ed4c7234524e582a8816743}]
TRACE[01-25|03:30:10] << FINDNODE/v4                           addr=192.168.70.183:30303 err=nil
masternode

1 修改core/types/block.go中Header的结构体。加上

Signature      []byte      `json:"signature"        gencodec:"required"`

2 在params/bootno

de.go 中加上自己的bootnode

"enode://3ccd479f747ac233649fd6013f6bc1676ce55f9e2ba3d415c16fe663dc20a172a7450f20f96d2c640b6431a9f5bcad34bdb4a287dbe1bcf2d7c658edae810bd6@192.168.70.183:10101"

3 在concensus/ethash/sealer.go中加上

"crypto/ecdsa"
	"github.com/ethereum/go-ethereum/crypto"
prv := ethash.NodeKey()
				h := header.HashNoNonce().Bytes()
				sig, err := crypto.Sign(h[:],prv)
				if err != nil{
					return		
				}
				copy(header.Signature, sig)
				fmt.Println("this block sig ", header.Number,sig)
func (ethash *Ethash) NodeKey() *ecdsa.PrivateKey {
	
	keyfile := "/home/flq/blctest/blc/geth/nodekey"
	if key, err := crypto.LoadECDSA(keyfile); err == nil {
		fmt.Println(key)
		return key
	}
	// No persistent key found, generate and store a new one.
	key, err := crypto.GenerateKey()
	if err != nil {
		log.Crit(fmt.Sprintf("Failed to generate node key: %v", err))
	}
	
	
	return key
}

在verify.sealer.go中对区块的签名验证

if len(header.Signature) < 65 {
         prv := ethash. NodeKey()
         h := header. HashNoNonce(). Bytes()
         sig, err := crypto. Sign(h[:],prv)
        fmt. Println( "verify no sig then give sig ",number,sig)
         header.Signature = make([] byte, 65)
         copy(header.Signature[:], sig[:])
        fmt. Println( "verify no sig then give headersignature ",header.Signature)
         if err != nil{
         return err 
        }
    } else {
    fmt. Println( "verify sig ")
     //signer := types.MakeSigner(chain.Config(), header.Number)
     // Resolve the authorization key and check against signers
     signer, err := recovers(header)
     if err != nil {
    fmt. Println( "verify sig wrong ",signer)
         return err
    }
    
}


func recovers(header *types.Header) (common.Address, error) {
	
	
	signature := header.Signature


	pubkey, err := crypto.Ecrecover(header.HashNoNonce().Bytes(), signature)
	if err != nil {
		return common.Address{}, err
	}
	  var signer common.Address
	copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
	return signer, nil
}
这样,只要正常的进去geth则会同步区块的信息。说明bootnode起作用,而且会对没有签名的区块签名


verify no sig then give sig  77 [66 167 41 73 93 40 200 178 78 5 62 159 38 140 57 1 211 206 48 58 113 155 1 96 46 43 150 18 79 166 137 18 80 55 31 69 0 173 151 243 253 177 229 42 82 220 31 117 164 177 150 231 22 165 99 0 108 134 236 187 204 106 68 206 1]
verify no sig then give headersignature  [66 167 41 73 93 40 200 178 78 5 62 159 38 140 57 1 211 206 48 58 113 155 1 96 46 43 150 18 79 166 137 18 80 55 31 69 0 173 151 243 253 177 229 42 82 220 31 117 164 177 150 231 22 165 99 0 108 134 236 187 204 106 68 206 1]
verify no sig then give sig  78 [71 199 15 69 230 71 2 141 190 92 174 58 41 169 108 34 250 87 227 46 80 88 79 238 71 123 196 157 204 247 10 44 104 133 205 248 237 205 100 233 14 174 29 101 151 194 35 131 246 119 50 92 118 83 174 234 106 237 35 157 7 175 243 241 0]
verify no sig then give headersignature  [71 199 15 69 230 71 2 141 190 92 174 58 41 169 108 34 250 87 227 46 80 88 79 238 71 123 196 157 204 247 10 44 104 133 205 248 237 205 100 233 14 174 29 101 151 194 35 131 246 119 50 92 118 83 174 234 106 237 35 157 7 175 243 241 0]


你可能感兴趣的:(WTC)