gitlab.com/SiaPrime/SiaPrime@v1.4.1/node/api/consensus_test.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"gitlab.com/SiaPrime/SiaPrime/types"
     8  )
     9  
    10  // TestConsensusGet probes the GET call to /consensus.
    11  func TestIntegrationConsensusGET(t *testing.T) {
    12  	if testing.Short() {
    13  		t.SkipNow()
    14  	}
    15  	t.Parallel()
    16  	st, err := createServerTester(t.Name())
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer st.server.panicClose()
    21  
    22  	var cg ConsensusGET
    23  	err = st.getAPI("/consensus", &cg)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	if cg.Height != 4 {
    28  		t.Error("wrong height returned in consensus GET call")
    29  	}
    30  	if cg.CurrentBlock != st.server.api.cs.CurrentBlock().ID() {
    31  		t.Error("wrong block returned in consensus GET call")
    32  	}
    33  	expectedTarget := types.Target{128}
    34  	if cg.Target != expectedTarget {
    35  		t.Error("wrong target returned in consensus GET call")
    36  	}
    37  
    38  	if cg.BlockFrequency != types.BlockFrequency {
    39  		t.Error("constant mismatch")
    40  	}
    41  	if cg.SiafundCount.Cmp(types.SiafundCount) != 0 {
    42  		t.Error("constant mismatch")
    43  	}
    44  	if cg.InitialCoinbase != types.InitialCoinbase {
    45  		t.Error("constant mismatch")
    46  	}
    47  }
    48  
    49  // TestConsensusValidateTransactionSet probes the POST call to
    50  // /consensus/validate/transactionset.
    51  func TestConsensusValidateTransactionSet(t *testing.T) {
    52  	if testing.Short() {
    53  		t.SkipNow()
    54  	}
    55  	t.Parallel()
    56  	st, err := createServerTester(t.Name())
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	defer st.server.panicClose()
    61  
    62  	// Get a transaction to validate.
    63  	txnSet, err := st.wallet.SendSiacoins(types.SiacoinPrecision, types.UnlockHash{})
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	jsonTxns, err := json.Marshal(txnSet)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	resp, err := HttpPOST("http://"+st.server.listener.Addr().String()+"/consensus/validate/transactionset", string(jsonTxns))
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	defer resp.Body.Close()
    77  	if non2xx(resp.StatusCode) {
    78  		t.Fatal(decodeError(resp))
    79  	}
    80  
    81  	// Try again with an invalid transaction set
    82  	txnSet = []types.Transaction{{TransactionSignatures: []types.TransactionSignature{{}}}}
    83  	jsonTxns, err = json.Marshal(txnSet)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	resp, err = HttpPOST("http://"+st.server.listener.Addr().String()+"/consensus/validate/transactionset", string(jsonTxns))
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	defer resp.Body.Close()
    92  	if !non2xx(resp.StatusCode) {
    93  		t.Fatal("expected validation error")
    94  	}
    95  }