Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

State-Aware Gbot #164

Merged
merged 18 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions bin/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ impl Bank for Runtime {
impl Bounty for Runtime {
type IpfsReference = CidBytes;
type BountyId = u64;
type BountyPost = BountyBody;
type BountyPost = GithubIssue;
type SubmissionId = u64;
type BountySubmission = BountyBody;
type BountySubmission = GithubIssue;
}

impl sunshine_identity_client::Identity for Runtime {
Expand All @@ -120,7 +120,7 @@ impl substrate_subxt::Runtime for Runtime {
}

pub struct OffchainClient<S> {
bounties: IpldCache<S, Codec, BountyBody>,
bounties: IpldCache<S, Codec, GithubIssue>,
constitutions: IpldCache<S, Codec, TextBlock>,
}

Expand All @@ -133,7 +133,7 @@ impl<S: Store> OffchainClient<S> {
}
}

derive_cache!(OffchainClient, bounties, Codec, BountyBody);
derive_cache!(OffchainClient, bounties, Codec, GithubIssue);
derive_cache!(OffchainClient, constitutions, Codec, TextBlock);

impl<S: Store> From<S> for OffchainClient<S> {
Expand Down
14 changes: 7 additions & 7 deletions client/cli/src/bounty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sunshine_bounty_client::{
Bounty,
BountyClient,
},
BountyBody,
GithubIssue,
};
use sunshine_client_utils::{
cid::CidBytes,
Expand All @@ -43,11 +43,11 @@ impl BountyPostCommand {
<R as System>::AccountId: Ss58Codec,
<R as Balances>::Balance: From<u128> + Display,
<R as Bounty>::BountyId: Display,
<R as Bounty>::BountyPost: From<BountyBody>,
<R as Bounty>::BountyPost: From<GithubIssue>,
{
let metadata: GithubIssueMetadata =
self.issue_url.as_str().try_into()?;
let bounty: <R as Bounty>::BountyPost = BountyBody {
let bounty: <R as Bounty>::BountyPost = GithubIssue {
repo_owner: metadata.owner,
repo_name: metadata.repo,
issue_number: metadata.issue,
Expand Down Expand Up @@ -106,11 +106,11 @@ impl BountySubmitCommand {
<R as Balances>::Balance: From<u128> + Display,
<R as Bounty>::BountyId: From<u64> + Display,
<R as Bounty>::SubmissionId: Display,
<R as Bounty>::BountySubmission: From<BountyBody>,
<R as Bounty>::BountySubmission: From<GithubIssue>,
{
let metadata: GithubIssueMetadata =
self.issue_url.as_str().try_into()?;
let bounty: <R as Bounty>::BountySubmission = BountyBody {
let bounty: <R as Bounty>::BountySubmission = GithubIssue {
repo_owner: metadata.owner,
repo_name: metadata.repo,
issue_number: metadata.issue,
Expand Down Expand Up @@ -222,7 +222,7 @@ impl GetOpenBountiesCommand {
) -> Result<()>
where
R: Bounty<IpfsReference = CidBytes>,
C::OffchainClient: Cache<Codec, BountyBody>,
C::OffchainClient: Cache<Codec, GithubIssue>,
<R as Balances>::Balance: From<u128> + Display,
<R as Bounty>::BountyId: Display,
<R as Bounty>::SubmissionId: Display + From<u64>,
Expand Down Expand Up @@ -275,7 +275,7 @@ impl GetOpenSubmissionsCommand {
) -> Result<()>
where
R: Bounty<IpfsReference = CidBytes>,
C::OffchainClient: Cache<Codec, BountyBody>,
C::OffchainClient: Cache<Codec, GithubIssue>,
<R as Balances>::Balance: Display,
<R as Bounty>::BountyId: From<u64> + Display,
<R as Bounty>::SubmissionId: Display,
Expand Down
46 changes: 36 additions & 10 deletions client/client/src/bounty/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
mod subxt;

pub use subxt::*;

use crate::error::Error;
use codec::Encode;
use std::convert::TryInto;
use substrate_subxt::{
sp_core::H256,
system::System,
Runtime,
SignedExtension,
SignedExtra,
};
pub use subxt::*;
use sunshine_client_utils::{
async_trait,
Client,
Result,
};

fn slice_to_array(from: &[u8]) -> [u8; 32] {
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
if let Ok(s) = from.try_into() {
s
} else {
let mut buf: [u8; 32] = Default::default();
buf.clone_from_slice(&from[0..32]);
buf
}
}

#[async_trait]
pub trait BountyClient<T: Runtime + Bounty>: Client<T> {
async fn post_bounty(
Expand Down Expand Up @@ -70,6 +83,7 @@ where
T: Runtime + Bounty,
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
Send + Sync,
<T as System>::Hash: From<H256>,
<T as Bounty>::IpfsReference: From<libipld::cid::Cid>,
C: Client<T>,
C::OffchainClient: ipld_block_builder::Cache<
Expand All @@ -86,9 +100,16 @@ where
amount: BalanceOf<T>,
) -> Result<BountyPostedEvent<T>> {
let signer = self.chain_signer()?;
let issue_hash =
H256::from(slice_to_array(Encode::encode(&bounty).as_slice()));
let info = crate::post(self, bounty).await?;
self.chain_client()
.post_bounty_and_watch(&signer, info.into(), amount)
.post_bounty_and_watch(
&signer,
issue_hash.into(),
info.into(),
amount,
)
.await?
.bounty_posted()?
.ok_or_else(|| Error::EventNotFound.into())
Expand All @@ -112,11 +133,14 @@ where
amount: BalanceOf<T>,
) -> Result<BountySubmissionPostedEvent<T>> {
let signer = self.chain_signer()?;
let issue_hash =
H256::from(slice_to_array(Encode::encode(&submission).as_slice()));
let submission_ref = crate::post(self, submission).await?;
self.chain_client()
.submit_for_bounty_and_watch(
&signer,
bounty_id,
issue_hash.into(),
submission_ref.into(),
amount,
)
Expand Down Expand Up @@ -249,7 +273,7 @@ mod tests {
Client,
},
utils::bounty::BountyInformation,
BountyBody,
GithubIssue,
};

// For testing purposes only, NEVER use this to generate AccountIds in practice because it's random
Expand Down Expand Up @@ -281,7 +305,7 @@ mod tests {
let (node, _node_tmp) = test_node();
let client = Client::mock(&node, AccountKeyring::Alice).await;
let alice_account_id = AccountKeyring::Alice.to_account_id();
let bounty = BountyBody {
let bounty = GithubIssue {
repo_owner: "sunshine-protocol".to_string(),
repo_name: "sunshine-bounty".to_string(),
issue_number: 124,
Expand All @@ -291,6 +315,7 @@ mod tests {
depositer: alice_account_id,
amount: 10,
id: 1,
issue_hash: event.issue_hash,
description: event.description.clone(),
};
assert_eq!(event, expected_event);
Expand All @@ -301,16 +326,16 @@ mod tests {
let (node, _node_tmp) = test_node();
let client = Client::mock(&node, AccountKeyring::Alice).await;
let alice_account_id = AccountKeyring::Alice.to_account_id();
let bounty1 = BountyBody {
let bounty1 = GithubIssue {
repo_owner: "sunshine-protocol".to_string(),
repo_name: "sunshine-bounty".to_string(),
issue_number: 124,
issue_number: 125,
};
let event1 = client.post_bounty(bounty1, 10u128).await.unwrap();
let bounty2 = BountyBody {
let bounty2 = GithubIssue {
repo_owner: "sunshine-protocol".to_string(),
repo_name: "sunshine-bounty".to_string(),
issue_number: 124,
issue_number: 126,
};
let event2 = client.post_bounty(bounty2, 10u128).await.unwrap();
let bounties = client.open_bounties(9u128).await.unwrap().unwrap();
Expand Down Expand Up @@ -340,7 +365,7 @@ mod tests {
let (node, _node_tmp) = test_node();
let client = Client::mock(&node, AccountKeyring::Alice).await;
let alice_account_id = AccountKeyring::Alice.to_account_id();
let bounty = BountyBody {
let bounty = GithubIssue {
repo_owner: "sunshine-protocol".to_string(),
repo_name: "sunshine-bounty".to_string(),
issue_number: 124,
Expand All @@ -360,6 +385,7 @@ mod tests {
depositer: alice_account_id.clone(),
amount: 1000,
id: 1,
issue_hash: event1.issue_hash,
description: event1.description.clone(),
};
assert_eq!(event1, expected_event1);
Expand Down
10 changes: 10 additions & 0 deletions client/client/src/bounty/subxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ pub type Contrib<T> = Contribution<
BalanceOf<T>,
>;

#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
pub struct IssueHashSetStore<T: System> {
#[store(returns = ())]
pub issue_hash: T::Hash,
}

#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
pub struct BountiesStore<T: Bounty> {
#[store(returns = BountyState<T>)]
Expand All @@ -145,6 +151,7 @@ pub struct ContributionsStore<T: Bounty> {

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct PostBountyCall<T: Bounty> {
pub issue_hash: T::Hash,
pub info: T::IpfsReference,
pub amount: BalanceOf<T>,
}
Expand All @@ -154,6 +161,7 @@ pub struct BountyPostedEvent<T: Bounty> {
pub depositer: <T as System>::AccountId,
pub amount: BalanceOf<T>,
pub id: T::BountyId,
pub issue_hash: T::Hash,
pub description: T::IpfsReference,
}

Expand All @@ -175,6 +183,7 @@ pub struct BountyRaiseContributionEvent<T: Bounty> {
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct SubmitForBountyCall<T: Bounty> {
pub bounty_id: T::BountyId,
pub issue_hash: T::Hash,
pub submission_ref: T::IpfsReference,
pub amount: BalanceOf<T>,
}
Expand All @@ -186,6 +195,7 @@ pub struct BountySubmissionPostedEvent<T: Bounty> {
pub amount: BalanceOf<T>,
pub id: T::SubmissionId,
pub bounty_ref: T::IpfsReference,
pub issue_hash: T::Hash,
pub submission_ref: T::IpfsReference,
}

Expand Down
4 changes: 2 additions & 2 deletions client/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub struct TextBlock {
}

#[derive(Debug, Default, Clone, DagCbor, Encode, Decode)]
pub struct BountyBody {
pub struct GithubIssue {
pub issue_number: u64,
pub repo_owner: String,
pub repo_name: String,
pub issue_number: u64,
}

pub(crate) async fn post<R, C, V>(
Expand Down
16 changes: 8 additions & 8 deletions client/ffi/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use sunshine_bounty_client::{
BountyState,
SubState,
},
BountyBody,
GithubIssue,
};
use sunshine_client_utils::{
cid::CidBytes,
Expand Down Expand Up @@ -197,12 +197,12 @@ where
C: BountyClient<R> + Send + Sync,
R: Runtime + BountyTrait + Debug,
R: BountyTrait<IpfsReference = CidBytes>,
C::OffchainClient: Cache<Codec, BountyBody>,
C::OffchainClient: Cache<Codec, GithubIssue>,
<R as System>::AccountId: Ss58Codec + Into<<R as System>::Address>,
<R as BountyTrait>::BountyId: From<u64> + Into<u64> + Display,
<R as BountyTrait>::SubmissionId: From<u64> + Into<u64> + Display,
<R as BountyTrait>::BountyPost: From<BountyBody> + Debug,
<R as BountyTrait>::BountySubmission: From<BountyBody> + Debug,
<R as BountyTrait>::BountyPost: From<GithubIssue> + Debug,
<R as BountyTrait>::BountySubmission: From<GithubIssue> + Debug,
<R as Balances>::Balance: Into<u128> + From<u64>,
{
pub async fn get(&self, bounty_id: &str) -> Result<String> {
Expand All @@ -228,7 +228,7 @@ where
issue_number: u64,
amount: &str,
) -> Result<u64> {
let bounty = BountyBody {
let bounty = GithubIssue {
repo_owner: repo_owner.to_string(),
repo_name: repo_name.to_string(),
issue_number,
Expand Down Expand Up @@ -272,7 +272,7 @@ where
issue_number: u64,
amount: &str,
) -> Result<u64> {
let bounty = BountyBody {
let bounty = GithubIssue {
repo_owner: repo_owner.to_string(),
repo_name: repo_name.to_string(),
issue_number,
Expand Down Expand Up @@ -496,7 +496,7 @@ where
) -> Result<BountyInformation> {
info!("Get bounty info of id: {}", id);
let event_cid = state.info().to_cid()?;
let bounty_body: BountyBody = self
let bounty_body: GithubIssue = self
.client
.read()
.await
Expand All @@ -523,7 +523,7 @@ where
info!("Get submission info of id: {}", id);
let event_cid = state.submission().to_cid()?;

let submission_body: BountyBody = self
let submission_body: GithubIssue = self
.client
.read()
.await
Expand Down
4 changes: 3 additions & 1 deletion client/gbot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ edition = "2018"
description = "github bot for sunshine bounty"

[dependencies]
octocrab = "0.5.0"
chrono = { version = "0.4.11", features = ["serde"] }
octocrab = { git = "https://github.com/4meta5/octocrab", branch = "just-update-comment" }
thiserror = "1.0.20"
lazy_static = "1.4.0"

[dev-dependencies]
tokio = { version = "0.2.22", default-features = false, features = ["macros"] }
Expand Down
Loading