From 5f508f9278e53e1223d9fa3815d027d0bf877c54 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 16 Mar 2022 18:15:06 +0800 Subject: [PATCH] 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. --- channeldb/channel.go | 40 +++++++++++++++++---------------------- channeldb/channel_test.go | 8 ++++---- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index b17e398a8..b07843563 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -2608,22 +2608,24 @@ func (c *OpenChannel) RemoveFwdPkgs(heights ...uint64) error { }, func() {}) } -// RevocationLogTail returns the "tail", or the end of the current revocation -// log. This entry represents the last previous state for the remote node's -// commitment chain. The ChannelDelta returned by this method will always lag -// one state behind the most current (unrevoked) state of the remote node's -// commitment chain. -func (c *OpenChannel) RevocationLogTail() (*ChannelCommitment, error) { +// revocationLogTailCommitHeight returns the commit height at the end of the +// revocation log. This entry represents the last previous state for the remote +// node's commitment chain. The ChannelDelta returned by this method will +// always lag one state behind the most current (unrevoked) state of the remote +// node's commitment chain. +// NOTE: used in unit test only. +func (c *OpenChannel) revocationLogTailCommitHeight() (uint64, error) { c.RLock() defer c.RUnlock() + var height uint64 + // 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. 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 { chanBucket, err := fetchChanBucket( 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 - // this channel, we'll jump to the _last_ key in bucket. As we - // store the update number on disk in a big-endian format, - // this will retrieve the latest entry. + // this channel, we'll jump to the _last_ key in bucket. Since + // the key is the commit height, we'll decode the bytes and + // return it. cursor := logBucket.ReadCursor() - _, tailLogEntry := cursor.Last() - logEntryReader := bytes.NewReader(tailLogEntry) - - // 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 - } + rawHeight, _ := cursor.Last() + height = byteOrder.Uint64(rawHeight) return nil }, func() {}); err != nil { - return nil, err + return height, err } - return &commit, nil + return height, nil } // CommitmentHeight returns the current commitment height. The commitment diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index b74d125fb..85ecd9eb6 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -791,11 +791,11 @@ func TestChannelStateTransition(t *testing.T) { // The state number recovered from the tail of the revocation log // should be identical to this current state. - logTail, err := channel.RevocationLogTail() + logTailHeight, err := channel.revocationLogTailCommitHeight() if err != nil { t.Fatalf("unable to retrieve log: %v", err) } - if logTail.CommitHeight != oldRemoteCommit.CommitHeight { + if logTailHeight != oldRemoteCommit.CommitHeight { 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 // log should be identical to this current state. - logTail, err = channel.RevocationLogTail() + logTailHeight, err = channel.revocationLogTailCommitHeight() if err != nil { t.Fatalf("unable to retrieve log: %v", err) } - if logTail.CommitHeight != oldRemoteCommit.CommitHeight { + if logTailHeight != oldRemoteCommit.CommitHeight { t.Fatal("update number doesn't match") }