lnwallet: calculate channel's total satoshis sent and received

When HTLCs are settled, the channel’s TotalSatoshisSent and
TotalSatoshisReceived fields are updated.
This commit is contained in:
bryanvu 2016-11-17 14:39:38 -08:00 committed by Olaoluwa Osuntokun
parent 61f0d87138
commit faf9daddf6
2 changed files with 47 additions and 4 deletions

@ -960,6 +960,9 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
if entry.EntryType == Add {
continue
}
if entry.EntryType == Settle && !remoteChain {
lc.channelState.TotalSatoshisReceived += uint64(entry.Amount)
}
addEntry := lc.theirLogIndex[entry.ParentIndex].Value.(*PaymentDescriptor)
@ -971,6 +974,9 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
if entry.EntryType == Add {
continue
}
if entry.EntryType == Settle && !remoteChain {
lc.channelState.TotalSatoshisSent += uint64(entry.Amount)
}
addEntry := lc.ourLogIndex[entry.ParentIndex].Value.(*PaymentDescriptor)

@ -475,7 +475,7 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
}
aliceRevocation2, err := aliceChannel.RevokeCurrentCommitment()
if err != nil {
t.Fatalf("alice unable to generate revoation: %v", err)
t.Fatalf("alice unable to generate revocation: %v", err)
}
if err := bobChannel.ReceiveNewCommitment(aliceSig2, bobLogIndex2); err != nil {
t.Fatalf("bob unable to process alice's new commitment: %v", err)
@ -499,11 +499,13 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
"instead can forward %v: %v", len(htlcs), spew.Sdump(htlcs))
}
// At this point, bob should have 6BTC settled, with Alice still having
// 4 BTC. They should also be at a commitment height at two, with the
// revocation window extended by by 1 (5).
// At this point, Bob should have 6 BTC settled, with Alice still having
// 4 BTC. Alice's channel should show 1 BTC sent and Bob's channel should
// show 1 BTC received. They should also be at commitment height two,
// with the revocation window extended by by 1 (5).
aliceSettleBalance := btcutil.Amount(4 * 1e8)
bobSettleBalance := btcutil.Amount(6 * 1e8)
satoshisTransferred := uint64(100000000)
if aliceChannel.channelState.OurBalance != aliceSettleBalance {
t.Fatalf("alice has incorrect local balance %v vs %v",
aliceChannel.channelState.OurBalance, aliceSettleBalance)
@ -520,6 +522,22 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
t.Fatalf("bob has incorrect remote balance %v vs %v",
bobChannel.channelState.TheirBalance, aliceSettleBalance)
}
if aliceChannel.channelState.TotalSatoshisSent != satoshisTransferred {
t.Fatalf("alice satoshis sent incorrect %v vs %v expected",
aliceChannel.channelState.TotalSatoshisSent, satoshisTransferred)
}
if aliceChannel.channelState.TotalSatoshisReceived != 0 {
t.Fatalf("alice satoshis received incorrect %v vs %v expected",
aliceChannel.channelState.TotalSatoshisSent, 0)
}
if bobChannel.channelState.TotalSatoshisReceived != satoshisTransferred {
t.Fatalf("bob satoshis received incorrect %v vs %v expected",
bobChannel.channelState.TotalSatoshisReceived, satoshisTransferred)
}
if bobChannel.channelState.TotalSatoshisSent != 0 {
t.Fatalf("bob satoshis sent incorrect %v vs %v expected",
bobChannel.channelState.TotalSatoshisReceived, 0)
}
if bobChannel.currentHeight != 2 {
t.Fatalf("bob has incorrect commitment height, %v vs %v",
bobChannel.currentHeight, 2)
@ -804,4 +822,23 @@ func TestStateUpdatePersistence(t *testing.T) {
t.Fatalf("expected %v bob balance, got %v", expectedBobBalance,
bobBalance)
}
// The amounts transferred should been updated as per the amounts in
// the HTLCs
if aliceChannelNew.channelState.TotalSatoshisSent != 3000 {
t.Fatalf("expected %v alice satoshis sent, got %v",
3000, aliceChannelNew.channelState.TotalSatoshisSent)
}
if aliceChannelNew.channelState.TotalSatoshisReceived != 1000 {
t.Fatalf("expected %v alice satoshis received, got %v",
1000, aliceChannelNew.channelState.TotalSatoshisReceived)
}
if bobChannelNew.channelState.TotalSatoshisSent != 1000 {
t.Fatalf("expected %v bob satoshis sent, got %v",
1000, bobChannel.channelState.TotalSatoshisSent)
}
if bobChannelNew.channelState.TotalSatoshisReceived != 3000 {
t.Fatalf("expected %v bob satoshis sent, got %v",
3000, bobChannel.channelState.TotalSatoshisSent)
}
}