Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
cd009053
Commit
cd009053
authored
Aug 21, 2023
by
Hamdi Allam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixes
parent
8f2dcb99
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
24 deletions
+26
-24
bytes.go
indexer/database/serializers/bytes.go
+20
-14
rlp.go
indexer/database/serializers/rlp.go
+6
-10
No files found.
indexer/database/serializers/bytes.go
View file @
cd009053
...
@@ -2,7 +2,6 @@ package serializers
...
@@ -2,7 +2,6 @@ package serializers
import
(
import
(
"context"
"context"
"errors"
"fmt"
"fmt"
"reflect"
"reflect"
...
@@ -19,9 +18,8 @@ func init() {
...
@@ -19,9 +18,8 @@ func init() {
}
}
func
(
BytesSerializer
)
Scan
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
dbValue
interface
{})
error
{
func
(
BytesSerializer
)
Scan
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
dbValue
interface
{})
error
{
// Empty slices are serialized as '0x'
if
dbValue
==
nil
||
(
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
&&
reflect
.
ValueOf
(
dbValue
)
.
IsNil
())
{
if
dbValue
==
nil
{
return
nil
return
errors
.
New
(
"cannot unmarshal an empty dbValue"
)
}
}
hexStr
,
ok
:=
dbValue
.
(
string
)
hexStr
,
ok
:=
dbValue
.
(
string
)
...
@@ -35,13 +33,22 @@ func (BytesSerializer) Scan(ctx context.Context, field *schema.Field, dst reflec
...
@@ -35,13 +33,22 @@ func (BytesSerializer) Scan(ctx context.Context, field *schema.Field, dst reflec
}
}
fieldValue
:=
reflect
.
New
(
field
.
FieldType
)
fieldValue
:=
reflect
.
New
(
field
.
FieldType
)
fieldInterface
:=
fieldValue
.
Interface
()
// Detect if we're deserializing into a pointer. If so, we'll need to
// also allocate memory to where the allocated pointer should point to
if
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
{
if
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
{
// Allocate memory if this is pointer which by
nestedField
:=
fieldValue
.
Elem
()
// default when deserializing is probably `nil`
if
nestedField
.
Elem
()
.
Kind
()
==
reflect
.
Pointer
{
fieldValue
.
Set
(
reflect
.
New
(
field
.
FieldType
.
Elem
()))
return
fmt
.
Errorf
(
"double pointers are the max depth supported: %T"
,
fieldValue
)
}
// We'll want to call `SetBytes` on the pointer to the
// allocated structmemory and not the douple pointer
nestedField
.
Set
(
reflect
.
New
(
field
.
FieldType
.
Elem
()))
fieldInterface
=
nestedField
.
Interface
()
}
}
fieldInterface
:=
fieldValue
.
Interface
()
fieldSetBytes
,
ok
:=
fieldInterface
.
(
SetBytesInterface
)
fieldSetBytes
,
ok
:=
fieldInterface
.
(
SetBytesInterface
)
if
!
ok
{
if
!
ok
{
return
fmt
.
Errorf
(
"field does not satisfy the `SetBytes([]byte)` interface: %T"
,
fieldInterface
)
return
fmt
.
Errorf
(
"field does not satisfy the `SetBytes([]byte)` interface: %T"
,
fieldInterface
)
...
@@ -53,16 +60,15 @@ func (BytesSerializer) Scan(ctx context.Context, field *schema.Field, dst reflec
...
@@ -53,16 +60,15 @@ func (BytesSerializer) Scan(ctx context.Context, field *schema.Field, dst reflec
}
}
func
(
BytesSerializer
)
Value
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
fieldValue
interface
{})
(
interface
{},
error
)
{
func
(
BytesSerializer
)
Value
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
fieldValue
interface
{})
(
interface
{},
error
)
{
if
fieldValue
==
nil
||
(
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
&&
reflect
.
ValueOf
(
fieldValue
)
.
IsNil
())
{
return
nil
,
nil
}
fieldBytes
,
ok
:=
fieldValue
.
(
BytesInterface
)
fieldBytes
,
ok
:=
fieldValue
.
(
BytesInterface
)
if
!
ok
{
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"field does not satisfy the `Bytes() []byte` interface"
)
return
nil
,
fmt
.
Errorf
(
"field does not satisfy the `Bytes() []byte` interface"
)
}
}
var
b
[]
byte
hexStr
:=
hexutil
.
Encode
(
fieldBytes
.
Bytes
())
if
fieldValue
!=
nil
&&
reflect
.
ValueOf
(
fieldValue
)
.
IsNil
()
{
b
=
fieldBytes
.
Bytes
()
}
hexStr
:=
hexutil
.
Encode
(
b
)
return
hexStr
,
nil
return
hexStr
,
nil
}
}
indexer/database/serializers/rlp.go
View file @
cd009053
...
@@ -2,7 +2,6 @@ package serializers
...
@@ -2,7 +2,6 @@ package serializers
import
(
import
(
"context"
"context"
"errors"
"fmt"
"fmt"
"reflect"
"reflect"
...
@@ -19,9 +18,8 @@ func init() {
...
@@ -19,9 +18,8 @@ func init() {
}
}
func
(
RLPSerializer
)
Scan
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
dbValue
interface
{})
error
{
func
(
RLPSerializer
)
Scan
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
dbValue
interface
{})
error
{
// RLP encoding never results in nil bytes
if
dbValue
==
nil
||
(
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
&&
reflect
.
ValueOf
(
dbValue
)
.
IsNil
())
{
if
dbValue
==
nil
{
return
nil
return
errors
.
New
(
"cannot unmarshal an empty dbValue"
)
}
}
hexStr
,
ok
:=
dbValue
.
(
string
)
hexStr
,
ok
:=
dbValue
.
(
string
)
...
@@ -35,12 +33,6 @@ func (RLPSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.
...
@@ -35,12 +33,6 @@ func (RLPSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.
}
}
fieldValue
:=
reflect
.
New
(
field
.
FieldType
)
fieldValue
:=
reflect
.
New
(
field
.
FieldType
)
if
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
{
// Allocate memory if this is pointer which by
// default when deserializing is probably `nil`
fieldValue
.
Set
(
reflect
.
New
(
field
.
FieldType
.
Elem
()))
}
if
err
:=
rlp
.
DecodeBytes
(
b
,
fieldValue
.
Interface
());
err
!=
nil
{
if
err
:=
rlp
.
DecodeBytes
(
b
,
fieldValue
.
Interface
());
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to decode rlp bytes: %s"
,
err
)
return
fmt
.
Errorf
(
"failed to decode rlp bytes: %s"
,
err
)
}
}
...
@@ -50,6 +42,10 @@ func (RLPSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.
...
@@ -50,6 +42,10 @@ func (RLPSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.
}
}
func
(
RLPSerializer
)
Value
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
fieldValue
interface
{})
(
interface
{},
error
)
{
func
(
RLPSerializer
)
Value
(
ctx
context
.
Context
,
field
*
schema
.
Field
,
dst
reflect
.
Value
,
fieldValue
interface
{})
(
interface
{},
error
)
{
if
fieldValue
==
nil
||
(
field
.
FieldType
.
Kind
()
==
reflect
.
Pointer
&&
reflect
.
ValueOf
(
fieldValue
)
.
IsNil
())
{
return
nil
,
nil
}
rlpBytes
,
err
:=
rlp
.
EncodeToBytes
(
fieldValue
)
rlpBytes
,
err
:=
rlp
.
EncodeToBytes
(
fieldValue
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to encode rlp bytes: %s"
,
err
)
return
nil
,
fmt
.
Errorf
(
"failed to encode rlp bytes: %s"
,
err
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment