Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refund #36

Open
wants to merge 20 commits into
base: alpha-bitide
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ r2d2_redis = { git = "https://github.com/GopherJ/r2d2-redis", rev = "63be37454cf
gnark-plonky2-wrapper = { git = "https://github.com/cf/gnark-plonky2-verifier", rev = "75681e2a0e20270734f082145d13cdfe3349d009" }
bs58 = { version = "0.5.1", features = ["std", "check"] }
jsonrpsee = { version = "0.24.0", features = ["full"] }
rocksdb = { version = "0.21.0", features = ["serde", "multi-threaded-cf"] }
# rocksdb = { version = "0.21.0", features = ["serde", "multi-threaded-cf"] }
home = { version = "0.5.9" }
indicatif = "0.17.8"

[patch.'https://github.com/0xPolygonZero/plonky2.git']
plonky2 = { git = "https://github.com/QEDProtocol/plonky2-hwa", rev = "6a8ca008da97890b67a84f64784cfbc488b5238d" }

[patch.'https://github.com/succinctlabs/starkyx.git']
starkyx = { git = "https://github.com/QEDProtocol/starkyx", rev = "a53ea1060c6256a8a26820e43fbe8d75233e8b29" }

[profile.release]
debug = true
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TRACE_ENABLED := 1
TRACE_ENABLED := full
PROFILE := release
LOG_LEVEL := info,city_common_circuit=off,city_rollup_circuit=off,plonky2=off,city_crypto=off,city_store=off,city_rollup_common=off

Expand Down Expand Up @@ -28,7 +28,7 @@ dedup:

.PHONY: build
build:
cargo build --${PROFILE}
cargo build --profile ${PROFILE}

.PHONY: build-if-not-exists
build-if-not-exists:
Expand Down Expand Up @@ -142,6 +142,12 @@ cr_l1_deposit: build-if-not-exists
--private-key=2c6a1188f8739daaeff79c40f3690c573381c91a2359a0df2b45e4310b59f30b \
--amount=100000000

.PHONY: cr_l1_refund
cr_l1_refund: build-if-not-exists
@RUST_LOG=${LOG_LEVEL} RUST_BACKTRACE=${TRACE_ENABLED} ./target/${PROFILE}/city-rollup-user-cli l1-refund \
--private-key=2c6a1188f8739daaeff79c40f3690c573381c91a2359a0df2b45e4310b59f30b \
--txid=${TXID}

.PHONY: cr_claim_deposit
cr_claim_deposit: build-if-not-exists
@RUST_LOG=${LOG_LEVEL} RUST_BACKTRACE=${TRACE_ENABLED} ./target/${PROFILE}/city-rollup-user-cli claim-deposit \
Expand Down Expand Up @@ -184,7 +190,7 @@ cr_get_deposit_by_txid:
curl http://localhost:3000 \
-X POST \
-H "Content-Type: application/json" \
--data '{"method":"cr_getDepositByTxid","params":["a4740418b4c2119468ba1a561561ff4163b14476ec2171c92f987c32922c85ff"],"id":1,"jsonrpc":"2.0"}' | jq
--data '{"method":"cr_getDepositByTxid","params":["5096eab42d26997ef450567e3c1fd9646b31910fbfc71700affa8a0346fa4e5c"],"id":1,"jsonrpc":"2.0"}' | jq

.PHONY: cr_get_deposit_by_id
cr_get_deposit_by_id:
Expand Down
28 changes: 28 additions & 0 deletions city_common/src/cli/user_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ pub struct L1DepositArgs {
pub electrs_api: String,
}

#[derive(Clone, Args)]
pub struct L1RefundArgs {
#[clap(long, short, default_value = "http://127.0.0.1:3000", env)]
pub rpc_address: String,

#[clap(long, short)]
pub private_key: String,

#[clap(long, short)]
pub deposit_checkpoint_id: Option<u64>,

#[clap(long, short)]
pub txid: String,

#[clap(
env,
long,
default_value = "http://devnet:devnet@localhost:1337/bitcoin-rpc/?network=dogeRegtest",
env
)]
pub bitcoin_rpc: String,
#[clap(env, long, default_value = "http://localhost:1337/api", env)]
pub electrs_api: String,

#[clap(long, default_value = "dogeregtest", env)]
pub network: String,
}

#[derive(Clone, Args)]
pub struct AddWithdrawalArgs {
#[clap(long, short, default_value = "http://127.0.0.1:3000", env)]
Expand Down
1 change: 1 addition & 0 deletions city_common/src/config/rollup_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub const BALANCE_BIT_SIZE: usize = 64; //56;
pub const NONCE_BIT_SIZE: usize = 64; //56;
pub const WITHDRAWAL_FEE_AMOUNT: u64 = 100000;
pub const BLOCK_SCRIPT_SPEND_BASE_FEE_AMOUNT: u64 = 80000000;
pub const REFUND_SCRIPT_BASE_FEE_AMOUNT: u64 = 80000000;
pub const DEPOSIT_FEE_AMOUNT: u64 = 100000;
10 changes: 10 additions & 0 deletions city_common_circuit/src/builder/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub trait CircuitBuilderConnectHelpers<F: RichField + Extendable<D>, const D: us
allowed: &[HashOutTarget],
);
fn connect_vec(&mut self, x: &[Target], y: &[Target]);
fn connect_any_vec(&mut self, x: &[Target], y: &[Vec<Target>]);
fn connect_templated_array(
&mut self,
template_start: &[u64],
Expand Down Expand Up @@ -193,4 +194,13 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderConnectHelpers<
let combined = [tpl_start, template_middle.to_vec(), tpl_end].concat();
self.connect_vec(target_array, &combined);
}

fn connect_any_vec(&mut self, x: &[Target], y: &[Vec<Target>]) {
let mut result = self._false();
for arr in y.iter() {
let equal = self.is_equal_array(x, arr);
result = self.or(result, equal);
}
self.assert_one(result.target)
}
}
2 changes: 1 addition & 1 deletion city_common_circuit/src/builder/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<T: Witness<F>, F: PrimeField64> WitnessHelpersCore<F> for T {
//tracing::info!("target empty, trying constant");
let alt_result = alt_resolver.try_resolve_target_or_constant(target);
if alt_result.is_none() {
panic!("cannot resolve target!");
panic!("cannot resolve target! {:?}", target);
} else {
alt_result.unwrap()
}
Expand Down
8 changes: 6 additions & 2 deletions city_common_circuit/src/builder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub trait CircuitBuilderSelectHelpers<F: RichField + Extendable<D>, const D: usi
x: Hash256BytesTarget,
y: Hash256BytesTarget,
) -> BoolTarget;
fn is_equal_array(&mut self, x: &[Target], y: &[Target]) -> BoolTarget;
}

impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderSelectHelpers<F, D>
Expand Down Expand Up @@ -112,18 +113,21 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderSelectHelpers<F
}
last_allowed
}

fn is_equal_hash_256_bytes(
&mut self,
x: Hash256BytesTarget,
y: Hash256BytesTarget,
) -> BoolTarget {
self.is_equal_array(&x, &y)
}

fn is_equal_array(&mut self, x: &[Target], y: &[Target]) -> BoolTarget {
let mut result = self.constant_bool(true);
for i in 0..x.len() {
let equal = self.is_equal(x[i], y[i]);
result = self.and(equal, result);
}
result

}
}
32 changes: 5 additions & 27 deletions city_common_circuit/src/hash/accelerator/sha256/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ impl Sha256AcceleratorDomain {
W: Witness<F>,
F: RichField + Extendable<D>,
const D: usize,
Iter: Iterator<Item = &'a [u8]>,
T: AsRef<[u8]>,
Iter: IntoIterator<Item = T>,
>(
&self,
witness: &mut W,
Expand All @@ -175,6 +176,7 @@ impl Sha256AcceleratorDomain {
.iter()
.zip(preimages)
.for_each(|(hash_id, p)| {
let p = p.as_ref();
let digest = CoreSha256Hasher::hash_bytes(p);
witness.set_hash256_bytes_target(&self.planned_hashes[*hash_id].digest, &digest.0);
witness.set_target_arr(
Expand All @@ -190,39 +192,15 @@ impl Sha256AcceleratorDomain {
witness: &mut W,
preimages: &[Vec<u8>],
) {
self.witnessed_hash_ids
.iter()
.zip(preimages)
.for_each(|(hash_id, p)| {
let digest = CoreSha256Hasher::hash_bytes(p);
witness.set_hash256_bytes_target(&self.planned_hashes[*hash_id].digest, &digest.0);
witness.set_target_arr(
&self.planned_hashes[*hash_id].preimage,
&p.iter()
.map(|x| F::from_canonical_u8(*x))
.collect::<Vec<F>>(),
)
});
self.set_witness_iter(witness, preimages)
}

pub fn set_witness_refs<W: Witness<F>, F: RichField + Extendable<D>, const D: usize>(
&self,
witness: &mut W,
preimages: &[&[u8]],
) {
self.witnessed_hash_ids
.iter()
.zip(preimages)
.for_each(|(hash_id, p)| {
let digest = CoreSha256Hasher::hash_bytes(p);
witness.set_hash256_bytes_target(&self.planned_hashes[*hash_id].digest, &digest.0);
witness.set_target_arr(
&self.planned_hashes[*hash_id].preimage,
&p.iter()
.map(|x| F::from_canonical_u8(*x))
.collect::<Vec<F>>(),
)
});
self.set_witness_iter(witness, preimages)
}
pub fn process_derived_pass<W: Witness<F>, F: RichField + Extendable<D>, const D: usize>(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use city_common::logging::trace_timer::TraceTimer;
// use city_common::logging::trace_timer::TraceTimer;
use city_common_circuit::{builder::{connect::CircuitBuilderConnectHelpers, core::{CircuitBuilderHelpersCore, WitnessHelpersCore}, select::CircuitBuilderSelectHelpers, signature::CircuitBuilderSignatureHelpers}, hash::{accelerator::sha256::planner::{Sha256AcceleratorDomain, Sha256AcceleratorDomainID, Sha256AcceleratorDomainPlanner, Sha256AcceleratorDomainResolver}, base_types::{felthash248::CircuitBuilderFelt248Hash, felthash252::CircuitBuilderFelt252Hash, hash160bytes::Hash160BytesTarget, hash256bytes::{CircuitBuilderHash256Bytes, Hash256BytesTarget}}}};
use city_rollup_common::{block_template::config::{GENESIS_STATE_HASH, OP_CHECKGROTH16VERIFY, OP_CHECKGROTH16VERIFY_NOP}, introspection::rollup::introspection::{BlockSpendIntrospectionGadgetConfig, BlockSpendIntrospectionHint}};
use plonky2::{
Expand Down Expand Up @@ -54,7 +54,6 @@ pub struct BTCRollupIntrospectionGadget {

pub last_block_spend_index: i32,
pub block_spend_index: usize,

pub current_spend_index: usize,

pub next_block_redeem_script: Vec<Target>,
Expand All @@ -63,6 +62,7 @@ pub struct BTCRollupIntrospectionGadget {

pub hash_domain: Sha256AcceleratorDomain,
pub hash_domain_id: Sha256AcceleratorDomainID,

pub current_state_hash: HashOutTarget,
}

Expand All @@ -71,19 +71,18 @@ impl BTCRollupIntrospectionGadget {
builder: &mut CircuitBuilder<F, D>,
config: &BlockSpendIntrospectionGadgetConfig,
) -> Self {
let mut trace_timer = TraceTimer::new("BTCRollupIntrospectionGadget");
// let mut trace_timer = TraceTimer::new("BTCRollupIntrospectionGadget");

let mut hash_domain = Sha256AcceleratorDomain::new();

// start sig hash
let sighash_tx = BTCTransactionBytesGadget::add_virtual_to_fixed_locktime_version(
let sighash_tx = BTCTransactionBytesGadget::add_virtual_to_fixed_locktime(
builder,
config
.sighash_preimage_config
.transaction_config
.layout
.clone(),
config.sighash_preimage_config.transaction_config.version,
config.sighash_preimage_config.transaction_config.locktime,
false,
);
Expand All @@ -105,25 +104,23 @@ impl BTCRollupIntrospectionGadget {

let mut funding_transactions = Vec::with_capacity(config.funding_transaction_configs.len());
for (i, funding_tx_config) in config.funding_transaction_configs.iter().enumerate() {
trace_timer.event(format!(
"funding_tx_config: {:?}",
funding_tx_config.layout.input_script_sizes
));
// trace_timer.event(format!(
// "funding_tx_config: {:?}",
// funding_tx_config.layout.input_script_sizes
// ));

let funding_tx = if i != config.block_spend_index {
BTCTransactionBytesGadget::add_virtual_to_fixed_locktime_version_with_der(
BTCTransactionBytesGadget::add_virtual_to_fixed_locktime_with_der(
builder,
funding_tx_config.layout.clone(),
funding_tx_config.version,
funding_tx_config.locktime,
false,
true,
)
}else{
BTCTransactionBytesGadget::add_virtual_to_fixed_locktime_version(
BTCTransactionBytesGadget::add_virtual_to_fixed_locktime(
builder,
funding_tx_config.layout.clone(),
funding_tx_config.version,
funding_tx_config.locktime,
false,
)
Expand Down
Loading