fix: move everything using endian to uint64

This commit is contained in:
Derrick Hammer 2024-01-10 11:00:01 -05:00
parent f9a0bd863c
commit 19fb3b9967
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
6 changed files with 44 additions and 26 deletions

View File

@ -21,7 +21,7 @@ type CID struct {
Multibase Multibase
Type types.CIDType Type types.CIDType
Hash Multihash Hash Multihash
Size uint32 Size uint64
} }
var _ json.Marshaler = (*CID)(nil) var _ json.Marshaler = (*CID)(nil)
@ -29,7 +29,7 @@ var _ json.Unmarshaler = (*CID)(nil)
var _ msgpack.CustomEncoder = (*CID)(nil) var _ msgpack.CustomEncoder = (*CID)(nil)
var _ msgpack.CustomDecoder = (*CID)(nil) var _ msgpack.CustomDecoder = (*CID)(nil)
func NewCID(Type types.CIDType, Hash Multihash, Size uint32) *CID { func NewCID(Type types.CIDType, Hash Multihash, Size uint64) *CID {
c := &CID{ c := &CID{
Type: Type, Type: Type,
Hash: Hash, Hash: Hash,
@ -104,7 +104,7 @@ func CIDFromBytes(bytes []byte) (*CID, error) {
return initCID(bytes) return initCID(bytes)
} }
func CIDFromHash(bytes interface{}, size uint32, cidType types.CIDType) (*CID, error) { func CIDFromHash(bytes interface{}, size uint64, cidType types.CIDType) (*CID, error) {
var ( var (
byteSlice []byte byteSlice []byte
err error err error
@ -151,7 +151,7 @@ func CIDVerify(bytes interface{}) bool {
return err == nil return err == nil
} }
func (cid *CID) CopyWith(newType int, newSize uint32) (*CID, error) { func (cid *CID) CopyWith(newType int, newSize uint64) (*CID, error) {
if newType == 0 { if newType == 0 {
newType = int(cid.Type) newType = int(cid.Type)
} }
@ -282,7 +282,7 @@ func initCID(bytes []byte) (*CID, error) {
hashBytes := bytes[1:34] hashBytes := bytes[1:34]
hash := NewMultihash(hashBytes) hash := NewMultihash(hashBytes)
var size uint32 var size uint64
if len(bytes) > 34 { if len(bytes) > 34 {
sizeBytes := bytes[34:] sizeBytes := bytes[34:]
sizeValue := utils.DecodeEndian(sizeBytes) sizeValue := utils.DecodeEndian(sizeBytes)

View File

@ -13,7 +13,7 @@ type EncryptedCID struct {
encryptedBlobHash Multihash encryptedBlobHash Multihash
OriginalCID CID OriginalCID CID
encryptionAlgorithm byte encryptionAlgorithm byte
padding uint32 padding uint64
chunkSizeAsPowerOf2 int chunkSizeAsPowerOf2 int
encryptionKey []byte encryptionKey []byte
} }
@ -23,7 +23,7 @@ var _ msgpack.CustomDecoder = (*EncryptedCID)(nil)
var _ json.Marshaler = (*EncryptedCID)(nil) var _ json.Marshaler = (*EncryptedCID)(nil)
var _ json.Unmarshaler = (*EncryptedCID)(nil) var _ json.Unmarshaler = (*EncryptedCID)(nil)
func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint32, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID { func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint64, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID {
e := &EncryptedCID{ e := &EncryptedCID{
encryptedBlobHash: encryptedBlobHash, encryptedBlobHash: encryptedBlobHash,
OriginalCID: originalCID, OriginalCID: originalCID,

View File

@ -109,7 +109,7 @@ func MarshalRegistryEntry(data []byte, revision uint64) []byte {
var buffer []byte var buffer []byte
buffer = append(buffer, byte(types.RecordTypeRegistryEntry)) buffer = append(buffer, byte(types.RecordTypeRegistryEntry))
revBytes := utils.EncodeEndian(uint32(revision), 8) revBytes := utils.EncodeEndian(revision, 8)
buffer = append(buffer, revBytes...) buffer = append(buffer, revBytes...)
buffer = append(buffer, byte(len(data))) buffer = append(buffer, byte(len(data)))

View File

@ -70,7 +70,7 @@ func (h *HTTPImpl) p2pHandler(ctx jape.Context) {
return return
} }
go func() { go func() {
err := h.node.Services().P2P().OnNewPeer(peer, true) err := h.node.Services().P2P().OnNewPeer(peer, false)
if err != nil { if err != nil {
h.node.Logger().Error("error handling new peer", zap.Error(err)) h.node.Logger().Error("error handling new peer", zap.Error(err))
} }

View File

@ -248,7 +248,14 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool) error {
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error { func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
var wg sync.WaitGroup var wg sync.WaitGroup
pid, _ := peer.Id().ToString() var pid string
if peer.Id() != nil {
pid, _ = peer.Id().ToString()
} else {
pid = "unknown"
}
p.logger.Debug("OnNewPeer started", zap.String("peer", pid)) p.logger.Debug("OnNewPeer started", zap.String("peer", pid))
challenge := protocol.GenerateChallenge() challenge := protocol.GenerateChallenge()
@ -277,18 +284,29 @@ func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
return nil return nil
} }
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) { func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
peerId, err := peer.Id().ToString()
var pid string
if peer.Id() != nil {
pid, _ = peer.Id().ToString()
} else {
pid = "unknown"
}
onDone := net.CloseCallback(func() {
if peer.Id() != nil {
pid, err := peer.Id().ToString()
if err != nil { if err != nil {
p.logger.Error("failed to get peer id", zap.Error(err)) p.logger.Error("failed to get peer id", zap.Error(err))
return return
} }
onDone := net.CloseCallback(func() {
// Handle closure of the connection // Handle closure of the connection
if p.peers.Contains(peerId) { if p.peers.Contains(pid) {
p.peers.Remove(peerId) p.peers.Remove(pid)
}
if p.peersPending.Contains(pid) {
p.peersPending.Remove(pid)
} }
if p.peersPending.Contains(peerId) {
p.peersPending.Remove(peerId)
} }
}) })
@ -300,7 +318,7 @@ func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
imsg := base.NewIncomingMessageUnknown() imsg := base.NewIncomingMessageUnknown()
err := msgpack.Unmarshal(message, imsg) err := msgpack.Unmarshal(message, imsg)
p.logger.Debug("ListenForMessages", zap.Any("message", imsg), zap.String("peer", peerId)) p.logger.Debug("ListenForMessages", zap.Any("message", imsg), zap.String("peer", pid))
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,15 +2,15 @@ package utils
import "encoding/binary" import "encoding/binary"
func EncodeEndian(value uint32, length int) []byte { func EncodeEndian(value uint64, length int) []byte {
byteSlice := make([]byte, length) byteSlice := make([]byte, length)
binary.LittleEndian.PutUint32(byteSlice, value) binary.LittleEndian.PutUint64(byteSlice, value)
return byteSlice return byteSlice
} }
func DecodeEndian(byteSlice []byte) uint32 { func DecodeEndian(byteSlice []byte) uint64 {
buffer := make([]byte, 4) buffer := make([]byte, 8)
copy(buffer, byteSlice) copy(buffer, byteSlice)
return binary.LittleEndian.Uint32(buffer) return binary.LittleEndian.Uint64(buffer)
} }