OSDN Git Service

add the coinbase vm verify logic
authorColt <colt@ColtdeMBP.lan>
Fri, 18 Aug 2017 08:35:02 +0000 (16:35 +0800)
committerColt <colt@ColtdeMBP.lan>
Fri, 18 Aug 2017 08:35:02 +0000 (16:35 +0800)
protocol/bc/bc.pb.go
protocol/bc/bc.proto
protocol/bc/coinbase.go
protocol/validation/validation.go

index 8e42854..03aa354 100644 (file)
@@ -474,9 +474,7 @@ func (m *Nonce) GetWitnessAnchoredId() *Hash {
 }
 
 type Coinbase struct {
-       Program          *Program `protobuf:"bytes,1,opt,name=program" json:"program,omitempty"`
-       ExtHash          *Hash    `protobuf:"bytes,2,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"`
-       WitnessArguments [][]byte `protobuf:"bytes,3,rep,name=witness_arguments,json=witnessArguments,proto3" json:"witness_arguments,omitempty"`
+       WitnessDestination *ValueDestination `protobuf:"bytes,1,opt,name=witness_destination,json=witnessDestination" json:"witness_destination,omitempty"`
 }
 
 func (m *Coinbase) Reset()                    { *m = Coinbase{} }
@@ -484,23 +482,9 @@ func (m *Coinbase) String() string            { return proto.CompactTextString(m
 func (*Coinbase) ProtoMessage()               {}
 func (*Coinbase) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{10} }
 
-func (m *Coinbase) GetProgram() *Program {
+func (m *Coinbase) GetWitnessDestination() *ValueDestination {
        if m != nil {
-               return m.Program
-       }
-       return nil
-}
-
-func (m *Coinbase) GetExtHash() *Hash {
-       if m != nil {
-               return m.ExtHash
-       }
-       return nil
-}
-
-func (m *Coinbase) GetWitnessArguments() [][]byte {
-       if m != nil {
-               return m.WitnessArguments
+               return m.WitnessDestination
        }
        return nil
 }
index 0494ac9..8eea3c9 100644 (file)
@@ -85,9 +85,7 @@ message Nonce {
 }
 
 message Coinbase {
-  Program        program             = 1;
-  Hash           ext_hash            = 2;
-  repeated bytes witness_arguments   = 3;
+  ValueDestination witness_destination = 1;
 }
 
 message Output {
index 6e1e780..fa51429 100644 (file)
@@ -3,13 +3,17 @@ package bc
 import "io"
 
 func (Coinbase) typ() string { return "coinbase1" }
-func (n *Coinbase) writeForHash(w io.Writer) {
-       mustWriteForHash(w, n.Program)
+func (c *Coinbase) writeForHash(w io.Writer) {
+       mustWriteForHash(w, c.WitnessDestination)
 }
 
 // NewCoinbase creates a new Coinbase.
-func NewCoinbase(p *Program) *Coinbase {
+func NewCoinbase(id *Hash, val *AssetAmount, pos uint64) *Coinbase {
        return &Coinbase{
-               Program: p,
+               WitnessDestination: &ValueDestination{
+                       Ref:      id,
+                       Value:    val,
+                       Position: pos,
+               },
        }
 }
index 414f229..81f1a90 100644 (file)
@@ -127,6 +127,13 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        return errWrongCoinbaseTransaction
                }
 
+               vs2 := *vs
+               vs2.destPos = 0
+               err = checkValidDest(&vs2, e.WitnessDestination)
+               if err != nil {
+                       return errors.Wrap(err, "checking coinbase destination")
+               }
+
        case *bc.Mux:
                parity := make(map[bc.AssetID]int64)
                for i, src := range e.Sources {
@@ -351,6 +358,11 @@ func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
 
        var dest *bc.ValueDestination
        switch ref := e.(type) {
+       case *bc.Coinbase:
+               if vs.Position != 0 {
+                       return errors.Wrapf(errPosition, "invalid position %d for coinbase source", vs.Position)
+               }
+               dest = ref.WitnessDestination
        case *bc.Issuance:
                if vs.Position != 0 {
                        return errors.Wrapf(errPosition, "invalid position %d for issuance source", vs.Position)
@@ -370,7 +382,7 @@ func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
                dest = ref.WitnessDestinations[vs.Position]
 
        default:
-               return errors.Wrapf(bc.ErrEntryType, "value source is %T, should be issuance, spend, or mux", e)
+               return errors.Wrapf(bc.ErrEntryType, "value source is %T, should be coinbase, issuance, spend, or mux", e)
        }
 
        if dest.Ref == nil || *dest.Ref != vstate.entryID {