Skip to content

Commit

Permalink
update to cvppairs extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-o-how authored and Yostra committed Jan 12, 2021
1 parent 3fd5227 commit c7f4128
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ vdf_bench
# Offer Files
*.offer

# Backup Files
*.backup

# Compiled CLVM
main.sym

Expand Down
2 changes: 1 addition & 1 deletion src/full_node/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ async def _validate_transactions(
ConditionOpcode.ASSERT_FEE
]
for cvp in fee_list:
fee = int_from_bytes(cvp.var1)
fee = int_from_bytes(cvp.vars[0])
assert_fee_sum = assert_fee_sum + fee

if fees < assert_fee_sum:
Expand Down
10 changes: 5 additions & 5 deletions src/full_node/blockchain_check_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def blockchain_assert_coin_consumed(
Checks coin consumed conditions
Returns None if conditions are met, if not returns the reason why it failed
"""
coin_name = condition.var1
coin_name = condition.vars[0]
if coin_name not in removed:
return Err.ASSERT_COIN_CONSUMED_FAILED
return None
Expand All @@ -30,7 +30,7 @@ def blockchain_assert_my_coin_id(
"""
Checks if CoinID matches the id from the condition
"""
if unspent.coin.name() != condition.var1:
if unspent.coin.name() != condition.vars[0]:
return Err.ASSERT_MY_COIN_ID_FAILED
return None

Expand All @@ -42,7 +42,7 @@ def blockchain_assert_block_index_exceeds(
Checks if the next block index exceeds the block index from the condition
"""
try:
expected_block_index = int_from_bytes(condition.var1)
expected_block_index = int_from_bytes(condition.vars[0])
except ValueError:
return Err.INVALID_CONDITION
# + 1 because min block it can be included is +1 from current
Expand All @@ -58,7 +58,7 @@ def blockchain_assert_block_age_exceeds(
Checks if the coin age exceeds the age from the condition
"""
try:
expected_block_age = int_from_bytes(condition.var1)
expected_block_age = int_from_bytes(condition.vars[0])
expected_block_index = expected_block_age + unspent.confirmed_block_index
except ValueError:
return Err.INVALID_CONDITION
Expand All @@ -72,7 +72,7 @@ def blockchain_assert_time_exceeds(condition: ConditionVarPair):
Checks if current time in millis exceeds the time specified in condition
"""
try:
expected_mili_time = int_from_bytes(condition.var1)
expected_mili_time = int_from_bytes(condition.vars[0])
except ValueError:
return Err.INVALID_CONDITION

Expand Down
12 changes: 6 additions & 6 deletions src/full_node/mempool_check_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def mempool_assert_coin_consumed(
Returns None if conditions are met, if not returns the reason why it failed
"""
bundle_removals = spend_bundle.removal_names()
coin_name = condition.var1
coin_name = condition.vars[0]
if coin_name not in bundle_removals:
return Err.ASSERT_COIN_CONSUMED_FAILED
return None
Expand All @@ -40,7 +40,7 @@ def mempool_assert_my_coin_id(
"""
Checks if CoinID matches the id from the condition
"""
if unspent.coin.name() != condition.var1:
if unspent.coin.name() != condition.vars[0]:
return Err.ASSERT_MY_COIN_ID_FAILED
return None

Expand All @@ -52,7 +52,7 @@ def mempool_assert_block_index_exceeds(
Checks if the next block index exceeds the block index from the condition
"""
try:
expected_block_index = int_from_bytes(condition.var1)
expected_block_index = int_from_bytes(condition.vars[0])
except ValueError:
return Err.INVALID_CONDITION
# + 1 because min block it can be included is +1 from current
Expand All @@ -68,7 +68,7 @@ def mempool_assert_block_age_exceeds(
Checks if the coin age exceeds the age from the condition
"""
try:
expected_block_age = int_from_bytes(condition.var1)
expected_block_age = int_from_bytes(condition.vars[0])
expected_block_index = expected_block_age + unspent.confirmed_block_index
except ValueError:
return Err.INVALID_CONDITION
Expand All @@ -82,7 +82,7 @@ def mempool_assert_time_exceeds(condition: ConditionVarPair):
Check if the current time in millis exceeds the time specified by condition
"""
try:
expected_mili_time = int_from_bytes(condition.var1)
expected_mili_time = int_from_bytes(condition.vars[0])
except ValueError:
return Err.INVALID_CONDITION

Expand Down Expand Up @@ -166,7 +166,7 @@ def get_name_puzzle_conditions2(block_program):
if len(cond) == 3:
cvp = ConditionVarPair(opcode, cond[1], cond[2])
else:
cvp = ConditionVarPair(opcode, cond[1], None)
cvp = ConditionVarPair(opcode, cond[1])
conditions_list.append(cvp)
conditions_dict = conditions_by_opcode(conditions_list)
if conditions_dict is None:
Expand Down
2 changes: 1 addition & 1 deletion src/full_node/mempool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ async def add_spendbundle(
ConditionOpcode.ASSERT_FEE
]
for cvp in fee_list:
fee = int_from_bytes(cvp.var1)
fee = int_from_bytes(cvp.vars[0])
assert_fee_sum = assert_fee_sum + fee

if fees < assert_fee_sum:
Expand Down
20 changes: 17 additions & 3 deletions src/types/condition_var_pair.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional
from typing import Optional, List

from src.types.condition_opcodes import ConditionOpcode

Expand All @@ -12,5 +12,19 @@ class ConditionVarPair:
"""

opcode: ConditionOpcode
var1: Optional[bytes]
var2: Optional[bytes]
vars: List[bytes]

def __init__(
self,
opc: ConditionOpcode,
*args
):
i = 0
var_list = []
for arg in args:
if arg is not None:
assert isinstance(arg, bytes)
var_list.append(bytes(arg))
i = i + 1
object.__setattr__(self, "opcode", opc)
object.__setattr__(self, "vars", var_list)
8 changes: 4 additions & 4 deletions src/util/condition_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ def pkm_pairs_for_conditions_dict(
for cvp in conditions_dict.get(ConditionOpcode.AGG_SIG, []):
# TODO: check types
# assert len(_) == 3
assert cvp.var2 is not None
ret.append((G1Element.from_bytes(cvp.var1), cvp.var2))
assert cvp.vars[1] is not None
ret.append((G1Element.from_bytes(cvp.vars[0]), cvp.vars[1]))
if coin_name is not None:
for cvp in conditions_dict.get(ConditionOpcode.AGG_SIG_ME, []):
ret.append((G1Element.from_bytes(cvp.var1), cvp.var2 + coin_name))
ret.append((G1Element.from_bytes(cvp.vars[0]), cvp.vars[1] + coin_name))
return ret


Expand All @@ -102,7 +102,7 @@ def created_outputs_for_conditions_dict(
# (ensure there are the correct number and type of parameters)
# maybe write a type-checking framework for conditions
# and don't just fail with asserts
puzzle_hash, amount_bin = cvp.var1, cvp.var2
puzzle_hash, amount_bin = cvp.vars[0], cvp.vars[1]
amount = int_from_bytes(amount_bin)
coin = Coin(input_coin_name, puzzle_hash, amount)
output_coins.append(coin)
Expand Down
18 changes: 9 additions & 9 deletions src/util/wallet_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,21 @@ def make_solution(
for con_list in condition_dic.values():
for cvp in con_list:
if cvp.opcode == ConditionOpcode.CREATE_COIN:
ret.append(make_create_coin_condition(cvp.var1, cvp.var2))
ret.append(make_create_coin_condition(cvp.vars[0], cvp.vars[1]))
if cvp.opcode == ConditionOpcode.AGG_SIG:
ret.append(make_assert_aggsig_condition(cvp.var1))
ret.append(make_assert_aggsig_condition(cvp.vars[0]))
if cvp.opcode == ConditionOpcode.ASSERT_COIN_CONSUMED:
ret.append(make_assert_coin_consumed_condition(cvp.var1))
ret.append(make_assert_coin_consumed_condition(cvp.vars[0]))
if cvp.opcode == ConditionOpcode.ASSERT_TIME_EXCEEDS:
ret.append(make_assert_time_exceeds_condition(cvp.var1))
ret.append(make_assert_time_exceeds_condition(cvp.vars[0]))
if cvp.opcode == ConditionOpcode.ASSERT_MY_COIN_ID:
ret.append(make_assert_my_coin_id_condition(cvp.var1))
ret.append(make_assert_my_coin_id_condition(cvp.vars[0]))
if cvp.opcode == ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS:
ret.append(make_assert_block_index_exceeds_condition(cvp.var1))
ret.append(make_assert_block_index_exceeds_condition(cvp.vars[0]))
if cvp.opcode == ConditionOpcode.ASSERT_BLOCK_AGE_EXCEEDS:
ret.append(make_assert_block_age_exceeds_condition(cvp.var1))
ret.append(make_assert_block_age_exceeds_condition(cvp.vars[0]))
if cvp.opcode == ConditionOpcode.ASSERT_FEE:
ret.append(make_assert_fee_condition(cvp.var1))
ret.append(make_assert_fee_condition(cvp.vars[0]))

return solution_for_conditions(Program.to(ret))

Expand Down Expand Up @@ -146,7 +146,7 @@ def generate_unsigned_transaction(
)
condition_dic[output.opcode].append(output)
amount_total = sum(
int_from_bytes(cvp.var2)
int_from_bytes(cvp.vars[1])
for cvp in condition_dic[ConditionOpcode.CREATE_COIN]
)
change = spend_value - amount_total - fee
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/cc_wallet/cc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def spend_bundle_for_spendable_ccs(
total = 0
if conditions:
for _ in conditions.get(ConditionOpcode.CREATE_COIN, []):
total += Program.to(_.var2).as_int()
total += Program.to(_.vars[1]).as_int()
output_amounts.append(total)

coin_solutions = []
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/cc_wallet/cc_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ async def search_for_parent_info(
continue
for cvp in created_output_conditions:
result = await self.wallet_state_manager.puzzle_store.wallet_info_for_puzzle_hash(
cvp.var1
cvp.vars[0]
)
if result is None:
continue
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/cc_wallet/debug_spend_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def debug_spend_bundle(spend_bundle: SpendBundle) -> None:
for condition_programs in conditions.values():
print()
for c in condition_programs:
as_prog = Program.to([c.opcode, c.var1, c.var2])
as_prog = Program.to([c.opcode, c.vars[0], c.vars[1]])
print(f" {disassemble(as_prog)}")
print()
for _ in conditions.get(ConditionOpcode.ASSERT_COIN_CONSUMED, []):
assert_consumed_set.add(bytes32(_.var1))
assert_consumed_set.add(bytes32(c.vars[0]))
else:
print("(no output conditions generated)")
print()
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/util/trade_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_output_amount_for_puzzle_and_solution(puzzle, solution):
total = 0
if conditions:
for _ in conditions.get(ConditionOpcode.CREATE_COIN, []):
total += Program.to(_.var2).as_int()
total += Program.to(_.vars[1]).as_int()
return total


Expand Down

0 comments on commit c7f4128

Please sign in to comment.