Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (st *StateTransition) buyAltTokenGas() error {
if feeVaultAddress == nil || bytes.Equal(feeVaultAddress.Bytes(), common.Address{}.Bytes()) {
return fmt.Errorf("fee vault address is not configured")
}
if err := st.TransferAltTokenHybrid(tokenInfo.TokenAddress, st.msg.From(), *feeVaultAddress, tokenFee, tokenInfo.BalanceSlot, tokenBalance); err != nil {
if err = st.TransferAltTokenHybrid(tokenInfo.TokenAddress, st.msg.From(), *feeVaultAddress, tokenFee, tokenInfo.BalanceSlot, tokenBalance); err != nil {
return fmt.Errorf("failed to transfer alt tokens for gas payment: %v", err)
}

Expand Down
6 changes: 3 additions & 3 deletions core/token_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (st *StateTransition) GetAltTokenBalanceHybrid(tokenID uint16, user common.
}
return info, balance, nil
}
balance, _, err = fees.GetAltTokenBalanceFromSlot(st.state, info.TokenAddress, user, info.BalanceSlot)
balance, _, err = fees.GetAltTokenBalanceFromSlot(st.state, info.TokenAddress, user, fees.DecodeBalanceSlot(info.BalanceSlot))
if err != nil {
return nil, nil, err
}
Expand All @@ -53,7 +53,7 @@ func (st *StateTransition) TransferAltTokenHybrid(tokenAddress, from, to common.
return transferAltTokenByEVM(st.evm, tokenAddress, from, to, amount, userBalanceBefore)
}
// Use storage slot method
return fees.TransferAltTokenByState(st.state, tokenAddress, balanceSlot, from, to, amount)
return fees.TransferAltTokenByState(st.state, tokenAddress, fees.DecodeBalanceSlot(balanceSlot), from, to, amount)
}

// GetAltTokenBalance returns the balance of an alt token for a specific address.
Expand All @@ -65,7 +65,7 @@ func GetAltTokenBalance(evm *vm.EVM, tokenID uint16, user common.Address) (*big.
balance := new(big.Int)
if !bytes.Equal(info.BalanceSlot.Bytes(), common.Hash{}.Bytes()) {
// balance slot exist
balance, _, err = fees.GetAltTokenBalanceFromSlot(evm.StateDB, info.TokenAddress, user, info.BalanceSlot)
balance, _, err = fees.GetAltTokenBalanceFromSlot(evm.StateDB, info.TokenAddress, user, fees.DecodeBalanceSlot(info.BalanceSlot))
if err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions rollup/fees/token_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ func CalculateStructFieldSlot(baseSlot common.Hash, fieldOffset uint64) common.H
return common.BigToHash(result)
}

// DecodeBalanceSlot decodes the balance slot from storage
// Storage uses balanceSlot + 1 to distinguish between unset (0) and actual slot 0
// This function subtracts 1 to get the actual balance slot value
// If the stored value is 0 (common.Hash{}), it means balanceSlot is not set, returns as-is
func DecodeBalanceSlot(storedBalanceSlot common.Hash) common.Hash {
// If the stored value is zero hash, it means balanceSlot is not set
if storedBalanceSlot == (common.Hash{}) {
Comment thread
Kukoomomo marked this conversation as resolved.
Outdated
return storedBalanceSlot
}

// Subtract 1 to get the actual balance slot
slotInt := new(big.Int).SetBytes(storedBalanceSlot[:])
actualSlotInt := new(big.Int).Sub(slotInt, big.NewInt(1))
return common.BigToHash(actualSlotInt)
}

// GetUint256MappingValue retrieves a value from a mapping storage slot
func GetUint256MappingValue(state StateDB, contractAddr common.Address, key uint16, mappingSlot common.Hash) (*big.Int, error) {
// Calculate the storage slot
Expand Down
Loading