Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiments #141

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
protobuf updated support
pkieltyka committed Mar 16, 2025
commit bf8666e23733cde72b6e327e58b8becc10a3fa88
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -2,9 +2,13 @@ module github.com/go-bond/bond

go 1.24

replace github.com/0xsequence/go-sequence => /Users/peter/Dev/0xsequence/go-sequence

require (
github.com/0xsequence/go-sequence v0.50.3
github.com/bits-and-blooms/bloom/v3 v3.7.0
github.com/cockroachdb/pebble v0.0.0-20250315124616-793f8132e4f7
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.1
github.com/fatih/structs v1.1.0
github.com/fxamacker/cbor/v2 v2.7.0
@@ -18,9 +22,11 @@ require (
github.com/vmihailenco/msgpack/v5 v5.4.1
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
golang.org/x/sync v0.12.0
google.golang.org/protobuf v1.36.5
)

require (
github.com/0xsequence/ethkit v1.30.7 // indirect
github.com/DataDog/zstd v1.5.6 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.21.0 // indirect
@@ -32,10 +38,10 @@ require (
github.com/cockroachdb/swiss v0.0.0-20250304010804-34a2c6a59016 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/sentry-go v0.31.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/holiman/uint256 v1.3.2 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
@@ -52,9 +58,9 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/0xsequence/ethkit v1.30.7 h1:Z98+cGWcoWlpQvnAJMjxRjsPU77I1N6pdM6jUnFQDdY=
github.com/0xsequence/ethkit v1.30.7/go.mod h1:WMCHNUqQoWC9h7FYlz4xSl4sY1Q4S1WETF6ml0hdvJQ=
github.com/DataDog/zstd v1.5.6 h1:LbEglqepa/ipmmQJUDnSsfvA8e8IStVcGaFWDuxvGOY=
github.com/DataDog/zstd v1.5.6/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/aclements/go-perfevent v0.0.0-20240301234650-f7843625020f h1:JjxwchlOepwsUWcQwD2mLUAGE9aCp0/ehy6yCHFBOvo=
@@ -56,6 +58,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/holiman/uint256 v1.3.2 h1:a9EgMPSC1AAaj1SZL5zIQD3WbwTuHrMGOerLjGmM/TA=
github.com/holiman/uint256 v1.3.2/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
@@ -115,6 +119,8 @@ go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
39 changes: 21 additions & 18 deletions serializers/protobuf.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package serializers

import "fmt"
import (
"fmt"

"google.golang.org/protobuf/proto"
)

// ProtobufSerializer with support for vtproto (code-gen'd) protobuf,
// and standard protobuf (github.com/golang/protobuf/proto).
// and standard protobuf (google.golang.org/protobuf/proto).
//
// To use standard protobuf, set the Encoder and Decoder to
// proto.Marshal and proto.Unmarshal respectively. You can
// leave them unset if you use vtproto.
type ProtobufSerializer struct {
Encoder ProtobufMarshaler
Decoder ProtobufUnmarshaler
SkipVT bool
}

type ProtobufVTMmarshaler interface {
@@ -22,29 +27,25 @@ type ProtobufVTUnmarshaler interface {
}

type ProtobufMarshaler interface {
Marshal(m ProtobufMessageV1) ([]byte, error)
Marshal(m proto.Message) ([]byte, error)
}

type ProtobufUnmarshaler interface {
Unmarshal(b []byte, m ProtobufMessageV1) error
}

type ProtobufMessageV1 interface {
Reset()
String() string
ProtoMessage()
Unmarshal(b []byte, m proto.Message) error
}

func (s *ProtobufSerializer) Serialize(i interface{}) ([]byte, error) {
// check if protobuf type uses vtproto, which is code-generated
// marshaler which is faster than the default protobuf marshaler.
if v, ok := i.(ProtobufVTMmarshaler); ok {
return v.MarshalVT()
if !s.SkipVT {
if v, ok := i.(ProtobufVTMmarshaler); ok {
return v.MarshalVT()
}
}

// check if default marshaller is set, via github.com/golang/protobuf/proto
// check if default marshaller is set, via google.golang.org/protobuf/proto
if s.Encoder != nil {
if v, ok := i.(ProtobufMessageV1); ok {
if v, ok := i.(proto.Message); ok {
return s.Encoder.Marshal(v)
}
}
@@ -55,13 +56,15 @@ func (s *ProtobufSerializer) Serialize(i interface{}) ([]byte, error) {
func (s *ProtobufSerializer) Deserialize(b []byte, i interface{}) error {
// check if protobuf type uses vtproto, which is code-generated
// unmarshaler which is faster than the default protobuf unmarshaler.
if v, ok := i.(ProtobufVTUnmarshaler); ok {
return v.UnmarshalVT(b)
if !s.SkipVT {
if v, ok := i.(ProtobufVTUnmarshaler); ok {
return v.UnmarshalVT(b)
}
}

// check if default unmarshaller is set, via github.com/golang/protobuf/proto
// check if default unmarshaller is set, via google.golang.org/protobuf/proto
if s.Decoder != nil {
if v, ok := i.(ProtobufMessageV1); ok {
if v, ok := i.(proto.Message); ok {
return s.Decoder.Unmarshal(b, v)
}
}
349 changes: 349 additions & 0 deletions serializers/sample/pb/sample.pb.go
958 changes: 958 additions & 0 deletions serializers/sample/pb/sample_vtproto.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions serializers/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate protoc -I=./ --go_out=./ --go-vtproto_out=./ --go-vtproto_opt=features=marshal+unmarshal+size ./sample.proto
package sample
36 changes: 36 additions & 0 deletions serializers/sample/sample.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
syntax = "proto3";

package protobuf;

//option go_package = "github.com/yourusername/yourproject/serializers/protobuf";
option go_package = "./pb";

import "google/protobuf/timestamp.proto";

message BigInt {
bytes value = 1;
}

// TokenHistory represents blockchain token transaction history
message TokenHistory {
uint64 block_number = 1;
bytes block_hash = 2;
bytes account_address = 3;
bytes contract_address = 4;
uint32 contract_type = 5;
bytes from_address = 6;
bytes to_address = 7;
bytes txn_hash = 8;
uint32 txn_index = 9;
uint32 txn_log_index = 10;

// deprecated in favour of token_ids and amounts
string log_data = 11;

// Timestamp when the event occurred
google.protobuf.Timestamp ts = 12;

// Lists for token IDs and amounts
repeated BigInt token_ids = 13;
repeated BigInt amounts = 14;
}
172 changes: 172 additions & 0 deletions serializers/serializers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package serializers_test

import (
"fmt"
"testing"
"time"

"github.com/0xsequence/go-sequence/lib/prototyp"

Check failure on line 8 in serializers/serializers_test.go

GitHub Actions / build

github.com/0xsequence/go-sequence@v0.50.3: replacement directory /Users/peter/Dev/0xsequence/go-sequence does not exist
"github.com/davecgh/go-spew/spew"
"github.com/go-bond/bond/serializers"
"github.com/go-bond/bond/serializers/sample/pb"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

// TODO: lets inspect how cbor is marshalling this.. maybe we can make it
// more efficient out of the gate..? by having it implement the encoding.BinaryMarshaler
// interface. ....??..
//
// .. then lets review prototyp.BigInt

type TokenHistory struct {
BlockNumber uint64 `json:"blockNumber" cbor:"1"`
BlockHash prototyp.Hash `json:"blockHash" cbor:"2"`
AccountAddress prototyp.Hash `json:"accountAddress" cbor:"3"`
ContractAddress prototyp.Hash `json:"contractAddress" cbor:"4"`
ContractType uint32 `json:"contractType" cbor:"5"`
FromAddress prototyp.Hash `json:"fromAddress" cbor:"6"`
ToAddress prototyp.Hash `json:"toAddress" cbor:"7"`
TxnHash prototyp.Hash `json:"txnHash" cbor:"8"`
TxnIndex uint `json:"txnIndex" cbor:"9"`
TxnLogIndex uint `json:"txnLogIndex" cbor:"a"`
// deprecated in favour of TokenIDs and Amounts
LogData string `json:"logData" cbor:"b"`
TokenIDs []prototyp.BigInt `json:"tokenIDs" cbor:"d"`
Amounts []prototyp.BigInt `json:"Amounts" cbor:"e"`
TS time.Time `json:"ts" cbor:"c"`
}

// TODO: we need to add the Hash and BigInt types here ..

func TestSerializeCBOR(t *testing.T) {
serializer := &serializers.CBORSerializer{}
tokenHistory := &TokenHistory{
BlockNumber: 1,
BlockHash: prototyp.HashFromString("0x1234567890abcdef"),
AccountAddress: prototyp.HashFromString("0x2"),
ContractAddress: prototyp.HashFromString("0x3"),
ContractType: 0x4,
FromAddress: prototyp.HashFromString("0x5"),
ToAddress: prototyp.HashFromString("0x6"),
TxnHash: prototyp.HashFromString("0x7"),
TxnIndex: 1,
TxnLogIndex: 2,
LogData: "logData",
TokenIDs: []prototyp.BigInt{prototyp.NewBigIntFromNumberString("888888888888888888888888")},
Amounts: []prototyp.BigInt{prototyp.NewBigIntFromNumberString("999999999999999999999999")},
TS: time.Now(),
}

serialized, err := serializer.Serialize(tokenHistory)
if err != nil {
t.Fatalf("failed to serialize token history: %v", err)
}

fmt.Println("serialized num of bytes:", len(serialized))
fmt.Printf("serialized: %v\n", serialized)
spew.Dump(serialized)

// assertion to ensure that the serialized data is less than 90 bytes
// to confirm that the cbor serialization is efficient.
//
// if this test fails, we need to review the cbor serialization as
// the underlying library may have changed its behavior.
require.True(t, len(serialized) <= 90)
}

func TestSerializeProtobuf(t *testing.T) {
protoMarshaller := proto.MarshalOptions{}
protoUnmarshaller := proto.UnmarshalOptions{}

serializer := &serializers.ProtobufSerializer{
Encoder: protoMarshaller,
Decoder: protoUnmarshaller,
}
tokenHistory := &pb.TokenHistory{
BlockNumber: 1,
BlockHash: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5}, // prototyp.HashFromString("0x1"),
AccountAddress: []byte{2}, // prototyp.HashFromString("0x2"),
ContractAddress: []byte{3}, // prototyp.HashFromString("0x3"),
ContractType: 0x4,
FromAddress: []byte{5}, // prototyp.HashFromString("0x5"),
ToAddress: []byte{6}, // prototyp.HashFromString("0x6"),
TxnHash: []byte{7}, // prototyp.HashFromString("0x7"),
TxnIndex: 1,
TxnLogIndex: 2,
LogData: "logData",
TokenIds: []*pb.BigInt{{Value: prototyp.NewBigIntFromNumberString("888888888888888888888888").Bytes()}}, //[]prototyp.BigInt{prototyp.NewBigIntFromNumberString("8")},
Amounts: []*pb.BigInt{{Value: prototyp.NewBigIntFromNumberString("999999999999999999999999").Bytes()}}, //[]prototyp.BigInt{prototyp.NewBigIntFromNumberString("9")},
Ts: timestamppb.New(time.Now()),
}

serialized, err := serializer.Serialize(tokenHistory)
if err != nil {
t.Fatalf("failed to serialize token history: %v", err)
}

fmt.Println("serialized num of bytes:", len(serialized))
fmt.Printf("serialized: %v\n", serialized)
spew.Dump(serialized)
}

func BenchmarkSerializeCBOR(b *testing.B) {
serializer := &serializers.CBORSerializer{}
tokenHistory := &TokenHistory{
BlockNumber: 1,
BlockHash: prototyp.HashFromString("0x1"),
AccountAddress: prototyp.HashFromString("0x2"),
ContractAddress: prototyp.HashFromString("0x3"),
ContractType: 0x4,
FromAddress: prototyp.HashFromString("0x5"),
ToAddress: prototyp.HashFromString("0x6"),
TxnHash: prototyp.HashFromString("0x7"),
TxnIndex: 1,
TxnLogIndex: 2,
LogData: "logData",
TokenIDs: []prototyp.BigInt{prototyp.NewBigIntFromNumberString("888888888888888888888888")},
Amounts: []prototyp.BigInt{prototyp.NewBigIntFromNumberString("999999999999999999999999")},
TS: time.Now(),
}

for i := 0; i < b.N; i++ {
_, err := serializer.Serialize(tokenHistory)
if err != nil {
b.Fatalf("failed to serialize token history: %v", err)
}
}
}

func BenchmarkSerializeProtobuf(b *testing.B) {
protoMarshaller := proto.MarshalOptions{}
protoUnmarshaller := proto.UnmarshalOptions{}

serializer := &serializers.ProtobufSerializer{
Encoder: protoMarshaller,
Decoder: protoUnmarshaller,
}
tokenHistory := &pb.TokenHistory{
BlockNumber: 1,
BlockHash: []byte{1}, // prototyp.HashFromString("0x1"),
AccountAddress: []byte{2}, // prototyp.HashFromString("0x2"),
ContractAddress: []byte{3}, // prototyp.HashFromString("0x3"),
ContractType: 0x4,
FromAddress: []byte{5}, // prototyp.HashFromString("0x5"),
ToAddress: []byte{6}, // prototyp.HashFromString("0x6"),
TxnHash: []byte{7}, // prototyp.HashFromString("0x7"),
TxnIndex: 1,
TxnLogIndex: 2,
LogData: "logData",
TokenIds: []*pb.BigInt{{Value: []byte{8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}}}, //[]prototyp.BigInt{prototyp.NewBigIntFromNumberString("8")},
Amounts: []*pb.BigInt{{Value: []byte{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}}}, //[]prototyp.BigInt{prototyp.NewBigIntFromNumberString("9")},
Ts: timestamppb.New(time.Now()),
}

for i := 0; i < b.N; i++ {
_, err := serializer.Serialize(tokenHistory)
if err != nil {
b.Fatalf("failed to serialize token history: %v", err)
}
}
}