|
1 | 1 | package blockchain
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
4 | 7 | "github.com/NethermindEth/juno/core"
|
5 | 8 | "github.com/NethermindEth/juno/core/felt"
|
| 9 | + "github.com/NethermindEth/juno/db" |
6 | 10 | )
|
7 | 11 |
|
8 | 12 | type Pending struct {
|
@@ -65,3 +69,123 @@ func (p *PendingState) Class(classHash *felt.Felt) (*core.DeclaredClass, error)
|
65 | 69 |
|
66 | 70 | return p.head.Class(classHash)
|
67 | 71 | }
|
| 72 | + |
| 73 | +func (p *PendingState) SetStorage(contractAddress, key, value *felt.Felt) error { |
| 74 | + if _, found := p.stateDiff.StorageDiffs[*contractAddress]; !found { |
| 75 | + p.stateDiff.StorageDiffs[*contractAddress] = make(map[felt.Felt]*felt.Felt) |
| 76 | + } |
| 77 | + p.stateDiff.StorageDiffs[*contractAddress][*key] = value.Clone() |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (p *PendingState) IncrementNonce(contractAddress *felt.Felt) error { |
| 82 | + currentNonce, err := p.ContractNonce(contractAddress) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("get contract nonce: %v", err) |
| 85 | + } |
| 86 | + newNonce := new(felt.Felt).SetUint64(1) |
| 87 | + p.stateDiff.Nonces[*contractAddress] = newNonce.Add(currentNonce, newNonce) |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (p *PendingState) SetClassHash(contractAddress, classHash *felt.Felt) error { |
| 92 | + if _, err := p.head.ContractClassHash(contractAddress); err != nil { |
| 93 | + if errors.Is(err, db.ErrKeyNotFound) { |
| 94 | + p.stateDiff.DeployedContracts[*contractAddress] = classHash.Clone() |
| 95 | + return nil |
| 96 | + } |
| 97 | + return fmt.Errorf("get latest class hash: %v", err) |
| 98 | + } |
| 99 | + p.stateDiff.ReplacedClasses[*contractAddress] = classHash.Clone() |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (p *PendingState) SetContractClass(classHash *felt.Felt, class core.Class) error { |
| 104 | + if _, err := p.Class(classHash); err == nil { |
| 105 | + return errors.New("class already declared") |
| 106 | + } else if !errors.Is(err, db.ErrKeyNotFound) { |
| 107 | + return fmt.Errorf("get class: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + p.newClasses[*classHash] = class |
| 111 | + if class.Version() == 0 { |
| 112 | + p.stateDiff.DeclaredV0Classes = append(p.stateDiff.DeclaredV0Classes, classHash.Clone()) |
| 113 | + } // assumption: SetCompiledClassHash will be called for Cairo1 contracts |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (p *PendingState) SetCompiledClassHash(classHash, compiledClassHash *felt.Felt) error { |
| 118 | + // assumption: SetContractClass was called for classHash and succeeded |
| 119 | + p.stateDiff.DeclaredV1Classes[*classHash] = compiledClassHash.Clone() |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +type PendingStateWriter struct { |
| 124 | + *PendingState |
| 125 | +} |
| 126 | + |
| 127 | +func NewPendingStateWriter(stateDiff *core.StateDiff, newClasses map[felt.Felt]core.Class, head core.StateReader) *PendingStateWriter { |
| 128 | + return &PendingStateWriter{ |
| 129 | + PendingState: &PendingState{ |
| 130 | + stateDiff: stateDiff, |
| 131 | + newClasses: newClasses, |
| 132 | + head: head, |
| 133 | + }, |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func (p *PendingStateWriter) SetStorage(contractAddress, key, value *felt.Felt) error { |
| 138 | + if _, found := p.stateDiff.StorageDiffs[*contractAddress]; !found { |
| 139 | + p.stateDiff.StorageDiffs[*contractAddress] = make(map[felt.Felt]*felt.Felt) |
| 140 | + } |
| 141 | + p.stateDiff.StorageDiffs[*contractAddress][*key] = value.Clone() |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func (p *PendingStateWriter) IncrementNonce(contractAddress *felt.Felt) error { |
| 146 | + currentNonce, err := p.ContractNonce(contractAddress) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("get contract nonce: %v", err) |
| 149 | + } |
| 150 | + newNonce := new(felt.Felt).SetUint64(1) |
| 151 | + p.stateDiff.Nonces[*contractAddress] = newNonce.Add(currentNonce, newNonce) |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func (p *PendingStateWriter) SetClassHash(contractAddress, classHash *felt.Felt) error { |
| 156 | + if _, err := p.head.ContractClassHash(contractAddress); err != nil { |
| 157 | + if errors.Is(err, db.ErrKeyNotFound) { |
| 158 | + p.stateDiff.DeployedContracts[*contractAddress] = classHash.Clone() |
| 159 | + return nil |
| 160 | + } |
| 161 | + return fmt.Errorf("get latest class hash: %v", err) |
| 162 | + } |
| 163 | + p.stateDiff.ReplacedClasses[*contractAddress] = classHash.Clone() |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func (p *PendingStateWriter) SetContractClass(classHash *felt.Felt, class core.Class) error { |
| 168 | + if _, err := p.Class(classHash); err == nil { |
| 169 | + return errors.New("class already declared") |
| 170 | + } else if !errors.Is(err, db.ErrKeyNotFound) { |
| 171 | + return fmt.Errorf("get class: %v", err) |
| 172 | + } |
| 173 | + |
| 174 | + p.newClasses[*classHash] = class |
| 175 | + if class.Version() == 0 { |
| 176 | + p.stateDiff.DeclaredV0Classes = append(p.stateDiff.DeclaredV0Classes, classHash.Clone()) |
| 177 | + } // assumption: SetCompiledClassHash will be called for Cairo1 contracts |
| 178 | + return nil |
| 179 | +} |
| 180 | + |
| 181 | +func (p *PendingStateWriter) SetCompiledClassHash(classHash, compiledClassHash *felt.Felt) error { |
| 182 | + // assumption: SetContractClass was called for classHash and succeeded |
| 183 | + p.stateDiff.DeclaredV1Classes[*classHash] = compiledClassHash.Clone() |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +// StateDiffAndClasses returns the pending state's internal data. The returned objects will continue to be |
| 188 | +// read and modified by the pending state. |
| 189 | +func (p *PendingStateWriter) StateDiffAndClasses() (*core.StateDiff, map[felt.Felt]core.Class) { |
| 190 | + return p.stateDiff, p.newClasses |
| 191 | +} |
0 commit comments