Skip to content

Commit

Permalink
support of both endian platform.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryogrid committed May 2, 2024
1 parent 7ba14d3 commit 95c56d2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
4 changes: 2 additions & 2 deletions core/event_data_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (l *EventDataLogger) WriteLog(lfile *LogFile, data []byte) error {
lfile.file.Seek(lfile.curSize, 0)
}
sizeBuf := make([]byte, 0)
sizeBuf = binary.LittleEndian.AppendUint32(sizeBuf, uint32(len(data)))
sizeBuf = binary.BigEndian.AppendUint32(sizeBuf, uint32(len(data)))
// each log entry is prefixed with a 4-byte size
n, err := lfile.file.Write(sizeBuf)
if err != nil || n != 4 {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (l *EventDataLogger) ReadLog(lfile *LogFile) (int, []byte, error) {
}
}
lfile.lastReadPos += int64(4)
size := int(binary.LittleEndian.Uint32(sizeBuf))
size := int(binary.BigEndian.Uint32(sizeBuf))
data := make([]byte, size)
n, err = lfile.file.Read(data)
if err != nil || n != size {
Expand Down
4 changes: 1 addition & 3 deletions core/nuts_db_data_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (n *NutsDBDataManager) StoreEvent(evt *schema.Np2pEvent) {

func (n *NutsDBDataManager) getEventByTimestampBytes(tsBytes []byte) *schema.Np2pEvent {
var ret *schema.Np2pEvent
ts := float64(binary.LittleEndian.Uint64(tsBytes))
ts := float64(binary.BigEndian.Uint64(tsBytes))
if err := n.db.View(func(tx *nutsdb.Tx) error {
if entries, err2 := tx.ZRangeByScore(EventListTimeKey, []byte("time"), ts, ts, nil); err2 != nil {
return err2
Expand Down Expand Up @@ -167,8 +167,6 @@ func (n *NutsDBDataManager) StoreProfile(evt *schema.Np2pEvent) {
if err := n.db.Update(func(tx *nutsdb.Tx) error {
tmpPubKey := evt.Pubkey
key := tmpPubKey[len(tmpPubKey)-8:]
// little endian
slices.Reverse(key)
return tx.Put(ProfEvtIdxMap, key, np2p_util.ConvInt64ToBytes(evt.Created_at), nutsdb.Persistent)
}); err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion core/on_memory_data_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (dman *OnMemoryDataManager) RemoveReSendNeededEvent(_resendEvt *schema.Rese
dman.reSendNeededEvtList.Remove(evt.Created_at)
dman.reSendNeededEvtListMtx.Unlock()
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(evt.Created_at))
binary.BigEndian.PutUint64(buf, uint64(evt.Created_at))
dman.EvtLogger.WriteLog(dman.EvtLogger.reSendFinishedEvtLogFile, buf)
}

Expand Down
2 changes: 1 addition & 1 deletion core/recovery_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (rm *RecoveryManager) Recover() {
break
}

createdAtUint := binary.LittleEndian.Uint64(buf)
createdAtUint := binary.BigEndian.Uint64(buf)
createdAt := int64(createdAtUint)
tmpFinishedMap[createdAt] = struct{}{}
_, buf, err = _edlogger.ReadLog(_edlogger.reSendFinishedEvtLogFile)
Expand Down
7 changes: 4 additions & 3 deletions np2p_util/np2p_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func IsHit(prob float64) bool {
}

func GetLower64bitUint(bytes [np2p_const.PubkeySize]byte) uint64 {
return binary.LittleEndian.Uint64(bytes[:8])
return binary.BigEndian.Uint64(bytes[np2p_const.PubkeySize-8:])
}

func GzipCompless(data []byte) []byte {
Expand Down Expand Up @@ -151,6 +151,7 @@ func GetUint64FromHexPubKeyStr(pubKeyStr string) uint64 {
return binary.BigEndian.Uint64(pubKeyBytes[len(pubKeyBytes)-8:])
}

// byte array is BigEndian
func StrTo32BytesArr(pubKeyStr string) [32]byte {
bytes, err := hex.DecodeString(pubKeyStr)
if err != nil {
Expand All @@ -172,12 +173,12 @@ func ConvStringArrToTagArr(tagStrArr [][]string) []nostr.Tag {

func ConvInt64ToBytes(val int64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(val))
binary.BigEndian.PutUint64(buf, uint64(val))
return buf
}

func ConvUint64ToBytes(val uint64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, val)
binary.BigEndian.PutUint64(buf, val)
return buf
}
26 changes: 12 additions & 14 deletions schema/np2p_event.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package schema

import (
"github.com/holiman/uint256"
"github.com/ryogrid/nostrp2p/np2p_const"
"github.com/vmihailenco/msgpack/v5"
"math/big"
)

type TagElem []byte
Expand All @@ -20,18 +18,18 @@ type Np2pEvent struct {
Sig *[np2p_const.SignatureSize]byte // BigEndian, 64-bytes integr of the signature of the sha256 hash of the serialized event data
}

func (e *Np2pEvent) GetPubkey() *big.Int {
fixed256 := uint256.NewInt(0)
return fixed256.SetBytes(e.Pubkey[:]).ToBig()
}

func (e *Np2pEvent) SetPubkey(pubkey *big.Int) {
fixed256, isOverflow := uint256.FromBig(pubkey)
if isOverflow {
panic("overflow")
}
fixed256.WriteToArray32(&e.Pubkey)
}
//func (e *Np2pEvent) GetPubkey() *big.Int {
// fixed256 := uint256.NewInt(0)
// return fixed256.SetBytes(e.Pubkey[:]).ToBig()
//}
//
//func (e *Np2pEvent) SetPubkey(pubkey *big.Int) {
// fixed256, isOverflow := uint256.FromBig(pubkey)
// if isOverflow {
// panic("overflow")
// }
// fixed256.WriteToArray32(&e.Pubkey)
//}

func (e *Np2pEvent) Encode() []byte {
b, err := msgpack.Marshal(e)
Expand Down

0 comments on commit 95c56d2

Please sign in to comment.