-
Notifications
You must be signed in to change notification settings - Fork 1
/
struct_value.go
49 lines (38 loc) · 1.12 KB
/
struct_value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package storagescan
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"math/big"
"reflect"
"strings"
)
type StructValueI interface {
Field(f string) interface{}
String() string
}
type StructValue struct {
baseSlotIndex common.Hash
filedValueMap map[string]Variable
f GetValueStorageAtFunc
}
func (s StructValue) Field(fd string) interface{} {
filedValue, ok := s.filedValueMap[fd]
if !ok {
return nil
}
oldSlot := filedValue.Slot()
slotIndex := new(big.Int)
slotIndex.Add(s.baseSlotIndex.Big(), filedValue.Slot().Big())
// convert the slotIndex to common.Hash and assign it to the SlotIndex field of filed Value.V, using reflection
reflect.ValueOf(filedValue).Elem().FieldByName("SlotIndex").Set(reflect.ValueOf(common.BigToHash(slotIndex)))
value := filedValue.Value(s.f)
reflect.ValueOf(filedValue).Elem().FieldByName("SlotIndex").Set(reflect.ValueOf(oldSlot))
return value
}
func (s StructValue) String() string {
var fSting string
for filedName := range s.filedValueMap {
fSting += fmt.Sprintf("%v:%v ", filedName, s.Field(filedName))
}
return "struct{" + strings.TrimRight(fSting, " ") + "}"
}