Skip to content

Commit

Permalink
WIP: state
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniRamirezM committed Nov 12, 2021
1 parent 8ca5e37 commit a6c49dd
Show file tree
Hide file tree
Showing 13 changed files with 1,274 additions and 0 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/hermeznetwork/hermez-core

go 1.16

require (
github.com/ethereum/go-ethereum v1.10.12
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.4
github.com/holiman/uint256 v1.2.0
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
)
582 changes: 582 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions state/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package state

// Batch
type Batch struct{}
34 changes: 34 additions & 0 deletions state/batchprocessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package state

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

// BatchProcessor is used to process a batch of transactions
type BatchProcessor struct{}

// ProcessBatch processes all transactions inside a batch
func (b *BatchProcessor) ProcessBatch(batch Batch) error {
return nil
}

// ProcessTransaction processes a transaction inside a batch
func (b *BatchProcessor) ProcessTransaction(tx types.Transaction) error {
return nil
}

// CheckTransaction checks a transaction is valid inside a batch context
func (b *BatchProcessor) CheckTransaction(tx types.Transaction) error {
return nil
}

// Commits the batch state into state
func (b *BatchProcessor) Commit() (*common.Hash, *Proof, error) {
return nil, nil, nil
}

// Rollback does not apply batch state into state
func (b *BatchProcessor) Rollback() error {
return nil
}
8 changes: 8 additions & 0 deletions state/db/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package db

// KeyValuer interface that a compatible DB backend should implement
type KeyValuer interface {
Put(key string, data []byte) error
// Get IMPORTANT: should return nil, nil in case of no data found!!!!
Get(key string) ([]byte, error)
}
4 changes: 4 additions & 0 deletions state/proof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package state

// Proof
type Proof struct{}
10 changes: 10 additions & 0 deletions state/runtime/evm/evm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package evm

// EVM is the Ethereum Virtual Machine
type EVM struct {
}

// NewEVM creates a new EVM
func NewEVM() *EVM {
return &EVM{}
}
1 change: 1 addition & 0 deletions state/runtime/evm/intructions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package evm
75 changes: 75 additions & 0 deletions state/runtime/evm/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package evm

import "github.com/holiman/uint256"

type Memory struct {
data []byte
}

// NewMemory is the constructor
func NewMemory() *Memory {
return &Memory{}
}

// Set sets a value at memory offset
func (m *Memory) Set(offset uint64, size uint64, value []byte) {
if size > 0 {
if offset+size > uint64(len(m.data)) {
panic("invalid memory")
}
copy(m.data[offset:offset+size], value)
}
}

// Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to
// 32 bytes.
func (m *Memory) Set32(offset uint64, val *uint256.Int) {
// length of store may never be less than offset + size.
// The store should be resized PRIOR to setting the memory
if offset+32 > uint64(len(m.data)) {
panic("invalid memory: store empty")
}
// Zero the memory area
copy(m.data[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
// Fill in relevant bits
val.WriteToSlice(m.data[offset:])
}

// Get returns size bytes from memory offset as a new slice
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
if size == 0 {
return nil
}

if len(m.data) > int(offset) {
cpy = make([]byte, size)
copy(cpy, m.data[offset:offset+size])

return
}

return
}

// GetPtr returns size bytes from memory offset
func (m *Memory) GetPtr(offset, size int64) []byte {
if size == 0 {
return nil
}

if len(m.data) > int(offset) {
return m.data[offset : offset+size]
}

return nil
}

// Len returns the length of the backing slice
func (m *Memory) Len() int {
return len(m.data)
}

// Data returns the backing slice
func (m *Memory) Data() []byte {
return m.data
}
Loading

0 comments on commit a6c49dd

Please sign in to comment.