OSDN Git Service

prevent tx size is 0 (#562)
authorPaladz <yzhu101@uottawa.ca>
Mon, 9 Apr 2018 11:26:16 +0000 (19:26 +0800)
committerGitHub <noreply@github.com>
Mon, 9 Apr 2018 11:26:16 +0000 (19:26 +0800)
* prevent tx size is 0

* make unit test pass

blockchain/txbuilder/builder.go
blockchain/txbuilder/finalize.go
blockchain/txbuilder/txbuilder_test.go
test/integration/standard_transaction_test.go

index 011476d..07aaff3 100644 (file)
@@ -127,12 +127,6 @@ func (b *TemplateBuilder) Build() (*Template, *types.TxData, error) {
                tx.Inputs = append(tx.Inputs, in)
        }
 
-       txSerialized, err := tx.MarshalText()
-       if err != nil {
-               return nil, nil, err
-       }
-
-       tx.SerializedSize = uint64(len(txSerialized))
        tpl.Transaction = types.NewTx(*tx)
        return tpl, tx, nil
 }
index 0104e39..3928fe0 100644 (file)
@@ -23,12 +23,22 @@ var (
 // assembles a fully signed tx, and stores the effects of
 // its changes on the UTXO set.
 func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
-       err := checkTxSighashCommitment(tx)
-       if err != nil {
+       if err := checkTxSighashCommitment(tx); err != nil {
                return err
        }
 
-       _, err = c.ValidateTx(tx)
+       // This paret is use for prevent tx size  is 0
+       if tx.SerializedSize == 0 {
+               data, err := tx.TxData.MarshalText()
+               if err != nil {
+                       return err
+               }
+               if err := tx.UnmarshalText(data); err != nil {
+                       return err
+               }
+       }
+
+       _, err := c.ValidateTx(tx)
        if errors.Root(err) == protocol.ErrBadTx {
                return errors.Sub(ErrRejected, err)
        }
index 864ae81..080ddfc 100644 (file)
@@ -60,8 +60,7 @@ func TestBuild(t *testing.T) {
 
        want := &Template{
                Transaction: types.NewTx(types.TxData{
-                       Version:        1,
-                       SerializedSize: 330,
+                       Version: 1,
                        Inputs: []*types.TxInput{
                                types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), assetID1, 5, 0, nil),
                        },
index 391ed7b..707a351 100644 (file)
@@ -61,6 +61,7 @@ func TestP2PKH(t *testing.T) {
                t.Fatal(err)
        }
 
+       tx.SerializedSize = 1
        if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
                t.Fatal(err)
        }
@@ -117,6 +118,7 @@ func TestP2SH(t *testing.T) {
                t.Fatal(err)
        }
 
+       tx.SerializedSize = 1
        if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
                t.Fatal(err)
        }
@@ -187,6 +189,7 @@ func TestMutilNodeSign(t *testing.T) {
                t.Fatal("sign progress is not finish,  but both xpub1 and xpub2 is signed")
        }
 
+       tx.SerializedSize = 1
        if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
                t.Fatal(err)
        }