OSDN Git Service

add block pointer to verify tx
authorColt <colt@ColtdeMBP.lan>
Fri, 18 Aug 2017 03:12:44 +0000 (11:12 +0800)
committerColt <colt@ColtdeMBP.lan>
Fri, 18 Aug 2017 03:12:44 +0000 (11:12 +0800)
protocol/bc/bc.pb.go
protocol/bc/bc.proto
protocol/bc/coinbase.go [new file with mode: 0644]
protocol/validation/fuzz_test.go [deleted file]
protocol/validation/validation.go
protocol/validation/validation_test.go

index 70898a4..3cbfba9 100644 (file)
@@ -483,11 +483,10 @@ func (m *Nonce) GetWitnessAnchoredId() *Hash {
 }
 
 type Coinbase struct {
-       Program           *Program `protobuf:"bytes,1,opt,name=program" json:"program,omitempty"`
-       BlockId           *Hash    `protobuf:"bytes,2,opt,name=block_id,json=blockId" json:"block_id,omitempty"`
-       ExtHash           *Hash    `protobuf:"bytes,3,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"`
-       WitnessArguments  [][]byte `protobuf:"bytes,4,rep,name=witness_arguments,json=witnessArguments,proto3" json:"witness_arguments,omitempty"`
-       WitnessAnchoredId *Hash    `protobuf:"bytes,5,opt,name=witness_anchored_id,json=witnessAnchoredId" json:"witness_anchored_id,omitempty"`
+       Program          *Program `protobuf:"bytes,1,opt,name=program" json:"program,omitempty"`
+       BlockId          *Hash    `protobuf:"bytes,2,opt,name=block_id,json=blockId" json:"block_id,omitempty"`
+       ExtHash          *Hash    `protobuf:"bytes,3,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"`
+       WitnessArguments [][]byte `protobuf:"bytes,4,rep,name=witness_arguments,json=witnessArguments,proto3" json:"witness_arguments,omitempty"`
 }
 
 func (m *Coinbase) Reset()                    { *m = Coinbase{} }
@@ -523,13 +522,6 @@ func (m *Coinbase) GetWitnessArguments() [][]byte {
        return nil
 }
 
-func (m *Coinbase) GetWitnessAnchoredId() *Hash {
-       if m != nil {
-               return m.WitnessAnchoredId
-       }
-       return nil
-}
-
 type Output 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"`
index f82e9c8..5ee30ff 100644 (file)
@@ -90,7 +90,6 @@ message Coinbase {
   Hash           block_id            = 2;
   Hash           ext_hash            = 3;
   repeated bytes witness_arguments   = 4;
-  Hash           witness_anchored_id = 5;
 }
 
 message Output {
diff --git a/protocol/bc/coinbase.go b/protocol/bc/coinbase.go
new file mode 100644 (file)
index 0000000..8c50c85
--- /dev/null
@@ -0,0 +1,17 @@
+package bc
+
+import "io"
+
+func (Coinbase) typ() string { return "coinbase1" }
+func (n *Coinbase) writeForHash(w io.Writer) {
+       mustWriteForHash(w, n.Program)
+       mustWriteForHash(w, n.BlockId)
+}
+
+// NewCoinbase creates a new Coinbase.
+func NewCoinbase(p *Program, trID *Hash) *Coinbase {
+       return &Coinbase{
+               Program: p,
+               BlockId: trID,
+       }
+}
diff --git a/protocol/validation/fuzz_test.go b/protocol/validation/fuzz_test.go
deleted file mode 100644 (file)
index b0d6da1..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-package validation
-
-import (
-       "testing"
-
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/legacy"
-)
-
-func TestFuzzAssetIdNilPointer(t *testing.T) {
-       const (
-               blockchainID = `50935a092ffad7ec9fbac4f4486db6c3b8cd5b9f51cf697248584dde286a7220`
-               input        = `07300730303030303030000001302b3030303030303030303030303030303030303030303030303030303030303030303030303030303030303000253030303030303030303030303030303030303030303030303030303030303030303030303000`
-       )
-
-       var testBlockchainID bc.Hash
-       err := testBlockchainID.UnmarshalText([]byte(blockchainID))
-       if err != nil {
-               t.Fatal(err)
-       }
-
-       var tx legacy.Tx
-       err = tx.UnmarshalText([]byte(input))
-       if err != nil {
-               t.Fatal(err)
-       }
-
-       ValidateTx(tx.Tx, testBlockchainID)
-}
index fee81cd..36b51bf 100644 (file)
@@ -44,7 +44,7 @@ func (g *gasState) updateUsage(gasLeft int64) {
 // the transaction graph when validating entries.
 type validationState struct {
        // The ID of the blockchain
-       blockchainID bc.Hash
+       block *bc.Block
 
        // The enclosing transaction object
        tx *bc.Tx
@@ -255,10 +255,6 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                }
 
        case *bc.Issuance:
-               if *e.WitnessAssetDefinition.InitialBlockId != vs.blockchainID {
-                       return errors.WithDetailf(errWrongBlockchain, "current blockchain %x, asset defined on blockchain %x", vs.blockchainID.Bytes(), e.WitnessAssetDefinition.InitialBlockId.Bytes())
-               }
-
                computedAssetID := e.WitnessAssetDefinition.ComputeAssetID()
                if computedAssetID != *e.Value.AssetId {
                        return errors.WithDetailf(errMismatchedAssetID, "asset ID is %x, issuance wants %x", computedAssetID.Bytes(), e.Value.AssetId.Bytes())
@@ -542,12 +538,12 @@ func validateBlockAgainstPrev(b, prev *bc.Block) error {
 }
 
 // ValidateTx validates a transaction.
-func ValidateTx(tx *bc.Tx, initialBlockID bc.Hash) (*uint64, error) {
+func ValidateTx(tx *bc.Tx, block *bc.Block) (*uint64, error) {
        //TODO: handle the gas limit
        vs := &validationState{
-               blockchainID: initialBlockID,
-               tx:           tx,
-               entryID:      tx.ID,
+               block:   block,
+               tx:      tx,
+               entryID: tx.ID,
                gas: &gasState{
                        gasLeft: defaultGasLimit,
                },
index 35ca99a..584c3f4 100644 (file)
@@ -261,13 +261,6 @@ func TestTxValidation(t *testing.T) {
                        },
                },
                {
-                       desc: "wrong blockchain",
-                       f: func() {
-                               vs.blockchainID = *newHash(2)
-                       },
-                       err: errWrongBlockchain,
-               },
-               {
                        desc: "issuance program failure",
                        f: func() {
                                iss := txIssuance(t, tx, 0)
@@ -334,9 +327,9 @@ func TestTxValidation(t *testing.T) {
                        fixture = sample(t, nil)
                        tx = legacy.NewTx(*fixture.tx).Tx
                        vs = &validationState{
-                               blockchainID: fixture.initialBlockID,
-                               tx:           tx,
-                               entryID:      tx.ID,
+                               block:   nil,
+                               tx:      tx,
+                               entryID: tx.ID,
                                gas: &gasState{
                                        gasLeft: uint64(1000),
                                        gasUsed: 0,
@@ -364,7 +357,7 @@ func TestNoncelessIssuance(t *testing.T) {
                tx.Inputs[0].TypedInput.(*legacy.IssuanceInput).Nonce = nil
        })
 
-       _, err := ValidateTx(legacy.MapTx(&tx.TxData), bc.EmptyStringHash)
+       _, err := ValidateTx(legacy.MapTx(&tx.TxData), nil)
        if errors.Root(err) != bc.ErrMissingEntry {
                t.Fatalf("got %s, want %s", err, bc.ErrMissingEntry)
        }