Last active
February 8, 2023 16:17
-
-
Save yyforyongyu/fc82b46314d3ee7128dd8312182de785 to your computer and use it in GitHub Desktop.
A test inside `channeldb/channel_test.go`
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
func TestBatchUpdate(t *testing.T) { | |
t.Parallel() | |
fullDB, err := MakeTestDB(t, OptionStoreFinalHtlcResolutions(true)) | |
require.NoError(t, err, "unable to make test database") | |
cdb := fullDB.ChannelStateDB() | |
chanID := lnwire.ShortChannelID{ | |
BlockHeight: 1, | |
TxIndex: 2, | |
TxPosition: 3, | |
} | |
const ( | |
modulus = 10001 | |
num = 100_000 | |
) | |
var ( | |
counter atomic.Uint32 | |
numErr atomic.Uint32 | |
) | |
addHtlc := func(htlcID uint64) error { | |
return kvdb.Batch(cdb.backend, func(tx kvdb.RwTx) error { | |
counter.Add(1) | |
bucket, err := fetchFinalHtlcsBucketRw( | |
tx, chanID, | |
) | |
if err != nil { | |
return err | |
} | |
if htlcID%modulus == 0 { | |
numErr.Add(1) | |
return fmt.Errorf("error from %d", htlcID) | |
} | |
return putFinalHtlc(bucket, htlcID, FinalHtlcInfo{ | |
Settled: true, | |
Offchain: true, | |
}) | |
}) | |
} | |
errChan := make(chan error, num) | |
for i := uint64(0); i < num; i++ { | |
go func(i uint64) { | |
errChan <- addHtlc(i) | |
}(i) | |
} | |
for i := uint64(0); i < num; i++ { | |
select { | |
case err := <-errChan: | |
if err != nil { | |
// t.Log(err) | |
} | |
} | |
} | |
for i := uint64(0); i < num; i++ { | |
info, err := cdb.LookupFinalHtlc(chanID, i) | |
if i%modulus == 0 { | |
// Test unknown htlc lookup for existing channel. | |
require.ErrorIs(t, err, ErrHtlcUnknown) | |
} else { | |
require.NoError(t, err) | |
require.True(t, info.Settled) | |
require.True(t, info.Offchain) | |
} | |
} | |
t.Log("stats from NOT using updateErr") | |
t.Log("total records:", num) | |
t.Log("total errors:", num/modulus+1) | |
t.Log("batch updates:", counter.Load()) | |
t.Log("updates with error:", numErr.Load()) | |
} | |
func TestBatchUpdateErr(t *testing.T) { | |
t.Parallel() | |
fullDB, err := MakeTestDB(t, OptionStoreFinalHtlcResolutions(true)) | |
require.NoError(t, err, "unable to make test database") | |
cdb := fullDB.ChannelStateDB() | |
chanID := lnwire.ShortChannelID{ | |
BlockHeight: 1, | |
TxIndex: 2, | |
TxPosition: 3, | |
} | |
const ( | |
modulus = 10001 | |
num = 100_000 | |
) | |
var ( | |
counter atomic.Uint32 | |
numErr atomic.Uint32 | |
) | |
addHtlc := func(htlcID uint64) error { | |
var updateErr error | |
err := kvdb.Batch(cdb.backend, func(tx kvdb.RwTx) error { | |
counter.Add(1) | |
bucket, err := fetchFinalHtlcsBucketRw( | |
tx, chanID, | |
) | |
if err != nil { | |
return err | |
} | |
if htlcID%modulus == 0 { | |
numErr.Add(1) | |
updateErr = fmt.Errorf("error from %d", htlcID) | |
return nil | |
} | |
return putFinalHtlc(bucket, htlcID, FinalHtlcInfo{ | |
Settled: true, | |
Offchain: true, | |
}) | |
}) | |
if err != nil { | |
return err | |
} | |
return updateErr | |
} | |
errChan := make(chan error, num) | |
for i := uint64(0); i < num; i++ { | |
go func(i uint64) { | |
errChan <- addHtlc(i) | |
}(i) | |
} | |
for i := uint64(0); i < num; i++ { | |
select { | |
case err := <-errChan: | |
if err != nil { | |
// t.Log(err) | |
} | |
} | |
} | |
for i := uint64(0); i < num; i++ { | |
info, err := cdb.LookupFinalHtlc(chanID, i) | |
if i%modulus == 0 { | |
// Test unknown htlc lookup for existing channel. | |
require.ErrorIs(t, err, ErrHtlcUnknown) | |
} else { | |
require.NoError(t, err) | |
require.True(t, info.Settled) | |
require.True(t, info.Offchain) | |
} | |
} | |
t.Log("stats from using updateErr") | |
t.Log("total records:", num) | |
t.Log("total errors:", num/modulus+1) | |
t.Log("batch updates:", counter.Load()) | |
t.Log("updates with error:", numErr.Load()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test result,