• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision1eff3e9ba2c69ecef16a1a31af212256e597db4c (tree)
Time2019-06-28 13:16:43
Authoroys <oys@oysd...>
Commiteroys

Log Message

fix array out of index range

Change Summary

Incremental Difference

--- a/protocol/block.go
+++ b/protocol/block.go
@@ -205,7 +205,7 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
205205 log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("attach from mainchain")
206206 }
207207
208- if detachBlockHeaders[len(detachBlockHeaders)-1].Height <= c.bestIrrBlockHeader.Height && irrBlockHeader.Height <= c.bestIrrBlockHeader.Height {
208+ if len(detachBlockHeaders) > 0 && detachBlockHeaders[len(detachBlockHeaders)-1].Height <= c.bestIrrBlockHeader.Height && irrBlockHeader.Height <= c.bestIrrBlockHeader.Height {
209209 return errors.New("rollback block below the height of irreversible block")
210210 }
211211 voteResults = append(voteResults, voteResult.Fork())
--- a/protocol/block_test.go
+++ b/protocol/block_test.go
@@ -71,12 +71,26 @@ func TestCalcReorganizeChain(t *testing.T) {
7171 c.store.SaveBlockHeader(newChainBlockHeader)
7272 }
7373
74+ // normal
7475 getAttachBlockHeaders, getDetachBlockHeaders, _ := c.calcReorganizeChain(newChainBlockHeader, mainChainBlockHeader)
7576 if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) {
76- t.Errorf("attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
77+ t.Errorf("normal test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
7778 }
7879
7980 if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
80- t.Errorf("detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
81+ t.Errorf("normal test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
82+ }
83+
84+ // detachBlockHeaders is empty
85+ forkChainBlockHeader := wantAttachBlockHeaders[7]
86+ wantAttachBlockHeaders = wantAttachBlockHeaders[8:]
87+ wantDetachBlockHeaders = []*types.BlockHeader{}
88+ getAttachBlockHeaders, getDetachBlockHeaders, _ = c.calcReorganizeChain(newChainBlockHeader, forkChainBlockHeader)
89+ if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) {
90+ t.Errorf("detachBlockHeaders is empty test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
91+ }
92+
93+ if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
94+ t.Errorf("detachBlockHeaders is empty test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
8195 }
8296 }
Show on old repository browser