OSDN Git Service

678d2f474c778d6ef47c48bc00c8ae305878cfe1
[bytom/bytom.git] / protocol / bc / types / vote_output.go
1 package types
2
3 import (
4         "io"
5
6         "github.com/bytom/bytom/encoding/blockchain"
7         "github.com/bytom/bytom/errors"
8         "github.com/bytom/bytom/protocol/bc"
9 )
10
11 // VoteOutput satisfies the TypedOutput interface and represents a vote transaction.
12 type VoteOutput struct {
13         Vote []byte
14 }
15
16 // NewVoteOutput create a new output struct
17 func NewVoteOutput(assetID bc.AssetID, amount uint64, controlProgram []byte, vote []byte, state []byte) *TxOutput {
18         return &TxOutput{
19                 AssetVersion: 1,
20                 OutputCommitment: OutputCommitment{
21                         AssetAmount: bc.AssetAmount{
22                                 AssetId: &assetID,
23                                 Amount:  amount,
24                         },
25                         VMVersion:      1,
26                         ControlProgram: controlProgram,
27                         StateData:      state,
28                 },
29                 TypedOutput: &VoteOutput{Vote: vote},
30         }
31 }
32
33 func (v *VoteOutput) readFrom(r *blockchain.Reader) error {
34         var err error
35         if v.Vote, err = blockchain.ReadVarstr31(r); err != nil {
36                 return errors.Wrap(err, "reading vote output vote")
37         }
38         return nil
39 }
40
41 func (v *VoteOutput) writeTo(w io.Writer) error {
42         _, err := blockchain.WriteVarstr31(w, v.Vote)
43         return err
44 }
45
46 // OutputType implement the txout interface
47 func (v *VoteOutput) OutputType() uint8 { return VoteOutputType }