Skip to content
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/ethereum/go-ethereum v1.10.9 // indirect
github.com/joho/godotenv v1.3.0
github.com/koinos/go-prompt v0.0.0-20221201222302-dba4c3542a91
github.com/koinos/koinos-proto-golang/v2 v2.3.0
github.com/koinos/koinos-proto-golang/v2 v2.6.0
github.com/koinos/koinos-util-golang/v2 v2.0.1
github.com/minio/sio v0.3.0
github.com/multiformats/go-multihash v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ github.com/koinos/go-prompt v0.0.0-20221201222302-dba4c3542a91/go.mod h1:Q5ndhnC
github.com/koinos/koinos-proto-golang/v2 v2.0.2/go.mod h1:BJg2czLOSVW2/ExpK/SBIrcN+N9bu7ejVyDnNYFuW/o=
github.com/koinos/koinos-proto-golang/v2 v2.3.0 h1:Snh50MJuV4jwVgtahB4XuOl21MEkjo+6gJJC/wMNl2k=
github.com/koinos/koinos-proto-golang/v2 v2.3.0/go.mod h1:BJg2czLOSVW2/ExpK/SBIrcN+N9bu7ejVyDnNYFuW/o=
github.com/koinos/koinos-proto-golang/v2 v2.6.0 h1:g2m1XKJ3VUl6T8zoeaC5PG5zVcitY8cMgLJUvIisosw=
github.com/koinos/koinos-proto-golang/v2 v2.6.0/go.mod h1:BJg2czLOSVW2/ExpK/SBIrcN+N9bu7ejVyDnNYFuW/o=
github.com/koinos/koinos-util-golang/v2 v2.0.1 h1:sKXIpko9BSPJ9Nymb4ES+2rtbQKYDapCv2HjMYuhuzo=
github.com/koinos/koinos-util-golang/v2 v2.0.1/go.mod h1:Iw80hOODPUeXU1rnfUgtn2hMT2GiuZ2h+bomNGL1q2A=
github.com/koinos/protobuf-go v1.27.2-0.20211026185306-2456c83214fe h1:PJ+2AnN4ibN2WxldiClplZZosQNPnXj7S5vOeFNtV+M=
Expand Down
24 changes: 19 additions & 5 deletions internal/cli/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ func (abi *ABI) GetFiles() (*protoregistry.Files, error) {

// ABIMethod represents an ABI method descriptor
type ABIMethod struct {
Argument string `json:"argument"`
Return string `json:"return"`
EntryPoint string `json:"entry-point"`
Description string `json:"description"`
ReadOnly bool `json:"read-only"`
Argument string `json:"argument"`
Return string `json:"return"`
EntryPoint uint64 `json:"entry_point"`
Description string `json:"description"`
ReadOnly bool `json:"read_only"`
EntryPointOld string `json:"entry-point"`
ReadOnlyOld bool `json:"read-only"`
}

// ContractInfo represents the information about a contract
Expand Down Expand Up @@ -424,3 +426,15 @@ func ParseResultToMessage(cmd *CommandParseResult, contracts Contracts) (proto.M

return DataToMessage(cmd.Args, md)
}

func GetEntryPoint(method *ABIMethod) (uint64, error) {
if len(method.EntryPointOld) > 2 && method.EntryPointOld[:2] == "0x" {
return strconv.ParseUint(method.EntryPointOld[2:], 16, 32)
} else {
return uint64(method.EntryPoint), nil
}
}

func GetReadOnly(method *ABIMethod) bool {
return method.ReadOnly || method.ReadOnlyOld
}
57 changes: 49 additions & 8 deletions internal/cli/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,65 @@ var (
"argument": "abi_test.empty_arguments",
"return": "abi_test.empty_result",
"description": "Empty arguments",
"entry_point": "0x2e1cfa82",
"entry_point": 773651074,
"read_only": false
},
"simple": {
"argument": "abi_test.simple_arguments",
"return": "abi_test.simple_result",
"description": "Simple arguments",
"entry_point": 2812517234,
"read_only": false
},
"nested": {
"argument": "abi_test.nested_arguments",
"return": "abi_test.nested_result",
"description": "Nested arguments",
"entry_point": 590701278,
"read_only": false
}
},
"types": "Cr4ECit0ZXN0X2FiaS9hc3NlbWJseS9wcm90by9jb25zdGVsbGF0aW9uLnByb3RvEghhYmlfdGVzdBoUa29pbm9zL29wdGlvbnMucHJvdG8iEQoPZW1wdHlfYXJndW1lbnRzIg4KDGVtcHR5X3Jlc3VsdCJOChBzaW1wbGVfYXJndW1lbnRzEg4KAmlkGAEgASgNUgJpZBISCgRuYW1lGAIgASgJUgRuYW1lEhYKBmFjdGl2ZRgDIAEoCFIGYWN0aXZlIg8KDXNpbXBsZV9yZXN1bHQiYgoQbmVzdGVkX2FyZ3VtZW50cxISCgRuYW1lGAEgASgJUgRuYW1lEiQKBGRhdGEYAiABKAsyEC5hYmlfdGVzdC5kYXRhX2NSBGRhdGESFAoFdmFsdWUYAyABKA1SBXZhbHVlIg8KDW5lc3RlZF9yZXN1bHQiRAoGZGF0YV9hEhQKBXZhbHVlGAEgASgNUgV2YWx1ZRISCgRuYW1lGAIgASgJUgRuYW1lEhAKA251bRgDIAEoCVIDbnVtIjQKBmRhdGFfYhIWCgZhY3RpdmUYASABKAhSBmFjdGl2ZRISCgRuYW1lGAIgASgJUgRuYW1lInIKBmRhdGFfYxISCgRuYW1lGAEgASgJUgRuYW1lEh4KAWEYAiABKAsyEC5hYmlfdGVzdC5kYXRhX2FSAWESFAoFdmFsdWUYAyABKA1SBXZhbHVlEh4KAWIYBCABKAsyEC5hYmlfdGVzdC5kYXRhX2JSAWJiBnByb3RvMw=="
}`

JSONABI_OLD = `{
"methods": {
"empty": {
"argument": "abi_test.empty_arguments",
"return": "abi_test.empty_result",
"description": "Empty arguments",
"entry-point": "0x2e1cfa82",
"read-only": false
},
"simple": {
"argument": "abi_test.simple_arguments",
"return": "abi_test.simple_result",
"description": "Simple arguments",
"entry_point": "0xa7a39b72",
"entry-point": "0xa7a39b72",
"read-only": false
},
"nested": {
"argument": "abi_test.nested_arguments",
"return": "abi_test.nested_result",
"description": "Nested arguments",
"entry_point": "0x233562de",
"entry-point": "0x233562de",
"read-only": false
}
},
"types": "Cr4ECit0ZXN0X2FiaS9hc3NlbWJseS9wcm90by9jb25zdGVsbGF0aW9uLnByb3RvEghhYmlfdGVzdBoUa29pbm9zL29wdGlvbnMucHJvdG8iEQoPZW1wdHlfYXJndW1lbnRzIg4KDGVtcHR5X3Jlc3VsdCJOChBzaW1wbGVfYXJndW1lbnRzEg4KAmlkGAEgASgNUgJpZBISCgRuYW1lGAIgASgJUgRuYW1lEhYKBmFjdGl2ZRgDIAEoCFIGYWN0aXZlIg8KDXNpbXBsZV9yZXN1bHQiYgoQbmVzdGVkX2FyZ3VtZW50cxISCgRuYW1lGAEgASgJUgRuYW1lEiQKBGRhdGEYAiABKAsyEC5hYmlfdGVzdC5kYXRhX2NSBGRhdGESFAoFdmFsdWUYAyABKA1SBXZhbHVlIg8KDW5lc3RlZF9yZXN1bHQiRAoGZGF0YV9hEhQKBXZhbHVlGAEgASgNUgV2YWx1ZRISCgRuYW1lGAIgASgJUgRuYW1lEhAKA251bRgDIAEoCVIDbnVtIjQKBmRhdGFfYhIWCgZhY3RpdmUYASABKAhSBmFjdGl2ZRISCgRuYW1lGAIgASgJUgRuYW1lInIKBmRhdGFfYxISCgRuYW1lGAEgASgJUgRuYW1lEh4KAWEYAiABKAsyEC5hYmlfdGVzdC5kYXRhX2FSAWESFAoFdmFsdWUYAyABKA1SBXZhbHVlEh4KAWIYBCABKAsyEC5hYmlfdGVzdC5kYXRhX2JSAWJiBnByb3RvMw=="
}`
)

func loadABI(t *testing.T) *ABI {
func loadABI(t *testing.T, jsonABI string) *ABI {
var abi ABI
err := json.Unmarshal([]byte(JSONABI), &abi)
err := json.Unmarshal([]byte(jsonABI), &abi)
assert.NoError(t, err)
return &abi
}

func loadContracts(t *testing.T) Contracts {
func loadContracts(t *testing.T, jsonABI string) Contracts {
contracts := Contracts(make(map[string]*ContractInfo))
abi := loadABI(t)
abi := loadABI(t, jsonABI)

files, err := abi.GetFiles()
assert.NoError(t, err)
Expand All @@ -68,7 +95,21 @@ func testMethod(t *testing.T, contracts Contracts, method string, expectedArgume
}

func TestABI(t *testing.T) {
contracts := loadContracts(t)
contracts := loadContracts(t, JSONABI)

// Test empty arguments
testMethod(t, contracts, "abi_test.empty", []string{})

// Test simple arguments
testMethod(t, contracts, "abi_test.simple", []string{"id", "name", "active"})

// Test nested arguments
testMethod(t, contracts, "abi_test.nested", []string{"name", "data.name", "data.a.value", "data.a.name", "data.a.num",
"data.value", "data.b.active", "data.b.name", "value"})
}

func TestABIOld(t *testing.T) {
contracts := loadContracts(t, JSONABI_OLD)

// Test empty arguments
testMethod(t, contracts, "abi_test.empty", []string{})
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/contract_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"os"
"strconv"

"github.com/btcsuite/btcutil/base58"
"github.com/koinos/koinos-cli/internal/cliutil"
Expand Down Expand Up @@ -125,7 +124,7 @@ func (c *RegisterCommand) Execute(ctx context.Context, ee *ExecutionEnvironment)

// Create the command
var cmd *CommandDeclaration
if method.ReadOnly {
if GetReadOnly(method) {
cmd = NewCommandDeclaration(commandName, method.Description, false, NewReadContractCommand, params...)
} else {
cmd = NewCommandDeclaration(commandName, method.Description, false, NewWriteContractCommand, params...)
Expand Down Expand Up @@ -171,7 +170,7 @@ func (c *ReadContractCommand) Execute(ctx context.Context, ee *ExecutionEnvironm

contract := ee.Contracts.GetFromMethodName(c.ParseResult.CommandName)

entryPoint, err := strconv.ParseUint(ee.Contracts.GetMethod(c.ParseResult.CommandName).EntryPoint[2:], 16, 32)
entryPoint, err := GetEntryPoint(ee.Contracts.GetMethod(c.ParseResult.CommandName))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -310,7 +309,7 @@ func (c *WriteContractCommand) Execute(ctx context.Context, ee *ExecutionEnviron

contract := ee.Contracts.GetFromMethodName(c.ParseResult.CommandName)

entryPoint, err := strconv.ParseUint(ee.Contracts.GetMethod(c.ParseResult.CommandName).EntryPoint[2:], 16, 32)
entryPoint, err := GetEntryPoint(ee.Contracts.GetMethod(c.ParseResult.CommandName))
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion internal/cli/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func NewCommandParser(commands *CommandSet) *CommandParser {
parser.commandNameRE = regexp.MustCompile(fmt.Sprintf(`^(%s+\.)?%s+`, CommandNameTokens, CommandNameTokens))
parser.skipRE = regexp.MustCompile(`^\s*`)
parser.terminatorRE = regexp.MustCompile(`^(;|$)`)
parser.addressRE = regexp.MustCompile(`^[1-9A-HJ-NP-Za-km-z]+`)
parser.addressRE = regexp.MustCompile(`^[1-9A-HJ-NP-Za-km-z]+|""`)
parser.simpleStringRE = regexp.MustCompile(`^[^\s"\';]+`)
parser.amountRE = regexp.MustCompile(`^((\d+(\.\d*)?)|(\.\d+))`)
parser.uintRE = regexp.MustCompile(`^[+]?[0-9]+`)
Expand Down Expand Up @@ -314,6 +314,10 @@ func (p *CommandParser) parseAddress(input []byte) ([]byte, int, error) {
return nil, 0, fmt.Errorf("%w", cliutil.ErrInvalidParam)
}

if string(m) == "\"\"" {
return make([]byte, 0), 2, nil
}

return m, len(m), nil
}

Expand Down
Loading