本文整理匯總了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()
}
}
}
}
示例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,
}
}
示例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)
}
}
示例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())
}
}
}
示例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)
}
}