BTMValue: 0,
},
output: &GasState{
- GasLeft: 10000 / consensus.VMGasRate,
+ GasLeft: 10000/consensus.ActiveNetParams.VMGasRate + consensus.ActiveNetParams.DefaultGasCredit,
GasUsed: 0,
BTMValue: 10000,
},
},
{
input: &GasState{
- GasLeft: consensus.DefaultGasCredit,
+ GasLeft: consensus.ActiveNetParams.DefaultGasCredit,
GasUsed: 0,
BTMValue: 0,
},
output: &GasState{
- GasLeft: 200000,
+ GasLeft: 640000,
GasUsed: 0,
BTMValue: 80000000000,
},
},
{
input: &GasState{
- GasLeft: consensus.DefaultGasCredit,
+ GasLeft: consensus.ActiveNetParams.DefaultGasCredit,
GasUsed: 0,
BTMValue: 0,
},
output: &GasState{
- GasLeft: 200000,
+ GasLeft: 640000,
GasUsed: 0,
BTMValue: math.MaxInt64,
},
txInputs := make([]*types.TxInput, 0, len(inputs))
txOutputs := make([]*types.TxOutput, 0, len(outputs))
- for _, amount := range inputs {
- txInput := types.NewSpendInput(nil, *sourceID, *consensus.BTMAssetID, amount, 0, ctrlProgram)
+ for i, amount := range inputs {
+ txInput := types.NewSpendInput(nil, *sourceID, *consensus.BTMAssetID, amount, uint64(i), ctrlProgram)
txInputs = append(txInputs, txInput)
}
for _, amount := range outputs {
- txOutput := types.NewTxOutput(*consensus.BTMAssetID, amount, ctrlProgram)
+ txOutput := types.NewIntraChainOutput(*consensus.BTMAssetID, amount, ctrlProgram)
txOutputs = append(txOutputs, txOutput)
}
addCoinbase := func(assetID *bc.AssetID, amount uint64, arbitrary []byte) {
coinbase := bc.NewCoinbase(arbitrary)
- txOutput := types.NewTxOutput(*assetID, amount, []byte{byte(vm.OP_TRUE)})
+ txOutput := types.NewIntraChainOutput(*assetID, amount, []byte{byte(vm.OP_TRUE)})
+ assetAmount := txOutput.AssetAmount()
muxID := getMuxID(tx)
- coinbase.SetDestination(muxID, &txOutput.AssetAmount, uint64(len(mux.Sources)))
+ coinbase.SetDestination(muxID, &assetAmount, uint64(len(mux.Sources)))
coinbaseID := bc.EntryID(coinbase)
tx.Entries[coinbaseID] = coinbase
mux.Sources = append(mux.Sources, &bc.ValueSource{
Ref: &coinbaseID,
- Value: &txOutput.AssetAmount,
+ Value: &assetAmount,
})
src := &bc.ValueSource{
Ref: muxID,
- Value: &txOutput.AssetAmount,
+ Value: &assetAmount,
Position: uint64(len(tx.ResultIds)),
}
- prog := &bc.Program{txOutput.VMVersion, txOutput.ControlProgram}
- output := bc.NewOutput(src, prog, uint64(len(tx.ResultIds)))
+ prog := &bc.Program{txOutput.VMVersion(), txOutput.ControlProgram()}
+ output := bc.NewIntraChainOutput(src, prog, uint64(len(tx.ResultIds)))
outputID := bc.EntryID(output)
tx.Entries[outputID] = output
{
desc: "unbalanced mux amounts",
f: func() {
- mux.Sources[0].Value.Amount++
- iss := tx.Entries[*mux.Sources[0].Ref].(*bc.Issuance)
- iss.WitnessDestination.Value.Amount++
- },
- err: ErrUnbalanced,
- },
- {
- desc: "unbalanced mux amounts",
- f: func() {
mux.WitnessDestinations[0].Value.Amount++
},
err: ErrUnbalanced,
err: nil,
},
{
- desc: "overflowing mux source amounts",
- f: func() {
- mux.Sources[0].Value.Amount = math.MaxInt64
- iss := tx.Entries[*mux.Sources[0].Ref].(*bc.Issuance)
- iss.WitnessDestination.Value.Amount = math.MaxInt64
- },
- err: ErrOverflow,
- },
- {
desc: "underflowing mux destination amounts",
f: func() {
mux.WitnessDestinations[0].Value.Amount = math.MaxInt64
- out := tx.Entries[*mux.WitnessDestinations[0].Ref].(*bc.Output)
+ out := tx.Entries[*mux.WitnessDestinations[0].Ref].(*bc.IntraChainOutput)
out.Source.Value.Amount = math.MaxInt64
mux.WitnessDestinations[1].Value.Amount = math.MaxInt64
- out = tx.Entries[*mux.WitnessDestinations[1].Ref].(*bc.Output)
+ out = tx.Entries[*mux.WitnessDestinations[1].Ref].(*bc.IntraChainOutput)
out.Source.Value.Amount = math.MaxInt64
},
err: ErrOverflow,
{
desc: "mismatched output source / mux dest position",
f: func() {
- tx.Entries[*tx.ResultIds[0]].(*bc.Output).Source.Position = 1
+ tx.Entries[*tx.ResultIds[0]].(*bc.IntraChainOutput).Source.Position = 1
},
err: ErrMismatchedPosition,
},
fixture2 := sample(t, fixture)
tx2 := types.NewTx(*fixture2.tx).Tx
out2ID := tx2.ResultIds[0]
- out2 := tx2.Entries[*out2ID].(*bc.Output)
+ out2 := tx2.Entries[*out2ID].(*bc.IntraChainOutput)
tx.Entries[*out2ID] = out2
mux.WitnessDestinations[0].Ref = out2ID
},
err: ErrMismatchedReference,
},
{
- desc: "mismatched input dest and mux source",
- f: func() {
- fixture2 := sample(t, fixture)
- tx2 := types.NewTx(*fixture2.tx).Tx
- input2ID := tx2.InputIDs[2]
- input2 := tx2.Entries[input2ID].(*bc.Spend)
- dest2Ref := input2.WitnessDestination.Ref
- dest2 := tx2.Entries[*dest2Ref].(*bc.Mux)
- tx.Entries[*dest2Ref] = dest2
- tx.Entries[input2ID] = input2
- mux.Sources[0].Ref = &input2ID
- },
- err: ErrMismatchedReference,
- },
- {
desc: "invalid mux destination position",
f: func() {
mux.WitnessDestinations[0].Position = 1
desc: "mismatched mux dest value / output source value",
f: func() {
outID := tx.ResultIds[0]
- out := tx.Entries[*outID].(*bc.Output)
+ out := tx.Entries[*outID].(*bc.IntraChainOutput)
mux.WitnessDestinations[0].Value = &bc.AssetAmount{
AssetId: out.Source.Value.AssetId,
Amount: out.Source.Value.Amount + 1,
},
},
{
- desc: "issuance program failure",
- f: func() {
- iss := txIssuance(t, tx, 0)
- iss.WitnessArguments[0] = []byte{}
- },
- err: vm.ErrFalseVMResult,
- },
- {
desc: "spend control program failure",
f: func() {
spend := txSpend(t, tx, 1)
desc: "mismatched spent source/witness value",
f: func() {
spend := txSpend(t, tx, 1)
- spentOutput := tx.Entries[*spend.SpentOutputId].(*bc.Output)
+ spentOutput := tx.Entries[*spend.SpentOutputId].(*bc.IntraChainOutput)
spentOutput.Source.Value = &bc.AssetAmount{
AssetId: spend.WitnessDestination.Value.AssetId,
Amount: spend.WitnessDestination.Value.Amount + 1,
err: bc.ErrMissingEntry,
},
{
- desc: "no gas spend input",
+ desc: "normal check with no gas spend input",
f: func() {
spendID := mux.Sources[len(mux.Sources)-1].Ref
delete(tx.Entries, *spendID)
tx.GasInputIDs = nil
vs.gasStatus.GasLeft = 0
},
- err: vm.ErrRunLimitExceeded,
+ err: nil,
},
{
desc: "no gas spend input, but set gas left, so it's ok",
err: ErrMismatchedValue,
},
{
- desc: "mismatched witness asset destination",
- f: func() {
- issuanceID := mux.Sources[0].Ref
- issuance := tx.Entries[*issuanceID].(*bc.Issuance)
- issuance.WitnessAssetDefinition.Data = &bc.Hash{V0: 9999}
- },
- err: ErrMismatchedAssetID,
- },
- {
- desc: "issuance witness position greater than length of mux sources",
- f: func() {
- issuanceID := mux.Sources[0].Ref
- issuance := tx.Entries[*issuanceID].(*bc.Issuance)
- issuance.WitnessDestination.Position = uint64(len(mux.Sources) + 1)
- },
- err: ErrPosition,
- },
- {
desc: "normal coinbase tx",
f: func() {
addCoinbase(consensus.BTMAssetID, 100000, nil)
{
desc: "coinbase arbitrary size out of limit",
f: func() {
- arbitrary := make([]byte, consensus.CoinbaseArbitrarySizeLimit+1)
+ arbitrary := make([]byte, consensus.ActiveNetParams.CoinbaseArbitrarySizeLimit+1)
addCoinbase(consensus.BTMAssetID, 100000, arbitrary)
},
err: ErrCoinbaseArbitraryOversize,
desc: "normal retirement output",
f: func() {
outputID := tx.ResultIds[0]
- output := tx.Entries[*outputID].(*bc.Output)
+ output := tx.Entries[*outputID].(*bc.IntraChainOutput)
retirement := bc.NewRetirement(output.Source, output.Ordinal)
retirementID := bc.EntryID(retirement)
tx.Entries[retirementID] = retirement
desc: "ordinal doesn't matter for prevouts",
f: func() {
spend := txSpend(t, tx, 1)
- prevout := tx.Entries[*spend.SpentOutputId].(*bc.Output)
- newPrevout := bc.NewOutput(prevout.Source, prevout.ControlProgram, 10)
+ prevout := tx.Entries[*spend.SpentOutputId].(*bc.IntraChainOutput)
+ newPrevout := bc.NewIntraChainOutput(prevout.Source, prevout.ControlProgram, 10)
hash := bc.EntryID(newPrevout)
spend.SpentOutputId = &hash
},
types.NewCoinbaseInput(nil),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 888, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
},
})
types.NewCoinbaseInput(nil),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 888, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
},
}),
},
types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 888, cp),
- types.NewTxOutput(*consensus.BTMAssetID, 90000000, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
},
}),
},
types.MapTx(&types.TxData{
SerializedSize: 1,
Inputs: []*types.TxInput{
- types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
types.NewCoinbaseInput(nil),
+ types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 888, cp),
- types.NewTxOutput(*consensus.BTMAssetID, 90000000, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
},
}),
},
types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 888, cp),
- types.NewTxOutput(*consensus.BTMAssetID, 90000000, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
},
}),
},
types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, retire),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 888, cp),
- types.NewTxOutput(*consensus.BTMAssetID, 90000000, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 90000000, cp),
},
}),
},
}
}
-func TestRuleAA(t *testing.T) {
- testData := "070100040161015f9bc47dda88eee18c7433340c16e054cabee4318a8d638e873be19e979df81dc7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0e3f9f5c80e01011600147c7662d92bd5e77454736f94731c60a6e9cbc69f6302404a17a5995b8163ee448719b462a5694b22a35522dd9883333fd462cc3d0aabf049445c5cbb911a40e1906a5bea99b23b1a79e215eeb1a818d8b1dd27e06f3004200530c4bc9dd3cbf679fec6d824ce5c37b0c8dab88b67bcae3b000924b7dce9940160015ee334d4fe18398f0232d2aca7050388ce4ee5ae82c8148d7f0cea748438b65135ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80ace6842001011600147c7662d92bd5e77454736f94731c60a6e9cbc69f6302404a17a5995b8163ee448719b462a5694b22a35522dd9883333fd462cc3d0aabf049445c5cbb911a40e1906a5bea99b23b1a79e215eeb1a818d8b1dd27e06f3004200530c4bc9dd3cbf679fec6d824ce5c37b0c8dab88b67bcae3b000924b7dce9940161015f9bc47dda88eee18c7433340c16e054cabee4318a8d638e873be19e979df81dc7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0e3f9f5c80e01011600147c7662d92bd5e77454736f94731c60a6e9cbc69f63024062c29b20941e7f762c3afae232f61d8dac1c544825931e391408c6715c408ef69f494a1b3b61ce380ddee0c8b18ecac2b46ef96a62eebb6ec40f9f545410870a200530c4bc9dd3cbf679fec6d824ce5c37b0c8dab88b67bcae3b000924b7dce9940160015ee334d4fe18398f0232d2aca7050388ce4ee5ae82c8148d7f0cea748438b65135ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80ace6842001011600147c7662d92bd5e77454736f94731c60a6e9cbc69f630240e443d66c75b4d5fa71676d60b0b067e6941f06349f31e5f73a7d51a73f5797632b2e01e8584cd1c8730dc16df075866b0c796bd7870182e2da4b37188208fe02200530c4bc9dd3cbf679fec6d824ce5c37b0c8dab88b67bcae3b000924b7dce99402013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa08ba3fae80e01160014aac0345165045e612b3d7363f39a372bead80ce700013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08fe0fae80e01160014aac0345165045e612b3d7363f39a372bead80ce700"
- tx := types.Tx{}
- if err := tx.UnmarshalText([]byte(testData)); err != nil {
- t.Errorf("fail on unmarshal txData: %s", err)
- }
-
- cases := []struct {
- block *bc.Block
- GasValid bool
- err error
- }{
- {
- block: &bc.Block{
- BlockHeader: &bc.BlockHeader{
- Height: ruleAA - 1,
- },
- },
- GasValid: true,
- err: ErrMismatchedPosition,
- },
- {
- block: &bc.Block{
- BlockHeader: &bc.BlockHeader{
- Height: ruleAA,
- },
- },
- GasValid: false,
- err: ErrEmptyInputIDs,
- },
- }
-
- for i, c := range cases {
- gasStatus, err := ValidateTx(tx.Tx, c.block)
- if rootErr(err) != c.err {
- t.Errorf("#%d got error %s, want %s", i, err, c.err)
- }
- if c.GasValid != gasStatus.GasValid {
- t.Errorf("#%d got GasValid %t, want %t", i, gasStatus.GasValid, c.GasValid)
- }
- }
-
-}
-
// TestTimeRange test the checkTimeRange function (txtest#1004)
func TestTimeRange(t *testing.T) {
cases := []struct {
block := &bc.Block{
BlockHeader: &bc.BlockHeader{
Height: 333,
- Timestamp: 1521625823,
+ Timestamp: 1521625823000,
},
}
mockGasTxInput(),
},
Outputs: []*types.TxOutput{
- types.NewTxOutput(*consensus.BTMAssetID, 1, []byte{0x6a}),
+ types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6a}),
},
})
}
}
-func TestStandardTx(t *testing.T) {
- fixture := sample(t, nil)
- tx := types.NewTx(*fixture.tx).Tx
-
+func TestValidateTxVersion(t *testing.T) {
cases := []struct {
- desc string
- f func()
- err error
+ desc string
+ block *bc.Block
+ err error
}{
{
- desc: "normal standard tx",
- err: nil,
+ desc: "tx version greater than 1 (txtest#1001)",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 1},
+ Transactions: []*bc.Tx{
+ {TxHeader: &bc.TxHeader{Version: 2}},
+ },
+ },
+ err: ErrTxVersion,
},
{
- desc: "not standard tx in spend input",
- f: func() {
- inputID := tx.GasInputIDs[0]
- spend := tx.Entries[inputID].(*bc.Spend)
- spentOutput, err := tx.Output(*spend.SpentOutputId)
- if err != nil {
- t.Fatal(err)
- }
- spentOutput.ControlProgram = &bc.Program{Code: []byte{0}}
+ desc: "tx version equals 0 (txtest#1002)",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 1},
+ Transactions: []*bc.Tx{
+ {TxHeader: &bc.TxHeader{Version: 0}},
+ },
},
- err: ErrNotStandardTx,
+ err: ErrTxVersion,
},
{
- desc: "not standard tx in output",
- f: func() {
- outputID := tx.ResultIds[0]
- output := tx.Entries[*outputID].(*bc.Output)
- output.ControlProgram = &bc.Program{Code: []byte{0}}
+ desc: "tx version equals max uint64 (txtest#1003)",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 1},
+ Transactions: []*bc.Tx{
+ {TxHeader: &bc.TxHeader{Version: math.MaxUint64}},
+ },
},
- err: ErrNotStandardTx,
+ err: ErrTxVersion,
},
}
for i, c := range cases {
- if c.f != nil {
- c.f()
- }
- if err := checkStandardTx(tx, 0); err != c.err {
+ if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, err, c.err)
}
}
}
-func TestValidateTxVersion(t *testing.T) {
+func TestMagneticContractTx(t *testing.T) {
+ buyerArgs := vmutil.MagneticContractArgs{
+ RequestedAsset: bc.AssetID{V0: 1},
+ RatioNumerator: 1,
+ RatioDenominator: 2,
+ SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
+ SellerKey: testutil.MustDecodeHexString("af1927316233365dd525d3b48f2869f125a656958ee3946286f42904c35b9c91"),
+ }
+
+ sellerArgs := vmutil.MagneticContractArgs{
+ RequestedAsset: bc.AssetID{V0: 2},
+ RatioNumerator: 2,
+ RatioDenominator: 1,
+ SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
+ SellerKey: testutil.MustDecodeHexString("af1927316233365dd525d3b48f2869f125a656958ee3946286f42904c35b9c91"),
+ }
+
+ programBuyer, err := vmutil.P2WMCProgram(buyerArgs)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ programSeller, err := vmutil.P2WMCProgram(sellerArgs)
+ if err != nil {
+ t.Fatal(err)
+ }
+
cases := []struct {
desc string
block *bc.Block
err error
}{
{
- desc: "tx version greater than 1 (txtest#1001)",
+ desc: "contracts all full trade",
block: &bc.Block{
- BlockHeader: &bc.BlockHeader{Version: 1},
+ BlockHeader: &bc.BlockHeader{Version: 0},
Transactions: []*bc.Tx{
- {TxHeader: &bc.TxHeader{Version: 2}},
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 200000000, 0, programBuyer),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000000, sellerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, buyerArgs.SellerProgram),
+ },
+ }),
},
},
- err: ErrTxVersion,
+ err: nil,
},
{
- desc: "tx version equals 0 (txtest#1002)",
+ desc: "first contract partial trade, second contract full trade",
block: &bc.Block{
- BlockHeader: &bc.BlockHeader{Version: 1},
+ BlockHeader: &bc.BlockHeader{Version: 0},
Transactions: []*bc.Tx{
- {TxHeader: &bc.TxHeader{Version: 0}},
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(100000000), vm.Int64Bytes(0), vm.Int64Bytes(0)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 200000000, 1, programSeller),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(2), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 100000000, 0, programBuyer),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 100000000, sellerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 150000000, programSeller),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000000, buyerArgs.SellerProgram),
+ },
+ }),
},
},
- err: ErrTxVersion,
+ err: nil,
},
{
- desc: "tx version equals max uint64 (txtest#1003)",
+ desc: "first contract full trade, second contract partial trade",
block: &bc.Block{
- BlockHeader: &bc.BlockHeader{Version: 1},
+ BlockHeader: &bc.BlockHeader{Version: 0},
Transactions: []*bc.Tx{
- {TxHeader: &bc.TxHeader{Version: math.MaxUint64}},
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(100000000), vm.Int64Bytes(1), vm.Int64Bytes(0)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 300000000, 0, programBuyer),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000000, sellerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, buyerArgs.SellerProgram),
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 100000000, programBuyer),
+ },
+ }),
},
},
- err: ErrTxVersion,
+ err: nil,
+ },
+ {
+ desc: "cancel magnetic contract",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 0},
+ Transactions: []*bc.Tx{
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{testutil.MustDecodeHexString("851a14d69076507e202a94a884cdfb3b9f1ecbc1fb0634d2f0d1f9c1a275fdbdf921af0c5309d2d0a0deb85973cba23a4076d2c169c7f08ade2af4048d91d209"), vm.Int64Bytes(0), vm.Int64Bytes(2)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 0, programSeller),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, sellerArgs.SellerProgram),
+ },
+ }),
+ },
+ },
+ err: nil,
+ },
+ {
+ desc: "wrong signature with cancel magnetic contract",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 0},
+ Transactions: []*bc.Tx{
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{testutil.MustDecodeHexString("686b983a8de1893ef723144389fd1f07b12b048f52f389faa863243195931d5732dbfd15470b43ed63d5067900718cf94f137073f4a972d277bbd967b022545d"), vm.Int64Bytes(0), vm.Int64Bytes(2)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 0, programSeller),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, sellerArgs.SellerProgram),
+ },
+ }),
+ },
+ },
+ err: vm.ErrFalseVMResult,
+ },
+ {
+ desc: "wrong output amount with contracts all full trade",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 0},
+ Transactions: []*bc.Tx{
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 200000000, 0, programBuyer),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000000, sellerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000000, buyerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000000, []byte{0x55}),
+ },
+ }),
+ },
+ },
+ err: vm.ErrFalseVMResult,
+ },
+ {
+ desc: "wrong output assetID with contracts all full trade",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 0},
+ Transactions: []*bc.Tx{
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 100000000, 1, programSeller),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 200000000, 0, programBuyer),
+ types.NewSpendInput(nil, bc.Hash{V0: 30}, bc.AssetID{V0: 1}, 200000000, 0, []byte{0x51}),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(bc.AssetID{V0: 1}, 200000000, sellerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 100000000, buyerArgs.SellerProgram),
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 200000000, []byte{0x55}),
+ },
+ }),
+ },
+ },
+ err: vm.ErrFalseVMResult,
+ },
+ {
+ desc: "wrong output change program with first contract partial trade and second contract full trade",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 0},
+ Transactions: []*bc.Tx{
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(100000000), vm.Int64Bytes(0), vm.Int64Bytes(0)}, bc.Hash{V0: 10}, buyerArgs.RequestedAsset, 200000000, 1, programSeller),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(2), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, sellerArgs.RequestedAsset, 100000000, 0, programBuyer),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(sellerArgs.RequestedAsset, 100000000, sellerArgs.SellerProgram),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 150000000, []byte{0x55}),
+ types.NewIntraChainOutput(buyerArgs.RequestedAsset, 50000000, buyerArgs.SellerProgram),
+ },
+ }),
+ },
+ },
+ err: vm.ErrFalseVMResult,
},
}
for i, c := range cases {
if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
- t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, err, c.err)
+ t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, rootErr(err), c.err)
+ }
+ }
+}
+
+func TestRingMagneticContractTx(t *testing.T) {
+ aliceArgs := vmutil.MagneticContractArgs{
+ RequestedAsset: bc.AssetID{V0: 1},
+ RatioNumerator: 2,
+ RatioDenominator: 1,
+ SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
+ SellerKey: testutil.MustDecodeHexString("960ecabafb88ba460a40912841afecebf0e84884178611ac97210e327c0d1173"),
+ }
+
+ bobArgs := vmutil.MagneticContractArgs{
+ RequestedAsset: bc.AssetID{V0: 2},
+ RatioNumerator: 2,
+ RatioDenominator: 1,
+ SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
+ SellerKey: testutil.MustDecodeHexString("ad79ec6bd3a6d6dbe4d0ee902afc99a12b9702fb63edce5f651db3081d868b75"),
+ }
+
+ jackArgs := vmutil.MagneticContractArgs{
+ RequestedAsset: bc.AssetID{V0: 3},
+ RatioNumerator: 1,
+ RatioDenominator: 4,
+ SellerProgram: testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19204"),
+ SellerKey: testutil.MustDecodeHexString("9c19a91988c62046c2767bd7e9999b0c142891b9ebf467bfa59210b435cb0de7"),
+ }
+
+ aliceProgram, err := vmutil.P2WMCProgram(aliceArgs)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ bobProgram, err := vmutil.P2WMCProgram(bobArgs)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ jackProgram, err := vmutil.P2WMCProgram(jackArgs)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ cases := []struct {
+ desc string
+ block *bc.Block
+ err error
+ }{
+ {
+ desc: "contracts all full trade",
+ block: &bc.Block{
+ BlockHeader: &bc.BlockHeader{Version: 0},
+ Transactions: []*bc.Tx{
+ types.MapTx(&types.TxData{
+ SerializedSize: 1,
+ Inputs: []*types.TxInput{
+ types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, bc.Hash{V0: 10}, jackArgs.RequestedAsset, 100000000, 0, aliceProgram),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, bc.Hash{V0: 20}, aliceArgs.RequestedAsset, 200000000, 0, bobProgram),
+ types.NewSpendInput([][]byte{vm.Int64Bytes(2), vm.Int64Bytes(1)}, bc.Hash{V0: 30}, bobArgs.RequestedAsset, 400000000, 0, jackProgram),
+ },
+ Outputs: []*types.TxOutput{
+ types.NewIntraChainOutput(aliceArgs.RequestedAsset, 200000000, aliceArgs.SellerProgram),
+ types.NewIntraChainOutput(bobArgs.RequestedAsset, 400000000, bobArgs.SellerProgram),
+ types.NewIntraChainOutput(jackArgs.RequestedAsset, 100000000, jackArgs.SellerProgram),
+ },
+ }),
+ },
+ },
+ err: nil,
+ },
+ }
+
+ for i, c := range cases {
+ if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
+ t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, rootErr(err), c.err)
}
}
}
result.assetDef = []byte{2}
}
if result.assetID.IsZero() {
- refdatahash := hashData(result.assetDef)
- result.assetID = bc.ComputeAssetID(result.issuanceProg.Code, result.issuanceProg.VmVersion, &refdatahash)
+ result.assetID = bc.AssetID{V0: 9999}
}
if result.txVersion == 0 {
args2 := [][]byte{{6}, {7}}
result.txInputs = []*types.TxInput{
- types.NewIssuanceInput([]byte{3}, 10, result.issuanceProg.Code, result.issuanceArgs, result.assetDef),
+ types.NewSpendInput(nil, *newHash(9), result.assetID, 10, 0, []byte{byte(vm.OP_TRUE)}),
types.NewSpendInput(args1, *newHash(5), result.assetID, 20, 0, cp1),
types.NewSpendInput(args2, *newHash(8), result.assetID, 40, 0, cp2),
}
}
result.txOutputs = []*types.TxOutput{
- types.NewTxOutput(result.assetID, 25, cp1),
- types.NewTxOutput(result.assetID, 45, cp2),
+ types.NewIntraChainOutput(result.assetID, 25, cp1),
+ types.NewIntraChainOutput(result.assetID, 45, cp2),
}
}
return &a
}
-func txIssuance(t *testing.T, tx *bc.Tx, index int) *bc.Issuance {
- id := tx.InputIDs[index]
- res, err := tx.Issuance(id)
- if err != nil {
- t.Fatal(err)
- }
- return res
-}
-
func txSpend(t *testing.T, tx *bc.Tx, index int) *bc.Spend {
id := tx.InputIDs[index]
res, err := tx.Spend(id)
func getMuxID(tx *bc.Tx) *bc.Hash {
out := tx.Entries[*tx.ResultIds[0]]
switch result := out.(type) {
- case *bc.Output:
+ case *bc.IntraChainOutput:
return result.Source.Ref
case *bc.Retirement:
return result.Source.Ref