Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct OpCodes #32

Merged
merged 7 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/bazo-blockchain/bazo-miner v0.0.0-20190416182631-de8b4c07084f
github.com/google/go-cmp v0.2.0 // indirect
github.com/pkg/errors v0.8.1
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/willf/bitset v1.1.10 // indirect
golang.org/x/crypto v0.0.0-20190417174047-f416ebab96af
Expand Down
18 changes: 9 additions & 9 deletions vm/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestArray_IncerementSize(t *testing.T) {
}

func TestArray_DecrementSize(t *testing.T) {
a := Array([]byte{0x02, 0x02, 0x00})
a := Array([]byte{0x02, 0x00, 0x02})

s, err := ByteArrayToUI16(a[1:3])
if s != 2 || err != nil {
Expand All @@ -46,11 +46,11 @@ func TestArray_DecrementSize(t *testing.T) {

func TestArray_At(t *testing.T) {
a := Array([]byte{0x02,
0x03, 0x00,
0x00, 0x03,

0x08, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x65, 0x00, 0x00, 0x00,
0x02, 0x00, 0x65, 0x00,
0x00, 0x08, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x65, 0x00, 0x00, 0x00,
0x00, 0x02, 0x65, 0x00,
})

expected0 := []byte{0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Expand Down Expand Up @@ -84,11 +84,11 @@ func TestArray_At(t *testing.T) {

func TestArray_Insert(t *testing.T) {
a := Array([]byte{0x02,
0x03, 0x00,
0x00, 0x03,

0x08, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x65, 0x00, 0x00, 0x00,
0x02, 0x00, 0x65, 0x00,
0x00, 0x08, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x65, 0x00, 0x00, 0x00,
0x00, 0x02, 0x65, 0x00,
})

v := []byte{0x01}
Expand Down
2 changes: 1 addition & 1 deletion vm/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMap_IncerementSize(t *testing.T) {
}

func TestMap_DecrementSize(t *testing.T) {
a := Array([]byte{0x02, 0x02, 0x00})
a := Array([]byte{0x02, 0x00, 0x02})

s, err := ByteArrayToUI16(a[1:3])
if s != 2 || err != nil {
Expand Down
6 changes: 6 additions & 0 deletions vm/op_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const (
ArrInsert
ArrRemove
ArrAt
NewStr
StoreFld
LoadFld
SHA3
CheckSig
ErrHalt
Expand Down Expand Up @@ -134,6 +137,9 @@ var OpCodes = []OpCode{
{ArrInsert, "arrinsert", 0, nil, 1, 2},
{ArrRemove, "arrremove", 0, nil, 1, 2},
{ArrAt, "arrat", 0, nil, 1, 2},
{NewStr, "newstr", 1, []int{BYTE}, 1, 2},
{StoreFld, "storefld", 1, []int{BYTE}, 1, 2},
{LoadFld, "loadfld", 1, []int{BYTE}, 1, 2},
{SHA3, "sha3", 0, nil, 1, 2},
{CheckSig, "checksig", 0, nil, 1, 2},
{ErrHalt, "errhalt", 0, nil, 0, 1},
Expand Down
61 changes: 61 additions & 0 deletions vm/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package vm

import (
"github.com/pkg/errors"
)

// Struct type represents the composite data type declaration that
// defines a group of variables.
type Struct Array

// NewStruct creates a new struct data structure.
func newStruct(size uint16) Struct {
array := NewArray()
for i := uint16(0); i < size; i++ {
_ = array.Append([]byte{0})
}
return Struct(array)
}

func structFromByteArray(arr []byte) (Struct, error) {
array, err := ArrayFromByteArray(arr)
if err != nil {
return nil, err
}

return Struct(array), nil
}

func (s *Struct) toArray() *Array {
return (*Array)(s)
}

// loadField returns the field at the given index
func (s *Struct) loadField(index uint16) ([]byte, error) {
array := s.toArray()
return array.At(index)
}

// storeField sets the element on the given index
func (s *Struct) storeField(index uint16, element []byte) error {
array := s.toArray()
size, err := array.getSize()
if err != nil {
return err
}

if index >= size {
return errors.New("index out of bounds")
}

// Array insert does not work for an array with size = 1
if size == index+1 {
err := array.Remove(index)
if err != nil {
return err
}
err = array.Append(element)
return err
}
return array.Insert(index, element)
}
56 changes: 56 additions & 0 deletions vm/struct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package vm

import (
"gotest.tools/assert"
"testing"
)

func TestStruct_NewStruct(t *testing.T) {
s := newStruct(2)
array := s.toArray()
size, err := array.getSize()

assert.NilError(t, err)
assert.Equal(t, size, uint16(2))

// Initial field value at index 0
elementAt, atErr := array.At(0)
assert.NilError(t, atErr)
assertBytes(t, elementAt, 0)

// Initial field value at index 1
elementAt, atErr = array.At(1)
assert.NilError(t, atErr)
assertBytes(t, elementAt, 0)
}

func TestStruct_StoreField(t *testing.T) {
s := newStruct(1)
element := []byte{2}

err := s.storeField(0, element)
assert.NilError(t, err)

fieldValue, loadErr := s.loadField(0)
assert.NilError(t, loadErr)
assertBytes(t, fieldValue, element...)
}

func TestStruct_StoreFields(t *testing.T) {
s := newStruct(2)
element1 := []byte{2}
element2 := []byte{3}

err := s.storeField(0, element1)
assert.NilError(t, err)
err = s.storeField(1, element2)
assert.NilError(t, err)

fieldValue, loadErr := s.loadField(0)
assert.NilError(t, loadErr)
assertBytes(t, fieldValue, element1...)

fieldValue, loadErr = s.loadField(1)
assert.NilError(t, loadErr)
assertBytes(t, fieldValue, element2...)
}
4 changes: 2 additions & 2 deletions vm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func UInt64ToByteArray(element uint64) []byte {

func UInt16ToByteArray(element uint16) []byte {
ba := make([]byte, 2)
binary.LittleEndian.PutUint16(ba, uint16(element))
binary.BigEndian.PutUint16(ba, uint16(element))
return ba
}

Expand All @@ -30,7 +30,7 @@ func ByteArrayToUI16(element []byte) (uint16, error) {
return 0, errors.New("byte array to uint16 invalid parameters provided")
}

return binary.LittleEndian.Uint16(element), nil
return binary.BigEndian.Uint16(element), nil
}

func StrToBigInt(element string) big.Int {
Expand Down
71 changes: 71 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"golang.org/x/crypto/sha3"
)

// Context is the VM execution context
// which is composed with data coming from the transaction and the account.
// Context interface declares functions required to start the execution of the contract.
type Context interface {
GetContract() []byte
GetContractVariable(index int) ([]byte, error)
Expand Down Expand Up @@ -1179,7 +1182,71 @@ func (vm *VM) Exec(trace bool) bool {
vm.evaluationStack.Push([]byte(opCode.Name + ": " + err.Error()))
return false
}
case NewStr:
sizeBytes, err := vm.fetchMany(opCode.Name, 2)
if err != nil {
vm.pushError(opCode, err)
return false
}

size, err := ByteArrayToUI16(sizeBytes)
if err != nil {
vm.pushError(opCode, err)
return false
}

str := newStruct(size)
err = vm.evaluationStack.Push(str)
if err != nil {
return false
}
case StoreFld:
indexBytes, indexErr := vm.fetchMany(opCode.Name, 2)
element, elementErr := vm.PopBytes(opCode)
structBytes, structErr := vm.PopBytes(opCode)

if !vm.checkErrors(opCode.Name, structErr, indexErr, elementErr) {
return false
}

str, structErr := structFromByteArray(structBytes)
index, indexErr := ByteArrayToUI16(indexBytes)
if !vm.checkErrors(opCode.Name, structErr, indexErr) {
return false
}

err := str.storeField(index, element)
if err != nil {
vm.pushError(opCode, err)
return false
}
err = vm.evaluationStack.Push(str)
if err != nil {
return false
}
case LoadFld:
indexBytes, indexErr := vm.fetchMany(opCode.Name, 2)
structBytes, structErr := vm.PopBytes(opCode)

if !vm.checkErrors(opCode.Name, structErr, indexErr) {
return false
}

str, structErr := structFromByteArray(structBytes)
index, indexErr := ByteArrayToUI16(indexBytes)
if !vm.checkErrors(opCode.Name, structErr, indexErr) {
return false
}

element, err := str.loadField(index)
if err != nil {
vm.pushError(opCode, err)
return false
}
err = vm.evaluationStack.Push(element)
if err != nil {
return false
}
case SHA3:
right, err := vm.PopBytes(opCode)
if err != nil {
Expand Down Expand Up @@ -1267,6 +1334,10 @@ func (vm *VM) checkErrors(errorLocation string, errors ...error) bool {
return true
}

func (vm *VM) pushError(opCode OpCode, err error) {
_ = vm.evaluationStack.Push([]byte(opCode.Name + ": " + err.Error()))
}

func (vm *VM) PopBytes(opCode OpCode) (elements []byte, err error) {
bytes, err := vm.evaluationStack.Pop()
if err != nil {
Expand Down
Loading