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

refactor(sia): Sia http client refactor and #2108

Draft
wants to merge 136 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
18fb2e8
bare bones sia scaffold
Alrighttt Dec 22, 2023
1e580c5
bare bones sia skeleton
Alrighttt Jan 18, 2024
3b24947
minimal async sia init
Alrighttt Feb 21, 2024
e7a3861
Merge pull request #15 from KomodoPlatform/dev
Alrighttt Feb 21, 2024
fc0f54e
add dummy http server param
Alrighttt Feb 23, 2024
32fdb85
init basic sia docker node
Alrighttt Feb 23, 2024
5c25994
Allow sia docker container to stay alive
Alrighttt Mar 6, 2024
41ec8ef
enable enable_sia::status rpc
Alrighttt Mar 6, 2024
310fa14
initial API interface
Alrighttt Mar 6, 2024
7346a4c
Parse to Url instead of String; improve parse errors
Alrighttt Mar 6, 2024
516c67a
cargo fmt
Alrighttt Mar 6, 2024
4b67aef
use reqwest version already in dep tree
Alrighttt Mar 6, 2024
aeca0ed
generalize GET requests
Alrighttt Mar 6, 2024
da9ec8c
add api/addresses/addr/balance walletd endpoint
Alrighttt Mar 11, 2024
6771231
cargo fmt
Alrighttt Mar 12, 2024
aebb48c
add blake2b_simd dep for sia
Alrighttt Mar 12, 2024
ee3fc8c
init sia address module
Alrighttt Mar 12, 2024
9499399
blake2b cargo.lock
Alrighttt Mar 12, 2024
412436c
ignore all api client tests; must be dockerized
Alrighttt Mar 15, 2024
9d541e0
more verbose address module error handling
Alrighttt Mar 15, 2024
d2f8724
init blake2b_internal module; allows v1 address gen
Alrighttt Mar 15, 2024
1144694
remove SiaProtocolInfo dummy from lp_coins
Alrighttt Mar 15, 2024
80468c3
wrap sia keypair in PrivKeyPolicy; cargo fmt
Alrighttt Mar 15, 2024
920bdab
remove MmWeak from SiaCoinFields - used for balance streaming only
Alrighttt Mar 15, 2024
5d0745e
add v1 pubkey->address function
Alrighttt Mar 16, 2024
cf7034e
replace my_address mocked value with v1 address
Alrighttt Mar 17, 2024
c96ef4b
make hash_blake2b_pair fn sig more intuitive
Alrighttt Mar 17, 2024
1f16617
make unlock_hash gen funcs pub
Alrighttt Mar 17, 2024
e163b17
spend_policy module skeleton
Alrighttt Mar 17, 2024
5abdb2f
fix build warning in spend_policy skeleton
Alrighttt Mar 17, 2024
a7aa49e
port Accumulator type and methods; introduce UnlockCondition type
Alrighttt Mar 17, 2024
a5079a7
Merge branch 'dev' into sia-dev
Alrighttt Mar 17, 2024
498770d
cargo fmt
Alrighttt Mar 17, 2024
bc89322
feat(nft): enable eth with non fungible tokens (#2049)
laruh Mar 1, 2024
0d5a47e
Revert "feat(nft): enable eth with non fungible tokens (#2049)"
Alrighttt Mar 18, 2024
d766bcd
make all sia code conditionally compile with "enable-sia" feature
Alrighttt Mar 18, 2024
b8d1ed1
add buffer Encoder / Hasher
Alrighttt Mar 19, 2024
340d7c7
make hash_blake2b_single fn public for Encoder
Alrighttt Mar 19, 2024
7a07024
add Clone trait to Address struct
Alrighttt Mar 22, 2024
995f7b0
make ED25519_IDENTIFIER const pub
Alrighttt Mar 22, 2024
3617c69
add encoder u64 test; cargo fmt
Alrighttt Mar 22, 2024
07eed5d
add SpendPolicy encoding
Alrighttt Mar 22, 2024
b6af96e
cargo fmt
Alrighttt Mar 22, 2024
d180505
add additional unlock_hash test case
Alrighttt Mar 22, 2024
71aaaf8
Merge branch 'dev' into sia-dev
Alrighttt Apr 4, 2024
dd288ab
seperate http auth conf to distinct struct
Alrighttt Apr 4, 2024
31f18df
remove unused import
Alrighttt Apr 4, 2024
a6a7957
fix missing import; cargo fmt
Alrighttt Apr 4, 2024
a117da1
cargo fmt
Alrighttt Apr 4, 2024
58986fe
remove irrelevant comment
Alrighttt Apr 4, 2024
bcbdcf3
remove allow dead_code flag; formatting
Alrighttt Apr 4, 2024
c78cff0
formatting
Alrighttt Apr 4, 2024
faca52f
add str_without_prefix method for Address
Alrighttt Apr 4, 2024
faa74c3
add Address serde to get_addresses_balance endpoint; formatting
Alrighttt Apr 4, 2024
c410f8f
add comment re: additional Activation Params fields
Alrighttt Apr 4, 2024
bfa79f7
comment re: planned SiaCoinFields fields
Alrighttt Apr 4, 2024
5365dfc
fix clippy warnings
Alrighttt Apr 4, 2024
79b16ed
use const for consensus/tip endpoint str
Alrighttt Apr 4, 2024
62ed639
Merge remote-tracking branch 'kp/dev' into sia-dev
Alrighttt Apr 12, 2024
a8597ce
fix git merge confusion
Alrighttt Apr 12, 2024
0ceb9cb
simplify Address Display trait
Alrighttt Apr 12, 2024
f62e97c
func name typo
Alrighttt Apr 12, 2024
e0dc4e5
move test funcs to end of file
Alrighttt Apr 12, 2024
1049dd0
remove deprecated base64 fn
Alrighttt Apr 12, 2024
a43e563
cargo fmt
Alrighttt Apr 12, 2024
0da2bd1
Merge remote-tracking branch 'kp/dev' into sia-dev
Alrighttt Apr 16, 2024
0b2acd0
serde traits for Address struct
Alrighttt Apr 16, 2024
83bef4d
remove frivulous comment
Alrighttt Apr 16, 2024
6cd07cc
add additional SiaApliClientError variants
Alrighttt Apr 16, 2024
ac3da5b
begin http client refactor
Alrighttt Apr 16, 2024
421ebdc
continue Api client refactor
Alrighttt Apr 16, 2024
93676c4
change fn name
Alrighttt Apr 17, 2024
cdd061f
return full AddressBalanceResponse
Alrighttt Apr 18, 2024
70d5ece
remove unused struct
Alrighttt Apr 18, 2024
979fa3d
add inline comments
Alrighttt Apr 18, 2024
541ac53
simplify address_balance fn
Alrighttt Apr 23, 2024
600f627
tidy use import
Alrighttt Apr 23, 2024
b306d7a
tidy imports
Alrighttt Apr 23, 2024
67e3ca2
tidy imports
Alrighttt Apr 23, 2024
a92bc2a
remove .unwrap() in prod
Alrighttt Apr 23, 2024
fb0c95a
remove unused struct
Alrighttt Apr 23, 2024
e23ee8c
simplify protocolinfo struct
Alrighttt Apr 23, 2024
4552791
revert manual debugging static value
Alrighttt Apr 23, 2024
4600b5e
revert debuggin static value
Alrighttt Apr 23, 2024
2373247
use to_string instead of format!()
Alrighttt Apr 23, 2024
85b3e37
remove additional debug static return values
Alrighttt Apr 23, 2024
8729c11
add TODO comment for orderbook
Alrighttt Apr 23, 2024
38d220d
change "init_sia__coin_task_manager" to "init_sia_task_manager"
Alrighttt Apr 23, 2024
f457310
reorganize inline comment
Alrighttt Apr 23, 2024
0914929
reorganize inline comment
Alrighttt Apr 23, 2024
9de9bec
change inline comment to doc comment
Alrighttt Apr 23, 2024
38cbfae
remove frivolous comment
Alrighttt Apr 23, 2024
e1988d0
Omar review suggestions
Alrighttt Apr 23, 2024
d5fbd24
remove dead code
Alrighttt Apr 23, 2024
396e4d0
cargo fmt
Alrighttt Apr 23, 2024
1f16d03
simplify spend policies
Alrighttt Apr 23, 2024
6a388c2
Merge branch 'sia-dev' into url-refactor
Alrighttt Apr 23, 2024
ed27979
update fn name; fix mod import
Alrighttt Apr 23, 2024
06f2509
remove unused import
Alrighttt Apr 24, 2024
517df63
rename import for verbosity
Alrighttt Apr 24, 2024
e8a67d4
add comments for impossible test cases
Alrighttt Apr 24, 2024
92f1621
remove unnecessary wrapper from SiaApiClient
Alrighttt Apr 24, 2024
0a3eba5
more verbose reqwest error handling
Alrighttt Apr 24, 2024
2b797a7
begin fetch and parse revamp
Alrighttt Apr 25, 2024
b3e5678
Merge remote-tracking branch 'upstream/dev' into url-refactor
Alrighttt Apr 26, 2024
943a4b8
fix botched merge
Alrighttt Apr 26, 2024
edb2597
add initial sia docker tests
Alrighttt Apr 26, 2024
81d32de
assign static name to sia docker container
Alrighttt May 2, 2024
43401a9
move http client tests to docker test suite
Alrighttt May 2, 2024
aa62540
handle HTTP status codes WIP
Alrighttt May 2, 2024
b4abf14
WIP comment old http client tests
Alrighttt May 2, 2024
f34b402
reorder imports
Alrighttt May 2, 2024
3834faf
refactor SpendPolicy Encoder
Alrighttt May 2, 2024
c6d8d41
Add EncodeTo trait
Alrighttt May 2, 2024
70e1d84
cargo fmt
Alrighttt May 2, 2024
7803dca
Cargo.lock edit - no dep changes
Alrighttt May 2, 2024
ae023f6
refactor Specifier constants
Alrighttt May 2, 2024
ae6d455
Specifier link+comment
Alrighttt May 2, 2024
af2f0be
seperate specifiers to seperate file
Alrighttt May 3, 2024
5cf800a
remove irrelevant test
Alrighttt May 4, 2024
98b4ada
impl EncodeTo for H256
Alrighttt May 4, 2024
a12d934
add v1 transaction stubs
Alrighttt May 4, 2024
15f73fe
refactor UnlockKey encoding
Alrighttt May 4, 2024
5ed1ea9
new sia modules
Alrighttt May 4, 2024
65d58e8
cargo fmt
Alrighttt May 4, 2024
a9dbed1
clean up specifier defs with a macro
Alrighttt May 6, 2024
57a2e51
fix PublicKey encoder
Alrighttt May 7, 2024
a57ed21
add new encoder tests
Alrighttt May 7, 2024
668f6fa
rename EncodeTo trait -> Encodable
Alrighttt May 7, 2024
8c95362
vout encoding; begin vin encoding
Alrighttt May 9, 2024
6a03bf6
cargo fmt
Alrighttt May 9, 2024
87faaa3
impl Encodable for Address
Alrighttt May 9, 2024
33307d3
address encoding unit test
Alrighttt May 9, 2024
5b804b8
cargo fmt
Alrighttt May 9, 2024
b114633
actually fix pubkey encoding
Alrighttt May 13, 2024
c74bff4
cargo clippy
Alrighttt May 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

12 changes: 8 additions & 4 deletions mm2src/coins/sia.rs
Expand Up @@ -32,7 +32,10 @@ pub mod blake2b_internal;
pub mod encoding;
pub mod http_client;
use http_client::{SiaApiClient, SiaApiClientError};
pub mod http_endpoints;
pub mod specifier;
pub mod spend_policy;
pub mod transaction;

#[derive(Clone)]
pub struct SiaCoin(SiaArc);
Expand All @@ -57,7 +60,7 @@ pub struct SiaCoinConf {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SiaHttpConf {
pub url: Url,
pub auth: String,
pub password: String,
}

// TODO see https://github.com/KomodoPlatform/komodo-defi-framework/pull/2086#discussion_r1521660384
Expand Down Expand Up @@ -174,8 +177,9 @@ impl<'a> SiaCoinBuilder<'a> {
let conf = SiaConfBuilder::new(self.conf, self.ticker()).build()?;
let sia_fields = SiaCoinFields {
conf,
http_client: SiaApiClient::new(self.ticker(), self.params.http_conf.clone())
.map_err(SiaCoinBuildError::ClientError)?,
http_client: SiaApiClient::new(self.params.http_conf.clone())
.map_err(SiaCoinBuildError::ClientError)
.await?,
priv_key_policy: PrivKeyPolicy::Iguana(self.key_pair),
};
let sia_arc = SiaArc::new(sia_fields);
Expand Down Expand Up @@ -359,7 +363,7 @@ impl MarketCoinOps for SiaCoin {
fn current_block(&self) -> Box<dyn Future<Item = u64, Error = String> + Send> {
let http_client = self.0.http_client.clone(); // Clone the client

let height_fut = async move { http_client.get_height().await.map_err(|e| e.to_string()) }
let height_fut = async move { http_client.current_height().await.map_err(|e| e.to_string()) }
.boxed() // Make the future 'static by boxing
.compat(); // Convert to a futures 0.1-compatible future

Expand Down
22 changes: 20 additions & 2 deletions mm2src/coins/sia/address.rs
@@ -1,4 +1,5 @@
use crate::sia::blake2b_internal::standard_unlock_hash;
use crate::sia::encoding::{Encodable, Encoder};
use blake2b_simd::Params;
use ed25519_dalek::PublicKey;
use hex::FromHexError;
Expand All @@ -10,7 +11,7 @@ use std::str::FromStr;

// TODO this could probably include the checksum within the data type
// generating the checksum on the fly is how Sia Go does this however
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Address(pub H256);

impl Address {
Expand All @@ -21,6 +22,10 @@ impl Address {
}
}

impl Encodable for Address {
fn encode(&self, encoder: &mut Encoder) { encoder.write_slice(self.0 .0.as_ref()); }
}

impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "addr:{}", self.str_without_prefix()) }
}
Expand All @@ -36,7 +41,6 @@ pub enum ParseAddressError {
InvalidHexEncoding(String),
InvalidChecksum,
InvalidLength,
// Add other error kinds as needed
}

impl From<FromHexError> for ParseAddressError {
Expand Down Expand Up @@ -165,3 +169,17 @@ fn test_address_str_without_prefix() {
"591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f"
);
}

#[test]
fn test_address_encode() {
let pubkey = PublicKey::from_bytes(
&hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(),
)
.unwrap();
let address = v1_standard_address_from_pubkey(&pubkey);

let hash = Encoder::encode_and_hash(&address);
let expected = H256::from("d64b9a56043a909494f07520915e10dae62d75dba24b17c8414f8f3f30c53425");

assert_eq!(hash, expected);
}
18 changes: 2 additions & 16 deletions mm2src/coins/sia/blake2b_internal.rs
@@ -1,3 +1,4 @@
use crate::sia::specifier::Identifier;
use blake2b_simd::Params;
use ed25519_dalek::PublicKey;
use rpc::v1::types::H256;
Expand All @@ -9,10 +10,6 @@ use std::default::Default;
const LEAF_HASH_PREFIX: [u8; 1] = [0u8];
const NODE_HASH_PREFIX: [u8; 1] = [1u8];

pub const ED25519_IDENTIFIER: [u8; 16] = [
0x65, 0x64, 0x32, 0x35, 0x35, 0x31, 0x39, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
];

// Precomputed hash values used for all standard v1 addresses
// a standard address has 1 ed25519 public key, requires 1 signature and has a timelock of 0
// https://github.com/SiaFoundation/core/blob/b5b08cde6b7d0f1b3a6f09b8aa9d0b817e769efb/types/hash.go#L94
Expand Down Expand Up @@ -88,7 +85,7 @@ pub fn sigs_required_leaf(sigs_required: u64) -> H256 {
pub fn public_key_leaf(pubkey: &PublicKey) -> H256 {
let mut combined = Vec::new();
combined.extend_from_slice(&LEAF_HASH_PREFIX);
combined.extend_from_slice(&ED25519_IDENTIFIER);
combined.extend_from_slice(Identifier::Ed25519.as_bytes());
combined.extend_from_slice(&32u64.to_le_bytes());
combined.extend_from_slice(pubkey.as_bytes());
hash_blake2b_single(&combined)
Expand Down Expand Up @@ -281,17 +278,6 @@ fn test_hash_blake2b_pair() {
assert_eq!(hash, expected)
}

#[test]
fn test_create_ed25519_identifier() {
let mut ed25519_identifier: [u8; 16] = [0; 16];

let bytes = "ed25519".as_bytes();
for (i, &byte) in bytes.iter().enumerate() {
ed25519_identifier[i] = byte;
}
assert_eq!(ed25519_identifier, ED25519_IDENTIFIER);
}

#[test]
fn test_timelock_leaf() {
let hash = timelock_leaf(0);
Expand Down
15 changes: 15 additions & 0 deletions mm2src/coins/sia/encoding.rs
Expand Up @@ -8,6 +8,14 @@ pub struct Encoder {
pub buffer: Vec<u8>,
}

pub trait Encodable {
fn encode(&self, encoder: &mut Encoder);
}

impl Encodable for H256 {
fn encode(&self, encoder: &mut Encoder) { encoder.write_slice(&self.0); }
}

impl Encoder {
pub fn reset(&mut self) { self.buffer.clear(); }

Expand All @@ -28,6 +36,13 @@ impl Encoder {
pub fn write_bool(&mut self, b: bool) { self.buffer.push(b as u8) }

pub fn hash(&self) -> H256 { hash_blake2b_single(&self.buffer) }

// Utility method to create, encode, and hash
pub fn encode_and_hash<T: Encodable>(item: &T) -> H256 {
let mut encoder = Encoder::default();
item.encode(&mut encoder);
encoder.hash()
}
}

#[test]
Expand Down