gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/skynetfees_test.go (about)

     1  package renter
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"gitlab.com/NebulousLabs/fastrand"
    11  	"gitlab.com/SkynetLabs/skyd/build"
    12  	"go.sia.tech/siad/persist"
    13  	"go.sia.tech/siad/types"
    14  )
    15  
    16  // TestSpendingHistory tests the spending history persistence.
    17  func TestSpendingHistory(t *testing.T) {
    18  	if testing.Short() {
    19  		t.SkipNow()
    20  	}
    21  	t.Parallel()
    22  
    23  	testDir := build.TempDir("renter", t.Name())
    24  	fileName := "test"
    25  
    26  	spending1 := types.NewCurrency64(1)
    27  	txn1 := []types.Transaction{{ArbitraryData: [][]byte{fastrand.Bytes(100)}}}
    28  	time1 := time.Time{}.Add(time.Nanosecond) // a long time ago
    29  
    30  	// Create a new history.
    31  	sh, err := NewSpendingHistory(testDir, fileName)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	// Spending should be zero.
    36  	lastSpending, lastTime := sh.LastSpending()
    37  	if !lastSpending.IsZero() {
    38  		t.Fatal("spending should be zero.")
    39  	}
    40  	if !lastTime.IsZero() {
    41  		t.Fatal("bh should be zero")
    42  	}
    43  	// Add some spending
    44  	err = sh.AddSpending(spending1, txn1, time1)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	// Spending should match now.
    49  	lastSpending, lastTime = sh.LastSpending()
    50  	if !lastSpending.Equals(spending1) {
    51  		t.Fatal("wrong spending", lastSpending, spending1)
    52  	}
    53  	if lastTime != time1 {
    54  		t.Fatal("wrong time", lastTime, time1)
    55  	}
    56  	// Close history.
    57  	if err := sh.Close(); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	// Open the file directly and make sure the content matches the
    62  	// expectations.
    63  	aop, r, err := persist.NewAppendOnlyPersist(testDir, fileName, spendingHistoryMDHeader, persist.MetadataVersionv156)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	content, err := ioutil.ReadAll(r)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if err := aop.Close(); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	var entry spendingEntry
    75  	err = json.Unmarshal(content, &entry)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	if entry.Time != time1 {
    80  		t.Fatal("wrong time", entry.Time)
    81  	}
    82  	if !reflect.DeepEqual(entry.Txn, txn1) {
    83  		t.Fatal("wrong txn", entry.Txn)
    84  	}
    85  	if !entry.Value.Equals(spending1) {
    86  		t.Fatal("wrong spending", entry.Value)
    87  	}
    88  
    89  	// Reopen it.
    90  	sh, err = NewSpendingHistory(testDir, fileName)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	// Spending should still match.
    95  	lastSpending, lastTime = sh.LastSpending()
    96  	if !lastSpending.Equals(spending1) {
    97  		t.Fatal("wrong spending", lastSpending, spending1)
    98  	}
    99  	if lastTime != time1 {
   100  		t.Fatal("wrong time", lastTime, time1)
   101  	}
   102  	// Close history.
   103  	if err := sh.Close(); err != nil {
   104  		t.Fatal(err)
   105  	}
   106  }