added required copy(true) to addr

This commit is contained in:
StephenButtolph 2020-05-14 14:25:45 -04:00
parent 19db6fe6d5
commit 5c915d54b3
3 changed files with 17 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/ava-labs/salticidae-go"
"github.com/ava-labs/gecko/utils"
"github.com/ava-labs/gecko/utils/wrappers"
)
@ -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 {
@ -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 {

View File

@ -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
}

View File

@ -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
@ -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()
@ -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()
@ -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)
@ -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 {