Skip to content

Commit

Permalink
Merge pull request #167 from StephenButtolph/mem-cleanup
Browse files Browse the repository at this point in the history
added required copy(true) to addr
  • Loading branch information
StephenButtolph authored May 14, 2020
2 parents ccdc7b8 + 8f409e2 commit 7c4abde
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
13 changes: 7 additions & 6 deletions networking/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ava-labs/salticidae-go"

"github.com/ava-labs/gecko/utils"
"github.com/ava-labs/gecko/utils/wrappers"
)

Expand All @@ -22,6 +23,8 @@ var (
type Codec struct{}

// Pack attempts to pack a map of fields into a message.
//
// If a nil error is returned, the message's datastream must be freed manually
func (Codec) Pack(op salticidae.Opcode, fields map[Field]interface{}) (Msg, error) {
message, ok := Messages[op]
if !ok {
Expand Down Expand Up @@ -49,20 +52,18 @@ func (Codec) Pack(op salticidae.Opcode, fields map[Field]interface{}) (Msg, erro
}

// Parse attempts to convert a byte stream into a message.
//
// The datastream is not freed.
func (Codec) Parse(op salticidae.Opcode, ds salticidae.DataStream) (Msg, error) {
message, ok := Messages[op]
if !ok {
return nil, errBadOp
}

// TODO: make this work without copy
size := ds.Size()
p := wrappers.Packer{Bytes: make([]byte, size)}

byteHandle := ds.GetDataInPlace(size)
defer byteHandle.Release()

copy(p.Bytes, byteHandle.Get())
p := wrappers.Packer{Bytes: utils.CopyBytes(byteHandle.Get())}
byteHandle.Release()

fields := make(map[Field]interface{}, len(message))
for _, field := range message {
Expand Down
3 changes: 2 additions & 1 deletion networking/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,12 @@ func toID(peer salticidae.PeerID) [32]byte {

size := ds.Size()
dsb := ds.GetDataInPlace(size)
idBytes := dsb.Get()

idBytes := dsb.Get()
id := [32]byte{}
copy(id[:], idBytes)

dsb.Release()
ds.Free()
return id
}
Expand Down
10 changes: 8 additions & 2 deletions networking/handshake_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func (nm *Handshake) Initialize(
}

// ConnectTo add the peer as a connection and connects to them.
//
// assumes the peerID and addr are autofreed
func (nm *Handshake) ConnectTo(peer salticidae.PeerID, stakerID ids.ShortID, addr salticidae.NetAddr) {
if nm.pending.ContainsPeerID(peer) || nm.connections.ContainsPeerID(peer) {
return
Expand All @@ -230,7 +232,9 @@ func (nm *Handshake) ConnectTo(peer salticidae.PeerID, stakerID ids.ShortID, add
})
}

// Connect ...
// Connect attempts to start a connection with this provided address
//
// assumes addr is autofreed.
func (nm *Handshake) Connect(addr salticidae.NetAddr) {
ip := toIPDesc(addr)
ipStr := ip.String()
Expand Down Expand Up @@ -441,7 +445,7 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool, _ unsafe.P
defer HandshakeNet.requestedLock.Unlock()

conn := salticidae.MsgNetworkConnFromC(salticidae.CMsgNetworkConn(_conn))
addr := conn.GetAddr()
addr := conn.GetAddr().Copy(true)
ip := toIPDesc(addr)
ipStr := ip.String()

Expand All @@ -461,6 +465,7 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool, _ unsafe.P
return true
}

// assumes peer is autofreed
func (nm *Handshake) connectedToPeer(conn *C.struct_peernetwork_conn_t, peer salticidae.PeerID) {
peerBytes := toID(peer)
peerID := ids.NewID(peerBytes)
Expand Down Expand Up @@ -490,6 +495,7 @@ func (nm *Handshake) connectedToPeer(conn *C.struct_peernetwork_conn_t, peer sal
(*handler)()
}

// assumes peer is autofreed
func (nm *Handshake) disconnectedFromPeer(peer salticidae.PeerID) {
cert := ids.ShortID{}
if pendingCert, exists := nm.pending.GetID(peer); exists {
Expand Down

0 comments on commit 7c4abde

Please sign in to comment.