1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-07-02 11:43:40 +02:00

api/message/types.go: Change ActionTrace.Data from []byte to interface{}

This commit is contained in:
Henrik Hautakoski 2023-05-09 16:46:12 +02:00
parent df840da128
commit f925692354
4 changed files with 85 additions and 104 deletions

View file

@ -1,7 +1,6 @@
package json package json
import ( import (
"encoding/json"
"testing" "testing"
"time" "time"
@ -10,14 +9,6 @@ import (
) )
func TestJson_EncodeActionTrace(t *testing.T) { func TestJson_EncodeActionTrace(t *testing.T) {
dataJson, err := json.Marshal(map[string]string{
"from": "account1",
"to": "account2",
"quantity": "1000.0000 WAX",
})
assert.NoError(t, err)
msg := message.ActionTrace{ msg := message.ActionTrace{
TxID: "ed3b8e853647971cf8296f004c3a1aeac255f082b2cb3c12cc3222e2d7c174ab", TxID: "ed3b8e853647971cf8296f004c3a1aeac255f082b2cb3c12cc3222e2d7c174ab",
BlockNum: 267372365, BlockNum: 267372365,
@ -25,8 +16,11 @@ func TestJson_EncodeActionTrace(t *testing.T) {
Name: "transfer", Name: "transfer",
Contract: "eosio", Contract: "eosio",
Receiver: "account2", Receiver: "account2",
Data: dataJson, Data: map[string]interface{}{
"from": "account1",
"to": "account2",
"quantity": "1000.0000 WAX",
},
Authorization: []message.PermissionLevel{ Authorization: []message.PermissionLevel{
{Actor: "account1", Permission: "active"}, {Actor: "account1", Permission: "active"},
}, },
@ -35,7 +29,7 @@ func TestJson_EncodeActionTrace(t *testing.T) {
Return: []byte{0xde, 0xad, 0xbe, 0xef}, Return: []byte{0xde, 0xad, 0xbe, 0xef},
} }
expected := `{"tx_id":"ed3b8e853647971cf8296f004c3a1aeac255f082b2cb3c12cc3222e2d7c174ab","blocknum":267372365,"blocktimestamp":"2003-03-21T17:23:09.500","name":"transfer","contract":"eosio","receiver":"account2","data":"eyJmcm9tIjoiYWNjb3VudDEiLCJxdWFudGl0eSI6IjEwMDAuMDAwMCBXQVgiLCJ0byI6ImFjY291bnQyIn0=","authorization":[{"actor":"account1","permission":"active"}],"except":"errstr","error":2,"return":"3q2+7w=="}` expected := `{"tx_id":"ed3b8e853647971cf8296f004c3a1aeac255f082b2cb3c12cc3222e2d7c174ab","blocknum":267372365,"blocktimestamp":"2003-03-21T17:23:09.500","name":"transfer","contract":"eosio","receiver":"account2","data":{"from":"account1","quantity":"1000.0000 WAX","to":"account2"},"authorization":[{"actor":"account1","permission":"active"}],"except":"errstr","error":2,"return":"3q2+7w=="}`
data, err := encoder(msg) data, err := encoder(msg)
assert.NoError(t, err) assert.NoError(t, err)
@ -43,14 +37,6 @@ func TestJson_EncodeActionTrace(t *testing.T) {
} }
func TestJson_DecodeActionTrace(t *testing.T) { func TestJson_DecodeActionTrace(t *testing.T) {
dataJson, err := json.Marshal(map[string]string{
"from": "account1",
"to": "account2",
"quantity": "1000.0000 WAX",
})
assert.NoError(t, err)
expected := message.ActionTrace{ expected := message.ActionTrace{
TxID: "952989f7464237b6cf9926e533ecd331df6794ed07564bd052bc368cbd65b4bc", TxID: "952989f7464237b6cf9926e533ecd331df6794ed07564bd052bc368cbd65b4bc",
BlockNum: 8723971, BlockNum: 8723971,
@ -58,7 +44,11 @@ func TestJson_DecodeActionTrace(t *testing.T) {
Name: "transfer", Name: "transfer",
Contract: "eosio", Contract: "eosio",
Receiver: "account2", Receiver: "account2",
Data: dataJson, Data: map[string]interface{}{
"from": "account1",
"to": "account2",
"quantity": "1000.0000 WAX",
},
Authorization: []message.PermissionLevel{ Authorization: []message.PermissionLevel{
{Actor: "account1", Permission: "active"}, {Actor: "account1", Permission: "active"},
}, },
@ -67,10 +57,10 @@ func TestJson_DecodeActionTrace(t *testing.T) {
Return: []byte{0xde, 0xad, 0xbe, 0xef}, Return: []byte{0xde, 0xad, 0xbe, 0xef},
} }
input := `{"tx_id":"952989f7464237b6cf9926e533ecd331df6794ed07564bd052bc368cbd65b4bc","blocknum":8723971,"blocktimestamp":"2024-06-21T08:08:26.500","name":"transfer","contract":"eosio","receiver":"account2","data":"eyJmcm9tIjoiYWNjb3VudDEiLCJxdWFudGl0eSI6IjEwMDAuMDAwMCBXQVgiLCJ0byI6ImFjY291bnQyIn0=","authorization":[{"actor":"account1","permission":"active"}],"except":"errstr","error":2,"return":"3q2+7w=="}` input := `{"tx_id":"952989f7464237b6cf9926e533ecd331df6794ed07564bd052bc368cbd65b4bc","blocknum":8723971,"blocktimestamp":"2024-06-21T08:08:26.500","name":"transfer","contract":"eosio","receiver":"account2","data":{"from":"account1","quantity":"1000.0000 WAX","to":"account2"},"authorization":[{"actor":"account1","permission":"active"}],"except":"errstr","error":2,"return":"3q2+7w=="}`
msg := message.ActionTrace{} msg := message.ActionTrace{}
err = decoder([]byte(input), &msg) err := decoder([]byte(input), &msg)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, msg) assert.Equal(t, expected, msg)
} }

View file

@ -1,7 +1,6 @@
package msgpack package msgpack
import ( import (
"encoding/json"
"testing" "testing"
"time" "time"
@ -10,22 +9,6 @@ import (
) )
func TestMsgpack_EncodeActionTrace(t *testing.T) { func TestMsgpack_EncodeActionTrace(t *testing.T) {
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{ msg := message.ActionTrace{
TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3", TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3",
BlockNum: 12345, BlockNum: 12345,
@ -33,7 +16,19 @@ func TestMsgpack_EncodeActionTrace(t *testing.T) {
Name: "sellitem", Name: "sellitem",
Contract: "mygame", Contract: "mygame",
Receiver: "eosio", Receiver: "eosio",
Data: dataJson, Data: map[string]any{
"item": map[string]any{
"id": 2131242,
"name": "Great Sword",
"str": "100",
"agi": "20",
"dur": "100",
"qual": "epic",
},
"from": "account1",
"to": "account2",
"amount": "1000.0000 SCAM",
},
Authorization: []message.PermissionLevel{ Authorization: []message.PermissionLevel{
{Actor: "mygame", Permission: "active"}, {Actor: "mygame", Permission: "active"},
}, },
@ -59,65 +54,45 @@ func TestMsgpack_EncodeActionTrace(t *testing.T) {
0x00, 0x65, 0x4e, 0x19, 0xff, 0xa8, 0x63, 0x6f, 0x00, 0x65, 0x4e, 0x19, 0xff, 0xa8, 0x63, 0x6f,
0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0xa6, 0x6d, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0xa6, 0x6d,
0x79, 0x67, 0x61, 0x6d, 0x65, 0xa4, 0x64, 0x61, 0x79, 0x67, 0x61, 0x6d, 0x65, 0xa4, 0x64, 0x61,
0x74, 0x61, 0xc4, 0x9b, 0x7b, 0x22, 0x61, 0x6d, 0x74, 0x61, 0x84, 0xa6, 0x61, 0x6d, 0x6f, 0x75,
0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3a, 0x22, 0x31, 0x6e, 0x74, 0xae, 0x31, 0x30, 0x30, 0x30, 0x2e,
0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x20, 0x53, 0x43, 0x41,
0x20, 0x53, 0x43, 0x41, 0x4d, 0x22, 0x2c, 0x22, 0x4d, 0xa4, 0x66, 0x72, 0x6f, 0x6d, 0xa8, 0x61,
0x66, 0x72, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x31, 0xa4,
0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x31, 0x22, 0x69, 0x74, 0x65, 0x6d, 0x86, 0xa3, 0x61, 0x67,
0x2c, 0x22, 0x69, 0x74, 0x65, 0x6d, 0x22, 0x3a, 0x69, 0xa2, 0x32, 0x30, 0xa3, 0x64, 0x75, 0x72,
0x7b, 0x22, 0x61, 0x67, 0x69, 0x22, 0x3a, 0x22, 0xa3, 0x31, 0x30, 0x30, 0xa2, 0x69, 0x64, 0xd2,
0x32, 0x30, 0x22, 0x2c, 0x22, 0x64, 0x75, 0x72, 0x00, 0x20, 0x85, 0x2a, 0xa4, 0x6e, 0x61, 0x6d,
0x22, 0x3a, 0x22, 0x31, 0x30, 0x30, 0x22, 0x2c, 0x65, 0xab, 0x47, 0x72, 0x65, 0x61, 0x74, 0x20,
0x22, 0x69, 0x64, 0x22, 0x3a, 0x22, 0x32, 0x31, 0x53, 0x77, 0x6f, 0x72, 0x64, 0xa4, 0x71, 0x75,
0x33, 0x31, 0x32, 0x34, 0x32, 0x22, 0x2c, 0x22, 0x61, 0x6c, 0xa4, 0x65, 0x70, 0x69, 0x63, 0xa3,
0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x47, 0x73, 0x74, 0x72, 0xa3, 0x31, 0x30, 0x30, 0xa2,
0x72, 0x65, 0x61, 0x74, 0x20, 0x53, 0x77, 0x6f, 0x74, 0x6f, 0xa8, 0x61, 0x63, 0x63, 0x6f, 0x75,
0x72, 0x64, 0x22, 0x2c, 0x22, 0x71, 0x75, 0x61, 0x6e, 0x74, 0x32, 0xa5, 0x65, 0x72, 0x72, 0x6f,
0x6c, 0x22, 0x3a, 0x22, 0x65, 0x70, 0x69, 0x63, 0x72, 0x02, 0xa6, 0x65, 0x78, 0x63, 0x65, 0x70,
0x22, 0x2c, 0x22, 0x73, 0x74, 0x72, 0x22, 0x3a, 0x74, 0xa6, 0x65, 0x72, 0x72, 0x73, 0x74, 0x72,
0x22, 0x31, 0x30, 0x30, 0x22, 0x7d, 0x2c, 0x22, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa8, 0x73, 0x65,
0x74, 0x6f, 0x22, 0x3a, 0x22, 0x61, 0x63, 0x63, 0x6c, 0x6c, 0x69, 0x74, 0x65, 0x6d, 0xa8, 0x72,
0x6f, 0x75, 0x6e, 0x74, 0x32, 0x22, 0x7d, 0xa5, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x72, 0xa5,
0x65, 0x72, 0x72, 0x6f, 0x72, 0x02, 0xa6, 0x65, 0x65, 0x6f, 0x73, 0x69, 0x6f, 0xa6, 0x72, 0x65,
0x78, 0x63, 0x65, 0x70, 0x74, 0xa6, 0x65, 0x72, 0x74, 0x75, 0x72, 0x6e, 0xc4, 0x04, 0xde, 0xad,
0x72, 0x73, 0x74, 0x72, 0xa4, 0x6e, 0x61, 0x6d, 0xbe, 0xef, 0xa5, 0x74, 0x78, 0x5f, 0x69, 0x64,
0x65, 0xa8, 0x73, 0x65, 0x6c, 0x6c, 0x69, 0x74, 0xd9, 0x40, 0x65, 0x64, 0x63, 0x30, 0x36, 0x64,
0x65, 0x6d, 0xa8, 0x72, 0x65, 0x63, 0x65, 0x69, 0x63, 0x65, 0x36, 0x33, 0x32, 0x30, 0x34, 0x35,
0x76, 0x65, 0x72, 0xa5, 0x65, 0x6f, 0x73, 0x69, 0x39, 0x66, 0x64, 0x36, 0x34, 0x34, 0x37, 0x35,
0x6f, 0xa6, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x36, 0x39, 0x37, 0x32, 0x30, 0x34, 0x38, 0x64,
0xc4, 0x04, 0xde, 0xad, 0xbe, 0xef, 0xa5, 0x74, 0x61, 0x34, 0x35, 0x33, 0x62, 0x32, 0x38, 0x31,
0x78, 0x5f, 0x69, 0x64, 0xd9, 0x40, 0x65, 0x64, 0x36, 0x62, 0x30, 0x61, 0x34, 0x33, 0x34, 0x63,
0x63, 0x30, 0x36, 0x64, 0x63, 0x65, 0x36, 0x33, 0x33, 0x37, 0x64, 0x64, 0x66, 0x66, 0x64, 0x65,
0x32, 0x30, 0x34, 0x35, 0x39, 0x66, 0x64, 0x36, 0x33, 0x36, 0x37, 0x37, 0x38, 0x64, 0x63, 0x61,
0x34, 0x34, 0x37, 0x35, 0x36, 0x39, 0x37, 0x32, 0x62, 0x33,
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,
} }
assert.Equal(t, expected, data) assert.Equal(t, expected, data)
} }
func TestMsgpack_Decode(t *testing.T) { func TestMsgpack_Decode(t *testing.T) {
data := []byte("\x8b\xadauthorization\x91\x82\xa5actor\xa6mygame\xaapermission\xa6active\xa8blocknum\xce\x00\x85F7\xaeblocktimestamp\xd6\xffH\xf1U\x1f\xa8contract\xa6mygame\xa4dataċ{\"dropped_from_id\":674562,\"item\":{\"dur\":145,\"id\":49623,\"name\":\"Shadowmourne\",\"qual\":\"legendary\",\"sta\":198,\"str\":223},\"receiver\":\"account1\"}\xa5error\x02\xa6except\xa6errstr\xa4name\xa4drop\xa8receiver\xa8account1\xa6return\xc4\x04ޭ\xbe\xef\xa5tx_id\xd9@edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3") data := []byte("\x8b\xadauthorization\x91\x82\xa5actor\xa6mygame\xaapermission\xa6active\xa8blocknum\xce\x00\x85F7\xaeblocktimestamp\xd6\xffH\xf1U\x1f\xa8contract\xa6mygame\xa4data\x83\xafdropped_from_id\xd2\x00\nK\x02\xa4item\x86\xa3dur\xd1\x00\x91\xa2id\xd2\x00\x00\xc1פname\xacShadowmourne\xa4qual\xa9legendary\xa3sta\xd1\x00ƣstr\xd1\x00ߨreceiver\xa8account1\xa5error\x02\xa6except\xa6errstr\xa4name\xa4drop\xa8receiver\xa8account1\xa6return\xc4\x04ޭ\xbe\xef\xa5tx_id\xd9@edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3")
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{ expected := message.ActionTrace{
TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3", TxID: "edc06dce6320459fd644756972048da453b2816b0a434c37ddffde36778dcab3",
@ -126,7 +101,18 @@ func TestMsgpack_Decode(t *testing.T) {
Name: "drop", Name: "drop",
Contract: "mygame", Contract: "mygame",
Receiver: "account1", Receiver: "account1",
Data: dataJson, Data: map[string]any{
"item": map[string]any{
"id": int64(49623),
"name": "Shadowmourne",
"str": int64(223),
"sta": int64(198),
"dur": int64(145),
"qual": "legendary",
},
"dropped_from_id": int64(674562),
"receiver": "account1",
},
Authorization: []message.PermissionLevel{ Authorization: []message.PermissionLevel{
{Actor: "mygame", Permission: "active"}, {Actor: "mygame", Permission: "active"},
}, },
@ -136,7 +122,7 @@ func TestMsgpack_Decode(t *testing.T) {
} }
res := message.ActionTrace{} res := message.ActionTrace{}
err = decode(data, &res) err := decode(data, &res)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, res) assert.Equal(t, expected, res)

View file

@ -1,7 +1,7 @@
package message package message
import ( import (
"encoding/json" "errors"
"time" "time"
) )
@ -29,8 +29,8 @@ type ActionTrace struct {
// Contract account. // Contract account.
Contract string `json:"contract" msgpack:"contract"` Contract string `json:"contract" msgpack:"contract"`
Receiver string `json:"receiver" msgpack:"receiver"` Receiver string `json:"receiver" msgpack:"receiver"`
Data []byte `json:"data" msgpack:"data"` Data interface{} `json:"data" msgpack:"data"`
Authorization []PermissionLevel `json:"authorization" msgpack:"authorization"` Authorization []PermissionLevel `json:"authorization" msgpack:"authorization"`
@ -39,7 +39,9 @@ type ActionTrace struct {
Return []byte `json:"return" msgpack:"return"` Return []byte `json:"return" msgpack:"return"`
} }
func (act ActionTrace) GetData() (map[string]interface{}, error) { func (act ActionTrace) GetData() (map[string]any, error) {
data := map[string]interface{}{} if data, ok := act.Data.(map[string]any); ok {
return data, json.Unmarshal(act.Data, &data) return data, nil
}
return nil, errors.New("failed to convert data to map")
} }

View file

@ -8,14 +8,17 @@ import (
func TestTypes_ActionTraceGetData(t *testing.T) { func TestTypes_ActionTraceGetData(t *testing.T) {
act := ActionTrace{ act := ActionTrace{
Data: []byte(`{"one": 1234, "two": "string"}`), Data: map[string]any{
"one": 1234,
"two": "string",
},
} }
actual, err := act.GetData() actual, err := act.GetData()
assert.NoError(t, err) assert.NoError(t, err)
exptected := map[string]interface{}{ exptected := map[string]interface{}{
"one": float64(1234), "one": 1234,
"two": "string", "two": "string",
} }