Skip to content

Commit

Permalink
Merge pull request #58 from ElrondNetwork/update-2020.12.11
Browse files Browse the repository at this point in the history
Update 2020.12.11
  • Loading branch information
iulianpascalau authored Dec 11, 2020
2 parents 79c9513 + 34b5330 commit 9d6b220
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
24 changes: 12 additions & 12 deletions process/block/postprocess/intermediateResults.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data.
"dump", spew.Sdump(addScr))
}

sndShId, dstShId, err := irp.getShardIdsFromAddresses(addScr.SndAddr, addScr.RcvAddr)
if err != nil {
return err
}
sndShId, dstShId := irp.getShardIdsFromAddresses(addScr.SndAddr, addScr.RcvAddr)

addScrShardInfo := &txShardInfo{receiverShardID: dstShId, senderShardID: sndShId}
scrInfo := &txInfo{tx: addScr, txShardInfo: addScrShardInfo}
Expand All @@ -248,6 +245,13 @@ func (irp *intermediateResultsProcessor) checkSmartContractResultIntegrity(scr *
if len(scr.SndAddr) == 0 {
return process.ErrNilSndAddr
}

sndShId, dstShId := irp.getShardIdsFromAddresses(scr.SndAddr, scr.RcvAddr)
isInShardSCR := dstShId == irp.shardCoordinator.SelfId()
if isInShardSCR {
return nil
}

if scr.Value == nil {
return process.ErrNilValue
}
Expand All @@ -258,18 +262,14 @@ func (irp *intermediateResultsProcessor) checkSmartContractResultIntegrity(scr *
return process.ErrNilTxHash
}

if !core.IsEmptyAddress(scr.SndAddr) && !core.IsEmptyAddress(scr.RcvAddr) {
sndShardID := irp.shardCoordinator.ComputeId(scr.SndAddr)
dstShardID := irp.shardCoordinator.ComputeId(scr.RcvAddr)
if sndShardID != irp.shardCoordinator.SelfId() && dstShardID != irp.shardCoordinator.SelfId() {
return process.ErrShardIdMissmatch
}
if sndShId != irp.shardCoordinator.SelfId() && dstShId != irp.shardCoordinator.SelfId() {
return process.ErrShardIdMissmatch
}

return nil
}

func (irp *intermediateResultsProcessor) getShardIdsFromAddresses(sndAddr []byte, rcvAddr []byte) (uint32, uint32, error) {
func (irp *intermediateResultsProcessor) getShardIdsFromAddresses(sndAddr []byte, rcvAddr []byte) (uint32, uint32) {
shardForSrc := irp.shardCoordinator.ComputeId(sndAddr)
shardForDst := irp.shardCoordinator.ComputeId(rcvAddr)

Expand All @@ -283,7 +283,7 @@ func (irp *intermediateResultsProcessor) getShardIdsFromAddresses(sndAddr []byte
shardForDst = irp.shardCoordinator.SelfId()
}

return shardForSrc, shardForDst, nil
return shardForSrc, shardForDst
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
42 changes: 40 additions & 2 deletions process/block/postprocess/intermediateResults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ func TestIntermediateResultsProcessor_getShardIdsFromAddressesGood(t *testing.T)
sndAddr := []byte("sndAddress")
dstAddr := []byte("dstAddress")

sndId, dstId, err := irp.getShardIdsFromAddresses(sndAddr, dstAddr)
sndId, dstId := irp.getShardIdsFromAddresses(sndAddr, dstAddr)
assert.Equal(t, uint32(0), sndId, dstId)
assert.Nil(t, err)
}

func TestIntermediateResultsProcessor_AddIntermediateTransactions(t *testing.T) {
Expand Down Expand Up @@ -194,6 +193,45 @@ func TestIntermediateResultsProcessor_AddIntermediateTransactionsWrongType(t *te
assert.Equal(t, process.ErrWrongTypeAssertion, err)
}

func TestIntermediateResultsProcessor_AddIntermediateTransactionsNegativeValueIntraAndCrossShard(t *testing.T) {
t.Parallel()

shardC := mock.NewMultiShardsCoordinatorMock(2)
irp, err := NewIntermediateResultsProcessor(
&mock.HasherMock{},
&mock.MarshalizerMock{},
shardC,
createMockPubkeyConverter(),
&mock.ChainStorerMock{},
block.SmartContractResultBlock,
&mock.TxForCurrentBlockStub{},
)

assert.NotNil(t, irp)
assert.Nil(t, err)

scr := &smartContractResult.SmartContractResult{RcvAddr: []byte("rcv"), SndAddr: []byte("snd"), Value: big.NewInt(-100), PrevTxHash: []byte("hash")}
txs := make([]data.TransactionHandler, 0)
txs = append(txs, scr)
txs = append(txs, scr)
txs = append(txs, scr)
txs = append(txs, scr)
txs = append(txs, scr)

shardC.ComputeIdCalled = func(address []byte) uint32 {
return shardC.SelfId()
}
err = irp.AddIntermediateTransactions(txs)
assert.Nil(t, err)

shardC.ComputeIdCalled = func(address []byte) uint32 {
return shardC.SelfId() + 1
}

err = irp.AddIntermediateTransactions(txs)
assert.Equal(t, err, process.ErrNegativeValue)
}

func TestIntermediateResultsProcessor_AddIntermediateTransactionsAddrGood(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 9d6b220

Please sign in to comment.