gitlab.com/jokerrs1/Sia@v1.3.2/node/api/consensus_test.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/NebulousLabs/Sia/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  
    39  // TestConsensusValidateTransactionSet probes the POST call to
    40  // /consensus/validate/transactionset.
    41  func TestConsensusValidateTransactionSet(t *testing.T) {
    42  	if testing.Short() {
    43  		t.SkipNow()
    44  	}
    45  	t.Parallel()
    46  	st, err := createServerTester(t.Name())
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	defer st.server.panicClose()
    51  
    52  	// Get a transaction to validate.
    53  	txnSet, err := st.wallet.SendSiacoins(types.SiacoinPrecision, types.UnlockHash{})
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	jsonTxns, err := json.Marshal(txnSet)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	resp, err := HttpPOST("http://"+st.server.listener.Addr().String()+"/consensus/validate/transactionset", string(jsonTxns))
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	defer resp.Body.Close()
    67  	if non2xx(resp.StatusCode) {
    68  		t.Fatal(decodeError(resp))
    69  	}
    70  
    71  	// Try again with an invalid transaction set
    72  	txnSet = []types.Transaction{{TransactionSignatures: []types.TransactionSignature{{}}}}
    73  	jsonTxns, err = json.Marshal(txnSet)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	resp, err = HttpPOST("http://"+st.server.listener.Addr().String()+"/consensus/validate/transactionset", string(jsonTxns))
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	defer resp.Body.Close()
    82  	if !non2xx(resp.StatusCode) {
    83  		t.Fatal("expected validation error")
    84  	}
    85  }