Skip to content

Commit

Permalink
deep copy header when returning from trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
laurci committed Jul 29, 2024
1 parent f94bc3c commit 8d9114d
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 42 deletions.
6 changes: 3 additions & 3 deletions epochStart/bootstrap/disabled/disabledEpochStartTrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func (e *epochStartTrigger) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *epochStartTrigger) EpochStartHdr() data.HeaderHandler {
return nil
// LastCommitedEpochStartHdr -
func (e *epochStartTrigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
return nil, nil
}

// GetSavedStateKey -
Expand Down
2 changes: 1 addition & 1 deletion epochStart/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type TriggerHandler interface {
Update(round uint64, nonce uint64)
EpochStartRound() uint64
EpochStartMetaHdrHash() []byte
EpochStartHdr() data.HeaderHandler
LastCommitedEpochStartHdr() (data.HeaderHandler, error)
GetSavedStateKey() []byte
LoadState(key []byte) error
SetProcessed(header data.HeaderHandler, body data.BodyHandler)
Expand Down
22 changes: 18 additions & 4 deletions epochStart/metachain/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-logger-go"
logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("epochStart/metachain")
Expand Down Expand Up @@ -403,9 +403,23 @@ func (t *trigger) EpochStartMetaHdrHash() []byte {
return t.epochStartMetaHash
}

// EpochStartHdr returns the header of the epoch start block
func (t *trigger) EpochStartHdr() data.HeaderHandler {
return t.epochStartMeta
// LastCommitedEpochStartHdr returns the header of the epoch start block
func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()

// marshal + unmarshal deep copy
headerBytes, err := t.marshaller.Marshal(t.epochStartMeta)
if err != nil {
return nil, err
}

header, err := process.UnmarshalMetaHeader(t.marshaller, headerBytes)
if err != nil {
return nil, err
}

return header, nil
}

// GetSavedStateKey returns the last saved trigger state key
Expand Down
19 changes: 15 additions & 4 deletions epochStart/shardchain/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-logger-go"
logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("epochStart/shardchain")
Expand Down Expand Up @@ -1102,12 +1102,23 @@ func (t *trigger) EpochStartMetaHdrHash() []byte {
return t.epochMetaBlockHash
}

// EpochStartHdr returns the epoch start header
func (t *trigger) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr returns the epoch start header
func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()

return t.epochStartShardHeader
// marshal + unmarshal deep copy
headerBytes, err := t.marshaller.Marshal(t.epochStartShardHeader)
if err != nil {
return nil, err
}

header, err := process.UnmarshalShardHeader(t.marshaller, headerBytes)
if err != nil {
return nil, err
}

return header, nil
}

// GetSavedStateKey returns the last saved trigger state key
Expand Down
8 changes: 4 additions & 4 deletions factory/mock/epochStartTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}

return nil
return nil, nil
}

// GetSavedStateKey -
Expand Down
2 changes: 1 addition & 1 deletion factory/processing/blockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta(
Counter: counters.NewDisabledCounter(),
MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(),
EpochStartTrigger: epochStartTriggerHandler,
RoundHandler: pcf.coreData.RoundHandler(),
RoundHandler: pcf.coreData.RoundHandler(), // TODO: @laurci - this needs to be replaced when changing the round duration
}

blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook)
Expand Down
8 changes: 4 additions & 4 deletions integrationTests/mock/endOfEpochTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}
return nil
return nil, nil
}

// GetSavedStateKey -
Expand Down
8 changes: 4 additions & 4 deletions node/mock/endOfEpochTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}

return nil
return nil, nil
}

// Revert -
Expand Down
2 changes: 1 addition & 1 deletion process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ type EpochStartTriggerHandler interface {
Epoch() uint32
MetaEpoch() uint32
EpochStartRound() uint64
EpochStartHdr() data.HeaderHandler
LastCommitedEpochStartHdr() (data.HeaderHandler, error)
SetProcessed(header data.HeaderHandler, body data.BodyHandler)
RevertStateToBlock(header data.HeaderHandler) error
EpochStartMetaHdrHash() []byte
Expand Down
8 changes: 4 additions & 4 deletions process/mock/endOfEpochTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func (e *EpochStartTriggerStub) EpochStartRound() uint64 {
return 0
}

// ReceivedHeader -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}
return nil
return nil, nil
}

// Update -
Expand Down
30 changes: 23 additions & 7 deletions process/smartContract/hooks/blockChainHook.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type ArgBlockChainHook struct {
Counter BlockChainHookCounter
MissingTrieNodesNotifier common.MissingTrieNodesNotifier
EpochStartTrigger EpochStartTriggerHandler
RoundHandler RoundHandler
RoundHandler RoundHandler // TODO: @laurci - this needs to be replaced when changing the round duration
}

// BlockChainHookImpl is a wrapper over AccountsAdapter that satisfy vmcommon.BlockchainHook interface
Expand All @@ -84,7 +84,7 @@ type BlockChainHookImpl struct {
enableEpochsHandler common.EnableEpochsHandler
counter BlockChainHookCounter
epochStartTrigger EpochStartTriggerHandler
roundHandler RoundHandler
roundHandler RoundHandler // TODO: @laurci - this needs to be replaced when changing the round duration

mutCurrentHdr sync.RWMutex
currentHdr data.HeaderHandler
Expand Down Expand Up @@ -411,6 +411,7 @@ func (bh *BlockChainHookImpl) LastEpoch() uint32 {

// RoundTime returns the duration of a round
func (bh *BlockChainHookImpl) RoundTime() uint64 {
// TODO: @laurci - this needs to be replaced when changing the round duration
roundDuration := bh.roundHandler.TimeDuration()

return uint64(roundDuration.Milliseconds())
Expand Down Expand Up @@ -801,24 +802,39 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) {

bh.mutCurrentHdr.Lock()
bh.currentHdr = hdr
bh.updateEpochStartHeader(hdr)
err := bh.updateEpochStartHeader(hdr)
if err != nil {
log.Debug("BlockChainHookImpl.SetCurrentHeader: updateEpochStartHeader", "error", err)
}

bh.mutCurrentHdr.Unlock()
}

func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) {
func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) error {
bh.mutEpochStartHdr.Lock()
defer bh.mutEpochStartHdr.Unlock()

if hdr.IsStartOfEpochBlock() {
bh.epochStartHdr = hdr
return
return nil
}

if bh.epochStartHdr.GetEpoch() == hdr.GetEpoch() {
return
return nil
}

bh.epochStartHdr = bh.epochStartTrigger.EpochStartHdr()
epochStartHdr, err := bh.epochStartTrigger.LastCommitedEpochStartHdr()
if err != nil {
return err
}

if epochStartHdr.GetEpoch() != hdr.GetEpoch() {
return ErrLastCommitedEpochStartHdrMismatch
}

bh.epochStartHdr = epochStartHdr

return nil
}

// SaveCompiledCode saves the compiled code to cache and storage
Expand Down
3 changes: 3 additions & 0 deletions process/smartContract/hooks/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ var ErrNilEpochStartTriggerHandler = errors.New("nil epoch start trigger handler

// ErrNilRoundHandler signals that a nil round handler was provided
var ErrNilRoundHandler = errors.New("nil round handler")

// ErrLastCommitedEpochStartHdrMismatch signals that the current header epoch and last commited epoch start header epoch do not match
var ErrLastCommitedEpochStartHdrMismatch = errors.New("current header epoch and last commited epoch start header epoch do not match")
2 changes: 1 addition & 1 deletion process/smartContract/hooks/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type BlockChainHookCounter interface {

// EpochStartTriggerHandler defines the operations of an epoch start trigger handler needed by the blockchain hook
type EpochStartTriggerHandler interface {
EpochStartHdr() data.HeaderHandler
LastCommitedEpochStartHdr() (data.HeaderHandler, error)
IsInterfaceNil() bool
}

Expand Down
8 changes: 4 additions & 4 deletions testscommon/epochStartTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}
return nil
return nil, nil
}

// GetSavedStateKey -
Expand Down

0 comments on commit 8d9114d

Please sign in to comment.