mirror of
https://github.com/ACINQ/eclair.git
synced 2025-03-27 02:37:06 +01:00
added theirCommit test in clearing state
This commit is contained in:
parent
c143756fc7
commit
c5fa32d24e
3 changed files with 80 additions and 40 deletions
|
@ -0,0 +1,51 @@
|
|||
package fr.acinq.eclair.channel.simulator.states
|
||||
|
||||
import akka.testkit.{TestFSMRef, TestKitBase, TestProbe}
|
||||
import fr.acinq.bitcoin.Crypto
|
||||
import fr.acinq.eclair.channel._
|
||||
import fr.acinq.eclair._
|
||||
import lightning.locktime.Locktime.Blocks
|
||||
import lightning._
|
||||
|
||||
import scala.util.Random
|
||||
|
||||
/**
|
||||
* Created by PM on 23/08/2016.
|
||||
*/
|
||||
trait StateTestsHelperMethods extends TestKitBase {
|
||||
|
||||
def addHtlc(amountMsat: Int, s: TestFSMRef[State, Data, Channel], r: TestFSMRef[State, Data, Channel], s2r: TestProbe, r2s: TestProbe): (rval, update_add_htlc) = {
|
||||
val rand = new Random()
|
||||
val R = rval(rand.nextInt(), rand.nextInt(), rand.nextInt(), rand.nextInt())
|
||||
val H: sha256_hash = Crypto.sha256(R)
|
||||
val sender = TestProbe()
|
||||
sender.send(s, CMD_ADD_HTLC(amountMsat, H, locktime(Blocks(1440))))
|
||||
sender.expectMsg("ok")
|
||||
val htlc = s2r.expectMsgType[update_add_htlc]
|
||||
s2r.forward(r)
|
||||
awaitCond(r.stateData.asInstanceOf[DATA_NORMAL].commitments.theirChanges.proposed.contains(htlc))
|
||||
(R, htlc)
|
||||
}
|
||||
|
||||
def fulfillHtlc(id: Long, R: rval, s: TestFSMRef[State, Data, Channel], r: TestFSMRef[State, Data, Channel], s2r: TestProbe, r2s: TestProbe) = {
|
||||
val sender = TestProbe()
|
||||
sender.send(s, CMD_FULFILL_HTLC(id, R))
|
||||
sender.expectMsg("ok")
|
||||
val fulfill = s2r.expectMsgType[update_fulfill_htlc]
|
||||
s2r.forward(r)
|
||||
awaitCond(r.stateData.asInstanceOf[DATA_NORMAL].commitments.theirChanges.proposed.contains(fulfill))
|
||||
}
|
||||
|
||||
def sign(s: TestFSMRef[State, Data, Channel], r: TestFSMRef[State, Data, Channel], s2r: TestProbe, r2s: TestProbe) = {
|
||||
val sender = TestProbe()
|
||||
val rCommitIndex = r.stateData.asInstanceOf[HasCommitments].commitments.ourCommit.index
|
||||
sender.send(s, CMD_SIGN)
|
||||
sender.expectMsg("ok")
|
||||
s2r.expectMsgType[update_commit]
|
||||
s2r.forward(r)
|
||||
r2s.expectMsgType[update_revocation]
|
||||
r2s.forward(s)
|
||||
awaitCond(r.stateData.asInstanceOf[HasCommitments].commitments.ourCommit.index == rCommitIndex + 1)
|
||||
}
|
||||
|
||||
}
|
|
@ -8,6 +8,7 @@ import fr.acinq.eclair._
|
|||
import fr.acinq.eclair.TestBitcoinClient
|
||||
import fr.acinq.eclair.TestConstants.{Alice, Bob}
|
||||
import fr.acinq.eclair.blockchain._
|
||||
import fr.acinq.eclair.channel.simulator.states.StateTestsHelperMethods
|
||||
import fr.acinq.eclair.channel.{BITCOIN_ANCHOR_DEPTHOK, Data, State, _}
|
||||
import lightning._
|
||||
import lightning.locktime.Locktime.Blocks
|
||||
|
@ -15,14 +16,13 @@ import org.junit.runner.RunWith
|
|||
import org.scalatest.junit.JUnitRunner
|
||||
import org.scalatest.{BeforeAndAfterAll, fixture}
|
||||
|
||||
import scala.util.Random
|
||||
import scala.concurrent.duration._
|
||||
|
||||
/**
|
||||
* Created by PM on 05/07/2016.
|
||||
*/
|
||||
@RunWith(classOf[JUnitRunner])
|
||||
class NormalStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSuiteLike with BeforeAndAfterAll {
|
||||
class NormalStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSuiteLike with BeforeAndAfterAll with StateTestsHelperMethods {
|
||||
|
||||
type FixtureParam = Tuple6[TestFSMRef[State, Data, Channel], TestFSMRef[State, Data, Channel], TestProbe, TestProbe, TestProbe, TestProbe]
|
||||
|
||||
|
@ -71,40 +71,6 @@ class NormalStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSuite
|
|||
TestKit.shutdownActorSystem(system)
|
||||
}
|
||||
|
||||
def addHtlc(amountMsat: Int, s: TestFSMRef[State, Data, Channel], r: TestFSMRef[State, Data, Channel], s2r: TestProbe, r2s: TestProbe): (rval, update_add_htlc) = {
|
||||
val rand = new Random()
|
||||
val R = rval(rand.nextInt(), rand.nextInt(), rand.nextInt(), rand.nextInt())
|
||||
val H: sha256_hash = Crypto.sha256(R)
|
||||
val sender = TestProbe()
|
||||
sender.send(s, CMD_ADD_HTLC(amountMsat, H, locktime(Blocks(1440))))
|
||||
sender.expectMsg("ok")
|
||||
val htlc = s2r.expectMsgType[update_add_htlc]
|
||||
s2r.forward(r)
|
||||
awaitCond(r.stateData.asInstanceOf[DATA_NORMAL].commitments.theirChanges.proposed.contains(htlc))
|
||||
(R, htlc)
|
||||
}
|
||||
|
||||
def fulfillHtlc(id: Long, R: rval, s: TestFSMRef[State, Data, Channel], r: TestFSMRef[State, Data, Channel], s2r: TestProbe, r2s: TestProbe) = {
|
||||
val sender = TestProbe()
|
||||
sender.send(s, CMD_FULFILL_HTLC(id, R))
|
||||
sender.expectMsg("ok")
|
||||
val fulfill = s2r.expectMsgType[update_fulfill_htlc]
|
||||
s2r.forward(r)
|
||||
awaitCond(r.stateData.asInstanceOf[DATA_NORMAL].commitments.theirChanges.proposed.contains(fulfill))
|
||||
}
|
||||
|
||||
def sign(s: TestFSMRef[State, Data, Channel], r: TestFSMRef[State, Data, Channel], s2r: TestProbe, r2s: TestProbe) = {
|
||||
val sender = TestProbe()
|
||||
val rCommitIndex = r.stateData.asInstanceOf[HasCommitments].commitments.ourCommit.index
|
||||
sender.send(s, CMD_SIGN)
|
||||
sender.expectMsg("ok")
|
||||
s2r.expectMsgType[update_commit]
|
||||
s2r.forward(r)
|
||||
r2s.expectMsgType[update_revocation]
|
||||
r2s.forward(s)
|
||||
awaitCond(r.stateData.asInstanceOf[HasCommitments].commitments.ourCommit.index == rCommitIndex + 1)
|
||||
}
|
||||
|
||||
test("recv CMD_ADD_HTLC") { case (alice, _, alice2bob, _, _, _) =>
|
||||
within(30 seconds) {
|
||||
val initialState = alice.stateData.asInstanceOf[DATA_NORMAL]
|
||||
|
|
|
@ -3,10 +3,11 @@ package fr.acinq.eclair.channel.simulator.states.f
|
|||
import akka.actor.ActorSystem
|
||||
import akka.testkit.{TestActorRef, TestFSMRef, TestKit, TestProbe}
|
||||
import com.google.protobuf.ByteString
|
||||
import fr.acinq.bitcoin.Crypto
|
||||
import fr.acinq.bitcoin.{Crypto, Satoshi, ScriptFlags, Transaction}
|
||||
import fr.acinq.eclair.TestConstants.{Alice, Bob}
|
||||
import fr.acinq.eclair.{TestBitcoinClient, _}
|
||||
import fr.acinq.eclair.blockchain._
|
||||
import fr.acinq.eclair.channel.simulator.states.StateTestsHelperMethods
|
||||
import fr.acinq.eclair.channel.{BITCOIN_ANCHOR_DEPTHOK, Data, State, _}
|
||||
import lightning._
|
||||
import lightning.locktime.Locktime.Blocks
|
||||
|
@ -20,7 +21,7 @@ import scala.concurrent.duration._
|
|||
* Created by PM on 05/07/2016.
|
||||
*/
|
||||
@RunWith(classOf[JUnitRunner])
|
||||
class ClearingStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSuiteLike with BeforeAndAfterAll {
|
||||
class ClearingStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSuiteLike with BeforeAndAfterAll with StateTestsHelperMethods {
|
||||
|
||||
type FixtureParam = Tuple6[TestFSMRef[State, Data, Channel], TestFSMRef[State, Data, Channel], TestProbe, TestProbe, TestProbe, TestProbe]
|
||||
|
||||
|
@ -66,8 +67,8 @@ class ClearingStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSui
|
|||
// alice sends an HTLC to bob
|
||||
val r: rval = rval(1, 2, 3, 4)
|
||||
val h: sha256_hash = Crypto.sha256(r)
|
||||
val amount = 500000
|
||||
sender.send(alice, CMD_ADD_HTLC(amount, h, locktime(Blocks(3))))
|
||||
val amount = 300000000
|
||||
sender.send(alice, CMD_ADD_HTLC(amount, h, locktime(Blocks(1440))))
|
||||
sender.expectMsg("ok")
|
||||
val htlc = alice2bob.expectMsgType[update_add_htlc]
|
||||
alice2bob.forward(bob)
|
||||
|
@ -361,6 +362,28 @@ class ClearingStateSpec extends TestKit(ActorSystem("test")) with fixture.FunSui
|
|||
}
|
||||
}
|
||||
|
||||
test("recv BITCOIN_ANCHOR_SPENT (their commit)") { case (alice, bob, alice2bob, bob2alice, alice2blockchain, bob2blockchain) =>
|
||||
within(30 seconds) {
|
||||
val sender = TestProbe()
|
||||
|
||||
// bob publishes his current commit tx, which contains one pending htlc alice->bob
|
||||
val bobCommitTx = bob.stateData.asInstanceOf[DATA_CLEARING].commitments.ourCommit.publishableTx
|
||||
assert(bobCommitTx.txOut.size == 2) // one main outputs (bob has zero) and 1 pending htlc
|
||||
alice ! (BITCOIN_ANCHOR_SPENT, bobCommitTx)
|
||||
|
||||
alice2blockchain.expectMsgType[WatchConfirmed].txId == bobCommitTx.txid
|
||||
val claimHtlcTx = alice2blockchain.expectMsgType[PublishAsap].tx
|
||||
assert(claimHtlcTx.txIn.size == 1)
|
||||
val previousOutputs = Map(claimHtlcTx.txIn(0).outPoint -> bobCommitTx.txOut(claimHtlcTx.txIn(0).outPoint.index.toInt))
|
||||
Transaction.correctlySpends(claimHtlcTx, previousOutputs, ScriptFlags.STANDARD_SCRIPT_VERIFY_FLAGS)
|
||||
assert(claimHtlcTx.txOut.size == 1)
|
||||
assert(claimHtlcTx.txOut(0).amount == Satoshi(300000))
|
||||
|
||||
awaitCond(alice.stateName == CLOSING)
|
||||
assert(alice.stateData.asInstanceOf[DATA_CLOSING].theirCommitPublished == Some(bobCommitTx))
|
||||
}
|
||||
}
|
||||
|
||||
ignore("recv BITCOIN_ANCHOR_SPENT") { case (alice, _, alice2bob, bob2alice, alice2blockchain, _) =>
|
||||
within(30 seconds) {
|
||||
val tx = alice.stateData.asInstanceOf[DATA_CLEARING].commitments.ourCommit.publishableTx
|
||||
|
|
Loading…
Add table
Reference in a new issue