Skip to content

Commit

Permalink
Merge branch 'main' into feat/reports-exporting
Browse files Browse the repository at this point in the history
# Conflicts:
#	internal/polkavm/common.go
  • Loading branch information
carlos-romano committed Jan 29, 2025
2 parents 3316b37 + 70e04d2 commit d54ff63
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 166 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Contributing

**Due to the JAM Prize rules, we do not accept any external contributions at the moment.**

## Strawberry

To contribute to the Strawberry implementation, start with the proper development copy.
Expand Down
30 changes: 15 additions & 15 deletions internal/polkavm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,22 @@ type Mutator interface {
CmovIfZero(d Reg, s, c Reg)
CmovIfZeroImm(d Reg, c Reg, s uint32)
CmovIfNotZero(d Reg, s, c Reg)
RotL64(d Reg, s, c Reg)
RotL32(d Reg, s, c Reg)
RotR64(d Reg, s, c Reg)
RotR32(d Reg, s, c Reg)
AndInv(d Reg, s, c Reg)
OrInv(d Reg, s, c Reg)
Xnor(d Reg, s, c Reg)
Max(d Reg, s, c Reg)
MaxU(d Reg, s, c Reg)
Min(d Reg, s, c Reg)
MinU(d Reg, s, c Reg)
RotateLeft64(d Reg, s1, s2 Reg)
RotateLeft32(d Reg, s1, s2 Reg)
RotateRight64(d Reg, s1, s2 Reg)
RotateRight32(d Reg, s1, s2 Reg)
AndInverted(d Reg, s1, s2 Reg)
OrInverted(d Reg, s1, s2 Reg)
Xnor(d Reg, s1, s2 Reg)
Max(d Reg, s1, s2 Reg)
MaxUnsigned(d Reg, s1, s2 Reg)
Min(d Reg, s1, s2 Reg)
MinUnsigned(d Reg, s1, s2 Reg)
CmovIfNotZeroImm(d Reg, c Reg, s uint32)
RotR64Imm(d Reg, c Reg, s uint32)
RotR64ImmAlt(d Reg, c Reg, s uint32)
RotR32Imm(d Reg, c Reg, s uint32)
RotR32ImmAlt(d Reg, c Reg, s uint32)
RotateRight64Imm(d Reg, s1 Reg, s2 uint32)
RotateRight64ImmAlt(d Reg, s1 Reg, s2 uint32)
RotateRight32Imm(d Reg, s1 Reg, s2 uint32)
RotateRight32ImmAlt(d Reg, s1 Reg, s2 uint32)
StoreU8(src Reg, offset uint32) error
StoreU16(src Reg, offset uint32) error
StoreU32(src Reg, offset uint32) error
Expand Down
52 changes: 49 additions & 3 deletions internal/polkavm/host_call/accumulate_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,42 @@ func Query(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (
}
gas -= QueryCost

// TODO: implement method
addr, preimageMetaKeyLength := regs[A0], regs[A1]

// let h = μo..o+32 if Zo..o+32 ⊂ Vμ
h := make([]byte, 32)
if err := mem.Read(uint32(addr), h); err != nil {
// otherwise ∇ => OOB
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

// let a = (xs)l[h, z] if (h, z) ∈ K((xs)l)
serviceAccount := ctxPair.RegularCtx.ServiceAccount()
key := service.PreImageMetaKey{
Hash: crypto.Hash(h),
Length: service.PreimageLength(preimageMetaKeyLength),
}
a, exists := serviceAccount.PreimageMeta[key]
if !exists {
// a = ∇ => (NONE, 0)
regs[A1] = 0
return gas, withCode(regs, NONE), mem, ctxPair, nil
}

switch len(a) {
case 0:
// a = [] => (0, 0)
regs[A0], regs[A1] = 0, 0
case 1:
// a = [x] => (1 + 2^32 * x, 0)
regs[A0], regs[A1] = 1+(uint64(a[0])<<32), 0
case 2:
// a = [x, y] => (2 + 2^32 * x, y)
regs[A0], regs[A1] = 2+(uint64(a[0])<<32), uint64(a[1])
case 3:
// a = [x, y, z] => (3 + 2^32 * x, y + 2^32 * z)
regs[A0], regs[A1] = 3+(uint64(a[0])<<32), uint64(a[1])+(uint64(a[2])<<32)
}

return gas, regs, mem, ctxPair, nil
}
Expand Down Expand Up @@ -440,7 +475,18 @@ func Yield(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (
}
gas -= YieldCost

// TODO: implement method
addr := regs[A0]

return gas, regs, mem, ctxPair, nil
// let h = μo..o+32 if Zo..o+32 ⊂ Vμ otherwise ∇
hBytes := make([]byte, 32)
if err := mem.Read(uint32(addr), hBytes); err != nil {
// (ω′7, x′_y) = (OOB, x_y) if h = ∇
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

// (ω′7, x′_y) = (OK, h) otherwise
h := crypto.Hash(hBytes)
ctxPair.RegularCtx.AccumulationHash = &h

return gas, withCode(regs, OK), mem, ctxPair, nil
}
191 changes: 191 additions & 0 deletions internal/polkavm/host_call/accumulate_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,178 @@ func TestAccumulate(t *testing.T) {
},
},
},
{
name: "query_empty_timeslots",
fn: fnStd(Query),
initialGas: 100,
initialRegs: deltaRegs{
A1: 123,
},
alloc: alloc{
A0: hash2bytes(randomHash),
},
X: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{
Hash: randomHash,
Length: 123,
}: {},
},
},
},
},
},
expectedDeltaRegs: deltaRegs{
A0: 0,
A1: 0,
},
expectedGas: 88,
expectedX: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{Hash: randomHash, Length: 123}: {},
},
},
},
},
},
},
{
name: "query_1timeslot",
fn: fnStd(Query),
initialGas: 100,
initialRegs: deltaRegs{
A1: 123,
},
alloc: alloc{
A0: hash2bytes(randomHash),
},
X: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{
Hash: randomHash,
Length: 123,
}: {11},
},
},
},
},
},
expectedDeltaRegs: deltaRegs{
A0: 1 + (uint64(11) << 32),
A1: 0,
},
expectedGas: 88,
expectedX: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{Hash: randomHash, Length: 123}: {11},
},
},
},
},
},
},
{
name: "query_2timeslots",
fn: fnStd(Query),
initialGas: 100,
initialRegs: deltaRegs{
A1: 123,
},
alloc: alloc{
A0: hash2bytes(randomHash),
},
X: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{
Hash: randomHash,
Length: 123,
}: {11, 12},
},
},
},
},
},
expectedDeltaRegs: deltaRegs{
A0: 2 + (uint64(11) << 32),
A1: uint64(12),
},
expectedGas: 88,
expectedX: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{Hash: randomHash, Length: 123}: {11, 12},
},
},
},
},
},
},
{
name: "query_3timeslots",
fn: fnStd(Query),
initialGas: 100,
initialRegs: deltaRegs{
A1: 123,
},
alloc: alloc{
A0: hash2bytes(randomHash),
},
X: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{
Hash: randomHash,
Length: 123,
}: {11, 12, 13},
},
},
},
},
},
expectedDeltaRegs: deltaRegs{
A0: 3 + (uint64(11) << 32),
A1: uint64(12) + (uint64(13) << 32),
},
expectedGas: 88,
expectedX: AccumulateContext{
ServiceId: 999,
AccumulationState: state.AccumulationState{
ServiceState: service.ServiceState{
999: {
PreimageMeta: map[service.PreImageMetaKey]service.PreimageHistoricalTimeslots{
{Hash: randomHash, Length: 123}: {11, 12, 13},
},
},
},
},
},
},
{
name: "solicit_out_of_gas",
fn: fnTms(Solicit),
Expand Down Expand Up @@ -694,6 +866,25 @@ func TestAccumulate(t *testing.T) {
},
},
},
{
name: "yield",
fn: fnStd(Yield),
initialGas: 100,
alloc: alloc{
A0: hash2bytes(randomHash),
},
X: AccumulateContext{
ServiceId: 999,
},
expectedDeltaRegs: deltaRegs{
A0: uint64(OK),
},
expectedGas: 88,
expectedX: AccumulateContext{
ServiceId: 999,
AccumulationHash: &randomHash,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions internal/polkavm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ func (i *Instruction) Mutate(mutator Mutator) (uint32, error) {
case CmovIfNotZeroImm:
mutator.CmovIfNotZeroImm(i.Reg[0], i.Reg[1], i.Imm[0])
case RotR64Imm:
mutator.RotR64Imm(i.Reg[0], i.Reg[1], i.Imm[0])
mutator.RotateRight64Imm(i.Reg[0], i.Reg[1], i.Imm[0])
case RotR64ImmAlt:
mutator.RotR64ImmAlt(i.Reg[0], i.Reg[1], i.Imm[0])
mutator.RotateRight64ImmAlt(i.Reg[0], i.Reg[1], i.Imm[0])
case RotR32Imm:
mutator.RotR32Imm(i.Reg[0], i.Reg[1], i.Imm[0])
mutator.RotateRight32Imm(i.Reg[0], i.Reg[1], i.Imm[0])
case RotR32ImmAlt:
mutator.RotR32ImmAlt(i.Reg[0], i.Reg[1], i.Imm[0])
mutator.RotateRight32ImmAlt(i.Reg[0], i.Reg[1], i.Imm[0])
case BranchEq:
mutator.BranchEq(i.Reg[0], i.Reg[1], i.Imm[0])
case BranchNotEq:
Expand Down Expand Up @@ -700,27 +700,27 @@ func (i *Instruction) Mutate(mutator Mutator) (uint32, error) {
case CmovIfNotZero:
mutator.CmovIfNotZero(i.Reg[0], i.Reg[1], i.Reg[2])
case RotL64:
mutator.RotL64(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.RotateLeft64(i.Reg[0], i.Reg[1], i.Reg[2])
case RotL32:
mutator.RotL32(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.RotateLeft32(i.Reg[0], i.Reg[1], i.Reg[2])
case RotR64:
mutator.RotR64(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.RotateRight64(i.Reg[0], i.Reg[1], i.Reg[2])
case RotR32:
mutator.RotR32(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.RotateRight32(i.Reg[0], i.Reg[1], i.Reg[2])
case AndInv:
mutator.AndInv(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.AndInverted(i.Reg[0], i.Reg[1], i.Reg[2])
case OrInv:
mutator.OrInv(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.OrInverted(i.Reg[0], i.Reg[1], i.Reg[2])
case Xnor:
mutator.Xnor(i.Reg[0], i.Reg[1], i.Reg[2])
case Max:
mutator.Max(i.Reg[0], i.Reg[1], i.Reg[2])
case MaxU:
mutator.MaxU(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.MaxUnsigned(i.Reg[0], i.Reg[1], i.Reg[2])
case Min:
mutator.Min(i.Reg[0], i.Reg[1], i.Reg[2])
case MinU:
mutator.MinU(i.Reg[0], i.Reg[1], i.Reg[2])
mutator.MinUnsigned(i.Reg[0], i.Reg[1], i.Reg[2])
case Jump:
mutator.Jump(i.Imm[0])
case Ecalli:
Expand Down
Loading

0 comments on commit d54ff63

Please sign in to comment.