Skip to content

Commit e43c6db

Browse files
authored
Merge pull request #75 from hyperledger/solsig-indexed
2 parents 8f68c17 + 465aa79 commit e43c6db

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

pkg/abi/abi.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,13 @@ func (e *Entry) SolidityDef() (string, []string, error) {
701701
// SolidityDefCtx returns a Solidity-like descriptor of the entry, including its type
702702
func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
703703
// Everything apart from event and error is a type of function
704-
isFunction := e.Type != Error && e.Type != Event
704+
var fieldType SolFieldType
705+
switch e.Type {
706+
case Error, Event:
707+
fieldType = EventOrErrorField
708+
default:
709+
fieldType = FunctionInput
710+
}
705711

706712
allChildStructs := []string{}
707713
buff := new(strings.Builder)
@@ -713,7 +719,7 @@ func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
713719
if i > 0 {
714720
buff.WriteString(", ")
715721
}
716-
s, childStructs, err := p.SolidityDefCtx(ctx, isFunction)
722+
s, childStructs, err := p.SolidityDefCtx(ctx, fieldType)
717723
if err != nil {
718724
return "", nil, err
719725
}
@@ -722,7 +728,7 @@ func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
722728
}
723729
buff.WriteRune(')')
724730

725-
if isFunction {
731+
if fieldType == FunctionInput {
726732
buff.WriteString(" external")
727733
if e.StateMutability != "" &&
728734
// The state mutability nonpayable is reflected in Solidity by not specifying a state mutability modifier at all.
@@ -736,7 +742,7 @@ func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
736742
if i > 0 {
737743
buff.WriteString(", ")
738744
}
739-
s, childStructs, err := p.SolidityDefCtx(ctx, isFunction)
745+
s, childStructs, err := p.SolidityDefCtx(ctx, fieldType)
740746
if err != nil {
741747
return "", nil, err
742748
}
@@ -782,13 +788,13 @@ func (p *Parameter) SignatureStringCtx(ctx context.Context) (string, error) {
782788
return tc.String(), nil
783789
}
784790

785-
func (p *Parameter) SolidityDefCtx(ctx context.Context, inFunction bool) (string, []string, error) {
791+
func (p *Parameter) SolidityDefCtx(ctx context.Context, fieldType SolFieldType) (string, []string, error) {
786792
// Ensure the type component tree has been parsed
787793
tc, err := p.TypeComponentTreeCtx(ctx)
788794
if err != nil {
789795
return "", nil, err
790796
}
791-
solDef, childStructs := tc.SolidityParamDef(inFunction)
797+
solDef, childStructs := tc.SolidityParamDef(fieldType)
792798
return solDef, childStructs, nil
793799
}
794800

pkg/abi/abi_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ const sampleABI5 = `[
226226
"internalType": "struct AribtraryWidgets.Widget[]",
227227
"name": "widgets",
228228
"type": "tuple[]"
229+
},
230+
{
231+
"name": "account",
232+
"type": "address",
233+
"indexed": true
229234
}
230235
],
231236
"name": "Invoiced",
@@ -1015,7 +1020,7 @@ func TestComplexStructSolidityDef(t *testing.T) {
10151020

10161021
solDef, childStructs, err = abi.Events()["Invoiced"].SolidityDef()
10171022
assert.NoError(t, err)
1018-
assert.Equal(t, "event Invoiced(Customer customer, Widget[] widgets)", solDef)
1023+
assert.Equal(t, "event Invoiced(Customer customer, Widget[] widgets, address indexed account)", solDef)
10191024
assert.Equal(t, []string{
10201025
"struct Customer { address owner; bytes32 locator; }",
10211026
"struct Widget { string description; uint256 price; string[] attributes; }",

pkg/abi/typecomponents.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type TypeComponent interface {
6666
DecodeABIData(d []byte, offset int) (*ComponentValue, error)
6767
DecodeABIDataCtx(ctx context.Context, d []byte, offest int) (*ComponentValue, error)
6868

69-
SolidityParamDef(inFunction bool) (solDef string, structDefs []string) // gives a string that can be used to define this param in solidity
69+
SolidityParamDef(fieldType SolFieldType) (solDef string, structDefs []string) // gives a string that can be used to define this param in solidity
7070
SolidityTypeDef() (isRef bool, typeDef string, childStructs []string)
7171
SolidityStructDef() (structName string, structs []string)
7272
}
@@ -187,6 +187,14 @@ const (
187187
BaseTypeString BaseTypeName = "string"
188188
)
189189

190+
type SolFieldType int
191+
192+
const (
193+
FunctionInput SolFieldType = iota // input to a function, or a constructor
194+
EventOrErrorField // a field of an event or an error
195+
StructField // a field of a struct
196+
)
197+
190198
// tupleTypeString appears in the same place in the ABI as elementary type strings, but it is not an elementary type.
191199
// We treat it separately.
192200
const tupleTypeString = "tuple"
@@ -371,13 +379,18 @@ func (tc *typeComponent) String() string {
371379
}
372380
}
373381

374-
func (tc *typeComponent) SolidityParamDef(inFunction bool) (string, []string) {
382+
func (tc *typeComponent) SolidityParamDef(fieldType SolFieldType) (string, []string) {
375383
isRef, paramDef, childStructs := tc.SolidityTypeDef()
376-
if isRef && inFunction {
377-
paramDef = fmt.Sprintf("%s memory", paramDef)
384+
if isRef && fieldType == FunctionInput {
385+
paramDef += " memory"
378386
}
379-
if tc.parameter != nil && tc.parameter.Name != "" {
380-
paramDef = fmt.Sprintf("%s %s", paramDef, tc.parameter.Name)
387+
if tc.parameter != nil {
388+
if fieldType == EventOrErrorField && tc.parameter.Indexed {
389+
paramDef += " indexed"
390+
}
391+
if tc.parameter.Name != "" {
392+
paramDef = fmt.Sprintf("%s %s", paramDef, tc.parameter.Name)
393+
}
381394
}
382395
return paramDef, childStructs
383396
}

pkg/rpcbackend/backend.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package rpcbackend
1919
import (
2020
"context"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"sync/atomic"
2425
"time"
@@ -91,7 +92,7 @@ type RPCError struct {
9192
}
9293

9394
func (e *RPCError) Error() error {
94-
return fmt.Errorf(e.Message)
95+
return errors.New(e.Message)
9596
}
9697

9798
func (e *RPCError) String() string {
@@ -208,7 +209,7 @@ func (rc *RPCClient) SyncRequest(ctx context.Context, rpcReq *RPCRequest) (rpcRe
208209
rpcMsg = i18n.NewError(ctx, signermsgs.MsgRPCRequestFailed, res.Status()).Error()
209210
}
210211
log.L(ctx).Errorf("RPC[%s] <-- [%d]: %s", rpcTraceID, res.StatusCode(), errLog)
211-
err := fmt.Errorf(rpcMsg)
212+
err := errors.New(rpcMsg)
212213
return rpcRes, err
213214
}
214215
log.L(ctx).Infof("RPC[%s] <-- %s [%d] OK (%.2fms)", rpcTraceID, rpcReq.Method, res.StatusCode(), float64(time.Since(rpcStartTime))/float64(time.Millisecond))

0 commit comments

Comments
 (0)