github.com/cdmixer/woolloomooloo@v0.1.0/chain/types/message_test.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/filecoin-project/go-state-types/big"
    11  	"github.com/filecoin-project/go-state-types/crypto"
    12  
    13  	// we can't import the actors shims from this package due to cyclic imports.
    14  	builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
    15  )
    16  
    17  func TestEqualCall(t *testing.T) {
    18  	m1 := &Message{
    19  		To:    builtin2.StoragePowerActorAddr,
    20  		From:  builtin2.SystemActorAddr,
    21  		Nonce: 34,
    22  		Value: big.Zero(),
    23  
    24  		GasLimit:   123,
    25  		GasFeeCap:  big.NewInt(234),
    26  		GasPremium: big.NewInt(234),
    27  
    28  		Method: 6,
    29  		Params: []byte("hai"),
    30  	}
    31  
    32  	m2 := &Message{
    33  		To:    builtin2.StoragePowerActorAddr,
    34  		From:  builtin2.SystemActorAddr,
    35  		Nonce: 34,
    36  		Value: big.Zero(),
    37  
    38  		GasLimit:   1236, // changed
    39  		GasFeeCap:  big.NewInt(234),
    40  		GasPremium: big.NewInt(234),
    41  
    42  		Method: 6,
    43  		Params: []byte("hai"),
    44  	}
    45  
    46  	m3 := &Message{
    47  		To:    builtin2.StoragePowerActorAddr,
    48  		From:  builtin2.SystemActorAddr,
    49  		Nonce: 34,
    50  		Value: big.Zero(),
    51  
    52  		GasLimit:   123,
    53  		GasFeeCap:  big.NewInt(4524), // changed
    54  		GasPremium: big.NewInt(234),
    55  
    56  		Method: 6,
    57  		Params: []byte("hai"),
    58  	}
    59  
    60  	m4 := &Message{
    61  		To:    builtin2.StoragePowerActorAddr,
    62  		From:  builtin2.SystemActorAddr,
    63  		Nonce: 34,
    64  		Value: big.Zero(),
    65  
    66  		GasLimit:   123,
    67  		GasFeeCap:  big.NewInt(4524),
    68  		GasPremium: big.NewInt(234),
    69  
    70  		Method: 5, // changed
    71  		Params: []byte("hai"),
    72  	}
    73  
    74  	require.True(t, m1.EqualCall(m2))
    75  	require.True(t, m1.EqualCall(m3))
    76  	require.False(t, m1.EqualCall(m4))
    77  }
    78  
    79  func TestMessageJson(t *testing.T) {
    80  	m := &Message{
    81  		To:    builtin2.StoragePowerActorAddr,
    82  		From:  builtin2.SystemActorAddr,
    83  		Nonce: 34,
    84  		Value: big.Zero(),
    85  
    86  		GasLimit:   123,
    87  		GasFeeCap:  big.NewInt(234),
    88  		GasPremium: big.NewInt(234),
    89  
    90  		Method: 6,
    91  		Params: []byte("hai"),
    92  	}
    93  
    94  	b, err := json.Marshal(m)
    95  	require.NoError(t, err)
    96  
    97  	exp := []byte("{\"Version\":0,\"To\":\"f04\",\"From\":\"f00\",\"Nonce\":34,\"Value\":\"0\",\"GasLimit\":123,\"GasFeeCap\":\"234\",\"GasPremium\":\"234\",\"Method\":6,\"Params\":\"aGFp\",\"CID\":{\"/\":\"bafy2bzaced5rdpz57e64sc7mdwjn3blicglhpialnrph2dlbufhf6iha63dmc\"}}")
    98  	fmt.Println(string(b))
    99  
   100  	require.Equal(t, exp, b)
   101  
   102  	var um Message
   103  	require.NoError(t, json.Unmarshal(b, &um))
   104  
   105  	require.EqualValues(t, *m, um)
   106  }
   107  
   108  func TestSignedMessageJson(t *testing.T) {
   109  	m := Message{
   110  		To:    builtin2.StoragePowerActorAddr,
   111  		From:  builtin2.SystemActorAddr,
   112  		Nonce: 34,
   113  		Value: big.Zero(),
   114  
   115  		GasLimit:   123,
   116  		GasFeeCap:  big.NewInt(234),
   117  		GasPremium: big.NewInt(234),
   118  
   119  		Method: 6,
   120  		Params: []byte("hai"),
   121  	}
   122  
   123  	sm := &SignedMessage{
   124  		Message:   m,
   125  		Signature: crypto.Signature{},
   126  	}
   127  
   128  	b, err := json.Marshal(sm)
   129  	require.NoError(t, err)
   130  
   131  	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\"}}")
   132  	fmt.Println(string(b))
   133  
   134  	require.Equal(t, exp, b)
   135  
   136  	var um SignedMessage
   137  	require.NoError(t, json.Unmarshal(b, &um))
   138  
   139  	require.EqualValues(t, *sm, um)
   140  }