watchtower/wtserver/server: permit unused session overrwite
This commit is contained in:
parent
9b1bc9fd79
commit
978a0a8de6
@ -61,7 +61,8 @@ func (db *MockDB) InsertSessionInfo(info *SessionInfo) error {
|
|||||||
db.mu.Lock()
|
db.mu.Lock()
|
||||||
defer db.mu.Unlock()
|
defer db.mu.Unlock()
|
||||||
|
|
||||||
if _, ok := db.sessions[info.ID]; ok {
|
dbInfo, ok := db.sessions[info.ID]
|
||||||
|
if ok && dbInfo.LastApplied > 0 {
|
||||||
return ErrSessionAlreadyExists
|
return ErrSessionAlreadyExists
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,11 +21,15 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID,
|
|||||||
existingInfo, err := s.cfg.DB.GetSessionInfo(id)
|
existingInfo, err := s.cfg.DB.GetSessionInfo(id)
|
||||||
switch {
|
switch {
|
||||||
|
|
||||||
|
// We already have a session, though it is currently unused. We'll allow
|
||||||
|
// the client to recommit the session if it wanted to change the policy.
|
||||||
|
case err == nil && existingInfo.LastApplied == 0:
|
||||||
|
|
||||||
// We already have a session corresponding to this session id, return an
|
// We already have a session corresponding to this session id, return an
|
||||||
// error signaling that it already exists in our database. We return the
|
// error signaling that it already exists in our database. We return the
|
||||||
// reward address to the client in case they were not able to process
|
// reward address to the client in case they were not able to process
|
||||||
// our reply earlier.
|
// our reply earlier.
|
||||||
case err == nil:
|
case err == nil && existingInfo.LastApplied > 0:
|
||||||
log.Debugf("Already have session for %s", id)
|
log.Debugf("Already have session for %s", id)
|
||||||
return s.replyCreateSession(
|
return s.replyCreateSession(
|
||||||
peer, id, wtwire.CreateSessionCodeAlreadyExists,
|
peer, id, wtwire.CreateSessionCodeAlreadyExists,
|
||||||
|
@ -29,6 +29,8 @@ var (
|
|||||||
addrScript, _ = txscript.PayToAddrScript(addr)
|
addrScript, _ = txscript.PayToAddrScript(addr)
|
||||||
|
|
||||||
testnetChainHash = *chaincfg.TestNet3Params.GenesisHash
|
testnetChainHash = *chaincfg.TestNet3Params.GenesisHash
|
||||||
|
|
||||||
|
rewardType = (blob.FlagCommitOutputs | blob.FlagReward).Type()
|
||||||
)
|
)
|
||||||
|
|
||||||
// randPubKey generates a new secp keypair, and returns the public key.
|
// randPubKey generates a new secp keypair, and returns the public key.
|
||||||
@ -152,16 +154,17 @@ func TestServerOnlyAcceptOnePeer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type createSessionTestCase struct {
|
type createSessionTestCase struct {
|
||||||
name string
|
name string
|
||||||
initMsg *wtwire.Init
|
initMsg *wtwire.Init
|
||||||
createMsg *wtwire.CreateSession
|
createMsg *wtwire.CreateSession
|
||||||
expReply *wtwire.CreateSessionReply
|
expReply *wtwire.CreateSessionReply
|
||||||
expDupReply *wtwire.CreateSessionReply
|
expDupReply *wtwire.CreateSessionReply
|
||||||
|
sendStateUpdate bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var createSessionTests = []createSessionTestCase{
|
var createSessionTests = []createSessionTestCase{
|
||||||
{
|
{
|
||||||
name: "reject duplicate session create",
|
name: "duplicate session create",
|
||||||
initMsg: wtwire.NewInitMessage(
|
initMsg: wtwire.NewInitMessage(
|
||||||
lnwire.NewRawFeatureVector(),
|
lnwire.NewRawFeatureVector(),
|
||||||
testnetChainHash,
|
testnetChainHash,
|
||||||
@ -173,12 +176,58 @@ var createSessionTests = []createSessionTestCase{
|
|||||||
RewardRate: 0,
|
RewardRate: 0,
|
||||||
SweepFeeRate: 1,
|
SweepFeeRate: 1,
|
||||||
},
|
},
|
||||||
|
expReply: &wtwire.CreateSessionReply{
|
||||||
|
Code: wtwire.CodeOK,
|
||||||
|
Data: []byte{},
|
||||||
|
},
|
||||||
|
expDupReply: &wtwire.CreateSessionReply{
|
||||||
|
Code: wtwire.CodeOK,
|
||||||
|
Data: []byte{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "duplicate session create after use",
|
||||||
|
initMsg: wtwire.NewInitMessage(
|
||||||
|
lnwire.NewRawFeatureVector(),
|
||||||
|
testnetChainHash,
|
||||||
|
),
|
||||||
|
createMsg: &wtwire.CreateSession{
|
||||||
|
BlobType: blob.TypeDefault,
|
||||||
|
MaxUpdates: 1000,
|
||||||
|
RewardBase: 0,
|
||||||
|
RewardRate: 0,
|
||||||
|
SweepFeeRate: 1,
|
||||||
|
},
|
||||||
|
expReply: &wtwire.CreateSessionReply{
|
||||||
|
Code: wtwire.CodeOK,
|
||||||
|
Data: []byte{},
|
||||||
|
},
|
||||||
|
expDupReply: &wtwire.CreateSessionReply{
|
||||||
|
Code: wtwire.CreateSessionCodeAlreadyExists,
|
||||||
|
LastApplied: 1,
|
||||||
|
Data: []byte{},
|
||||||
|
},
|
||||||
|
sendStateUpdate: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "duplicate session create reward",
|
||||||
|
initMsg: wtwire.NewInitMessage(
|
||||||
|
lnwire.NewRawFeatureVector(),
|
||||||
|
testnetChainHash,
|
||||||
|
),
|
||||||
|
createMsg: &wtwire.CreateSession{
|
||||||
|
BlobType: rewardType,
|
||||||
|
MaxUpdates: 1000,
|
||||||
|
RewardBase: 0,
|
||||||
|
RewardRate: 0,
|
||||||
|
SweepFeeRate: 1,
|
||||||
|
},
|
||||||
expReply: &wtwire.CreateSessionReply{
|
expReply: &wtwire.CreateSessionReply{
|
||||||
Code: wtwire.CodeOK,
|
Code: wtwire.CodeOK,
|
||||||
Data: addrScript,
|
Data: addrScript,
|
||||||
},
|
},
|
||||||
expDupReply: &wtwire.CreateSessionReply{
|
expDupReply: &wtwire.CreateSessionReply{
|
||||||
Code: wtwire.CreateSessionCodeAlreadyExists,
|
Code: wtwire.CodeOK,
|
||||||
Data: addrScript,
|
Data: addrScript,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -251,6 +300,18 @@ func testServerCreateSession(t *testing.T, i int, test createSessionTestCase) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if test.sendStateUpdate {
|
||||||
|
peer = wtmock.NewMockPeer(localPub, peerPub, nil, 0)
|
||||||
|
connect(t, s, peer, test.initMsg, timeoutDuration)
|
||||||
|
update := &wtwire.StateUpdate{
|
||||||
|
SeqNum: 1,
|
||||||
|
IsComplete: 1,
|
||||||
|
}
|
||||||
|
sendMsg(t, update, peer, timeoutDuration)
|
||||||
|
|
||||||
|
assertConnClosed(t, peer, 2*timeoutDuration)
|
||||||
|
}
|
||||||
|
|
||||||
// Simulate a peer with the same session id connection to the server
|
// Simulate a peer with the same session id connection to the server
|
||||||
// again.
|
// again.
|
||||||
peer = wtmock.NewMockPeer(localPub, peerPub, nil, 0)
|
peer = wtmock.NewMockPeer(localPub, peerPub, nil, 0)
|
||||||
@ -705,7 +766,7 @@ func TestServerDeleteSession(t *testing.T) {
|
|||||||
send: createSession,
|
send: createSession,
|
||||||
recv: &wtwire.CreateSessionReply{
|
recv: &wtwire.CreateSessionReply{
|
||||||
Code: wtwire.CodeOK,
|
Code: wtwire.CodeOK,
|
||||||
Data: addrScript,
|
Data: []byte{},
|
||||||
},
|
},
|
||||||
assert: func(t *testing.T) {
|
assert: func(t *testing.T) {
|
||||||
// Both peers should have sessions.
|
// Both peers should have sessions.
|
||||||
|
Loading…
Reference in New Issue
Block a user