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

feat: add e2e test stateful contract #2703

Merged
merged 33 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d94f788
feat: add test precompiled contract
fbac Aug 14, 2024
b9878a6
feat: add test stateful contract
fbac Aug 14, 2024
40ff210
feat: enable contracts granularly
fbac Aug 14, 2024
83457fc
feat: allow passing the app ctx to contracts
fbac Aug 14, 2024
d13079a
revert ctx changes
fbac Aug 14, 2024
e58c783
format ABI
fbac Aug 14, 2024
eaac523
add first e2e test
fbac Aug 15, 2024
fadf37a
add RegularCaller contract to e2e
fbac Aug 15, 2024
8ad612b
add signer to deployment
fbac Aug 15, 2024
a717f01
fix e2e, use regular abi
fbac Aug 16, 2024
5c88cf1
minor rewording
fbac Aug 16, 2024
f32ff32
use idiomatic errors
fbac Aug 16, 2024
ddee0c5
remove unused files
fbac Aug 19, 2024
c1d41e2
rename and generate bindings automatically
fbac Aug 19, 2024
797dbc4
create a new e2e section for precompiles
fbac Aug 19, 2024
bb69a61
add bindings generator and makefile target
fbac Aug 19, 2024
53098a1
add GetGasStabilityPoolBalance as prototype contract function
fbac Aug 19, 2024
dbe971c
include getGasStabilityPoolBalance function
fbac Aug 19, 2024
906c438
apply code review fixes
fbac Aug 20, 2024
cefb8d2
include tests for precompiles package
fbac Aug 20, 2024
1750644
delete regularcaller
fbac Aug 20, 2024
0f0a517
ignore bindings from codecov
fbac Aug 20, 2024
ee52f66
introduce more unit testing
fbac Aug 20, 2024
3fa0cab
add unit testing for prototype.go
fbac Aug 20, 2024
3a31ddb
minor linting fixes
fbac Aug 20, 2024
fe78f69
remove ctx, keep fungible keeper private
fbac Aug 20, 2024
b62a99e
increase unit tests coverage
fbac Aug 20, 2024
d2bcc79
add keys for new user_precompile
fbac Aug 20, 2024
adbc0a6
add init unit test
fbac Aug 20, 2024
de401aa
switch to double quotes
fbac Aug 20, 2024
f49ac43
avoid go:embed
fbac Aug 20, 2024
ac48e29
fixes derived from code review
fbac Aug 20, 2024
9cbc274
refactor some parts of bech32 functions
fbac Aug 21, 2024
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
Prev Previous commit
Next Next commit
add init unit test
  • Loading branch information
fbac committed Aug 20, 2024
commit adbc0a64e07d3217f06dc6d3ed025e70810c4e69
4 changes: 4 additions & 0 deletions precompiles/prototype/prototype.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
)

func init() {
initABI()
}

func initABI() {
if prototypeABI == "" {
panic("missing prototype ABI")
}
Expand Down Expand Up @@ -124,16 +128,16 @@

bech32Prefix := strings.SplitN(address, "1", 2)[0]
if bech32Prefix == address {
return nil, fmt.Errorf("invalid bech32 address: %s", address)

Check warning on line 131 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L131

Added line #L131 was not covered by tests
}

addressBz, err := sdk.GetFromBech32(address, bech32Prefix)
if err != nil {
return nil, err

Check warning on line 136 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L136

Added line #L136 was not covered by tests
}

if err := sdk.VerifyAddressFormat(addressBz); err != nil {
return nil, err

Check warning on line 140 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L140

Added line #L140 was not covered by tests
}

return method.Outputs.Pack(common.BytesToAddress(addressBz))
Expand Down Expand Up @@ -169,21 +173,21 @@

// NOTE: safety check, should not happen given that the address is 20 bytes.
if err := sdk.VerifyAddressFormat(address.Bytes()); err != nil {
return nil, err

Check warning on line 176 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L176

Added line #L176 was not covered by tests
}

bech32Str, err := sdk.Bech32ifyAddressBytes(prefix, address.Bytes())
if err != nil {
return nil, err

Check warning on line 181 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L181

Added line #L181 was not covered by tests
}

addressBz, err := sdk.GetFromBech32(bech32Str, "zeta")
if err != nil {
return nil, err

Check warning on line 186 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L186

Added line #L186 was not covered by tests
}

if err := sdk.VerifyAddressFormat(addressBz); err != nil {
return nil, err

Check warning on line 190 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L190

Added line #L190 was not covered by tests
}

return method.Outputs.Pack(bech32Str)
Expand Down Expand Up @@ -214,43 +218,43 @@
return nil, fmt.Errorf("error calling fungible keeper: %s", err.Error())
}

return method.Outputs.Pack(balance)

Check warning on line 221 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L221

Added line #L221 was not covered by tests
}

// Run is the entrypoint of the precompiled contract, it switches over the input method,
// and execute them accordingly.
func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, error) {
method, err := ABI.MethodById(contract.Input[:4])
if err != nil {
return nil, err

Check warning on line 229 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L226-L229

Added lines #L226 - L229 were not covered by tests
}

args, err := method.Inputs.Unpack(contract.Input[4:])
if err != nil {
return nil, err

Check warning on line 234 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L232-L234

Added lines #L232 - L234 were not covered by tests
}

stateDB := evm.StateDB.(ptypes.ExtStateDB)

Check warning on line 237 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L237

Added line #L237 was not covered by tests

switch method.Name {
case GetGasStabilityPoolBalanceName:
var res []byte
execErr := stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error {
res, err = c.GetGasStabilityPoolBalance(ctx, method, args)
return err
})
if execErr != nil {
return nil, err

Check warning on line 247 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L239-L247

Added lines #L239 - L247 were not covered by tests
}
return res, nil

Check warning on line 249 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L249

Added line #L249 was not covered by tests

case Bech32ToHexAddrMethodName:
return c.Bech32ToHexAddr(method, args)
case Bech32ifyMethodName:
return c.Bech32ify(method, args)
default:
return nil, ptypes.ErrInvalidMethod{
Method: method.Name,

Check warning on line 257 in precompiles/prototype/prototype.go

View check run for this annotation

Codecov / codecov/patch

precompiles/prototype/prototype.go#L251-L257

Added lines #L251 - L257 were not covered by tests
}
}
}
23 changes: 23 additions & 0 deletions precompiles/prototype/prototype_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prototype

import (
"encoding/json"
"testing"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -277,4 +278,26 @@ func Test_InvalidMethod(t *testing.T) {
// Test for non existent method.
_, doNotExist := abi.Methods["invalidMethod"]
require.False(t, doNotExist, "invalidMethod should not be present in the ABI")
}

func Test_MissingABI(t *testing.T) {
prototypeABI = ""
defer func() {
if r := recover(); r != nil {
require.Equal(t, "missing prototype ABI", r, "expected error: missing ABI, got: %v", r)
}
}()

initABI()
}

func Test_InvalidABI(t *testing.T) {
prototypeABI = "invalid json"
defer func() {
if r := recover(); r != nil {
require.IsType(t, &json.SyntaxError{}, r, "expected error type: json.SyntaxError, got: %T", r)
}
}()

initABI()
}
Loading