mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
175 lines
5.8 KiB
Go
175 lines
5.8 KiB
Go
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)
|
|
}
|