channeldb: replace method RevocationLogTail

This commit replaces the method RevocationLogTail which in used in our
unit test with a private method revocationLogTailCommitHeight. The new
method returns the commit height only since that's what's needed in the
test.
This commit is contained in:
yyforyongyu committed 2022-04-20 01:18:11 +08:00
1 parent fcb314bf2c
commit 5f508f9278
2 files changed
+21 -27

No files matched your search

+17 -23
View File
@@ -2608,22 +2608,24 @@ func (c *OpenChannel) RemoveFwdPkgs(heights ...uint64) error {
}, func() {}) }, func() {})
} }
// RevocationLogTail returns the "tail", or the end of the current revocation // revocationLogTailCommitHeight returns the commit height at the end of the
// log. This entry represents the last previous state for the remote node's // revocation log. This entry represents the last previous state for the remote
// commitment chain. The ChannelDelta returned by this method will always lag // node's commitment chain. The ChannelDelta returned by this method will
// one state behind the most current (unrevoked) state of the remote node's // always lag one state behind the most current (unrevoked) state of the remote
// commitment chain. // node's commitment chain.
func (c *OpenChannel) RevocationLogTail() (*ChannelCommitment, error) { // NOTE: used in unit test only.
func (c *OpenChannel) revocationLogTailCommitHeight() (uint64, error) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
var height uint64
// If we haven't created any state updates yet, then we'll exit early as // If we haven't created any state updates yet, then we'll exit early as
// there's nothing to be found on disk in the revocation bucket. // there's nothing to be found on disk in the revocation bucket.
if c.RemoteCommitment.CommitHeight == 0 { if c.RemoteCommitment.CommitHeight == 0 {
return nil, nil return height, nil
} }
var commit ChannelCommitment
if err := kvdb.View(c.Db.backend, func(tx kvdb.RTx) error { if err := kvdb.View(c.Db.backend, func(tx kvdb.RTx) error {
chanBucket, err := fetchChanBucket( chanBucket, err := fetchChanBucket(
tx, c.IdentityPub, &c.FundingOutpoint, c.ChainHash, tx, c.IdentityPub, &c.FundingOutpoint, c.ChainHash,
@@ -2638,27 +2640,19 @@ func (c *OpenChannel) RevocationLogTail() (*ChannelCommitment, error) {
} }
// Once we have the bucket that stores the revocation log from // Once we have the bucket that stores the revocation log from
// this channel, we'll jump to the _last_ key in bucket. As we // this channel, we'll jump to the _last_ key in bucket. Since
// store the update number on disk in a big-endian format, // the key is the commit height, we'll decode the bytes and
// this will retrieve the latest entry. // return it.
cursor := logBucket.ReadCursor() cursor := logBucket.ReadCursor()
_, tailLogEntry := cursor.Last() rawHeight, _ := cursor.Last()
logEntryReader := bytes.NewReader(tailLogEntry) height = byteOrder.Uint64(rawHeight)
// Once we have the entry, we'll decode it into the channel
// delta pointer we created above.
var dbErr error
commit, dbErr = deserializeChanCommit(logEntryReader)
if dbErr != nil {
return dbErr
}
return nil return nil
}, func() {}); err != nil { }, func() {}); err != nil {
return nil, err return height, err
} }
return &commit, nil return height, nil
} }
// CommitmentHeight returns the current commitment height. The commitment // CommitmentHeight returns the current commitment height. The commitment
+4 -4
View File
@@ -791,11 +791,11 @@ func TestChannelStateTransition(t *testing.T) {
// The state number recovered from the tail of the revocation log // The state number recovered from the tail of the revocation log
// should be identical to this current state. // should be identical to this current state.
logTail, err := channel.RevocationLogTail() logTailHeight, err := channel.revocationLogTailCommitHeight()
if err != nil { if err != nil {
t.Fatalf("unable to retrieve log: %v", err) t.Fatalf("unable to retrieve log: %v", err)
} }
if logTail.CommitHeight != oldRemoteCommit.CommitHeight { if logTailHeight != oldRemoteCommit.CommitHeight {
t.Fatal("update number doesn't match") t.Fatal("update number doesn't match")
} }
@@ -827,11 +827,11 @@ func TestChannelStateTransition(t *testing.T) {
// Once again, state number recovered from the tail of the revocation // Once again, state number recovered from the tail of the revocation
// log should be identical to this current state. // log should be identical to this current state.
logTail, err = channel.RevocationLogTail() logTailHeight, err = channel.revocationLogTailCommitHeight()
if err != nil { if err != nil {
t.Fatalf("unable to retrieve log: %v", err) t.Fatalf("unable to retrieve log: %v", err)
} }
if logTail.CommitHeight != oldRemoteCommit.CommitHeight { if logTailHeight != oldRemoteCommit.CommitHeight {
t.Fatal("update number doesn't match") t.Fatal("update number doesn't match")
} }