diff --git a/cosmos/x/evm/plugins/base.go b/cosmos/x/evm/plugins/base.go index d799de83a..a88eadf76 100644 --- a/cosmos/x/evm/plugins/base.go +++ b/cosmos/x/evm/plugins/base.go @@ -28,9 +28,7 @@ import ( // Base is the base interface which all x/evm Polaris plugins must implement -type Base interface { - IsPlugin() -} +type Base interface{} // HasGenesis represents the base class that all x/evm Polaris plugins which have // InitGenesis or ExportGenesis methods must implement diff --git a/cosmos/x/evm/plugins/block/plugin.go b/cosmos/x/evm/plugins/block/plugin.go index 3edfeaaaa..415939f1a 100644 --- a/cosmos/x/evm/plugins/block/plugin.go +++ b/cosmos/x/evm/plugins/block/plugin.go @@ -89,5 +89,3 @@ func (p *plugin) GetNewBlockMetadata(number uint64) (common.Address, uint64) { } return common.BytesToAddress(valBz), uint64(cometHeader.Time.UTC().Unix()) } - -func (p *plugin) IsPlugin() {} diff --git a/cosmos/x/evm/plugins/configuration/plugin.go b/cosmos/x/evm/plugins/configuration/plugin.go index a58c100be..3110e8f9a 100644 --- a/cosmos/x/evm/plugins/configuration/plugin.go +++ b/cosmos/x/evm/plugins/configuration/plugin.go @@ -67,5 +67,3 @@ func (p *plugin) FeeCollector() *common.Address { addr := common.BytesToAddress([]byte(authtypes.FeeCollectorName)) return &addr } - -func (p *plugin) IsPlugin() {} diff --git a/cosmos/x/evm/plugins/gas/plugin.go b/cosmos/x/evm/plugins/gas/plugin.go index 793c9a8e7..dfe453e34 100644 --- a/cosmos/x/evm/plugins/gas/plugin.go +++ b/cosmos/x/evm/plugins/gas/plugin.go @@ -126,5 +126,3 @@ func (p *plugin) resetMeters(ctx sdk.Context) { p.consensusMaxGas = uint64(block.MaxGas) } } - -func (p *plugin) IsPlugin() {} diff --git a/cosmos/x/evm/plugins/historical/plugin.go b/cosmos/x/evm/plugins/historical/plugin.go index 4846912a5..eb19e21b6 100644 --- a/cosmos/x/evm/plugins/historical/plugin.go +++ b/cosmos/x/evm/plugins/historical/plugin.go @@ -69,5 +69,3 @@ func NewPlugin( func (p *plugin) Prepare(ctx context.Context) { p.ctx = sdk.UnwrapSDKContext(ctx) } - -func (p *plugin) IsPlugin() {} diff --git a/cosmos/x/evm/plugins/precompile/plugin.go b/cosmos/x/evm/plugins/precompile/plugin.go index 68cdcd381..8f0aeaed8 100644 --- a/cosmos/x/evm/plugins/precompile/plugin.go +++ b/cosmos/x/evm/plugins/precompile/plugin.go @@ -207,5 +207,3 @@ func (p *plugin) disableReentrancy(sdb vm.PolarisStateDB) { // restore ctx gas configs for continuing precompile execution p.sp.SetGasConfig(p.kvGasConfig, p.transientKVGasConfig) } - -func (p *plugin) IsPlugin() {} diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index 95cdf11ce..af3ac8573 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -546,4 +546,3 @@ func (p *plugin) SetGasConfig(kvGasConfig, transientKVGasConfig storetypes.GasCo } // IsPlugin implements plugins.Base. -func (p *plugin) IsPlugin() {} diff --git a/cosmos/x/evm/plugins/txpool/plugin.go b/cosmos/x/evm/plugins/txpool/plugin.go index 2f3fcd4b4..fd9e9e75b 100644 --- a/cosmos/x/evm/plugins/txpool/plugin.go +++ b/cosmos/x/evm/plugins/txpool/plugin.go @@ -120,5 +120,3 @@ func (p *plugin) SendPrivTx(signedTx *coretypes.Transaction) error { // the tx. return p.EthTxPool.Insert(sdk.Context{}.WithPriority(signedTx.GasPrice().Int64()), cosmosTx) } - -func (p *plugin) IsPlugin() {} diff --git a/eth/core/state/journal/access_list.go b/eth/core/state/journal/access_list.go index 12e4080ee..14b54117f 100644 --- a/eth/core/state/journal/access_list.go +++ b/eth/core/state/journal/access_list.go @@ -22,8 +22,6 @@ package journal import ( "pkg.berachain.dev/polaris/eth/common" - "pkg.berachain.dev/polaris/lib/ds" - "pkg.berachain.dev/polaris/lib/ds/stack" libtypes "pkg.berachain.dev/polaris/lib/types" "pkg.berachain.dev/polaris/lib/utils" ) @@ -43,18 +41,17 @@ type Accesslist interface { AddressInAccessList(common.Address) bool } +// accessList is a `baseJournal` that tracks the access list. type accessList struct { - *AccessList // current access list, always the head of journal stack. - journal ds.Stack[*AccessList] // journal of access lists. + baseJournal[*AccessList] // journal of access lists. } // NewAccesslist returns a new `accessList` journal. func NewAccesslist() Accesslist { - journal := stack.New[*AccessList](initCapacity) + journal := newBaseJournal[*AccessList](initCapacity) journal.Push(NewAccessList()) return &accessList{ - AccessList: journal.Peek(), - journal: journal, + baseJournal: journal, } } @@ -65,35 +62,31 @@ func (al *accessList) RegistryKey() string { // AddAddressToAccessList implements `state.AccessListJournal`. func (al *accessList) AddAddressToAccessList(addr common.Address) { - al.AddAddress(addr) + al.Peek().AddAddress(addr) } // AddSlotToAccessList implements `state.AccessListJournal`. func (al *accessList) AddSlotToAccessList(addr common.Address, slot common.Hash) { - al.AddSlot(addr, slot) + al.Peek().AddSlot(addr, slot) } // AddressInAccessList implements `state.AccessListJournal`. func (al *accessList) AddressInAccessList(addr common.Address) bool { - return al.ContainsAddress(addr) + return al.Peek().ContainsAddress(addr) } // SlotInAccessList implements `state.AccessListJournal`. func (al *accessList) SlotInAccessList(addr common.Address, slot common.Hash) (bool, bool) { - return al.Contains(addr, slot) + return al.Peek().Contains(addr, slot) } -// `Snapshot` implements `libtypes.Snapshottable`. +// Snapshot implements `libtypes.Snapshottable`. func (al *accessList) Snapshot() int { - al.AccessList = al.AccessList.Copy() - al.journal.Push(al.AccessList) - return al.journal.Size() - 1 -} - -// RevertToSnapshot implements `libtypes.Snapshottable`. -func (al *accessList) RevertToSnapshot(id int) { - al.journal.PopToSize(id) - al.AccessList = al.journal.Peek() + al.Push(al.Peek().Copy()) + // Accesslist is size minus one, since we want to revert to the place in the stack + // where snapshot was called, which since we need to push a copy on the stack, is + // the size minus one. + return al.baseJournal.Size() - 1 } // Finalize implements `libtypes.Controllable`. @@ -103,15 +96,12 @@ func (al *accessList) Finalize() { // Clone implements `libtypes.Cloneable`. func (al *accessList) Clone() Accesslist { - size := al.journal.Size() cpy := &accessList{ - AccessList: al.AccessList.Copy(), - journal: stack.New[*AccessList](size), + baseJournal: newBaseJournal[*AccessList](al.Capacity()), } - cpy.journal.Push(cpy.AccessList) - for i := 1; i < size; i++ { // skip the root, already pushed above - cpy.journal.Push(al.journal.PeekAt(i).Copy()) + for i := 0; i < al.Size(); i++ { // skip the root, already pushed above + cpy.Push(al.PeekAt(i).Copy()) } return cpy diff --git a/eth/core/state/journal/access_list_test.go b/eth/core/state/journal/access_list_test.go index 4c6743919..b34ab36bb 100644 --- a/eth/core/state/journal/access_list_test.go +++ b/eth/core/state/journal/access_list_test.go @@ -46,49 +46,37 @@ var _ = Describe("AccessList", func() { }) It("should support controllable access list operations", func() { - Expect(al.AddAddress(a1)).To(BeTrue()) - Expect(al.ContainsAddress(a1)).To(BeTrue()) - Expect(al.ContainsAddress(a2)).To(BeFalse()) - al.DeleteAddress(a1) - Expect(al.ContainsAddress(a1)).To(BeFalse()) + al.AddAddressToAccessList(a1) + Expect(al.AddressInAccessList(a1)).To(BeTrue()) + Expect(al.AddressInAccessList(a2)).To(BeFalse()) + al.Peek().DeleteAddress(a1) + Expect(al.AddressInAccessList(a1)).To(BeFalse()) - ac, sc := al.AddSlot(a1, s1) - Expect(ac).To(BeTrue()) - Expect(sc).To(BeTrue()) - ac, sc = al.AddSlot(a1, s2) - Expect(ac).To(BeFalse()) - Expect(sc).To(BeTrue()) + al.AddSlotToAccessList(a1, s1) + al.AddSlotToAccessList(a1, s2) id := al.Snapshot() + al.AddSlotToAccessList(a2, s1) - ac, sc = al.AddSlot(a2, s1) - Expect(ac).To(BeTrue()) - Expect(sc).To(BeTrue()) - Expect(al.ContainsAddress(a2)).To(BeTrue()) + Expect(al.AddressInAccessList(a2)).To(BeTrue()) al.RevertToSnapshot(id) - Expect(al.ContainsAddress(a2)).To(BeFalse()) + Expect(al.AddressInAccessList(a2)).To(BeFalse()) Expect(func() { al.Finalize() }).ToNot(Panic()) - Expect(al.journal.Size()).To(Equal(1)) + Expect(al.Size()).To(Equal(1)) }) It("should clone correctly", func() { - ac, sc := al.AddSlot(a1, s1) - Expect(ac).To(BeTrue()) - Expect(sc).To(BeTrue()) - ac, sc = al.AddSlot(a1, s2) - Expect(ac).To(BeFalse()) - Expect(sc).To(BeTrue()) + al.AddSlotToAccessList(a1, s1) + al.AddSlotToAccessList(a1, s2) al2 := utils.MustGetAs[*accessList](al.Clone()) - Expect(al2.ContainsAddress(a1)).To(BeTrue()) - Expect(al2.ContainsAddress(a2)).To(BeFalse()) + Expect(al2.AddressInAccessList(a1)).To(BeTrue()) + Expect(al2.AddressInAccessList(a2)).To(BeFalse()) - ac, sc = al2.AddSlot(a2, s1) - Expect(ac).To(BeTrue()) - Expect(sc).To(BeTrue()) - Expect(al2.ContainsAddress(a2)).To(BeTrue()) - Expect(al.ContainsAddress(a2)).To(BeFalse()) + al2.AddSlotToAccessList(a2, s1) + Expect(al2.AddressInAccessList(a2)).To(BeTrue()) + Expect(al.AddressInAccessList(a2)).To(BeFalse()) }) }) diff --git a/eth/core/state/journal/base.go b/eth/core/state/journal/base.go new file mode 100644 index 000000000..af87a40c3 --- /dev/null +++ b/eth/core/state/journal/base.go @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2023, Berachain Foundation. All rights reserved. +// Use of this software is govered by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package journal + +import ( + "pkg.berachain.dev/polaris/lib/ds" + "pkg.berachain.dev/polaris/lib/ds/stack" +) + +// baseJournal is a struct that holds a stack of items. +type baseJournal[T any] struct { + ds.Stack[T] +} + +// newBaseJournal returns a new `baseJournal` with the given initial capacity. +func newBaseJournal[T any](initialCapacity int) baseJournal[T] { + return baseJournal[T]{ + Stack: stack.New[T](initialCapacity), + } +} + +// Snapshot takes a snapshot of the `Logs` store. +// +// Snapshot implements `libtypes.Snapshottable`. +func (j *baseJournal[T]) Snapshot() int { + return j.Size() +} + +// RevertToSnapshot reverts the `Logs` store to a given snapshot id. +// +// RevertToSnapshot implements `libtypes.Snapshottable`. +func (j *baseJournal[T]) RevertToSnapshot(id int) { + j.PopToSize(id) +} diff --git a/eth/core/state/journal/logs.go b/eth/core/state/journal/logs.go index 18eb788e2..8157bdff4 100644 --- a/eth/core/state/journal/logs.go +++ b/eth/core/state/journal/logs.go @@ -23,8 +23,6 @@ package journal import ( "pkg.berachain.dev/polaris/eth/common" coretypes "pkg.berachain.dev/polaris/eth/core/types" - "pkg.berachain.dev/polaris/lib/ds" - "pkg.berachain.dev/polaris/lib/ds/stack" libtypes "pkg.berachain.dev/polaris/lib/types" ) @@ -48,7 +46,7 @@ type Log interface { // logs is a state plugin that tracks Ethereum logs. type logs struct { - journal ds.Stack[*coretypes.Log] // journal of logs that resets on every tx + baseJournal[*coretypes.Log] txHash common.Hash txIndex int } @@ -56,7 +54,7 @@ type logs struct { // NewLogs returns a new `logs` journal. func NewLogs() Log { return &logs{ - journal: stack.New[*coretypes.Log](initCapacity), + baseJournal: newBaseJournal[*coretypes.Log](initCapacity), } } @@ -67,7 +65,7 @@ func (l *logs) RegistryKey() string { // SetTxContext sets the transaction hash and index for the current transaction. func (l *logs) SetTxContext(thash common.Hash, ti int) { - l.journal = stack.New[*coretypes.Log](initCapacity) + l.baseJournal = newBaseJournal[*coretypes.Log](initCapacity) // Set the transaction hash and index. l.txHash = thash l.txIndex = ti @@ -82,45 +80,31 @@ func (l *logs) TxIndex() int { func (l *logs) AddLog(log *coretypes.Log) { log.TxHash = l.txHash log.TxIndex = uint(l.txIndex) - l.journal.Push(log) + l.Push(log) } // Logs returns the logs for the current tx with the existing metadata. func (l *logs) Logs() []*coretypes.Log { - size := l.journal.Size() + size := l.Size() buf := make([]*coretypes.Log, size) for i := 0; i < size; i++ { - buf[i] = l.journal.PeekAt(i) + buf[i] = l.PeekAt(i) } return buf } // GetLogs returns the logs for the tx with the given metadata. func (l *logs) GetLogs(_ common.Hash, blockNumber uint64, blockHash common.Hash) []*coretypes.Log { - size := l.journal.Size() + size := l.Size() buf := make([]*coretypes.Log, size) for i := 0; i < size; i++ { - buf[i] = l.journal.PeekAt(i) + buf[i] = l.PeekAt(i) buf[i].BlockHash = blockHash buf[i].BlockNumber = blockNumber } return buf } -// Snapshot takes a snapshot of the `Logs` store. -// -// Snapshot implements `libtypes.Snapshottable`. -func (l *logs) Snapshot() int { - return l.journal.Size() -} - -// RevertToSnapshot reverts the `Logs` store to a given snapshot id. -// -// RevertToSnapshot implements `libtypes.Snapshottable`. -func (l *logs) RevertToSnapshot(id int) { - l.journal.PopToSize(id) -} - // Finalize clears the journal of the tx logs. // // Finalize implements `libtypes.Controllable`. @@ -128,19 +112,17 @@ func (l *logs) Finalize() {} // Clone implements `libtypes.Cloneable`. func (l *logs) Clone() Log { - capacity := l.journal.Capacity() - size := l.journal.Size() clone := &logs{ - journal: stack.New[*coretypes.Log](capacity), - txHash: l.txHash, - txIndex: l.txIndex, + baseJournal: newBaseJournal[*coretypes.Log](l.Capacity()), + txHash: l.txHash, + txIndex: l.txIndex, } // copy every individual log from the journal - for i := 0; i < size; i++ { + for i := 0; i < l.Size(); i++ { cpy := new(coretypes.Log) - *cpy = *l.journal.PeekAt(i) - clone.journal.Push(cpy) + *cpy = *l.PeekAt(i) + clone.Push(cpy) } return clone diff --git a/eth/core/state/journal/logs_test.go b/eth/core/state/journal/logs_test.go index 165fe15ef..01091bbf9 100644 --- a/eth/core/state/journal/logs_test.go +++ b/eth/core/state/journal/logs_test.go @@ -42,7 +42,7 @@ var _ = Describe("Logs", func() { BeforeEach(func() { l = utils.MustGetAs[*logs](NewLogs()) l.SetTxContext(thash, int(ti)) - Expect(l.journal.Capacity()).To(Equal(32)) + Expect(l.Capacity()).To(Equal(32)) }) It("should have the correct registry key", func() { @@ -52,21 +52,21 @@ var _ = Describe("Logs", func() { When("adding logs", func() { BeforeEach(func() { l.AddLog(&coretypes.Log{Address: a1}) - Expect(l.journal.Size()).To(Equal(1)) - Expect(l.journal.PeekAt(0).Address).To(Equal(a1)) - Expect(l.journal.PeekAt(0).TxHash).To(Equal(thash)) - Expect(l.journal.PeekAt(0).TxIndex).To(Equal(ti)) + Expect(l.Size()).To(Equal(1)) + Expect(l.PeekAt(0).Address).To(Equal(a1)) + Expect(l.PeekAt(0).TxHash).To(Equal(thash)) + Expect(l.PeekAt(0).TxIndex).To(Equal(ti)) }) It("should correctly snapshot and revert", func() { id := l.Snapshot() l.AddLog(&coretypes.Log{Address: a2}) - Expect(l.journal.Size()).To(Equal(2)) - Expect(l.journal.PeekAt(1).Address).To(Equal(a2)) + Expect(l.Size()).To(Equal(2)) + Expect(l.PeekAt(1).Address).To(Equal(a2)) l.RevertToSnapshot(id) - Expect(l.journal.Size()).To(Equal(1)) + Expect(l.Size()).To(Equal(1)) }) It("should correctly get logs", func() { @@ -88,18 +88,18 @@ var _ = Describe("Logs", func() { It("should correctly clone", func() { l.AddLog(&coretypes.Log{Address: a2}) - Expect(l.journal.Size()).To(Equal(2)) - Expect(l.journal.PeekAt(1).Address).To(Equal(a2)) + Expect(l.Size()).To(Equal(2)) + Expect(l.PeekAt(1).Address).To(Equal(a2)) l2 := utils.MustGetAs[*logs](l.Clone()) - Expect(l2.journal.Size()).To(Equal(2)) - Expect(l2.journal.PeekAt(0).Address).To(Equal(a1)) - Expect(l2.journal.PeekAt(1).Address).To(Equal(a2)) + Expect(l2.Size()).To(Equal(2)) + Expect(l2.PeekAt(0).Address).To(Equal(a1)) + Expect(l2.PeekAt(1).Address).To(Equal(a2)) l2.AddLog(&coretypes.Log{Address: a3}) - Expect(l2.journal.Size()).To(Equal(3)) - Expect(l2.journal.PeekAt(2).Address).To(Equal(a3)) - Expect(l.journal.Size()).To(Equal(2)) + Expect(l2.Size()).To(Equal(3)) + Expect(l2.PeekAt(2).Address).To(Equal(a3)) + Expect(l.Size()).To(Equal(2)) }) }) }) diff --git a/eth/core/state/journal/refund.go b/eth/core/state/journal/refund.go index 31983f317..47a4d9249 100644 --- a/eth/core/state/journal/refund.go +++ b/eth/core/state/journal/refund.go @@ -21,8 +21,6 @@ package journal import ( - "pkg.berachain.dev/polaris/lib/ds" - "pkg.berachain.dev/polaris/lib/ds/stack" libtypes "pkg.berachain.dev/polaris/lib/types" ) @@ -42,13 +40,14 @@ type Refund interface { // refund is a `Store` that tracks the refund counter. type refund struct { - ds.Stack[uint64] // journal of historical refunds. + baseJournal[uint64] // journal of historical refunds. + } // NewRefund creates and returns a `refund` journal. func NewRefund() Refund { return &refund{ - Stack: stack.New[uint64](initCapacity), + baseJournal: newBaseJournal[uint64](initCapacity), } } @@ -73,33 +72,17 @@ func (r *refund) SubRefund(gas uint64) { r.Push(r.Peek() - gas) } -// Snapshot returns the current size of the refund counter, which is used to -// revert the refund counter to a previous value. -// -// Snapshot implements `libtypes.Snapshottable`. -func (r *refund) Snapshot() int { - return r.Size() -} - -// RevertToSnapshot reverts the refund counter to the value at the given `snap`. -// -// RevertToSnapshot implements `libtypes.Snapshottable`. -func (r *refund) RevertToSnapshot(id int) { - r.PopToSize(id) -} - // Finalize implements `libtypes.Controllable`. func (r *refund) Finalize() { - r.Stack = stack.New[uint64](initCapacity) + r.baseJournal = newBaseJournal[uint64](initCapacity) } // Clone implements `libtypes.Cloneable`. func (r *refund) Clone() Refund { - size := r.Size() clone := &refund{ - Stack: stack.New[uint64](size), + baseJournal: newBaseJournal[uint64](initCapacity), } - for i := 0; i < size; i++ { + for i := 0; i < r.Size(); i++ { clone.Push(r.PeekAt(i)) } return clone diff --git a/eth/core/state/journal/selfdestruct_test.go b/eth/core/state/journal/selfdestruct_test.go index 564f1ceba..1f93e9e93 100644 --- a/eth/core/state/journal/selfdestruct_test.go +++ b/eth/core/state/journal/selfdestruct_test.go @@ -66,7 +66,7 @@ var _ = Describe("SelfDestructs", func() { s.Finalize() Expect(s.lastSnapshot).To(Equal(-1)) - Expect(s.journal.Size()).To(Equal(0)) + Expect(s.Size()).To(Equal(0)) }) It("should not suicide when snapshot is not called", func() { diff --git a/eth/core/state/journal/selfdestructs.go b/eth/core/state/journal/selfdestructs.go index 0d3e9d3c3..f96b448a8 100644 --- a/eth/core/state/journal/selfdestructs.go +++ b/eth/core/state/journal/selfdestructs.go @@ -25,8 +25,6 @@ import ( "pkg.berachain.dev/polaris/eth/common" "pkg.berachain.dev/polaris/eth/crypto" - "pkg.berachain.dev/polaris/lib/ds" - "pkg.berachain.dev/polaris/lib/ds/stack" libtypes "pkg.berachain.dev/polaris/lib/types" "pkg.berachain.dev/polaris/lib/utils" ) @@ -65,8 +63,8 @@ type SelfDestructs interface { // NOTE: we are only supporting one self destructed address per EVM call (and consequently per snapshot). type selfDestructs struct { // journal of suicide address per call, very rare to suicide so we alloc only 1 address - journal ds.Stack[*common.Address] - ssp selfDestructStatePlugin + baseJournal[*common.Address] + ssp selfDestructStatePlugin // lastSnapshot ensures that only 1 address is being self destructed per snapshot lastSnapshot int } @@ -74,7 +72,7 @@ type selfDestructs struct { // NewSelfDestructs returns a new selfDestructs journal. func NewSelfDestructs(ssp selfDestructStatePlugin) SelfDestructs { return &selfDestructs{ - journal: stack.New[*common.Address](initCapacity), + baseJournal: newBaseJournal[*common.Address](initCapacity), ssp: ssp, lastSnapshot: -1, } @@ -90,7 +88,7 @@ func (s *selfDestructs) RegistryKey() string { // until after Commit is called. func (s *selfDestructs) SelfDestruct(addr common.Address) { // ensure only one suicide per snapshot call - if s.journal.Size() > s.lastSnapshot { + if s.Size() > s.lastSnapshot { // pushed one suicide for this contract call, can do no more return } @@ -105,7 +103,7 @@ func (s *selfDestructs) SelfDestruct(addr common.Address) { s.ssp.SubBalance(addr, s.ssp.GetBalance(addr)) // add to journal. - s.journal.Push(&addr) + s.Push(&addr) } func (s *selfDestructs) Selfdestruct6780(_ common.Address) { @@ -115,8 +113,8 @@ func (s *selfDestructs) Selfdestruct6780(_ common.Address) { // HasSelfDestructed implements the PolarisStateDB interface by returning if the contract was self destructed // in current transaction. func (s *selfDestructs) HasSelfDestructed(addr common.Address) bool { - for i := s.journal.Size() - 1; i >= 0; i-- { - if *s.journal.PeekAt(i) == addr { + for i := s.Size() - 1; i >= 0; i-- { + if *s.PeekAt(i) == addr { return true } } @@ -126,21 +124,15 @@ func (s *selfDestructs) HasSelfDestructed(addr common.Address) bool { // GetSelfDestructs implements state.SelfDestructsJournal. func (s *selfDestructs) GetSelfDestructs() []common.Address { var suicidalAddrs []common.Address - for i := 0; i < s.journal.Size(); i++ { - suicidalAddrs = append(suicidalAddrs, *s.journal.PeekAt(i)) + for i := 0; i < s.Size(); i++ { + suicidalAddrs = append(suicidalAddrs, *s.PeekAt(i)) } return suicidalAddrs } -// Snapshot implements libtypes.Controllable. func (s *selfDestructs) Snapshot() int { - s.lastSnapshot = s.journal.Size() - return s.lastSnapshot -} - -// RevertToSnapshot implements libtypes.Controllable. -func (s *selfDestructs) RevertToSnapshot(id int) { - s.journal.PopToSize(id) + s.lastSnapshot = s.Size() + return s.baseJournal.Snapshot() } // Finalize implements libtypes.Controllable. @@ -150,18 +142,17 @@ func (s *selfDestructs) Finalize() { // Clone implements libtypes.Cloneable. func (s *selfDestructs) Clone() SelfDestructs { - size := s.journal.Size() clone := &selfDestructs{ - journal: stack.New[*common.Address](size), + baseJournal: newBaseJournal[*common.Address](s.Capacity()), ssp: s.ssp, lastSnapshot: s.lastSnapshot, } // copy every address from the journal - for i := 0; i < size; i++ { + for i := 0; i < s.Size(); i++ { cpy := new(common.Address) - *cpy = *s.journal.PeekAt(i) - clone.journal.Push(cpy) + *cpy = *s.PeekAt(i) + clone.Push(cpy) } return clone diff --git a/eth/core/state/journal/transient_storage.go b/eth/core/state/journal/transient_storage.go index 81891da4d..9d4602100 100644 --- a/eth/core/state/journal/transient_storage.go +++ b/eth/core/state/journal/transient_storage.go @@ -23,7 +23,6 @@ package journal import ( "github.com/ethereum/go-ethereum/common" - "pkg.berachain.dev/polaris/lib/ds" "pkg.berachain.dev/polaris/lib/ds/stack" libtypes "pkg.berachain.dev/polaris/lib/types" ) @@ -74,13 +73,13 @@ type TransientStorage interface { // `transientStorage` is a journal that tracks the transient state. type transientStorage struct { - ds.Stack[transientState] + baseJournal[transientState] } // `NewTransientStorage` returns a new `transient` journal. func NewTransientStorage() TransientStorage { return &transientStorage{ - stack.New[transientState](initCapacity), + newBaseJournal[transientState](initCapacity), } } @@ -105,16 +104,6 @@ func (t *transientStorage) GetTransientState(addr common.Address, key common.Has return t.Peek().Get(addr, key) } -// `Snapshot` implements `libtypes.Snapshottable`. -func (t *transientStorage) Snapshot() int { - return t.Size() -} - -// `RevertToSnapshot` implements `libtypes.Snapshottable`. -func (t *transientStorage) RevertToSnapshot(id int) { - t.PopToSize(id) -} - // `Finalize` implements `libtypes.Controllable`. func (t *transientStorage) Finalize() { t.Stack = stack.New[transientState](initCapacity) @@ -124,7 +113,7 @@ func (t *transientStorage) Finalize() { func (t *transientStorage) Clone() TransientStorage { size := t.Size() clone := &transientStorage{ - stack.New[transientState](size), + newBaseJournal[transientState](initCapacity), } // copy every individual transient state