Skip to content

Commit

Permalink
Bytecode package unit tests and constructor decoding bugfix (#49)
Browse files Browse the repository at this point in the history
* Constructor fixes and unit test
* Unary prefix in ast had dumpnode that needs to go out...
  • Loading branch information
0x19 authored Aug 4, 2023
1 parent e20a28c commit f7a7e58
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 14 deletions.
3 changes: 0 additions & 3 deletions ast/unary_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ func (u *UnaryPrefix) GetReferencedDeclaration() int64 {
}

func (u *UnaryPrefix) ToProto() NodeType {
if u.GetTypeDescription() == nil {
u.dumpNode(u)
}
proto := ast_pb.UnaryPrefix{
Id: u.GetId(),
NodeType: u.GetType(),
Expand Down
31 changes: 20 additions & 11 deletions bytecode/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@ import (
"github.com/ethereum/go-ethereum/common"
)

// Argument represents a single argument in a contract constructor.
// It includes the argument's name, type, value, and whether it is indexed.
type Argument struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
Indexed bool `json:"indexed"`
Name string `json:"name"` // Name of the argument
Type string `json:"type"` // Type of the argument
Value string `json:"value"` // Value of the argument
Indexed bool `json:"indexed"` // Indicates if the argument is indexed
}

// Constructor represents a contract constructor.
// It includes the ABI of the constructor, the raw signature, and the arguments.
type Constructor struct {
Abi string `json:"abi"`
SignatureRaw string `json:"signature_raw"`
Arguments []Argument
Abi string `json:"abi"` // ABI of the constructor
SignatureRaw string `json:"signature_raw"` // Raw signature of the constructor
Arguments []Argument // List of arguments in the constructor
}

// DecodeConstructorFromAbi decodes the constructor from the provided ABI and bytecode.
// It returns a Constructor object and an error if any occurred during the decoding process.
//
// The function first checks if the bytecode is empty or does not start with '['. If so, it prepends '[' to the constructorAbi.
// Then it attempts to parse the ABI using the abi.JSON function from the go-ethereum library.
// If the ABI parsing is successful, it unpacks the values from the bytecode using the UnpackValues function.
// It then checks if the number of unpacked values matches the number of inputs in the constructor.
// If they match, it creates an Argument object for each input and adds it to the arguments slice.
// Finally, it returns a Constructor object containing the ABI, raw signature, and arguments.
func DecodeConstructorFromAbi(bytecode []byte, constructorAbi string) (*Constructor, error) {
if len(bytecode) == 0 || bytecode[0] != '[' {
constructorAbi = "[" + constructorAbi + "]"
}

parsed, err := abi.JSON(strings.NewReader(constructorAbi))
if err != nil {
return nil, fmt.Errorf("failed to parse ABI: %w", err)
Expand Down
Loading

0 comments on commit f7a7e58

Please sign in to comment.