Skip to content

Commit

Permalink
Add support for Primary communication protocol (ethereum#333)
Browse files Browse the repository at this point in the history
* Add support for Primary communication protocol

* Cleanup

* Cleanup
  • Loading branch information
Asa Oines authored and mergify[bot] committed Jul 11, 2019
1 parent 3b392df commit 24650a8
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions consensus/istanbul/backend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (sb *Backend) Protocol() consensus.Protocol {
Name: "istanbul",
Versions: []uint{64},
Lengths: []uint64{19},
Primary: true,
}
}

Expand Down
2 changes: 2 additions & 0 deletions consensus/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Protocol struct {
Versions []uint
// Number of implemented message corresponding to different protocol versions.
Lengths []uint64
// Whether this should be the primary form of communication between nodes that support this protocol.
Primary bool
}

// Broadcaster defines the interface to enqueue blocks to fetcher and find peer
Expand Down
2 changes: 1 addition & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func (evm *EVM) ABIStaticCall(caller ContractRef, address common.Address, abi ab

func (evm *EVM) ABICall(caller ContractRef, address common.Address, abi abi.ABI, funcName string, args []interface{}, returnObj interface{}, gas uint64, value *big.Int) (uint64, error) {
call := func(transactionData []byte) ([]byte, uint64, error) {
log.Trace("Performing call in the EVM", "caller", caller, "transactionData", hexutil.Encode(transactionData))
log.Trace("Performing call in the EVM", "caller", caller, "transactionData", hexutil.Encode(transactionData), "funcName", funcName)

return evm.Call(caller, address, transactionData, gas, value)
}
Expand Down
1 change: 1 addition & 0 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
Name: protocol.Name,
Version: version,
Length: protocol.Lengths[i],
Primary: protocol.Primary,
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
peer := manager.newPeer(int(version), p, rw)
select {
Expand Down
11 changes: 11 additions & 0 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ outer:
}
}
}

// If a primary protocol matched, return only that protocol.
for _, proto := range protocols {
if proto.Primary {
if _, ok := result[proto.Name]; ok {
primary := make(map[string]*protoRW)
primary[proto.Name] = result[proto.Name]
return primary
}
}
}
return result
}

Expand Down
3 changes: 3 additions & 0 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Protocol struct {
// by the protocol.
Length uint64

// Whether this should be the primary form of communication between nodes that support this protocol.
Primary bool

// Run is called in a new goroutine when the protocol has been
// negotiated with a peer. It should read and write messages from
// rw. The Payload for each message must be fully consumed.
Expand Down
8 changes: 8 additions & 0 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"net"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -521,11 +522,18 @@ func (srv *Server) setupLocalNode() error {
srv.localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
srv.localnode.Set(capsByNameAndVersion(srv.ourHandshake.Caps))
// TODO: check conflicts
var primaries []string
for _, p := range srv.Protocols {
if p.Primary {
primaries = append(primaries, p.Name)
}
for _, e := range p.Attributes {
srv.localnode.Set(e)
}
}
if len(primaries) > 1 {
srv.log.Crit("Multiple primary protocols specified", "names", strings.Join(primaries, ", "))
}
switch srv.NAT.(type) {
case nil:
// No NAT interface, do nothing.
Expand Down

0 comments on commit 24650a8

Please sign in to comment.