Skip to content

Commit

Permalink
Merge pull request #4338 from filecoin-project/feat/message-json-cids
Browse files Browse the repository at this point in the history
types: Add CID fields to messages in json marshalers
  • Loading branch information
Jakub Sztandera authored Oct 13, 2020
2 parents 8232cc8 + 61661fb commit 5fd0bf1
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 18 deletions.
15 changes: 15 additions & 0 deletions chain/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"encoding/json"
"fmt"

"github.com/filecoin-project/go-state-types/abi"
Expand Down Expand Up @@ -106,6 +107,20 @@ func (m *Message) Cid() cid.Cid {
return b.Cid()
}

type mCid struct {
*RawMessage
CID cid.Cid
}

type RawMessage Message

func (m *Message) MarshalJSON() ([]byte, error) {
return json.Marshal(&mCid{
RawMessage: (*RawMessage)(m),
CID: m.Cid(),
})
}

func (m *Message) RequiredFunds() BigInt {
return BigMul(m.GasFeeCap, NewInt(uint64(m.GasLimit)))
}
Expand Down
66 changes: 66 additions & 0 deletions chain/types/message_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package types

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/specs-actors/actors/builtin"
)

Expand Down Expand Up @@ -70,3 +73,66 @@ func TestEqualCall(t *testing.T) {
require.True(t, m1.EqualCall(m3))
require.False(t, m1.EqualCall(m4))
}

func TestMessageJson(t *testing.T) {
m := &Message{
To: builtin.StoragePowerActorAddr,
From: builtin.SystemActorAddr,
Nonce: 34,
Value: big.Zero(),

GasLimit: 123,
GasFeeCap: big.NewInt(234),
GasPremium: big.NewInt(234),

Method: 6,
Params: []byte("hai"),
}

b, err := json.Marshal(m)
require.NoError(t, err)

exp := []byte("{\"Version\":0,\"To\":\"f04\",\"From\":\"f00\",\"Nonce\":34,\"Value\":\"0\",\"GasLimit\":123,\"GasFeeCap\":\"234\",\"GasPremium\":\"234\",\"Method\":6,\"Params\":\"aGFp\",\"CID\":{\"/\":\"bafy2bzaced5rdpz57e64sc7mdwjn3blicglhpialnrph2dlbufhf6iha63dmc\"}}")
fmt.Println(string(b))

require.Equal(t, exp, b)

var um Message
require.NoError(t, json.Unmarshal(b, &um))

require.EqualValues(t, *m, um)
}

func TestSignedMessageJson(t *testing.T) {
m := Message{
To: builtin.StoragePowerActorAddr,
From: builtin.SystemActorAddr,
Nonce: 34,
Value: big.Zero(),

GasLimit: 123,
GasFeeCap: big.NewInt(234),
GasPremium: big.NewInt(234),

Method: 6,
Params: []byte("hai"),
}

sm := &SignedMessage{
Message: m,
Signature: crypto.Signature{},
}

b, err := json.Marshal(sm)
require.NoError(t, err)

exp := []byte("{\"Message\":{\"Version\":0,\"To\":\"f04\",\"From\":\"f00\",\"Nonce\":34,\"Value\":\"0\",\"GasLimit\":123,\"GasFeeCap\":\"234\",\"GasPremium\":\"234\",\"Method\":6,\"Params\":\"aGFp\",\"CID\":{\"/\":\"bafy2bzaced5rdpz57e64sc7mdwjn3blicglhpialnrph2dlbufhf6iha63dmc\"}},\"Signature\":{\"Type\":0,\"Data\":null},\"CID\":{\"/\":\"bafy2bzacea5ainifngxj3rygaw2hppnyz2cw72x5pysqty2x6dxmjs5qg2uus\"}}")
fmt.Println(string(b))

require.Equal(t, exp, b)

var um SignedMessage
require.NoError(t, json.Unmarshal(b, &um))

require.EqualValues(t, *sm, um)
}
15 changes: 15 additions & 0 deletions chain/types/signedmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"encoding/json"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
Expand Down Expand Up @@ -62,6 +63,20 @@ func (sm *SignedMessage) Serialize() ([]byte, error) {
return buf.Bytes(), nil
}

type smCid struct {
*RawSignedMessage
CID cid.Cid
}

type RawSignedMessage SignedMessage

func (sm *SignedMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&smCid{
RawSignedMessage: (*RawSignedMessage)(sm),
CID: sm.Cid(),
})
}

func (sm *SignedMessage) ChainLength() int {
ser, err := sm.Serialize()
if err != nil {
Expand Down
105 changes: 87 additions & 18 deletions documentation/en/api-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
```

Expand Down Expand Up @@ -1334,7 +1337,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
9,
[
Expand Down Expand Up @@ -1370,7 +1376,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
[
{
Expand Down Expand Up @@ -1430,7 +1439,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
{
"MaxFee": "0"
Expand Down Expand Up @@ -1458,7 +1470,10 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
```

Expand Down Expand Up @@ -1742,11 +1757,17 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"Signature": {
"Type": 2,
"Data": "Ynl0ZSBhcnJheQ=="
},
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
]
Expand Down Expand Up @@ -1783,7 +1804,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
{
"MaxFee": "0"
Expand All @@ -1804,11 +1828,17 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"Signature": {
"Type": 2,
"Data": "Ynl0ZSBhcnJheQ=="
},
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
```
Expand All @@ -1833,11 +1863,17 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"Signature": {
"Type": 2,
"Data": "Ynl0ZSBhcnJheQ=="
},
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
]
Expand Down Expand Up @@ -1917,11 +1953,17 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"Signature": {
"Type": 2,
"Data": "Ynl0ZSBhcnJheQ=="
},
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
}
Expand Down Expand Up @@ -2998,7 +3040,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
[
{
Expand All @@ -3024,7 +3069,10 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"MsgRct": {
"ExitCode": 0,
Expand All @@ -3042,7 +3090,10 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"MsgRct": {
"ExitCode": 0,
Expand Down Expand Up @@ -3289,7 +3340,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
[
{
Expand Down Expand Up @@ -4035,7 +4089,10 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"MsgRct": {
"ExitCode": 0,
Expand All @@ -4053,7 +4110,10 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"MsgRct": {
"ExitCode": 0,
Expand Down Expand Up @@ -4857,7 +4917,10 @@ Inputs:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
]
```
Expand All @@ -4875,11 +4938,17 @@ Response:
"GasFeeCap": "0",
"GasPremium": "0",
"Method": 1,
"Params": "Ynl0ZSBhcnJheQ=="
"Params": "Ynl0ZSBhcnJheQ==",
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
},
"Signature": {
"Type": 2,
"Data": "Ynl0ZSBhcnJheQ=="
},
"CID": {
"/": "bafy2bzacebbpdegvr3i4cosewthysg5xkxpqfn2wfcz6mv2hmoktwbdxkax4s"
}
}
```
Expand Down

0 comments on commit 5fd0bf1

Please sign in to comment.