2019-07-18 19:12:30 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
2020-09-25 19:45:27 +00:00
|
|
|
"crypto/rand"
|
2019-07-18 19:12:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-25 19:45:27 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-07-18 19:12:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignedMessageJsonRoundtrip(t *testing.T) {
|
|
|
|
to, _ := address.NewIDAddress(5234623)
|
|
|
|
from, _ := address.NewIDAddress(603911192)
|
2019-07-25 22:15:03 +00:00
|
|
|
smsg := &types.SignedMessage{
|
2019-07-18 19:12:30 +00:00
|
|
|
Message: types.Message{
|
2020-08-06 21:08:42 +00:00
|
|
|
To: to,
|
|
|
|
From: from,
|
|
|
|
Params: []byte("some bytes, idk"),
|
|
|
|
Method: 1235126,
|
|
|
|
Value: types.NewInt(123123),
|
|
|
|
GasFeeCap: types.NewInt(1234),
|
|
|
|
GasPremium: types.NewInt(132414234),
|
|
|
|
GasLimit: 100_000_000,
|
|
|
|
Nonce: 123123,
|
2019-07-18 19:12:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(smsg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
var osmsg types.SignedMessage
|
2019-07-18 19:12:30 +00:00
|
|
|
if err := json.Unmarshal(out, &osmsg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 19:45:27 +00:00
|
|
|
|
|
|
|
func TestAddressType(t *testing.T) {
|
|
|
|
build.SetAddressNetwork(address.Testnet)
|
|
|
|
addr, err := makeRandomAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(addr[0]) != address.TestnetPrefix {
|
|
|
|
t.Fatalf("address should start with %s", address.TestnetPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
build.SetAddressNetwork(address.Mainnet)
|
|
|
|
addr, err = makeRandomAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(addr[0]) != address.MainnetPrefix {
|
|
|
|
t.Fatalf("address should start with %s", address.MainnetPrefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeRandomAddress() (string, error) {
|
|
|
|
bytes := make([]byte, 32)
|
|
|
|
_, err := rand.Read(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := address.NewActorAddress(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.String(), nil
|
|
|
|
}
|