-
Notifications
You must be signed in to change notification settings - Fork 158
/
pending.go
67 lines (58 loc) · 1.72 KB
/
pending.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
package blockchain
import (
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
)
type Pending struct {
Block *core.Block
StateUpdate *core.StateUpdate
NewClasses map[felt.Felt]core.Class
}
type PendingState struct {
stateDiff *core.StateDiff
newClasses map[felt.Felt]core.Class
head core.StateReader
}
func NewPendingState(stateDiff *core.StateDiff, newClasses map[felt.Felt]core.Class, head core.StateReader) *PendingState {
return &PendingState{
stateDiff: stateDiff,
newClasses: newClasses,
head: head,
}
}
func (p *PendingState) ContractClassHash(addr *felt.Felt) (*felt.Felt, error) {
if classHash, ok := p.stateDiff.ReplacedClasses[*addr]; ok {
return classHash, nil
} else if classHash, ok = p.stateDiff.DeployedContracts[*addr]; ok {
return classHash, nil
}
return p.head.ContractClassHash(addr)
}
func (p *PendingState) ContractNonce(addr *felt.Felt) (*felt.Felt, error) {
if nonce, found := p.stateDiff.Nonces[*addr]; found {
return nonce, nil
} else if _, found = p.stateDiff.DeployedContracts[*addr]; found {
return &felt.Felt{}, nil
}
return p.head.ContractNonce(addr)
}
func (p *PendingState) ContractStorage(addr, key *felt.Felt) (*felt.Felt, error) {
if diffs, found := p.stateDiff.StorageDiffs[*addr]; found {
if value, found := diffs[*key]; found {
return value, nil
}
}
if _, found := p.stateDiff.DeployedContracts[*addr]; found {
return &felt.Felt{}, nil
}
return p.head.ContractStorage(addr, key)
}
func (p *PendingState) Class(classHash *felt.Felt) (*core.DeclaredClass, error) {
if class, found := p.newClasses[*classHash]; found {
return &core.DeclaredClass{
At: 0,
Class: class,
}, nil
}
return p.head.Class(classHash)
}