From 58d0b58b6174e2eb2e694c232e0a8d1f59afea49 Mon Sep 17 00:00:00 2001 From: wz Date: Wed, 15 May 2019 12:54:56 +0800 Subject: [PATCH] votetx output (#62) * refactor: move ComputeOutputID() for SpendCommitment * wip: init OutputType * refactor: rename NewTxOutput to NewIntraChainTxOutput * wip: typed intra-chain output * feat: add CrossChainTxOutput * fix: fix CrossChainOutputType * doc: update todo * init bc * fix * wip: bc NewIntraChainOutput * fix: bc IntraChainOutput * fix: fix tx.Output() * move ComputeOutputID back * fix types.NewIntraChainTxOutput & types.NewCrossChainTxOutput * doc: update todo * fix: add VMVersion, ControlProgram and AssetAmount for types.Output * fix map.go * fix txfeed * fix peer.go * fi * rename * fix TxOutput.readFrom() * fix TxOutput.readFrom() * use currentAssetVersion * clean * update todos * add cross-chain in mapTx.go * doc: update comments * wip: check IntraChainOutput source in validation * wip: checkValidDest() checks CrossChainOutput * fix checkStandardTx * fix: fix checkStandardTx * fix: fix TxOutput.writeTo() * fix hard-coded AssetVersion * roll back assetVersion * refactor: rename ComputeOutputID * refactor: rename ComputeOutputID() * fix blockchain/txbuilder/txbuilder_test.go * fix: fix crosschainOut as Input * doc: fix comments * wip * clean * fix * fix * fix * Add vote output for types and bc * add uint test * Add uint test * modify name * fix review --- protocol/bc/bc.pb.go | 167 +++++++++++++++++++++++-------------- protocol/bc/bc.proto | 7 ++ protocol/bc/tx.go | 13 +++ protocol/bc/types/map.go | 7 ++ protocol/bc/types/txoutput.go | 33 ++++++++ protocol/bc/types/txoutput_test.go | 46 ++++++++++ protocol/bc/types/vote_txoutput.go | 33 ++++++++ protocol/bc/vote_output.go | 20 +++++ 8 files changed, 264 insertions(+), 62 deletions(-) create mode 100644 protocol/bc/types/vote_txoutput.go create mode 100644 protocol/bc/vote_output.go diff --git a/protocol/bc/bc.pb.go b/protocol/bc/bc.pb.go index 9e31f8ec..e42656d7 100644 --- a/protocol/bc/bc.pb.go +++ b/protocol/bc/bc.pb.go @@ -23,6 +23,7 @@ It has these top-level messages: Coinbase IntraChainOutput CrossChainOutput + VoteOutput Retirement Issuance Spend @@ -545,6 +546,46 @@ func (m *CrossChainOutput) GetOrdinal() uint64 { return 0 } +type VoteOutput struct { + Source *ValueSource `protobuf:"bytes,1,opt,name=source" json:"source,omitempty"` + ControlProgram *Program `protobuf:"bytes,2,opt,name=control_program,json=controlProgram" json:"control_program,omitempty"` + Ordinal uint64 `protobuf:"varint,3,opt,name=ordinal" json:"ordinal,omitempty"` + Vote []byte `protobuf:"bytes,4,opt,name=vote,proto3" json:"vote,omitempty"` +} + +func (m *VoteOutput) Reset() { *m = VoteOutput{} } +func (m *VoteOutput) String() string { return proto.CompactTextString(m) } +func (*VoteOutput) ProtoMessage() {} +func (*VoteOutput) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{15} } + +func (m *VoteOutput) GetSource() *ValueSource { + if m != nil { + return m.Source + } + return nil +} + +func (m *VoteOutput) GetControlProgram() *Program { + if m != nil { + return m.ControlProgram + } + return nil +} + +func (m *VoteOutput) GetOrdinal() uint64 { + if m != nil { + return m.Ordinal + } + return 0 +} + +func (m *VoteOutput) GetVote() []byte { + if m != nil { + return m.Vote + } + return nil +} + type Retirement struct { Source *ValueSource `protobuf:"bytes,1,opt,name=source" json:"source,omitempty"` Ordinal uint64 `protobuf:"varint,2,opt,name=ordinal" json:"ordinal,omitempty"` @@ -553,7 +594,7 @@ type Retirement struct { func (m *Retirement) Reset() { *m = Retirement{} } func (m *Retirement) String() string { return proto.CompactTextString(m) } func (*Retirement) ProtoMessage() {} -func (*Retirement) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{15} } +func (*Retirement) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{16} } func (m *Retirement) GetSource() *ValueSource { if m != nil { @@ -581,7 +622,7 @@ type Issuance struct { func (m *Issuance) Reset() { *m = Issuance{} } func (m *Issuance) String() string { return proto.CompactTextString(m) } func (*Issuance) ProtoMessage() {} -func (*Issuance) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{16} } +func (*Issuance) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{17} } func (m *Issuance) GetNonceHash() *Hash { if m != nil { @@ -635,7 +676,7 @@ type Spend struct { func (m *Spend) Reset() { *m = Spend{} } func (m *Spend) String() string { return proto.CompactTextString(m) } func (*Spend) ProtoMessage() {} -func (*Spend) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{17} } +func (*Spend) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{18} } func (m *Spend) GetSpentOutputId() *Hash { if m != nil { @@ -681,6 +722,7 @@ func init() { proto.RegisterType((*Coinbase)(nil), "bc.Coinbase") proto.RegisterType((*IntraChainOutput)(nil), "bc.IntraChainOutput") proto.RegisterType((*CrossChainOutput)(nil), "bc.CrossChainOutput") + proto.RegisterType((*VoteOutput)(nil), "bc.VoteOutput") proto.RegisterType((*Retirement)(nil), "bc.Retirement") proto.RegisterType((*Issuance)(nil), "bc.Issuance") proto.RegisterType((*Spend)(nil), "bc.Spend") @@ -689,63 +731,64 @@ func init() { func init() { proto.RegisterFile("bc.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 921 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x56, 0xcd, 0x6e, 0xdb, 0x46, - 0x10, 0x86, 0x28, 0x5a, 0x92, 0x47, 0x89, 0x25, 0xad, 0x93, 0x94, 0x08, 0x52, 0xd4, 0x20, 0x90, - 0x3a, 0x45, 0x01, 0xc3, 0x96, 0xd3, 0xf6, 0xd2, 0x43, 0x5d, 0xbb, 0x69, 0x74, 0x30, 0x52, 0xac, - 0x0d, 0x5f, 0x89, 0x15, 0xb9, 0x92, 0x16, 0xa5, 0xb8, 0xea, 0xee, 0x52, 0x75, 0xfc, 0x0a, 0x3d, - 0xf7, 0x59, 0xfa, 0x08, 0x3d, 0xf5, 0x99, 0x5a, 0xec, 0x70, 0x29, 0x51, 0x3f, 0xce, 0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4, 0x81, 0xbb, 0xfd, 0x9e, 0x76, 0xa9, - 0x7f, 0x64, 0xbb, 0x5c, 0x42, 0xb0, 0x68, 0x57, 0x7c, 0x98, 0x93, 0xc5, 0xcb, 0x8a, 0xdb, 0xb9, - 0xdd, 0xdf, 0x5f, 0x24, 0xb0, 0x7c, 0x74, 0xe9, 0x93, 0xb2, 0x95, 0xd7, 0x1e, 0xe3, 0xad, 0xdd, - 0xbf, 0xb3, 0xbd, 0xfb, 0xab, 0xcc, 0x35, 0x56, 0x99, 0xfb, 0xb3, 0x06, 0x3b, 0x57, 0x33, 0x9e, - 0x25, 0xe4, 0x18, 0x3a, 0x7a, 0xc6, 0x33, 0x13, 0x49, 0xec, 0x8e, 0xe5, 0x7f, 0xc3, 0x92, 0xbb, - 0x87, 0xe8, 0x50, 0x74, 0xcf, 0x20, 0xb9, 0x8f, 0x18, 0xef, 0x23, 0x89, 0xd9, 0x5a, 0x49, 0xfd, - 0xfd, 0x95, 0xf8, 0x2b, 0x95, 0x0c, 0x1b, 0xf8, 0x6f, 0x77, 0xfa, 0x4f, 0x00, 0x00, 0x00, 0xff, - 0xff, 0xc9, 0x1e, 0x5d, 0xa5, 0xe7, 0x09, 0x00, 0x00, + // 943 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x56, 0xcd, 0x6e, 0x23, 0x45, + 0x10, 0x96, 0xc7, 0x13, 0xdb, 0x29, 0x67, 0xe3, 0xb8, 0xb3, 0xbb, 0x8c, 0x56, 0x8b, 0x88, 0x46, + 0x5a, 0xb2, 0x08, 0x29, 0x4a, 0x9c, 0x05, 0x2e, 0x1c, 0x08, 0x09, 0xcb, 0xfa, 0x10, 0x2d, 0xea, + 0x44, 0xb9, 0x8e, 0xda, 0x9e, 0xb6, 0xdd, 0x62, 0x3c, 0x6d, 0xba, 0x7b, 0x86, 0x6c, 0x5e, 0x81, + 0x33, 0x07, 0x9e,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, 0xb8, + 0xed, 0x9b, 0xf7, 0x4f, 0xad, 0x12, 0xc4, 0x5b, 0x2e, 0xf6, 0x77, 0x0f, 0x5a, 0x7d, 0xf7, 0x22, + 0xda, 0xd5, 0x90, 0x4a, 0xfb, 0x7e, 0xe2, 0xc6, 0x5c, 0x7d, 0x71, 0xb6, 0xd1, 0x86, 0x5b, 0xf2, + 0x3d, 0xdf, 0x9d, 0x7b, 0x5a, 0xb9, 0xfe, 0x81, 0xad, 0x7c, 0x09, 0xc1, 0x7c, 0x94, 0xf0, 0xa7, + 0x21, 0x9e, 0xbf, 0xfa, 0x48, 0x4e, 0xbb, 0xb7, 0x3f, 0x4f, 0x60, 0xf1, 0x43, 0x40, 0x1f, 0x97, + 0x63, 0xb6, 0xf2, 0xa3, 0xb0, 0x71, 0x32, 0xb7, 0x36, 0x4f, 0x66, 0x95, 0xb9, 0xc6, 0x32, 0x73, + 0x7f, 0xd4, 0x60, 0xeb, 0x6a, 0xc6, 0xd3, 0x98, 0x1c, 0x43, 0x47, 0xcf, 0x78, 0x6a, 0x22, 0x89, + 0x1d, 0xb3, 0xf8, 0xa7, 0x59, 0x70, 0xf7, 0x00, 0x1d, 0x8a, 0x8e, 0xea, 0xc7, 0xf7, 0x11, 0xe3, + 0x7d, 0x20, 0x31, 0x1b, 0x2b, 0xa9, 0xbf, 0xbb, 0x12, 0x7f, 0xa9, 0x92, 0x41, 0x03, 0xff, 0x3b, + 0x4f, 0xff, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x9e, 0xe2, 0x40, 0x89, 0x83, 0x0a, 0x00, 0x00, } diff --git a/protocol/bc/bc.proto b/protocol/bc/bc.proto index 00eda9ae..0aba4b7b 100644 --- a/protocol/bc/bc.proto +++ b/protocol/bc/bc.proto @@ -99,6 +99,13 @@ message CrossChainOutput { uint64 ordinal = 3; } +message VoteOutput { + ValueSource source = 1; + Program control_program = 2; + uint64 ordinal = 3; + bytes vote = 4; +} + message Retirement { ValueSource source = 1; uint64 ordinal = 2; diff --git a/protocol/bc/tx.go b/protocol/bc/tx.go index c07ed0f1..cf5f763b 100644 --- a/protocol/bc/tx.go +++ b/protocol/bc/tx.go @@ -84,3 +84,16 @@ func (tx *Tx) Issuance(id Hash) (*Issuance, error) { } return iss, nil } + +// VoteOutput try to get the vote output entry by given hash +func (tx *Tx) VoteOutput(id Hash) (*VoteOutput, error) { + e, ok := tx.Entries[id] + if !ok || e == nil { + return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) + } + o, ok := e.(*VoteOutput) + if !ok { + return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) + } + return o, nil +} diff --git a/protocol/bc/types/map.go b/protocol/bc/types/map.go index ae92eb74..34ba49cb 100644 --- a/protocol/bc/types/map.go +++ b/protocol/bc/types/map.go @@ -172,6 +172,13 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash o := bc.NewCrossChainOutput(src, prog, uint64(i)) resultID = addEntry(o) + case out.OutputType() == VoteOutputType: + // non-retirement vote tx + voteOut, _ := out.TypedOutput.(*VoteTxOutput) + prog := &bc.Program{out.VMVersion(), out.ControlProgram()} + o := bc.NewVoteOutput(src, prog, uint64(i), voteOut.Vote) + resultID = addEntry(o) + default: log.Warn("unknown outType") } diff --git a/protocol/bc/types/txoutput.go b/protocol/bc/types/txoutput.go index 620abab4..7cc2b618 100644 --- a/protocol/bc/types/txoutput.go +++ b/protocol/bc/types/txoutput.go @@ -13,6 +13,7 @@ import ( const ( IntraChainOutputType uint8 = iota CrossChainOutputType + VoteOutputType ) type ( @@ -39,6 +40,9 @@ func (to *TxOutput) OutputCommitment() OutputCommitment { case *CrossChainOutput: return outp.OutputCommitment + case *VoteTxOutput: + return outp.OutputCommitment + default: return OutputCommitment{} } @@ -53,6 +57,9 @@ func (to *TxOutput) AssetAmount() bc.AssetAmount { case *CrossChainOutput: return outp.AssetAmount + case *VoteTxOutput: + return outp.AssetAmount + default: return bc.AssetAmount{} } @@ -67,6 +74,9 @@ func (to *TxOutput) ControlProgram() []byte { case *CrossChainOutput: return outp.ControlProgram + case *VoteTxOutput: + return outp.ControlProgram + default: return nil } @@ -81,6 +91,9 @@ func (to *TxOutput) VMVersion() uint64 { case *CrossChainOutput: return outp.VMVersion + case *VoteTxOutput: + return outp.VMVersion + default: return 0 } @@ -116,6 +129,17 @@ func (to *TxOutput) readFrom(r *blockchain.Reader) (err error) { return errors.Wrap(err, "reading cross-chain output commitment") } + case VoteOutputType: + out := new(VoteTxOutput) + to.TypedOutput = out + if out.Vote, err = blockchain.ReadVarstr31(r); err != nil { + return errors.Wrap(err, "reading vote output vote") + } + + if out.CommitmentSuffix, err = out.OutputCommitment.readFrom(r, to.AssetVersion); err != nil { + return errors.Wrap(err, "reading vote output commitment") + } + default: return fmt.Errorf("unsupported output type %d", outType[0]) } @@ -166,6 +190,15 @@ func (to *TxOutput) writeOutputCommitment(w io.Writer) error { } return outp.OutputCommitment.writeExtensibleString(w, outp.CommitmentSuffix, to.AssetVersion) + case *VoteTxOutput: + if _, err := w.Write([]byte{VoteOutputType}); err != nil { + return err + } + if _, err := blockchain.WriteVarstr31(w, outp.Vote); err != nil { + return err + } + return outp.OutputCommitment.writeExtensibleString(w, outp.CommitmentSuffix, to.AssetVersion) + default: return nil } diff --git a/protocol/bc/types/txoutput_test.go b/protocol/bc/types/txoutput_test.go index 0c8f38af..b978073a 100644 --- a/protocol/bc/types/txoutput_test.go +++ b/protocol/bc/types/txoutput_test.go @@ -56,6 +56,52 @@ func TestSerializationTxOutput(t *testing.T) { } } +func TestSerializationVoteTxOutput(t *testing.T) { + assetID := testutil.MustDecodeAsset("81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47") + voteTxOutput := NewVoteOutput(assetID, 1000, []byte("TestSerializationTxOutput"), []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269")) + + wantHex := strings.Join([]string{ + "01", // asset version + "c101", // serialization length + "02", // outType + "8001", // output xpub length + "6166353934303036613430383337643966303238646161626236643538396466306239313338646165666164353638336535323333633236343632373932313732393461386435333265363038363362636631393636323561333566623863656566666133633039363130656239326463666236353561393437663133323639", // xpub + "3d", // output commitment length + "81756fdab39a17163b0ce582ee4ee256fb4d1e156c692b997d608a42ecb38d47", // assetID + "e807", // amount + "01", // version + "19", // control program length + "5465737453657269616c697a6174696f6e54784f7574707574", // control program + "00", // witness length + }, "") + + // Test convert struct to hex + var buffer bytes.Buffer + if err := voteTxOutput.writeTo(&buffer); err != nil { + t.Fatal(err) + } + + gotHex := hex.EncodeToString(buffer.Bytes()) + if gotHex != wantHex { + t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) + } + + // Test convert hex to struct + var gotTxOutput TxOutput + decodeHex, err := hex.DecodeString(wantHex) + if err != nil { + t.Fatal(err) + } + + if err := gotTxOutput.readFrom(blockchain.NewReader(decodeHex)); err != nil { + t.Fatal(err) + } + + if !testutil.DeepEqual(*voteTxOutput, gotTxOutput) { + t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*voteTxOutput), spew.Sdump(gotTxOutput)) + } +} + func TestComputeOutputID(t *testing.T) { btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") cases := []struct { diff --git a/protocol/bc/types/vote_txoutput.go b/protocol/bc/types/vote_txoutput.go new file mode 100644 index 00000000..a34107a4 --- /dev/null +++ b/protocol/bc/types/vote_txoutput.go @@ -0,0 +1,33 @@ +package types + +import ( + "github.com/vapor/protocol/bc" +) + +// VoteOutput satisfies the TypedOutput interface and represents a vote transaction. +type VoteTxOutput struct { + OutputCommitment + // Unconsumed suffixes of the commitment and witness extensible strings. + CommitmentSuffix []byte + Vote []byte +} + +// NewVoteOutput create a new output struct +func NewVoteOutput(assetID bc.AssetID, amount uint64, controlProgram []byte, vote []byte) *TxOutput { + return &TxOutput{ + AssetVersion: 1, + TypedOutput: &VoteTxOutput{ + OutputCommitment: OutputCommitment{ + AssetAmount: bc.AssetAmount{ + AssetId: &assetID, + Amount: amount, + }, + VMVersion: 1, + ControlProgram: controlProgram, + }, + Vote: vote, + }, + } +} + +func (it *VoteTxOutput) OutputType() uint8 { return VoteOutputType } diff --git a/protocol/bc/vote_output.go b/protocol/bc/vote_output.go new file mode 100644 index 00000000..9e574db0 --- /dev/null +++ b/protocol/bc/vote_output.go @@ -0,0 +1,20 @@ +package bc + +import "io" + +func (VoteOutput) typ() string { return "crosschainoutput1" } +func (o *VoteOutput) writeForHash(w io.Writer) { + mustWriteForHash(w, o.Source) + mustWriteForHash(w, o.ControlProgram) + mustWriteForHash(w, o.Vote) +} + +// NewCrossChainOutput creates a new CrossChainOutput. +func NewVoteOutput(source *ValueSource, controlProgram *Program, ordinal uint64, vote []byte) *VoteOutput { + return &VoteOutput{ + Source: source, + ControlProgram: controlProgram, + Ordinal: ordinal, + Vote: vote, + } +} -- 2.11.0