Skip to content

Commit d5e82a9

Browse files
committed
fix: WRITE host call + other minor fixes
1 parent 384e1ee commit d5e82a9

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

jam/execution/invocations/functions/accumulate_fns.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
from jam.state.accounts import AccountDataView, DeltaView
32
from jam.state.partial import GhostPartial
43
from jam.types.state.accumulation.types import (
@@ -187,10 +186,7 @@ def new(gas: Gas, registers: list, memory: Memory, context: AccumulationContext,
187186
new_service = AccountData(
188187
service=AccountMetadata(
189188
code_hash=ServiceCodeHash(c),
190-
balance=Balance(BASIC_MINIMUM_BALANCE
191-
+ ADDITIONAL_BALANCE_PER_ITEM * 2
192-
+ ADDITIONAL_BALANCE_PER_OCTET * (81+l) - f
193-
),
189+
balance=Balance(0),
194190
gas_limit=Gas(g),
195191
min_gas=Gas(m),
196192
num_o=Ao(0),
@@ -206,6 +202,9 @@ def new(gas: Gas, registers: list, memory: Memory, context: AccumulationContext,
206202
LookupTable(hash=ServiceCodeHash(c), length=BlobLength(l)): Timestamps([])
207203
})
208204
)
205+
new_service.service.balance = new_service.service.t()
206+
# print("new balance", new_service.service.balance)
207+
209208

210209
if accounts[ServiceId(context.x.s_index)].service.balance < new_service.service.balance:
211210
registers[7] = HostStatus.CASH.value
@@ -261,6 +260,7 @@ def transfer(gas: Gas, registers: list, memory: Memory, context: AccumulationCon
261260
if ServiceId(d) not in delta:
262261
registers[7] = HostStatus.WHO.value
263262
return CONTINUE, gas, registers, memory, context
263+
# print("SERVICE DSDSD", ServiceId(d), context.x.s_index, delta[context.x.s_index].service.balance, Balance(a))
264264

265265
memo = memory.read(o, TRANSFER_MEMO_SIZE)
266266

@@ -271,20 +271,20 @@ def transfer(gas: Gas, registers: list, memory: Memory, context: AccumulationCon
271271
memo=Bytes[128](memo),
272272
gas=Gas(l),
273273
)
274-
275-
new_balance_sender = delta[context.x.s_index].service.balance - Balance(a)
274+
# print("SERVICE BAL", context.x.s_index, delta[context.x.s_index].service.balance, Balance(a))
275+
new_balance_sender = int(delta[context.x.s_index].service.balance) - int(a)
276276

277277
if l < delta[d].service.min_gas:
278278
registers[7] = HostStatus.LOW.value
279279
return CONTINUE, gas, registers, memory, context
280280

281-
if new_balance_sender < delta[context.x.s_index].service.t:
281+
if new_balance_sender < int(delta[context.x.s_index].service.t):
282282
registers[7] = HostStatus.CASH.value
283283
return CONTINUE, gas, registers, memory, context
284284
else:
285285
registers[7] = HostStatus.OK.value
286286
context.x.deferred_transfers.append(t)
287-
delta[context.x.s_index].service.balance = new_balance_sender
287+
delta[context.x.s_index].service.balance = Balance(new_balance_sender)
288288
gas = gas - l
289289
return CONTINUE, gas, registers, memory, context
290290

@@ -309,13 +309,14 @@ def eject(
309309
if d in delta and d != context.x.s_index:
310310
account = delta[d]
311311

312-
l = BlobLength(max(81, account.service.num_o) - 81)
313-
lookup_key = LookupTable(hash=ServiceCodeHash(code_hash), length=BlobLength(l))
314312
if account == None or account.service.code_hash != ServiceCodeHash(list(context.x.s_index.encode()) + [0] * 28):
315313
logger.warning("Eject function called with mismatched code hash for service account")
316314
registers[7] = HostStatus.WHO.value
317315
return CONTINUE, gas, registers, memory, context
318-
elif account.service.num_i != 2 or account.lookup[lookup_key] is None:
316+
l = BlobLength(max(81, account.service.num_o) - 81)
317+
lookup_key = LookupTable(hash=ServiceCodeHash(code_hash), length=BlobLength(l))
318+
319+
if account.service.num_i != 2 or account.lookup[lookup_key] is None:
319320
logger.warning("Eject function called with invalid number of items or missing lookup entry for service account")
320321
registers[7] = HostStatus.HUH.value
321322
return CONTINUE, gas, registers, memory, context

jam/execution/invocations/functions/general_fns.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Optional, List
22

3-
from jam.log_setup import pvm_logger as logger, logger as jam_logger
3+
from jam.log_setup import logger as logger, logger as jam_logger
44
from jam.execution.invocations.functions.protocol import (
55
InvocationFunctions as INVF,
66
)
@@ -80,7 +80,7 @@ def fetch(
8080
item_index: int,
8181
import_segments: Optional[List],
8282
extrinsics: Optional[Extrinsics],
83-
o: Optional[AccumulationInputs]
83+
o: Optional[AccumulationInputs] # bold i
8484
):
8585
fetch_type = registers[10]
8686

@@ -132,21 +132,33 @@ def fetch(
132132
v = trace
133133
elif (
134134
w10 == 3
135-
and item_index is not None
135+
and extrinsics is not None
136136
and w11 < len(extrinsics)
137137
and w12 < len(extrinsics[int(w11)])
138138
):
139139
v = extrinsics[w11][int(w12)]
140-
elif w10 == 4 and item_index is not None and w11 < len(extrinsics[item_index]):
140+
elif (
141+
w10 == 4
142+
and extrinsics is not None
143+
and item_index is not None
144+
and item_index < len(extrinsics)
145+
and w11 < len(extrinsics[item_index])
146+
):
141147
v = extrinsics[item_index][w11]
142148
elif (
143149
w10 == 5
144-
and item_index is not None
150+
and import_segments is not None
145151
and w11 < len(import_segments)
146152
and w12 < len(import_segments[w11])
147153
):
148154
v = import_segments[w11][w12]
149-
elif w10 == 6 and item_index is not None and w11 < len(import_segments[item_index]):
155+
elif (
156+
w10 == 6
157+
and import_segments is not None
158+
and item_index is not None
159+
and item_index < len(import_segments)
160+
and w11 < len(import_segments[item_index])
161+
):
150162
v = import_segments[item_index][w11]
151163
elif package is not None:
152164

@@ -347,7 +359,7 @@ def write(
347359
service_data: AccountData,
348360
service_index: ServiceId,
349361
):
350-
# Get key,value start,end
362+
# Get key, value offset & size
351363
[ko, kz, vo, vz] = registers[7 : 7 + 4]
352364

353365
logger.debug(
@@ -367,25 +379,21 @@ def write(
367379
)
368380
raise PvmError(PANIC)
369381

370-
371-
# TODO: Handle out of balance using temp caches
372-
# from jam.state.state import state
373-
# state.store.save_n_clear_cache()
374-
375382
k = Bytes(memory.read(ko, kz))
376383

377384
a = service_data.storage
378385

379386
curr_value = a.get(k)
380387
storage_len = len(curr_value) if curr_value else HostStatus.NONE.value
388+
381389
if vz == 0:
382390
a.__delitem__(k)
383-
elif memory.is_accessible(vo, vz, Accessibility.WRITE):
391+
elif memory.is_accessible(vo, vz):
384392
pre_data = a[k]
385393
a[k] = Bytes(memory.read(vo, vz))
386394
if service_data.service.t > service_data.service.balance:
387-
# state.store.clear()
388395
registers[7] = HostStatus.FULL.value
396+
# print("Storing FULL val on r[7]", registers[7])
389397
if pre_data is None:
390398
del a[k]
391399
else:
@@ -400,6 +408,7 @@ def write(
400408
)
401409
raise PvmError(PANIC)
402410

411+
# print("Storing s_len val on r[7]", storage_len)
403412
registers[7] = storage_len
404413
return CONTINUE, gas, registers, memory, context
405414

@@ -426,15 +435,14 @@ def info(
426435
if target_service == 2**64 - 1:
427436
target_service = service_index
428437

429-
if target_service > 2**32-1:
430-
registers[7] = HostStatus.NONE.value
431-
return CONTINUE, gas, registers, memory, context
432438

433-
if target_service not in accounts:
439+
if target_service > 2**32-1 or target_service not in accounts:
434440
registers[7] = HostStatus.NONE.value
435441
return CONTINUE, gas, registers, memory, context
436442

443+
# print("TYPE OF SERVICE", target_service, type(target_service))
437444
acc: AccountData = accounts[target_service]
445+
438446
v = (
439447
bytes(acc.service.code_hash)
440448
+ U64(acc.service.balance).encode()

jam/execution/invocations/is_authorized.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ def build_table(self):
3838
def execute(self):
3939
from jam.state.state import state
4040

41+
# pc == pu
4142
_, pc = self.work_package.m_c(state.delta)
4243

43-
if pc == None:
44+
if pc is None:
4445
return HostStatus.BAD, 0
4546
elif len(pc) > MAX_AUTH_CODE_SIZE:
4647
return HostStatus.BIG, 0

jam/execution/invocations/protocol.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def dispatch(
3131
if table_entry is None:
3232
registers[7] = HostStatus.WHAT.value
3333
return ExecutionStatus.CONTINUE, gas - 10, registers, memory, x
34-
34+
35+
# print("dispatching call >> ", host_call)
3536
# Direct unpacking and execution
3637
dispatch_fn_calls, args = table_entry
3738

0 commit comments

Comments
 (0)