-
Notifications
You must be signed in to change notification settings - Fork 379
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
[WIP]feat: vm result, error handling #718
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package gnolang | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// These convert string representations of public-facing arguments to GNO types. | ||
// The limited set of input types available should map 1:1 to types supported | ||
// in FunctionSignature{}. | ||
// String representation of arg must be deterministic. | ||
// NOTE: very important that there is no malleability. | ||
func ConvertArgToGno(arg string, argT Type) (tv TypedValue) { | ||
tv.T = argT | ||
switch bt := BaseOf(argT).(type) { | ||
case PrimitiveType: | ||
switch bt { | ||
case BoolType: | ||
if arg == "true" { | ||
tv.SetBool(true) | ||
return | ||
} else if arg == "false" { | ||
tv.SetBool(false) | ||
return | ||
} else { | ||
panic(fmt.Sprintf( | ||
"unexpected bool value %q", | ||
arg)) | ||
} | ||
case StringType: | ||
tv.SetString(StringValue(arg)) | ||
return | ||
case IntType: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
i64, err := strconv.ParseInt(arg, 10, 64) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing int %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetInt(int(i64)) | ||
return | ||
case Int8Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
i8, err := strconv.ParseInt(arg, 10, 8) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing int8 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetInt8(int8(i8)) | ||
return | ||
case Int16Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
i16, err := strconv.ParseInt(arg, 10, 16) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing int16 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetInt16(int16(i16)) | ||
return | ||
case Int32Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
i32, err := strconv.ParseInt(arg, 10, 32) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing int32 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetInt32(int32(i32)) | ||
return | ||
case Int64Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
i64, err := strconv.ParseInt(arg, 10, 64) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing int64 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetInt64(i64) | ||
return | ||
case UintType: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
u64, err := strconv.ParseUint(arg, 10, 64) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing uint %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetUint(uint(u64)) | ||
return | ||
case Uint8Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
u8, err := strconv.ParseUint(arg, 10, 8) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing uint8 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetUint8(uint8(u8)) | ||
return | ||
case Uint16Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
u16, err := strconv.ParseUint(arg, 10, 16) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing uint16 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetUint16(uint16(u16)) | ||
return | ||
case Uint32Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
u32, err := strconv.ParseUint(arg, 10, 32) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing uint32 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetUint32(uint32(u32)) | ||
return | ||
case Uint64Type: | ||
if arg[0] == '+' { | ||
panic("numbers cannot start with +") | ||
} | ||
u64, err := strconv.ParseUint(arg, 10, 64) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing uint64 %q: %v", | ||
arg, err)) | ||
} | ||
tv.SetUint64(uint64(u64)) | ||
return | ||
default: | ||
panic(fmt.Sprintf("unexpected primitive type %s", bt.String())) | ||
} | ||
case *ArrayType: | ||
if bt.Elt == Uint8Type { | ||
bz, err := base64.StdEncoding.DecodeString(arg) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing byte array %q: %v", | ||
arg, err)) | ||
} | ||
tv.V = &ArrayValue{ | ||
Data: bz, | ||
} | ||
return | ||
} else { | ||
panic("unexpected array type in contract arg") | ||
} | ||
case *SliceType: | ||
if bt.Elt == Uint8Type { | ||
bz, err := base64.StdEncoding.DecodeString(arg) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"error parsing byte array %q: %v", | ||
arg, err)) | ||
} | ||
tv.V = &SliceValue{ | ||
Base: &ArrayValue{ | ||
Data: bz, | ||
}, | ||
Offset: 0, | ||
Length: len(bz), | ||
Maxcap: len(bz), | ||
} | ||
return | ||
} else { | ||
panic("unexpected slice type in contract arg") | ||
} | ||
default: | ||
panic(fmt.Sprintf("unexpected type in contract arg: %v", argT)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package std | ||
|
||
import "errors" | ||
|
||
const ( | ||
ModuleName = "vm" | ||
RouterKey = ModuleName | ||
) | ||
// std result | ||
type Result struct { | ||
Response | ||
// GasWanted int64 | ||
// GasUsed int64 | ||
// additional fields? | ||
} | ||
|
||
func GnoResult(r Response) *Result { | ||
return &Result{Response: r} | ||
} | ||
|
||
type Event string | ||
|
||
func (e Event) AssertABCIEvent() {} | ||
|
||
type Response struct { | ||
ErrMsg string // TODO: user err_code instead, for safety | ||
Data []byte | ||
Events []Event | ||
// gas limit? | ||
|
||
Log string // nondeterministic | ||
Info string // nondeterministic | ||
} | ||
|
||
func NewResponse() Response { | ||
return Response{} | ||
} | ||
|
||
func (r Response) WithData(data []byte) Response { | ||
r.Data = data | ||
return r | ||
} | ||
|
||
func (r Response) WithEvents(events []Event) Response { | ||
r.Events = events | ||
return r | ||
} | ||
|
||
func (r Response) WithLog(log string) Response { | ||
r.Log = log | ||
return r | ||
} | ||
|
||
func (r Response) WithInfo(info string) Response { | ||
r.Info = info | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this copied from anywhere?