From bed2d1b0524cc5c3c0040e8de0736213fa487568 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 22 Jul 2018 12:30:22 +0300 Subject: [PATCH] [DEV-31] Regenrate transactions for mining policy tests --- integration/rpctest/memwallet.go | 2 +- mining/policy_test.go | 131 +++++++++++++++++++------------ rpcserver.go | 2 +- 3 files changed, 81 insertions(+), 54 deletions(-) diff --git a/integration/rpctest/memwallet.go b/integration/rpctest/memwallet.go index 72f8d4b5b..eb6980599 100644 --- a/integration/rpctest/memwallet.go +++ b/integration/rpctest/memwallet.go @@ -560,7 +560,7 @@ func (m *memWallet) ConfirmedBalance() btcutil.Amount { } // keyToAddr maps the passed private to corresponding p2pkh address. -func keyToAddr(key *btcec.PrivateKey, net *chaincfg.Params) (btcutil.Address, error) { +func keyToAddr(key *btcec.PrivateKey, net *dagconfig.Params) (btcutil.Address, error) { serializedKey := key.PubKey().SerializeCompressed() pubKeyAddr, err := btcutil.NewAddressPubKey(serializedKey, net) if err != nil { diff --git a/mining/policy_test.go b/mining/policy_test.go index 07a229622..cfaaccc48 100644 --- a/mining/policy_test.go +++ b/mining/policy_test.go @@ -6,11 +6,14 @@ package mining import ( "encoding/hex" - "math" + "fmt" "testing" "github.com/daglabs/btcd/blockdag" + "github.com/daglabs/btcd/btcec" + "github.com/daglabs/btcd/dagconfig" "github.com/daglabs/btcd/dagconfig/daghash" + "github.com/daglabs/btcd/txscript" "github.com/daglabs/btcd/wire" "github.com/daglabs/btcutil" ) @@ -55,6 +58,61 @@ func newUtxoViewpoint(sourceTxns []*wire.MsgTx, sourceTxHeights []int32) *blockd return view } +func getTxIn(originTx *wire.MsgTx, outputIndex uint32) *wire.TxIn { + var prevOut *wire.OutPoint + if originTx != nil { + originTxHash := originTx.TxHash() + prevOut = wire.NewOutPoint(&originTxHash, 0) + } else { + prevOut = &wire.OutPoint{ + Hash: daghash.Hash{}, + Index: 0xFFFFFFFF, + } + } + return wire.NewTxIn(prevOut, nil) +} + +func createTransaction(value int64, originTx *wire.MsgTx, outputIndex uint32, sigScript []byte) (*wire.MsgTx, error) { + lookupKey := func(a btcutil.Address) (*btcec.PrivateKey, bool, error) { + // Ordinarily this function would involve looking up the private + // key for the provided address, but since the only thing being + // signed in this example uses the address associated with the + // private key from above, simply return it with the compressed + // flag set since the address is using the associated compressed + // public key. + // + // NOTE: If you want to prove the code is actually signing the + // transaction properly, uncomment the following line which + // intentionally returns an invalid key to sign with, which in + // turn will result in a failure during the script execution + // when verifying the signature. + // + // privKey.D.SetInt64(12345) + // + return privKey, true, nil + } + + redeemTx := wire.NewMsgTx(wire.TxVersion) + + redeemTx.AddTxIn(getTxIn(originTx, outputIndex)) + + pkScript, err := txscript.PayToAddrScript(addr) + if err != nil { + fmt.Println(err) + return nil, err + } + + txOut := wire.NewTxOut(value, pkScript) + redeemTx.AddTxOut(txOut) + if sigScript == nil { + sigScript, err = txscript.SignTxOutput(&dagconfig.MainNetParams, + redeemTx, 0, originTx.TxOut[0].PkScript, txscript.SigHashAll, + txscript.KeyClosure(lookupKey), nil, nil) + } + redeemTx.TxIn[0].SignatureScript = sigScript + return redeemTx, nil +} + // TestCalcPriority ensures the priority calculations work as intended. func TestCalcPriority(t *testing.T) { // commonSourceTx1 is a valid transaction used in the tests below as an @@ -62,59 +120,20 @@ func TestCalcPriority(t *testing.T) { // // From block 7 in main blockchain. // tx 0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9 - commonSourceTx1 := &wire.MsgTx{ - Version: 1, - TxIn: []*wire.TxIn{{ - PreviousOutPoint: wire.OutPoint{ - Hash: daghash.Hash{}, - Index: wire.MaxPrevOutIndex, - }, - SignatureScript: hexToBytes("04ffff001d0134"), - Sequence: math.MaxUint64, - }}, - TxOut: []*wire.TxOut{{ - Value: 5000000000, - PkScript: hexToBytes("410411db93e1dcdb8a016b49840f8c5" + - "3bc1eb68a382e97b1482ecad7b148a6909a5cb2e0ead" + - "dfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8" + - "643f656b412a3ac"), - }}, - LockTime: 0, + commonSourceTx1, err := createTransaction(5000000000, nil, 0, hexToBytes("04ffff001d0134")) + + if err != nil { + t.Errorf("Error with creating source tx: %v", err) } // commonRedeemTx1 is a valid transaction used in the tests below as the // transaction to calculate the priority for. // // It originally came from block 170 in main blockchain. - commonRedeemTx1 := &wire.MsgTx{ - Version: 1, - TxIn: []*wire.TxIn{{ - PreviousOutPoint: wire.OutPoint{ - Hash: *newHashFromStr("0437cd7f8525ceed232435" + - "9c2d0ba26006d92d856a9c20fa0241106ee5" + - "a597c9"), - Index: 0, - }, - SignatureScript: hexToBytes("47304402204e45e16932b8af" + - "514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5f" + - "b8cd410220181522ec8eca07de4860a4acdd12909d83" + - "1cc56cbbac4622082221a8768d1d0901"), - Sequence: math.MaxUint64, - }}, - TxOut: []*wire.TxOut{{ - Value: 1000000000, - PkScript: hexToBytes("4104ae1a62fe09c5f51b13905f07f06" + - "b99a2f7159b2225f374cd378d71302fa28414e7aab37" + - "397f554a7df5f142c21c1b7303b8a0626f1baded5c72" + - "a704f7e6cd84cac"), - }, { - Value: 4000000000, - PkScript: hexToBytes("410411db93e1dcdb8a016b49840f8c5" + - "3bc1eb68a382e97b1482ecad7b148a6909a5cb2e0ead" + - "dfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8" + - "643f656b412a3ac"), - }}, - LockTime: 0, + commonRedeemTx1, err := createTransaction(5000000000, commonSourceTx1, 0, nil) + + if err != nil { + t.Errorf("Error with creating redeem tx: %v", err) } tests := []struct { @@ -130,7 +149,7 @@ func TestCalcPriority(t *testing.T) { utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1}, []int32{7}), nextHeight: 169, - want: 5e9, + want: 1.5576923076923077e+10, }, { name: "one height 100 input, prio tx height 169", @@ -138,7 +157,7 @@ func TestCalcPriority(t *testing.T) { utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1}, []int32{100}), nextHeight: 169, - want: 2129629629.6296296, + want: 6.634615384615385e+09, }, { name: "one height 7 input, prio tx height 100000", @@ -146,7 +165,7 @@ func TestCalcPriority(t *testing.T) { utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1}, []int32{7}), nextHeight: 100000, - want: 3086203703703.7036, + want: 9.61471153846154e+12, }, { name: "one height 100 input, prio tx height 100000", @@ -154,7 +173,7 @@ func TestCalcPriority(t *testing.T) { utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1}, []int32{100}), nextHeight: 100000, - want: 3083333333333.3335, + want: 9.60576923076923e+12, }, } @@ -167,3 +186,11 @@ func TestCalcPriority(t *testing.T) { } } } + +var privKeyBytes, _ = hex.DecodeString("22a47fa09a223f2aa079edf85a7c2" + + "d4f8720ee63e502ee2869afab7de234b80c") + +var privKey, pubKey = btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes) +var pubKeyHash = btcutil.Hash160(pubKey.SerializeCompressed()) +var addr, err = btcutil.NewAddressPubKeyHash(pubKeyHash, + &dagconfig.MainNetParams) diff --git a/rpcserver.go b/rpcserver.go index 1f66a5492..bb9f51c3b 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -741,7 +741,7 @@ func createTxRawResult(chainParams *dagconfig.Params, mtx *wire.MsgTx, if blkHeader != nil { // This is not a typo, they are identical in bitcoind as well. - txReply.Time = blkHeader.Timestamp.Unix() + txReply.Time = uint64(blkHeader.Timestamp.Unix()) txReply.Blocktime = uint64(blkHeader.Timestamp.Unix()) txReply.BlockHash = blkHash txReply.Confirmations = uint64(1 + chainHeight - blkHeight)