OSDN Git Service

init for remove issue (#63)
[bytom/vapor.git] / blockchain / txbuilder / txbuilder_test.go
index 5963e77..2fcd6a6 100644 (file)
@@ -9,18 +9,15 @@ import (
        "time"
 
        "github.com/davecgh/go-spew/spew"
-       "golang.org/x/crypto/sha3"
 
        "github.com/vapor/common"
        "github.com/vapor/consensus"
        "github.com/vapor/crypto"
-       "github.com/vapor/crypto/ed25519"
        "github.com/vapor/crypto/ed25519/chainkd"
        chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
-       "github.com/vapor/protocol/vm"
        "github.com/vapor/protocol/vm/vmutil"
        "github.com/vapor/testutil"
 )
@@ -35,7 +32,7 @@ func (t testAction) Build(ctx context.Context, b *TemplateBuilder) error {
        if err != nil {
                return err
        }
-       return b.AddOutput(types.NewTxOutput(*t.AssetId, t.Amount, []byte("change")))
+       return b.AddOutput(types.NewIntraChainOutput(*t.AssetId, t.Amount, []byte("change")))
 }
 
 func (t testAction) ActionType() string {
@@ -72,8 +69,8 @@ func TestBuild(t *testing.T) {
                                types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), assetID1, 5, 0, nil),
                        },
                        Outputs: []*types.TxOutput{
-                               types.NewTxOutput(assetID2, 6, []byte("dest")),
-                               types.NewTxOutput(assetID1, 5, []byte("change")),
+                               types.NewIntraChainOutput(assetID2, 6, []byte("dest")),
+                               types.NewIntraChainOutput(assetID1, 5, []byte("change")),
                        },
                }),
                SigningInstructions: []*SigningInstruction{{
@@ -90,97 +87,6 @@ func TestBuild(t *testing.T) {
        }
 }
 
-func TestSignatureWitnessMaterialize(t *testing.T) {
-       privkey1, pubkey1, err := chainkd.NewXKeys(nil)
-       if err != nil {
-               t.Fatal(err)
-       }
-       privkey2, pubkey2, err := chainkd.NewXKeys(nil)
-       if err != nil {
-               t.Fatal(err)
-       }
-       privkey3, pubkey3, err := chainkd.NewXKeys(nil)
-       if err != nil {
-               t.Fatal(err)
-       }
-       issuanceProg, _ := vmutil.P2SPMultiSigProgram([]ed25519.PublicKey{pubkey1.PublicKey(), pubkey2.PublicKey(), pubkey3.PublicKey()}, 2)
-       assetID := bc.ComputeAssetID(issuanceProg, 1, &bc.EmptyStringHash)
-       outscript := mustDecodeHex("76a914c5d128911c28776f56baaac550963f7b88501dc388c0")
-       unsigned := types.NewTx(types.TxData{
-               Version: 1,
-               Inputs: []*types.TxInput{
-                       types.NewIssuanceInput([]byte{1}, 100, issuanceProg, nil, nil),
-               },
-               Outputs: []*types.TxOutput{
-                       types.NewTxOutput(assetID, 100, outscript),
-               },
-       })
-
-       tpl := &Template{
-               Transaction: unsigned,
-       }
-       h := tpl.Hash(0)
-       builder := vmutil.NewBuilder()
-       builder.AddData(h.Bytes())
-       builder.AddOp(vm.OP_TXSIGHASH).AddOp(vm.OP_EQUAL)
-       prog, _ := builder.Build()
-       msg := sha3.Sum256(prog)
-       sig1 := privkey1.Sign(msg[:])
-       sig2 := privkey2.Sign(msg[:])
-       sig3 := privkey3.Sign(msg[:])
-       want := [][]byte{
-               vm.Int64Bytes(0),
-               sig1,
-               sig2,
-               prog,
-       }
-
-       // Test with more signatures than required, in correct order
-       tpl.SigningInstructions = []*SigningInstruction{{
-               WitnessComponents: []witnessComponent{
-                       &SignatureWitness{
-                               Quorum: 2,
-                               Keys: []keyID{
-                                       {
-                                               XPub:           pubkey1,
-                                               DerivationPath: []chainjson.HexBytes{{0, 0, 0, 0}},
-                                       },
-                                       {
-                                               XPub:           pubkey2,
-                                               DerivationPath: []chainjson.HexBytes{{0, 0, 0, 0}},
-                                       },
-                                       {
-                                               XPub:           pubkey3,
-                                               DerivationPath: []chainjson.HexBytes{{0, 0, 0, 0}},
-                                       },
-                               },
-                               Program: prog,
-                               Sigs:    []chainjson.HexBytes{sig1, sig2, sig3},
-                       },
-               },
-       }}
-       err = materializeWitnesses(tpl)
-       if err != nil {
-               testutil.FatalErr(t, err)
-       }
-       got := tpl.Transaction.Inputs[0].Arguments()
-       if !testutil.DeepEqual(got, want) {
-               t.Errorf("got input witness %v, want input witness %v", got, want)
-       }
-
-       // Test with exact amount of signatures required, in correct order
-       component := tpl.SigningInstructions[0].WitnessComponents[0].(*SignatureWitness)
-       component.Sigs = []chainjson.HexBytes{sig1, sig2}
-       err = materializeWitnesses(tpl)
-       if err != nil {
-               testutil.FatalErr(t, err)
-       }
-       got = tpl.Transaction.Inputs[0].Arguments()
-       if !testutil.DeepEqual(got, want) {
-               t.Errorf("got input witness %v, want input witness %v", got, want)
-       }
-}
-
 func mustDecodeHex(str string) []byte {
        data, err := hex.DecodeString(str)
        if err != nil {
@@ -201,7 +107,7 @@ func TestCheckBlankCheck(t *testing.T) {
        }, {
                tx: &types.TxData{
                        Inputs:  []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
-                       Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 3, nil)},
+                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 3, nil)},
                },
                want: ErrBlankCheck,
        }, {
@@ -210,15 +116,15 @@ func TestCheckBlankCheck(t *testing.T) {
                                types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil),
                                types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.NewAssetID([32]byte{1}), 5, 0, nil),
                        },
-                       Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 5, nil)},
+                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 5, nil)},
                },
                want: ErrBlankCheck,
        }, {
                tx: &types.TxData{
                        Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
                        Outputs: []*types.TxOutput{
-                               types.NewTxOutput(bc.AssetID{}, math.MaxInt64, nil),
-                               types.NewTxOutput(bc.AssetID{}, 7, nil),
+                               types.NewIntraChainOutput(bc.AssetID{}, math.MaxInt64, nil),
+                               types.NewIntraChainOutput(bc.AssetID{}, 7, nil),
                        },
                },
                want: ErrBadAmount,
@@ -233,18 +139,18 @@ func TestCheckBlankCheck(t *testing.T) {
        }, {
                tx: &types.TxData{
                        Inputs:  []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
-                       Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 5, nil)},
+                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 5, nil)},
                },
                want: nil,
        }, {
                tx: &types.TxData{
-                       Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 5, nil)},
+                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 5, nil)},
                },
                want: nil,
        }, {
                tx: &types.TxData{
                        Inputs:  []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
-                       Outputs: []*types.TxOutput{types.NewTxOutput(bc.NewAssetID([32]byte{1}), 5, nil)},
+                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.NewAssetID([32]byte{1}), 5, nil)},
                },
                want: nil,
        }}
@@ -299,7 +205,7 @@ func TestCreateTxByUtxo(t *testing.T) {
                        types.NewSpendInput(nil, utxo.SourceID, utxo.AssetID, utxo.Amount, utxo.SourcePos, utxo.ControlProgram),
                },
                Outputs: []*types.TxOutput{
-                       types.NewTxOutput(*consensus.BTMAssetID, 10000000000, recvProg),
+                       types.NewIntraChainOutput(*consensus.BTMAssetID, 10000000000, recvProg),
                },
        })