Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

联盟链节点改造,可以通过proxy互相访问 #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions gemmill/angine.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func NewAngine(app types.Application, tune *Tunes) (angine *Angine, err error) {

logger, err := getLogger(conf)
if err != nil {
err = fmt.Errorf("failed to get logger: %v", err)
err = fmt.Errorf("failed to get logger: %v", err)
return nil, err
}
log.SetLog(logger)
Expand All @@ -163,13 +163,13 @@ func NewAngine(app types.Application, tune *Tunes) (angine *Angine, err error) {
crypto.NodeInit(crypto.CryptoType)
privValidator, err := types.LoadPrivValidator(conf.GetString("priv_validator_file"))
if err != nil {
err = fmt.Errorf("LoadPrivValidator error: %v", err)
err = fmt.Errorf("LoadPrivValidator error: %v", err)
return nil, err
}
refuseList = refuse_list.NewRefuseList(dbBackend, dbDir)
p2psw, err := prepareP2P(conf, genesis, privValidator, refuseList)
if err != nil {
err = fmt.Errorf("prepare p2p error: %v", err)
err = fmt.Errorf("prepare p2p error: %v", err)
return nil, err
}

Expand Down Expand Up @@ -231,7 +231,7 @@ func (a *Angine) OnRecvExchangeData(data *p2p.ExchangeData) error {
a.p2pSwitch.GetExchangeData().GenesisJSON = data.GenesisJSON
if err = a.buildState(otherGenesis); err != nil {
// TODO log err
log.Warn("build state err:", zap.Error(err))
log.Warn("build state err:", zap.Error(err))
return err
}
if a.stateMachine == nil {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (ang *Angine) assembleStateMachine(stateM *state.State) {
var addrBook *p2p.AddrBook
if conf.GetBool("pex_reactor") {
addrBook = p2p.NewAddrBook(conf.GetString("addrbook_file"), conf.GetBool("addrbook_strict"))
pexReactor := p2p.NewPEXReactor(addrBook)
pexReactor := p2p.NewPEXReactor(addrBook, conf.GetString("p2p_proxy_addr"))
ang.p2pSwitch.AddReactor("PEX", pexReactor)
}

Expand Down Expand Up @@ -1131,11 +1131,12 @@ func prepareP2P(conf *viper.Viper, genesis *types.GenesisDoc, privValidator *typ
return nil, errors.Wrap(err, "prepareP2P")
}
nodeInfo := &p2p.NodeInfo{
PubKey: privValidator.GetPubKey(),
SigndPubKey: conf.GetString("signbyCA"),
Moniker: conf.GetString("moniker"),
ListenAddr: defaultListener.ExternalAddress().String(),
Version: version,
PubKey: privValidator.GetPubKey(),
SigndPubKey: conf.GetString("signbyCA"),
Moniker: conf.GetString("moniker"),
ListenAddr: defaultListener.ExternalAddress().String(),
P2pProxyAddr: conf.GetString("p2p_proxy_addr"),
Version: version,
}
privKey := privValidator.GetPrivKey()
p2psw.AddListener(defaultListener)
Expand Down
22 changes: 21 additions & 1 deletion gemmill/p2p/pex_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"math/rand"
"strings"
"time"

"github.com/dappledger/AnnChain/gemmill/go-wire"
Expand All @@ -45,12 +46,22 @@ type PEXReactor struct {
BaseReactor

book *AddrBook
//
localP2pProxyAddr *NetAddress
}

func NewPEXReactor(book *AddrBook) *PEXReactor {
func NewPEXReactor(book *AddrBook, localProxy string) *PEXReactor {
pexR := &PEXReactor{
book: book,
}
log.Infof("NewPEXReactor(%s)",localProxy)
pexR.localP2pProxyAddr = &NetAddress{}
if localProxy != "" {
addr, _ := NewNetAddressString(localProxy)
if addr != nil {
pexR.localP2pProxyAddr = addr
}
}
pexR.BaseReactor = *NewBaseReactor("PEXReactor", pexR)
return pexR
}
Expand Down Expand Up @@ -78,6 +89,15 @@ func (pexR *PEXReactor) GetChannels() []*ChannelDescriptor {

// Implements Reactor
func (pexR *PEXReactor) AddPeer(peer *Peer) {
if strings.TrimSpace(peer.P2pProxyAddr) != "" {
//如果用相同的代理,那么就是同一套内网系统,可以直接走原来的流程;否则,走proxy流程.
netAddr, _ := NewNetAddressString(peer.P2pProxyAddr)
if !bytes.Equal(pexR.localP2pProxyAddr.IP, netAddr.IP) {
pexR.book.AddAddress(netAddr, netAddr)
log.Debugf("AddPeer(%s)",netAddr.String())
return
}
}
// Add the peer to the address book
netAddr, _ := NewNetAddressString(peer.ListenAddr)
if peer.IsOutbound() {
Expand Down
17 changes: 9 additions & 8 deletions gemmill/p2p/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import (
const maxNodeInfoSize = 10240 // 10Kb

type NodeInfo struct {
PubKey crypto.PubKey `json:"pub_key"`
SigndPubKey string `json:"signd_pub_key"`
Moniker string `json:"moniker"`
Network string `json:"network"`
RemoteAddr string `json:"remote_addr"`
ListenAddr string `json:"listen_addr"`
Version string `json:"version"` // major.minor.revision
Other []string `json:"other"` // other application specific data
PubKey crypto.PubKey `json:"pub_key"`
SigndPubKey string `json:"signd_pub_key"`
Moniker string `json:"moniker"`
Network string `json:"network"`
RemoteAddr string `json:"remote_addr"`
ListenAddr string `json:"listen_addr"`
P2pProxyAddr string `json:"p2p_proxy_addr"`
Version string `json:"version"` // major.minor.revision
Other []string `json:"other"` // other application specific data
}

type ExchangeData struct {
Expand Down