From 905fc968bfacfdb9cdaa726466578b98b1ffddaa Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 18 Mar 2020 22:53:15 -0700 Subject: [PATCH] Integrate updated serialization vectors as tests --- .gitmodules | 3 + chain/vectors/gen/main.go | 197 ++++++++++++++++++++++++++++++++++ chain/vectors/vector_types.go | 25 +++++ chain/vectors/vectors_test.go | 45 ++++++++ extern/serialization-vectors | 1 + 5 files changed, 271 insertions(+) create mode 100644 chain/vectors/gen/main.go create mode 100644 chain/vectors/vector_types.go create mode 100644 chain/vectors/vectors_test.go create mode 160000 extern/serialization-vectors diff --git a/.gitmodules b/.gitmodules index a655f05b9..709a28003 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,6 @@ path = extern/filecoin-ffi url = https://github.com/filecoin-project/filecoin-ffi.git branch = master +[submodule "extern/serialization-vectors"] + path = extern/serialization-vectors + url = https://github.com/filecoin-project/serialization-vectors diff --git a/chain/vectors/gen/main.go b/chain/vectors/gen/main.go new file mode 100644 index 000000000..cf8ebd859 --- /dev/null +++ b/chain/vectors/gen/main.go @@ -0,0 +1,197 @@ +package main + +import ( + "encoding/json" + "fmt" + "math/rand" + "os" + + "github.com/filecoin-project/go-address" + + "github.com/filecoin-project/lotus/chain/gen" + "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/chain/types/mock" + "github.com/filecoin-project/lotus/chain/vectors" + "github.com/filecoin-project/lotus/chain/wallet" + "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/abi/big" + "github.com/filecoin-project/specs-actors/actors/builtin/power" + "github.com/filecoin-project/specs-actors/actors/crypto" + + _ "github.com/filecoin-project/lotus/lib/sigs/bls" + _ "github.com/filecoin-project/lotus/lib/sigs/secp" +) + +func init() { + power.ConsensusMinerMinPower = big.NewInt(2048) +} + +func MakeHeaderVectors() []vectors.HeaderVector { + cg, err := gen.NewGenerator() + if err != nil { + panic(err) + } + + var out []vectors.HeaderVector + for i := 0; i < 5; i++ { + nts, err := cg.NextTipSet() + if err != nil { + panic(err) + } + + h := nts.TipSet.Blocks[0].Header + data, err := h.Serialize() + if err != nil { + panic(err) + } + + out = append(out, vectors.HeaderVector{ + Block: h, + Cid: h.Cid().String(), + CborHex: fmt.Sprintf("%x", data), + }) + } + return out +} + +func MakeMessageSigningVectors() []vectors.MessageSigningVector { + w, err := wallet.NewWallet(wallet.NewMemKeyStore()) + if err != nil { + panic(err) + } + + blsk, err := w.GenerateKey(crypto.SigTypeBLS) + if err != nil { + panic(err) + } + bki, err := w.Export(blsk) + if err != nil { + panic(err) + } + + to, err := address.NewIDAddress(99999) + if err != nil { + panic(err) + } + + bmsg := mock.MkMessage(blsk, to, 55, w) + + blsmsv := vectors.MessageSigningVector{ + Unsigned: &bmsg.Message, + Cid: bmsg.Message.Cid().String(), + CidHexBytes: fmt.Sprintf("%x", bmsg.Message.Cid().Bytes()), + PrivateKey: bki.PrivateKey, + Signature: &bmsg.Signature, + } + + secpk, err := w.GenerateKey(crypto.SigTypeBLS) + if err != nil { + panic(err) + } + ski, err := w.Export(secpk) + if err != nil { + panic(err) + } + + smsg := mock.MkMessage(secpk, to, 55, w) + + smsv := vectors.MessageSigningVector{ + Unsigned: &smsg.Message, + Cid: smsg.Message.Cid().String(), + CidHexBytes: fmt.Sprintf("%x", smsg.Message.Cid().Bytes()), + PrivateKey: ski.PrivateKey, + Signature: &smsg.Signature, + } + + return []vectors.MessageSigningVector{blsmsv, smsv} +} + +func MakeUnsignedMessageVectors() []vectors.UnsignedMessageVector { + froms := []string{ + "t2ch7krq7l35i74rebqbjdsp3ucl47t24e3juxjfa", + "t1pyfq7dg6sq65acyomqvzvbgwni4zllglqffw5dy", + "t1cyg66djxytxhzdq7ynoqfxk7xinp6xsejbeufli", + "t16n7vrq5humzoqll7zg4yw6dta645tuakcoalp6y", + "t1awsiuji4wpbxpzslg36f3wnfxzi4o5gq67tz2mi", + "t14mb3j32uuwajy5b2mliz63isp6zl5xkppzyuhfy", + "t1dzdmyzzdy6q5elobj63eokzv2xnwsp4vm5l6aka", + "t1svd45rkcfpsyqedvvhuv77yvllvu5ygmygjlvka", + "t1mrret5liwh46qde6qhaxrmcwil7jawjeqdijwfq", + "t1ly3ynedw74p4q3ytdnb4stjdkiodrl54moeyxea", + "t1uqexvn66gj4lxkbvmrgposwrlxbyd655o2nayyi", + "t1dwwjod7vw62jzw2eva7gtxohaidjhgh6w2rofui", + "t1slswisymmkfulmvl3jynrnwqi27tkvmsgzhztvy", + "t1e3vymxcdqfkqwz6e6wnxxx6ayuml3vxi5gef4xa", + "t1bgqopgk64ywpprka4citgi62aldclyaegvwvx6y", + "t1aizqgl2klzkzffwu35rufyuzefke2i6ndbewuhi", + "t1mzposcnsd2tc66yu5i3kajtrh5pvwohdjvitcey", + "t1x7xvs6oorrrlefyzn6wlbvaibzj3a2fyt4hsmvq", + "t1ez743nvc4j7qfirwnmxbh4qdqwha3iyalnq4rya", + "t17dvtgkop7cqgi6myjne5kzvrnsbg5wnowjphhwy", + "t1kvar5z3q7dwrfxjqsnuqpq5qsd7mvh2xypblwta", + } + var out []vectors.UnsignedMessageVector + for _, a := range froms { + from, err := address.NewFromString(a) + if err != nil { + panic(err) + } + to, err := address.NewIDAddress(rand.Uint64()) + if err != nil { + panic(err) + } + + params := make([]byte, 32) + rand.Read(params) + + msg := &types.Message{ + To: to, + From: from, + Value: types.NewInt(rand.Uint64()), + Method: abi.MethodNum(rand.Uint64()), + GasPrice: types.NewInt(rand.Uint64()), + GasLimit: rand.Int63(), + Nonce: rand.Uint64(), + Params: params, + } + + ser, err := msg.Serialize() + if err != nil { + panic(err) + } + + out = append(out, vectors.UnsignedMessageVector{ + Message: msg, + HexCbor: fmt.Sprintf("%x", ser), + }) + } + return out +} + +func WriteJsonToFile(fname string, obj interface{}) error { + fi, err := os.Create(fname) + if err != nil { + return err + } + defer fi.Close() + + out, err := json.MarshalIndent(obj, "", " ") + if err != nil { + return err + } + + fi.Write(out) + return nil +} + +func main() { + if err := WriteJsonToFile("block_headers.json", MakeHeaderVectors()); err != nil { + panic(err) + } + if err := WriteJsonToFile("message_signing.json", MakeMessageSigningVectors()); err != nil { + panic(err) + } + if err := WriteJsonToFile("unsigned_messages.json", MakeUnsignedMessageVectors()); err != nil { + panic(err) + } +} diff --git a/chain/vectors/vector_types.go b/chain/vectors/vector_types.go new file mode 100644 index 000000000..73216a049 --- /dev/null +++ b/chain/vectors/vector_types.go @@ -0,0 +1,25 @@ +package vectors + +import ( + "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/specs-actors/actors/crypto" +) + +type HeaderVector struct { + Block *types.BlockHeader `json:"block"` + CborHex string `json:"cbor_hex"` + Cid string `json:"cid"` +} + +type MessageSigningVector struct { + Unsigned *types.Message + Cid string + CidHexBytes string + PrivateKey []byte + Signature *crypto.Signature +} + +type UnsignedMessageVector struct { + Message *types.Message `json:"message"` + HexCbor string `json:"hex_cbor"` +} diff --git a/chain/vectors/vectors_test.go b/chain/vectors/vectors_test.go new file mode 100644 index 000000000..f7916e9d1 --- /dev/null +++ b/chain/vectors/vectors_test.go @@ -0,0 +1,45 @@ +package vectors + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "testing" +) + +func LoadVector(t *testing.T, f string, out interface{}) { + p := filepath.Join("../../extern/serialization-vectors", f) + fi, err := os.Open(p) + if err != nil { + t.Fatal(err) + } + + if err := json.NewDecoder(fi).Decode(out); err != nil { + t.Fatal(err) + } +} + +func TestBlockHeaderVectors(t *testing.T) { + var headers []HeaderVector + LoadVector(t, "block_headers.json", &headers) + + for i, hv := range headers { + if hv.Block.Cid().String() != hv.Cid { + t.Fatalf("CID mismatch in test vector %d", i) + } + + data, err := hv.Block.Serialize() + if err != nil { + t.Fatal(err) + } + + if fmt.Sprintf("%x", data) != hv.CborHex { + t.Fatalf("serialized data mismatched for test vector %d", i) + } + } +} + +func TestMessageSigningVectors(t *testing.T) { + // TODO: +} diff --git a/extern/serialization-vectors b/extern/serialization-vectors new file mode 160000 index 000000000..1e778d5bd --- /dev/null +++ b/extern/serialization-vectors @@ -0,0 +1 @@ +Subproject commit 1e778d5bd77f758e83a18c41d10c2649b0e70fef