Skip to content

Commit

Permalink
Add console2 log support (#126)
Browse files Browse the repository at this point in the history
* Add console log support

* Fix lint
  • Loading branch information
ferranbt authored and dmarzzz committed Dec 21, 2023
1 parent 026c84c commit e9ec23d
Show file tree
Hide file tree
Showing 9 changed files with 579 additions and 15 deletions.
24 changes: 15 additions & 9 deletions accounts/abi/type2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ func (t Type) Pack(v interface{}) ([]byte, error) {
return t.pack(reflect.ValueOf(v))
}

func (t Type) Unpack(data []byte, obj interface{}) error {
fmt.Println(toGoType(0, t, data))

return nil
func (t Type) Unpack(data []byte) (interface{}, error) {
return toGoType(0, t, data)
}

// newTypeForTuple implements the format described in https://blog.ricmoo.com/human-readable-contract-abis-in-ethers-js-141902f4d917
Expand Down Expand Up @@ -76,25 +74,33 @@ func newTypeForTuple(s string) (string, []ArgumentMarshaling, error) {
}
}

if len(fields) == 1 && fields[0] == "" {
// empty tuple (i.e. tuple())
fields = []string{}
}

// trim the args of spaces
for i := range fields {
fields[i] = strings.TrimSpace(fields[i])
}

// decode the type of each field
var args []ArgumentMarshaling
for _, field := range fields {
for indx, field := range fields {
// anonymous fields are not supported so the first
// string should be the identifier of the field.
var name string

spacePos := strings.Index(field, " ")
if spacePos == -1 {
return "", nil, fmt.Errorf("invalid tuple field name not found '%s'", field)
// it has no name
name = fmt.Sprintf("arg%d", indx)
} else {
// it has name and field (name field)
name = field[:spacePos]
field = field[spacePos+1:]
}

name := field[:spacePos]
field = field[spacePos+1:]

if strings.HasPrefix(field, "tuple") {
// decode a recursive tuple
sig, elems, err := newTypeForTuple(field)
Expand Down
19 changes: 13 additions & 6 deletions accounts/abi/type2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ func TestNewType2FromString(t *testing.T) {
}{A: 1, B: 2},
},
{
"tuple(a uint64, b tuple(c uint64), d uint64)",
// empty tuple
"tuple()",
"()",
&struct{}{},
},
{
// anonymous type
"tuple(uint64,tuple(uint64),uint64)",
"(uint64,(uint64),uint64)",
&struct {
A uint64
B struct {
C uint64
Arg0 uint64
Arg1 struct {
Arg0 uint64
}
D uint64
}{A: 1, B: struct{ C uint64 }{C: 2}, D: 3},
Arg2 uint64
}{Arg0: 1, Arg1: struct{ Arg0 uint64 }{Arg0: 2}, Arg2: 3},
},
{
"tuple(a uint64, b tuple[](x tuple(c uint64), d uint64), e tuple(f uint64, g uint64))",
Expand Down
13 changes: 13 additions & 0 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/suave/consolelog"
suave "github.com/ethereum/go-ethereum/suave/core"
)

Expand Down Expand Up @@ -149,6 +150,18 @@ type suaveRuntime struct {

var _ SuaveRuntime = &suaveRuntime{}

type consoleLogPrecompile struct {
}

func (c *consoleLogPrecompile) RequiredGas(input []byte) uint64 {
return 0
}

func (c *consoleLogPrecompile) Run(input []byte) ([]byte, error) {
consolelog.Print(input)
return nil, nil
}

func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error) {
if request.Method != "GET" && request.Method != "POST" {
return nil, fmt.Errorf("only GET and POST methods are supported")
Expand Down
6 changes: 6 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/suave/consolelog"
"github.com/holiman/uint256"
)

Expand All @@ -43,6 +44,11 @@ type (
func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
// First check confidential precompiles, only then continue to the regular ones
if evm.chainRules.IsSuave {
// console-log precompile available in the suave context
if addr == consolelog.Console2ContractAddr {
return &consoleLogPrecompile{}, true
}

if isPrecompileAddr(addr) && evm.Config.IsConfidential {
suaveContext := NewRuntimeSuaveContext(evm, addr)
return NewSuavePrecompiledContractWrapper(addr, suaveContext), true
Expand Down
Loading

0 comments on commit e9ec23d

Please sign in to comment.