Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

Commit

Permalink
chore: legacy managment for v2 and v3 transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Moustikitos committed Jul 3, 2022
1 parent a1d6928 commit 0792056
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dposlib/ark/builders/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def downVote(*usernames):
necessary supported by targeted dpos blockchain.
Args:
usernames (iterable): delegate usernames as str iterable.
*usernames (iterable): delegate usernames as str iterable.
Returns:
dposlib.ark.tx.Transaction: orphan transaction.
Expand Down
34 changes: 28 additions & 6 deletions dposlib/ark/builders/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def upVote(*usernames, **weights):

remind -= sum([math.trunc(w) for w in weights.values()])
while remind > 0:
weights[usernames[remind]] += 1 # % nb not to necessary...
weights[usernames[remind]] += 1
remind -= 1

sorter_fn = cmp_to_key(
Expand All @@ -91,10 +91,11 @@ def upVote(*usernames, **weights):

def legacyVote(*usernames):
"""
Build an upvote transaction.
Build an upvote transaction. Multiple usernames are allowed but not
necessary supported by targeted dpos blockchain.
Args:
usernames (iterable): delegate usernames as str iterable.
*usernames (iterable): delegate usernames as str iterable.
Returns:
dposlib.ark.tx.Transaction: orphan transaction.
Expand All @@ -115,9 +116,30 @@ def downVote(*usernames):


def switchVote(tx, identifier=None):
raise NotImplementedError(
"switchVote is not implemented for v3 transactions"
)
"""
Transform a [`dposlib.ark.builders.v3.legacyVote`](
v3.md#dposlib.ark.builders.legacyVote
) transaction into a switchVote. It makes the transaction downvote
former delegate if any and then apply new vote.
Arguments:
tx (dposlib.ark.tx.Transaction): upVote transaction.
identifier (dposlib.ark.tx.Transaction): any identifier accepted by
/api/wallets API endpoint. it could be a username, a wallet address
or a publicKey.
Returns:
dposlib.ark.tx.Transaction: orphan transaction.
"""
assert tx["typeGroup"] == 1, \
"switch vote only available for legacy vote transactions"
identifier = identifier or tx["senderPublicKey"]
if identifier is not None:
wallet = rest.GET.api.wallets(identifier, returnKey="data")
usernames = list(wallet.get("votingFor", {}).keys())
for user in [u for u in usernames if u not in tx["asset"]["votes"]]:
tx["asset"]["votes"].insert(0, "-" + user)
return tx
else:
raise Exception("orphan vote transaction can not be set as multivote")


def transfer(*pairs, **kwargs):
Expand Down
20 changes: 16 additions & 4 deletions dposlib/cmd/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def main():

builder_name = builders[args.action]
builder_types = arg_types[builder_name]
if args.net not in ["tsxp"]:
if builder_name == "transfer":
builder_name = "legacyTransfer"
if builder_name == "upVote":
builder_name = "legacyVote"
builder = getattr(dposlib.core, builder_name)

# fee management
Expand Down Expand Up @@ -85,17 +90,24 @@ def main():
try:
for i in range(len(args.args)):
params.append(builder_types[i](args.args[i]))
tx = builder(*params)
if args.action == "vote" and args.net in ["tsxp"]:
if "=" in params[0]:
pairs = [elem.split("=") for elem in params[0].split(",")]
tx = builder(**dict([p[0], float(p[1])] for p in pairs))
else:
tx = builder(*params[0].split(","))
else:
tx = builder(*params)
except Exception as error:
print("Error occured on transaction build... Check command line args.")
print(builder.__doc__)
print("%r" % error)
return 1

# if vote transaction apply switch vote transformation
if args.action == "vote":
# if legacyVote transaction apply switch vote transformation
if tx["type"] == 3 and tx["typeGroup"] == 1:
tx.senderPublicKey = wallet.publicKey
dposlib.core.switchVote(tx)
tx = getattr(dposlib.core, "switchVote")(tx)

# update the vendor field f asked
if args.vendorField is not None:
Expand Down

0 comments on commit 0792056

Please sign in to comment.