@@ -25,7 +25,7 @@ func createChannelAccountFixture(t *testing.T, ctx context.Context, dbConnection
25
25
require .NoError (t , err )
26
26
}
27
27
28
- func TestChannelAccountModelGetIdleChannelAccount (t * testing.T ) {
28
+ func TestChannelAccountModelGetAndLockIdleChannelAccount (t * testing.T ) {
29
29
dbt := dbtest .Open (t )
30
30
defer dbt .Close ()
31
31
@@ -46,14 +46,15 @@ func TestChannelAccountModelGetIdleChannelAccount(t *testing.T) {
46
46
channel_accounts
47
47
SET
48
48
locked_at = NOW(),
49
- locked_until = NOW() + '5 minutes'::INTERVAL
49
+ locked_until = NOW() + '5 minutes'::INTERVAL,
50
+ locked_tx_hash = 'hash'
50
51
WHERE
51
52
public_key = ANY($1)
52
53
`
53
54
_ , err := dbConnectionPool .ExecContext (ctx , lockChannelAccountQuery , pq .Array ([]string {channelAccount1 .Address (), channelAccount2 .Address ()}))
54
55
require .NoError (t , err )
55
56
56
- ca , err := m .GetIdleChannelAccount (ctx , time .Minute )
57
+ ca , err := m .GetAndLockIdleChannelAccount (ctx , time .Minute )
57
58
assert .ErrorIs (t , err , ErrNoIdleChannelAccountAvailable )
58
59
assert .Nil (t , ca )
59
60
})
@@ -64,18 +65,19 @@ func TestChannelAccountModelGetIdleChannelAccount(t *testing.T) {
64
65
createChannelAccountFixture (t , ctx , dbConnectionPool , ChannelAccount {PublicKey : channelAccount1 .Address (), EncryptedPrivateKey : channelAccount1 .Seed ()}, ChannelAccount {PublicKey : channelAccount2 .Address (), EncryptedPrivateKey : channelAccount2 .Seed ()})
65
66
66
67
const lockChannelAccountQuery = `
67
- UPDATE
68
- channel_accounts
69
- SET
70
- locked_at = NOW(),
71
- locked_until = NOW() + '5 minutes'::INTERVAL
72
- WHERE
73
- public_key = $1
74
- `
68
+ UPDATE
69
+ channel_accounts
70
+ SET
71
+ locked_at = NOW(),
72
+ locked_until = NOW() + '5 minutes'::INTERVAL,
73
+ locked_tx_hash = 'hash'
74
+ WHERE
75
+ public_key = $1
76
+ `
75
77
_ , err := dbConnectionPool .ExecContext (ctx , lockChannelAccountQuery , channelAccount1 .Address ())
76
78
require .NoError (t , err )
77
79
78
- ca , err := m .GetIdleChannelAccount (ctx , time .Minute )
80
+ ca , err := m .GetAndLockIdleChannelAccount (ctx , time .Minute )
79
81
require .NoError (t , err )
80
82
assert .Equal (t , ca .PublicKey , channelAccount2 .Address ())
81
83
assert .Equal (t , ca .EncryptedPrivateKey , channelAccount2 .Seed ())
@@ -128,6 +130,56 @@ func TestChannelAccountModelGetAllByPublicKey(t *testing.T) {
128
130
assert .Equal (t , channelAccount2 .Address (), channelAccounts [1 ].PublicKey )
129
131
}
130
132
133
+ func TestAssignTxToChannelAccount (t * testing.T ) {
134
+ dbt := dbtest .Open (t )
135
+ defer dbt .Close ()
136
+
137
+ dbConnectionPool , err := db .OpenDBConnectionPool (dbt .DSN )
138
+ require .NoError (t , err )
139
+ defer dbConnectionPool .Close ()
140
+
141
+ ctx := context .Background ()
142
+ m := NewChannelAccountModel (dbConnectionPool )
143
+
144
+ channelAccount := keypair .MustRandom ()
145
+ createChannelAccountFixture (t , ctx , dbConnectionPool , ChannelAccount {PublicKey : channelAccount .Address (), EncryptedPrivateKey : channelAccount .Seed ()})
146
+
147
+ err = m .AssignTxToChannelAccount (ctx , channelAccount .Address (), "txhash" )
148
+ assert .NoError (t , err )
149
+ channelAccountFromDB , err := m .Get (ctx , dbConnectionPool , channelAccount .Address ())
150
+ assert .NoError (t , err )
151
+ assert .Equal (t , "txhash" , channelAccountFromDB .LockedTxHash .String )
152
+
153
+ }
154
+
155
+ func TestUnlockChannelAccountFromTx (t * testing.T ) {
156
+ dbt := dbtest .Open (t )
157
+ defer dbt .Close ()
158
+
159
+ dbConnectionPool , err := db .OpenDBConnectionPool (dbt .DSN )
160
+ require .NoError (t , err )
161
+ defer dbConnectionPool .Close ()
162
+
163
+ ctx := context .Background ()
164
+ m := NewChannelAccountModel (dbConnectionPool )
165
+
166
+ channelAccount := keypair .MustRandom ()
167
+ createChannelAccountFixture (t , ctx , dbConnectionPool , ChannelAccount {PublicKey : channelAccount .Address (), EncryptedPrivateKey : channelAccount .Seed ()})
168
+ err = m .AssignTxToChannelAccount (ctx , channelAccount .Address (), "txhash" )
169
+ assert .NoError (t , err )
170
+ channelAccountFromDB , err := m .Get (ctx , dbConnectionPool , channelAccount .Address ())
171
+ assert .NoError (t , err )
172
+ assert .Equal (t , "txhash" , channelAccountFromDB .LockedTxHash .String )
173
+
174
+ err = m .UnassignTxAndUnlockChannelAccount (ctx , "txhash" )
175
+ assert .NoError (t , err )
176
+ channelAccountFromDB , err = m .Get (ctx , dbConnectionPool , channelAccount .Address ())
177
+ assert .NoError (t , err )
178
+ assert .False (t , channelAccountFromDB .LockedTxHash .Valid )
179
+ assert .False (t , channelAccountFromDB .LockedAt .Valid )
180
+ assert .False (t , channelAccountFromDB .LockedUntil .Valid )
181
+ }
182
+
131
183
func TestChannelAccountModelBatchInsert (t * testing.T ) {
132
184
dbt := dbtest .Open (t )
133
185
defer dbt .Close ()
0 commit comments