-
Notifications
You must be signed in to change notification settings - Fork 164
/
state_reader.go
92 lines (75 loc) · 2.5 KB
/
state_reader.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package vm
//#include <stdint.h>
//#include <stdlib.h>
import "C"
import (
"errors"
"unsafe"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
)
//export JunoFree
func JunoFree(ptr unsafe.Pointer) {
C.free(ptr)
}
//export JunoStateGetStorageAt
func JunoStateGetStorageAt(readerHandle C.uintptr_t, contractAddress, storageLocation unsafe.Pointer) unsafe.Pointer {
context := unwrapContext(readerHandle)
contractAddressFelt := makeFeltFromPtr(contractAddress)
storageLocationFelt := makeFeltFromPtr(storageLocation)
val, err := context.state.ContractStorage(contractAddressFelt, storageLocationFelt)
if err != nil {
if !errors.Is(err, db.ErrKeyNotFound) {
context.log.Errorw("JunoStateGetStorageAt failed to read contract storage", "err", err)
return nil
}
val = &felt.Zero
}
return makePtrFromFelt(val)
}
//export JunoStateGetNonceAt
func JunoStateGetNonceAt(readerHandle C.uintptr_t, contractAddress unsafe.Pointer) unsafe.Pointer {
context := unwrapContext(readerHandle)
contractAddressFelt := makeFeltFromPtr(contractAddress)
val, err := context.state.ContractNonce(contractAddressFelt)
if err != nil {
if !errors.Is(err, db.ErrKeyNotFound) {
context.log.Errorw("JunoStateGetNonceAt failed to read contract nonce", "err", err)
return nil
}
val = &felt.Zero
}
return makePtrFromFelt(val)
}
//export JunoStateGetClassHashAt
func JunoStateGetClassHashAt(readerHandle C.uintptr_t, contractAddress unsafe.Pointer) unsafe.Pointer {
context := unwrapContext(readerHandle)
contractAddressFelt := makeFeltFromPtr(contractAddress)
val, err := context.state.ContractClassHash(contractAddressFelt)
if err != nil {
if !errors.Is(err, db.ErrKeyNotFound) {
context.log.Errorw("JunoStateGetClassHashAt failed to read contract class", "err", err)
return nil
}
val = &felt.Zero
}
return makePtrFromFelt(val)
}
//export JunoStateGetCompiledClass
func JunoStateGetCompiledClass(readerHandle C.uintptr_t, classHash unsafe.Pointer) unsafe.Pointer {
context := unwrapContext(readerHandle)
classHashFelt := makeFeltFromPtr(classHash)
val, err := context.state.Class(classHashFelt)
if err != nil {
if !errors.Is(err, db.ErrKeyNotFound) {
context.log.Errorw("JunoStateGetCompiledClass failed to read class", "err", err)
}
return nil
}
compiledClass, err := marshalClassInfo(val.Class)
if err != nil {
context.log.Errorw("JunoStateGetCompiledClass failed to marshal compiled class", "err", err)
return nil
}
return unsafe.Pointer(cstring(compiledClass))
}