Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
83 changes: 45 additions & 38 deletions process/smartContract/hooks/blockChainHook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/multiversx/mx-chain-core-go/data/typeConverters"
"github.com/multiversx/mx-chain-core-go/hashing/keccak"
"github.com/multiversx/mx-chain-core-go/marshal"
logger "github.com/multiversx/mx-chain-logger-go"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-common-go/parsers"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
Expand All @@ -30,9 +34,6 @@ import (
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/storage/factory"
"github.com/multiversx/mx-chain-go/storage/storageunit"
logger "github.com/multiversx/mx-chain-logger-go"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-common-go/parsers"
)

var _ process.BlockChainHookHandler = (*BlockChainHookImpl)(nil)
Expand Down Expand Up @@ -338,16 +339,16 @@ func (bh *BlockChainHookImpl) processMaxReadsCounters() error {
func (bh *BlockChainHookImpl) GetBlockhash(nonce uint64) ([]byte, error) {
defer stopMeasure(startMeasure("GetBlockhash"))

hdr := bh.blockChain.GetCurrentBlockHeader()

if check.IfNil(hdr) {
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return nil, process.ErrNilBlockHeader
}
if nonce > hdr.GetNonce() {
if nonce > lastExecHdr.GetNonce() {
return nil, process.ErrInvalidNonceRequest
}
if nonce == hdr.GetNonce() {
return bh.blockChain.GetCurrentBlockHeaderHash(), nil
if nonce == lastExecHdr.GetNonce() {
_, lastExecHash, _ := bh.blockChain.GetLastExecutedBlockInfo()
return lastExecHash, nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L347 _, lastExecHash, _ := bh.blockChain.GetLastExecutedBlockInfo() can be moved inside this branch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
if bh.enableEpochsHandler.IsFlagEnabled(common.DoNotReturnOldBlockInBlockchainHookFlag) {
return nil, process.ErrInvalidNonceRequest
Expand All @@ -364,62 +365,73 @@ func (bh *BlockChainHookImpl) GetBlockhash(nonce uint64) ([]byte, error) {
return nil, err
}

if header.GetEpoch() != hdr.GetEpoch() {
if header.GetEpoch() != lastExecHdr.GetEpoch() {
return nil, process.ErrInvalidBlockRequestOldEpoch
}

return hash, nil
}

// LastNonce returns the nonce from the last committed block
// LastNonce returns the nonce from the last executed block
func (bh *BlockChainHookImpl) LastNonce() uint64 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetNonce()
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return 0
}
return 0

return lastExecHdr.GetNonce()
}

// LastRound returns the round from the last committed block
// LastRound returns the round from the last executed block
func (bh *BlockChainHookImpl) LastRound() uint64 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetRound()
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return 0
}
return 0

return lastExecHdr.GetRound()
}

// LastTimeStamp returns the timeStamp from the last committed block
// LastTimeStamp returns the timeStamp from the last executed block
func (bh *BlockChainHookImpl) LastTimeStamp() uint64 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetTimeStamp()
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return 0
}
return 0

return lastExecHdr.GetTimeStamp()
}

// LastTimeStampMs returns the timeStamp in milliseconds from the last committed block
// LastTimeStampMs returns the timeStamp in milliseconds from the last executed block
func (bh *BlockChainHookImpl) LastTimeStampMs() uint64 {
if check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return 0
}

_, timestampMs, _ := common.GetHeaderTimestamps(bh.blockChain.GetCurrentBlockHeader(), bh.enableEpochsHandler)
_, timestampMs, _ := common.GetHeaderTimestamps(lastExecHdr, bh.enableEpochsHandler)

return timestampMs
}

// LastRandomSeed returns the random seed from the last committed block
// LastRandomSeed returns the random seed from the last executed block
func (bh *BlockChainHookImpl) LastRandomSeed() []byte {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetRandSeed()
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return make([]byte, 0)
}
return make([]byte, 0)

return lastExecHdr.GetRandSeed()
}

// LastEpoch returns the epoch from the last committed block
// LastEpoch returns the epoch from the last executed block
func (bh *BlockChainHookImpl) LastEpoch() uint32 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetEpoch()
lastExecHdr := bh.blockChain.GetLastExecutedBlockHeader()
if check.IfNil(lastExecHdr) {
return 0
}
return 0

return lastExecHdr.GetEpoch()
}

// RoundTime returns the duration of a round
Expand Down Expand Up @@ -466,11 +478,6 @@ func (bh *BlockChainHookImpl) GetStateRootHash() []byte {
}

func (bh *BlockChainHookImpl) getCurrentRootHash() []byte {
currentHeader := bh.blockChain.GetCurrentBlockHeader()
if currentHeader != nil && !currentHeader.IsHeaderV3() {
return bh.blockChain.GetCurrentBlockRootHash()
}

_, _, lastExecutedRootHash := bh.blockChain.GetLastExecutedBlockInfo()
return lastExecutedRootHash
}
Expand Down
92 changes: 53 additions & 39 deletions process/smartContract/hooks/blockChainHook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func TestBlockChainHookImpl_GetBlockhashNilBlockHeaderExpectError(t *testing.T)

args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return nil
},
}
Expand All @@ -741,10 +741,14 @@ func TestBlockChainHookImpl_GetBlockhashNilBlockHeaderExpectError(t *testing.T)
func TestBlockChainHookImpl_GetBlockhashInvalidNonceExpectError(t *testing.T) {
t.Parallel()

lastExecHdr := &block.Header{Nonce: 1}
args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return &block.Header{Nonce: 1}
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecHdr
},
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return 1, []byte("hash"), []byte("rootHash")
},
}

Expand All @@ -757,15 +761,15 @@ func TestBlockChainHookImpl_GetBlockhashInvalidNonceExpectError(t *testing.T) {
func TestBlockChainHookImpl_GetBlockhashShouldReturnCurrentBlockHeaderHash(t *testing.T) {
t.Parallel()

hdrToRet := &block.Header{Nonce: 2}
lastExecHdr := &block.Header{Nonce: 2}
hashToRet := []byte("hash")
args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return hdrToRet
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecHdr
},
GetCurrentBlockHeaderHashCalled: func() []byte {
return hashToRet
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return 2, hashToRet, []byte("rootHash")
},
}
bh, _ := hooks.NewBlockChainHookImpl(args)
Expand All @@ -778,10 +782,14 @@ func TestBlockChainHookImpl_GetBlockhashShouldReturnCurrentBlockHeaderHash(t *te
func TestBlockChainHookImpl_GetBlockhashFromStorerErrorReadingFromStorage(t *testing.T) {
t.Parallel()

lastExecHdr := &block.Header{Nonce: 10}
args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return &block.Header{Nonce: 10}
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecHdr
},
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return 10, []byte("hash"), []byte("rootHash")
},
}
storer := &storageStubs.StorerStub{
Expand Down Expand Up @@ -813,9 +821,12 @@ func TestBlockChainHookImpl_GetBlockhashFromStorerInSameEpoch(t *testing.T) {
marshalledHeader, _ := args.Marshalizer.Marshal(header)

args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return header
},
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return nonce, []byte("execHash"), []byte("rootHash")
},
}

storerBlockHeader := &storageStubs.StorerStub{
Expand Down Expand Up @@ -860,9 +871,12 @@ func TestBlockChainHookImpl_GetBlockhashFromStorerInSameEpochWithFlagEnabled(t *
shardID := args.ShardCoordinator.SelfId()

args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return header
},
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return nonce, []byte("hash"), []byte("rootHash")
},
}

storerBlockHeader := &storageStubs.StorerStub{
Expand Down Expand Up @@ -902,13 +916,17 @@ func TestBlockChainHookImpl_GetBlockhashFromOldEpochExpectError(t *testing.T) {

hdrToRet := &block.Header{Nonce: 2, Epoch: 2}
hashToRet := []byte("hash")
lastExecHdr := &block.Header{Nonce: 10, Epoch: 10}
args := createMockBlockChainHookArgs()

marshaledData, _ := args.Marshalizer.Marshal(hdrToRet)

args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return &block.Header{Nonce: 10, Epoch: 10}
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecHdr
},
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return 10, []byte("execHash"), []byte("rootHash")
},
}
args.StorageService = &storageStubs.ChainStorerStub{
Expand Down Expand Up @@ -942,7 +960,7 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {

args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return nil
},
}
Expand All @@ -965,7 +983,7 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {
randSeed := []byte("a")
rootHash := []byte("b")
epoch := uint32(7)
hdrToRet := &block.Header{
lastExecutedHdr := &block.Header{
Nonce: nonce,
Round: round,
TimeStamp: timestamp,
Expand All @@ -976,11 +994,11 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {

args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return hdrToRet
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecutedHdr
},
GetCurrentBlockRootHashCalled: func() []byte {
return hdrToRet.RootHash
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return nonce, []byte("hash"), rootHash
},
}
bh, _ := hooks.NewBlockChainHookImpl(args)
Expand All @@ -1002,7 +1020,7 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {
randSeed := []byte("a")
rootHash := []byte("b")
epoch := uint32(7)
hdrToRet := &block.Header{
lastExecutedHdr := &block.Header{
Nonce: nonce,
Round: round,
TimeStamp: timestamp,
Expand All @@ -1014,11 +1032,11 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {
args := createMockBlockChainHookArgs()
args.EnableEpochsHandler = enableEpochsHandlerMock.NewEnableEpochsHandlerStub(common.DoNotReturnOldBlockInBlockchainHookFlag)
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return hdrToRet
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecutedHdr
},
GetCurrentBlockRootHashCalled: func() []byte {
return hdrToRet.RootHash
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return nonce, []byte("hash"), rootHash
},
}
bh, _ := hooks.NewBlockChainHookImpl(args)
Expand All @@ -1038,11 +1056,10 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {
round := uint64(5)
timestamp := uint64(1234)
randSeed := []byte("a")
rootHash := []byte("b")
rootHash1 := []byte("c")
epoch := uint32(7)

hdrToRet := &block.HeaderV3{
lastExecutedHdr := &block.HeaderV3{
Nonce: nonce,
Round: round,
TimestampMs: timestamp,
Expand All @@ -1052,14 +1069,11 @@ func TestBlockChainHookImpl_GettersFromBlockchainCurrentHeader(t *testing.T) {

args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return hdrToRet
},
GetCurrentBlockRootHashCalled: func() []byte {
return rootHash
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecutedHdr
},
GetLastExecutedBlockInfoCalled: func() (uint64, []byte, []byte) {
return 0, []byte(""), rootHash1
return nonce, []byte("hash"), rootHash1
},
}
bh, _ := hooks.NewBlockChainHookImpl(args)
Expand Down Expand Up @@ -2775,14 +2789,14 @@ func TestBlockChainHookImpl_LastTimeStampMs(t *testing.T) {

timestamp := uint64(1234)

hdr := &block.Header{
lastExecHdr := &block.Header{
TimeStamp: timestamp,
}

args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return hdr
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecHdr
},
}
args.EnableEpochsHandler = &enableEpochsHandlerMock.EnableEpochsHandlerStub{
Expand All @@ -2803,14 +2817,14 @@ func TestBlockChainHookImpl_LastTimeStampMs(t *testing.T) {
t.Parallel()

timestampMs := uint64(1234567)
hdr := &block.Header{
lastExecHdr := &block.Header{
TimeStamp: timestampMs,
}

args := createMockBlockChainHookArgs()
args.BlockChain = &testscommon.ChainHandlerStub{
GetCurrentBlockHeaderCalled: func() data.HeaderHandler {
return hdr
GetLastExecutedBlockHeaderCalled: func() data.HeaderHandler {
return lastExecHdr
},
}
args.EnableEpochsHandler = &enableEpochsHandlerMock.EnableEpochsHandlerStub{
Expand Down
Loading
Loading