Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

feat(statedb): Reintroduce and simplify StateDB #14

Merged
merged 95 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
8104037
add cachekv btree
Jan 11, 2023
d5e0489
bing bong
Jan 11, 2023
8afe910
stack
Jan 11, 2023
9303cf6
gomod
Jan 11, 2023
3534072
format
Jan 11, 2023
5376766
lmao what
Jan 11, 2023
fce73d8
lint
Jan 11, 2023
390c8a3
deep copy
Jan 11, 2023
1f4ff6c
trees to lib
Jan 11, 2023
e49053f
common
Jan 11, 2023
130a1c6
remove sdk alias
Jan 11, 2023
e986d02
lint imports
Jan 11, 2023
7052697
small cleanup
Jan 11, 2023
b3ce5f5
small cleanup
Jan 11, 2023
a5e4365
small cleanup
Jan 11, 2023
23e1bce
upgrade cachekv
Jan 11, 2023
7c9e9ac
improvements
Jan 11, 2023
b8beaeb
optimize control flow
Jan 11, 2023
4e28d4b
cvalue
Jan 11, 2023
fb19474
bing bong
Jan 11, 2023
29e490c
lint
Jan 11, 2023
e6ef005
bing bong
Jan 11, 2023
d1d4d56
cleanup test commands a bit
Jan 11, 2023
71b471a
Merge branch 'main' into cachekv
Jan 11, 2023
35ead99
Update lib/ds/stack.go
Jan 11, 2023
f4cd915
Update lib/ds/stack.go
Jan 11, 2023
262aedc
Merge branch 'main' into cachekv
Jan 11, 2023
f9a8a45
Merge branch 'cachekv' into upgrade-cachekv
Jan 11, 2023
1a11eac
typo
Jan 11, 2023
9d09be4
comments
Jan 11, 2023
cd3e74e
Revert "comments"
Jan 11, 2023
2e168ad
comments
Jan 11, 2023
b58b43d
Merge branch 'cachekv' into upgrade-cachekv
Jan 11, 2023
c1b0047
Merge branch 'main' into cachekv
Jan 11, 2023
6caa813
Merge branch 'main' into cachekv
Jan 11, 2023
770beb8
Merge branch 'cachekv' into upgrade-cachekv
Jan 11, 2023
cfd316d
lint
Jan 11, 2023
3ea6816
Apply suggestions from code review
Jan 11, 2023
50bd41f
Merge branch 'cachekv' into upgrade-cachekv
Jan 11, 2023
08ce21f
bing bong
Jan 12, 2023
a32c899
statedb reader
Jan 12, 2023
1903e7a
cleanup
Jan 12, 2023
f77aab1
lint
Jan 12, 2023
c40bf96
lint
Jan 12, 2023
02c0359
lint
Jan 12, 2023
365f0bf
Merge branch 'main' into cachekv
Jan 12, 2023
d8e6c68
Merge branch 'cachekv' into upgrade-cachekv
Jan 12, 2023
78781ce
Merge branch 'upgrade-cachekv' into statedb
Jan 12, 2023
6eb5c7e
statedb minor improvements
Jan 12, 2023
ef13e1d
transfer functions
Jan 12, 2023
bb8b5ff
improve comment
Jan 12, 2023
f231837
name
Jan 12, 2023
01ba137
bing bong
Jan 12, 2023
56b93f6
cleaner
Jan 12, 2023
bec6b4e
remove prefix store for performance increase
Jan 12, 2023
92a8f48
cleanup
Jan 12, 2023
da306b5
more changes
Jan 12, 2023
d555d0b
dont wanna break the chain on god fr fr
Jan 12, 2023
3013b37
bing bong
Jan 12, 2023
e236118
bing bong
Jan 12, 2023
c809c24
test get refund
Jan 12, 2023
b4e04be
add log tests
Jan 12, 2023
122e77c
finish statedb tests
Jan 12, 2023
1b0108e
improve code coverage
Jan 12, 2023
a43dc6d
cache entry test
Jan 12, 2023
7d6892e
cache entry test
Jan 12, 2023
ace2abf
fix test
Jan 12, 2023
897ba67
fix
Jan 12, 2023
381d0a3
opt
Jan 13, 2023
80ec2a5
merge main
Jan 13, 2023
1a8c381
cache value implements Cloneable
calbera Jan 13, 2023
ff4a882
clean struct creators
calbera Jan 13, 2023
62e806c
comments
calbera Jan 13, 2023
74a6573
linter
Jan 13, 2023
6937207
amend
Jan 13, 2023
d54f460
Merge branch 'main' into upgrade-cachekv
Jan 13, 2023
e92845c
impr(cachekv): Remove DeleteCacheValue (#19)
Jan 13, 2023
71c9add
Merge branch 'main' into upgrade-cachekv
Jan 13, 2023
15eee26
benchmarks
Jan 13, 2023
6944532
convert more tests
Jan 13, 2023
5f661ab
rename
Jan 13, 2023
8162272
test name
Jan 13, 2023
8479b36
renaming, cleanup
calbera Jan 13, 2023
df6b357
Merge branch 'upgrade-cachekv' of github.com:berachain/stargazer into…
calbera Jan 13, 2023
d8d81d3
fix merge
Jan 13, 2023
5c8679a
lint
Jan 13, 2023
5468e71
Merge branch 'upgrade-cachekv' into statedb
Jan 13, 2023
9dcb6a3
merge
Jan 13, 2023
ab229b7
evm namespace
Jan 13, 2023
f64c236
Merge branch 'main' into statedb
Jan 13, 2023
22a37a5
bing bong
Jan 15, 2023
c7febf6
remove log factory from statedb
Jan 16, 2023
bb3f9bb
Merge branch 'main' into statedb
Jan 16, 2023
a807952
merge
Jan 16, 2023
2d951fb
remove from interface for now
Jan 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core/evm.go
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)
}
68 changes: 68 additions & 0 deletions core/state/cache_entries.go
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() {
Copy link
Author

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.

Copy link
Contributor

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) ?

Copy link
Contributor

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

Copy link
Author

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.

ce.sdb.refund = ce.prev
}

// `Clone` implements `journal.CacheEntry`.
func (ce *RefundChange) Clone() journal.CacheEntry {
return &RefundChange{
sdb: ce.sdb,
prev: ce.prev,
}
}
86 changes: 86 additions & 0 deletions core/state/cache_entries_test.go
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))
})
})
34 changes: 34 additions & 0 deletions core/state/interface.go
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)
}
27 changes: 27 additions & 0 deletions core/state/state_test.go
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")
}
Loading