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

Several bug fixes and improvements #43

Merged
merged 5 commits into from
Oct 23, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ func main() {
},
},
},
{
Name: "inspect",
Usage: "creates a user readable dump of the program instructions",
Action: inspect,
Flags: []cli.Flag{
cli.StringFlag{
Name: "in, i",
Usage: "input file of the program",
},
},
},
}
ctl.Run(os.Args)
}
Expand Down Expand Up @@ -158,7 +169,7 @@ func contractTestInvoke(ctx *cli.Context) error {
}

// FIXME: Make this configurable (probably in the general storm.yml file/config)
endpoint := "http://seed3.ngd.network:10332"
endpoint := "http://node1.ams2.bridgeprotocol.io:10332"
client, err := rpc.NewClient(context.TODO(), endpoint, rpc.ClientOptions{})
if err != nil {
return cli.NewExitError(err, 1)
Expand Down Expand Up @@ -234,3 +245,12 @@ func parseContractDetails() ContractDetails {

return details
}

func inspect(ctx *cli.Context) error {
src := ctx.String("in")
if len(src) == 0 {
return cli.NewExitError(errNoInput, 1)
}
compiler.CompileAndInspect(src)
return nil
}
16 changes: 16 additions & 0 deletions compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/token"
"go/types"
"log"
"strconv"
"strings"

"github.com/CityOfZion/neo-go/pkg/crypto"
Expand Down Expand Up @@ -248,6 +249,21 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
default:
log.Fatal("nested selector assigns not supported yet")
}

// Assignments to index expressions.
// slice[0] = 10
case *ast.IndexExpr:
ast.Walk(c, n.Rhs[i])
name := t.X.(*ast.Ident).Name
c.emitLoadLocal(name)
// For now storm only supports basic index operations. Hence we
// cast this to an *ast.BasicLit (1, 2 , 3)
indexStr := t.Index.(*ast.BasicLit).Value
index, err := strconv.Atoi(indexStr)
if err != nil {
log.Fatal("failed to convert slice index to integer")
}
c.emitStoreStructField(index)
}
}
return nil
Expand Down
26 changes: 18 additions & 8 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"text/tabwriter"

"github.com/CityOfZion/neo-storm/vm"
"golang.org/x/tools/go/loader"
)

Expand Down Expand Up @@ -89,16 +90,15 @@ func CompileAndSave(src string, o *Options) error {
if err != nil {
return fmt.Errorf("Error while trying to compile smart contract file: %v", err)
}
if o.Debug {
log.Println(hex.EncodeToString(b))
}

log.Println(hex.EncodeToString(b))

out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
return ioutil.WriteFile(out, b, os.ModePerm)
}

// DumpOpcode compiles the program and dumps the opcode in a user friendly format.
func DumpOpcode(src string) error {
// CompileAndInspect compiles the program and dumps the opcode in a user friendly format.
func CompileAndInspect(src string) error {
b, err := ioutil.ReadFile(src)
if err != nil {
return err
Expand All @@ -110,9 +110,19 @@ func DumpOpcode(src string) error {

w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintln(w, "INDEX\tOPCODE\tDESC\t")
for i := 0; i < len(b); i++ {
// TODO: generate stringer for instruction.
// fmt.Fprintf(w, "%d\t0x%2x\t%s\t\n", i, b[i], vm.Instruction(b[i]))

for i := 0; i <= len(b)-1; {
instr := vm.Instruction(b[i])
if instr >= vm.PUSHBYTES1 && instr <= vm.PUSHBYTES75 {
fmt.Fprintf(w, "%d\t0x%x\t%s\t\n", i, b[i], fmt.Sprintf("PUSHBYTES%d", int(instr)))
for x := 0; x < int(instr); x++ {
fmt.Fprintf(w, "%d\t0x%x\t%s\t\n", i, b[i+1+x], string(b[i+1+x]))
}
i += int(instr) + 1
continue
}
fmt.Fprintf(w, "%d\t0x%x\t%s\t\n", i, b[i], instr)
i++
}
w.Flush()
return nil
Expand Down
1 change: 1 addition & 0 deletions compiler/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var syscalls = map[string]map[string]string{
"GetTransaction": "Neo.Block.GetTransaction",
},
"transaction": {
"GetHash": "Neo.Transaction.GetHash",
"GetType": "Neo.Transaction.GetType",
"GetAttributes": "Neo.Transaction.GetAttributes",
"GetInputs": "Neo.Transaction.GetInputs",
Expand Down
4 changes: 2 additions & 2 deletions interop/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func GetAttributes(t Transaction) []attribute.Attribute {

// FIXME: What is the correct return type for this?
// GetReferences returns a slice of references for the given transaction.
func GetReferences(t Transaction) interface{} {
return 0
func GetReferences(t Transaction) []interface{} {
return []interface{}{}
}

// FIXME: What is the correct return type for this?
Expand Down
118 changes: 118 additions & 0 deletions vm/instruction_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.