make it pin all existing packs in the contract

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2020-06-15 16:57:15 +02:00
parent 1d2bdfc741
commit f012289b99
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
3 changed files with 24 additions and 16 deletions

19
ipfs.py
View File

@ -4,15 +4,6 @@ import requests
import content_hash # https://pypi.org/project/content-hash import content_hash # https://pypi.org/project/content-hash
import ipfscluster # https://pypi.org/project/ipfscluster import ipfscluster # https://pypi.org/project/ipfscluster
# TODO parametrize
IPFS_CLUSTER_ADDR = 'http://localhost:9094'
# TODO parametrize
STICKER_PACKS_META_URLS = [
"https://cloudflare-ipfs.com/ipfs/QmWVVLwVKCwkVNjYJrRzQWREVvEk917PhbHYAUhA1gECTM",
"https://cloudflare-ipfs.com/ipfs/QmWpG2Q5NB472KLgFysdCjB8D1Qf9hxR2KNJvtCJQJufDj",
]
# Converts binary content hash to text verison, see EIP-1577 # Converts binary content hash to text verison, see EIP-1577
def ipfsBinToText(text): def ipfsBinToText(text):
return content_hash.decode(text) return content_hash.decode(text)
@ -22,10 +13,14 @@ class IpfsPinner:
def __init__(self, addr=ipfscluster.DEFAULT_ADDR): def __init__(self, addr=ipfscluster.DEFAULT_ADDR):
self.client = ipfscluster.connect(addr) self.client = ipfscluster.connect(addr)
def is_pinned(self, chash): def statuses(self, chash):
resp = self.client.pins.ls(chash) resp = self.client.pins.ls(chash)
statuses = [peer['status'] for peer in resp['peer_map'].values()] return [peer['status'] for peer in resp['peer_map'].values()]
return all(s == 'pinned' for s in statuses), statuses
def is_pinned(self, chash):
return all(s == 'pinned' for s in self.statuses(chash))
def pin(self, chash): def pin(self, chash):
if self.is_pinned(chash):
return True
return self.client.pins.add(chash) return self.client.pins.add(chash)

17
main.py
View File

@ -9,15 +9,24 @@ from ipfs import IpfsPinner
from pack import StickerPack, ipfsBinToText from pack import StickerPack, ipfsBinToText
from contract import StickerPackContract from contract import StickerPackContract
STICKER_PACK_CONTRACT = "0x0577215622f43a39F4Bc9640806DFea9b10D2A36" SPACK_CONTRACT = "0x0577215622f43a39F4Bc9640806DFea9b10D2A36"
with open("./abi.json", "r") as f: with open("./abi.json", "r") as f:
STICKER_PACK_ABI = json.load(f) STICKER_PACK_ABI = json.load(f)
w3 = Web3(Web3.HTTPProvider("http://localhost:8545")) w3 = Web3(Web3.HTTPProvider("http://localhost:8545"))
s = StickerPackContract(STICKER_PACK_CONTRACT, STICKER_PACK_ABI, w3) ipfs = IpfsPinner()
pack_hashes = s.getAllPackHashes() sPack = StickerPackContract(SPACK_CONTRACT, STICKER_PACK_ABI, w3)
pack_hashes = sPack.getAllPackHashes()
packs = [StickerPack(ipfsBinToText(h)) for h in pack_hashes] packs = [StickerPack(ipfsBinToText(h)) for h in pack_hashes]
print(packs)
for pack in packs:
print("pack:", pack)
rval = ipfs.pin(pack.content_hash)
for chash in pack.image_hashes:
print("image:", chash)
ipfs.pin(pack.content_hash)
print("rval:", rval)

View File

@ -24,3 +24,7 @@ class StickerPack:
matches = re.findall(StickerPack.content_hash_rgx, data) matches = re.findall(StickerPack.content_hash_rgx, data)
# IPFS can't handle EIP-1577 content hashes # IPFS can't handle EIP-1577 content hashes
return [ipfsBinToText(ch) for ch in matches] return [ipfsBinToText(ch) for ch in matches]
def __repr__(self):
return '<Pack hash={} imgs={}>'.format(
self.content_hash, len(self.image_hashes))