From 320e36e33da794b85fb6da7efaec986f440b3254 Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Thu, 15 Mar 2018 16:21:21 +0800 Subject: [PATCH] remove RefDataHash in SpendCommitment and UTXO (#428) * remove RefDataHash in SpendCommitment * remove RefDataHash in UTXO * remove RefDataHash in annotatedUTXO --- blockchain/account/builder.go | 2 +- blockchain/account/reserve.go | 1 - blockchain/query.go | 2 -- blockchain/txbuilder/txbuilder_test.go | 24 ++++++++++++------------ protocol/bc/legacy/spend.go | 12 +----------- protocol/bc/legacy/transaction_test.go | 18 +++++++++--------- protocol/bc/legacy/tx_test.go | 4 ++-- protocol/validation/validation_test.go | 8 +++----- protocol/validation/vmcontext_test.go | 11 +---------- 9 files changed, 29 insertions(+), 53 deletions(-) diff --git a/blockchain/account/builder.go b/blockchain/account/builder.go index f24bf97a..bfe42b59 100755 --- a/blockchain/account/builder.go +++ b/blockchain/account/builder.go @@ -141,7 +141,7 @@ func canceler(ctx context.Context, m *Manager, rid uint64) func() { // UtxoToInputs convert an utxo to the txinput func UtxoToInputs(signer *signers.Signer, u *UTXO) (*legacy.TxInput, *txbuilder.SigningInstruction, error) { - txInput := legacy.NewSpendInput(nil, u.SourceID, u.AssetID, u.Amount, u.SourcePos, u.ControlProgram, u.RefDataHash) + txInput := legacy.NewSpendInput(nil, u.SourceID, u.AssetID, u.Amount, u.SourcePos, u.ControlProgram) sigInst := &txbuilder.SigningInstruction{} if signer == nil { return txInput, sigInst, nil diff --git a/blockchain/account/reserve.go b/blockchain/account/reserve.go index 1d42abed..40adfb8f 100755 --- a/blockchain/account/reserve.go +++ b/blockchain/account/reserve.go @@ -47,7 +47,6 @@ type UTXO struct { SourcePos uint64 ControlProgram []byte - RefDataHash bc.Hash AccountID string Address string diff --git a/blockchain/query.go b/blockchain/query.go index 4eb86fe1..bf29f59c 100755 --- a/blockchain/query.go +++ b/blockchain/query.go @@ -143,7 +143,6 @@ type annotatedUTXO struct { Program string `json:"program"` SourceID string `json:"source_id"` SourcePos uint64 `json:"source_pos"` - RefDataHash string `json:"ref_data"` ValidHeight uint64 `json:"valid_height"` } @@ -168,7 +167,6 @@ func (bcr *BlockchainReactor) listUnspentOutputs(ctx context.Context, filter str tmpUTXO.Amount = utxo.Amount tmpUTXO.SourcePos = utxo.SourcePos tmpUTXO.Program = fmt.Sprintf("%x", utxo.ControlProgram) - tmpUTXO.RefDataHash = utxo.RefDataHash.String() tmpUTXO.ControlProgramIndex = utxo.ControlProgramIndex tmpUTXO.Address = utxo.Address tmpUTXO.ValidHeight = utxo.ValidHeight diff --git a/blockchain/txbuilder/txbuilder_test.go b/blockchain/txbuilder/txbuilder_test.go index 3c707d8a..678bf7bb 100755 --- a/blockchain/txbuilder/txbuilder_test.go +++ b/blockchain/txbuilder/txbuilder_test.go @@ -25,7 +25,7 @@ import ( type testAction bc.AssetAmount func (t testAction) Build(ctx context.Context, b *TemplateBuilder) error { - in := legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), *t.AssetId, t.Amount, 0, nil, bc.Hash{}) + in := legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), *t.AssetId, t.Amount, 0, nil) tplIn := &SigningInstruction{} err := b.AddInput(in, tplIn) @@ -61,9 +61,9 @@ func TestBuild(t *testing.T) { want := &Template{ Transaction: legacy.NewTx(legacy.TxData{ Version: 1, - SerializedSize: 396, + SerializedSize: 332, Inputs: []*legacy.TxInput{ - legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), assetID1, 5, 0, nil, bc.Hash{}), + legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), assetID1, 5, 0, nil), }, Outputs: []*legacy.TxOutput{ legacy.NewTxOutput(assetID2, 6, []byte("dest")), @@ -190,27 +190,27 @@ func TestCheckBlankCheck(t *testing.T) { want error }{{ tx: &legacy.TxData{ - Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{})}, + Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)}, }, want: ErrBlankCheck, }, { tx: &legacy.TxData{ - Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{})}, + Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)}, Outputs: []*legacy.TxOutput{legacy.NewTxOutput(bc.AssetID{}, 3, nil)}, }, want: ErrBlankCheck, }, { tx: &legacy.TxData{ Inputs: []*legacy.TxInput{ - legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{}), - legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.NewAssetID([32]byte{1}), 5, 0, nil, bc.Hash{}), + legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil), + legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.NewAssetID([32]byte{1}), 5, 0, nil), }, Outputs: []*legacy.TxOutput{legacy.NewTxOutput(bc.AssetID{}, 5, nil)}, }, want: ErrBlankCheck, }, { tx: &legacy.TxData{ - Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{})}, + Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)}, Outputs: []*legacy.TxOutput{ legacy.NewTxOutput(bc.AssetID{}, math.MaxInt64, nil), legacy.NewTxOutput(bc.AssetID{}, 7, nil), @@ -220,14 +220,14 @@ func TestCheckBlankCheck(t *testing.T) { }, { tx: &legacy.TxData{ Inputs: []*legacy.TxInput{ - legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{}), - legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, math.MaxInt64, 0, nil, bc.Hash{}), + legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil), + legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, math.MaxInt64, 0, nil), }, }, want: ErrBadAmount, }, { tx: &legacy.TxData{ - Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{})}, + Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)}, Outputs: []*legacy.TxOutput{legacy.NewTxOutput(bc.AssetID{}, 5, nil)}, }, want: nil, @@ -238,7 +238,7 @@ func TestCheckBlankCheck(t *testing.T) { want: nil, }, { tx: &legacy.TxData{ - Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil, bc.Hash{})}, + Inputs: []*legacy.TxInput{legacy.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)}, Outputs: []*legacy.TxOutput{legacy.NewTxOutput(bc.NewAssetID([32]byte{1}), 5, nil)}, }, want: nil, diff --git a/protocol/bc/legacy/spend.go b/protocol/bc/legacy/spend.go index afc6e567..bba88a26 100644 --- a/protocol/bc/legacy/spend.go +++ b/protocol/bc/legacy/spend.go @@ -25,7 +25,7 @@ type SpendInput struct { func (si *SpendInput) IsIssuance() bool { return false } func (si *SpendInput) IsCoinbase() bool { return false } -func NewSpendInput(arguments [][]byte, sourceID bc.Hash, assetID bc.AssetID, amount uint64, sourcePos uint64, controlProgram []byte, outRefDataHash bc.Hash) *TxInput { +func NewSpendInput(arguments [][]byte, sourceID bc.Hash, assetID bc.AssetID, amount uint64, sourcePos uint64, controlProgram []byte) *TxInput { const ( vmver = 1 assetver = 1 @@ -39,7 +39,6 @@ func NewSpendInput(arguments [][]byte, sourceID bc.Hash, assetID bc.AssetID, amo SourcePosition: sourcePos, VMVersion: vmver, ControlProgram: controlProgram, - RefDataHash: outRefDataHash, } return &TxInput{ AssetVersion: assetver, @@ -58,7 +57,6 @@ type SpendCommitment struct { SourcePosition uint64 VMVersion uint64 ControlProgram []byte - RefDataHash bc.Hash } func (sc *SpendCommitment) writeExtensibleString(w io.Writer, suffix []byte, assetVersion uint64) error { @@ -90,10 +88,6 @@ func (sc *SpendCommitment) writeContents(w io.Writer, suffix []byte, assetVersio if err != nil { return errors.Wrap(err, "writing control program") } - _, err = sc.RefDataHash.WriteTo(w) - if err != nil { - return errors.Wrap(err, "writing reference data hash") - } } if len(suffix) > 0 { _, err = w.Write(suffix) @@ -130,10 +124,6 @@ func (sc *SpendCommitment) readFrom(r *blockchain.Reader, assetVersion uint64) ( if err != nil { return errors.Wrap(err, "reading control program") } - _, err = sc.RefDataHash.ReadFrom(r) - if err != nil { - return errors.Wrap(err, "reading reference data hash") - } return nil } return nil diff --git a/protocol/bc/legacy/transaction_test.go b/protocol/bc/legacy/transaction_test.go index 813c4e25..fc45dfc3 100644 --- a/protocol/bc/legacy/transaction_test.go +++ b/protocol/bc/legacy/transaction_test.go @@ -76,16 +76,16 @@ func TestTransaction(t *testing.T) { { tx: NewTx(TxData{ Version: 1, - SerializedSize: uint64(206), + SerializedSize: uint64(174), Inputs: []*TxInput{ - NewSpendInput(nil, mustDecodeHash("dd385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292"), bc.AssetID{}, 1000000000000, 1, []byte{1}, bc.Hash{}), + NewSpendInput(nil, mustDecodeHash("dd385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292"), bc.AssetID{}, 1000000000000, 1, []byte{1}), }, Outputs: []*TxOutput{ NewTxOutput(assetID, 600000000000, []byte{1}), NewTxOutput(assetID, 400000000000, []byte{2}), }, }), - hex: ("0701000001016c016add385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292000000000000000000000000000000000000000000000000000000000000000080a094a58d1d0101010100000000000000000000000000000000000000000000000000000000000000000100020129fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680e0a596bb11010101000129fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680c0ee8ed20b01010200"), // output 1, output witness + hex: ("0701000001014c014add385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292000000000000000000000000000000000000000000000000000000000000000080a094a58d1d010101010100020129fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680e0a596bb11010101000129fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680c0ee8ed20b01010200"), // output 1, output witness hash: mustDecodeHash("828b56bb1946e8fe04e4dffd6b856d3a5ded4c96d05464d3d2ceb470c1620bc4"), }, } @@ -133,7 +133,7 @@ func TestHasIssuance(t *testing.T) { }, { tx: &TxData{ Inputs: []*TxInput{ - NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, bc.Hash{}), + NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil), NewIssuanceInput(nil, 0, bc.Hash{}, nil, nil, nil), }, }, @@ -141,7 +141,7 @@ func TestHasIssuance(t *testing.T) { }, { tx: &TxData{ Inputs: []*TxInput{ - NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, bc.Hash{}), + NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil), }, }, want: false, @@ -214,7 +214,7 @@ func BenchmarkTxWriteToFalse(b *testing.B) { func BenchmarkTxWriteToTrue200(b *testing.B) { tx := &Tx{} for i := 0; i < 200; i++ { - tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, bc.Hash{})) + tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil)) tx.Outputs = append(tx.Outputs, NewTxOutput(bc.AssetID{}, 0, nil)) } for i := 0; i < b.N; i++ { @@ -225,7 +225,7 @@ func BenchmarkTxWriteToTrue200(b *testing.B) { func BenchmarkTxWriteToFalse200(b *testing.B) { tx := &Tx{} for i := 0; i < 200; i++ { - tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, bc.Hash{})) + tx.Inputs = append(tx.Inputs, NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil)) tx.Outputs = append(tx.Outputs, NewTxOutput(bc.AssetID{}, 0, nil)) } for i := 0; i < b.N; i++ { @@ -234,7 +234,7 @@ func BenchmarkTxWriteToFalse200(b *testing.B) { } func BenchmarkTxInputWriteToTrue(b *testing.B) { - input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, bc.Hash{}) + input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil) ew := errors.NewWriter(ioutil.Discard) for i := 0; i < b.N; i++ { input.writeTo(ew, 0) @@ -242,7 +242,7 @@ func BenchmarkTxInputWriteToTrue(b *testing.B) { } func BenchmarkTxInputWriteToFalse(b *testing.B) { - input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, bc.Hash{}) + input := NewSpendInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil) ew := errors.NewWriter(ioutil.Discard) for i := 0; i < b.N; i++ { input.writeTo(ew, serRequired) diff --git a/protocol/bc/legacy/tx_test.go b/protocol/bc/legacy/tx_test.go index fa1cd7eb..32d5fe93 100644 --- a/protocol/bc/legacy/tx_test.go +++ b/protocol/bc/legacy/tx_test.go @@ -55,8 +55,8 @@ func sampleTx() *TxData { Version: 1, SerializedSize: 66, Inputs: []*TxInput{ - NewSpendInput(nil, mustDecodeHash("dd385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292"), assetID, 1000000000000, 1, []byte{1}, bc.Hash{}), - NewSpendInput(nil, bc.NewHash([32]byte{0x11}), assetID, 1, 1, []byte{2}, bc.Hash{}), + NewSpendInput(nil, mustDecodeHash("dd385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292"), assetID, 1000000000000, 1, []byte{1}), + NewSpendInput(nil, bc.NewHash([32]byte{0x11}), assetID, 1, 1, []byte{2}), }, Outputs: []*TxOutput{ NewTxOutput(assetID, 600000000000, []byte{1}), diff --git a/protocol/validation/validation_test.go b/protocol/validation/validation_test.go index 4ae138c0..67059fda 100644 --- a/protocol/validation/validation_test.go +++ b/protocol/validation/validation_test.go @@ -18,8 +18,6 @@ import ( "github.com/bytom/testutil" ) -const dirPath = "pseudohsm/testdata/pseudo" - func init() { spew.Config.DisableMethods = true } @@ -596,8 +594,8 @@ func sample(tb testing.TB, in *txFixture) *txFixture { result.txInputs = []*legacy.TxInput{ legacy.NewIssuanceInput([]byte{3}, 10, result.initialBlockID, result.issuanceProg.Code, result.issuanceArgs, result.assetDef), - legacy.NewSpendInput(args1, *newHash(5), result.assetID, 20, 0, cp1, *newHash(6)), - legacy.NewSpendInput(args2, *newHash(8), result.assetID, 40, 0, cp2, *newHash(9)), + legacy.NewSpendInput(args1, *newHash(5), result.assetID, 20, 0, cp1), + legacy.NewSpendInput(args2, *newHash(8), result.assetID, 40, 0, cp2), } } @@ -654,7 +652,7 @@ func mockCoinbaseTx(amount uint64) *bc.Tx { func mockGasTxInput() *legacy.TxInput { cp, _ := vmutil.DefaultCoinbaseProgram() - return legacy.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp, *newHash(9)) + return legacy.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp) } // Like errors.Root, but also unwraps vm.Error objects. diff --git a/protocol/validation/vmcontext_test.go b/protocol/validation/vmcontext_test.go index d418cb17..63e12d25 100644 --- a/protocol/validation/vmcontext_test.go +++ b/protocol/validation/vmcontext_test.go @@ -1,7 +1,6 @@ package validation import ( - "encoding/hex" "fmt" "testing" @@ -14,7 +13,7 @@ import ( func TestCheckOutput(t *testing.T) { tx := legacy.NewTx(legacy.TxData{ Inputs: []*legacy.TxInput{ - legacy.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendprog"), bc.Hash{}), + legacy.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendprog")), legacy.NewIssuanceInput(nil, 6, bc.Hash{}, []byte("issueprog"), nil, nil), }, Outputs: []*legacy.TxOutput{ @@ -94,11 +93,3 @@ func TestCheckOutput(t *testing.T) { }) } } - -func mustDecodeHex(h string) []byte { - bits, err := hex.DecodeString(h) - if err != nil { - panic(err) - } - return bits -} -- 2.11.0