9 "github.com/bytom/crypto/sha3pool"
10 "github.com/bytom/errors"
11 "github.com/bytom/protocol/bc"
12 "github.com/bytom/protocol/bc/bctest"
13 "github.com/bytom/protocol/bc/legacy"
14 "github.com/bytom/protocol/vm"
15 "github.com/bytom/testutil"
17 "github.com/davecgh/go-spew/spew"
18 "github.com/golang/protobuf/proto"
22 spew.Config.DisableMethods = true
25 func TestTxValidation(t *testing.T) {
31 // the mux from tx, pulled out for convenience
36 desc string // description of the test case
37 f func() // function to adjust tx, vs, and/or mux
38 err error // expected error
44 desc: "failing mux program",
46 mux.Program.Code = []byte{byte(vm.OP_FALSE)}
48 err: vm.ErrFalseVMResult,
51 desc: "unbalanced mux amounts",
53 mux.Sources[0].Value.Amount++
54 iss := tx.Entries[*mux.Sources[0].Ref].(*bc.Issuance)
55 iss.WitnessDestination.Value.Amount++
60 desc: "overflowing mux source amounts",
62 mux.Sources[0].Value.Amount = math.MaxInt64
63 iss := tx.Entries[*mux.Sources[0].Ref].(*bc.Issuance)
64 iss.WitnessDestination.Value.Amount = math.MaxInt64
69 desc: "underflowing mux destination amounts",
71 mux.WitnessDestinations[0].Value.Amount = math.MaxInt64
72 out := tx.Entries[*mux.WitnessDestinations[0].Ref].(*bc.Output)
73 out.Source.Value.Amount = math.MaxInt64
74 mux.WitnessDestinations[1].Value.Amount = math.MaxInt64
75 out = tx.Entries[*mux.WitnessDestinations[1].Ref].(*bc.Output)
76 out.Source.Value.Amount = math.MaxInt64
81 desc: "unbalanced mux assets",
83 mux.Sources[1].Value.AssetId = newAssetID(255)
84 sp := tx.Entries[*mux.Sources[1].Ref].(*bc.Spend)
85 sp.WitnessDestination.Value.AssetId = newAssetID(255)
90 desc: "nonempty mux exthash",
92 mux.ExtHash = newHash(1)
94 err: errNonemptyExtHash,
97 desc: "nonempty mux exthash, but that's OK",
100 mux.ExtHash = newHash(1)
104 desc: "failing nonce program",
106 iss := txIssuance(t, tx, 0)
107 nonce := tx.Entries[*iss.AnchorId].(*bc.Nonce)
108 nonce.Program.Code = []byte{byte(vm.OP_FALSE)}
110 err: vm.ErrFalseVMResult,
113 desc: "nonce exthash nonempty",
115 iss := txIssuance(t, tx, 0)
116 nonce := tx.Entries[*iss.AnchorId].(*bc.Nonce)
117 nonce.ExtHash = newHash(1)
119 err: errNonemptyExtHash,
122 desc: "nonce exthash nonempty, but that's OK",
125 iss := txIssuance(t, tx, 0)
126 nonce := tx.Entries[*iss.AnchorId].(*bc.Nonce)
127 nonce.ExtHash = newHash(1)
131 desc: "mismatched output source / mux dest position",
133 tx.Entries[*tx.ResultIds[0]].(*bc.Output).Source.Position = 1
135 err: errMismatchedPosition,
138 desc: "mismatched output source and mux dest",
140 // For this test, it's necessary to construct a mostly
141 // identical second transaction in order to get a similar but
142 // not equal output entry for the mux to falsely point
143 // to. That entry must be added to the first tx's Entries map.
144 fixture.txOutputs[0].ReferenceData = []byte{1}
145 fixture2 := sample(t, fixture)
146 tx2 := legacy.NewTx(*fixture2.tx).Tx
147 out2ID := tx2.ResultIds[0]
148 out2 := tx2.Entries[*out2ID].(*bc.Output)
149 tx.Entries[*out2ID] = out2
150 mux.WitnessDestinations[0].Ref = out2ID
152 err: errMismatchedReference,
155 desc: "invalid mux destination position",
157 mux.WitnessDestinations[0].Position = 1
162 desc: "mismatched mux dest value / output source value",
164 outID := tx.ResultIds[0]
165 out := tx.Entries[*outID].(*bc.Output)
166 mux.WitnessDestinations[0].Value = &bc.AssetAmount{
167 AssetId: out.Source.Value.AssetId,
168 Amount: out.Source.Value.Amount + 1,
170 mux.Sources[0].Value.Amount++ // the mux must still balance
172 err: errMismatchedValue,
175 desc: "output exthash nonempty",
177 tx.Entries[*tx.ResultIds[0]].(*bc.Output).ExtHash = newHash(1)
179 err: errNonemptyExtHash,
182 desc: "output exthash nonempty, but that's OK",
185 tx.Entries[*tx.ResultIds[0]].(*bc.Output).ExtHash = newHash(1)
189 desc: "misordered tx time range",
191 tx.MinTimeMs = tx.MaxTimeMs + 1
193 err: errBadTimeRange,
196 desc: "empty tx results",
200 err: errEmptyResults,
203 desc: "empty tx results, but that's OK",
210 desc: "tx header exthash nonempty",
212 tx.ExtHash = newHash(1)
214 err: errNonemptyExtHash,
217 desc: "tx header exthash nonempty, but that's OK",
220 tx.ExtHash = newHash(1)
224 desc: "issuance program failure",
226 iss := txIssuance(t, tx, 0)
227 iss.WitnessArguments[0] = []byte{}
229 err: vm.ErrFalseVMResult,
232 desc: "issuance exthash nonempty",
234 iss := txIssuance(t, tx, 0)
235 iss.ExtHash = newHash(1)
237 err: errNonemptyExtHash,
240 desc: "issuance exthash nonempty, but that's OK",
243 iss := txIssuance(t, tx, 0)
244 iss.ExtHash = newHash(1)
248 desc: "spend control program failure",
250 spend := txSpend(t, tx, 1)
251 spend.WitnessArguments[0] = []byte{}
253 err: vm.ErrFalseVMResult,
256 desc: "mismatched spent source/witness value",
258 spend := txSpend(t, tx, 1)
259 spentOutput := tx.Entries[*spend.SpentOutputId].(*bc.Output)
260 spentOutput.Source.Value = &bc.AssetAmount{
261 AssetId: spend.WitnessDestination.Value.AssetId,
262 Amount: spend.WitnessDestination.Value.Amount + 1,
265 err: errMismatchedValue,
268 desc: "spend exthash nonempty",
270 spend := txSpend(t, tx, 1)
271 spend.ExtHash = newHash(1)
273 err: errNonemptyExtHash,
276 desc: "spend exthash nonempty, but that's OK",
279 spend := txSpend(t, tx, 1)
280 spend.ExtHash = newHash(1)
285 for _, c := range cases {
286 t.Run(c.desc, func(t *testing.T) {
287 fixture = sample(t, nil)
288 tx = legacy.NewTx(*fixture.tx).Tx
289 vs = &validationState{
294 gasLeft: uint64(1000),
297 cache: make(map[bc.Hash]error),
299 out := tx.Entries[*tx.ResultIds[0]].(*bc.Output)
300 muxID := out.Source.Ref
301 mux = tx.Entries[*muxID].(*bc.Mux)
306 err := checkValid(vs, tx.TxHeader)
307 if rootErr(err) != c.err {
308 t.Errorf("got error %s, want %s; validationState is:\n%s", err, c.err, spew.Sdump(vs))
314 func TestNoncelessIssuance(t *testing.T) {
315 tx := bctest.NewIssuanceTx(t, bc.EmptyStringHash, func(tx *legacy.Tx) {
316 // Remove the issuance nonce.
317 tx.Inputs[0].TypedInput.(*legacy.IssuanceInput).Nonce = nil
320 _, err := ValidateTx(legacy.MapTx(&tx.TxData), nil)
321 if errors.Root(err) != bc.ErrMissingEntry {
322 t.Fatalf("got %s, want %s", err, bc.ErrMissingEntry)
326 func TestBlockHeaderValid(t *testing.T) {
327 base := bc.NewBlockHeader(1, 1, &bc.Hash{}, 1, &bc.Hash{}, &bc.Hash{}, 0, 0)
328 baseBytes, _ := proto.Marshal(base)
330 var bh bc.BlockHeader
344 for i, c := range cases {
345 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
346 proto.Unmarshal(baseBytes, &bh)
354 // A txFixture is returned by sample (below) to produce a sample
355 // transaction, which takes a separate, optional _input_ txFixture to
356 // affect the transaction that's built. The components of the
357 // transaction are the fields of txFixture.
358 type txFixture struct {
359 initialBlockID bc.Hash
360 issuanceProg bc.Program
361 issuanceArgs [][]byte
365 txInputs []*legacy.TxInput
366 txOutputs []*legacy.TxOutput
367 txMinTime, txMaxTime uint64
372 // Produces a sample transaction in a txFixture object (see above). A
373 // separate input txFixture can be used to alter the transaction
376 // The output of this function can be used as the input to a
377 // subsequent call to make iterative refinements to a test object.
379 // The default transaction produced is valid and has three inputs:
380 // - an issuance of 10 units
381 // - a spend of 20 units
382 // - a spend of 40 units
383 // and two outputs, one of 25 units and one of 45 units.
384 // All amounts are denominated in the same asset.
386 // The issuance program for the asset requires two numbers as
387 // arguments that add up to 5. The prevout control programs require
388 // two numbers each, adding to 9 and 13, respectively.
390 // The min and max times for the transaction are now +/- one minute.
391 func sample(tb testing.TB, in *txFixture) *txFixture {
397 if result.initialBlockID.IsZero() {
398 result.initialBlockID = *newHash(1)
400 if testutil.DeepEqual(result.issuanceProg, bc.Program{}) {
401 prog, err := vm.Assemble("ADD 5 NUMEQUAL")
405 result.issuanceProg = bc.Program{VmVersion: 1, Code: prog}
407 if len(result.issuanceArgs) == 0 {
408 result.issuanceArgs = [][]byte{[]byte{2}, []byte{3}}
410 if len(result.assetDef) == 0 {
411 result.assetDef = []byte{2}
413 if result.assetID.IsZero() {
414 refdatahash := hashData(result.assetDef)
415 result.assetID = bc.ComputeAssetID(result.issuanceProg.Code, &result.initialBlockID, result.issuanceProg.VmVersion, &refdatahash)
418 if result.txVersion == 0 {
421 if len(result.txInputs) == 0 {
422 cp1, err := vm.Assemble("ADD 9 NUMEQUAL")
426 args1 := [][]byte{[]byte{4}, []byte{5}}
428 cp2, err := vm.Assemble("ADD 13 NUMEQUAL")
432 args2 := [][]byte{[]byte{6}, []byte{7}}
434 result.txInputs = []*legacy.TxInput{
435 legacy.NewIssuanceInput([]byte{3}, 10, []byte{4}, result.initialBlockID, result.issuanceProg.Code, result.issuanceArgs, result.assetDef),
436 legacy.NewSpendInput(args1, *newHash(5), result.assetID, 20, 0, cp1, *newHash(6), []byte{7}),
437 legacy.NewSpendInput(args2, *newHash(8), result.assetID, 40, 0, cp2, *newHash(9), []byte{10}),
440 if len(result.txOutputs) == 0 {
441 cp1, err := vm.Assemble("ADD 17 NUMEQUAL")
445 cp2, err := vm.Assemble("ADD 21 NUMEQUAL")
450 result.txOutputs = []*legacy.TxOutput{
451 legacy.NewTxOutput(result.assetID, 25, cp1, []byte{11}),
452 legacy.NewTxOutput(result.assetID, 45, cp2, []byte{12}),
455 if result.txMinTime == 0 {
456 result.txMinTime = bc.Millis(time.Now().Add(-time.Minute))
458 if result.txMaxTime == 0 {
459 result.txMaxTime = bc.Millis(time.Now().Add(time.Minute))
461 if len(result.txRefData) == 0 {
462 result.txRefData = []byte{13}
465 result.tx = &legacy.TxData{
466 Version: result.txVersion,
467 Inputs: result.txInputs,
468 Outputs: result.txOutputs,
469 MinTime: result.txMinTime,
470 MaxTime: result.txMaxTime,
471 ReferenceData: result.txRefData,
477 // Like errors.Root, but also unwraps vm.Error objects.
478 func rootErr(e error) error {
481 if e2, ok := e.(vm.Error); ok {
489 func hashData(data []byte) bc.Hash {
491 sha3pool.Sum256(b32[:], data)
492 return bc.NewHash(b32)
495 func newHash(n byte) *bc.Hash {
496 h := bc.NewHash([32]byte{n})
500 func newAssetID(n byte) *bc.AssetID {
501 a := bc.NewAssetID([32]byte{n})
505 func txIssuance(t *testing.T, tx *bc.Tx, index int) *bc.Issuance {
506 id := tx.InputIDs[index]
507 res, err := tx.Issuance(id)
514 func txSpend(t *testing.T, tx *bc.Tx, index int) *bc.Spend {
515 id := tx.InputIDs[index]
516 res, err := tx.Spend(id)