gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/explorer/update_test.go (about)

     1  package explorer
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gitlab.com/SiaPrime/SiaPrime/modules"
     7  	"gitlab.com/SiaPrime/SiaPrime/types"
     8  )
     9  
    10  func (et *explorerTester) currentFacts() (facts modules.BlockFacts, exists bool) {
    11  	var height types.BlockHeight
    12  	err := et.explorer.db.View(dbGetInternal(internalBlockHeight, &height))
    13  	if err != nil {
    14  		exists = false
    15  		return
    16  	}
    17  	return et.explorer.BlockFacts(height)
    18  }
    19  
    20  // TestIntegrationExplorerFileContractMetrics checks that the siacoin
    21  // transfer volume metric is working correctly.
    22  func TestIntegrationExplorerFileContractMetrics(t *testing.T) {
    23  	if testing.Short() {
    24  		t.Skip()
    25  	}
    26  
    27  	et, err := createExplorerTester(t.Name())
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	// Propel explorer tester past the hardfork height.
    32  	for i := 0; i < 10; i++ {
    33  		_, err = et.miner.AddBlock()
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  	}
    38  	facts, ok := et.currentFacts()
    39  	if !ok {
    40  		t.Fatal("couldn't get current facts")
    41  	}
    42  	if !facts.ActiveContractCost.IsZero() {
    43  		t.Error("fresh explorer has nonzero active contract cost")
    44  	}
    45  	if facts.ActiveContractCount != 0 {
    46  		t.Error("active contract count should initialize to zero")
    47  	}
    48  	if !facts.ActiveContractSize.IsZero() {
    49  		t.Error("active contract size should initialize to zero")
    50  	}
    51  
    52  	// Put a file contract into the chain, and check that the explorer
    53  	// correctly does all of the counting.
    54  	builder, err := et.wallet.StartTransaction()
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	builder.FundSiacoins(types.NewCurrency64(5e9))
    59  	fcOutputs := []types.SiacoinOutput{{Value: types.NewCurrency64(4805e6)}}
    60  	fc := types.FileContract{
    61  		FileSize:           5e3,
    62  		WindowStart:        et.cs.Height() + 2,
    63  		WindowEnd:          et.cs.Height() + 3,
    64  		Payout:             types.NewCurrency64(5e9),
    65  		ValidProofOutputs:  fcOutputs,
    66  		MissedProofOutputs: fcOutputs,
    67  	}
    68  	_ = builder.AddFileContract(fc)
    69  	txns, err := builder.Sign(true)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	err = et.tpool.AcceptTransactionSet(txns)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	_, err = et.miner.AddBlock()
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	// Check that the stats have updated to represent the file contract.
    83  	facts, ok = et.currentFacts()
    84  	if !ok {
    85  		t.Fatal("couldn't get current facts")
    86  	}
    87  	if !facts.ActiveContractCost.Equals64(5e9) {
    88  		t.Error("active resources providing wrong file contract cost")
    89  	}
    90  	if facts.ActiveContractCount != 1 {
    91  		t.Error("active contract count does not read correctly")
    92  	}
    93  	if !facts.ActiveContractSize.Equals64(5e3) {
    94  		t.Error("active contract size is not correctly reported")
    95  	}
    96  	if !facts.TotalContractCost.Equals64(5e9) {
    97  		t.Error("total cost is not tallied correctly")
    98  	}
    99  	if facts.FileContractCount != 1 {
   100  		t.Error("total contract count is not accurate")
   101  	}
   102  	if !facts.TotalContractSize.Equals64(5e3) {
   103  		t.Error("total contract size is not accurate")
   104  	}
   105  
   106  	// Put a second file into the explorer to check that multiple files are
   107  	// handled well.
   108  	builder, err = et.wallet.StartTransaction()
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	builder.FundSiacoins(types.NewCurrency64(1e9))
   113  	fcOutputs = []types.SiacoinOutput{{Value: types.NewCurrency64(961e6)}}
   114  	fc = types.FileContract{
   115  		FileSize:           15e3,
   116  		WindowStart:        et.cs.Height() + 2,
   117  		WindowEnd:          et.cs.Height() + 3,
   118  		Payout:             types.NewCurrency64(1e9),
   119  		ValidProofOutputs:  fcOutputs,
   120  		MissedProofOutputs: fcOutputs,
   121  	}
   122  	_ = builder.AddFileContract(fc)
   123  	txns, err = builder.Sign(true)
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	err = et.tpool.AcceptTransactionSet(txns)
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  	_, err = et.miner.AddBlock()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  
   136  	// Check that the stats have updated to represent the file contracts.
   137  	facts, ok = et.currentFacts()
   138  	if !ok {
   139  		t.Fatal("couldn't get current facts")
   140  	}
   141  	if !facts.ActiveContractCost.Equals64(6e9) {
   142  		t.Error("active resources providing wrong file contract cost")
   143  	}
   144  	if facts.ActiveContractCount != 2 {
   145  		t.Error("active contract count does not read correctly")
   146  	}
   147  	if !facts.ActiveContractSize.Equals64(20e3) {
   148  		t.Error("active contract size is not correctly reported")
   149  	}
   150  	if !facts.TotalContractCost.Equals64(6e9) {
   151  		t.Error("total cost is not tallied correctly")
   152  	}
   153  	if facts.FileContractCount != 2 {
   154  		t.Error("total contract count is not accurate")
   155  	}
   156  	if !facts.TotalContractSize.Equals64(20e3) {
   157  		t.Error("total contract size is not accurate")
   158  	}
   159  
   160  	// Expire the first file contract but not the second.
   161  	_, err = et.miner.AddBlock()
   162  	if err != nil {
   163  		t.Fatal(err)
   164  	}
   165  
   166  	// Check that the stats have updated to reflect the expired file contract.
   167  	facts, ok = et.currentFacts()
   168  	if !ok {
   169  		t.Fatal("couldn't get current facts")
   170  	}
   171  	if !facts.ActiveContractCost.Equals64(1e9) {
   172  		t.Error("active resources providing wrong file contract cost", facts.ActiveContractCost)
   173  	}
   174  	if facts.ActiveContractCount != 1 {
   175  		t.Error("active contract count does not read correctly")
   176  	}
   177  	if !facts.ActiveContractSize.Equals64(15e3) {
   178  		t.Error("active contract size is not correctly reported")
   179  	}
   180  	if !facts.TotalContractCost.Equals64(6e9) {
   181  		t.Error("total cost is not tallied correctly")
   182  	}
   183  	if facts.FileContractCount != 2 {
   184  		t.Error("total contract count is not accurate")
   185  	}
   186  	if !facts.TotalContractSize.Equals64(20e3) {
   187  		t.Error("total contract size is not accurate")
   188  	}
   189  
   190  	// Reorg the block explorer to a blank state, see that all of the file
   191  	// contract statistics got removed.
   192  
   193  	// TODO: broken by new block facts model
   194  
   195  	// err = et.reorgToBlank()
   196  	// if err != nil {
   197  	// 	t.Fatal(err)
   198  	// }
   199  	// facts, ok = et.currentFacts()
   200  	// if !ok {
   201  	// 	t.Fatal("couldn't get current facts")
   202  	// }
   203  	// if !facts.ActiveContractCost.IsZero() {
   204  	// 	t.Error("post reorg active contract cost should be zero, got", facts.ActiveContractCost)
   205  	// }
   206  	// if facts.ActiveContractCount != 0 {
   207  	// 	t.Error("post reorg active contract count should be zero, got", facts.ActiveContractCount)
   208  	// }
   209  	// if !facts.TotalContractCost.IsZero() {
   210  	// 	t.Error("post reorg total contract cost should be zero, got", facts.TotalContractCost)
   211  	// }
   212  	// if facts.FileContractCount != 0 {
   213  	// 	t.Error("post reorg file contract count should be zero, got", facts.FileContractCount)
   214  	// }
   215  }