From 55846ac931461d36bcac345c666b1023740df9ae Mon Sep 17 00:00:00 2001 From: Colt Date: Fri, 18 Aug 2017 11:12:44 +0800 Subject: [PATCH] add block pointer to verify tx --- protocol/bc/bc.pb.go | 16 ++++------------ protocol/bc/bc.proto | 1 - protocol/bc/coinbase.go | 17 +++++++++++++++++ protocol/validation/fuzz_test.go | 29 ----------------------------- protocol/validation/validation.go | 14 +++++--------- protocol/validation/validation_test.go | 15 ++++----------- 6 files changed, 30 insertions(+), 62 deletions(-) create mode 100644 protocol/bc/coinbase.go delete mode 100644 protocol/validation/fuzz_test.go diff --git a/protocol/bc/bc.pb.go b/protocol/bc/bc.pb.go index 70898a46..3cbfba97 100644 --- a/protocol/bc/bc.pb.go +++ b/protocol/bc/bc.pb.go @@ -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"` diff --git a/protocol/bc/bc.proto b/protocol/bc/bc.proto index f82e9c81..5ee30ffa 100644 --- a/protocol/bc/bc.proto +++ b/protocol/bc/bc.proto @@ -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 index 00000000..8c50c85a --- /dev/null +++ b/protocol/bc/coinbase.go @@ -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 index b0d6da1e..00000000 --- a/protocol/validation/fuzz_test.go +++ /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) -} diff --git a/protocol/validation/validation.go b/protocol/validation/validation.go index fee81cdb..36b51bf1 100644 --- a/protocol/validation/validation.go +++ b/protocol/validation/validation.go @@ -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, }, diff --git a/protocol/validation/validation_test.go b/protocol/validation/validation_test.go index 35ca99a2..584c3f42 100644 --- a/protocol/validation/validation_test.go +++ b/protocol/validation/validation_test.go @@ -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) } -- 2.11.0