From 0650845a0c8a8a539735468c74abd4622bf0686a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 26 Apr 2023 08:46:00 +0200 Subject: [PATCH] api/message/types.go: Change ActionTrace.Data to a byte slice and provide GetData() function that unmarshalls. Only the client code should care about the content. So just transmit it as a byte slice containing json data. --- api/message/msgpack/codec_test.go | 125 +++++++++++++++++++++--------- api/message/types.go | 13 +++- api/message/types_test.go | 23 ++++++ app/ship_processor.go | 4 +- 4 files changed, 123 insertions(+), 42 deletions(-) create mode 100644 api/message/types_test.go diff --git a/api/message/msgpack/codec_test.go b/api/message/msgpack/codec_test.go index b336308..8922719 100644 --- a/api/message/msgpack/codec_test.go +++ b/api/message/msgpack/codec_test.go @@ -1,6 +1,8 @@ package msgpack import ( + "encoding/hex" + "encoding/json" "testing" "github.com/eosswedenorg/thalos/api/message" @@ -11,68 +13,117 @@ import ( func TestMsgpack_EncodeActionTrace(t *testing.T) { RegisterGeneratedResolver() + dataJson, err := json.Marshal(map[string]interface{}{ + "item": map[string]interface{}{ + "id": "2131242", + "name": "Great Sword", + "str": "100", + "agi": "20", + "dur": "100", + "qual": "epic", + }, + "from": "account1", + "to": "account2", + "amount": "1000.0000 SCAM", + }) + + assert.NoError(t, err) + msg := message.ActionTrace{ TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3", Name: "sellitem", Contract: "mygame", Receiver: "eosio", - Data: map[interface{}]interface{}{ - "item": map[interface{}]interface{}{ - "id": "2131242", - "name": "Great Sword", - "str": "100", - "agi": "20", - "dur": "100", - "qual": "epic", - }, - "from": "account1", - "to": "account2", - "amount": "1000.0000 SCAM", - }, - HexData: "d0fa1b2ab8a6fd0d1b0173df91aa9ffd277642d05780cf750", + Data: dataJson, + HexData: "d0fa1b2ab8a6fd0d1b0173df91aa9ffd277642d05780cf750", } data, err := msgpack.Marshal(msg) assert.NoError(t, err) - res := message.ActionTrace{} - err = msgpack.Unmarshal(data, &res) - assert.NoError(t, err) + expected := []byte{ + 0x86, 0xa5, 0x74, 0x78, 0x5f, 0x69, 0x64, 0xd9, + 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, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa8, 0x73, + 0x65, 0x6c, 0x6c, 0x69, 0x74, 0x65, 0x6d, 0xa8, + 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, + 0xa6, 0x6d, 0x79, 0x67, 0x61, 0x6d, 0x65, 0xa8, + 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x72, + 0xa5, 0x65, 0x6f, 0x73, 0x69, 0x6f, 0xa4, 0x64, + 0x61, 0x74, 0x61, 0xc4, 0x9b, 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, + 0xa8, 0x68, 0x65, 0x78, 0x5f, 0x64, 0x61, 0x74, + 0x61, 0xd9, 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, msg, res) + assert.Equal(t, expected, data) } func TestMsgpack_Decode(t *testing.T) { RegisterGeneratedResolver() - data := []byte("\x86\xa5tx_id\xd9@edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3\xa4name\xa8sellitem\xa8contract\xa6mygame\xa8receiver\xa5eosio\xa4data\x84\xa4item\x86\xa4name\xabGreat Sword\xa3str\xa3100\xa3agi\xa220\xa3dur\xa3100\xa4qual\xa4epic\xa2id\xa72131242\xa4from\xa8account1\xa2to\xa8account2\xa6amount\xae1000.0000 SCAM\xa8hex_data\xd91d0fa1b2ab8a6fd0d1b0173df91aa9ffd277642d05780cf750") + data := []byte("\x86\xa5tx_id\xd9@edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3\xa4name\xa4drop\xa8contract\xa6mygame\xa8receiver\xa8account1\xa4dataċ{\"dropped_from_id\":674562,\"item\":{\"dur\":145,\"id\":49623,\"name\":\"Shadowmourne\",\"qual\":\"legendary\",\"sta\":198,\"str\":223},\"receiver\":\"account1\"}\xa8hex_data\xda\x01\x167b2264726f707065645f66726f6d5f6964223a3637343536322c226974656d223a7b22647572223a3134352c226964223a34393632332c226e616d65223a22536861646f776d6f75726e65222c227175616c223a226c6567656e64617279222c22737461223a3139382c22737472223a3232337d2c227265636569766572223a226163636f756e7431227d") + + dataJson, err := json.Marshal(map[string]interface{}{ + "item": map[string]interface{}{ + "id": 49623, + "name": "Shadowmourne", + "str": 223, + "sta": 198, + "dur": 145, + "qual": "legendary", + }, + "dropped_from_id": 674562, + "receiver": "account1", + }) + + assert.NoError(t, err) expected := message.ActionTrace{ TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3", - Name: "sellitem", + Name: "drop", Contract: "mygame", - Receiver: "eosio", - Data: map[interface{}]interface{}{ - "item": map[interface{}]interface{}{ - "id": "2131242", - "name": "Great Sword", - "str": "100", - "agi": "20", - "dur": "100", - "qual": "epic", - }, - "from": "account1", - "to": "account2", - "amount": "1000.0000 SCAM", - }, - HexData: "d0fa1b2ab8a6fd0d1b0173df91aa9ffd277642d05780cf750", + Receiver: "account1", + Data: dataJson, + HexData: hex.EncodeToString(dataJson), } res := message.ActionTrace{} - err := msgpack.Unmarshal(data, &res) + err = msgpack.Unmarshal(data, &res) assert.NoError(t, err) - assert.Equal(t, res, expected) + assert.Equal(t, expected, res) } func TestMsgpack_EncodeHeartbeat(t *testing.T) { diff --git a/api/message/types.go b/api/message/types.go index 04f9210..ae651bd 100644 --- a/api/message/types.go +++ b/api/message/types.go @@ -1,5 +1,7 @@ package message +import "encoding/json" + type HeartBeat struct { BlockNum uint32 `json:"blocknum" msgpack:"blocknum"` HeadBlockNum uint32 `json:"head_blocknum" msgpack:"head_blocknum"` @@ -15,7 +17,12 @@ type ActionTrace struct { // Contract account. Contract string `json:"contract" msgpack:"contract"` - Receiver string `json:"receiver" msgpack:"receiver"` - Data interface{} `json:"data" msgpack:"data"` - HexData string `json:"hex_data" msgpack:"hex_data"` + Receiver string `json:"receiver" msgpack:"receiver"` + Data []byte `json:"data" msgpack:"data"` + HexData string `json:"hex_data" msgpack:"hex_data"` +} + +func (act ActionTrace) GetData() (map[string]interface{}, error) { + data := map[string]interface{}{} + return data, json.Unmarshal(act.Data, &data) } diff --git a/api/message/types_test.go b/api/message/types_test.go new file mode 100644 index 0000000..5ac77a0 --- /dev/null +++ b/api/message/types_test.go @@ -0,0 +1,23 @@ +package message + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTypes_ActionTraceGetData(t *testing.T) { + act := ActionTrace{ + Data: []byte(`{"one": 1234, "two": "string"}`), + } + + actual, err := act.GetData() + assert.NoError(t, err) + + exptected := map[string]interface{}{ + "one": float64(1234), + "two": "string", + } + + assert.Equal(t, exptected, actual) +} diff --git a/app/ship_processor.go b/app/ship_processor.go index 267d84f..7bc3248 100644 --- a/app/ship_processor.go +++ b/app/ship_processor.go @@ -124,11 +124,11 @@ func (processor *ShipProcessor) processTraces(traces []*ship.TransactionTraceV0) ABI, err := processor.abi.GetAbi(act_trace.Act.Account) if err == nil { - v, err := abi.DecodeAction(ABI, act_trace.Act.Data, act_trace.Act.Name) + data, err := ABI.DecodeAction(act_trace.Act.Data, act_trace.Act.Name) if err != nil { log.WithError(err).Warn("Failed to decode action") } - act.Data = v + act.Data = data } else { log.WithError(err).Errorf("Failed to get abi for contract %s", act_trace.Act.Account) }