OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / protocol / bc / types / block_header.go
index ab53157..392b3ec 100644 (file)
@@ -17,13 +17,14 @@ type BlockHeader struct {
        Version           uint64  // The version of the block.
        Height            uint64  // The height of the block.
        PreviousBlockHash bc.Hash // The hash of the previous block.
-       Timestamp         uint64  // The time of the block in seconds.
+       Timestamp         uint64  // The time of the block in milliseconds.
        BlockCommitment
+       BlockWitness
 }
 
 // Time returns the time represented by the Timestamp in block header.
 func (bh *BlockHeader) Time() time.Time {
-       return time.Unix(int64(bh.Timestamp), 0).UTC()
+       return time.Unix(0, int64(bh.Timestamp)*int64(time.Millisecond)).UTC()
 }
 
 // Hash returns complete hash of the block header.
@@ -55,8 +56,15 @@ func (bh *BlockHeader) UnmarshalText(text []byte) error {
                return err
        }
 
-       _, err := bh.readFrom(blockchain.NewReader(decoded))
-       return err
+       serflag, err := bh.readFrom(blockchain.NewReader(decoded))
+       if err != nil {
+               return err
+       }
+
+       if serflag == SerBlockTransactions {
+               return fmt.Errorf("unsupported serialization flags 0x%02x", serflag)
+       }
+       return nil
 }
 
 func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error) {
@@ -65,6 +73,8 @@ func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error)
        serflag = serflags[0]
        switch serflag {
        case SerBlockHeader, SerBlockFull:
+       case SerBlockTransactions:
+               return
        default:
                return 0, fmt.Errorf("unsupported serialization flags 0x%x", serflags)
        }
@@ -84,6 +94,10 @@ func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error)
        if _, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom); err != nil {
                return 0, err
        }
+
+       if _, err = blockchain.ReadExtensibleString(r, bh.BlockWitness.readFrom); err != nil {
+               return 0, err
+       }
        return
 }
 
@@ -113,5 +127,9 @@ func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) (err error) {
        if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockCommitment.writeTo); err != nil {
                return err
        }
+
+       if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockWitness.writeTo); err != nil {
+               return err
+       }
        return nil
 }