Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 81b4b1a

Browse files
committed
Only assign VMState._chaindb in __init__ + Mutable VMState
1 parent 558bdb4 commit 81b4b1a

File tree

4 files changed

+14
-41
lines changed

4 files changed

+14
-41
lines changed

evm/vm/base.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,21 @@ def apply_transaction(self, transaction):
9696
Apply the transaction to the vm in the current block.
9797
"""
9898
if self.is_stateless:
99-
computation, block, trie_data = self.state.apply_transaction(
99+
computation, block, trie_data = self.get_state_class().apply_transaction(
100100
self.state,
101101
transaction,
102102
self.block,
103103
is_stateless=True,
104-
witness_db=self.chaindb,
105104
)
106105
self.block = block
107106

108-
# Update chaindb
109107
# TODO: Modify Chain.apply_transaction to update the local vm state before
110-
# returning the computation object.
111-
self.chaindb.db.wrapped_db.kv_store.update(
112-
computation.vm_state.chaindb.db.wrapped_db.kv_store,
113-
)
108+
114109
# Persist changed transaction and receipt key-values to self.chaindb.
115110
for key, value in trie_data.items():
116111
self.chaindb.db[key] = value
117112
else:
118-
computation, _, _ = self.state.apply_transaction(
113+
computation, _, _ = self.get_state_class().apply_transaction(
119114
self.state,
120115
transaction,
121116
self.block,
@@ -270,7 +265,6 @@ def create_block(
270265
transaction=transaction,
271266
block=block,
272267
is_stateless=True,
273-
witness_db=witness_db,
274268
)
275269

276270
if not computation.is_error:

evm/vm/forks/frontier/headers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def configure_frontier_header(vm, **header_params):
9090
setattr(vm.block.header, field_name, value)
9191

9292
if 'timestamp' in header_params and vm.block.header.block_number > 0:
93-
# TODO: replace vm.chaindb with vm.state.db?
9493
parent_header = vm.get_parent_header(vm.block.header, vm.chaindb)
9594
vm.block.header.difficulty = compute_frontier_difficulty(
9695
parent_header,

evm/vm_state.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def gas_limit(self):
8080
#
8181
# chaindb
8282
#
83-
@property
84-
def chaindb(self):
85-
return self._chaindb
83+
# @property
84+
# def chaindb(self):
85+
# return self._chaindb
8686

8787
def set_chaindb(self, db):
8888
self._chaindb = db
@@ -205,37 +205,26 @@ def apply_transaction(
205205
vm_state,
206206
transaction,
207207
block,
208-
is_stateless=True,
209-
witness_db=None):
208+
is_stateless=True):
210209
"""
211210
Apply transaction to the given block
212211
213212
:param vm_state: the VMState object
214213
:param transaction: the transaction need to be applied
215214
:param block: the block which the transaction applies on
216215
:param is_stateless: if is_stateless, call VMState.add_transactionto set block
217-
:param witness_db: for stateless mode, use witness_db as chaindb
218216
:type vm_state: VMState
219217
:type transaction: Transaction
220218
:type block: Block
221219
:type is_stateless: bool
222-
:type witness_db: BaseChainDB
223220
224221
:return: the computation, applied block, and the trie_data
225222
:rtype: (Computation, Block, dict[bytes, bytes])
226223
"""
227224
if is_stateless:
228-
# Update block in this level.
229-
assert witness_db is not None
230-
231-
# Don't change the given vm_state, block, and witness_db
232-
vm_state = copy.deepcopy(vm_state)
225+
# Don't modify the given block
233226
block = copy.deepcopy(block)
234-
witness_db = copy.deepcopy(witness_db)
235-
236-
vm_state.set_chaindb(witness_db)
237-
cls.block_header = block.header
238-
227+
vm_state.block_header = block.header
239228
computation, block_header = cls.execute_transaction(vm_state, transaction)
240229

241230
# Set block.

tests/core/vm/test_vm_state.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
6464

6565
# Don't change these variables
6666
vm = chain.get_vm()
67-
vm._is_stateless = False # Only for testing
6867
chaindb = copy.deepcopy(vm.chaindb)
6968
block0 = copy.deepcopy(vm.block)
7069
prev_block_hash = chain.get_canonical_block_by_number(0).hash
@@ -74,6 +73,7 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
7473
# The first transaction
7574
chain1 = copy.deepcopy(chain)
7675
vm_example = chain1.get_vm()
76+
vm_example._is_stateless = False # Only for testing
7777
recipient1 = decode_hex('0x1111111111111111111111111111111111111111')
7878
amount = 100
7979
from_ = chain.funded_address
@@ -119,18 +119,16 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
119119
vm_state1,
120120
tx1,
121121
block1,
122-
witness_db=chaindb1,
123122
)
124123
access_logs1 = computation.vm_state.access_logs
125124

126125
# Check if prev_headers hasn't been changed
127126
assert parent_header.hash == prev_headers[0].hash
128-
129-
for key, value in enumerate(access_logs1.writes):
130-
chaindb1.db[key] = value
127+
# Make sure that block1 hasn't been changed
128+
assert block1.header.state_root == initial_state_root
131129

132130
vm_state1 = FrontierVMState(
133-
chaindb=BaseChainDB(MemoryDB()),
131+
chaindb=chaindb1,
134132
block_header=block.header,
135133
prev_headers=prev_headers,
136134
receipts=computation.vm_state.receipts,
@@ -139,7 +137,6 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
139137
vm_state1,
140138
tx2,
141139
block,
142-
witness_db=computation.vm_state.chaindb,
143140
)
144141
access_logs2 = computation.vm_state.access_logs
145142
post_vm_state = computation.vm_state
@@ -158,12 +155,8 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
158155
assert block.header.transaction_root == result_block.header.transaction_root
159156
assert block.header.receipt_root == result_block.header.receipt_root
160157

161-
# Make sure that block1 hasn't been changed
162-
assert block1.header.state_root == initial_state_root
163-
164158
# Make sure that vm_state1 hasn't been changed
165159
assert post_vm_state.block_header.state_root == result_block.header.state_root
166-
assert post_vm_state.block_header.state_root != vm_state1.block_header.state_root
167160

168161
# (3) Testing using witness as db data
169162
# Witness_db
@@ -187,7 +180,6 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
187180
vm_state2,
188181
tx1,
189182
block2,
190-
witness_db=witness_db,
191183
)
192184

193185
# Update witness_db
@@ -196,7 +188,7 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
196188

197189
# Apply the second transaction
198190
vm_state2 = FrontierVMState(
199-
chaindb=BaseChainDB(MemoryDB()),
191+
chaindb=witness_db,
200192
block_header=block.header,
201193
prev_headers=prev_headers,
202194
receipts=computation.vm_state.receipts,
@@ -205,7 +197,6 @@ def test_apply_transaction(chain_without_block_validation): # noqa: F811
205197
vm_state2,
206198
tx2,
207199
block,
208-
witness_db=witness_db,
209200
)
210201

211202
# After applying

0 commit comments

Comments
 (0)