Skip to content

Commit

Permalink
update NewView interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine committed Nov 28, 2023
1 parent afa056b commit 192072c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
33 changes: 33 additions & 0 deletions scripts/mocks.mockgen.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
github.com/ava-labs/avalanchego/api/server=Server=api/server/mock_server.go
github.com/ava-labs/avalanchego/chains/atomic=SharedMemory=chains/atomic/mock_shared_memory.go
github.com/ava-labs/avalanchego/codec=Manager=codec/mock_manager.go
github.com/ava-labs/avalanchego/database=Batch=database/mock_batch.go
github.com/ava-labs/avalanchego/database=Iterator=database/mock_iterator.go
github.com/ava-labs/avalanchego/message=OutboundMessage=message/mock_message.go
github.com/ava-labs/avalanchego/message=OutboundMsgBuilder=message/mock_outbound_message_builder.go
github.com/ava-labs/avalanchego/network/peer=GossipTracker=network/peer/mock_gossip_tracker.go
github.com/ava-labs/avalanchego/network/p2p=Handler=network/p2p/mocks/mock_handler.go
github.com/ava-labs/avalanchego/snow/consensus/snowman=Block=snow/consensus/snowman/mock_block.go
github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex=LinearizableVM=snow/engine/avalanche/vertex/mock_vm.go
github.com/ava-labs/avalanchego/snow/engine/snowman/block=BuildBlockWithContextChainVM=snow/engine/snowman/block/mocks/build_block_with_context_vm.go
github.com/ava-labs/avalanchego/snow/engine/snowman/block=ChainVM=snow/engine/snowman/block/mocks/chain_vm.go
github.com/ava-labs/avalanchego/snow/engine/snowman/block=StateSyncableVM=snow/engine/snowman/block/mocks/state_syncable_vm.go
github.com/ava-labs/avalanchego/snow/engine/snowman/block=WithVerifyContext=snow/engine/snowman/block/mocks/with_verify_context.go
github.com/ava-labs/avalanchego/snow/networking/handler=Handler=snow/networking/handler/mock_handler.go
github.com/ava-labs/avalanchego/snow/networking/timeout=Manager=snow/networking/timeout/mock_manager.go
github.com/ava-labs/avalanchego/snow/networking/tracker=Targeter=snow/networking/tracker/mock_targeter.go
github.com/ava-labs/avalanchego/snow/networking/tracker=Tracker=snow/networking/tracker/mock_resource_tracker.go
github.com/ava-labs/avalanchego/snow/uptime=Calculator=snow/uptime/mock_calculator.go
github.com/ava-labs/avalanchego/snow/validators=State=snow/validators/mock_state.go
github.com/ava-labs/avalanchego/snow/validators=SubnetConnector=snow/validators/mock_subnet_connector.go
github.com/ava-labs/avalanchego/utils/crypto/keychain=Ledger=utils/crypto/keychain/mock_ledger.go
github.com/ava-labs/avalanchego/utils/filesystem=Reader=utils/filesystem/mock_io.go
github.com/ava-labs/avalanchego/utils/hashing=Hasher=utils/hashing/mock_hasher.go
github.com/ava-labs/avalanchego/utils/logging=Logger=utils/logging/mock_logger.go
github.com/ava-labs/avalanchego/utils/resource=User=utils/resource/mock_user.go
github.com/ava-labs/avalanchego/vms/avm/block=Block=vms/avm/block/mock_block.go
github.com/ava-labs/avalanchego/vms/avm/metrics=Metrics=vms/avm/metrics/mock_metrics.go
github.com/ava-labs/avalanchego/vms/avm/state=Chain,State,Diff=vms/avm/state/mock_state.go
github.com/ava-labs/avalanchego/vms/avm/txs/mempool=Mempool=vms/avm/txs/mempool/mock_mempool.go
github.com/ava-labs/avalanchego/vms/components/avax=TransferableIn=vms/components/avax/mock_transferable_in.go
github.com/ava-labs/avalanchego/vms/components/verify=Verifiable=vms/components/verify/mock_verifiable.go
github.com/ava-labs/avalanchego/vms/platformvm/block/executor=Manager=vms/platformvm/block/executor/mock_manager.go
github.com/ava-labs/avalanchego/vms/platformvm/block=Block=vms/platformvm/block/mock_block.go
github.com/ava-labs/avalanchego/vms/platformvm/state=Chain,Diff,State,Versions=vms/platformvm/state/mock_state.go
Expand Down
15 changes: 11 additions & 4 deletions vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package state

import (
"context"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -80,20 +81,26 @@ func NewDiff(
}, nil
}

func (d *diff) NewView(ops []database.BatchOp) (merkledb.TrieView, error) {
// Returns a view that contains the merkle state of this diff.
func (d *diff) NewView() (merkledb.TrieView, error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

diffOps, err := d.GetMerkleChanges()
ops, err := d.GetMerkleChanges()
if err != nil {
return nil, err
}

ops = append(diffOps, ops...)
parentView, err := parentState.NewView()
if err != nil {
return nil, err
}

return parentState.NewView(ops)
return parentView.NewView(context.Background(), merkledb.ViewChanges{
BatchOps: ops,
})
}

func (d *diff) GetTimestamp() time.Time {
Expand Down
24 changes: 12 additions & 12 deletions vms/platformvm/state/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Chain interface {
avax.UTXOGetter
avax.UTXODeleter

NewView(ops []database.BatchOp) (merkledb.TrieView, error)
NewView() (merkledb.TrieView, error)

GetTimestamp() time.Time
SetTimestamp(tm time.Time)
Expand Down Expand Up @@ -1507,10 +1507,8 @@ func (s *state) processPendingStakers() (map[ids.ID]*stakersData, error) {
return output, nil
}

func (s *state) NewView(ops []database.BatchOp) (merkledb.TrieView, error) {
return s.merkleDB.NewView(context.TODO(), merkledb.ViewChanges{
BatchOps: ops,
})
func (s *state) NewView() (merkledb.TrieView, error) {
return s.merkleDB.NewView(context.TODO(), merkledb.ViewChanges{})
}

func (s *state) getMerkleChanges(currentData, pendingData map[ids.ID]*stakersData) ([]database.BatchOp, error) {
Expand All @@ -1535,10 +1533,14 @@ func (s *state) writeMerkleState(currentData, pendingData map[ids.ID]*stakersDat
if err != nil {
return err
}
view, err := s.NewView(changes)

view, err := s.merkleDB.NewView(context.TODO(), merkledb.ViewChanges{
BatchOps: changes,
})
if err != nil {
return err
}

if err := view.CommitToDB(context.Background()); err != nil {
return err
}
Expand Down

0 comments on commit 192072c

Please sign in to comment.