diff --git a/internal/polkavm/host_call/accumulate_functions.go b/internal/polkavm/host_call/accumulate_functions.go index 57a60931..0d2cfdf5 100644 --- a/internal/polkavm/host_call/accumulate_functions.go +++ b/internal/polkavm/host_call/accumulate_functions.go @@ -23,12 +23,12 @@ func Bless(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) ( // let [m, a, v, o, n] = ω7...12 managerServiceId, assignServiceId, designateServiceId, addr, servicesNr := regs[A0], regs[A1], regs[A2], regs[A3], regs[A4] // let g = {(s ↦ g) where E4(s) ⌢ E8(g) = μ_o+12i⋅⋅⋅+12 | i ∈ Nn} if Zo⋅⋅⋅+12n ⊂ Vμ otherwise ∇ - for i := range servicesNr { - serviceId, err := readNumber[block.ServiceId](mem, addr+(12*i), 4) + for i := range uint32(servicesNr) { + serviceId, err := readNumber[block.ServiceId](mem, uint32(addr)+(12*i), 4) if err != nil { return gas, withCode(regs, OOB), mem, ctxPair, err } - serviceGas, err := readNumber[uint64](mem, addr+(12*i)+4, 8) + serviceGas, err := readNumber[uint64](mem, uint32(addr)+(12*i)+4, 8) if err != nil { return gas, withCode(regs, OOB), mem, ctxPair, err } @@ -54,12 +54,12 @@ func Assign(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) // let o = ω8 addr := regs[A1] core := regs[A0] - if core >= uint32(common.TotalNumberOfCores) { + if core >= uint64(common.TotalNumberOfCores) { return gas, withCode(regs, CORE), mem, ctxPair, nil } for i := 0; i < state.PendingAuthorizersQueueSize; i++ { bytes := make([]byte, 32) - if err := mem.Read(addr+uint32(32*i), bytes); err != nil { + if err := mem.Read(uint32(addr)+uint32(32*i), bytes); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } ctxPair.RegularCtx.AccumulationState.WorkReportsQueue[core][i] = crypto.Hash(bytes) @@ -84,7 +84,7 @@ func Designate(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPai addr := regs[A0] for i := 0; i < common.NumberOfValidators; i++ { bytes := make([]byte, 336) - if err := mem.Read(addr+uint32(336*i), bytes); err != nil { + if err := mem.Read(uint32(addr)+uint32(336*i), bytes); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } @@ -109,7 +109,7 @@ func Checkpoint(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPa ctxPair.ExceptionalCtx = ctxPair.RegularCtx // Set the new ϱ' value into ω′7 - regs[A0] = uint32(gas & ((1 << 32) - 1)) + regs[A0] = uint64(gas & ((1 << 32) - 1)) return gas, regs, mem, ctxPair, nil } @@ -126,7 +126,7 @@ func New(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (Ga // c = μo⋅⋅⋅+32 if No⋅⋅⋅+32 ⊂ Vμ otherwise ∇ codeHashBytes := make([]byte, 32) - if err := mem.Read(addr, codeHashBytes); err != nil { + if err := mem.Read(uint32(addr), codeHashBytes); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } @@ -149,7 +149,7 @@ func New(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (Ga // if a ≠ ∇ ∧ b ≥ (xs)t if b >= ctxPair.RegularCtx.ServiceAccount().ThresholdBalance() { - regs[A0] = uint32(ctxPair.RegularCtx.ServiceId) + regs[A0] = uint64(ctxPair.RegularCtx.ServiceId) currentAccount := ctxPair.RegularCtx.ServiceAccount() currentAccount.Balance = b ctxPair.RegularCtx.ServiceState[ctxPair.RegularCtx.ServiceId] = currentAccount @@ -176,7 +176,7 @@ func Upgrade(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) // c = μo⋅⋅⋅+32 if No⋅⋅⋅+32 ⊂ Vμ otherwise ∇ codeHash := make([]byte, 32) - if err := mem.Read(addr, codeHash); err != nil { + if err := mem.Read(uint32(addr), codeHash); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } @@ -202,7 +202,7 @@ func Transfer(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair // m = μo⋅⋅⋅+M if No⋅⋅⋅+M ⊂ Vμ otherwise ∇ m := make([]byte, service.TransferMemoSizeBytes) - if err := mem.Read(o, m); err != nil { + if err := mem.Read(uint32(o), m); err != nil { return gas, withCode(regs, OK), mem, ctxPair, nil } @@ -263,7 +263,7 @@ func Quit(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (G // m = E−1(μo⋅⋅⋅+M) memo := make([]byte, service.TransferMemoSizeBytes) - if err := mem.Read(addr, memo); err != nil { + if err := mem.Read(uint32(addr), memo); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } @@ -309,7 +309,7 @@ func Solicit(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair, addr, preimageLength := regs[A0], regs[A1] // let h = μo⋅⋅⋅+32 if Zo⋅⋅⋅+32 ⊂ Vμ otherwise ∇ preimageHashBytes := make([]byte, 32) - if err := mem.Read(addr, preimageHashBytes); err != nil { + if err := mem.Read(uint32(addr), preimageHashBytes); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } @@ -350,7 +350,7 @@ func Forget(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair, // let h = μo⋅⋅⋅+32 if Zo⋅⋅⋅+32 ⊂ Vμ otherwise ∇ preimageHashBytes := make([]byte, 32) - if err := mem.Read(addr, preimageHashBytes); err != nil { + if err := mem.Read(uint32(addr), preimageHashBytes); err != nil { return gas, withCode(regs, OOB), mem, ctxPair, nil } diff --git a/internal/polkavm/host_call/common.go b/internal/polkavm/host_call/common.go index 545552d5..68139989 100644 --- a/internal/polkavm/host_call/common.go +++ b/internal/polkavm/host_call/common.go @@ -43,19 +43,19 @@ const ( ForgetID = 14 ) -type Code uint32 +type Code uint64 const ( - NONE Code = math.MaxUint32 - WHAT Code = math.MaxUint32 - 1 - OOB Code = math.MaxUint32 - 2 - WHO Code = math.MaxUint32 - 3 - FULL Code = math.MaxUint32 - 4 - CORE Code = math.MaxUint32 - 5 - CASH Code = math.MaxUint32 - 6 - LOW Code = math.MaxUint32 - 7 - HIGH Code = math.MaxUint32 - 8 - HUH Code = math.MaxUint32 - 9 + NONE Code = math.MaxUint64 + WHAT Code = math.MaxUint64 - 1 + OOB Code = math.MaxUint64 - 2 + WHO Code = math.MaxUint64 - 3 + FULL Code = math.MaxUint64 - 4 + CORE Code = math.MaxUint64 - 5 + CASH Code = math.MaxUint64 - 6 + LOW Code = math.MaxUint64 - 7 + HIGH Code = math.MaxUint64 - 8 + HUH Code = math.MaxUint64 - 9 OK Code = 0 ) @@ -98,6 +98,6 @@ func readNumber[U interface{ ~uint32 | ~uint64 }](mem Memory, addr uint32, lengt } func withCode(regs Registers, s Code) Registers { - regs[A0] = uint32(s) + regs[A0] = uint64(s) return regs } diff --git a/internal/polkavm/host_call/general_functions.go b/internal/polkavm/host_call/general_functions.go index 8b711d0d..89fed621 100644 --- a/internal/polkavm/host_call/general_functions.go +++ b/internal/polkavm/host_call/general_functions.go @@ -28,7 +28,7 @@ func GasRemaining(gas polkavm.Gas, regs polkavm.Registers) (polkavm.Gas, polkavm gas -= GasRemainingCost // Set the new ϱ' value into ω′7 - regs[polkavm.A0] = uint32(gas) + regs[polkavm.A0] = uint64(gas) return gas, regs, nil } @@ -44,12 +44,12 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi // Determine the lookup key 'a' a := s - if uint64(omega7) != math.MaxUint64 && omega7 != uint32(serviceId) { + if uint64(omega7) != math.MaxUint64 && omega7 != uint64(serviceId) { var exists bool // Lookup service account by serviceId in the serviceState a, exists = serviceState[serviceId] if !exists { - regs[polkavm.A0] = uint32(NONE) + regs[polkavm.A0] = uint64(NONE) return gas, regs, mem, nil } } @@ -60,8 +60,7 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi memorySlice := make([]byte, 32) err := mem.Read(ho, memorySlice) if err != nil { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, err + return gas, withCode(regs, OOB), mem, err } // Compute the hash H(µho..ho+32) @@ -70,8 +69,7 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi // Lookup value in storage (v) using the hash v, exists := a.Storage[hash] if !exists { - regs[polkavm.A0] = uint32(NONE) - return gas, regs, mem, nil + return gas, withCode(regs, NONE), mem, nil } bo := regs[polkavm.A2] @@ -80,15 +78,13 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi // Write value to memory if within bounds if len(v) > 0 && len(v) <= int(bz) { if err = mem.Write(bo, v); err != nil { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, err + return gas, withCode(regs, OOB), mem, err } } else { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, err + return gas, withCode(regs, OOB), mem, err } - regs[polkavm.A0] = uint32(len(v)) + regs[polkavm.A0] = uint64(len(v)) return gas, regs, mem, err } @@ -106,7 +102,7 @@ func Read(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s service bz := regs[polkavm.A4] a := s - if uint64(omega7) != math.MaxUint64 && omega7 != uint32(serviceId) { + if uint64(omega7) != math.MaxUint64 && omega7 != uint64(serviceId) { var exists bool a, exists = serviceState[block.ServiceId(omega7)] if !exists { @@ -118,8 +114,7 @@ func Read(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s service keyData := make([]byte, kz) err := mem.Read(ko, keyData) if err != nil { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, nil + return gas, withCode(regs, OOB), mem, nil } serviceIdBytes, err := jam.Marshal(omega7) @@ -137,24 +132,21 @@ func Read(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s service v, exists := a.Storage[k] if !exists { - regs[polkavm.A0] = uint32(NONE) - return gas, regs, mem, nil + return gas, withCode(regs, NONE), mem, nil } writeLen := int(math.Min(float64(bz), float64(len(v)))) if writeLen > 0 { if err = mem.Write(bo, v[:writeLen]); err != nil { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, nil + return gas, withCode(regs, OOB), mem, nil } - regs[polkavm.A0] = uint32(len(v)) + regs[polkavm.A0] = uint64(len(v)) return gas, regs, mem, nil } - regs[polkavm.A0] = uint32(NONE) - return gas, regs, mem, nil + return gas, withCode(regs, NONE), mem, nil } // Write ΩW(ϱ, ω, μ, s, s) @@ -189,8 +181,7 @@ func Write(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servic valueData := make([]byte, vz) err := mem.Read(vo, valueData) if err != nil { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, s, err + return gas, withCode(regs, OOB), mem, s, err } a.Storage[k] = valueData @@ -198,17 +189,15 @@ func Write(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servic storageItem, ok := s.Storage[k] if !ok { - regs[polkavm.A0] = uint32(NONE) - return gas, regs, mem, s, err + return gas, withCode(regs, NONE), mem, s, err } if a.ThresholdBalance() > a.Balance { - regs[polkavm.A0] = uint32(FULL) - return gas, regs, mem, s, err + return gas, withCode(regs, FULL), mem, s, err } // otherwise a.ThresholdBalance() <= a.Balance - regs[polkavm.A0] = uint32(len(storageItem)) // l + regs[polkavm.A0] = uint64(len(storageItem)) // l return gas, regs, mem, a, err // return service account 'a' as opposed to 's' for not successful paths } @@ -247,10 +236,8 @@ func Info(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, serviceId } if err := mem.Write(omega8, m); err != nil { - regs[polkavm.A0] = uint32(OOB) - return gas, regs, mem, nil + return gas, withCode(regs, OOB), mem, nil } - regs[polkavm.A0] = uint32(OK) - return gas, regs, mem, nil + return gas, withCode(regs, OK), mem, nil }