当前位置: 首页>>代码示例>>Golang>>正文


Golang MsgTx.AddTxIn方法代码示例

本文整理汇总了Golang中github.com/decred/dcrd/wire.MsgTx.AddTxIn方法的典型用法代码示例。如果您正苦于以下问题:Golang MsgTx.AddTxIn方法的具体用法?Golang MsgTx.AddTxIn怎么用?Golang MsgTx.AddTxIn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/decred/dcrd/wire.MsgTx的用法示例。


在下文中一共展示了MsgTx.AddTxIn方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestForVMFailure

// TestForVMFailure feeds random scripts to the VMs to check and see if it
// crashes. Try increasing the number of iterations or the length of the
// byte string to sample a greater space.
func TestForVMFailure(t *testing.T) {
	numTests := 2
	bsLength := 11

	for i := 0; i < numTests; i++ {
		tests := randByteSliceSlice(65536, bsLength, i)

		for j := range tests {
			if j == 0 {
				continue
			}

			msgTx := new(wire.MsgTx)
			msgTx.AddTxIn(&wire.TxIn{
				PreviousOutPoint: wire.OutPoint{},
				SignatureScript:  tests[j-1],
				Sequence:         0xFFFFFFFF,
			})
			msgTx.AddTxOut(&wire.TxOut{
				Value:    0x00FFFFFF00000000,
				PkScript: []byte{0x01},
			})
			flags := StandardVerifyFlags
			engine, err := NewEngine(tests[j], msgTx, 0, flags, 0,
				nil)

			if err == nil {
				engine.Execute()
			}
		}
	}
}
开发者ID:decred,项目名称:dcrd,代码行数:35,代码来源:opcode_test.go

示例2: NewTxDeepTxIns

// NewTxDeepTxIns is used to deep copy a transaction, maintaining the old
// pointers to the TxOuts while replacing the old pointers to the TxIns with
// deep copies. This is to prevent races when the fraud proofs for the
// transactions are set by the miner.
func NewTxDeepTxIns(msgTx *wire.MsgTx) *Tx {
	if msgTx == nil {
		return nil
	}

	newMsgTx := new(wire.MsgTx)

	// Copy the fixed fields.
	newMsgTx.Version = msgTx.Version
	newMsgTx.LockTime = msgTx.LockTime
	newMsgTx.Expiry = msgTx.Expiry

	// Copy the TxIns deeply.
	for _, txIn := range msgTx.TxIn {
		sigScrLen := len(txIn.SignatureScript)
		sigScrCopy := make([]byte, sigScrLen, sigScrLen)

		txInCopy := new(wire.TxIn)
		txInCopy.PreviousOutPoint.Hash = txIn.PreviousOutPoint.Hash
		txInCopy.PreviousOutPoint.Index = txIn.PreviousOutPoint.Index
		txInCopy.PreviousOutPoint.Tree = txIn.PreviousOutPoint.Tree

		txInCopy.Sequence = txIn.Sequence
		txInCopy.ValueIn = txIn.ValueIn
		txInCopy.BlockHeight = txIn.BlockHeight
		txInCopy.BlockIndex = txIn.BlockIndex

		txInCopy.SignatureScript = sigScrCopy

		newMsgTx.AddTxIn(txIn)
	}

	// Shallow copy the TxOuts.
	for _, txOut := range msgTx.TxOut {
		newMsgTx.AddTxOut(txOut)
	}

	return &Tx{
		hash:    msgTx.TxHash(),
		msgTx:   msgTx,
		txTree:  wire.TxTreeUnknown,
		txIndex: TxIndexUnknown,
	}
}
开发者ID:decred,项目名称:dcrutil,代码行数:48,代码来源:tx.go

示例3: Test_dupTx


//.........这里部分代码省略.........
				}
			}
		}
		newheight, err := db.InsertBlock(block)
		if err != nil {
			t.Errorf("failed to insert block %v err %v", height, err)
			break out
		}
		if newheight != height {
			t.Errorf("height mismatch expect %v returned %v", height, newheight)
			break out
		}

		newSha, blkid, err := db.NewestSha()
		if err != nil {
			t.Errorf("failed to obtain latest sha %v %v", height, err)
		}

		if blkid != height {
			t.Errorf("height doe not match latest block height %v %v %v", blkid, height, err)
		}

		blkSha := block.Sha()
		if *newSha != *blkSha {
			t.Errorf("Newest block sha does not match freshly inserted one %v %v %v ", newSha, blkSha, err)
		}
		lastSha = blkSha
	}

	// generate a new block based on the last sha
	// these block are not verified, so there are a bunch of garbage fields
	// in the 'generated' block.

	var bh wire.BlockHeader

	bh.Version = 0
	bh.PrevBlock = *lastSha
	// Bits, Nonce are not filled in

	mblk := wire.NewMsgBlock(&bh)

	hash, _ := chainhash.NewHashFromStr("c23953c56cb2ef8e4698e3ed3b0fc4c837754d3cd16485192d893e35f32626b4")

	po := wire.NewOutPoint(hash, 0, dcrutil.TxTreeRegular)
	txI := wire.NewTxIn(po, []byte("garbage"))
	txO := wire.NewTxOut(50000000, []byte("garbageout"))

	var tx wire.MsgTx
	tx.AddTxIn(txI)
	tx.AddTxOut(txO)

	mblk.AddTransaction(&tx)

	blk := dcrutil.NewBlock(mblk)

	fetchList := []*chainhash.Hash{hash}
	listReply := db.FetchUnSpentTxByShaList(fetchList)
	for _, lr := range listReply {
		if lr.Err != nil {
			t.Errorf("sha %v spent %v err %v\n", lr.Sha,
				lr.TxSpent, lr.Err)
		}
	}

	_, err = db.InsertBlock(blk)
	if err != nil {
		t.Errorf("failed to insert phony block %v", err)
	}

	// ok, did it 'spend' the tx ?

	listReply = db.FetchUnSpentTxByShaList(fetchList)
	for _, lr := range listReply {
		if lr.Err != nil && lr.Err != database.ErrTxShaMissing {
			t.Errorf("sha %v spent %v err %v\n", lr.Sha,
				lr.TxSpent, lr.Err)
		}
	}

	txlist := blk.Transactions()
	for _, tx := range txlist {
		txsha := tx.Sha()
		txReply, err := db.FetchTxBySha(txsha)
		if err != nil {
			t.Errorf("fully spent lookup %v err %v\n", hash, err)
		} else {
			for _, lr := range txReply {
				if lr.Err != nil {
					t.Errorf("stx %v spent %v err %v\n", lr.Sha,
						lr.TxSpent, lr.Err)
				}
			}
		}
	}

	err = db.DropAfterBlockBySha(lastSha)
	if err != nil {
		t.Errorf("failed to drop spending block %v", err)
	}
}
开发者ID:ironbits,项目名称:dcrd,代码行数:101,代码来源:dup_test.go

示例4: TestNewlyEnabledOpCodes


//.........这里部分代码省略.........
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptLogic,
			expected:  true,
		},
		{
			name: "xor",
			pkScript: []byte{
				0x86, // OP_XOR
				0x04, // Expected result push
				0x30, 0xe2, 0x34, 0xa9,
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptLogic,
			expected:  true,
		},
		{
			name: "cat",
			pkScript: []byte{
				0x7e, // OP_CAT
				0x0c, // Expected result push
				0x21, 0x12, 0x34, 0x56, 0x44, 0x55,
				0x0f, 0xf0, 0x00, 0xff, 0x88, 0x99,
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptCat,
			expected:  true,
		},
		{
			name: "catoverflow",
			pkScript: []byte{
				0x7e, // OP_CAT
				0x0c, // Expected result push
				0x21, 0x12, 0x34, 0x56, 0x44, 0x55,
				0x0f, 0xf0, 0x00, 0xff, 0x88, 0x99,
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptCatOverflow,
			expected:  false,
		},
		{
			name: "substr",
			pkScript: []byte{
				0x7f, // OP_SUBSTR
				0x04, // Expected result push
				0x34, 0x56, 0x59, 0x32,
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptSubstr,
			expected:  true,
		},
		{
			name: "left",
			pkScript: []byte{
				0x80, // OP_LEFT
				0x04, // Expected result push
				0x21, 0x12, 0x34, 0x56,
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptLR,
			expected:  true,
		},
		{
			name: "right",
			pkScript: []byte{
				0x81, // OP_RIGHT
				0x04, // Expected result push
				0x59, 0x32, 0x40, 0x21,
				0x87, // OP_EQUAL
			},
			sigScript: sigScriptLR,
			expected:  true,
		},
	}

	for _, test := range tests {
		msgTx := new(wire.MsgTx)
		msgTx.AddTxIn(&wire.TxIn{
			PreviousOutPoint: wire.OutPoint{},
			SignatureScript:  test.sigScript,
			Sequence:         0xFFFFFFFF,
		})
		msgTx.AddTxOut(&wire.TxOut{
			Value:    0x00FFFFFF00000000,
			PkScript: []byte{0x01},
		})
		flags := StandardVerifyFlags
		engine, err := NewEngine(test.pkScript, msgTx, 0, flags, 0, nil)
		if err != nil {
			t.Errorf("Bad script result for test %v because of error: %v",
				test.name, err.Error())
			continue
		}
		err = engine.Execute()
		if err != nil && test.expected {
			t.Errorf("Bad script exec for test %v because of error: %v",
				test.name, err.Error())
		}
	}
}
开发者ID:decred,项目名称:dcrd,代码行数:101,代码来源:opcode_test.go

示例5: TestCalcSignatureHash

// TestCalcSignatureHash does some rudimentary testing of msg hash calculation.
func TestCalcSignatureHash(t *testing.T) {
	tx := new(wire.MsgTx)
	for i := 0; i < 3; i++ {
		txIn := new(wire.TxIn)
		txIn.Sequence = 0xFFFFFFFF
		txIn.PreviousOutPoint.Hash = chainhash.HashFuncH([]byte{byte(i)})
		txIn.PreviousOutPoint.Index = uint32(i)
		txIn.PreviousOutPoint.Tree = int8(0)
		tx.AddTxIn(txIn)
	}
	for i := 0; i < 2; i++ {
		txOut := new(wire.TxOut)
		txOut.PkScript = []byte{0x01, 0x01, 0x02, 0x03}
		txOut.Value = 0x0000FF00FF00FF00
		tx.AddTxOut(txOut)
	}

	want, _ := hex.DecodeString("d09285b6f60c71329323bc2e76c48" +
		"a462cde4e1032aa8f59c55823f1722c7f4a")
	pops, _ := txscript.TstParseScript([]byte{0x01, 0x01, 0x02, 0x03})

	// Test prefix caching.
	msg1, err := txscript.CalcSignatureHash(pops, txscript.SigHashAll, tx, 0, nil)
	if err != nil {
		t.Fatalf("unexpected error %v", err.Error())
	}

	prefixHash := tx.TxSha()
	msg2, err := txscript.CalcSignatureHash(pops, txscript.SigHashAll, tx, 0,
		&prefixHash)
	if err != nil {
		t.Fatalf("unexpected error %v", err.Error())
	}

	if !bytes.Equal(msg1, want) {
		t.Errorf("for sighash all sig noncached wrong msg %x given, want %x",
			msg1,
			want)
	}
	if !bytes.Equal(msg2, want) {
		t.Errorf("for sighash all sig cached wrong msg %x given, want %x",
			msg1,
			want)
	}
	if !bytes.Equal(msg1, msg2) {
		t.Errorf("for sighash all sig non-equivalent msgs %x and %x were "+
			"returned when using a cached prefix",
			msg1,
			msg2)
	}

	// Move the index and make sure that we get a whole new hash, despite
	// using the same TxOuts.
	msg3, err := txscript.CalcSignatureHash(pops, txscript.SigHashAll, tx, 1,
		&prefixHash)
	if err != nil {
		t.Fatalf("unexpected error %v", err.Error())
	}

	if bytes.Equal(msg1, msg3) {
		t.Errorf("for sighash all sig equivalent msgs %x and %x were "+
			"returned when using a cached prefix but different indices",
			msg1,
			msg3)
	}
}
开发者ID:zebbra2014,项目名称:dcrd,代码行数:67,代码来源:script_test.go


注:本文中的github.com/decred/dcrd/wire.MsgTx.AddTxIn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。