Skip to content

Commit

Permalink
refactor: removing golden package that was shared between package cmd…
Browse files Browse the repository at this point in the history
… and integration_test, because infrastructure was too complex for the benefit it provided.

cmd and integration_test will have their own golden contract variables, in separate files. Tests will show if these are out of date for the compiler.

feature: CLI supports new arguments for compiler v4. Removed online chain broadcast test, it was being ignored anyway.
  • Loading branch information
randomshinichi committed Sep 11, 2019
1 parent 0c263d3 commit 490e19f
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 166 deletions.
14 changes: 0 additions & 14 deletions cmd/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func Test_broadcastFunc(t *testing.T) {
name string
args args
wantErr bool
online bool
}{
{
name: "Normal PostTransaction without error", // this looks like it tests nothing, but actually exercises hashing/cross checking code. the mock pretends that the node checked the hash and found that it matched.
Expand All @@ -79,23 +78,10 @@ func Test_broadcastFunc(t *testing.T) {
args: []string{"tx_+KgLAfhCuEAPX1l3BdFOcLeduH3PPwPV25mETXZE8IBDe6PGuasSEKJeB/cDDm+kW05Cdp38+mpvVSTTPMx7trL/7qxfUr8IuGD4XhYBoQHOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wGTcXVlcnkgU3BlY2lmaWNhdGlvbpZyZXNwb25zZSBTcGVjaWZpY2F0aW9uAABkhrXmIPSAAIIB9AHdGxXf"},
},
wantErr: false,
online: false,
},
{
name: "Online Status",
args: args{
conn: newAeNode(),
args: []string{"tx_+KgLAfhCuEAPX1l3BdFOcLeduH3PPwPV25mETXZE8IBDe6PGuasSEKJeB/cDDm+kW05Cdp38+mpvVSTTPMx7trL/7qxfUr8IuGD4XhYBoQHOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wGTcXVlcnkgU3BlY2lmaWNhdGlvbpZyZXNwb25zZSBTcGVjaWZpY2F0aW9uAABkhrXmIPSAAIIB9AHdGxXf"},
},
wantErr: false,
online: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !online && tt.online {
t.Skip("Skipping online test")
}
if err := broadcastFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("broadcastFunc() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
88 changes: 47 additions & 41 deletions cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func compileFunc(conn aeternity.CompileContracter, args []string) (err error) {
return err
}

bytecode, err := conn.CompileContract(s)
bytecode, err := conn.CompileContract(s, aeternity.Config.Compiler.Backend)
fmt.Println(bytecode)
return err
}
Expand All @@ -57,67 +57,72 @@ func encodeCalldataFunc(conn aeternity.EncodeCalldataer, args []string) (err err
return err
}

callData, err := conn.EncodeCalldata(s, args[1], args[2:])
callData, err := conn.EncodeCalldata(s, args[1], args[2:], aeternity.Config.Compiler.Backend)
if err != nil {
return err
}
fmt.Println(callData)
return
}

var decodeCalldataCmd = &cobra.Command{
Use: "decodeCalldata SOURCE_FILE/BYTECODE CALLDATA [..ARGS]",
type decodeCalldataer interface {
aeternity.DecodeCalldataBytecoder
aeternity.DecodeCalldataSourcer
}

var decodeCalldataBytecodeCmd = &cobra.Command{
Use: "decodeCalldataBytecode BYTECODE CALLDATA [..ARGS]",
Short: "Decode contract function calls. Needs the path to contract source file/compiled bytecode",
Long: ``,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return decodeCalldataFunc(compiler, args)
return decodeCalldataBytecodeFunc(compiler, args)
},
SilenceUsage: true,
}

type decodeCalldataer interface {
aeternity.DecodeCalldataBytecoder
aeternity.DecodeCalldataSourcer
}

func decodeCalldataFunc(conn decodeCalldataer, args []string) (err error) {
var decodeWithSource = func(path string, callData string) (function string, arguments []interface{}, err error) {
source, err := readSource(path)
if err != nil {
return
}
r, err := conn.DecodeCalldataSource(source, callData)
if err != nil {
return
}
arguments = r.Arguments
function = *r.Function
return
func decodeCalldataBytecodeFunc(conn decodeCalldataer, args []string) (err error) {
if !IsBytecode(args[0]) {
return fmt.Errorf("%s is not bytecode", args[0])
}
var decodeWithBytecode = func(bytecode string, callData string) (function string, arguments []interface{}, err error) {
r, err := conn.DecodeCalldataBytecode(bytecode, callData)
if err != nil {
return
}
arguments = r.Arguments
function = *r.Function
return
if !IsBytecode(args[1]) {
return fmt.Errorf("%s is not bytecode", args[0])
}

var function string
var arguments []interface{}
if !IsBytecode(args[0]) {
function, arguments, err = decodeWithSource(args[0], args[1])
} else {
function, arguments, err = decodeWithBytecode(args[0], args[1])
}
r, err := conn.DecodeCalldataBytecode(args[0], args[1], aeternity.Config.Compiler.Backend)
if err != nil {
return
}

fmt.Println(function, arguments)
fmt.Println(*r.Function, r.Arguments)
return nil
}

var decodeCalldataSourceCmd = &cobra.Command{
Use: "decodeCalldataSource SOURCE_FILE FUNCTION_NAME CALLDATA [..ARGS]",
Short: "Decode contract function calls. Needs the path to contract source file/compiled bytecode",
Long: ``,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return decodeCalldataSourceFunc(compiler, args)
},
SilenceUsage: true,
}

func decodeCalldataSourceFunc(conn decodeCalldataer, args []string) (err error) {
source, err := readSource(args[0])
if err != nil {
return err
}
if !IsBytecode(args[2]) {
return fmt.Errorf("%s is not bytecode", args[0])
}

r, err := conn.DecodeCalldataSource(source, args[1], args[2], aeternity.Config.Compiler.Backend)

fmt.Println(*r.Function, r.Arguments)
return
}

Expand All @@ -139,7 +144,7 @@ func generateAciFunc(conn aeternity.GenerateACIer, args []string) (err error) {
return
}

aci, err := conn.GenerateACI(source)
aci, err := conn.GenerateACI(source, aeternity.Config.Compiler.Backend)
if err != nil {
return
}
Expand All @@ -160,5 +165,6 @@ func init() {
RootCmd.AddCommand(contractCmd)
contractCmd.AddCommand(compileCmd)
contractCmd.AddCommand(encodeCalldataCmd)
contractCmd.AddCommand(decodeCalldataCmd)
contractCmd.AddCommand(decodeCalldataBytecodeCmd)
contractCmd.AddCommand(decodeCalldataSourceCmd)
}
72 changes: 46 additions & 26 deletions cmd/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"testing"

"github.com/aeternity/aepp-sdk-go/aeternity"
"github.com/aeternity/aepp-sdk-go/golden"
)

const contractSimpleStorageErr = "contract SimpleStorage =\n record state = { data : int }\n function init(value : int) : state = { data = value }\n function get() : int = state.data\n function set(value : int) = put(state{data = value})"

func Test_compileFunc(t *testing.T) {
type args struct {
conn aeternity.CompileContracter
Expand All @@ -25,7 +22,7 @@ func Test_compileFunc(t *testing.T) {
name: "Simple storage, mocked compiler",
args: args{
conn: &mockCompileContracter{},
source: golden.SimpleStorageSource,
source: contractSimpleStorage,
},
wantErr: false,
online: false,
Expand All @@ -34,7 +31,7 @@ func Test_compileFunc(t *testing.T) {
name: "Simple storage, online compiler: should compile",
args: args{
conn: newCompiler(),
source: golden.SimpleStorageSource,
source: contractSimpleStorage,
},
wantErr: false,
online: true,
Expand Down Expand Up @@ -81,7 +78,7 @@ func Test_encodeCalldataFunc(t *testing.T) {
args: args{
conn: &mockEncodeCalldataer{},
args: []string{"init", "42"},
source: golden.SimpleStorageSource,
source: contractSimpleStorage,
},
wantErr: false,
online: false,
Expand All @@ -91,7 +88,7 @@ func Test_encodeCalldataFunc(t *testing.T) {
args: args{
conn: newCompiler(),
args: []string{"init", "42"},
source: golden.SimpleStorageSource,
source: contractSimpleStorage,
},
wantErr: false,
online: true,
Expand All @@ -113,15 +110,11 @@ func Test_encodeCalldataFunc(t *testing.T) {
}
}

func Test_decodeCalldataFunc(t *testing.T) {
func Test_decodeCalldataBytecodeFunc(t *testing.T) {
type args struct {
conn decodeCalldataer
args []string
}
// Write source file for Decode with source file test
tempdir, path := writeTestContractFile(t, golden.SimpleStorageSource)
defer os.RemoveAll(tempdir)

tests := []struct {
name string
args args
Expand All @@ -131,35 +124,63 @@ func Test_decodeCalldataFunc(t *testing.T) {
{
name: "Decode with bytecode",
args: args{
conn: &mockdecodeCalldataer{decodedCalldata: `{"arguments":[{"type":"word","value":42}],"function":"init"}`},
args: []string{golden.SimpleStorageBytecode, golden.SimpleStorageCalldata},
conn: &mockdecodeCalldataer{decodedCalldata: `{"arguments":[{"type":"init","value":42}],"function":"init"}`},
args: []string{contractSimpleStorageBytecode, contractSimpleStorageInit42},
},
wantErr: false,
online: false,
},
{
name: "Decode with source file",
name: "Decode with bytecode (online)",
args: args{
conn: &mockdecodeCalldataer{decodedCalldata: `{"arguments":[{"type":"word","value":42}],"function":"init"}`},
args: []string{path, golden.SimpleStorageCalldata},
conn: newCompiler(),
args: []string{contractSimpleStorageBytecode, contractSimpleStorageInit42},
},
wantErr: false,
online: false,
online: true,
},
}

for _, tt := range tests {
if !online && tt.online {
t.Skip("Skipping online test")
}
t.Run(tt.name, func(t *testing.T) {
if err := decodeCalldataBytecodeFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("decodeCalldataFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_decodeCalldataSourceFunc(t *testing.T) {
type args struct {
conn decodeCalldataer
args []string
}
// Write source file for Decode with source file test
tempdir, path := writeTestContractFile(t, contractSimpleStorage)
defer os.RemoveAll(tempdir)

tests := []struct {
name string
args args
wantErr bool
online bool
}{
{
name: "Decode with bytecode (online)",
name: "Decode with source file",
args: args{
conn: newCompiler(),
args: []string{golden.SimpleStorageBytecode, golden.SimpleStorageCalldata},
conn: &mockdecodeCalldataer{decodedCalldata: `{"arguments":[{"type":"int","value":42}],"function":"init"}`},
args: []string{path, "init", contractSimpleStorageInit42},
},
wantErr: false,
online: true,
online: false,
},
{
name: "Decode with source file (online)",
args: args{
conn: newCompiler(),
args: []string{path, golden.SimpleStorageCalldata},
args: []string{path, "init", contractSimpleStorageInit42},
},
wantErr: false,
online: true,
Expand All @@ -171,16 +192,15 @@ func Test_decodeCalldataFunc(t *testing.T) {
t.Skip("Skipping online test")
}
t.Run(tt.name, func(t *testing.T) {
if err := decodeCalldataFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
if err := decodeCalldataSourceFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("decodeCalldataFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_generateAciFunc(t *testing.T) {
// Write source file for Decode with source file test
tempdir, path := writeTestContractFile(t, golden.SimpleStorageSource)
tempdir, path := writeTestContractFile(t, contractSimpleStorage)
defer os.RemoveAll(tempdir)
type args struct {
conn aeternity.GenerateACIer
Expand Down
18 changes: 18 additions & 0 deletions cmd/golden_contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

const contractSimpleStorageErr = `
contract SimpleStorage =
record state = { data : int }
function init(value : int) : state = { data = value }
function get() : int = state.data
function set(value : int) = put(state{data = value})
`
const contractSimpleStorage = `
contract SimpleStorage =
record state = { data : int }
entrypoint init(value : int) : state = { data = value }
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})
`
const contractSimpleStorageBytecode = `cb_+QOERgOgcPPaLbPhA5ZUYYI1viyMLIiudfF0RqUBtpvYh9wt5RL5Ao/5Aoyg4iMdbN/JORbeTLOphXv2XPQPwlb0oUmLP358mAwZk0SEaW5pdAC4wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACg//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALkBoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEA//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYD//////////////////////////////////////////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAuMJiAAA5YgAAbJGAgFF/4iMdbN/JORbeTLOphXv2XPQPwlb0oUmLP358mAwZk0QUYgAAsFdQYAEZUQBbYAAZWWAgAZCBUmAgkANgAFmQgVKBUllgIAGQgVJgIJADYAOBUpBZYABRWVJgAFJgAPNbYACAUmAA81uAWZCBUllgIAGQgVJgIJADYAAZWWAgAZCBUmAgkANgAFmQgVKBUllgIAGQgVJgIJADYAOBUoFSkFCQVltgIAFRUYOSUICRUFBiAAB0Vok0LjAuMC1yYzEAsRFc7A==`
const contractSimpleStorageInit42 = `cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACDiIx1s38k5Ft5Ms6mFe/Zc9A/CVvShSYs/fnyYDBmTRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo7j+li`
10 changes: 5 additions & 5 deletions cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@ func (m *mockGetNameEntryByNamer) GetNameEntryByName(name string) (nameEntry *mo

type mockCompileContracter struct{}

func (m *mockCompileContracter) CompileContract(source string) (bytecode string, err error) {
func (m *mockCompileContracter) CompileContract(source string, backend string) (bytecode string, err error) {
return "cb_+QYYRgKg+HOI9x+n5+MOEpnQ/zO+GoibqhQxGO4bgnvASx0vzB75BKX5AUmgOoWULXtHOgf10E7h2cFqXOqxa3kc6pKJYRpEw/nlugeDc2V0uMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoP//////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC4YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///////////////////////////////////////////jJoEnsSQdsAgNxJqQzA+rc5DsuLDKUV7ETxQp+ItyJgJS3g2dldLhgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA///////////////////////////////////////////uEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA+QKLoOIjHWzfyTkW3kyzqYV79lz0D8JW9KFJiz9+fJgMGZNEhGluaXS4wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACg//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALkBoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEA//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYD//////////////////////////////////////////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAuQFEYgAAj2IAAMKRgICAUX9J7EkHbAIDcSakMwPq3OQ7LiwylFexE8UKfiLciYCUtxRiAAE5V1CAgFF/4iMdbN/JORbeTLOphXv2XPQPwlb0oUmLP358mAwZk0QUYgAA0VdQgFF/OoWULXtHOgf10E7h2cFqXOqxa3kc6pKJYRpEw/nlugcUYgABG1dQYAEZUQBbYAAZWWAgAZCBUmAgkANgAFmQgVKBUllgIAGQgVJgIJADYAOBUpBZYABRWVJgAFJgAPNbYACAUmAA81tgAFFRkFZbYCABUVGQUIOSUICRUFCAWZCBUllgIAGQgVJgIJADYAAZWWAgAZCBUmAgkANgAFmQgVKBUllgIAGQgVJgIJADYAOBUoFSkFCQVltgIAFRUVlQgJFQUGAAUYFZkIFSkFBgAFJZkFCQVltQUFlQUGIAAMpWhTMuMS4wHchc+w==", nil
}

type mockEncodeCalldataer struct{}

func (m *mockEncodeCalldataer) EncodeCalldata(source string, function string, args []string) (bytecode string, err error) {
func (m *mockEncodeCalldataer) EncodeCalldata(source string, function string, args []string, backend string) (bytecode string, err error) {
return "cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACDiIx1s38k5Ft5Ms6mFe/Zc9A/CVvShSYs/fnyYDBmTRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo7j+li", nil
}

type mockdecodeCalldataer struct {
decodedCalldata string
}

func (m *mockdecodeCalldataer) DecodeCalldataSource(source string, callData string) (decodedCallData *compilermodels.DecodedCalldata, err error) {
func (m *mockdecodeCalldataer) DecodeCalldataSource(source string, function string, callData string, backend string) (decodedCallData *compilermodels.DecodedCalldata, err error) {
decodedCallData = &compilermodels.DecodedCalldata{}
decodedCallData.UnmarshalBinary([]byte(m.decodedCalldata))
return decodedCallData, nil
}
func (m *mockdecodeCalldataer) DecodeCalldataBytecode(bytecode string, calldata string) (decodedCallData *compilermodels.DecodedCalldata, err error) {
func (m *mockdecodeCalldataer) DecodeCalldataBytecode(bytecode string, calldata string, backend string) (decodedCallData *compilermodels.DecodedCalldata, err error) {
decodedCallData = &compilermodels.DecodedCalldata{}
decodedCallData.UnmarshalBinary([]byte(m.decodedCalldata))
return decodedCallData, nil
Expand All @@ -174,7 +174,7 @@ type mockGenerateACIer struct {
aci string
}

func (m *mockGenerateACIer) GenerateACI(source string) (aci *compilermodels.ACI, err error) {
func (m *mockGenerateACIer) GenerateACI(source string, backend string) (aci *compilermodels.ACI, err error) {
aci = &compilermodels.ACI{}
err = aci.UnmarshalBinary([]byte(m.aci))
return aci, err
Expand Down

0 comments on commit 490e19f

Please sign in to comment.