chore: a bit of refactor to simplify a little just move things

This commit is contained in:
Ivan Folgueira Bande 2026-01-25 22:59:03 +01:00
parent 66722e0a35
commit 78679c8369
No known key found for this signature in database
GPG Key ID: 3C117481F89E24A7
9 changed files with 22 additions and 47 deletions

View File

@ -2,15 +2,13 @@ package common
import (
"encoding/json"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
)
// Envelope contains information about the pubsub topic of a WakuMessage
// and a hash used to identify a message based on the bytes of a WakuMessage
// protobuffer
type Envelope struct {
msg *pb.WakuMessage
msg *WakuMessage
topic string
hash MessageHash
}
@ -39,7 +37,7 @@ func (e *Envelope) UnmarshalJSON(input []byte) error {
return err
}
e.msg = &pb.WakuMessage{
e.msg = &WakuMessage{
Payload: wakuEnvelope.WakuMessage.Payload,
ContentTopic: wakuEnvelope.WakuMessage.ContentTopic,
Version: wakuEnvelope.WakuMessage.Version,
@ -54,7 +52,7 @@ func (e *Envelope) UnmarshalJSON(input []byte) error {
return nil
}
func (e *Envelope) Message() *pb.WakuMessage {
func (e *Envelope) Message() *WakuMessage {
return e.msg
}

View File

@ -1,13 +1,14 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// 14/WAKU2-MESSAGE rfc: https://rfc.vac.dev/spec/14/
package pb
package common
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
)
const (
@ -42,6 +43,12 @@ func (x *WakuMessage) String() string {
return protoimpl.X.MessageStringOf(x)
}
// Hash calculates the hash of a waku message
func (msg *WakuMessage) Hash(pubsubTopic string) MessageHash {
hash := SHA256([]byte(pubsubTopic), msg.Payload, []byte(msg.ContentTopic), msg.Meta, toBytes(msg.GetTimestamp()))
return ToMessageHash(hash)
}
func (*WakuMessage) ProtoMessage() {}
func (x *WakuMessage) ProtoReflect() protoreflect.Message {

View File

@ -7,10 +7,6 @@ import (
"sync"
"github.com/ethereum/go-ethereum/common/hexutil"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
)
// MessageHash represents an unique identifier for a message within a pubsub topic
@ -49,27 +45,8 @@ func SHA256(data ...[]byte) []byte {
return h.Sum(nil)
}
// Hash calculates the hash of a waku message
func (msg *pb.WakuMessage) Hash(pubsubTopic string) MessageHash {
hash := SHA256([]byte(pubsubTopic), msg.Payload, []byte(msg.ContentTopic), msg.Meta, toBytes(msg.GetTimestamp()))
return ToMessageHash(hash)
}
func toBytes(i int64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
return b
}
func (msg *pb.WakuMessage) LogFields(pubsubTopic string) []zapcore.Field {
return []zapcore.Field{
zap.Stringer("hash", msg.Hash(pubsubTopic)),
zap.String("pubsubTopic", pubsubTopic),
zap.String("contentTopic", msg.ContentTopic),
zap.Int64("timestamp", msg.GetTimestamp()),
}
}
func (msg *pb.WakuMessage) Logger(logger *zap.Logger, pubsubTopic string) *zap.Logger {
return logger.With(msg.LogFields(pubsubTopic)...)
}

View File

@ -359,7 +359,6 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
libp2pproto "github.com/libp2p/go-libp2p/core/protocol"
"github.com/multiformats/go-multiaddr"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/utils"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/common"

View File

@ -16,7 +16,6 @@ import (
"github.com/logos-messaging/logos-messaging-go-bindings/waku/common"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/store"
)

View File

@ -18,7 +18,6 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/logos-messaging/logos-messaging-go-bindings/utils"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/common"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
"google.golang.org/protobuf/proto"
)

View File

@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
)
// KeyKind indicates the type of encryption to apply
@ -90,7 +89,7 @@ func (payload Payload) Encode(version uint32) ([]byte, error) {
return nil, errors.New("unsupported wakumessage version")
}
func EncodeWakuMessage(message *pb.WakuMessage, keyInfo *KeyInfo) error {
func EncodeWakuMessage(message *WakuMessage, keyInfo *KeyInfo) error {
msgPayload := message.Payload
payload := Payload{
Data: msgPayload,
@ -108,7 +107,7 @@ func EncodeWakuMessage(message *pb.WakuMessage, keyInfo *KeyInfo) error {
// DecodePayload decodes a WakuMessage depending on the version parameter.
// 0 for raw unencrypted data, and 1 for using WakuV1 decoding
func DecodePayload(message *pb.WakuMessage, keyInfo *KeyInfo) (*DecodedPayload, error) {
func DecodePayload(message *WakuMessage, keyInfo *KeyInfo) (*DecodedPayload, error) {
switch message.GetVersion() {
case uint32(0):
return &DecodedPayload{Data: message.Payload}, nil
@ -153,7 +152,7 @@ func DecodePayload(message *pb.WakuMessage, keyInfo *KeyInfo) (*DecodedPayload,
return nil, errors.New("unsupported wakumessage version")
}
func DecodeWakuMessage(message *pb.WakuMessage, keyInfo *KeyInfo) error {
func DecodeWakuMessage(message *WakuMessage, keyInfo *KeyInfo) error {
decodedPayload, err := DecodePayload(message, keyInfo)
if err != nil {
return err

View File

@ -9,7 +9,6 @@ import (
"github.com/cenkalti/backoff/v3"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/common"
"github.com/stretchr/testify/require"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
"google.golang.org/protobuf/proto"
)

View File

@ -6,10 +6,8 @@ import (
"time"
"github.com/cenkalti/backoff/v3"
"github.com/stretchr/testify/require"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/pb"
"github.com/logos-messaging/logos-messaging-go-bindings/waku/common"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
@ -57,7 +55,7 @@ func TestStoreQuery3Nodes(t *testing.T) {
err = WaitForAutoConnection([]*WakuNode{node1, node2, node3})
require.NoError(t, err, "Nodes did not connect within timeout")
Debug("Publishing message from Node1 using RelayPublish")
message := node1.CreateMessage(&pb.WakuMessage{
message := node1.CreateMessage(&WakuMessage{
Payload: []byte("test-message"),
ContentTopic: "test-content-topic",
Timestamp: proto.Int64(time.Now().UnixNano()),
@ -136,7 +134,7 @@ func TestStoreQueryMultipleMessages(t *testing.T) {
Debug("Publishing %d messages from Node1 using RelayPublish", numMessages)
for i := 0; i < numMessages; i++ {
message := node1.CreateMessage(&pb.WakuMessage{
message := node1.CreateMessage(&WakuMessage{
Payload: []byte(fmt.Sprintf("message-%d", i)),
ContentTopic: "test-content-topic",
Timestamp: proto.Int64(time.Now().UnixNano()),
@ -226,7 +224,7 @@ func TestStoreQueryWith5Pagination(t *testing.T) {
Debug("Publishing %d messages from Node1 using RelayPublish", numMessages)
for i := 0; i < numMessages; i++ {
message := node1.CreateMessage(&pb.WakuMessage{
message := node1.CreateMessage(&WakuMessage{
Payload: []byte(fmt.Sprintf("message-%d", i)),
ContentTopic: "test-content-topic",
Timestamp: proto.Int64(time.Now().UnixNano()),
@ -308,7 +306,7 @@ func TestStoreQueryWithPaginationMultiplePages(t *testing.T) {
Debug("Publishing %d messages from Node1 using RelayPublish", numMessages)
for i := 0; i < numMessages; i++ {
message := node1.CreateMessage(&pb.WakuMessage{
message := node1.CreateMessage(&WakuMessage{
Payload: []byte(fmt.Sprintf("message-%d", i)),
ContentTopic: "test-content-topic",
Timestamp: proto.Int64(time.Now().UnixNano()),
@ -416,7 +414,7 @@ func TestStoreQueryWithPaginationReverseOrder(t *testing.T) {
queryTimestamp := proto.Int64(time.Now().UnixNano())
Debug("Publishing %d messages from Node1 using RelayPublish", numMessages)
for i := 0; i < numMessages; i++ {
message := node1.CreateMessage(&pb.WakuMessage{
message := node1.CreateMessage(&WakuMessage{
Payload: []byte(fmt.Sprintf("message-%d", i)),
ContentTopic: "test-content-topic",
Timestamp: proto.Int64(time.Now().UnixNano()),