Skip to content

Commit

Permalink
fix: host func casting
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Nov 28, 2024
1 parent 5b7a042 commit 136ac58
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 116 deletions.
28 changes: 14 additions & 14 deletions internal/polkavm/host_call/accumulate_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
42 changes: 21 additions & 21 deletions internal/polkavm/host_call/accumulate_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAccumulate(t *testing.T) {
A4: 3,
},
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},

initialGas: 100,
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestAccumulate(t *testing.T) {
initialGas: 100,
expectedGas: 88,
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
expectedX: AccumulateContext{
AccumulationState: state.AccumulationState{
Expand All @@ -141,7 +141,7 @@ func TestAccumulate(t *testing.T) {
initialGas: 100,
expectedGas: 88,
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
expectedX: AccumulateContext{
AccumulationState: state.AccumulationState{
Expand All @@ -166,7 +166,7 @@ func TestAccumulate(t *testing.T) {
},
initialRegs: deltaRegs{A1: 123123, A2: 123124123, A3: 756846353},
expectedDeltaRegs: deltaRegs{
A0: uint32(currentServiceID),
A0: uint64(currentServiceID),
},
initialGas: 100,
expectedGas: 88,
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestAccumulate(t *testing.T) {
},
initialRegs: deltaRegs{A1: 3453453453, A2: 456456456},
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
initialGas: 100,
expectedGas: 88,
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestAccumulate(t *testing.T) {
A2: 80, // g
},
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
initialGas: 1000000100,
expectedGas: 88,
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestAccumulate(t *testing.T) {
A0: 1234, // d: receiver address
},
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
initialGas: 100,
expectedGas: 88,
Expand Down Expand Up @@ -379,7 +379,7 @@ func TestAccumulate(t *testing.T) {
initialGas: 100,
expectedGas: 88,
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
X: AccumulateContext{
ServiceId: currentServiceID,
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestAccumulate(t *testing.T) {
initialGas: 100,
expectedGas: 88,
expectedDeltaRegs: deltaRegs{
A0: uint32(OK),
A0: uint64(OK),
},
X: AccumulateContext{
ServiceId: currentServiceID,
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestAccumulate(t *testing.T) {
initialGas: 100,
expectedGas: 88,
expectedDeltaRegs: deltaRegs{
A0: uint32(HUH),
A0: uint64(HUH),
},
X: AccumulateContext{
ServiceId: currentServiceID,
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestAccumulate(t *testing.T) {
initialGas: 100,
expectedGas: 88,
expectedDeltaRegs: deltaRegs{
A0: uint32(FULL),
A0: uint64(FULL),
},
X: AccumulateContext{
ServiceId: currentServiceID,
Expand All @@ -515,7 +515,7 @@ func TestAccumulate(t *testing.T) {
fn: fnTms(Forget),
alloc: alloc{A0: hash2bytes(randomHash)},
initialRegs: deltaRegs{A1: 123},
expectedDeltaRegs: deltaRegs{A0: uint32(OK)},
expectedDeltaRegs: deltaRegs{A0: uint64(OK)},
initialGas: 100,
expectedGas: 88,
X: AccumulateContext{
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestAccumulate(t *testing.T) {
fn: fnTms(Forget),
alloc: alloc{A0: hash2bytes(randomHash)},
initialRegs: deltaRegs{A1: 123},
expectedDeltaRegs: deltaRegs{A0: uint32(OK)},
expectedDeltaRegs: deltaRegs{A0: uint64(OK)},
initialGas: 100,
expectedGas: 88,
timeslot: randomTimeslot2,
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestAccumulate(t *testing.T) {
fn: fnTms(Forget),
alloc: alloc{A0: hash2bytes(randomHash)},
initialRegs: deltaRegs{A1: 123},
expectedDeltaRegs: deltaRegs{A0: uint32(OK)},
expectedDeltaRegs: deltaRegs{A0: uint64(OK)},
initialGas: 100,
expectedGas: 88,
timeslot: randomTimeslot2 + jamtime.PreimageExpulsionPeriod + 1,
Expand Down Expand Up @@ -615,7 +615,7 @@ func TestAccumulate(t *testing.T) {
fn: fnTms(Forget),
alloc: alloc{A0: hash2bytes(randomHash)},
initialRegs: deltaRegs{A1: 123},
expectedDeltaRegs: deltaRegs{A0: uint32(OK)},
expectedDeltaRegs: deltaRegs{A0: uint64(OK)},
initialGas: 100,
expectedGas: 88,
timeslot: randomTimeslot2 + jamtime.PreimageExpulsionPeriod + 1,
Expand Down Expand Up @@ -655,15 +655,15 @@ func TestAccumulate(t *testing.T) {
mem := memoryMap.NewMemory(nil, nil, nil)
initialRegs := Registers{
RA: VmAddressReturnToHost,
SP: memoryMap.StackAddressHigh,
SP: uint64(memoryMap.StackAddressHigh),
}
stackAddress := memoryMap.StackAddressLow
for addrReg, v := range tc.alloc {
require.Greater(t, addrReg, S1)
err = mem.Write(stackAddress, v)
require.NoError(t, err)

initialRegs[addrReg] = stackAddress
initialRegs[addrReg] = uint64(stackAddress)
stackAddress = stackAddress + uint32(len(v))
}
for i, v := range tc.initialRegs {
Expand All @@ -685,7 +685,7 @@ func TestAccumulate(t *testing.T) {

expectedRegs := initialRegs
for i, reg := range tc.expectedDeltaRegs {
expectedRegs[i] = reg
expectedRegs[i] = uint64(reg)
}
assert.Equal(t, expectedRegs, regs)
assert.Equal(t, tc.expectedGas, gasRemaining)
Expand All @@ -697,7 +697,7 @@ func TestAccumulate(t *testing.T) {

type hostCall func(Gas, Registers, Memory, AccumulateContextPair, jamtime.Timeslot) (Gas, Registers, Memory, AccumulateContextPair, error)
type alloc map[Reg][]byte
type deltaRegs map[Reg]uint32
type deltaRegs map[Reg]uint64

func fixedSizeBytes(size int, b []byte) []byte {
bb := make([]byte, size)
Expand All @@ -706,8 +706,8 @@ func fixedSizeBytes(size int, b []byte) []byte {
}

func checkUint64(t *testing.T, gas uint64) deltaRegs {
a0 := uint32(math.Mod(float64(gas), 1<<32))
a1 := uint32(math.Floor(float64(gas) / (1 << 32)))
a0 := uint64(math.Mod(float64(gas), 1<<32))
a1 := uint64(math.Floor(float64(gas) / (1 << 32)))
assert.Equal(t, gas, uint64(a1)<<32|uint64(a0))
return deltaRegs{
A0: a0,
Expand Down
24 changes: 12 additions & 12 deletions internal/polkavm/host_call/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 136ac58

Please sign in to comment.