Skip to content

Commit

Permalink
Remove unnecessary getters
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbosio committed Jul 31, 2023
1 parent 551fd09 commit 4a0f451
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 34 deletions.
12 changes: 4 additions & 8 deletions pkg/vm/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// Memory represents the Cairo VM's memory.
type Memory struct {
data map[Relocatable]MaybeRelocatable
Data map[Relocatable]MaybeRelocatable
num_segments uint
}

Expand All @@ -15,10 +15,6 @@ func NewMemory() *Memory {
return &Memory{data, 0}
}

func (m *Memory) Data() *map[Relocatable]MaybeRelocatable {
return &m.data
}

func (m *Memory) NumSegments() uint {
return m.num_segments
}
Expand All @@ -38,12 +34,12 @@ func (m *Memory) Insert(addr Relocatable, val *MaybeRelocatable) error {
}

// Check for possible overwrites
prev_elem, ok := m.data[addr]
prev_elem, ok := m.Data[addr]
if ok && prev_elem != *val {
return errors.New("Memory is write-once, cannot overwrite memory value")
}

m.data[addr] = *val
m.Data[addr] = *val

return nil
}
Expand All @@ -62,7 +58,7 @@ func (m *Memory) Get(addr Relocatable) (*MaybeRelocatable, error) {
// check if the value is a `Relocatable` with a negative
// segment index. Again, these are edge cases so not important
// right now. See cairo-vm code for details.
value, ok := m.data[addr]
value, ok := m.Data[addr]

if !ok {
return nil, errors.New("Memory Get: Value not found")
Expand Down
2 changes: 1 addition & 1 deletion pkg/vm/memory/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (m *MemorySegmentManager) ComputeEffectiveSizes() map[uint]uint {
if len(m.SegmentSizes) == 0 {
greatestIndex := uint(0)

for ptr := range *m.Memory.Data() {
for ptr := range m.Memory.Data {
segmentIndex := uint(ptr.segmentIndex)
segmentMaxSize, ok := m.SegmentSizes[segmentIndex]
segmentSize := ptr.offset + 1
Expand Down
27 changes: 11 additions & 16 deletions pkg/vm/runners/cairo_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,38 @@ import (

type CairoRunner struct {
// TODO: relocatedMemory should be of type felt
relocatedMemory []int
RelocatedMemory []int
}

func NewCairoRunner() *CairoRunner {
return &CairoRunner{relocatedMemory: make([]int, 0)}
}

func (c *CairoRunner) RelocatedMemory() *[]int {
return &c.relocatedMemory
return &CairoRunner{RelocatedMemory: make([]int, 0)}
}

func (c *CairoRunner) RelocateMemory(vm *vm.VirtualMachine, relocationTable []uint) error {
if len(c.relocatedMemory) != 0 {
if len(c.RelocatedMemory) != 0 {
return errors.New("Inconsistent relocation")
}

// Relocated addresses start at 1
// TODO: with felts, we should use nil instead of -1
c.relocatedMemory = append(c.relocatedMemory, -1)
segments := vm.Segments()
c.RelocatedMemory = append(c.RelocatedMemory, -1)

for i := uint(0); i < segments.Memory.NumSegments(); i++ {
for j := uint(0); j < segments.SegmentSizes[i]; j++ {
for i := uint(0); i < vm.Segments.Memory.NumSegments(); i++ {
for j := uint(0); j < vm.Segments.SegmentSizes[i]; j++ {
ptr := memory.NewRelocatable(int(i), j)
cell, err := segments.Memory.Get(ptr)
cell, err := vm.Segments.Memory.Get(ptr)
if err == nil {
relocatedAddr := ptr.RelocateAddress(relocationTable)
value, err := cell.RelocateValue(relocationTable)
if err != nil {
return err
}
for len(c.relocatedMemory) <= int(relocatedAddr) {
c.relocatedMemory = append(c.relocatedMemory, -1)
for len(c.RelocatedMemory) <= int(relocatedAddr) {
c.RelocatedMemory = append(c.RelocatedMemory, -1)
}
c.relocatedMemory[relocatedAddr] = value
c.RelocatedMemory[relocatedAddr] = value
} else {
c.relocatedMemory = append(c.relocatedMemory, -1)
c.RelocatedMemory = append(c.RelocatedMemory, -1)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/vm/runners/cairo_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestRelocateMemory(t *testing.T) {
runner := runners.NewCairoRunner()
virtualMachine := vm.NewVirtualMachine()
segments := virtualMachine.Segments()
segments := &virtualMachine.Segments
for i := 0; i < 4; i++ {
segments.AddSegment()
}
Expand All @@ -34,10 +34,9 @@ func TestRelocateMemory(t *testing.T) {
t.Errorf("Test failed with error: %s", err)
}

actualMemory := *runner.RelocatedMemory()
expectedMemory := []int{-1, 4613515612218425347, 5, 2345108766317314046, 10, 10, -1, -1, -1, 5}
for i, v := range expectedMemory {
actual := actualMemory[i]
actual := runner.RelocatedMemory[i]
if actual != v {
t.Errorf("Expected relocated memory at index %d to be %d but it's %d", i, v, actual)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/vm/vm_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory"
type VirtualMachine struct {
runContext RunContext
currentStep uint
segments memory.MemorySegmentManager
Segments memory.MemorySegmentManager
}

func NewVirtualMachine() *VirtualMachine {
runContext := RunContext{pc: memory.NewRelocatable(0, 0), ap: 0, fp: 0}

Check failure on line 14 in pkg/vm/vm_core.go

View workflow job for this annotation

GitHub Actions / test_and_format

unknown field 'pc' in struct literal of type RunContext

Check failure on line 14 in pkg/vm/vm_core.go

View workflow job for this annotation

GitHub Actions / test_and_format

unknown field 'ap' in struct literal of type RunContext

Check failure on line 14 in pkg/vm/vm_core.go

View workflow job for this annotation

GitHub Actions / test_and_format

unknown field 'fp' in struct literal of type RunContext
segments := memory.NewMemorySegmentManager()

return &VirtualMachine{runContext: runContext, currentStep: 0, segments: *segments}
}

func (v *VirtualMachine) Segments() *memory.MemorySegmentManager {
return &v.segments
return &VirtualMachine{runContext: runContext, currentStep: 0, Segments: *segments}
}

0 comments on commit 4a0f451

Please sign in to comment.