From 5136b835a2dd8ed4d49fd12a7615419a36e7cfa4 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 26 Apr 2023 14:33:12 +0200 Subject: [PATCH] Adding api/message/protobuf --- api/message/protobuf/codec.go | 88 +++++++++++++++ api/message/protobuf/codec_test.go | 175 +++++++++++++++++++++++++++++ api/message/protobuf/schema.proto | 20 ++++ 3 files changed, 283 insertions(+) create mode 100644 api/message/protobuf/codec.go create mode 100644 api/message/protobuf/codec_test.go create mode 100644 api/message/protobuf/schema.proto diff --git a/api/message/protobuf/codec.go b/api/message/protobuf/codec.go new file mode 100644 index 0000000..bb35fb3 --- /dev/null +++ b/api/message/protobuf/codec.go @@ -0,0 +1,88 @@ +package protobuf + +import ( + "errors" + + "github.com/eosswedenorg/thalos/api/message" + "google.golang.org/protobuf/proto" +) + +//go:generate protoc --go_out=$DST_DIR $SRC_DIR/addressbook.proto + +func Encode(v any) ([]byte, error) { + switch p := v.(type) { + case message.HeartBeat: + return EncodeHeartbeat(p) + case message.ActionTrace: + return EncodeActionTrace(p) + } + + return nil, errors.New("invalid type") +} + +func EncodeHeartbeat(hb message.HeartBeat) ([]byte, error) { + return proto.Marshal(&Heartbeat{ + BlockNum: hb.BlockNum, + HeadBlocknum: hb.HeadBlockNum, + LastIrreversibleBlocknum: hb.LastIrreversibleBlockNum, + }) +} + +func EncodeActionTrace(act message.ActionTrace) ([]byte, error) { + return proto.Marshal(&ActionTrace{ + TxId: act.TxID, + Name: act.Name, + Contract: act.Contract, + Receiver: act.Receiver, + Data: act.Data, + HexData: act.HexData, + }) +} + +func Decode(data []byte, v any) error { + switch p := v.(type) { + case *message.HeartBeat: + return DecodeHeartbeat(data, p) + case *message.ActionTrace: + return DecodeActionTrace(data, p) + } + return errors.New("invalid type") +} + +func DecodeHeartbeat(data []byte, hb *message.HeartBeat) error { + msg := &Heartbeat{} + if err := proto.Unmarshal(data, msg); err != nil { + return err + } + + hb.BlockNum = msg.BlockNum + hb.HeadBlockNum = msg.HeadBlocknum + hb.LastIrreversibleBlockNum = msg.LastIrreversibleBlocknum + + return nil +} + +func DecodeActionTrace(b []byte, act *message.ActionTrace) error { + msg := &ActionTrace{} + if err := proto.Unmarshal(b, msg); err != nil { + return err + } + + *act = message.ActionTrace{ + TxID: msg.TxId, + Name: msg.Name, + Contract: msg.Contract, + Receiver: msg.Receiver, + Data: msg.Data, + HexData: msg.HexData, + } + + return nil +} + +func init() { + message.RegisterCodec("protobuf", message.Codec{ + Encoder: Encode, + Decoder: Decode, + }) +} diff --git a/api/message/protobuf/codec_test.go b/api/message/protobuf/codec_test.go new file mode 100644 index 0000000..6f561fd --- /dev/null +++ b/api/message/protobuf/codec_test.go @@ -0,0 +1,175 @@ +package protobuf + +import ( + "testing" + + "github.com/eosswedenorg/thalos/api/message" + "github.com/stretchr/testify/assert" +) + +func TestMsgpack_EncodeHeartbeat(t *testing.T) { + msg := message.HeartBeat{ + BlockNum: 1234, + HeadBlockNum: 1235, + LastIrreversibleBlockNum: 1236, + } + + data, err := Encode(msg) + assert.NoError(t, err) + + assert.Equal(t, []byte{0x08, 0xd2, 0x09, 0x10, 0xd3, 0x09, 0x18, 0xd4, 0x09}, data) +} + +func TestMsgpack_DecodeHeartbeat(t *testing.T) { + data := []byte{0x8, 0xd2, 0x9, 0x10, 0xd3, 0x9, 0x18, 0xd4, 0x9} + + expected := message.HeartBeat{ + BlockNum: 1234, + HeadBlockNum: 1235, + LastIrreversibleBlockNum: 1236, + } + + msg := message.HeartBeat{} + err := Decode(data, &msg) + assert.NoError(t, err) + + assert.Equal(t, expected, msg) +} + +func TestMsgpack_EncodeActionTrace(t *testing.T) { + msg := message.ActionTrace{ + TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3", + Name: "sellitem", + Contract: "mygame", + Receiver: "eosio", + Data: []byte(` + "item": { + "id": "2131242", + "name": "Great Sword", + "str": "100", + "agi": "20", + "dur": "100", + "qual": "epic", + }, + "from": "account1", + "to": "account2", + "amount": "1000.0000 SCAM", + }`), + HexData: "d0fa1b2ab8a6fd0d1b0173df91aa9ffd277642d05780cf750", + } + data, err := Encode(msg) + assert.NoError(t, err) + + expected := []byte{ + 0x0a, 0x40, 0x65, 0x64, 0x63, 0x30, 0x36, 0x64, + 0x63, 0x65, 0x36, 0x33, 0x32, 0x30, 0x34, 0x35, + 0x39, 0x66, 0x64, 0x36, 0x34, 0x34, 0x37, 0x35, + 0x36, 0x39, 0x37, 0x32, 0x30, 0x34, 0x38, 0x64, + 0x61, 0x34, 0x35, 0x33, 0x62, 0x32, 0x38, 0x31, + 0x36, 0x62, 0x30, 0x61, 0x34, 0x33, 0x34, 0x63, + 0x33, 0x37, 0x64, 0x64, 0x66, 0x66, 0x64, 0x65, + 0x33, 0x36, 0x37, 0x37, 0x38, 0x64, 0x63, 0x61, + 0x62, 0x33, 0x12, 0x08, 0x73, 0x65, 0x6c, 0x6c, + 0x69, 0x74, 0x65, 0x6d, 0x1a, 0x06, 0x6d, 0x79, + 0x67, 0x61, 0x6d, 0x65, 0x22, 0x05, 0x65, 0x6f, + 0x73, 0x69, 0x6f, 0x2a, 0x9b, 0x01, 0x7b, 0x22, + 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3a, + 0x22, 0x31, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, + 0x30, 0x30, 0x20, 0x53, 0x43, 0x41, 0x4d, 0x22, + 0x2c, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x22, 0x3a, + 0x22, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, + 0x31, 0x22, 0x2c, 0x22, 0x69, 0x74, 0x65, 0x6d, + 0x22, 0x3a, 0x7b, 0x22, 0x61, 0x67, 0x69, 0x22, + 0x3a, 0x22, 0x32, 0x30, 0x22, 0x2c, 0x22, 0x64, + 0x75, 0x72, 0x22, 0x3a, 0x22, 0x31, 0x30, 0x30, + 0x22, 0x2c, 0x22, 0x69, 0x64, 0x22, 0x3a, 0x22, + 0x32, 0x31, 0x33, 0x31, 0x32, 0x34, 0x32, 0x22, + 0x2c, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, + 0x22, 0x47, 0x72, 0x65, 0x61, 0x74, 0x20, 0x53, + 0x77, 0x6f, 0x72, 0x64, 0x22, 0x2c, 0x22, 0x71, + 0x75, 0x61, 0x6c, 0x22, 0x3a, 0x22, 0x65, 0x70, + 0x69, 0x63, 0x22, 0x2c, 0x22, 0x73, 0x74, 0x72, + 0x22, 0x3a, 0x22, 0x31, 0x30, 0x30, 0x22, 0x7d, + 0x2c, 0x22, 0x74, 0x6f, 0x22, 0x3a, 0x22, 0x61, + 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0x22, + 0x7d, 0x32, 0x31, 0x64, 0x30, 0x66, 0x61, 0x31, + 0x62, 0x32, 0x61, 0x62, 0x38, 0x61, 0x36, 0x66, + 0x64, 0x30, 0x64, 0x31, 0x62, 0x30, 0x31, 0x37, + 0x33, 0x64, 0x66, 0x39, 0x31, 0x61, 0x61, 0x39, + 0x66, 0x66, 0x64, 0x32, 0x37, 0x37, 0x36, 0x34, + 0x32, 0x64, 0x30, 0x35, 0x37, 0x38, 0x30, 0x63, + 0x66, 0x37, 0x35, 0x30, + } + + assert.Equal(t, expected, data) +} + +func TestMsgpack_DecodeActionTrace(t *testing.T) { + data := []byte{ + 0x0a, 0x40, 0x65, 0x64, 0x63, 0x30, 0x36, 0x64, + 0x63, 0x65, 0x36, 0x33, 0x32, 0x30, 0x34, 0x35, + 0x39, 0x66, 0x64, 0x36, 0x34, 0x34, 0x37, 0x35, + 0x36, 0x39, 0x37, 0x32, 0x30, 0x34, 0x38, 0x64, + 0x61, 0x34, 0x35, 0x33, 0x62, 0x32, 0x38, 0x31, + 0x36, 0x62, 0x30, 0x61, 0x34, 0x33, 0x34, 0x63, + 0x33, 0x37, 0x64, 0x64, 0x66, 0x66, 0x64, 0x65, + 0x33, 0x36, 0x37, 0x37, 0x38, 0x64, 0x63, 0x61, + 0x62, 0x33, 0x12, 0x08, 0x73, 0x65, 0x6c, 0x6c, + 0x69, 0x74, 0x65, 0x6d, 0x1a, 0x06, 0x6d, 0x79, + 0x67, 0x61, 0x6d, 0x65, 0x22, 0x05, 0x65, 0x6f, + 0x73, 0x69, 0x6f, 0x2a, 0x9b, 0x01, 0x7b, 0x22, + 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3a, + 0x22, 0x31, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, + 0x30, 0x30, 0x20, 0x53, 0x43, 0x41, 0x4d, 0x22, + 0x2c, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x22, 0x3a, + 0x22, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, + 0x31, 0x22, 0x2c, 0x22, 0x69, 0x74, 0x65, 0x6d, + 0x22, 0x3a, 0x7b, 0x22, 0x61, 0x67, 0x69, 0x22, + 0x3a, 0x22, 0x32, 0x30, 0x22, 0x2c, 0x22, 0x64, + 0x75, 0x72, 0x22, 0x3a, 0x22, 0x31, 0x30, 0x30, + 0x22, 0x2c, 0x22, 0x69, 0x64, 0x22, 0x3a, 0x22, + 0x32, 0x31, 0x33, 0x31, 0x32, 0x34, 0x32, 0x22, + 0x2c, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, + 0x22, 0x47, 0x72, 0x65, 0x61, 0x74, 0x20, 0x53, + 0x77, 0x6f, 0x72, 0x64, 0x22, 0x2c, 0x22, 0x71, + 0x75, 0x61, 0x6c, 0x22, 0x3a, 0x22, 0x65, 0x70, + 0x69, 0x63, 0x22, 0x2c, 0x22, 0x73, 0x74, 0x72, + 0x22, 0x3a, 0x22, 0x31, 0x30, 0x30, 0x22, 0x7d, + 0x2c, 0x22, 0x74, 0x6f, 0x22, 0x3a, 0x22, 0x61, + 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0x22, + 0x7d, 0x32, 0x31, 0x64, 0x30, 0x66, 0x61, 0x31, + 0x62, 0x32, 0x61, 0x62, 0x38, 0x61, 0x36, 0x66, + 0x64, 0x30, 0x64, 0x31, 0x62, 0x30, 0x31, 0x37, + 0x33, 0x64, 0x66, 0x39, 0x31, 0x61, 0x61, 0x39, + 0x66, 0x66, 0x64, 0x32, 0x37, 0x37, 0x36, 0x34, + 0x32, 0x64, 0x30, 0x35, 0x37, 0x38, 0x30, 0x63, + 0x66, 0x37, 0x35, 0x30, + } + + expected := message.ActionTrace{ + TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3", + Name: "sellitem", + Contract: "mygame", + Receiver: "eosio", + Data: []byte(`{ + "item": { + "id": "2131242", + "name": "Great Sword", + "str": "100", + "agi": "20", + "dur": "100", + "qual": "epic", + }, + "from": "account1", + "to": "account2", + "amount": "1000.0000 SCAM", + }`), + HexData: "d0fa1b2ab8a6fd0d1b0173df91aa9ffd277642d05780cf750", + } + + res := message.ActionTrace{} + err := Decode(data, &res) + assert.NoError(t, err) + + assert.Equal(t, expected, res) +} diff --git a/api/message/protobuf/schema.proto b/api/message/protobuf/schema.proto new file mode 100644 index 0000000..0fcd1d9 --- /dev/null +++ b/api/message/protobuf/schema.proto @@ -0,0 +1,20 @@ +syntax = "proto3"; +package protobuf; + +option go_package = "message/protobuf"; + +message Heartbeat { + uint32 block_num = 1; + uint32 head_blocknum = 2; + uint32 last_irreversible_blocknum = 3; +} + +message ActionTrace { + string tx_id = 1; + string name = 2; + string contract = 3; + string receiver = 4; + // NOTE: Data is json encoded. + bytes data = 5; + string hex_data = 6; +}