Skip to content

Commit

Permalink
Merge pull request #128 from 0xPolygonHermez/feature/access-storage
Browse files Browse the repository at this point in the history
Access storage refactor + depth
  • Loading branch information
krlosMata authored Sep 20, 2022
2 parents d503a0b + ac7ef3e commit 1370cfe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
54 changes: 37 additions & 17 deletions main/opcodes.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ opSTOPend:
$ => PC :MLOAD(lastPC)
D :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
$ => A :MLOAD(depth)
A - 1 :MSTORE(depth)
${commit()}
:JMP(readCode)

opADD:
Expand Down Expand Up @@ -744,7 +747,7 @@ opBALANCE:
$ => D :SLOAD
D :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
${touchedAddress(A)} => D
${isWarmedAddress(A)} => D
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:JMP(readCode)

Expand Down Expand Up @@ -1093,7 +1096,7 @@ opEXTCODESIZE:
SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
:CALL(maskAddress) ; Mask address to 20 bytes
${touchedAddress(A)} => D
${isWarmedAddress(A)} => D
%SMT_KEY_SC_LENGTH => B
0 => C
$ => A :SLOAD
Expand All @@ -1120,7 +1123,7 @@ opEXTCODECOPY:
SP - 1 => SP
$ => A :MLOAD(SP--) ;addr
:CALL(maskAddress) ; Mask address to 20 bytes
${touchedAddress(A)} => D
${isWarmedAddress(A)} => D
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:CALL(opEXTCODECOPYCheckHash)
$ => C :MLOAD(SP--) ;destOffset, memory offset
Expand Down Expand Up @@ -1345,7 +1348,7 @@ opEXTCODEHASH:
SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
:CALL(maskAddress) ; Mask address to 20 bytes
${touchedAddress(A)} => D
${isWarmedAddress(A)} => D
GAS-100-D*2500 => GAS :JMPN(outOfGas)
%SMT_KEY_SC_CODE => B
0 => C
Expand All @@ -1359,7 +1362,7 @@ opEXTCODEHASH:
;opEXTCODEHASHL2:
; SP - 1 => SP :JMPN(stackUnderflow)
; $ => A :MLOAD(SP)
; ${touchedAddress(A)} => D
; ${isWarmedAddress(A)} => D
; GAS-100-D*2500 => GAS :JMPN(outOfGas)
; %SMT_KEY_SC_CODE => B
; 0 => C
Expand Down Expand Up @@ -1595,7 +1598,7 @@ opSLOAD:
$ => E :SLOAD ;current value
E :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
${touchedStorageSlots(A,C)} => A
${isWarmedStorage(A,C)} => A
GAS - %SLOAD_GAS - A*2000 => GAS :JMPN(outOfGas)
:JMP(readCode)

Expand Down Expand Up @@ -1629,7 +1632,7 @@ opSSTOREinit:
$ => SR :MLOAD(originSR)
$ => B :SLOAD ;origin value
$ => SR :MLOAD(auxSR)
${touchedStorageSlots(A,C)} => A
${isWarmedStorage(A,C)} => A
B => C ;origin value
GAS - A*2100 => GAS :JMPN(outOfGas)
E => A
Expand Down Expand Up @@ -2707,6 +2710,8 @@ opSaveTopicsLoop:
; // TODO Check with DElEGATE CALL txDestAddr/txSrcAddr ( same as other calls)
; // TODO check revert create
opCREATE:
$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
SP - 3 :JMPN(stackUnderflow)
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
Expand Down Expand Up @@ -2803,7 +2808,8 @@ opCreateFail:
opCALL:

%MAX_CNT_STEPS - STEP - 600 :JMPN(outOfCounters)

$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
SP - 7 :JMPN(stackUnderflow)
SP - 1 => SP
CTX :MSTORE(originAuxCTX)
Expand Down Expand Up @@ -2860,8 +2866,7 @@ opCALL2:

; If addr in touched addres D = 0, else D = 1
$ => A :MLOAD(addrCall)
${touchedAddress(A)} => D ; TODO

${isWarmedAddress(A)} => D ; TODO
;gas_cost = memory_expansion_cost + code_execution_cost + address_access_cost + positive_value_cost + value_to_empty_account_cost
; Calculate address_access_cost: 100 for warm account, 2600 for cold account
GAS-100-D*2500 => GAS :JMPN(outOfGas)
Expand Down Expand Up @@ -2920,7 +2925,8 @@ opCALLend:
opCALLCODE: ; TODO check staticCall

%MAX_CNT_STEPS - STEP - 600 :JMPN(outOfCounters)

$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
SP - 7 :JMPN(stackUnderflow)
SP - 1 => SP
CTX :MSTORE(originAuxCTX)
Expand Down Expand Up @@ -2966,7 +2972,7 @@ opCALLCODE: ; TODO check staticCall

; If addr in touched addres D = 0, else D = 1
$ => A :MLOAD(addrCall)
${touchedAddress(A)} => D
${isWarmedAddress(A)} => D

;gas_cost = memory_expansion_cost + code_execution_cost + address_access_cost + positive_value_cost
; Calculate address_access_cost: 100 for warm account, 2600 for cold account
Expand Down Expand Up @@ -3093,6 +3099,9 @@ opRETURNend:
$ => PC :MLOAD(lastPC)
1 :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
$ => A :MLOAD(depth)
A - 1 :MSTORE(depth)
${commit()}
:JMP(readCode)


Expand Down Expand Up @@ -3160,7 +3169,8 @@ opRETURNcreateEnd:
opDELEGATECALL:

%MAX_CNT_STEPS - STEP - 600 :JMPN(outOfCounters)

$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
SP - 6 :JMPN(stackUnderflow)
SP - 1 => SP
CTX :MSTORE(originAuxCTX)
Expand Down Expand Up @@ -3201,7 +3211,7 @@ opDELEGATECALL:

$ => B :MLOAD(lastMemOffset)
$ => A :MLOAD(addrCall)
${touchedAddress(A)} => C
${isWarmedAddress(A)} => C
;gas_cost = base_gas + gas_sent_with_call
GAS-100-C*2500 => GAS :JMPN(outOfGas)

Expand Down Expand Up @@ -3246,6 +3256,8 @@ opDELEGATECALLend:
; // TODO Check with DElEGATE CALL txDestAddr/txSrcAddr ( same as other calls)
; // TODO check revert create
opCREATE2:
$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
SP - 4 :JMPN(stackUnderflow)
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
Expand Down Expand Up @@ -3347,6 +3359,8 @@ opCREATE2:

;TODO same as other calls
opSTATICCALL:
$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
SP - 6 :JMPN(stackUnderflow)
SP - 1 => SP
CTX :MSTORE(originAuxCTX)
Expand Down Expand Up @@ -3379,7 +3393,7 @@ opSTATICCALL:

; Substract gas cost
$ => A :MLOAD(addrCall)
${touchedAddress(A)} => E
${isWarmedAddress(A)} => E
;gas_cost = base_gas + gas_sent_with_call
GAS-100-E*2500 => GAS :JMPN(outOfGas)

Expand Down Expand Up @@ -3482,6 +3496,9 @@ opREVERTend:
$ => PC :MLOAD(lastPC)
0 :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
$ => A :MLOAD(depth)
A - 1 :MSTORE(depth)
${revert()}
:JMP(readCode)

; // TODO: handle if depth is over 0
Expand Down Expand Up @@ -3533,7 +3550,7 @@ opSELFDESTRUCT2:
; gas: check touched address
C => A
:CALL(maskAddress) ; Mask address to 20 bytes
${touchedAddress(A)} => E
${isWarmedAddress(A)} => E
GAS - 2600 * E => GAS :JMPN(outOfGas)

; read previous balance receiver
Expand All @@ -3558,7 +3575,7 @@ opSELFDESTRUCT2:
$ => SR :SSTORE

$ => A :MLOAD(originCTX)
$ :EQ,JMPC(handleGas)
$ :EQ,JMPC(handleGas)
$ => B :MLOAD(gasRefund)
$ => C :MLOAD(isCreate)
1 => D
Expand All @@ -3576,6 +3593,9 @@ opSELFDESTRUCTCend:
$ => PC :MLOAD(lastPC)
D :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
$ => A :MLOAD(depth)
A - 1 :MSTORE(depth)
${commit()}
:JMP(readCode)

opINVALID:
Expand Down
15 changes: 8 additions & 7 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ endCheckChainId:

;; Reset warm/cold information
$ => A :MLOAD(txSrcOriginAddr)
${resetTouchedAddress()} ; clean touchedAddresses since there is a new transaction
${resetStorageSlots()} ; clean storageSlots since there is a new transaction
${touchedAddress(A)} ; add tx.origin to touched addresses
${clearWarmedStorage()} ; clear accessed since there is a new transaction
${isWarmedAddress(A)} ; add tx.origin to touched addresses
0 :MSTORE(depth) ; Initial depth is 0

;; Set gasPrice global var
;; Set gasPrice global var
$ => A :MLOAD(txGasPriceRLP)
A :MSTORE(txGasPrice)
;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -201,7 +201,8 @@ txType:
0 - A :JMPN(getContractAddress)
$ => A :MLOAD(txDestAddr)
; Add 'to' to touched addresses
${touchedAddress(A)}
${isWarmedAddress(A)}
${checkpoint()}
; Check 'to' is zero or precompiled contract
; Check zero address since zero address is not a precompiled contract
0 => B
Expand Down Expand Up @@ -346,8 +347,8 @@ create2end:
;; deploy contract in state-tree
deploy:
; add address to touched addresses
${touchedAddress(A)}

${isWarmedAddress(A)}
${checkpoint()}
; check if address is deployable ( nonce == bytecode == 0)
A => E

Expand Down
3 changes: 3 additions & 0 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ handleInvalidStatic:
$ => SP :MLOAD(lastSP)
$ => PC :MLOAD(lastPC)
0 :MSTORE(SP++)
$ => A :MLOAD(depth)
A - 1 :MSTORE(depth)
${revert()}
:JMP(readCode)

handleBatchError:
Expand Down
1 change: 1 addition & 0 deletions main/vars.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ VAR GLOBAL valueCall ; value parameter when creating a new context
VAR GLOBAL argsLengthCall ; size af the calldata creating a new context
VAR GLOBAL txSrcOriginAddr ; origin address of a tx
VAR GLOBAL txGasPrice ; transaction parameter: 'gasPrice' global var
VAR GLOBAL depth ; Current depth execution

VAR GLOBAL nextFreeLogIndex ; pointer to the next free slot to store information in LOG opcode
VAR GLOBAL originSR ; State root before processing each transaction
Expand Down

0 comments on commit 1370cfe

Please sign in to comment.