This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
feat(statedb): Reintroduce and simplify StateDB #14
Merged
Merged
Changes from all commits
Commits
Show all changes
95 commits
Select commit
Hold shift + click to select a range
8104037
add cachekv btree
d5e0489
bing bong
8afe910
stack
9303cf6
gomod
3534072
format
5376766
lmao what
fce73d8
lint
390c8a3
deep copy
1f4ff6c
trees to lib
e49053f
common
130a1c6
remove sdk alias
e986d02
lint imports
7052697
small cleanup
b3ce5f5
small cleanup
a5e4365
small cleanup
23e1bce
upgrade cachekv
7c9e9ac
improvements
b8beaeb
optimize control flow
4e28d4b
cvalue
fb19474
bing bong
29e490c
lint
e6ef005
bing bong
d1d4d56
cleanup test commands a bit
71b471a
Merge branch 'main' into cachekv
35ead99
Update lib/ds/stack.go
f4cd915
Update lib/ds/stack.go
262aedc
Merge branch 'main' into cachekv
f9a8a45
Merge branch 'cachekv' into upgrade-cachekv
1a11eac
typo
9d09be4
comments
cd3e74e
Revert "comments"
2e168ad
comments
b58b43d
Merge branch 'cachekv' into upgrade-cachekv
c1b0047
Merge branch 'main' into cachekv
6caa813
Merge branch 'main' into cachekv
770beb8
Merge branch 'cachekv' into upgrade-cachekv
cfd316d
lint
3ea6816
Apply suggestions from code review
50bd41f
Merge branch 'cachekv' into upgrade-cachekv
08ce21f
bing bong
a32c899
statedb reader
1903e7a
cleanup
f77aab1
lint
c40bf96
lint
02c0359
lint
365f0bf
Merge branch 'main' into cachekv
d8e6c68
Merge branch 'cachekv' into upgrade-cachekv
78781ce
Merge branch 'upgrade-cachekv' into statedb
6eb5c7e
statedb minor improvements
ef13e1d
transfer functions
bb8b5ff
improve comment
f231837
name
01ba137
bing bong
56b93f6
cleaner
bec6b4e
remove prefix store for performance increase
92a8f48
cleanup
da306b5
more changes
d555d0b
dont wanna break the chain on god fr fr
3013b37
bing bong
e236118
bing bong
c809c24
test get refund
b4e04be
add log tests
122e77c
finish statedb tests
1b0108e
improve code coverage
a43dc6d
cache entry test
7d6892e
cache entry test
ace2abf
fix test
897ba67
fix
381d0a3
opt
80ec2a5
merge main
1a8c381
cache value implements Cloneable
calbera ff4a882
clean struct creators
calbera 62e806c
comments
calbera 74a6573
linter
6937207
amend
d54f460
Merge branch 'main' into upgrade-cachekv
e92845c
impr(cachekv): Remove DeleteCacheValue (#19)
71c9add
Merge branch 'main' into upgrade-cachekv
15eee26
benchmarks
6944532
convert more tests
5f661ab
rename
8162272
test name
8479b36
renaming, cleanup
calbera df6b357
Merge branch 'upgrade-cachekv' of github.com:berachain/stargazer into…
calbera d8d81d3
fix merge
5c8679a
lint
5468e71
Merge branch 'upgrade-cachekv' into statedb
9dcb6a3
merge
ab229b7
evm namespace
f64c236
Merge branch 'main' into statedb
22a37a5
bing bong
c7febf6
remove log factory from statedb
bb3f9bb
Merge branch 'main' into statedb
a807952
merge
2d951fb
remove from interface for now
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
package core | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/berachain/stargazer/core/state" | ||
"github.com/berachain/stargazer/core/vm" | ||
"github.com/berachain/stargazer/lib/common" | ||
) | ||
|
||
// Compile-time interface check. | ||
var _ vm.CanTransferFunc = CanTransfer | ||
var _ vm.TransferFunc = Transfer | ||
|
||
// `CanTransfer` checks whether there are enough funds in the address' account to make a transfer. | ||
// NOTE: This does not take the necessary gas in to account to make the transfer valid. | ||
func CanTransfer(sdb vm.StateDB, addr common.Address, amount *big.Int) bool { | ||
return sdb.GetBalance(addr).Cmp(amount) >= 0 | ||
} | ||
|
||
// `Transfer` subtracts amount from sender and adds amount to recipient using the `vm.StateDB`. | ||
func Transfer(sdb vm.StateDB, sender, recipient common.Address, amount *big.Int) { | ||
// We use `TransferBalance` to use the same logic as the native transfer in x/bank. | ||
sdb.(state.ExtStateDB).TransferBalance(sender, recipient, amount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (C) 2022, Berachain Foundation. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
//nolint:ireturn // all `CacheEntries` must adhere to the same interface. | ||
package state | ||
|
||
import ( | ||
"github.com/berachain/stargazer/core/state/store/journal" | ||
) | ||
|
||
var ( | ||
_ journal.CacheEntry = &RefundChange{} | ||
_ journal.CacheEntry = &AddLogChange{} | ||
) | ||
|
||
type ( | ||
AddLogChange struct { | ||
sdb *StateDB | ||
} | ||
RefundChange struct { | ||
sdb *StateDB | ||
prev uint64 | ||
} | ||
) | ||
|
||
// ============================================================================== | ||
// AddLogChange | ||
// ============================================================================== | ||
|
||
// `Revert` implements `journal.CacheEntry`. | ||
func (ce *AddLogChange) Revert() { | ||
ce.sdb.logs = ce.sdb.logs[:len(ce.sdb.logs)-1] | ||
} | ||
|
||
// `Clone` implements `journal.CacheEntry`. | ||
func (ce *AddLogChange) Clone() journal.CacheEntry { | ||
return &AddLogChange{ | ||
sdb: ce.sdb, | ||
} | ||
} | ||
|
||
// ============================================================================== | ||
// RefundChange | ||
// ============================================================================== | ||
|
||
// `Revert` implements `journal.CacheEntry`. | ||
func (ce *RefundChange) Revert() { | ||
ce.sdb.refund = ce.prev | ||
} | ||
|
||
// `Clone` implements `journal.CacheEntry`. | ||
func (ce *RefundChange) Clone() journal.CacheEntry { | ||
return &RefundChange{ | ||
sdb: ce.sdb, | ||
prev: ce.prev, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
package state | ||
|
||
import ( | ||
"github.com/berachain/stargazer/core/state/store/journal" | ||
coretypes "github.com/berachain/stargazer/core/types" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AddLogChange", func() { | ||
var ( | ||
ce *AddLogChange | ||
sdb *StateDB | ||
) | ||
BeforeEach(func() { | ||
sdb = &StateDB{ | ||
logs: []*coretypes.Log{}, | ||
} | ||
ce = &AddLogChange{ | ||
sdb: sdb, | ||
} | ||
}) | ||
It("implements journal.CacheEntry", func() { | ||
var _ journal.CacheEntry = ce | ||
Expect(ce).To(BeAssignableToTypeOf(&AddLogChange{})) | ||
}) | ||
It("Revert should remove the last log", func() { | ||
sdb.logs = append(sdb.logs, &coretypes.Log{}) | ||
ce.Revert() | ||
Expect(len(sdb.logs)).To(Equal(0)) | ||
}) | ||
It("Clone should return a new AddLogChange with the same sdb", func() { | ||
cloned, ok := ce.Clone().(*AddLogChange) | ||
Expect(ok).To(BeTrue()) | ||
Expect(cloned.sdb).To(Equal(sdb)) | ||
Expect(cloned).ToNot(BeIdenticalTo(ce)) | ||
}) | ||
}) | ||
|
||
var _ = Describe("RefundChange", func() { | ||
var ( | ||
ce *RefundChange | ||
sdb *StateDB | ||
) | ||
|
||
BeforeEach(func() { | ||
sdb = &StateDB{ | ||
refund: 0, | ||
} | ||
ce = &RefundChange{ | ||
sdb: sdb, | ||
prev: 0, | ||
} | ||
}) | ||
It("implements journal.CacheEntry", func() { | ||
var _ journal.CacheEntry = ce | ||
Expect(ce).To(BeAssignableToTypeOf(&RefundChange{})) | ||
}) | ||
It("Revert should restore the previous refund value", func() { | ||
sdb.refund = 100 | ||
ce.prev = 50 | ||
ce.Revert() | ||
Expect(sdb.refund).To(Equal(uint64(50))) | ||
}) | ||
It("Clone should return a new RefundChange with the same sdb and prev", func() { | ||
cloned, ok := ce.Clone().(*RefundChange) | ||
Expect(ok).To(BeTrue()) | ||
Expect(cloned.sdb).To(Equal(sdb)) | ||
Expect(cloned.prev).To(Equal(ce.prev)) | ||
Expect(cloned).ToNot(BeIdenticalTo(ce)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
package state | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/core/vm" | ||
|
||
"github.com/berachain/stargazer/lib/common" | ||
) | ||
|
||
type GethStateDB = vm.StateDB | ||
|
||
// `StargazerStateDB` defines an extension to the interface provided by go-ethereum to | ||
// support additional state transition functionalities that are useful in a Cosmos SDK context. | ||
type StargazerStateDB interface { | ||
GethStateDB | ||
|
||
// TransferBalance transfers the balance from one account to another | ||
TransferBalance(common.Address, common.Address, *big.Int) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
package state_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestState(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "core/state") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe for the revert in the SDB we store a mapping of reflect.Type(RefundChange) to revert function with the signature of
func (sdb *StateDB) {}
Then in sdb RevertToSnapshot we have a for loop which goes through the journal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why iterate over types (map keys) instead of iterating over journal indices (journal keys are just slice indices) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cuz these are all journal entries so its already taken care of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah was just a thought, evm.go updates probably nil this.