OSDN Git Service

add nonce and bits into the block header
authorColt <colt@ColtdeMBP.lan>
Thu, 17 Aug 2017 04:23:14 +0000 (12:23 +0800)
committerColt <colt@ColtdeMBP.lan>
Thu, 17 Aug 2017 04:23:14 +0000 (12:23 +0800)
protocol/bc/bc.pb.go
protocol/bc/blockheader.go
protocol/bc/legacy/block_commitment.go
protocol/bc/legacy/block_header.go
protocol/bc/legacy/block_witness.go [deleted file]
protocol/bc/legacy/map.go
protocol/validation/block_test.go
protocol/validation/validation.go
protocol/validation/validation_test.go

index 3119892..0794699 100644 (file)
@@ -273,15 +273,14 @@ func (m *ValueDestination) GetPosition() uint64 {
 }
 
 type BlockHeader struct {
-       Version              uint64   `protobuf:"varint,1,opt,name=version" json:"version,omitempty"`
-       Height               uint64   `protobuf:"varint,2,opt,name=height" json:"height,omitempty"`
-       PreviousBlockId      *Hash    `protobuf:"bytes,3,opt,name=previous_block_id,json=previousBlockId" json:"previous_block_id,omitempty"`
-       TimestampMs          uint64   `protobuf:"varint,4,opt,name=timestamp_ms,json=timestampMs" json:"timestamp_ms,omitempty"`
-       TransactionsRoot     *Hash    `protobuf:"bytes,5,opt,name=transactions_root,json=transactionsRoot" json:"transactions_root,omitempty"`
-       AssetsRoot           *Hash    `protobuf:"bytes,6,opt,name=assets_root,json=assetsRoot" json:"assets_root,omitempty"`
-       NextConsensusProgram []byte   `protobuf:"bytes,7,opt,name=next_consensus_program,json=nextConsensusProgram,proto3" json:"next_consensus_program,omitempty"`
-       ExtHash              *Hash    `protobuf:"bytes,8,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"`
-       WitnessArguments     [][]byte `protobuf:"bytes,9,rep,name=witness_arguments,json=witnessArguments,proto3" json:"witness_arguments,omitempty"`
+       Version          uint64 `protobuf:"varint,1,opt,name=version" json:"version,omitempty"`
+       Height           uint64 `protobuf:"varint,2,opt,name=height" json:"height,omitempty"`
+       PreviousBlockId  *Hash  `protobuf:"bytes,3,opt,name=previous_block_id,json=previousBlockId" json:"previous_block_id,omitempty"`
+       TimestampMs      uint64 `protobuf:"varint,4,opt,name=timestamp_ms,json=timestampMs" json:"timestamp_ms,omitempty"`
+       TransactionsRoot *Hash  `protobuf:"bytes,5,opt,name=transactions_root,json=transactionsRoot" json:"transactions_root,omitempty"`
+       AssetsRoot       *Hash  `protobuf:"bytes,6,opt,name=assets_root,json=assetsRoot" json:"assets_root,omitempty"`
+       Nonce            uint64 `protobuf:"varint,7,opt,name=nonce" json:"nonce,omitempty"`
+       Bits             uint64 `protobuf:"varint,8,opt,name=bits" json:"bits,omitempty"`
 }
 
 func (m *BlockHeader) Reset()                    { *m = BlockHeader{} }
@@ -331,27 +330,6 @@ func (m *BlockHeader) GetAssetsRoot() *Hash {
        return nil
 }
 
-func (m *BlockHeader) GetNextConsensusProgram() []byte {
-       if m != nil {
-               return m.NextConsensusProgram
-       }
-       return nil
-}
-
-func (m *BlockHeader) GetExtHash() *Hash {
-       if m != nil {
-               return m.ExtHash
-       }
-       return nil
-}
-
-func (m *BlockHeader) GetWitnessArguments() [][]byte {
-       if m != nil {
-               return m.WitnessArguments
-       }
-       return nil
-}
-
 type TxHeader struct {
        Version   uint64  `protobuf:"varint,1,opt,name=version" json:"version,omitempty"`
        ResultIds []*Hash `protobuf:"bytes,2,rep,name=result_ids,json=resultIds" json:"result_ids,omitempty"`
index 4e76347..0eb22b2 100644 (file)
@@ -13,20 +13,19 @@ func (bh *BlockHeader) writeForHash(w io.Writer) {
        mustWriteForHash(w, bh.TimestampMs)
        mustWriteForHash(w, bh.TransactionsRoot)
        mustWriteForHash(w, bh.AssetsRoot)
-       mustWriteForHash(w, bh.NextConsensusProgram)
-       mustWriteForHash(w, bh.ExtHash)
 }
 
 // NewBlockHeader creates a new BlockHeader and populates
 // its body.
-func NewBlockHeader(version, height uint64, previousBlockID *Hash, timestampMS uint64, transactionsRoot, assetsRoot *Hash, nextConsensusProgram []byte) *BlockHeader {
+func NewBlockHeader(version, height uint64, previousBlockID *Hash, timestampMS uint64, transactionsRoot, assetsRoot *Hash, nonce, bits uint64) *BlockHeader {
        return &BlockHeader{
-               Version:              version,
-               Height:               height,
-               PreviousBlockId:      previousBlockID,
-               TimestampMs:          timestampMS,
-               TransactionsRoot:     transactionsRoot,
-               AssetsRoot:           assetsRoot,
-               NextConsensusProgram: nextConsensusProgram,
+               Version:          version,
+               Height:           height,
+               PreviousBlockId:  previousBlockID,
+               TimestampMs:      timestampMS,
+               TransactionsRoot: transactionsRoot,
+               AssetsRoot:       assetsRoot,
+               Nonce:            nonce,
+               Bits:             bits,
        }
 }
index a28854c..e2d4e28 100644 (file)
@@ -17,9 +17,6 @@ type BlockCommitment struct {
        // the set of unspent outputs with asset version 1 after applying
        // the block.
        AssetsMerkleRoot bc.Hash
-
-       // ConsensusProgram is the predicate for validating the next block.
-       ConsensusProgram []byte
 }
 
 func (bc *BlockCommitment) readFrom(r *blockchain.Reader) error {
@@ -31,7 +28,6 @@ func (bc *BlockCommitment) readFrom(r *blockchain.Reader) error {
        if err != nil {
                return err
        }
-       bc.ConsensusProgram, err = blockchain.ReadVarstr31(r)
        return err
 }
 
@@ -44,6 +40,5 @@ func (bc *BlockCommitment) writeTo(w io.Writer) error {
        if err != nil {
                return err
        }
-       _, err = blockchain.WriteVarstr31(w, bc.ConsensusProgram)
        return err
 }
index 3aa16e3..f64113a 100644 (file)
@@ -32,10 +32,9 @@ type BlockHeader struct {
        TimestampMS uint64
 
        BlockCommitment
-       CommitmentSuffix []byte
 
-       BlockWitness
-       WitnessSuffix []byte
+       Nonce uint64
+       Bits  uint64
 }
 
 // Time returns the time represented by the Timestamp in bh.
@@ -128,19 +127,14 @@ func (bh *BlockHeader) readFrom(r *blockchain.Reader) (uint8, error) {
                return 0, err
        }
 
-       bh.CommitmentSuffix, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom)
+       bh.Nonce, err = blockchain.ReadVarint63(r)
        if err != nil {
                return 0, err
        }
 
-       if serflags[0]&SerBlockWitness == SerBlockWitness {
-               bh.WitnessSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) (err error) {
-                       bh.Witness, err = blockchain.ReadVarstrList(r)
-                       return err
-               })
-               if err != nil {
-                       return 0, err
-               }
+       bh.Bits, err = blockchain.ReadVarint63(r)
+       if err != nil {
+               return 0, err
        }
 
        return serflags[0], nil
@@ -174,18 +168,13 @@ func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) error {
        if err != nil {
                return err
        }
-
-       _, err = blockchain.WriteExtensibleString(w, bh.CommitmentSuffix, bh.BlockCommitment.writeTo)
+       _, err = blockchain.WriteVarint63(w, bh.Nonce)
        if err != nil {
                return err
        }
-
-       if serflags&SerBlockWitness == SerBlockWitness {
-               _, err = blockchain.WriteExtensibleString(w, bh.WitnessSuffix, bh.BlockWitness.writeTo)
-               if err != nil {
-                       return err
-               }
+       _, err = blockchain.WriteVarint63(w, bh.Bits)
+       if err != nil {
+               return err
        }
-
        return nil
 }
diff --git a/protocol/bc/legacy/block_witness.go b/protocol/bc/legacy/block_witness.go
deleted file mode 100644 (file)
index 41ab115..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-package legacy
-
-import (
-       "io"
-
-       "github.com/blockchain/encoding/blockchain"
-)
-
-type BlockWitness struct {
-       // Witness is a vector of arguments to the previous block's
-       // ConsensusProgram for validating this block.
-       Witness [][]byte
-}
-
-func (bw *BlockWitness) writeTo(w io.Writer) error {
-       _, err := blockchain.WriteVarstrList(w, bw.Witness)
-       return err
-}
index d9bbb54..994fa06 100644 (file)
@@ -217,8 +217,7 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
 }
 
 func mapBlockHeader(old *BlockHeader) (bhID bc.Hash, bh *bc.BlockHeader) {
-       bh = bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.TimestampMS, &old.TransactionsMerkleRoot, &old.AssetsMerkleRoot, old.ConsensusProgram)
-       bh.WitnessArguments = old.Witness
+       bh = bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.TimestampMS, &old.TransactionsMerkleRoot, &old.AssetsMerkleRoot, old.Nonce, old.Bits)
        bhID = bc.EntryID(bh)
        return
 }
index e9176bb..268439c 100644 (file)
@@ -18,9 +18,7 @@ func generate(tb testing.TB, prev *bc.Block) *bc.Block {
                        Height:            prev.Height + 1,
                        PreviousBlockHash: prev.ID,
                        TimestampMS:       prev.TimestampMs + 1,
-                       BlockCommitment: legacy.BlockCommitment{
-                               ConsensusProgram: prev.NextConsensusProgram,
-                       },
+                       BlockCommitment:   legacy.BlockCommitment{},
                },
        }
 
index dedab45..67a2c98 100644 (file)
@@ -360,13 +360,6 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
        return nil
 }
 
-func checkValidBlockHeader(bh *bc.BlockHeader) error {
-       if bh.Version == 1 && bh.ExtHash != nil && !bh.ExtHash.IsZero() {
-               return errNonemptyExtHash
-       }
-       return nil
-}
-
 func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
        if vs == nil {
                return errors.Wrap(errMissingField, "empty value source")
@@ -503,11 +496,6 @@ func ValidateBlock(b, prev *bc.Block, initialBlockID bc.Hash, validateTx func(*b
                }
        }
 
-       err := checkValidBlockHeader(b.BlockHeader)
-       if err != nil {
-               return errors.Wrap(err, "checking block header")
-       }
-
        for i, tx := range b.Transactions {
                if b.Version == 1 && tx.Version != 1 {
                        return errors.WithDetailf(errTxVersion, "block version %d, transaction version %d", b.Version, tx.Version)
@@ -519,7 +507,7 @@ func ValidateBlock(b, prev *bc.Block, initialBlockID bc.Hash, validateTx func(*b
                        return errors.WithDetailf(errUntimelyTransaction, "block timestamp %d, transaction time range %d-%d", b.TimestampMs, tx.MinTimeMs, tx.MaxTimeMs)
                }
 
-               err = validateTx(tx)
+               err := validateTx(tx)
                if err != nil {
                        return errors.Wrapf(err, "validity of transaction %d of %d", i, len(b.Transactions))
                }
@@ -544,6 +532,7 @@ func validateBlockAgainstPrev(b, prev *bc.Block) error {
        if b.Height != prev.Height+1 {
                return errors.WithDetailf(errMisorderedBlockHeight, "previous block height %d, current block height %d", prev.Height, b.Height)
        }
+
        if prev.ID != *b.PreviousBlockId {
                return errors.WithDetailf(errMismatchedBlock, "previous block ID %x, current block wants %x", prev.ID.Bytes(), b.PreviousBlockId.Bytes())
        }
index d48c12b..e369d6c 100644 (file)
@@ -372,7 +372,7 @@ func TestNoncelessIssuance(t *testing.T) {
 }
 
 func TestBlockHeaderValid(t *testing.T) {
-       base := bc.NewBlockHeader(1, 1, &bc.Hash{}, 1, &bc.Hash{}, &bc.Hash{}, nil)
+       base := bc.NewBlockHeader(1, 1, &bc.Hash{}, 1, &bc.Hash{}, &bc.Hash{}, 0, 0)
        baseBytes, _ := proto.Marshal(base)
 
        var bh bc.BlockHeader
@@ -387,12 +387,6 @@ func TestBlockHeaderValid(t *testing.T) {
                                bh.Version = 2
                        },
                },
-               {
-                       f: func() {
-                               bh.ExtHash = newHash(1)
-                       },
-                       err: errNonemptyExtHash,
-               },
        }
 
        for i, c := range cases {
@@ -401,10 +395,6 @@ func TestBlockHeaderValid(t *testing.T) {
                        if c.f != nil {
                                c.f()
                        }
-                       err := checkValidBlockHeader(&bh)
-                       if err != c.err {
-                               t.Errorf("got error %s, want %s; bh is:\n%s", err, c.err, spew.Sdump(bh))
-                       }
                })
        }
 }