7 "github.com/davecgh/go-spew/spew"
9 "github.com/bytom/vapor/consensus"
10 "github.com/bytom/vapor/crypto/ed25519/chainkd"
11 "github.com/bytom/vapor/crypto/sha3pool"
12 "github.com/bytom/vapor/errors"
13 "github.com/bytom/vapor/protocol/bc"
14 "github.com/bytom/vapor/protocol/bc/types"
15 "github.com/bytom/vapor/protocol/vm"
16 "github.com/bytom/vapor/protocol/vm/vmutil"
17 "github.com/bytom/vapor/testutil"
21 spew.Config.DisableMethods = true
24 func TestGasStatus(t *testing.T) {
28 f func(*GasState) error
38 GasLeft: 10000/consensus.ActiveNetParams.VMGasRate + consensus.ActiveNetParams.DefaultGasCredit,
42 f: func(input *GasState) error {
43 return input.setGas(10000, 0)
58 f: func(input *GasState) error {
59 return input.setGas(-10000, 0)
65 GasLeft: consensus.ActiveNetParams.DefaultGasCredit,
72 BTMValue: 80000000000,
74 f: func(input *GasState) error {
75 return input.setGas(80000000000, 0)
81 GasLeft: consensus.ActiveNetParams.DefaultGasCredit,
88 BTMValue: math.MaxInt64,
90 f: func(input *GasState) error {
91 return input.setGas(math.MaxInt64, 0)
106 f: func(input *GasState) error {
107 return input.updateUsage(-1)
109 err: ErrGasCalculate,
122 f: func(input *GasState) error {
123 return input.updateUsage(9999)
138 f: func(input *GasState) error {
139 return input.updateUsage(math.MaxInt64)
141 err: ErrGasCalculate,
156 f: func(input *GasState) error {
157 return input.setGasValid()
174 f: func(input *GasState) error {
175 return input.setGasValid()
177 err: ErrGasCalculate,
182 GasUsed: math.MaxInt64,
192 f: func(input *GasState) error {
193 return input.setGasValid()
195 err: ErrGasCalculate,
199 GasLeft: math.MinInt64,
210 f: func(input *GasState) error {
211 return input.setGasValid()
213 err: ErrGasCalculate,
217 for i, c := range cases {
220 if rootErr(err) != c.err {
221 t.Errorf("case %d: got error %s, want %s", i, err, c.err)
222 } else if *c.input != *c.output {
223 t.Errorf("case %d: gasStatus %v, want %v;", i, c.input, c.output)
228 func TestOverflow(t *testing.T) {
229 sourceID := &bc.Hash{V0: 9999}
230 ctrlProgram := []byte{byte(vm.OP_TRUE)}
231 newTx := func(inputs []uint64, outputs []uint64) *bc.Tx {
232 txInputs := make([]*types.TxInput, 0, len(inputs))
233 txOutputs := make([]*types.TxOutput, 0, len(outputs))
235 for i, amount := range inputs {
236 txInput := types.NewSpendInput(nil, *sourceID, *consensus.BTMAssetID, amount, uint64(i), ctrlProgram)
237 txInputs = append(txInputs, txInput)
240 for _, amount := range outputs {
241 txOutput := types.NewIntraChainOutput(*consensus.BTMAssetID, amount, ctrlProgram)
242 txOutputs = append(txOutputs, txOutput)
245 txData := &types.TxData{
252 return types.MapTx(txData)
261 inputs: []uint64{math.MaxUint64, 1},
262 outputs: []uint64{0},
266 inputs: []uint64{math.MaxUint64, math.MaxUint64},
267 outputs: []uint64{0},
271 inputs: []uint64{math.MaxUint64, math.MaxUint64 - 1},
272 outputs: []uint64{0},
276 inputs: []uint64{math.MaxInt64, 1},
277 outputs: []uint64{0},
281 inputs: []uint64{math.MaxInt64, math.MaxInt64},
282 outputs: []uint64{0},
286 inputs: []uint64{math.MaxInt64, math.MaxInt64 - 1},
287 outputs: []uint64{0},
292 outputs: []uint64{math.MaxUint64},
297 outputs: []uint64{math.MaxInt64},
298 err: ErrGasCalculate,
301 inputs: []uint64{math.MaxInt64 - 1},
302 outputs: []uint64{math.MaxInt64},
303 err: ErrGasCalculate,
307 for i, c := range cases {
308 tx := newTx(c.inputs, c.outputs)
309 if _, err := ValidateTx(tx, mockBlock()); rootErr(err) != c.err {
310 t.Fatalf("case %d test failed, want %s, have %s", i, c.err, rootErr(err))
315 func TestTxValidation(t *testing.T) {
321 // the mux from tx, pulled out for convenience
325 addCoinbase := func(assetID *bc.AssetID, amount uint64, arbitrary []byte) {
326 coinbase := bc.NewCoinbase(arbitrary)
327 txOutput := types.NewIntraChainOutput(*assetID, amount, []byte{byte(vm.OP_TRUE)})
328 assetAmount := txOutput.AssetAmount()
329 muxID := getMuxID(tx)
330 coinbase.SetDestination(muxID, &assetAmount, uint64(len(mux.Sources)))
331 coinbaseID := bc.EntryID(coinbase)
332 tx.Entries[coinbaseID] = coinbase
334 mux.Sources = append(mux.Sources, &bc.ValueSource{
339 src := &bc.ValueSource{
342 Position: uint64(len(tx.ResultIds)),
344 prog := &bc.Program{txOutput.VMVersion(), txOutput.ControlProgram()}
345 output := bc.NewIntraChainOutput(src, prog, uint64(len(tx.ResultIds)))
346 outputID := bc.EntryID(output)
347 tx.Entries[outputID] = output
349 dest := &bc.ValueDestination{
354 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
355 tx.ResultIds = append(tx.ResultIds, &outputID)
356 vs.block.Transactions = append(vs.block.Transactions, vs.tx)
360 desc string // description of the test case
361 f func() // function to adjust tx, vs, and/or mux
362 err error // expected error
368 desc: "unbalanced mux amounts",
370 mux.WitnessDestinations[0].Value.Amount++
375 desc: "balanced mux amounts",
377 mux.Sources[1].Value.Amount++
378 mux.WitnessDestinations[0].Value.Amount++
383 desc: "underflowing mux destination amounts",
385 mux.WitnessDestinations[0].Value.Amount = math.MaxInt64
386 out := tx.Entries[*mux.WitnessDestinations[0].Ref].(*bc.IntraChainOutput)
387 out.Source.Value.Amount = math.MaxInt64
388 mux.WitnessDestinations[1].Value.Amount = math.MaxInt64
389 out = tx.Entries[*mux.WitnessDestinations[1].Ref].(*bc.IntraChainOutput)
390 out.Source.Value.Amount = math.MaxInt64
395 desc: "unbalanced mux assets",
397 mux.Sources[1].Value.AssetId = newAssetID(255)
398 sp := tx.Entries[*mux.Sources[1].Ref].(*bc.Spend)
399 sp.WitnessDestination.Value.AssetId = newAssetID(255)
404 desc: "mismatched output source / mux dest position",
406 tx.Entries[*tx.ResultIds[0]].(*bc.IntraChainOutput).Source.Position = 1
408 err: ErrMismatchedPosition,
411 desc: "mismatched input dest / mux source position",
413 mux.Sources[0].Position = 1
415 err: ErrMismatchedPosition,
418 desc: "mismatched output source and mux dest",
420 // For this test, it's necessary to construct a mostly
421 // identical second transaction in order to get a similar but
422 // not equal output entry for the mux to falsely point
423 // to. That entry must be added to the first tx's Entries map.
424 fixture2 := sample(t, fixture)
425 tx2 := types.NewTx(*fixture2.tx).Tx
426 out2ID := tx2.ResultIds[0]
427 out2 := tx2.Entries[*out2ID].(*bc.IntraChainOutput)
428 tx.Entries[*out2ID] = out2
429 mux.WitnessDestinations[0].Ref = out2ID
431 err: ErrMismatchedReference,
434 desc: "invalid mux destination position",
436 mux.WitnessDestinations[0].Position = 1
441 desc: "mismatched mux dest value / output source value",
443 outID := tx.ResultIds[0]
444 out := tx.Entries[*outID].(*bc.IntraChainOutput)
445 mux.WitnessDestinations[0].Value = &bc.AssetAmount{
446 AssetId: out.Source.Value.AssetId,
447 Amount: out.Source.Value.Amount + 1,
449 mux.Sources[0].Value.Amount++ // the mux must still balance
451 err: ErrMismatchedValue,
454 desc: "empty tx results",
458 err: ErrEmptyResults,
461 desc: "empty tx results, but that's OK",
468 desc: "spend control program failure",
470 spend := txSpend(t, tx, 1)
471 spend.WitnessArguments[0] = []byte{}
473 err: vm.ErrFalseVMResult,
476 desc: "mismatched spent source/witness value",
478 spend := txSpend(t, tx, 1)
479 spentOutput := tx.Entries[*spend.SpentOutputId].(*bc.IntraChainOutput)
480 spentOutput.Source.Value = &bc.AssetAmount{
481 AssetId: spend.WitnessDestination.Value.AssetId,
482 Amount: spend.WitnessDestination.Value.Amount + 1,
485 err: ErrMismatchedValue,
488 desc: "gas out of limit",
490 vs.tx.SerializedSize = 10000000
492 err: ErrOverGasCredit,
495 desc: "can't find gas spend input in entries",
497 spendID := mux.Sources[len(mux.Sources)-1].Ref
498 delete(tx.Entries, *spendID)
499 mux.Sources = mux.Sources[:len(mux.Sources)-1]
501 err: bc.ErrMissingEntry,
504 desc: "normal check with no gas spend input",
506 spendID := mux.Sources[len(mux.Sources)-1].Ref
507 delete(tx.Entries, *spendID)
508 mux.Sources = mux.Sources[:len(mux.Sources)-1]
510 vs.gasStatus.GasLeft = 0
515 desc: "no gas spend input, but set gas left, so it's ok",
517 spendID := mux.Sources[len(mux.Sources)-1].Ref
518 delete(tx.Entries, *spendID)
519 mux.Sources = mux.Sources[:len(mux.Sources)-1]
525 desc: "mismatched gas spend input destination amount/prevout source amount",
527 spendID := mux.Sources[len(mux.Sources)-1].Ref
528 spend := tx.Entries[*spendID].(*bc.Spend)
529 spend.WitnessDestination.Value = &bc.AssetAmount{
530 AssetId: spend.WitnessDestination.Value.AssetId,
531 Amount: spend.WitnessDestination.Value.Amount + 1,
534 err: ErrMismatchedValue,
537 desc: "normal coinbase tx",
539 addCoinbase(consensus.BTMAssetID, 100000, nil)
544 desc: "invalid coinbase tx asset id",
546 addCoinbase(&bc.AssetID{V1: 100}, 100000, nil)
548 err: ErrWrongCoinbaseAsset,
551 desc: "coinbase tx is not first tx in block",
553 addCoinbase(consensus.BTMAssetID, 100000, nil)
554 vs.block.Transactions[0] = nil
556 err: ErrWrongCoinbaseTransaction,
559 desc: "coinbase arbitrary size out of limit",
561 arbitrary := make([]byte, consensus.ActiveNetParams.CoinbaseArbitrarySizeLimit+1)
562 addCoinbase(consensus.BTMAssetID, 100000, arbitrary)
564 err: ErrCoinbaseArbitraryOversize,
567 desc: "normal retirement output",
569 outputID := tx.ResultIds[0]
570 output := tx.Entries[*outputID].(*bc.IntraChainOutput)
571 retirement := bc.NewRetirement(output.Source, output.Ordinal)
572 retirementID := bc.EntryID(retirement)
573 tx.Entries[retirementID] = retirement
574 delete(tx.Entries, *outputID)
575 tx.ResultIds[0] = &retirementID
576 mux.WitnessDestinations[0].Ref = &retirementID
581 desc: "ordinal doesn't matter for prevouts",
583 spend := txSpend(t, tx, 1)
584 prevout := tx.Entries[*spend.SpentOutputId].(*bc.IntraChainOutput)
585 newPrevout := bc.NewIntraChainOutput(prevout.Source, prevout.ControlProgram, 10)
586 hash := bc.EntryID(newPrevout)
587 spend.SpentOutputId = &hash
592 desc: "mux witness destination have no source",
594 dest := &bc.ValueDestination{
595 Value: &bc.AssetAmount{
596 AssetId: &bc.AssetID{V2: 1000},
599 Ref: mux.WitnessDestinations[0].Ref,
602 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
608 for i, c := range cases {
609 t.Run(c.desc, func(t *testing.T) {
610 fixture = sample(t, nil)
611 tx = types.NewTx(*fixture.tx).Tx
612 vs = &validationState{
616 gasStatus: &GasState{
617 GasLeft: int64(80000),
620 cache: make(map[bc.Hash]error),
622 muxID := getMuxID(tx)
623 mux = tx.Entries[*muxID].(*bc.Mux)
628 err := checkValid(vs, tx.TxHeader)
630 if rootErr(err) != c.err {
631 t.Errorf("case #%d (%s) got error %s, want %s; validationState is:\n%s", i, c.desc, err, c.err, spew.Sdump(vs))
637 // TestCoinbase test the coinbase transaction is valid (txtest#1016)
638 func TestCoinbase(t *testing.T) {
639 cp, _ := vmutil.DefaultCoinbaseProgram()
640 retire, _ := vmutil.RetireProgram([]byte{})
641 CbTx := types.MapTx(&types.TxData{
643 Inputs: []*types.TxInput{
644 types.NewCoinbaseInput(nil),
646 Outputs: []*types.TxOutput{
647 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
659 BlockHeader: &bc.BlockHeader{Height: 666},
660 Transactions: []*bc.Tx{CbTx},
668 BlockHeader: &bc.BlockHeader{Height: 666},
669 Transactions: []*bc.Tx{
671 types.MapTx(&types.TxData{
673 Inputs: []*types.TxInput{
674 types.NewCoinbaseInput(nil),
676 Outputs: []*types.TxOutput{
677 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
684 err: ErrWrongCoinbaseTransaction,
688 BlockHeader: &bc.BlockHeader{Height: 666},
689 Transactions: []*bc.Tx{
691 types.MapTx(&types.TxData{
693 Inputs: []*types.TxInput{
694 types.NewCoinbaseInput(nil),
695 types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
697 Outputs: []*types.TxOutput{
698 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
699 types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
706 err: ErrWrongCoinbaseTransaction,
710 BlockHeader: &bc.BlockHeader{Height: 666},
711 Transactions: []*bc.Tx{
713 types.MapTx(&types.TxData{
715 Inputs: []*types.TxInput{
716 types.NewCoinbaseInput(nil),
717 types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
719 Outputs: []*types.TxOutput{
720 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
721 types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
728 err: ErrWrongCoinbaseTransaction,
732 BlockHeader: &bc.BlockHeader{Height: 666},
733 Transactions: []*bc.Tx{
734 types.MapTx(&types.TxData{
736 Inputs: []*types.TxInput{
737 types.NewCoinbaseInput(nil),
738 types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
740 Outputs: []*types.TxOutput{
741 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
742 types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
753 BlockHeader: &bc.BlockHeader{Height: 666},
754 Transactions: []*bc.Tx{
755 types.MapTx(&types.TxData{
757 Inputs: []*types.TxInput{
758 types.NewCoinbaseInput(nil),
759 types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, retire),
761 Outputs: []*types.TxOutput{
762 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
763 types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
774 for i, c := range cases {
775 gasStatus, err := ValidateTx(c.block.Transactions[c.txIndex], c.block)
777 if rootErr(err) != c.err {
778 t.Errorf("#%d got error %s, want %s", i, err, c.err)
780 if c.GasValid != gasStatus.GasValid {
781 t.Errorf("#%d got GasValid %t, want %t", i, gasStatus.GasValid, c.GasValid)
786 // TestTimeRange test the checkTimeRange function (txtest#1004)
787 func TestTimeRange(t *testing.T) {
805 timeRange: 1521625824,
811 BlockHeader: &bc.BlockHeader{
813 Timestamp: 1521625823000,
817 tx := types.MapTx(&types.TxData{
820 Inputs: []*types.TxInput{
823 Outputs: []*types.TxOutput{
824 types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6a}),
828 for i, c := range cases {
829 tx.TimeRange = c.timeRange
830 if _, err := ValidateTx(tx, block); (err != nil) != c.err {
831 t.Errorf("#%d got error %t, want %t", i, !c.err, c.err)
836 func TestValidateTxVersion(t *testing.T) {
843 desc: "tx version greater than 1 (txtest#1001)",
845 BlockHeader: &bc.BlockHeader{Version: 1},
846 Transactions: []*bc.Tx{
847 {TxHeader: &bc.TxHeader{Version: 2}},
853 desc: "tx version equals 0 (txtest#1002)",
855 BlockHeader: &bc.BlockHeader{Version: 1},
856 Transactions: []*bc.Tx{
857 {TxHeader: &bc.TxHeader{Version: 0}},
863 desc: "tx version equals max uint64 (txtest#1003)",
865 BlockHeader: &bc.BlockHeader{Version: 1},
866 Transactions: []*bc.Tx{
867 {TxHeader: &bc.TxHeader{Version: math.MaxUint64}},
874 for i, c := range cases {
875 if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
876 t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, err, c.err)
881 func TestMagneticContractTx(t *testing.T) {
882 buyerArgs := vmutil.MagneticContractArgs{
883 RequestedAsset: bc.AssetID{V0: 1},
886 SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
887 SellerKey: testutil.MustDecodeHexString("af1927316233365dd525d3b48f2869f125a656958ee3946286f42904c35b9c91"),
890 sellerArgs := vmutil.MagneticContractArgs{
891 RequestedAsset: bc.AssetID{V0: 2},
894 SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
895 SellerKey: testutil.MustDecodeHexString("af1927316233365dd525d3b48f2869f125a656958ee3946286f42904c35b9c91"),
898 programBuyer, err := vmutil.P2WMCProgram(buyerArgs)
903 programSeller, err := vmutil.P2WMCProgram(sellerArgs)
914 desc: "contracts all full trade",
916 BlockHeader: &bc.BlockHeader{Version: 0},
917 Transactions: []*bc.Tx{
918 types.MapTx(&types.TxData{
920 Inputs: []*types.TxInput{
921 types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
922 types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 200000000, 0, programBuyer),
924 Outputs: []*types.TxOutput{
925 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 199800000, sellerArgs.SellerProgram),
926 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 99900000, buyerArgs.SellerProgram),
927 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000, []byte{0x51}),
928 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000, []byte{0x51}),
936 desc: "first contract partial trade, second contract full trade",
938 BlockHeader: &bc.BlockHeader{Version: 0},
939 Transactions: []*bc.Tx{
940 types.MapTx(&types.TxData{
942 Inputs: []*types.TxInput{
943 types.NewSpendInput([][]byte{vm.Int64Bytes(100000000), vm.Int64Bytes(0), vm.Int64Bytes(0)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 200000000, 1, programSeller),
944 types.NewSpendInput([][]byte{vm.Int64Bytes(2), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 100000000, 0, programBuyer),
946 Outputs: []*types.TxOutput{
947 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 99900000, sellerArgs.SellerProgram),
948 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 150000000, programSeller),
949 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 49950000, buyerArgs.SellerProgram),
950 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 100000, []byte{0x51}),
951 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000, []byte{0x51}),
959 desc: "first contract full trade, second contract partial trade",
961 BlockHeader: &bc.BlockHeader{Version: 0},
962 Transactions: []*bc.Tx{
963 types.MapTx(&types.TxData{
965 Inputs: []*types.TxInput{
966 types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
967 types.NewSpendInput([][]byte{vm.Int64Bytes(100000000), vm.Int64Bytes(1), vm.Int64Bytes(0)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 300000000, 0, programBuyer),
969 Outputs: []*types.TxOutput{
970 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 199800000, sellerArgs.SellerProgram),
971 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 99900000, buyerArgs.SellerProgram),
972 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 100000000, programBuyer),
973 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000, []byte{0x51}),
974 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000, []byte{0x51}),
982 desc: "cancel magnetic contract",
984 BlockHeader: &bc.BlockHeader{Version: 0},
985 Transactions: []*bc.Tx{
986 types.MapTx(&types.TxData{
988 Inputs: []*types.TxInput{
989 types.NewSpendInput([][]byte{testutil.MustDecodeHexString("851a14d69076507e202a94a884cdfb3b9f1ecbc1fb0634d2f0d1f9c1a275fdbdf921af0c5309d2d0a0deb85973cba23a4076d2c169c7f08ade2af4048d91d209"), vm.Int64Bytes(0), vm.Int64Bytes(2)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 0, programSeller),
991 Outputs: []*types.TxOutput{
992 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, sellerArgs.SellerProgram),
1000 desc: "wrong signature with cancel magnetic contract",
1002 BlockHeader: &bc.BlockHeader{Version: 0},
1003 Transactions: []*bc.Tx{
1004 types.MapTx(&types.TxData{
1006 Inputs: []*types.TxInput{
1007 types.NewSpendInput([][]byte{testutil.MustDecodeHexString("686b983a8de1893ef723144389fd1f07b12b048f52f389faa863243195931d5732dbfd15470b43ed63d5067900718cf94f137073f4a972d277bbd967b022545d"), vm.Int64Bytes(0), vm.Int64Bytes(2)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 0, programSeller),
1009 Outputs: []*types.TxOutput{
1010 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, sellerArgs.SellerProgram),
1015 err: vm.ErrFalseVMResult,
1018 desc: "wrong output amount with contracts all full trade",
1020 BlockHeader: &bc.BlockHeader{Version: 0},
1021 Transactions: []*bc.Tx{
1022 types.MapTx(&types.TxData{
1024 Inputs: []*types.TxInput{
1025 types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
1026 types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 200000000, 0, programBuyer),
1028 Outputs: []*types.TxOutput{
1029 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000000, sellerArgs.SellerProgram),
1030 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000000, buyerArgs.SellerProgram),
1031 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000000, []byte{0x55}),
1036 err: vm.ErrFalseVMResult,
1039 desc: "wrong output assetID with contracts all full trade",
1041 BlockHeader: &bc.BlockHeader{Version: 0},
1042 Transactions: []*bc.Tx{
1043 types.MapTx(&types.TxData{
1045 Inputs: []*types.TxInput{
1046 types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
1047 types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 200000000, 0, programBuyer),
1048 types.NewSpendInput(nil, bc.Hash{V0: 30}, bc.AssetID{V0: 1}, 200000000, 0, []byte{0x51}),
1050 Outputs: []*types.TxOutput{
1051 types.NewIntraChainOutput(bc.AssetID{V0: 1}, 200000000, sellerArgs.SellerProgram),
1052 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, buyerArgs.SellerProgram),
1053 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000000, []byte{0x55}),
1058 err: vm.ErrFalseVMResult,
1061 desc: "wrong output change program with first contract partial trade and second contract full trade",
1063 BlockHeader: &bc.BlockHeader{Version: 0},
1064 Transactions: []*bc.Tx{
1065 types.MapTx(&types.TxData{
1067 Inputs: []*types.TxInput{
1068 types.NewSpendInput([][]byte{vm.Int64Bytes(100000000), vm.Int64Bytes(0), vm.Int64Bytes(0)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 200000000, 1, programSeller),
1069 types.NewSpendInput([][]byte{vm.Int64Bytes(2), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 100000000, 0, programBuyer),
1071 Outputs: []*types.TxOutput{
1072 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 99900000, sellerArgs.SellerProgram),
1073 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 150000000, []byte{0x55}),
1074 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 49950000, buyerArgs.SellerProgram),
1075 types.NewIntraChainOutput(sellerArgs.RequestedAsset, 100000, []byte{0x51}),
1076 types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000, []byte{0x51}),
1081 err: vm.ErrFalseVMResult,
1085 for i, c := range cases {
1086 if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
1087 t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, rootErr(err), c.err)
1092 func TestRingMagneticContractTx(t *testing.T) {
1093 aliceArgs := vmutil.MagneticContractArgs{
1094 RequestedAsset: bc.AssetID{V0: 1},
1096 RatioDenominator: 1,
1097 SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
1098 SellerKey: testutil.MustDecodeHexString("960ecabafb88ba460a40912841afecebf0e84884178611ac97210e327c0d1173"),
1101 bobArgs := vmutil.MagneticContractArgs{
1102 RequestedAsset: bc.AssetID{V0: 2},
1104 RatioDenominator: 1,
1105 SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
1106 SellerKey: testutil.MustDecodeHexString("ad79ec6bd3a6d6dbe4d0ee902afc99a12b9702fb63edce5f651db3081d868b75"),
1109 jackArgs := vmutil.MagneticContractArgs{
1110 RequestedAsset: bc.AssetID{V0: 3},
1112 RatioDenominator: 4,
1113 SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
1114 SellerKey: testutil.MustDecodeHexString("9c19a91988c62046c2767bd7e9999b0c142891b9ebf467bfa59210b435cb0de7"),
1117 aliceProgram, err := vmutil.P2WMCProgram(aliceArgs)
1122 bobProgram, err := vmutil.P2WMCProgram(bobArgs)
1127 jackProgram, err := vmutil.P2WMCProgram(jackArgs)
1138 desc: "contracts all full trade",
1140 BlockHeader: &bc.BlockHeader{Version: 0},
1141 Transactions: []*bc.Tx{
1142 types.MapTx(&types.TxData{
1144 Inputs: []*types.TxInput{
1145 types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, jackArgs.RequestedAsset, 100000000, 0, aliceProgram),
1146 types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, aliceArgs.RequestedAsset, 200000000, 0, bobProgram),
1147 types.NewSpendInput([][]byte{vm.Int64Bytes(2), vm.Int64Bytes(1)}, bc.Hash{V0: 30}, bobArgs.RequestedAsset, 400000000, 0, jackProgram),
1149 Outputs: []*types.TxOutput{
1150 types.NewIntraChainOutput(aliceArgs.RequestedAsset, 199800000, aliceArgs.SellerProgram),
1151 types.NewIntraChainOutput(bobArgs.RequestedAsset, 399600000, bobArgs.SellerProgram),
1152 types.NewIntraChainOutput(jackArgs.RequestedAsset, 99900000, jackArgs.SellerProgram),
1153 types.NewIntraChainOutput(aliceArgs.RequestedAsset, 200000, []byte{0x51}),
1154 types.NewIntraChainOutput(bobArgs.RequestedAsset, 400000, []byte{0x51}),
1155 types.NewIntraChainOutput(jackArgs.RequestedAsset, 100000, []byte{0x51}),
1164 for i, c := range cases {
1165 if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
1166 t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, rootErr(err), c.err)
1171 func TestValidateOpenFederationIssueAsset(t *testing.T) {
1172 tx := &types.Tx{TxData: types.TxData{Version: 1}}
1173 tx.Inputs = append(tx.Inputs, types.NewCrossChainInput(nil,
1174 testutil.MustDecodeHash("449143cb95389d19a1939879681168f78cc62614f4e0fb41f17b3232528a709d"),
1175 testutil.MustDecodeAsset("60b550a772ddde42717ef3ab1178cf4f712a02fc9faf3678aa5468facff128f5"),
1179 testutil.MustDecodeHexString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b0a202020202269737375655f61737365745f616374696f6e223a20226f70656e5f66656465726174696f6e5f63726f73735f636861696e220a20207d2c0a2020226e616d65223a2022454f53222c0a20202271756f72756d223a20312c0a20202272656973737565223a202274727565222c0a20202273796d626f6c223a2022454f53220a7d"),
1180 testutil.MustDecodeHexString("ae20d827c281d47f5de93f98544b20468feaac046bf8b89bd51102f6e971f09d215920be43bb856fe337b37f5f09040c2b6cdbe23eaf5aa4770b16ea51fdfc45514c295152ad"),
1183 tx.Outputs = append(tx.Outputs, types.NewIntraChainOutput(
1184 testutil.MustDecodeAsset("60b550a772ddde42717ef3ab1178cf4f712a02fc9faf3678aa5468facff128f5"),
1186 testutil.MustDecodeHexString("0014d8dd58f374f58cffb1b1a7cc1e18a712b4fe67b5"),
1189 byteData, err := tx.MarshalText()
1194 tx.SerializedSize = uint64(len(byteData))
1195 tx = types.NewTx(tx.TxData)
1197 xPrv := chainkd.XPrv(toByte64("f0293101b509a0e919b4775d849372f97c688af8bd85a9d369fc1a4528baa94c0d74dd09aa6eaeed582df47d391c816b916a0537302291b09743903b730333f9"))
1198 signature := xPrv.Sign(tx.SigHash(0).Bytes())
1199 tx.Inputs[0].SetArguments([][]byte{signature})
1200 tx = types.NewTx(tx.TxData)
1202 if _, err := ValidateTx(tx.Tx, &bc.Block{BlockHeader: &bc.BlockHeader{Version: 1}}); err != nil {
1207 func toByte64(str string) [64]byte {
1209 bytes := testutil.MustDecodeHexString(str)
1210 for i := range bytes {
1211 result[i] = bytes[i]
1216 // A txFixture is returned by sample (below) to produce a sample
1217 // transaction, which takes a separate, optional _input_ txFixture to
1218 // affect the transaction that's built. The components of the
1219 // transaction are the fields of txFixture.
1220 type txFixture struct {
1221 initialBlockID bc.Hash
1222 issuanceProg bc.Program
1223 issuanceArgs [][]byte
1227 txInputs []*types.TxInput
1228 txOutputs []*types.TxOutput
1232 // Produces a sample transaction in a txFixture object (see above). A
1233 // separate input txFixture can be used to alter the transaction
1236 // The output of this function can be used as the input to a
1237 // subsequent call to make iterative refinements to a test object.
1239 // The default transaction produced is valid and has three inputs:
1240 // - an issuance of 10 units
1241 // - a spend of 20 units
1242 // - a spend of 40 units
1243 // and two outputs, one of 25 units and one of 45 units.
1244 // All amounts are denominated in the same asset.
1246 // The issuance program for the asset requires two numbers as
1247 // arguments that add up to 5. The prevout control programs require
1248 // two numbers each, adding to 9 and 13, respectively.
1250 // The min and max times for the transaction are now +/- one minute.
1251 func sample(tb testing.TB, in *txFixture) *txFixture {
1252 var result txFixture
1257 if result.initialBlockID.IsZero() {
1258 result.initialBlockID = *newHash(1)
1260 if testutil.DeepEqual(result.issuanceProg, bc.Program{}) {
1261 prog, err := vm.Assemble("ADD 5 NUMEQUAL")
1265 result.issuanceProg = bc.Program{VmVersion: 1, Code: prog}
1267 if len(result.issuanceArgs) == 0 {
1268 result.issuanceArgs = [][]byte{{2}, {3}}
1270 if len(result.assetDef) == 0 {
1271 result.assetDef = []byte{2}
1273 if result.assetID.IsZero() {
1274 result.assetID = bc.AssetID{V0: 9999}
1277 if result.txVersion == 0 {
1278 result.txVersion = 1
1280 if len(result.txInputs) == 0 {
1281 cp1, err := vm.Assemble("ADD 9 NUMEQUAL")
1285 args1 := [][]byte{{4}, {5}}
1287 cp2, err := vm.Assemble("ADD 13 NUMEQUAL")
1291 args2 := [][]byte{{6}, {7}}
1293 result.txInputs = []*types.TxInput{
1294 types.NewSpendInput(nil, *newHash(9), result.assetID, 10, 0, []byte{byte(vm.OP_TRUE)}),
1295 types.NewSpendInput(args1, *newHash(5), result.assetID, 20, 0, cp1),
1296 types.NewSpendInput(args2, *newHash(8), result.assetID, 40, 0, cp2),
1300 result.txInputs = append(result.txInputs, mockGasTxInput())
1302 if len(result.txOutputs) == 0 {
1303 cp1, err := vm.Assemble("ADD 17 NUMEQUAL")
1307 cp2, err := vm.Assemble("ADD 21 NUMEQUAL")
1312 result.txOutputs = []*types.TxOutput{
1313 types.NewIntraChainOutput(result.assetID, 25, cp1),
1314 types.NewIntraChainOutput(result.assetID, 45, cp2),
1318 result.tx = &types.TxData{
1319 Version: result.txVersion,
1320 Inputs: result.txInputs,
1321 Outputs: result.txOutputs,
1327 func mockBlock() *bc.Block {
1329 BlockHeader: &bc.BlockHeader{
1335 func mockGasTxInput() *types.TxInput {
1336 cp, _ := vmutil.DefaultCoinbaseProgram()
1337 return types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp)
1340 // Like errors.Root, but also unwraps vm.Error objects.
1341 func rootErr(e error) error {
1342 return errors.Root(e)
1345 func hashData(data []byte) bc.Hash {
1347 sha3pool.Sum256(b32[:], data)
1348 return bc.NewHash(b32)
1351 func newHash(n byte) *bc.Hash {
1352 h := bc.NewHash([32]byte{n})
1356 func newAssetID(n byte) *bc.AssetID {
1357 a := bc.NewAssetID([32]byte{n})
1361 func txSpend(t *testing.T, tx *bc.Tx, index int) *bc.Spend {
1362 id := tx.InputIDs[index]
1363 res, err := tx.Spend(id)
1370 func getMuxID(tx *bc.Tx) *bc.Hash {
1371 out := tx.Entries[*tx.ResultIds[0]]
1372 switch result := out.(type) {
1373 case *bc.IntraChainOutput:
1374 return result.Source.Ref
1375 case *bc.Retirement:
1376 return result.Source.Ref