Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AgwaB committed Feb 22, 2019
1 parent 6288992 commit 7346178
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 189 deletions.
166 changes: 85 additions & 81 deletions koa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"os"
"testing"

"bytes"
"encoding/hex"

"github.com/DE-labtory/koa/abi"
"github.com/DE-labtory/koa/translate"
)

Expand Down Expand Up @@ -372,85 +376,85 @@ func TestCompile(t *testing.T) {
}

func TestExecute(t *testing.T) {
///*
//contract {
// func addVariable() int {
// int a = 5
// int b = 10
// return a + b
// }
//
// func addNative() int {
// return 5 + 10
// }
//
// func addArgs(a int, b int) int {
// return a + b
// }
//}
// */
//firstContractRawBytecode, err := hex.DecodeString("21000000000000001b24302100000000652f6077141521000000000000001c29302100000000a82ed9f7141521000000000000003629302100000000bdbaf70e141521000000000000003c29322100000000000000052100000000000000082100000000000000002321000000000000000a210000000000000008210000000000000008232100000000000000082100000000000000002221000000000000000821000000000000000822012621000000000000000521000000000000000a01262100000000000000002521000000000000000821000000000000001023210000000000000001252100000000000000082100000000000000182321000000000000000821000000000000001022210000000000000008210000000000000018220126")
//if err != nil {
// t.Error(err)
//}
//
///*
//contract {
// func hello() string{
// return "hello!"
// }
//}
// */
//secondContractRawBytecode, err := hex.DecodeString("21000000000000000b2430210000000019ff1d21141521000000000000000c2932212268656c6c6f212226")
//if err != nil {
// t.Error(err)
//}
//
//firstArgs1, err := abi.Encode()
//firstArgs2, err := abi.Encode()
//firstArgs3, err := abi.Encode(5, 10)
//second1, err := abi.Encode()
//
//tests := []struct {
// RawBytecode []byte
// Func []byte
// Args []byte
// output []byte
//}{
// {
// RawBytecode: firstContractRawBytecode,
// Func: abi.Selector("addVariable()"),
// Args: firstArgs1,
// output: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
// },
// {
// RawBytecode: firstContractRawBytecode,
// Func: abi.Selector("addNative()"),
// Args: firstArgs2,
// output: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
// },
// {
// RawBytecode: firstContractRawBytecode,
// Func: abi.Selector("addArgs(int,int)"),
// Args: firstArgs3,
// output: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
// },
// {
// RawBytecode: secondContractRawBytecode,
// Func: abi.Selector("hello()"),
// Args: second1,
// output: []byte{0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x22},
// },
//}
//
//for _, test := range tests {
// output, err := Execute(test.RawBytecode, test.Func, test.Args)
// if err != nil {
// t.Error(err)
// }
//
// if !bytes.Equal(test.output, output) {
// t.Errorf("Invalid output - expected=%x, got=%x ", test.output, output)
// }
//}
/*
contract {
func addVariable() int {
int a = 5
int b = 10
return a + b
}
func addNative() int {
return 5 + 10
}
func addArgs(a int, b int) int {
return a + b
}
}
*/
firstContractRawBytecode, err := hex.DecodeString("2100000000000000202421000000000000001e25312100000000652f6077141521000000000000001f30312100000000a82ed9f7141521000000000000003930312100000000c6be6f42141521000000000000003f30332100000000000000052100000000000000082100000000000000002321000000000000000a210000000000000008210000000000000008232100000000000000082100000000000000002221000000000000000821000000000000000822012721000000000000000521000000000000000a01272100000000000000002621000000000000000821000000000000001023210000000000000001262100000000000000082100000000000000182321000000000000000821000000000000001022210000000000000008210000000000000018220127")
if err != nil {
t.Error(err)
}

/*
contract {
func hello() string{
return "hello!"
}
}
*/
secondContractRawBytecode, err := hex.DecodeString("2100000000000000002421000000000000000e2531210000000019ff1d21141521000000000000000f3033212268656c6c6f212227")
if err != nil {
t.Error(err)
}

firstArgs1, err := abi.Encode()
firstArgs2, err := abi.Encode()
firstArgs3, err := abi.Encode(5, 10)
second1, err := abi.Encode()

tests := []struct {
RawBytecode []byte
Func []byte
Args []byte
output []byte
}{
{
RawBytecode: firstContractRawBytecode,
Func: abi.Selector("addVariable()"),
Args: firstArgs1,
output: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
},
{
RawBytecode: firstContractRawBytecode,
Func: abi.Selector("addNative()"),
Args: firstArgs2,
output: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
},
{
RawBytecode: firstContractRawBytecode,
Func: abi.Selector("addArgs(int,int)"),
Args: firstArgs3,
output: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
},
{
RawBytecode: secondContractRawBytecode,
Func: abi.Selector("hello()"),
Args: second1,
output: []byte{0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x22},
},
}

for _, test := range tests {
output, err := Execute(test.RawBytecode, test.Func, test.Args)
if err != nil {
t.Error(err)
}

if !bytes.Equal(test.output, output) {
t.Errorf("Invalid output - expected=%x, got=%x ", test.output, output)
}
}
}
46 changes: 29 additions & 17 deletions opcode/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ const (
// memory[offset:offset+size] = value
Mstore Type = 0x23

// Set memory size
//
// Ex)
//
// [size] ==>
// [y] [y]
//
// memory[offset:offset+size] = value
Msize Type = 0x24

// Get the function selector information in the CallFunc.
// `Func` data is 4bytes of Keccak(function(params))
//
Expand All @@ -216,7 +226,7 @@ const (
// [x] ==> [x]
// [y] [y]
// [x] is 4byte which encoded as function selector
LoadFunc Type = 0x24
LoadFunc Type = 0x25

// Get the function arguments in the CallFunc.
// 'Args' is arguments encoded as a bytes value according to abi.
Expand All @@ -225,36 +235,36 @@ const (
//
// [index] ==> [Callfunc.Args[index]]
// [y] [y]
LoadArgs Type = 0x25
LoadArgs Type = 0x26

// Jump to position at which function was called
// Ex)
// [value]
// [funcSel]
// [position] ==> [value]
// [y] [y]
Returning Type = 0x26
Returning Type = 0x27

// pop the data which present specific pc to jump
// The opcode pointed to by pc must be JumpDst.
Jump Type = 0x27
Jump Type = 0x28

// jumpDst should be where the jump will be.
JumpDst Type = 0x28
JumpDst Type = 0x29

// Pop the first two items in the stack.
// First item pointed to where to jump.
// Second item should be bool data that decide to jump.
// If second item is false, jump to first item(pc) pointed to
Jumpi Type = 0x29
Jumpi Type = 0x30

// Duplicate data that exists at the top of the stack.
//
// Ex)
// [a]
// [a] ==> [a]
// [b] [b]
DUP Type = 0x30
DUP Type = 0x31

// Swap the first two items in the stack
//
Expand All @@ -263,10 +273,10 @@ const (
// [a] [b]
// [b] ==> [a]
// [c] [c]
SWAP Type = 0x31
SWAP Type = 0x32

// Jump to last position (Terminate the contract)
Exit Type = 0x32
Exit Type = 0x33
)

// Change the bytecode of an opcode to string.
Expand Down Expand Up @@ -309,22 +319,24 @@ func (p Type) String() (string, error) {
case 0x23:
return "Mstore", nil
case 0x24:
return "LoadFunc", nil
return "Msize", nil
case 0x25:
return "LoadArgs", nil
return "LoadFunc", nil
case 0x26:
return "Returning", nil
return "LoadArgs", nil
case 0x27:
return "Jump", nil
return "Returning", nil
case 0x28:
return "JumpDst", nil
return "Jump", nil
case 0x29:
return "Jumpi", nil
return "JumpDst", nil
case 0x30:
return "DUP", nil
return "Jumpi", nil
case 0x31:
return "SWAP", nil
return "DUP", nil
case 0x32:
return "SWAP", nil
case 0x33:
return "Exit", nil

default:
Expand Down
4 changes: 4 additions & 0 deletions opcode/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func TestType_String(t *testing.T) {
opcode.Mstore,
"Mstore",
},
{
opcode.Msize,
"Msize",
},
{
opcode.LoadFunc,
"LoadFunc",
Expand Down
10 changes: 7 additions & 3 deletions translate/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,21 @@ func CompileContract(c ast.Contract) (Asm, error) {
return *asm, err
}
}

println("!!! : " + asm.String())
// Compile Memory size with updated memory table.
// And replace expected memory size with new memory size of the memory table.
if err := compileMemSize(asm, memTracer); err != nil {
return *asm, err
}

println("!!! : " + asm.String())

// Compile Function jumper with updated FuncMap.
// And replace expected function jumper with new function jumper
if err := compileFuncJmpr(c, asm, funcMap); err != nil {
return *asm, err
}
println("!!! : " + asm.String())

return *asm, nil
}
Expand All @@ -88,6 +91,7 @@ func createMemSizePlaceholder(asm *Asm) error {
return err
}
asm.Emerge(opcode.Push, operand)
asm.Emerge(opcode.Msize)

return nil
}
Expand Down Expand Up @@ -143,7 +147,7 @@ func compileMemSize(asm *Asm, tracer MemTracer) error {
return err
}

if err := asm.ReplaceOperandAt(0, operand); err != nil {
if err := asm.ReplaceOperandAt(1, operand); err != nil {
return err
}

Expand Down Expand Up @@ -193,7 +197,7 @@ func fillFuncJmpr(asm *Asm, funcJmpr Asm) error {
}

for i, asmCode := range funcJmpr.AsmCodes {
asm.AsmCodes[i] = asmCode
asm.AsmCodes[i+3] = asmCode
}

return nil
Expand Down
Loading

0 comments on commit 7346178

Please sign in to comment.