From 0d6cfc879d6b2809b39aba32f758f4fe6ef1dc6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 13 Feb 2020 00:52:36 +0100 Subject: [PATCH] Use specs-actors ignatures --- api/api_full.go | 8 +- api/apistruct/struct.go | 16 +- api/cbor_gen.go | 21 +- chain/blocksync/cbor_gen.go | 8 +- chain/gen/gen.go | 17 +- chain/gen/genesis/genesis.go | 5 +- chain/gen/mining.go | 11 +- chain/messagepool/messagepool.go | 5 +- chain/sync.go | 36 ++- chain/types/blockheader.go | 5 +- chain/types/cbor_gen.go | 429 +++++++++++++++++++++++-------- chain/types/keystore.go | 4 +- chain/types/signature.go | 51 +--- chain/types/signedmessage.go | 7 +- chain/types/vmcontext.go | 3 +- chain/types/voucher.go | 69 +---- chain/vm/vm.go | 3 +- chain/wallet/wallet.go | 11 +- cmd/lotus-seed/seed/seed.go | 3 +- lib/sigs/bls/init.go | 8 +- lib/sigs/secp/init.go | 7 +- lib/sigs/sigs.go | 19 +- node/impl/full/state.go | 9 +- node/impl/full/wallet.go | 6 +- 24 files changed, 444 insertions(+), 317 deletions(-) diff --git a/api/api_full.go b/api/api_full.go index 26b5dd0b2..ebcee4902 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -9,6 +9,8 @@ import ( "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/market" + "github.com/filecoin-project/specs-actors/actors/builtin/miner" + "github.com/filecoin-project/specs-actors/actors/crypto" "github.com/ipfs/go-cid" "github.com/ipfs/go-filestore" "github.com/libp2p/go-libp2p-core/peer" @@ -69,11 +71,11 @@ type FullNode interface { // wallet - WalletNew(context.Context, string) (address.Address, error) + WalletNew(context.Context, crypto.SigType) (address.Address, error) WalletHas(context.Context, address.Address) (bool, error) WalletList(context.Context) ([]address.Address, error) WalletBalance(context.Context, address.Address) (types.BigInt, error) - WalletSign(context.Context, address.Address, []byte) (*types.Signature, error) + WalletSign(context.Context, address.Address, []byte) (*crypto.Signature, error) WalletSignMessage(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) WalletDefaultAddress(context.Context) (address.Address, error) WalletSetDefault(context.Context, address.Address) error @@ -112,7 +114,7 @@ type FullNode interface { StateMinerPower(context.Context, address.Address, *types.TipSet) (MinerPower, error) StateMinerWorker(context.Context, address.Address, *types.TipSet) (address.Address, error) StateMinerPeerID(ctx context.Context, m address.Address, ts *types.TipSet) (peer.ID, error) - StateMinerElectionPeriodStart(ctx context.Context, actor address.Address, ts *types.TipSet) (abi.ChainEpoch, error) + StateMinerPostState(ctx context.Context, actor address.Address, ts *types.TipSet) (*miner.PoStState, error) StateMinerSectorSize(context.Context, address.Address, *types.TipSet) (abi.SectorSize, error) StateMinerFaults(context.Context, address.Address, *types.TipSet) ([]abi.SectorNumber, error) StatePledgeCollateral(context.Context, *types.TipSet) (types.BigInt, error) diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index 11800d093..3da6afbca 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -4,6 +4,8 @@ import ( "context" "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/builtin/miner" + "github.com/filecoin-project/specs-actors/actors/crypto" sectorbuilder "github.com/filecoin-project/go-sectorbuilder" @@ -74,11 +76,11 @@ type FullNodeStruct struct { MinerCreateBlock func(context.Context, address.Address, *types.TipSet, *types.Ticket, *types.EPostProof, []*types.SignedMessage, abi.ChainEpoch, uint64) (*types.BlockMsg, error) `perm:"write"` - WalletNew func(context.Context, string) (address.Address, error) `perm:"write"` + WalletNew func(context.Context, crypto.SigType) (address.Address, error) `perm:"write"` WalletHas func(context.Context, address.Address) (bool, error) `perm:"write"` WalletList func(context.Context) ([]address.Address, error) `perm:"write"` WalletBalance func(context.Context, address.Address) (types.BigInt, error) `perm:"read"` - WalletSign func(context.Context, address.Address, []byte) (*types.Signature, error) `perm:"sign"` + WalletSign func(context.Context, address.Address, []byte) (*crypto.Signature, error) `perm:"sign"` WalletSignMessage func(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) `perm:"sign"` WalletDefaultAddress func(context.Context) (address.Address, error) `perm:"write"` WalletSetDefault func(context.Context, address.Address) error `perm:"admin"` @@ -100,7 +102,7 @@ type FullNodeStruct struct { StateMinerPower func(context.Context, address.Address, *types.TipSet) (api.MinerPower, error) `perm:"read"` StateMinerWorker func(context.Context, address.Address, *types.TipSet) (address.Address, error) `perm:"read"` StateMinerPeerID func(ctx context.Context, m address.Address, ts *types.TipSet) (peer.ID, error) `perm:"read"` - StateMinerElectionPeriodStart func(ctx context.Context, actor address.Address, ts *types.TipSet) (abi.ChainEpoch, error) `perm:"read"` + StateMinerPostState func(ctx context.Context, actor address.Address, ts *types.TipSet) (*miner.PoStState, error) `perm:"read"` StateMinerSectorSize func(context.Context, address.Address, *types.TipSet) (abi.SectorSize, error) `perm:"read"` StateMinerFaults func(context.Context, address.Address, *types.TipSet) ([]abi.SectorNumber, error) `perm:"read"` StateCall func(context.Context, *types.Message, *types.TipSet) (*api.MethodCall, error) `perm:"read"` @@ -272,7 +274,7 @@ func (c *FullNodeStruct) ChainGetTipSetByHeight(ctx context.Context, h abi.Chain return c.Internal.ChainGetTipSetByHeight(ctx, h, ts) } -func (c *FullNodeStruct) WalletNew(ctx context.Context, typ string) (address.Address, error) { +func (c *FullNodeStruct) WalletNew(ctx context.Context, typ crypto.SigType) (address.Address, error) { return c.Internal.WalletNew(ctx, typ) } @@ -288,7 +290,7 @@ func (c *FullNodeStruct) WalletBalance(ctx context.Context, a address.Address) ( return c.Internal.WalletBalance(ctx, a) } -func (c *FullNodeStruct) WalletSign(ctx context.Context, k address.Address, msg []byte) (*types.Signature, error) { +func (c *FullNodeStruct) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) { return c.Internal.WalletSign(ctx, k, msg) } @@ -412,8 +414,8 @@ func (c *FullNodeStruct) StateMinerPeerID(ctx context.Context, m address.Address return c.Internal.StateMinerPeerID(ctx, m, ts) } -func (c *FullNodeStruct) StateMinerElectionPeriodStart(ctx context.Context, actor address.Address, ts *types.TipSet) (abi.ChainEpoch, error) { - return c.Internal.StateMinerElectionPeriodStart(ctx, actor, ts) +func (c *FullNodeStruct) StateMinerPostState(ctx context.Context, actor address.Address, ts *types.TipSet) (*miner.PoStState, error) { + return c.Internal.StateMinerPostState(ctx, actor, ts) } func (c *FullNodeStruct) StateMinerSectorSize(ctx context.Context, actor address.Address, ts *types.TipSet) (abi.SectorSize, error) { diff --git a/api/cbor_gen.go b/api/cbor_gen.go index b3201b3e7..ddb4e8416 100644 --- a/api/cbor_gen.go +++ b/api/cbor_gen.go @@ -6,7 +6,8 @@ import ( "fmt" "io" - "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/builtin/paych" cbg "github.com/whyrusleeping/cbor-gen" xerrors "golang.org/x/xerrors" ) @@ -60,7 +61,7 @@ func (t *PaymentInfo) MarshalCBOR(w io.Writer) error { } } - // t.Vouchers ([]*types.SignedVoucher) (slice) + // t.Vouchers ([]*paych.SignedVoucher) (slice) if len("Vouchers") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Vouchers\" was too long") } @@ -152,7 +153,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { } } - // t.Vouchers ([]*types.SignedVoucher) (slice) + // t.Vouchers ([]*paych.SignedVoucher) (slice) case "Vouchers": maj, extra, err = cbg.CborReadHeader(br) @@ -168,11 +169,11 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("expected cbor array") } if extra > 0 { - t.Vouchers = make([]*types.SignedVoucher, extra) + t.Vouchers = make([]*paych.SignedVoucher, extra) } for i := 0; i < int(extra); i++ { - var v types.SignedVoucher + var v paych.SignedVoucher if err := v.UnmarshalCBOR(br); err != nil { return err } @@ -196,7 +197,7 @@ func (t *SealedRef) MarshalCBOR(w io.Writer) error { return err } - // t.SectorID (uint64) (uint64) + // t.SectorID (abi.SectorNumber) (uint64) if len("SectorID") > cbg.MaxLength { return xerrors.Errorf("Value in field \"SectorID\" was too long") } @@ -228,7 +229,7 @@ func (t *SealedRef) MarshalCBOR(w io.Writer) error { return err } - // t.Size (uint64) (uint64) + // t.Size (abi.UnpaddedPieceSize) (uint64) if len("Size") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Size\" was too long") } @@ -276,7 +277,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { } switch name { - // t.SectorID (uint64) (uint64) + // t.SectorID (abi.SectorNumber) (uint64) case "SectorID": maj, extra, err = cbg.CborReadHeader(br) @@ -286,6 +287,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { if maj != cbg.MajUnsignedInt { return fmt.Errorf("wrong type for uint64 field") } + t.SectorID = abi.SectorNumber(extra) // t.Offset (uint64) (uint64) case "Offset": @@ -297,7 +299,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("wrong type for uint64 field") } t.Offset = uint64(extra) - // t.Size (uint64) (uint64) + // t.Size (abi.UnpaddedPieceSize) (uint64) case "Size": maj, extra, err = cbg.CborReadHeader(br) @@ -307,6 +309,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { if maj != cbg.MajUnsignedInt { return fmt.Errorf("wrong type for uint64 field") } + t.Size = abi.UnpaddedPieceSize(extra) default: return fmt.Errorf("unknown struct field %d: '%s'", i, name) diff --git a/chain/blocksync/cbor_gen.go b/chain/blocksync/cbor_gen.go index 0a4638da9..1e569a3ae 100644 --- a/chain/blocksync/cbor_gen.go +++ b/chain/blocksync/cbor_gen.go @@ -274,7 +274,7 @@ func (t *BSTipSet) MarshalCBOR(w io.Writer) error { return err } for _, v := range v { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, v); err != nil { + if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -311,7 +311,7 @@ func (t *BSTipSet) MarshalCBOR(w io.Writer) error { return err } for _, v := range v { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, v); err != nil { + if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -437,7 +437,7 @@ func (t *BSTipSet) UnmarshalCBOR(r io.Reader) error { return xerrors.Errorf("value read for array t.BlsMsgIncludes[i] was not a uint, instead got %d", maj) } - t.BlsMsgIncludes[i][j] = val + t.BlsMsgIncludes[i][j] = uint64(val) } } @@ -519,7 +519,7 @@ func (t *BSTipSet) UnmarshalCBOR(r io.Reader) error { return xerrors.Errorf("value read for array t.SecpkMsgIncludes[i] was not a uint, instead got %d", maj) } - t.SecpkMsgIncludes[i][j] = val + t.SecpkMsgIncludes[i][j] = uint64(val) } } diff --git a/chain/gen/gen.go b/chain/gen/gen.go index 1afd0d751..c602b42e8 100644 --- a/chain/gen/gen.go +++ b/chain/gen/gen.go @@ -10,6 +10,7 @@ import ( "sync/atomic" "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/crypto" block "github.com/ipfs/go-block-format" "github.com/ipfs/go-blockservice" "github.com/ipfs/go-car" @@ -114,14 +115,14 @@ func NewGenerator() (*ChainGen, error) { return nil, xerrors.Errorf("creating memrepo wallet failed: %w", err) } - banker, err := w.GenerateKey(types.KTSecp256k1) + banker, err := w.GenerateKey(crypto.SigTypeSecp256k1) if err != nil { return nil, xerrors.Errorf("failed to generate banker key: %w", err) } receievers := make([]address.Address, msgsPerBlock) for r := range receievers { - receievers[r], err = w.GenerateKey(types.KTBLS) + receievers[r], err = w.GenerateKey(crypto.SigTypeBLS) if err != nil { return nil, xerrors.Errorf("failed to generate receiver key: %w", err) } @@ -434,7 +435,7 @@ type MiningCheckAPI interface { StateMinerProvingSet(context.Context, address.Address, *types.TipSet) ([]*api.ChainSectorInfo, error) - WalletSign(context.Context, address.Address, []byte) (*types.Signature, error) + WalletSign(context.Context, address.Address, []byte) (*crypto.Signature, error) } type mca struct { @@ -470,7 +471,7 @@ func (mca mca) StateMinerProvingSet(ctx context.Context, maddr address.Address, return stmgr.GetMinerProvingSet(ctx, mca.sm, ts, maddr) } -func (mca mca) WalletSign(ctx context.Context, a address.Address, v []byte) (*types.Signature, error) { +func (mca mca) WalletSign(ctx context.Context, a address.Address, v []byte) (*crypto.Signature, error) { return mca.w.Sign(ctx, a, v) } @@ -598,7 +599,7 @@ func ComputeProof(ctx context.Context, epp ElectionPoStProver, pi *ProofInput) ( return &ept, nil } -type SignFunc func(context.Context, address.Address, []byte) (*types.Signature, error) +type SignFunc func(context.Context, address.Address, []byte) (*crypto.Signature, error) const ( DSepTicket = 1 @@ -632,8 +633,8 @@ func VerifyVRF(ctx context.Context, worker, miner address.Address, p uint64, inp return xerrors.Errorf("computing vrf base failed: %w", err) } - sig := &types.Signature{ - Type: types.KTBLS, + sig := &crypto.Signature{ + Type: crypto.SigTypeBLS, Data: vrfproof, } @@ -655,7 +656,7 @@ func ComputeVRF(ctx context.Context, sign SignFunc, worker, miner address.Addres return nil, err } - if sig.Type != types.KTBLS { + if sig.Type != crypto.SigTypeBLS { return nil, fmt.Errorf("miner worker address was not a BLS key") } diff --git a/chain/gen/genesis/genesis.go b/chain/gen/genesis/genesis.go index a85b537e0..c33456d84 100644 --- a/chain/gen/genesis/genesis.go +++ b/chain/gen/genesis/genesis.go @@ -8,6 +8,7 @@ import ( "github.com/filecoin-project/specs-actors/actors/abi/big" "github.com/filecoin-project/specs-actors/actors/builtin" "github.com/filecoin-project/specs-actors/actors/builtin/account" + "github.com/filecoin-project/specs-actors/actors/crypto" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" bstore "github.com/ipfs/go-ipfs-blockstore" @@ -249,8 +250,8 @@ func MakeGenesisBlock(ctx context.Context, bs bstore.Blockstore, sys *types.VMSy ParentStateRoot: stateroot, Messages: mmb.Cid(), ParentMessageReceipts: emptyroot, - BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")}, - BlockSig: &types.Signature{Type: types.KTBLS, Data: []byte("block signatureeee")}, + BLSAggregate: crypto.Signature{Type: crypto.SigTypeBLS, Data: []byte("signatureeee")}, + BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS, Data: []byte("block signatureeee")}, Timestamp: template.Timestamp, } diff --git a/chain/gen/mining.go b/chain/gen/mining.go index d63d7f500..5b8bafa85 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -6,6 +6,7 @@ import ( bls "github.com/filecoin-project/filecoin-ffi" amt "github.com/filecoin-project/go-amt-ipld/v2" "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/crypto" cid "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" cbg "github.com/whyrusleeping/cbor-gen" @@ -45,9 +46,9 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal var secpkMessages []*types.SignedMessage var blsMsgCids, secpkMsgCids []cid.Cid - var blsSigs []types.Signature + var blsSigs []crypto.Signature for _, msg := range msgs { - if msg.Signature.TypeCode() == types.IKTBLS { + if msg.Signature.Type == crypto.SigTypeBLS { blsSigs = append(blsSigs, msg.Signature) blsMessages = append(blsMessages, &msg.Message) @@ -132,7 +133,7 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal return fullBlock, nil } -func aggregateSignatures(sigs []types.Signature) (types.Signature, error) { +func aggregateSignatures(sigs []crypto.Signature) (crypto.Signature, error) { var blsSigs []bls.Signature for _, s := range sigs { var bsig bls.Signature @@ -141,8 +142,8 @@ func aggregateSignatures(sigs []types.Signature) (types.Signature, error) { } aggSig := bls.Aggregate(blsSigs) - return types.Signature{ - Type: types.KTBLS, + return crypto.Signature{ + Type: crypto.SigTypeBLS, Data: aggSig[:], }, nil } diff --git a/chain/messagepool/messagepool.go b/chain/messagepool/messagepool.go index 5a9deb569..7cd1aaa26 100644 --- a/chain/messagepool/messagepool.go +++ b/chain/messagepool/messagepool.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/filecoin-project/specs-actors/actors/crypto" lru "github.com/hashicorp/golang-lru" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" @@ -342,7 +343,7 @@ func (mp *MessagePool) addSkipChecks(m *types.SignedMessage) error { func (mp *MessagePool) addLocked(m *types.SignedMessage) error { log.Debugf("mpooladd: %s %s", m.Message.From, m.Message.Nonce) - if m.Signature.Type == types.KTBLS { + if m.Signature.Type == crypto.SigTypeBLS { mp.blsSigCache.Add(m.Cid(), m.Signature) } @@ -656,7 +657,7 @@ func (mp *MessagePool) RecoverSig(msg *types.Message) *types.SignedMessage { if !ok { return nil } - sig, ok := val.(types.Signature) + sig, ok := val.(crypto.Signature) if !ok { log.Errorf("value in signature cache was not a signature (got %T)", val) return nil diff --git a/chain/sync.go b/chain/sync.go index d41c10edf..8fb9ebd10 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -1,7 +1,6 @@ package chain import ( - "bytes" "context" "crypto/sha256" "errors" @@ -10,9 +9,11 @@ import ( "time" "github.com/Gurpartap/async" - bls "github.com/filecoin-project/filecoin-ffi" amt "github.com/filecoin-project/go-amt-ipld/v2" sectorbuilder "github.com/filecoin-project/go-sectorbuilder" + "github.com/filecoin-project/specs-actors/actors/builtin/power" + "github.com/filecoin-project/specs-actors/actors/crypto" + "github.com/filecoin-project/specs-actors/actors/util/adt" "github.com/hashicorp/go-multierror" "github.com/ipfs/go-cid" dstore "github.com/ipfs/go-datastore" @@ -26,6 +27,8 @@ import ( "go.opencensus.io/trace" "golang.org/x/xerrors" + bls "github.com/filecoin-project/filecoin-ffi" + "github.com/filecoin-project/go-address" "github.com/filecoin-project/lotus/api" @@ -460,30 +463,21 @@ func (syncer *Syncer) ValidateTipSet(ctx context.Context, fts *store.FullTipSet) } func (syncer *Syncer) minerIsValid(ctx context.Context, maddr address.Address, baseTs *types.TipSet) error { - var err error - enc, err := actors.SerializeParams(&actors.IsValidMinerParam{Addr: maddr}) + var spast power.State + + _, err := syncer.sm.LoadActorState(ctx, actors.StoragePowerAddress, &spast, baseTs) if err != nil { return err } - ret, err := syncer.sm.Call(ctx, &types.Message{ - To: actors.StoragePowerAddress, - From: maddr, - Method: actors.SPAMethods.IsValidMiner, - Params: enc, - }, baseTs) + var claim power.Claim + exist, err := adt.AsMap(syncer.store.Store(ctx), spast.Claims).Get(adt.AddrKey(maddr), &claim) if err != nil { - return xerrors.Errorf("checking if block miner is valid failed: %w", err) + return err } - - if ret.ExitCode != 0 { - return xerrors.Errorf("StorageMarket.IsValidMiner check failed (exit code %d)", ret.ExitCode) - } - - if !bytes.Equal(ret.Return, cbg.CborBoolTrue) { + if !exist { return xerrors.New("miner isn't valid") } - return nil } @@ -821,7 +815,7 @@ func (syncer *Syncer) checkBlockMessages(ctx context.Context, b *types.FullBlock return nil } -func (syncer *Syncer) verifyBlsAggregate(ctx context.Context, sig types.Signature, msgs []cid.Cid, pubks []bls.PublicKey) error { +func (syncer *Syncer) verifyBlsAggregate(ctx context.Context, sig crypto.Signature, msgs []cid.Cid, pubks []bls.PublicKey) error { _, span := trace.StartSpan(ctx, "syncer.verifyBlsAggregate") defer span.End() span.AddAttributes( @@ -1115,8 +1109,8 @@ func persistMessages(bs bstore.Blockstore, bst *blocksync.BSTipSet) error { } } for _, m := range bst.SecpkMessages { - if m.Signature.Type != types.KTSecp256k1 { - return xerrors.Errorf("unknown signature type on message %s: %q", m.Cid(), m.Signature.TypeCode) + if m.Signature.Type != crypto.SigTypeSecp256k1 { + return xerrors.Errorf("unknown signature type on message %s: %q", m.Cid(), m.Signature.Type) } //log.Infof("putting secp256k1 message: %s", m.Cid()) if _, err := store.PutMessage(bs, m); err != nil { diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index 4e8090e1a..1f3f9373c 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -6,6 +6,7 @@ import ( "github.com/filecoin-project/go-sectorbuilder" "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/crypto" block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" @@ -53,11 +54,11 @@ type BlockHeader struct { Messages cid.Cid // 8 - BLSAggregate Signature // 9 + BLSAggregate crypto.Signature // 9 Timestamp uint64 // 10 - BlockSig *Signature // 11 + BlockSig *crypto.Signature // 11 ForkSignaling uint64 // 12 } diff --git a/chain/types/cbor_gen.go b/chain/types/cbor_gen.go index e3a0d894e..d364fca63 100644 --- a/chain/types/cbor_gen.go +++ b/chain/types/cbor_gen.go @@ -7,6 +7,9 @@ import ( "io" "math" + "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/builtin/paych" + "github.com/filecoin-project/specs-actors/actors/crypto" "github.com/ipfs/go-cid" cbg "github.com/whyrusleeping/cbor-gen" xerrors "golang.org/x/xerrors" @@ -52,14 +55,20 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { } } - // t.ParentWeight (types.BigInt) (struct) + // t.ParentWeight (big.Int) (struct) if err := t.ParentWeight.MarshalCBOR(w); err != nil { return err } - // t.Height (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Height))); err != nil { - return err + // t.Height (abi.ChainEpoch) (int64) + if t.Height >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Height))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Height)-1)); err != nil { + return err + } } // t.ParentStateRoot (cid.Cid) (struct) @@ -80,7 +89,7 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("failed to write cid field t.Messages: %w", err) } - // t.BLSAggregate (types.Signature) (struct) + // t.BLSAggregate (crypto.Signature) (struct) if err := t.BLSAggregate.MarshalCBOR(w); err != nil { return err } @@ -90,7 +99,7 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { return err } - // t.BlockSig (types.Signature) (struct) + // t.BlockSig (crypto.Signature) (struct) if err := t.BlockSig.MarshalCBOR(w); err != nil { return err } @@ -182,7 +191,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { t.Parents[i] = c } - // t.ParentWeight (types.BigInt) (struct) + // t.ParentWeight (big.Int) (struct) { @@ -191,8 +200,43 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { } } - // t.Height (uint64) (uint64) + // t.Height (abi.ChainEpoch) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + t.Height = abi.ChainEpoch(extraI) + } + // t.ParentStateRoot (cid.Cid) (struct) + + { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("failed to read cid field t.ParentStateRoot: %w", err) + } + + t.ParentStateRoot = c + + } // t.ParentMessageReceipts (cid.Cid) (struct) { @@ -217,7 +261,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { t.Messages = c } - // t.BLSAggregate (types.Signature) (struct) + // t.BLSAggregate (crypto.Signature) (struct) { @@ -236,7 +280,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("wrong type for uint64 field") } t.Timestamp = uint64(extra) - // t.BlockSig (types.Signature) (struct) + // t.BlockSig (crypto.Signature) (struct) { @@ -250,7 +294,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { return err } } else { - t.BlockSig = new(Signature) + t.BlockSig = new(crypto.Signature) if err := t.BlockSig.UnmarshalCBOR(br); err != nil { return err } @@ -477,7 +521,7 @@ func (t *EPostTicket) MarshalCBOR(w io.Writer) error { return err } - // t.SectorID (uint64) (uint64) + // t.SectorID (abi.SectorNumber) (uint64) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.SectorID))); err != nil { return err } @@ -521,7 +565,7 @@ func (t *EPostTicket) UnmarshalCBOR(r io.Reader) error { if _, err := io.ReadFull(br, t.Partial); err != nil { return err } - // t.SectorID (uint64) (uint64) + // t.SectorID (abi.SectorNumber) (uint64) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -530,6 +574,7 @@ func (t *EPostTicket) UnmarshalCBOR(r io.Reader) error { if maj != cbg.MajUnsignedInt { return fmt.Errorf("wrong type for uint64 field") } + t.SectorID = abi.SectorNumber(extra) // t.ChallengeIndex (uint64) (uint64) maj, extra, err = cbg.CborReadHeader(br) @@ -567,22 +612,22 @@ func (t *Message) MarshalCBOR(w io.Writer) error { return err } - // t.Value (types.BigInt) (struct) + // t.Value (big.Int) (struct) if err := t.Value.MarshalCBOR(w); err != nil { return err } - // t.GasPrice (types.BigInt) (struct) + // t.GasPrice (big.Int) (struct) if err := t.GasPrice.MarshalCBOR(w); err != nil { return err } - // t.GasLimit (types.BigInt) (struct) + // t.GasLimit (big.Int) (struct) if err := t.GasLimit.MarshalCBOR(w); err != nil { return err } - // t.Method (uint64) (uint64) + // t.Method (abi.MethodNum) (uint64) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Method))); err != nil { return err } @@ -644,7 +689,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("wrong type for uint64 field") } t.Nonce = uint64(extra) - // t.Value (types.BigInt) (struct) + // t.Value (big.Int) (struct) { @@ -653,7 +698,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { } } - // t.GasPrice (types.BigInt) (struct) + // t.GasPrice (big.Int) (struct) { @@ -662,7 +707,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { } } - // t.GasLimit (types.BigInt) (struct) + // t.GasLimit (big.Int) (struct) { @@ -671,7 +716,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { } } - // t.Method (uint64) (uint64) + // t.Method (abi.MethodNum) (uint64) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -680,6 +725,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { if maj != cbg.MajUnsignedInt { return fmt.Errorf("wrong type for uint64 field") } + t.Method = abi.MethodNum(extra) // t.Params ([]uint8) (slice) maj, extra, err = cbg.CborReadHeader(br) @@ -714,7 +760,7 @@ func (t *SignedMessage) MarshalCBOR(w io.Writer) error { return err } - // t.Signature (types.Signature) (struct) + // t.Signature (crypto.Signature) (struct) if err := t.Signature.MarshalCBOR(w); err != nil { return err } @@ -745,7 +791,7 @@ func (t *SignedMessage) UnmarshalCBOR(r io.Reader) error { } } - // t.Signature (types.Signature) (struct) + // t.Signature (crypto.Signature) (struct) { @@ -832,9 +878,15 @@ func (t *SignedVoucher) MarshalCBOR(w io.Writer) error { return err } - // t.TimeLock (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.TimeLock))); err != nil { - return err + // t.TimeLock (abi.ChainEpoch) (int64) + if t.TimeLock >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.TimeLock))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.TimeLock)-1)); err != nil { + return err + } } // t.SecretPreimage ([]uint8) (slice) @@ -849,32 +901,50 @@ func (t *SignedVoucher) MarshalCBOR(w io.Writer) error { return err } - // t.Extra (types.ModVerifyParams) (struct) + // t.Extra (paych.ModVerifyParams) (struct) if err := t.Extra.MarshalCBOR(w); err != nil { return err } - // t.Lane (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Lane))); err != nil { - return err + // t.Lane (int64) (int64) + if t.Lane >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Lane))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Lane)-1)); err != nil { + return err + } } - // t.Nonce (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Nonce))); err != nil { - return err + // t.Nonce (int64) (int64) + if t.Nonce >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Nonce))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Nonce)-1)); err != nil { + return err + } } - // t.Amount (types.BigInt) (struct) + // t.Amount (big.Int) (struct) if err := t.Amount.MarshalCBOR(w); err != nil { return err } - // t.MinCloseHeight (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.MinCloseHeight))); err != nil { - return err + // t.MinSettleHeight (abi.ChainEpoch) (int64) + if t.MinSettleHeight >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.MinSettleHeight))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.MinSettleHeight)-1)); err != nil { + return err + } } - // t.Merges ([]types.Merge) (slice) + // t.Merges ([]paych.Merge) (slice) if len(t.Merges) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.Merges was too long") } @@ -888,7 +958,7 @@ func (t *SignedVoucher) MarshalCBOR(w io.Writer) error { } } - // t.Signature (types.Signature) (struct) + // t.Signature (crypto.Signature) (struct) if err := t.Signature.MarshalCBOR(w); err != nil { return err } @@ -910,16 +980,31 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.TimeLock (uint64) (uint64) + // t.TimeLock (abi.ChainEpoch) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err + t.TimeLock = abi.ChainEpoch(extraI) } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") - } - t.TimeLock = uint64(extra) // t.SecretPreimage ([]uint8) (slice) maj, extra, err = cbg.CborReadHeader(br) @@ -937,7 +1022,7 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { if _, err := io.ReadFull(br, t.SecretPreimage); err != nil { return err } - // t.Extra (types.ModVerifyParams) (struct) + // t.Extra (paych.ModVerifyParams) (struct) { @@ -951,34 +1036,64 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { return err } } else { - t.Extra = new(ModVerifyParams) + t.Extra = new(paych.ModVerifyParams) if err := t.Extra.UnmarshalCBOR(br); err != nil { return err } } } - // t.Lane (uint64) (uint64) + // t.Lane (int64) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err + t.Lane = int64(extraI) } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") - } - t.Lane = uint64(extra) - // t.Nonce (uint64) (uint64) + // t.Nonce (int64) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err + t.Nonce = int64(extraI) } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") - } - t.Nonce = uint64(extra) - // t.Amount (types.BigInt) (struct) + // t.Amount (big.Int) (struct) { @@ -987,17 +1102,32 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { } } - // t.MinCloseHeight (uint64) (uint64) + // t.MinSettleHeight (abi.ChainEpoch) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err + t.MinSettleHeight = abi.ChainEpoch(extraI) } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") - } - t.MinCloseHeight = uint64(extra) - // t.Merges ([]types.Merge) (slice) + // t.Merges ([]paych.Merge) (slice) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -1012,11 +1142,11 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("expected cbor array") } if extra > 0 { - t.Merges = make([]Merge, extra) + t.Merges = make([]paych.Merge, extra) } for i := 0; i < int(extra); i++ { - var v Merge + var v paych.Merge if err := v.UnmarshalCBOR(br); err != nil { return err } @@ -1024,7 +1154,7 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { t.Merges[i] = v } - // t.Signature (types.Signature) (struct) + // t.Signature (crypto.Signature) (struct) { @@ -1038,7 +1168,7 @@ func (t *SignedVoucher) UnmarshalCBOR(r io.Reader) error { return err } } else { - t.Signature = new(Signature) + t.Signature = new(crypto.Signature) if err := t.Signature.UnmarshalCBOR(br); err != nil { return err } @@ -1062,7 +1192,7 @@ func (t *ModVerifyParams) MarshalCBOR(w io.Writer) error { return err } - // t.Method (uint64) (uint64) + // t.Method (abi.MethodNum) (uint64) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Method))); err != nil { return err } @@ -1105,7 +1235,7 @@ func (t *ModVerifyParams) UnmarshalCBOR(r io.Reader) error { } } - // t.Method (uint64) (uint64) + // t.Method (abi.MethodNum) (uint64) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -1114,7 +1244,7 @@ func (t *ModVerifyParams) UnmarshalCBOR(r io.Reader) error { if maj != cbg.MajUnsignedInt { return fmt.Errorf("wrong type for uint64 field") } - t.Method = uint64(extra) + t.Method = abi.MethodNum(extra) // t.Data ([]uint8) (slice) maj, extra, err = cbg.CborReadHeader(br) @@ -1144,14 +1274,26 @@ func (t *Merge) MarshalCBOR(w io.Writer) error { return err } - // t.Lane (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Lane))); err != nil { - return err + // t.Lane (int64) (int64) + if t.Lane >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Lane))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Lane)-1)); err != nil { + return err + } } - // t.Nonce (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Nonce))); err != nil { - return err + // t.Nonce (int64) (int64) + if t.Nonce >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Nonce))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Nonce)-1)); err != nil { + return err + } } return nil } @@ -1171,26 +1313,56 @@ func (t *Merge) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.Lane (uint64) (uint64) + // t.Lane (int64) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err + t.Lane = int64(extraI) } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") - } - t.Lane = uint64(extra) - // t.Nonce (uint64) (uint64) + // t.Nonce (int64) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err + t.Nonce = int64(extraI) } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") - } - t.Nonce = uint64(extra) return nil } @@ -1220,7 +1392,7 @@ func (t *Actor) MarshalCBOR(w io.Writer) error { return err } - // t.Balance (types.BigInt) (struct) + // t.Balance (big.Int) (struct) if err := t.Balance.MarshalCBOR(w); err != nil { return err } @@ -1276,7 +1448,7 @@ func (t *Actor) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("wrong type for uint64 field") } t.Nonce = uint64(extra) - // t.Balance (types.BigInt) (struct) + // t.Balance (big.Int) (struct) { @@ -1314,7 +1486,7 @@ func (t *MessageReceipt) MarshalCBOR(w io.Writer) error { return err } - // t.GasUsed (types.BigInt) (struct) + // t.GasUsed (big.Int) (struct) if err := t.GasUsed.MarshalCBOR(w); err != nil { return err } @@ -1366,7 +1538,7 @@ func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error { if _, err := io.ReadFull(br, t.Return); err != nil { return err } - // t.GasUsed (types.BigInt) (struct) + // t.GasUsed (big.Int) (struct) { @@ -1603,7 +1775,7 @@ func (t *StorageAsk) MarshalCBOR(w io.Writer) error { return err } - // t.Price (types.BigInt) (struct) + // t.Price (big.Int) (struct) if err := t.Price.MarshalCBOR(w); err != nil { return err } @@ -1650,7 +1822,7 @@ func (t *StorageAsk) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.Price (types.BigInt) (struct) + // t.Price (big.Int) (struct) { @@ -1748,9 +1920,15 @@ func (t *ExpTipSet) MarshalCBOR(w io.Writer) error { } } - // t.Height (uint64) (uint64) - if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Height))); err != nil { - return err + // t.Height (abi.ChainEpoch) (int64) + if t.Height >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Height))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Height)-1)); err != nil { + return err + } } return nil } @@ -1823,5 +2001,30 @@ func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { t.Blocks[i] = &v } + // t.Height (abi.ChainEpoch) (int64) + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.Height = abi.ChainEpoch(extraI) + } return nil } diff --git a/chain/types/keystore.go b/chain/types/keystore.go index 76eb5f296..2f8c75184 100644 --- a/chain/types/keystore.go +++ b/chain/types/keystore.go @@ -2,6 +2,8 @@ package types import ( "fmt" + + "github.com/filecoin-project/specs-actors/actors/crypto" ) var ( @@ -11,7 +13,7 @@ var ( // KeyInfo is used for storing keys in KeyStore type KeyInfo struct { - Type string + Type crypto.SigType PrivateKey []byte } diff --git a/chain/types/signature.go b/chain/types/signature.go index 3b0862206..a6ab3a352 100644 --- a/chain/types/signature.go +++ b/chain/types/signature.go @@ -6,60 +6,26 @@ import ( "fmt" "io" + "github.com/filecoin-project/specs-actors/actors/crypto" cbg "github.com/whyrusleeping/cbor-gen" ) const SignatureMaxLength = 200 -const ( - KTSecp256k1 = "secp256k1" - KTBLS = "bls" -) - -const ( - IKTUnknown = -1 - - IKTSecp256k1 = iota - IKTBLS -) - -type Signature struct { - Type string - Data []byte -} +type Signature crypto.Signature func SignatureFromBytes(x []byte) (Signature, error) { val, nr := binary.Uvarint(x) if nr != 1 { return Signature{}, fmt.Errorf("signatures with type field longer than one byte are invalid") } - var ts string - switch val { - case IKTSecp256k1: - ts = KTSecp256k1 - case IKTBLS: - ts = KTBLS - default: - return Signature{}, fmt.Errorf("unsupported signature type: %d", val) - } return Signature{ - Type: ts, + Type: crypto.SigType(val), Data: x[1:], }, nil } -func (s *Signature) TypeCode() int { - switch s.Type { - case KTSecp256k1: - return IKTSecp256k1 - case KTBLS: - return IKTBLS - default: - return IKTUnknown - } -} - func (s *Signature) MarshalCBOR(w io.Writer) error { if s == nil { _, err := w.Write(cbg.CborNull) @@ -72,7 +38,7 @@ func (s *Signature) MarshalCBOR(w io.Writer) error { return err } - if _, err := w.Write([]byte{byte(s.TypeCode())}); err != nil { + if _, err := w.Write([]byte{byte(s.Type)}); err != nil { return err } @@ -102,14 +68,11 @@ func (s *Signature) UnmarshalCBOR(br io.Reader) error { return err } - switch buf[0] { - default: + if buf[0] != byte(crypto.SigTypeSecp256k1) || buf[0] != byte(crypto.SigTypeBLS) { return fmt.Errorf("invalid signature type in cbor input: %d", buf[0]) - case IKTSecp256k1: - s.Type = KTSecp256k1 - case IKTBLS: - s.Type = KTBLS } + + s.Type = crypto.SigType(buf[0]) s.Data = buf[1:] return nil diff --git a/chain/types/signedmessage.go b/chain/types/signedmessage.go index 0ede20a4f..9e3d45f0c 100644 --- a/chain/types/signedmessage.go +++ b/chain/types/signedmessage.go @@ -3,13 +3,14 @@ package types import ( "bytes" + "github.com/filecoin-project/specs-actors/actors/crypto" block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" "github.com/multiformats/go-multihash" ) func (m *SignedMessage) ToStorageBlock() (block.Block, error) { - if m.Signature.Type == KTBLS { + if m.Signature.Type == crypto.SigTypeBLS { return m.Message.ToStorageBlock() } @@ -28,7 +29,7 @@ func (m *SignedMessage) ToStorageBlock() (block.Block, error) { } func (m *SignedMessage) Cid() cid.Cid { - if m.Signature.Type == KTBLS { + if m.Signature.Type == crypto.SigTypeBLS { return m.Message.Cid() } @@ -42,7 +43,7 @@ func (m *SignedMessage) Cid() cid.Cid { type SignedMessage struct { Message Message - Signature Signature + Signature crypto.Signature } func DecodeSignedMessage(data []byte) (*SignedMessage, error) { diff --git a/chain/types/vmcontext.go b/chain/types/vmcontext.go index 3d83cfca1..bae0bcd29 100644 --- a/chain/types/vmcontext.go +++ b/chain/types/vmcontext.go @@ -5,6 +5,7 @@ import ( "github.com/filecoin-project/go-sectorbuilder" "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/crypto" "github.com/filecoin-project/go-address" "github.com/filecoin-project/lotus/chain/actors/aerrors" @@ -38,7 +39,7 @@ type VMContext interface { GasUsed() BigInt Storage() Storage StateTree() (StateTree, aerrors.ActorError) - VerifySignature(sig *Signature, from address.Address, data []byte) aerrors.ActorError + VerifySignature(sig *crypto.Signature, from address.Address, data []byte) aerrors.ActorError ChargeGas(uint64) aerrors.ActorError GetRandomness(height abi.ChainEpoch) ([]byte, aerrors.ActorError) GetBalance(address.Address) (BigInt, aerrors.ActorError) diff --git a/chain/types/voucher.go b/chain/types/voucher.go index ba2bab6ec..02df36dce 100644 --- a/chain/types/voucher.go +++ b/chain/types/voucher.go @@ -1,66 +1,13 @@ package types import ( - "bytes" "encoding/base64" - "github.com/filecoin-project/go-address" - cborrpc "github.com/filecoin-project/go-cbor-util" + "github.com/filecoin-project/specs-actors/actors/builtin/paych" cbor "github.com/ipfs/go-ipld-cbor" ) -type SignedVoucher struct { - TimeLock uint64 - SecretPreimage []byte - Extra *ModVerifyParams - Lane uint64 - Nonce uint64 - Amount BigInt - MinCloseHeight uint64 - - Merges []Merge - - Signature *Signature -} - -func (sv *SignedVoucher) SigningBytes() ([]byte, error) { - osv := *sv - osv.Signature = nil - - buf := new(bytes.Buffer) - if err := osv.MarshalCBOR(buf); err != nil { - return nil, err - } - - return buf.Bytes(), nil -} - -func (sv *SignedVoucher) EncodedString() (string, error) { - buf := new(bytes.Buffer) - if err := sv.MarshalCBOR(buf); err != nil { - return "", err - } - - return base64.RawURLEncoding.EncodeToString(buf.Bytes()), nil -} - -func (sv *SignedVoucher) Equals(other *SignedVoucher) bool { - // TODO: make this less bad - - selfB, err := cborrpc.Dump(sv) - if err != nil { - log.Errorf("SignedVoucher.Equals: dump self: %s", err) - return false - } - - otherB, err := cborrpc.Dump(other) - if err != nil { - log.Errorf("SignedVoucher.Equals: dump other: %s", err) - return false - } - - return bytes.Equal(selfB, otherB) -} +type SignedVoucher = paych.SignedVoucher func DecodeSignedVoucher(s string) (*SignedVoucher, error) { data, err := base64.RawURLEncoding.DecodeString(s) @@ -76,13 +23,5 @@ func DecodeSignedVoucher(s string) (*SignedVoucher, error) { return &sv, nil } -type Merge struct { - Lane uint64 - Nonce uint64 -} - -type ModVerifyParams struct { - Actor address.Address - Method uint64 - Data []byte -} +type Merge = paych.Merge +type ModVerifyParams = paych.ModVerifyParams diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 2ae7f273f..142d397a3 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -8,6 +8,7 @@ import ( "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/builtin/account" + "github.com/filecoin-project/specs-actors/actors/crypto" block "github.com/ipfs/go-block-format" cid "github.com/ipfs/go-cid" hamt "github.com/ipfs/go-hamt-ipld" @@ -187,7 +188,7 @@ func (vmc *VMContext) StateTree() (types.StateTree, aerrors.ActorError) { const GasVerifySignature = 50 -func (vmctx *VMContext) VerifySignature(sig *types.Signature, act address.Address, data []byte) aerrors.ActorError { +func (vmctx *VMContext) VerifySignature(sig *crypto.Signature, act address.Address, data []byte) aerrors.ActorError { if err := vmctx.ChargeGas(GasVerifySignature); err != nil { return err } diff --git a/chain/wallet/wallet.go b/chain/wallet/wallet.go index df860b045..57311e800 100644 --- a/chain/wallet/wallet.go +++ b/chain/wallet/wallet.go @@ -6,6 +6,7 @@ import ( "strings" "sync" + "github.com/filecoin-project/specs-actors/actors/crypto" logging "github.com/ipfs/go-log/v2" "golang.org/x/xerrors" @@ -52,7 +53,7 @@ func KeyWallet(keys ...*Key) *Wallet { } } -func (w *Wallet) Sign(ctx context.Context, addr address.Address, msg []byte) (*types.Signature, error) { +func (w *Wallet) Sign(ctx context.Context, addr address.Address, msg []byte) (*crypto.Signature, error) { ki, err := w.findKey(addr) if err != nil { return nil, err @@ -179,7 +180,7 @@ func (w *Wallet) SetDefault(a address.Address) error { return nil } -func GenerateKey(typ string) (*Key, error) { +func GenerateKey(typ crypto.SigType) (*Key, error) { pk, err := sigs.Generate(typ) if err != nil { return nil, err @@ -191,7 +192,7 @@ func GenerateKey(typ string) (*Key, error) { return NewKey(ki) } -func (w *Wallet) GenerateKey(typ string) (address.Address, error) { +func (w *Wallet) GenerateKey(typ crypto.SigType) (address.Address, error) { w.lk.Lock() defer w.lk.Unlock() @@ -246,12 +247,12 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) { } switch k.Type { - case types.KTSecp256k1: + case crypto.SigTypeSecp256k1: k.Address, err = address.NewSecp256k1Address(k.PublicKey) if err != nil { return nil, xerrors.Errorf("converting Secp256k1 to address: %w", err) } - case types.KTBLS: + case crypto.SigTypeBLS: k.Address, err = address.NewBLSAddress(k.PublicKey) if err != nil { return nil, xerrors.Errorf("converting BLS to address: %w", err) diff --git a/cmd/lotus-seed/seed/seed.go b/cmd/lotus-seed/seed/seed.go index 3972028be..f28380e13 100644 --- a/cmd/lotus-seed/seed/seed.go +++ b/cmd/lotus-seed/seed/seed.go @@ -14,6 +14,7 @@ import ( "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/market" + "github.com/filecoin-project/specs-actors/actors/crypto" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/namespace" @@ -99,7 +100,7 @@ func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumbe return nil, nil, err } } else { - minerAddr, err = wallet.GenerateKey(types.KTBLS) + minerAddr, err = wallet.GenerateKey(crypto.SigTypeBLS) if err != nil { return nil, nil, err } diff --git a/lib/sigs/bls/init.go b/lib/sigs/bls/init.go index 1c22202c8..fe916c446 100644 --- a/lib/sigs/bls/init.go +++ b/lib/sigs/bls/init.go @@ -3,9 +3,11 @@ package bls import ( "fmt" - ffi "github.com/filecoin-project/filecoin-ffi" "github.com/filecoin-project/go-address" - "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/specs-actors/actors/crypto" + + ffi "github.com/filecoin-project/filecoin-ffi" + "github.com/filecoin-project/lotus/lib/sigs" ) @@ -48,5 +50,5 @@ func (blsSigner) Verify(sig []byte, a address.Address, msg []byte) error { } func init() { - sigs.RegisterSignature(types.KTBLS, blsSigner{}) + sigs.RegisterSignature(crypto.SigTypeBLS, blsSigner{}) } diff --git a/lib/sigs/secp/init.go b/lib/sigs/secp/init.go index 84680ac81..44728202d 100644 --- a/lib/sigs/secp/init.go +++ b/lib/sigs/secp/init.go @@ -5,9 +5,10 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-crypto" - "github.com/filecoin-project/lotus/chain/types" - "github.com/filecoin-project/lotus/lib/sigs" + crypto2 "github.com/libp2p/go-libp2p-core/crypto" "github.com/minio/blake2b-simd" + + "github.com/filecoin-project/lotus/lib/sigs" ) type secpSigner struct{} @@ -54,5 +55,5 @@ func (secpSigner) Verify(sig []byte, a address.Address, msg []byte) error { } func init() { - sigs.RegisterSignature(types.KTSecp256k1, secpSigner{}) + sigs.RegisterSignature(crypto2.Secp256k1, secpSigner{}) } diff --git a/lib/sigs/sigs.go b/lib/sigs/sigs.go index 9d1bc4b6d..e6552372c 100644 --- a/lib/sigs/sigs.go +++ b/lib/sigs/sigs.go @@ -6,13 +6,14 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/specs-actors/actors/crypto" "go.opencensus.io/trace" "golang.org/x/xerrors" ) // Sign takes in signature type, private key and message. Returns a signature for that message. // Valid sigTypes are: "secp256k1" and "bls" -func Sign(sigType string, privkey []byte, msg []byte) (*types.Signature, error) { +func Sign(sigType crypto.SigType, privkey []byte, msg []byte) (*crypto.Signature, error) { sv, ok := sigs[sigType] if !ok { return nil, fmt.Errorf("cannot sign message with signature of unsupported type: %s", sigType) @@ -22,14 +23,14 @@ func Sign(sigType string, privkey []byte, msg []byte) (*types.Signature, error) if err != nil { return nil, err } - return &types.Signature{ + return &crypto.Signature{ Type: sigType, Data: sb, }, nil } // Verify verifies signatures -func Verify(sig *types.Signature, addr address.Address, msg []byte) error { +func Verify(sig *crypto.Signature, addr address.Address, msg []byte) error { if sig == nil { return xerrors.Errorf("signature is nil") } @@ -47,7 +48,7 @@ func Verify(sig *types.Signature, addr address.Address, msg []byte) error { } // Generate generates private key of given type -func Generate(sigType string) ([]byte, error) { +func Generate(sigType crypto.SigType) ([]byte, error) { sv, ok := sigs[sigType] if !ok { return nil, fmt.Errorf("cannot generate private key of unsupported type: %s", sigType) @@ -57,7 +58,7 @@ func Generate(sigType string) ([]byte, error) { } // ToPublic converts private key to public key -func ToPublic(sigType string, pk []byte) ([]byte, error) { +func ToPublic(sigType crypto.SigType, pk []byte) ([]byte, error) { sv, ok := sigs[sigType] if !ok { return nil, fmt.Errorf("cannot generate public key of unsupported type: %s", sigType) @@ -91,12 +92,12 @@ type SigShim interface { Verify(sig []byte, a address.Address, msg []byte) error } -var sigs map[string]SigShim +var sigs map[crypto.SigType]SigShim // RegisterSig should be only used during init -func RegisterSignature(name string, vs SigShim) { +func RegisterSignature(typ crypto.SigType, vs SigShim) { if sigs == nil { - sigs = make(map[string]SigShim) + sigs = make(map[crypto.SigType]SigShim) } - sigs[name] = vs + sigs[typ] = vs } diff --git a/node/impl/full/state.go b/node/impl/full/state.go index da5218dd8..bca86746d 100644 --- a/node/impl/full/state.go +++ b/node/impl/full/state.go @@ -8,7 +8,9 @@ import ( "github.com/filecoin-project/go-amt-ipld/v2" "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/market" + "github.com/filecoin-project/specs-actors/actors/builtin/miner" samsig "github.com/filecoin-project/specs-actors/actors/builtin/multisig" cid "github.com/ipfs/go-cid" "github.com/ipfs/go-hamt-ipld" @@ -81,7 +83,7 @@ func (a *StateAPI) StateMinerPeerID(ctx context.Context, m address.Address, ts * return stmgr.GetMinerPeerID(ctx, a.StateManager, ts, m) } -func (a *StateAPI) StateMinerElectionPeriodStart(ctx context.Context, actor address.Address, ts *types.TipSet) (abi.ChainEpoch, error) { +func (a *StateAPI) StateMinerPostState(ctx context.Context, actor address.Address, ts *types.TipSet) (*miner.PoStState, error) { return stmgr.GetMinerPostState(ctx, a.StateManager, ts, actor) } @@ -94,7 +96,7 @@ func (a *StateAPI) StateMinerFaults(ctx context.Context, addr address.Address, t } func (a *StateAPI) StatePledgeCollateral(ctx context.Context, ts *types.TipSet) (types.BigInt, error) { - param, err := actors.SerializeParams(&actors.PledgeCollateralParams{Size: types.NewInt(0)}) + /*param, err := actors.SerializeParams(&actors.PledgeCollateralParams{Size: types.NewInt(0)}) if err != nil { return types.NewInt(0), err } @@ -114,7 +116,8 @@ func (a *StateAPI) StatePledgeCollateral(ctx context.Context, ts *types.TipSet) return types.NewInt(0), xerrors.Errorf("failed to get miner worker addr (exit code %d)", ret.ExitCode) } - return types.BigFromBytes(ret.Return), nil + return types.BigFromBytes(ret.Return), nil*/ + return big.Zero(), nil } func (a *StateAPI) StateCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.MethodCall, error) { diff --git a/node/impl/full/wallet.go b/node/impl/full/wallet.go index b30a55f38..e9266c788 100644 --- a/node/impl/full/wallet.go +++ b/node/impl/full/wallet.go @@ -4,6 +4,8 @@ import ( "context" "github.com/filecoin-project/go-address" + "github.com/filecoin-project/specs-actors/actors/crypto" + "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/wallet" @@ -19,7 +21,7 @@ type WalletAPI struct { Wallet *wallet.Wallet } -func (a *WalletAPI) WalletNew(ctx context.Context, typ string) (address.Address, error) { +func (a *WalletAPI) WalletNew(ctx context.Context, typ crypto.SigType) (address.Address, error) { return a.Wallet.GenerateKey(typ) } @@ -35,7 +37,7 @@ func (a *WalletAPI) WalletBalance(ctx context.Context, addr address.Address) (ty return a.StateManager.GetBalance(addr, nil) } -func (a *WalletAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*types.Signature, error) { +func (a *WalletAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) { return a.Wallet.Sign(ctx, k, msg) }