-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad140d2
commit 592825b
Showing
5 changed files
with
292 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) 2024 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package udb | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"decred.org/dcrwallet/v4/wallet/walletdb" | ||
"github.com/decred/dcrd/chaincfg/chainhash" | ||
"github.com/decred/dcrd/wire" | ||
) | ||
|
||
var r = rand.New(rand.NewSource(time.Now().Unix())) | ||
|
||
func TestSetBirthday(t *testing.T) { | ||
var numTxn = 5000 | ||
if testing.Short() { | ||
numTxn = 500 | ||
} | ||
ctx := context.Background() | ||
db, _, txStore, _, teardown, err := cloneDB(ctx, "mgr_watching_only.kv") | ||
defer teardown() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
now := time.Now() | ||
middleTime := now.Add(time.Duration(numTxn) * time.Minute * 5) | ||
middleTimeHeaderHeight := 0 | ||
|
||
headers := make([]*wire.BlockHeader, numTxn) | ||
hashToHeight := make(map[chainhash.Hash]int) | ||
headerTime := now | ||
for i := range headers { | ||
h := wire.BlockHeader{ | ||
Height: uint32(i), | ||
Timestamp: headerTime, | ||
} | ||
headers[i] = &h | ||
if headerTime.Before(middleTime) { | ||
middleTimeHeaderHeight = i | ||
} | ||
hashToHeight[h.BlockHash()] = i | ||
headerTime = headerTime.Add(time.Duration(r.Intn(20))*time.Minute + time.Minute*5) | ||
} | ||
|
||
err = walletdb.Update(ctx, db, func(dbtx walletdb.ReadWriteTx) error { | ||
ns := dbtx.ReadWriteBucket(wtxmgrBucketKey) | ||
|
||
for i, header := range headers { | ||
var rbh RawBlockHeader | ||
buf := bytes.NewBuffer(rbh[:0]) | ||
err = header.Serialize(buf) | ||
if err != nil { | ||
return err | ||
} | ||
h := header.BlockHash() | ||
val := valueBlockRecordEmptyFromHeader(h[:], rbh[:]) | ||
err = putRawBlockRecord(ns, keyBlockRecord(int32(i)), val[:]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
tipHeader := headers[len(headers)-1] | ||
tipHash := tipHeader.BlockHash() | ||
var rbh RawBlockHeader | ||
buf := bytes.NewBuffer(rbh[:0]) | ||
err = tipHeader.Serialize(buf) | ||
if err != nil { | ||
return err | ||
} | ||
if err := putRawBlockHeader(ns, tipHash[:], rbh[:]); err != nil { | ||
return err | ||
} | ||
return ns.Put(rootTipBlock, tipHash[:]) | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
birthday time.Time | ||
wantHeight int | ||
}{{ | ||
name: "ok somewhere in the middle", | ||
birthday: middleTime, | ||
wantHeight: middleTimeHeaderHeight, | ||
}, { | ||
name: "ok after best block time", | ||
birthday: headerTime.Add(time.Hour * 48), | ||
wantHeight: numTxn - 1, | ||
}, { | ||
name: "ok before genesis", | ||
birthday: now.Add(time.Minute * -5), | ||
wantHeight: 0, | ||
}} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err = walletdb.Update(ctx, db, func(dbtx walletdb.ReadWriteTx) error { | ||
_, err := txStore.SetBirthday(dbtx, &test.birthday) | ||
return err | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var h *chainhash.Hash | ||
err = walletdb.View(ctx, db, func(dbtx walletdb.ReadTx) error { | ||
h, _ = txStore.Birthday(dbtx) | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
height := hashToHeight[*h] | ||
if height != test.wantHeight { | ||
t.Fatalf("birthday at height %d not at expected height %d", | ||
height, test.wantHeight) | ||
} | ||
}) | ||
} | ||
|
||
} |