Skip to content

Commit

Permalink
Pulled the contract updates from snapshots, removed by block filtering (
Browse files Browse the repository at this point in the history
#1569)

* Pulled the contract updates from snapshots, removed by block filtering

* Fixed a wrong check

* Removed temporary fix

* Added the first block processing

* Added an error handling

* Fixed a linter issue
  • Loading branch information
esuwu authored Jan 9, 2025
1 parent cdb6083 commit 7960470
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
6 changes: 3 additions & 3 deletions pkg/blockchaininfo/blockchaininfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestChangesGenerationNewEntries(t *testing.T) {
}

equal, changes, err := blockchaininfo.CompareBUpdatesInfo(currentBlockInfo, previousBlockInfo,
proto.TestNetScheme, 10)
proto.TestNetScheme)
if err != nil {
return
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestChangesGenerationContainsPrevious(t *testing.T) {
}

equal, changes, err := blockchaininfo.CompareBUpdatesInfo(currentBlockInfo, previousBlockInfo,
proto.TestNetScheme, 10)
proto.TestNetScheme)
if err != nil {
return
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestNoChangesGeneration(t *testing.T) {
}

equal, changes, err := blockchaininfo.CompareBUpdatesInfo(currentBlockInfo, previousBlockInfo,
proto.TestNetScheme, 10)
proto.TestNetScheme)
if err != nil {
return
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/blockchaininfo/bupdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type BlockchainUpdatesExtension struct {
enableBlockchainUpdatesPlugin bool
l2ContractAddress proto.WavesAddress
bUpdatesChannel chan<- BUpdatesInfo
firstBlock bool
}

func NewBlockchainUpdatesExtension(
Expand All @@ -23,6 +24,7 @@ func NewBlockchainUpdatesExtension(
enableBlockchainUpdatesPlugin: true,
l2ContractAddress: l2ContractAddress,
bUpdatesChannel: bUpdatesChannel,
firstBlock: true,
}
}

Expand All @@ -34,6 +36,14 @@ func (e *BlockchainUpdatesExtension) L2ContractAddress() proto.WavesAddress {
return e.l2ContractAddress
}

func (e *BlockchainUpdatesExtension) IsFirstRequestedBlock() bool {
return e.firstBlock
}

func (e *BlockchainUpdatesExtension) FirstBlockDone() {
e.firstBlock = false
}

func (e *BlockchainUpdatesExtension) WriteBUpdates(bUpdates BUpdatesInfo) {
if e.bUpdatesChannel == nil {
return
Expand Down
1 change: 0 additions & 1 deletion pkg/blockchaininfo/nats_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func handleBlockchainUpdate(updates BUpdatesInfo, bu *BUpdatesExtensionState, sc
bu.currentState = &updates
if bu.previousState == nil {
// publish initial updates

filteredDataEntries, err := filterDataEntries(updates.BlockUpdatesInfo.Height-bu.Limit,
updates.ContractUpdatesInfo.AllDataEntries)
if err != nil {
Expand Down
18 changes: 3 additions & 15 deletions pkg/blockchaininfo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ type BUpdatesInfo struct {
// TODO wrap errors.

func CompareBUpdatesInfo(current, previous BUpdatesInfo,
scheme proto.Scheme, heightLimit uint64) (bool, BUpdatesInfo, error) {
scheme proto.Scheme) (bool, BUpdatesInfo, error) {
changes := BUpdatesInfo{
BlockUpdatesInfo: BlockUpdatesInfo{},
ContractUpdatesInfo: L2ContractDataEntries{},
}

equal := true
// todo REMOVE POINTERS
if current.BlockUpdatesInfo.Height != previous.BlockUpdatesInfo.Height {
equal = false
changes.BlockUpdatesInfo.Height = current.BlockUpdatesInfo.Height
Expand All @@ -58,22 +57,11 @@ func CompareBUpdatesInfo(current, previous BUpdatesInfo,
changes.BlockUpdatesInfo.BlockHeader = current.BlockUpdatesInfo.BlockHeader
}

previousFilteredDataEntries, err := filterDataEntries(previous.BlockUpdatesInfo.Height-heightLimit,
equalEntries, dataEntryChanges, err := compareDataEntries(current.ContractUpdatesInfo.AllDataEntries,
previous.ContractUpdatesInfo.AllDataEntries)
if err != nil {
return false, BUpdatesInfo{}, err
}
currentFilteredDataEntries, err := filterDataEntries(current.BlockUpdatesInfo.Height-heightLimit,
current.ContractUpdatesInfo.AllDataEntries)
if err != nil {
return false, BUpdatesInfo{}, err
}

equalEntries, dataEntryChanges, err := compareDataEntries(currentFilteredDataEntries,
previousFilteredDataEntries)
if err != nil {
return false, BUpdatesInfo{}, err
}
if !equalEntries {
equal = false
changes.ContractUpdatesInfo.AllDataEntries = dataEntryChanges
Expand Down Expand Up @@ -152,5 +140,5 @@ func compareDataEntries(current, previous proto.DataEntries) (bool, []proto.Data
}

func statesEqual(state BUpdatesExtensionState, scheme proto.Scheme) (bool, BUpdatesInfo, error) {
return CompareBUpdatesInfo(*state.currentState, *state.previousState, scheme, state.Limit)
return CompareBUpdatesInfo(*state.currentState, *state.previousState, scheme)
}
44 changes: 29 additions & 15 deletions pkg/state/appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,15 +842,10 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error {
}

// write updates into the updatesChannel here
// TODO possibly run it in a goroutine? make sure goroutines run in order?
if a.bUpdatesExtension != nil && a.bUpdatesExtension.EnableBlockchainUpdatesPlugin() {
// TODO get info from block snapshot?

// blockSnapshot.TxSnapshots.

updtErr := a.updateBlockchainUpdateInfo(blockInfo, params.block)
if updtErr != nil {
return updtErr
err = a.updateBlockchainUpdateInfo(blockInfo, params.block, blockSnapshot)
if err != nil {
return errors.Errorf("failed to request blockchain info from L2 smart contract state, %v", err)
}
}

Expand All @@ -875,12 +870,8 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error {
return a.blockDiffer.saveCurFeeDistr(params.block)
}

func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, blockHeader *proto.BlockHeader) error {
// TODO improve this by using diffs instead grabbing all the records every time
dataEntries, err := a.ia.state.RetrieveEntries(proto.NewRecipientFromAddress(a.bUpdatesExtension.L2ContractAddress()))
if err != nil && !errors.Is(err, proto.ErrNotFound) {
return err
}
func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, blockHeader *proto.BlockHeader,
blockSnapshot proto.BlockSnapshot) error {
blockID := blockHeader.BlockID()
bUpdatesInfo := blockchaininfo.BUpdatesInfo{
BlockUpdatesInfo: blockchaininfo.BlockUpdatesInfo{
Expand All @@ -890,10 +881,33 @@ func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, bloc
BlockHeader: *blockHeader,
},
ContractUpdatesInfo: blockchaininfo.L2ContractDataEntries{
AllDataEntries: dataEntries,
AllDataEntries: nil,
Height: blockInfo.Height,
},
}
if a.bUpdatesExtension.IsFirstRequestedBlock() {
dataEntries, err := a.ia.state.RetrieveEntries(proto.NewRecipientFromAddress(a.bUpdatesExtension.L2ContractAddress()))
if err != nil && !errors.Is(err, proto.ErrNotFound) {
return err
}
bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = dataEntries
a.bUpdatesExtension.FirstBlockDone()
a.bUpdatesExtension.WriteBUpdates(bUpdatesInfo)
return nil
}

// Write the L2 contract updates into the structure.
for _, txSnapshots := range blockSnapshot.TxSnapshots {
for _, snapshot := range txSnapshots {
if dataEntriesSnapshot, ok := snapshot.(*proto.DataEntriesSnapshot); ok {
if dataEntriesSnapshot.Address == a.bUpdatesExtension.L2ContractAddress() {
bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = append(bUpdatesInfo.ContractUpdatesInfo.AllDataEntries,
dataEntriesSnapshot.DataEntries...)
}
}
}
}

a.bUpdatesExtension.WriteBUpdates(bUpdatesInfo)
return nil
}
Expand Down

0 comments on commit 7960470

Please sign in to comment.