Skip to content

Commit

Permalink
Add push override support + custom config (#49)
Browse files Browse the repository at this point in the history
* feat(ffs): override support

* fix: lint
  • Loading branch information
apogiatzis authored Oct 18, 2020
1 parent fc83657 commit 768ef7e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/ffs_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
stage_requests_iter = bytes_to_chunks(test_file)

stage_res = client.ffs.stage(stage_requests_iter, ffs.token)
push_res = client.ffs.push(stage_res.cid, ffs.token)
push_res = client.ffs.push(stage_res.cid, token=ffs.token)
logs_res = client.ffs.logs(stage_res.cid, ffs.token, history=True, timeout=5)

logs = []
Expand Down
20 changes: 15 additions & 5 deletions examples/ffs_push_pull_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@
print("Pushing file to FFS...")

# Push the given file
c.ffs.push(res.cid, ffs.token)
c.ffs.push(res.cid, override=False, token=ffs.token)
# Override push with another config
addresses = c.ffs.addrs_list(ffs.token)
wallet = addresses.addrs[0].addr
new_config = (
'{"hot":{"enabled":true,"allowUnfreeze":true,"ipfs":{"addTimeout":30}},'
'"cold":{"enabled":true,"filecoin":{"repFactor":1,"dealMinDuration":518400,'
'"excludedMiners":["t01101"],"trustedMiners":["t01000","t02000"],'
'"countryCodes":["ca","nl"],"renew":{"enabled":true,"threshold":3},'
'"addr":"' + wallet + '","maxPrice":50}},"repairable":true}'
)
c.ffs.push(res.cid, override=True, config=new_config, token=ffs.token)

# Check that CID is pinned to FFS
check = c.ffs.info(res.cid, ffs.token)
Expand All @@ -38,7 +49,6 @@

# Write to a file on disk
print("Saving as 'README_copy.md'")
f = open("README_copy.MD", "wb")
for f_ in file_:
f.write(f_)
f.close()
with open("README_copy.MD", "wb") as f:
for f_ in file_:
f.write(f_)
19 changes: 16 additions & 3 deletions pygate_grpc/ffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def show(self, cid: str, token: str = None):
req = ffs_rpc_pb2.ShowRequest(cid=cid)
return self.client.Show(req, metadata=self._get_meta_data(token))

def show_all(self, token: str = None):
req = ffs_rpc_pb2.ShowAllRequest()
return self.client.ShowAll(req, metadata=self._get_meta_data(token))

# Note that the chunkIter should be an iterator that yield `ffs_rpc_pb2.AddToHotRequest`,
# it is the caller's responsibility to create the iterator.
# The provided getFileChunks comes in handy some times.
Expand Down Expand Up @@ -139,9 +143,18 @@ def get_storage_job(self, jid: str, token: str = None):
return self.client.GetStorageJob(req, metadata=self._get_meta_data(token))

def push(
self, cid, token: str = None,
self, cid, token: str = None, override: bool = False, config: str = None,
):
req = ffs_rpc_pb2.PushStorageConfigRequest(cid=cid)
if config:
config = Parse(config, ffs_rpc_pb2.StorageConfig())

req = ffs_rpc_pb2.PushStorageConfigRequest(
cid=cid,
override_config=override,
has_override_config=override,
config=config,
has_config=config is not None,
)
return self.client.PushStorageConfig(req, metadata=self._get_meta_data(token))

def close(self, token: str = None):
Expand All @@ -156,7 +169,7 @@ def create_pay_channel(
self, sender: str, receiver: str, amount: int, token: str = None
):
kwargs = {"from": sender, "to": receiver, "amount": amount}
req = ffs_rpc_pb2.CreateRequest(kwargs)
req = ffs_rpc_pb2.CreatePayChannelRequest(**kwargs)
return self.client.CreatePayChannel(req, metadata=self._get_meta_data(token))

def redeem_pay_channel(
Expand Down
23 changes: 14 additions & 9 deletions tests/integration/test_ffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_grpc_ffs_add_then_get_content(pygate_client: PowerGateClient, ffs_insta

assert res is not None

pygate_client.ffs.push(res.cid, ffs_instance.token)
pygate_client.ffs.push(res.cid, token=ffs_instance.token)
f = pygate_client.ffs.get(res.cid, ffs_instance.token)

assert next(f) == b"test_content"
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_ffs_logs(pygate_client: PowerGateClient):
ffs = pygate_client.ffs.create()

stage_res = pygate_client.ffs.stage(chunks(), ffs.token)
pygate_client.ffs.push(stage_res.cid, ffs.token)
pygate_client.ffs.push(stage_res.cid, token=ffs.token)
logs_res = pygate_client.ffs.logs(stage_res.cid, ffs.token, history=True, timeout=5)

logs = []
Expand All @@ -124,30 +124,35 @@ def test_storage_deals(pygate_client: PowerGateClient):
ffs = pygate_client.ffs.create()

stage_res = pygate_client.ffs.stage(chunks(), ffs.token)
pygate_client.ffs.push(stage_res.cid, ffs.token)
pygate_client.ffs.push(stage_res.cid, token=ffs.token)

time.sleep(3)

storage_deals = pygate_client.ffs.list_storage_deal_records(
pygate_client.ffs.list_storage_deal_records(
include_pending=True, include_final=True, token=ffs.token
)

assert len(storage_deals.records) > 0


def test_retrieval_deals(pygate_client: PowerGateClient):
ffs = pygate_client.ffs.create()

stage_res = pygate_client.ffs.stage(chunks(), ffs.token)
pygate_client.ffs.push(stage_res.cid, ffs.token)
pygate_client.ffs.push(stage_res.cid, token=ffs.token)

time.sleep(3)

retrieval_deals = pygate_client.ffs.list_retrieval_deal_records(
pygate_client.ffs.list_retrieval_deal_records(
include_pending=True, include_final=True, token=ffs.token
)

assert len(retrieval_deals.records) == 0

def test_push_override(pygate_client: PowerGateClient):
ffs = pygate_client.ffs.create()

stage_res = pygate_client.ffs.stage(chunks(), ffs.token)
pygate_client.ffs.push(stage_res.cid, token=ffs.token)

pygate_client.ffs.push(stage_res.cid, token=ffs.token, override=True)


def chunks():
Expand Down
27 changes: 0 additions & 27 deletions tests/integration/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,3 @@ def test_grpc_wallet_balance(pygate_client: PowerGateClient):
balance_res = pygate_client.wallet.balance(new_res.address)
assert type(balance_res) is BalanceResponse
assert balance_res.balance == 250000000000000000


def test_send_file(pygate_client: PowerGateClient):
addr = pygate_client.wallet.new()

assert addr is not None
assert addr.address is not None

res = pygate_client.wallet.list()

assert res is not None

sender_addr = res.addresses[0]
receiver_addr = res.addresses[1]

before_sender_fil = pygate_client.wallet.balance(sender_addr)
before_receiver_fil = pygate_client.wallet.balance(receiver_addr)

pygate_client.wallet.send_fil(sender_addr, receiver_addr, 1)

# Wait a bit for transaction to complete
time.sleep(5)
after_sender_fil = pygate_client.wallet.balance(sender_addr)
after_receiver_fil = pygate_client.wallet.balance(receiver_addr)

assert before_sender_fil.balance > after_sender_fil.balance
assert before_receiver_fil.balance < after_receiver_fil.balance

0 comments on commit 768ef7e

Please sign in to comment.