Skip to content

Commit

Permalink
refactor: drop old nodes support and compiler switch
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 10, 2021
1 parent ae6f6df commit f8ab7a5
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 97 deletions.
12 changes: 6 additions & 6 deletions aeternity/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func DefaultCallResultListener(node callResultListener, txHash string, callResul
}

// Deploy lets one deploy a contract with minimum fuss.
func (c *Contract) Deploy(source, function string, args []string, backend string) (ctID string, createTxReceipt *TxReceipt, err error) {
bytecode, err := c.ctx.Compiler().CompileContract(source, backend)
func (c *Contract) Deploy(source, function string, args []string) (ctID string, createTxReceipt *TxReceipt, err error) {
bytecode, err := c.ctx.Compiler().CompileContract(source)
if err != nil {
return
}
calldata, err := c.ctx.Compiler().EncodeCalldata(source, function, args, backend)
calldata, err := c.ctx.Compiler().EncodeCalldata(source, function, args)
if err != nil {
return
}
Expand All @@ -58,7 +58,7 @@ func (c *Contract) Deploy(source, function string, args []string, backend string
if err != nil {
return
}
VMVersion, ABIVersion, err := findVMABIVersion(version, backend)
VMVersion, ABIVersion, err := findVMABIVersion(version)
if err != nil {
return
}
Expand All @@ -78,8 +78,8 @@ func (c *Contract) Deploy(source, function string, args []string, backend string

// Call calls a smart contract's function, automatically calling the
// compiler to transform the arguments into bytecode.
func (c *Contract) Call(ctID, source, function string, args []string, backend string) (txReceipt *TxReceipt, err error) {
callData, err := c.ctx.Compiler().EncodeCalldata(source, function, args, backend)
func (c *Contract) Call(ctID, source, function string, args []string) (txReceipt *TxReceipt, err error) {
callData, err := c.ctx.Compiler().EncodeCalldata(source, function, args)
if err != nil {
return
}
Expand Down
11 changes: 3 additions & 8 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,9 @@ func (t *TxReceipt) Watch(mined chan bool, waitBlocks uint64, node transactionWa
mined <- false
}

func findVMABIVersion(nodeVersion, compilerBackend string) (VMVersion, ABIVersion uint16, err error) {
if nodeVersion[0] == '5' && compilerBackend == "fate" {
func findVMABIVersion(nodeVersion string) (VMVersion, ABIVersion uint16, err error) {
if nodeVersion[0] == '5' {
return 5, 3, nil
} else if nodeVersion[0] == '5' && compilerBackend == "aevm" {
return 6, 1, nil
} else if nodeVersion[0] == '4' {
return 4, 1, nil
} else {
return 0, 0, errors.New("other node versions unsupported")
}
return 0, 0, errors.New("node is unsupported")
}
32 changes: 1 addition & 31 deletions aeternity/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func TestSignBroadcast(t *testing.T) {
func Test_findVMABIVersion(t *testing.T) {
type args struct {
nodeVersion string
compilerBackend string
}
tests := []struct {
name string
Expand All @@ -225,50 +224,21 @@ func Test_findVMABIVersion(t *testing.T) {
name: "node version 5, FATE backend",
args: args{
nodeVersion: "5",
compilerBackend: "fate",
},
wantVMVersion: 5,
wantABIVersion: 3,
},
{
name: "node version 5, AEVM backend",
args: args{
nodeVersion: "5",
compilerBackend: "aevm",
},
wantVMVersion: 6,
wantABIVersion: 1,
},
{
name: "node version 4, AEVM backend",
args: args{
nodeVersion: "4",
compilerBackend: "aevm",
},
wantVMVersion: 4,
wantABIVersion: 1,
},
{
name: "node version 4, does not actually support FATE, so it should return answer for AEVM anyway",
args: args{
nodeVersion: "4",
compilerBackend: "fate",
},
wantVMVersion: 4,
wantABIVersion: 1,
},
{
name: "Other versions of the node are not supported",
args: args{
nodeVersion: "3",
compilerBackend: "aevm",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotVMVersion, gotABIVersion, err := findVMABIVersion(tt.args.nodeVersion, tt.args.compilerBackend)
gotVMVersion, gotABIVersion, err := findVMABIVersion(tt.args.nodeVersion)
if (err != nil) != tt.wantErr {
t.Errorf("findVMABIVersion() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
11 changes: 5 additions & 6 deletions cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"

"github.com/aeternity/aepp-sdk-go/v8/config"
"github.com/aeternity/aepp-sdk-go/v8/naet"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -35,7 +34,7 @@ func compileFunc(conn naet.CompileContracter, args []string) (err error) {
return err
}

bytecode, err := conn.CompileContract(s, config.Compiler.Backend)
bytecode, err := conn.CompileContract(s)
fmt.Println(bytecode)
return err
}
Expand All @@ -58,7 +57,7 @@ func encodeCalldataFunc(conn naet.EncodeCalldataer, args []string) (err error) {
return err
}

callData, err := conn.EncodeCalldata(s, args[1], args[2:], config.Compiler.Backend)
callData, err := conn.EncodeCalldata(s, args[1], args[2:])
if err != nil {
return err
}
Expand Down Expand Up @@ -91,7 +90,7 @@ func decodeCalldataBytecodeFunc(conn decodeCalldataer, args []string) (err error
return fmt.Errorf("%s is not bytecode", args[0])
}

r, err := conn.DecodeCalldataBytecode(args[0], args[1], config.Compiler.Backend)
r, err := conn.DecodeCalldataBytecode(args[0], args[1])
if err != nil {
return
}
Expand Down Expand Up @@ -121,7 +120,7 @@ func decodeCalldataSourceFunc(conn decodeCalldataer, args []string) (err error)
return fmt.Errorf("%s is not bytecode", args[0])
}

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

fmt.Println(*r.Function, r.Arguments)
return
Expand All @@ -145,7 +144,7 @@ func generateAciFunc(conn naet.GenerateACIer, args []string) (err error) {
return
}

aci, err := conn.GenerateACI(source, config.Compiler.Backend)
aci, err := conn.GenerateACI(source)
if err != nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,26 @@ func (m *mockGetNameEntryByNamer) GetNameEntryByName(name string) (nameEntry *mo

type mockCompileContracter struct{}

func (m *mockCompileContracter) CompileContract(source string, backend string) (bytecode string, err error) {
func (m *mockCompileContracter) CompileContract(source 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, backend string) (bytecode string, err error) {
func (m *mockEncodeCalldataer) EncodeCalldata(source string, function string, args []string) (bytecode string, err error) {
return "cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACDiIx1s38k5Ft5Ms6mFe/Zc9A/CVvShSYs/fnyYDBmTRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo7j+li", nil
}

type mockdecodeCalldataer struct {
decodedCalldata string
}

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

func (m *mockGenerateACIer) GenerateACI(source string, backend string) (aci *compilermodels.ACI, err error) {
func (m *mockGenerateACIer) GenerateACI(source string) (aci *compilermodels.ACI, err error) {
aci = &compilermodels.ACI{}
err = aci.UnmarshalBinary([]byte(m.aci))
return aci, err
Expand Down
8 changes: 0 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ const (
NetworkIDTestnet = "ae_uat"
// URLTestnet is the URL to an aeternity Foundation maintained node
URLTestnet = "https://testnet.aeternity.io"
// CompilerBackendFATE indicates that the compiler should use the FATE VM
// for contract bytecode execution
CompilerBackendFATE = "fate"
// CompilerBackendAEVM indicates that the compiler should use the AEVM for
// contract bytecode execution
CompilerBackendAEVM = "aevm"
// OracleTTLTypeDelta indicates that the accompanying TTL value (in blocks)
// should be interpreted as currentHeight + TTLValue
OracleTTLTypeDelta = 0
Expand All @@ -45,7 +39,6 @@ type NodeConfig struct {
// CompilerConfig configuration for the compiler
type CompilerConfig struct {
URL string `json:"url" yaml:"url" mapstructure:"url"`
Backend string `json:"backend" yaml:"backend" mapstructure:"backend"`
}

// AensConfig contains default parameters for AENS
Expand Down Expand Up @@ -161,7 +154,6 @@ var Node = NodeConfig{
// Compiler holds default settings for CompilerConfig
var Compiler = CompilerConfig{
URL: "http://localhost:3080",
Backend: CompilerBackendFATE,
}

// Client holds default settings for ClientConfig
Expand Down
17 changes: 8 additions & 9 deletions integration_test/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"reflect"
"testing"

"github.com/aeternity/aepp-sdk-go/v8/config"
"github.com/aeternity/aepp-sdk-go/v8/naet"
"gotest.tools/golden"
)
Expand All @@ -23,7 +22,7 @@ func TestCompiler(t *testing.T) {
}
})
t.Run("CompileContract", func(t *testing.T) {
_, err := c.CompileContract(string(golden.Get(t, simplestorageSource)), config.Compiler.Backend)
_, err := c.CompileContract(string(golden.Get(t, simplestorageSource)))
if err != nil {
t.Error(err)
}
Expand All @@ -39,27 +38,27 @@ func TestCompiler(t *testing.T) {
ae_addres
`

_, err := c.CompileContract(wontcompileSource, config.Compiler.Backend)
_, err := c.CompileContract(wontcompileSource)
errtype := reflect.TypeOf(err).String()
if errtype != "*operations.CompileContractBadRequest" {
t.Error(err)
}
})
t.Run("DecodeCallResult", func(t *testing.T) {
// taken from contract_test.go running main(42) on identity.aes, GET /transactions/th_.../info
_, err := c.DecodeCallResult("ok", "cb_VNLOFXc=", "main", string(golden.Get(t, identitySource)), config.Compiler.Backend)
_, err := c.DecodeCallResult("ok", "cb_VNLOFXc=", "main", string(golden.Get(t, identitySource)))
if err != nil {
t.Error(err)
}
})
t.Run("DecodeCalldataBytecode", func(t *testing.T) {
_, err := c.DecodeCalldataBytecode(string(golden.Get(t, simplestorageBytecode)), string(golden.Get(t, simplestorageCalldata)), config.Compiler.Backend)
_, err := c.DecodeCalldataBytecode(string(golden.Get(t, simplestorageBytecode)), string(golden.Get(t, simplestorageCalldata)))
if err != nil {
t.Error(err)
}
})
t.Run("DecodeCalldataSource", func(t *testing.T) {
_, err := c.DecodeCalldataSource(string(golden.Get(t, simplestorageSource)), "init", string(golden.Get(t, simplestorageCalldata)), config.Compiler.Backend)
_, err := c.DecodeCalldataSource(string(golden.Get(t, simplestorageSource)), "init", string(golden.Get(t, simplestorageCalldata)))
if err != nil {
t.Error(err)
}
Expand All @@ -72,21 +71,21 @@ func TestCompiler(t *testing.T) {
}
})
t.Run("EncodeCalldata SimpleStorage set(123)", func(t *testing.T) {
encodedCalldata, err := c.EncodeCalldata(string(golden.Get(t, simplestorageSource)), "set", []string{"123"}, config.Compiler.Backend)
encodedCalldata, err := c.EncodeCalldata(string(golden.Get(t, simplestorageSource)), "set", []string{"123"})
if err != nil {
t.Error(err)
}
golden.Assert(t, encodedCalldata, "simplestorage_set123.txt")
})
t.Run("EncodeCalldata SimpleStorage init(42)", func(t *testing.T) {
encodedCalldata, err := c.EncodeCalldata(string(golden.Get(t, simplestorageSource)), "init", []string{"42"}, config.Compiler.Backend)
encodedCalldata, err := c.EncodeCalldata(string(golden.Get(t, simplestorageSource)), "init", []string{"42"})
if err != nil {
t.Error(err)
}
golden.Assert(t, encodedCalldata, "simplestorage_init42.txt")
})
t.Run("GenerateACI", func(t *testing.T) {
_, err := c.GenerateACI(string(golden.Get(t, simplestorageSource)), config.Compiler.Backend)
_, err := c.GenerateACI(string(golden.Get(t, simplestorageSource)))
if err != nil {
t.Error(err)
}
Expand Down
6 changes: 3 additions & 3 deletions integration_test/contract_higherlevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract SimpleStorage =
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})`

ctID, _, err := contract.Deploy(simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
ctID, _, err := contract.Deploy(simplestorage, "init", []string{"42"})
if err != nil {
t.Error(err)
}
Expand All @@ -50,12 +50,12 @@ contract SimpleStorage =
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})`

ctID, _, err := contract.Deploy(simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
ctID, _, err := contract.Deploy(simplestorage, "init", []string{"42"})
if err != nil {
t.Fatal(err)
}

callReceipt, err := contract.Call(ctID, simplestorage, "get", []string{}, config.CompilerBackendFATE)
callReceipt, err := contract.Call(ctID, simplestorage, "get", []string{})
if err != nil {
t.Error(err)
}
Expand Down
6 changes: 3 additions & 3 deletions integration_test/ga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func TestGeneralizedAccounts(t *testing.T) {

authorizeSource := string(golden.Get(t, "authorize.aes"))
// Read the auth contract from a file, compile and prepare its init() calldata
authBytecode, err := compiler.CompileContract(authorizeSource, config.Compiler.Backend)
authBytecode, err := compiler.CompileContract(authorizeSource)
if err != nil {
t.Fatal(err)
}
authInitCalldata, err := compiler.EncodeCalldata(authorizeSource, "init", []string{alice.Address}, config.Compiler.Backend)
authInitCalldata, err := compiler.EncodeCalldata(authorizeSource, "init", []string{alice.Address})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestGeneralizedAccounts(t *testing.T) {
// GAMetaTx
// spendTx will be wrapped in a SignedTx with 0 signatures before being
// included in GAMetaTx. The constructor NewGAMetaTx() does this for you.
authData, err := compiler.EncodeCalldata(authorizeSource, "authorize", []string{"3"}, config.Compiler.Backend)
authData, err := compiler.EncodeCalldata(authorizeSource, "authorize", []string{"3"})
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f8ab7a5

Please sign in to comment.