mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2024-11-13 12:09:25 +03:00
Rewrite "ROM" in clvm, including deserialization.
This commit is contained in:
parent
03304d22dc
commit
44ba53550d
@ -1,14 +1,13 @@
|
||||
import logging
|
||||
from typing import List, Optional, Union, Tuple
|
||||
from chia.types.blockchain_format.program import Program, SerializedProgram, NIL
|
||||
from chia.types.blockchain_format.program import Program, SerializedProgram
|
||||
from chia.types.generator_types import BlockGenerator, GeneratorArg, GeneratorBlockCacheInterface, CompressorArg
|
||||
from chia.util.ints import uint32, uint64
|
||||
from chia.wallet.puzzles.load_clvm import load_clvm
|
||||
from chia.wallet.puzzles.lowlevel_generator import get_generator
|
||||
from chia.wallet.puzzles.rom_bootstrap_generator import get_generator
|
||||
|
||||
GENERATOR_MOD = get_generator()
|
||||
|
||||
DESERIALIZE_MOD = load_clvm("chialisp_deserialisation.clvm", package_or_requirement="chia.wallet.puzzles")
|
||||
DECOMPRESS_BLOCK = load_clvm("block_program_zero.clvm", package_or_requirement="chia.wallet.puzzles")
|
||||
DECOMPRESS_PUZZLE = load_clvm("decompress_puzzle.clvm", package_or_requirement="chia.wallet.puzzles")
|
||||
# DECOMPRESS_CSE = load_clvm("decompress_coin_solution_entry.clvm", package_or_requirement="chia.wallet.puzzles")
|
||||
@ -39,7 +38,7 @@ def create_generator_args(generator_ref_list: List[SerializedProgram]) -> Progra
|
||||
"""
|
||||
gen_ref_list = [bytes(g) for g in generator_ref_list]
|
||||
gen_ref_tree = list_to_tree(gen_ref_list)
|
||||
return Program.to([DESERIALIZE_MOD, gen_ref_tree])
|
||||
return Program.to([gen_ref_tree])
|
||||
|
||||
|
||||
def create_compressed_generator(
|
||||
@ -60,10 +59,7 @@ def create_compressed_generator(
|
||||
|
||||
|
||||
def setup_generator_args(self: BlockGenerator):
|
||||
if not self.generator_args:
|
||||
args = NIL
|
||||
else:
|
||||
args = create_generator_args(self.generator_refs())
|
||||
args = create_generator_args(self.generator_refs())
|
||||
return self.program, args
|
||||
|
||||
|
||||
|
@ -15,7 +15,7 @@ from chia.util.condition_tools import ConditionOpcode, conditions_by_opcode
|
||||
from chia.util.errors import Err
|
||||
from chia.util.ints import uint32, uint64, uint16
|
||||
from chia.wallet.puzzles.generator_loader import GENERATOR_FOR_SINGLE_COIN_MOD
|
||||
from chia.wallet.puzzles.lowlevel_generator import get_generator
|
||||
from chia.wallet.puzzles.rom_bootstrap_generator import get_generator
|
||||
|
||||
GENERATOR_MOD = get_generator()
|
||||
|
||||
|
@ -1,45 +0,0 @@
|
||||
from clvm_tools import binutils
|
||||
|
||||
from chia.types.blockchain_format.program import Program, SerializedProgram
|
||||
from chia.wallet.chialisp import args, cons, eval, make_if, make_list, quote, rest, sexp, sha256tree
|
||||
|
||||
|
||||
def get_generator():
|
||||
|
||||
# args0 is generate_npc_pair_list, args1 is coin_solutions, args2 is output_list
|
||||
programs = args(0)
|
||||
coin_solutions = args(1)
|
||||
output_list = args(2)
|
||||
# coin_solution = first(coin_solutions)
|
||||
# coin_name = first(coin_solution)
|
||||
# puzzle_solution_pair = first(rest(coin_solution))
|
||||
# puzzle = first(puzzle_solution_pair)
|
||||
# solution = first(rest(puzzle_solution_pair))
|
||||
# coin_tuple = args(0, 0, 1)
|
||||
coin_parent = args(0, 0, 0, 1)
|
||||
coin_amount = args(1, 0, 0, 1)
|
||||
coin_puzzle = args(0, 1, 0, 1)
|
||||
coin_solution = args(1, 1, 0, 1)
|
||||
|
||||
get_npc = make_list(make_list(coin_parent, sha256tree(coin_puzzle), coin_amount), eval(coin_puzzle, coin_solution))
|
||||
|
||||
recursive_call = eval(programs, make_list(programs, rest(coin_solutions), cons(get_npc, output_list)))
|
||||
|
||||
generate_npc_pair_list = make_if(coin_solutions, recursive_call, output_list)
|
||||
|
||||
# Run the block_program and enter loop over the results
|
||||
# args0 is generate_npc_pair_list, args1 is block_program being passed in
|
||||
|
||||
programs = args(0)
|
||||
coin_solutions = args(1)
|
||||
execute_generate_npc_pair = eval(programs, make_list(programs, coin_solutions, sexp()))
|
||||
|
||||
# Bootstrap the execution by passing functions in as parameters before the actual data arguments
|
||||
get_coinsols = eval(args(0), args(1))
|
||||
core = eval(quote(execute_generate_npc_pair), make_list(quote(generate_npc_pair_list), get_coinsols))
|
||||
|
||||
# The below string is exactly the same as the value of 'core' above, except '(r 5)' is replaced with '13'
|
||||
# test = "(a (q . (a 2 (c 2 (c 5 (c () ()))))) (c (q . (a (i 5 (q . (a 2 (c 2 (c 13 (c (c (c 17 (c (a (q . (a 2 (c 2 (c 3 0)))) (c (q . (a (i (l 5) (q . (sha256 (q . 2) (a 2 (c 2 (c 9 0))) (a 2 (c 2 (c 13 0))))) (q . (sha256 (q . 1) 5))) 1)) 73)) (c (a 73 169) ()))) 11) ()))))) (q . 11)) 1)) (c (a 2 5) ())))" # noqa
|
||||
ret = SerializedProgram.from_bytes(bytes(Program.to(binutils.assemble(core))))
|
||||
|
||||
return ret
|
33
chia/wallet/puzzles/rom_bootstrap_generator.clvm
Normal file
33
chia/wallet/puzzles/rom_bootstrap_generator.clvm
Normal file
@ -0,0 +1,33 @@
|
||||
(mod (block_decompresser_program (historical_blocks_tree))
|
||||
|
||||
(defconstant local_deserialize_mod
|
||||
;; this monstrosity is the assembly output of `chialisp_deserialisation.clvm`
|
||||
;; it's pasted in here because the compiler doesn't yet support nested `mod`
|
||||
;; my apologies -- RK
|
||||
|
||||
(a (q 5 (a 62 (c 2 (c 5 ()))))
|
||||
(c (q ((-1 . 127) -33 . -65) ((a (i (= 11 (q . -128)) (q 4 () (c 5 ())) (q 2 (i (>s 11 24) (q 2 26 (c 2 (c (a (i (>s 11 28) (q 2 (i (>s 11 20) (q 8) (q 4 (concat (logand (q . 31) 11) (substr 5 () (q . 1))) (c (substr 5 (q . 1)) ()))) 1) (q 4 (logand (q . 63) 11) (c 5 ()))) 1) ()))) (q 4 11 (c 5 ()))) 1)) 1) 4 (substr 21 () 9) (c (substr 21 9) ())) (c (c 5 19) (c 43 ())) (a 22 (c 2 (c 9 (c (a 62 (c 2 (c 21 ()))) ())))) 2 (i (= (substr 5 () (q . 1)) 16) (q 2 46 (c 2 (c (a 62 (c 2 (c (substr 5 (q . 1)) ()))) ()))) (q 2 18 (c 2 (c (substr 5 (q . 1)) (c (substr 5 () (q . 1)) ()))))) 1)
|
||||
1))
|
||||
)
|
||||
|
||||
(defun sha256tree
|
||||
(TREE)
|
||||
(if (l TREE)
|
||||
(sha256 2 (sha256tree (f TREE)) (sha256tree (r TREE)))
|
||||
(sha256 1 TREE)
|
||||
)
|
||||
)
|
||||
|
||||
(defun process_coin_solution (((parent amount) (puzzle solution)))
|
||||
(list (list parent (sha256tree puzzle) amount) (a puzzle solution))
|
||||
)
|
||||
|
||||
(defun recurse (coin_solutions)
|
||||
(if coin_solutions
|
||||
(c (process_coin_solution (f coin_solutions)) (recurse (r coin_solutions)))
|
||||
0
|
||||
)
|
||||
)
|
||||
|
||||
(recurse (a block_decompresser_program (list local_deserialize_mod historical_blocks_tree)))
|
||||
)
|
1
chia/wallet/puzzles/rom_bootstrap_generator.clvm.hex
Normal file
1
chia/wallet/puzzles/rom_bootstrap_generator.clvm.hex
Normal file
@ -0,0 +1 @@
|
||||
ff02ffff01ff02ff0affff04ff02ffff04ffff02ff05ffff04ff08ffff04ff13ff80808080ff80808080ffff04ffff01ffffff02ffff01ff05ffff02ff3effff04ff02ffff04ff05ff8080808080ffff04ffff01ffffff81ff7fff81df81bfffffff02ffff03ffff09ff0bffff01818080ffff01ff04ff80ffff04ff05ff808080ffff01ff02ffff03ffff0aff0bff1880ffff01ff02ff1affff04ff02ffff04ffff02ffff03ffff0aff0bff1c80ffff01ff02ffff03ffff0aff0bff1480ffff01ff0880ffff01ff04ffff0effff18ffff011fff0b80ffff0cff05ff80ffff01018080ffff04ffff0cff05ffff010180ff80808080ff0180ffff01ff04ffff18ffff013fff0b80ffff04ff05ff80808080ff0180ff80808080ffff01ff04ff0bffff04ff05ff80808080ff018080ff0180ff04ffff0cff15ff80ff0980ffff04ffff0cff15ff0980ff808080ffff04ffff04ff05ff1380ffff04ff2bff808080ffff02ff16ffff04ff02ffff04ff09ffff04ffff02ff3effff04ff02ffff04ff15ff80808080ff8080808080ff02ffff03ffff09ffff0cff05ff80ffff010180ff1080ffff01ff02ff2effff04ff02ffff04ffff02ff3effff04ff02ffff04ffff0cff05ffff010180ff80808080ff80808080ffff01ff02ff12ffff04ff02ffff04ffff0cff05ffff010180ffff04ffff0cff05ff80ffff010180ff808080808080ff0180ff018080ff04ffff04ff11ffff04ffff02ff0effff04ff02ffff04ff25ff80808080ffff04ff29ff80808080ffff04ffff02ff25ff5580ff808080ffff02ffff03ff05ffff01ff04ffff02ff0cffff04ff02ffff04ff09ff80808080ffff02ff0affff04ff02ffff04ff0dff8080808080ff8080ff0180ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff0effff04ff02ffff04ff09ff80808080ffff02ff0effff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080
|
@ -0,0 +1 @@
|
||||
352859e971feb2bbea735c6fa05f06d6c30c495fa789bf4ec7310fd4c11a95d4
|
9
chia/wallet/puzzles/rom_bootstrap_generator.py
Normal file
9
chia/wallet/puzzles/rom_bootstrap_generator.py
Normal file
@ -0,0 +1,9 @@
|
||||
from chia.types.blockchain_format.program import SerializedProgram
|
||||
|
||||
from .load_clvm import load_clvm
|
||||
|
||||
MOD = SerializedProgram.from_bytes(load_clvm("rom_bootstrap_generator.clvm").as_bin())
|
||||
|
||||
|
||||
def get_generator():
|
||||
return MOD
|
@ -10,7 +10,7 @@ wallet_program_files = set(
|
||||
"chia/wallet/puzzles/calculate_synthetic_public_key.clvm",
|
||||
"chia/wallet/puzzles/cc.clvm",
|
||||
"chia/wallet/puzzles/chialisp_deserialisation.clvm",
|
||||
# "chia/wallet/puzzles/generator.clvm", - see chia/wallet/puzzles/lowlevel_generator.py
|
||||
"chia/wallet/puzzles/rom_bootstrap_generator.clvm",
|
||||
"chia/wallet/puzzles/generator_for_single_coin.clvm",
|
||||
"chia/wallet/puzzles/genesis-by-coin-id-with-0.clvm",
|
||||
"chia/wallet/puzzles/genesis-by-puzzle-hash-with-0.clvm",
|
||||
|
@ -201,7 +201,12 @@ class TestDecompression(TestCase):
|
||||
print(out)
|
||||
|
||||
p_with_cses = DECOMPRESS_BLOCK.curry(
|
||||
DECOMPRESS_PUZZLE, DECOMPRESS_CSE_WITH_PREFIX, start, Program.to(end), cse2
|
||||
DECOMPRESS_PUZZLE,
|
||||
DECOMPRESS_CSE_WITH_PREFIX,
|
||||
start,
|
||||
Program.to(end),
|
||||
cse2,
|
||||
DESERIALIZE_MOD,
|
||||
)
|
||||
generator_args = create_generator_args([SerializedProgram.from_bytes(original_generator)])
|
||||
cost, out = p_with_cses.run_with_cost(INFINITE_COST, generator_args)
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import Dict
|
||||
from unittest import TestCase
|
||||
|
||||
from chia.types.blockchain_format.program import Program, SerializedProgram, INFINITE_COST
|
||||
from chia.types.blockchain_format.program import Program, SerializedProgram
|
||||
from chia.types.generator_types import GeneratorBlockCacheInterface
|
||||
from chia.full_node.generator import create_block_generator, create_generator_args, list_to_tree
|
||||
from chia.util.ints import uint32
|
||||
@ -44,18 +44,8 @@ class TestGeneratorTypes(TestCase):
|
||||
gen_args = create_generator_args(generator_ref_list)
|
||||
gen_args_as_program = Program.from_bytes(bytes(gen_args))
|
||||
|
||||
d = gen_args_as_program.first()
|
||||
|
||||
# First argument: clvm deserializer
|
||||
|
||||
b = bytes.fromhex("ff8568656c6c6fff86667269656e6480") # ("hello" "friend")
|
||||
cost, output = d.run_with_cost(INFINITE_COST, [b])
|
||||
# print(cost, output)
|
||||
out = Program.to(output)
|
||||
assert out == Program.from_bytes(b)
|
||||
|
||||
# Second Argument to the block generator is the first template generator
|
||||
arg2 = gen_args_as_program.rest().first()
|
||||
# First Argument to the block generator is the first template generator
|
||||
arg2 = gen_args_as_program.first()
|
||||
print(arg2)
|
||||
assert arg2 == bytes(gen1)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user