@@ -2852,3 +2852,198 @@ func GeneratePhononBlocks() (tests [][]TestInstance, err error) {
28522852 })
28532853 return tests , nil
28542854}
2855+
2856+ // GenerateCosmicInflationBlocks generates a test chain containing several
2857+ // 64 bit integer and native introspection transactions per block.
2858+ func GenerateCosmicInflationBlocks () (tests [][]TestInstance , err error ) {
2859+ // In order to simplify the generation code which really should never
2860+ // fail unless the test code itself is broken, panics are used
2861+ // internally. This deferred func ensures any panics don't escape the
2862+ // generator by replacing the named error return with the underlying
2863+ // panic error.
2864+ defer func () {
2865+ if r := recover (); r != nil {
2866+ tests = nil
2867+
2868+ switch rt := r .(type ) {
2869+ case string :
2870+ err = errors .New (rt )
2871+ case error :
2872+ err = rt
2873+ default :
2874+ err = errors .New ("Unknown panic" )
2875+ }
2876+ }
2877+ }()
2878+
2879+ // Create a test generator instance initialized with the genesis block
2880+ // as the tip.
2881+ g , err := makeTestGenerator (regressionNetParams )
2882+ if err != nil {
2883+ return nil , err
2884+ }
2885+
2886+ acceptBlock := func (blockName string , block * wire.MsgBlock , isMainChain , isOrphan bool ) TestInstance {
2887+ blockHeight := g .blockHeights [blockName ]
2888+ return AcceptedBlock {blockName , block , blockHeight , isMainChain ,
2889+ isOrphan }
2890+ }
2891+
2892+ coinbaseMaturity := g .params .CoinbaseMaturity
2893+ var testInstances []TestInstance
2894+ for i := uint16 (0 ); i < coinbaseMaturity ; i ++ {
2895+ blockName := fmt .Sprintf ("bm%d" , i )
2896+ g .nextBlock (blockName , nil )
2897+ g .saveTipCoinbaseOut ()
2898+ testInstances = append (testInstances , acceptBlock (g .tipName ,
2899+ g .tip , true , false ))
2900+ }
2901+ tests = append (tests , testInstances )
2902+
2903+ // Collect spendable outputs. This simplifies the code below.
2904+ var outs []* spendableOut
2905+ for i := uint16 (0 ); i < coinbaseMaturity ; i ++ {
2906+ op := g .oldestCoinbaseOut ()
2907+ outs = append (outs , & op )
2908+ }
2909+
2910+ add64BitInteger := func (block * wire.MsgBlock ) {
2911+ // unsortedTxs is a slice of bchutil.Tx's that we will sort later using CTOR.
2912+ var unsortedTxs []* bchutil.Tx
2913+
2914+ // Let's first add any txs (excluding the coinbase) into unsortedTxs
2915+ for _ , tx := range block .Transactions [1 :] {
2916+ unsortedTxs = append (unsortedTxs , bchutil .NewTx (tx ))
2917+ }
2918+
2919+ // Create one tx paying a p2sh output
2920+ tx1 := createSpendTx (outs [0 ], 0 )
2921+ builder := txscript .NewScriptBuilder ().
2922+ AddOp (txscript .OP_1 ).
2923+ AddOp (txscript .OP_ADD ).
2924+ AddInt64 (1152921504606846976 ).
2925+ AddOp (txscript .OP_EQUAL )
2926+ redeemScript , err := builder .Script ()
2927+ if err != nil {
2928+ panic (err )
2929+ }
2930+
2931+ addr , err := bchutil .NewAddressScriptHash (redeemScript , & chaincfg .RegressionNetParams )
2932+ if err != nil {
2933+ panic (err )
2934+ }
2935+ script , err := txscript .PayToAddrScript (addr )
2936+ if err != nil {
2937+ panic (err )
2938+ }
2939+ tx1 .TxOut [0 ].PkScript = script
2940+
2941+ block .AddTransaction (tx1 )
2942+ unsortedTxs = append (unsortedTxs , bchutil .NewTx (tx1 ))
2943+
2944+ // Spend from tx1
2945+ so1 := & spendableOut {
2946+ amount : bchutil .Amount (tx1 .TxOut [0 ].Value ),
2947+ prevOut : wire.OutPoint {
2948+ Hash : tx1 .TxHash (),
2949+ Index : 0 ,
2950+ },
2951+ }
2952+
2953+ tx2 := createSpendTx (so1 , 0 )
2954+ scriptSig , err := txscript .NewScriptBuilder ().
2955+ AddInt64 (1152921504606846975 ).
2956+ AddData (redeemScript ).Script ()
2957+ if err != nil {
2958+ panic (err )
2959+ }
2960+ tx2 .TxIn [0 ].SignatureScript = scriptSig
2961+ block .AddTransaction (tx2 )
2962+ unsortedTxs = append (unsortedTxs , bchutil .NewTx (tx2 ))
2963+
2964+ // Sort the unsortedTxs slice
2965+ sort .Sort (mining .TxSorter (unsortedTxs ))
2966+ // Set block.Transactions to only the coinbase
2967+ block .Transactions = block .Transactions [:1 ]
2968+
2969+ // Add each tx from our (now sorted) slice
2970+ for _ , tx := range unsortedTxs {
2971+ block .Transactions = append (block .Transactions , tx .MsgTx ())
2972+ }
2973+ }
2974+
2975+ addIntrospection := func (block * wire.MsgBlock ) {
2976+ // unsortedTxs is a slice of bchutil.Tx's that we will sort later using CTOR.
2977+ var unsortedTxs []* bchutil.Tx
2978+
2979+ // Let's first add any txs (excluding the coinbase) into unsortedTxs
2980+ for _ , tx := range block .Transactions [1 :] {
2981+ unsortedTxs = append (unsortedTxs , bchutil .NewTx (tx ))
2982+ }
2983+
2984+ // Create one tx paying a p2sh output
2985+ tx1 := createSpendTx (outs [1 ], 0 )
2986+ builder := txscript .NewScriptBuilder ().
2987+ AddData (make ([]byte , 20 )).
2988+ AddOp (txscript .OP_DROP ).
2989+ AddOp (txscript .OP_ACTIVEBYTECODE ).
2990+ AddOp (txscript .OP_EQUAL )
2991+ redeemScript , err := builder .Script ()
2992+ if err != nil {
2993+ panic (err )
2994+ }
2995+
2996+ addr , err := bchutil .NewAddressScriptHash (redeemScript , & chaincfg .RegressionNetParams )
2997+ if err != nil {
2998+ panic (err )
2999+ }
3000+ script , err := txscript .PayToAddrScript (addr )
3001+ if err != nil {
3002+ panic (err )
3003+ }
3004+ tx1 .TxOut [0 ].PkScript = script
3005+
3006+ block .AddTransaction (tx1 )
3007+ unsortedTxs = append (unsortedTxs , bchutil .NewTx (tx1 ))
3008+
3009+ // Spend from tx1
3010+ so1 := & spendableOut {
3011+ amount : bchutil .Amount (tx1 .TxOut [0 ].Value ),
3012+ prevOut : wire.OutPoint {
3013+ Hash : tx1 .TxHash (),
3014+ Index : 0 ,
3015+ },
3016+ }
3017+
3018+ tx2 := createSpendTx (so1 , 0 )
3019+ scriptSig , err := txscript .NewScriptBuilder ().
3020+ AddData (redeemScript ).
3021+ AddData (redeemScript ).Script ()
3022+ if err != nil {
3023+ panic (err )
3024+ }
3025+ tx2 .TxIn [0 ].SignatureScript = scriptSig
3026+ block .AddTransaction (tx2 )
3027+ unsortedTxs = append (unsortedTxs , bchutil .NewTx (tx2 ))
3028+
3029+ // Sort the unsortedTxs slice
3030+ sort .Sort (mining .TxSorter (unsortedTxs ))
3031+ // Set block.Transactions to only the coinbase
3032+ block .Transactions = block .Transactions [:1 ]
3033+
3034+ // Add each tx from our (now sorted) slice
3035+ for _ , tx := range unsortedTxs {
3036+ block .Transactions = append (block .Transactions , tx .MsgTx ())
3037+ }
3038+ }
3039+
3040+ g .nextBlock ("b0" , nil , add64BitInteger )
3041+ tests = append (tests , []TestInstance {
3042+ acceptBlock (g .tipName , g .tip , true , false ),
3043+ })
3044+ g .nextBlock ("b1" , nil , addIntrospection )
3045+ tests = append (tests , []TestInstance {
3046+ acceptBlock (g .tipName , g .tip , true , false ),
3047+ })
3048+ return tests , nil
3049+ }
0 commit comments