Skip to content

Test protocol using elements-consensus rather than blockchain nodes#34

Merged
luckysori merged 1 commit into
masterfrom
elements-consensus
Aug 6, 2021
Merged

Test protocol using elements-consensus rather than blockchain nodes#34
luckysori merged 1 commit into
masterfrom
elements-consensus

Conversation

@luckysori
Copy link
Copy Markdown
Collaborator

@luckysori luckysori commented Jul 29, 2021

Fixes #21.

Comment thread src/loan/protocol_tests.rs Outdated
@luckysori luckysori force-pushed the elements-consensus branch from ba05fa3 to 94dc5f3 Compare July 30, 2021 13:34
@luckysori luckysori changed the base branch from master to release/0.2.0 July 30, 2021 13:36
Comment thread Cargo.toml Outdated
Comment thread src/loan/protocol_tests.rs Outdated
use tokio::sync::Mutex;

#[tokio::test]
async fn borrow_and_repay() {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomaseizinger I've rewritten this test to not use testcontainers, which makes it so much faster. What do you think? Should we do it for the rest and transition to this kind of testing?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your decision :)

To build more confidence with the lib, it may be good to include some negative tests as well. That way we know it is doing something!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, fast and high-confidence unit tests for protocols are a little dream coming true. Can't believe it was so easy to do. So much time wasted in the past 😭

Comment thread src/loan/protocol_tests.rs Outdated
Comment thread src/loan/protocol_tests.rs Outdated
Comment thread src/loan/protocol_tests.rs Outdated
Comment thread src/loan/protocol_tests.rs Outdated
Comment on lines +872 to +879
let hash = elements::hashes::hash160::Hash::hash(&pk.serialize());
let script = Builder::new()
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(&hash.into_inner())
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making an address from a key should give you this script I think.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip! Funnily enough for a p2wpkh address you have to construct the corresponding p2pkh address and call script_pubkey() on it. Otherwise it doesn't work!

Comment thread src/loan/protocol_tests.rs Outdated
use tokio::sync::Mutex;

#[tokio::test]
async fn borrow_and_repay() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your decision :)

To build more confidence with the lib, it may be good to include some negative tests as well. That way we know it is doing something!

Comment thread src/loan/protocol_tests.rs Outdated
use tokio::sync::Mutex;

#[tokio::test]
async fn borrow_and_repay() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, fast and high-confidence unit tests for protocols are a little dream coming true. Can't believe it was so easy to do. So much time wasted in the past 😭

@luckysori luckysori force-pushed the elements-consensus branch from 94dc5f3 to c61b860 Compare August 5, 2021 05:12
@luckysori luckysori changed the base branch from release/0.2.0 to master August 5, 2021 05:13
@luckysori
Copy link
Copy Markdown
Collaborator Author

luckysori commented Aug 5, 2021

To build more confidence with the lib, it may be good to include some negative tests as well. That way we know it is doing something!

@thomaseizinger Can you give me some examples of negative tests you would like to see added to the library. I wouldn't tackle it here since we're just "refactoring" the tests, but I do want to make sure that we record an issue for this.

@luckysori luckysori marked this pull request as ready for review August 5, 2021 05:30
@thomaseizinger
Copy link
Copy Markdown
Contributor

To build more confidence with the lib, it may be good to include some negative tests as well. That way we know it is doing something!

@thomaseizinger Can you give me some examples of negative tests you would like to see added to the library. I wouldn't tackle it here since we're just "refactoring" the tests, but I do want to make sure that we record an issue for this.

For example, trying to repay a loan with the wrong amount, trying to repay a loan to the wrong address etc

Copy link
Copy Markdown
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

If this is not a feels-good PR, then I don't know what is :)

Comment thread tests/swap_happy_path.rs Outdated
Comment thread tests/loan_protocol.rs
Comment thread tests/loan_protocol.rs Outdated
Comment thread tests/loan_protocol.rs Outdated
Comment on lines +395 to +399
let wallet = wallet.clone();
|tx| async move {
let wallet = wallet.lock().unwrap();
Ok(wallet.sign_inputs(tx))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wouldn't move here, you should be able to get away with not cloning either which might allow you to get rid of Arc<Mutex>>.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. I was really hoping we could get rid of most of these.

It can be done except for those API calls which use the wallet twice: for coin selection and signing. These closures are probably going away soon anyway, so the situation should improve.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to use Arc<Mutex<Wallet>> once!

@luckysori luckysori mentioned this pull request Aug 6, 2021
This means we no longer need `testcontainers`, ```elements-harness` or
`elements-rpc`! And the tests are so much faster.
@luckysori luckysori force-pushed the elements-consensus branch from 913ffed to a9d616b Compare August 6, 2021 02:16
@luckysori luckysori merged commit e87ba22 into master Aug 6, 2021
@luckysori luckysori deleted the elements-consensus branch August 6, 2021 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Experiment with testing using rust-elements-consensus

2 participants