github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/multichannel/chainsupport_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package multichannel
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
    24  	"github.com/hyperledger/fabric/common/mocks/crypto"
    25  	"github.com/hyperledger/fabric/orderer/common/filter"
    26  	"github.com/hyperledger/fabric/orderer/common/ledger"
    27  	cb "github.com/hyperledger/fabric/protos/common"
    28  	ab "github.com/hyperledger/fabric/protos/orderer"
    29  	"github.com/hyperledger/fabric/protos/utils"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  type mockLedgerReadWriter struct {
    34  	data     [][]byte
    35  	metadata [][]byte
    36  	height   uint64
    37  }
    38  
    39  func (mlw *mockLedgerReadWriter) Append(block *cb.Block) error {
    40  	mlw.data = block.Data.Data
    41  	mlw.metadata = block.Metadata.Metadata
    42  	mlw.height++
    43  	return nil
    44  }
    45  
    46  func (mlw *mockLedgerReadWriter) Iterator(startType *ab.SeekPosition) (ledger.Iterator, uint64) {
    47  	panic("Unimplemented")
    48  }
    49  
    50  func (mlw *mockLedgerReadWriter) Height() uint64 {
    51  	return mlw.height
    52  }
    53  
    54  type mockCommitter struct {
    55  	committed int
    56  }
    57  
    58  func (mc *mockCommitter) Isolated() bool {
    59  	panic("Unimplemented")
    60  }
    61  
    62  func (mc *mockCommitter) Commit() {
    63  	mc.committed++
    64  }
    65  
    66  func TestCommitConfig(t *testing.T) {
    67  	ml := &mockLedgerReadWriter{}
    68  	cm := &mockconfigtx.Manager{}
    69  	cs := &chainSupport{
    70  		ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml},
    71  		filters:         filter.NewRuleSet([]filter.Rule{filter.AcceptRule}),
    72  		signer:          mockCrypto(),
    73  	}
    74  	assert.Equal(t, uint64(0), cs.Height(), "Should has height of 0")
    75  
    76  	txs := []*cb.Envelope{makeNormalTx("foo", 0), makeNormalTx("bar", 1)}
    77  	block := cs.CreateNextBlock(txs)
    78  	cs.WriteConfigBlock(block, nil)
    79  	assert.Equal(t, uint64(1), cs.Height(), "Should has height of 1")
    80  
    81  	blockTXs := make([]*cb.Envelope, len(ml.data))
    82  	for i := range ml.data {
    83  		blockTXs[i] = utils.UnmarshalEnvelopeOrPanic(ml.data[i])
    84  	}
    85  
    86  	assert.Equal(t, txs, blockTXs, "Should have written input data to ledger but did not")
    87  }
    88  
    89  func TestWriteBlockSignatures(t *testing.T) {
    90  	ml := &mockLedgerReadWriter{}
    91  	cm := &mockconfigtx.Manager{}
    92  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
    93  
    94  	actual := utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil), cb.BlockMetadataIndex_SIGNATURES)
    95  	assert.NotNil(t, actual, "Block should have block signature")
    96  }
    97  
    98  func TestWriteBlockOrdererMetadata(t *testing.T) {
    99  	ml := &mockLedgerReadWriter{}
   100  	cm := &mockconfigtx.Manager{}
   101  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   102  
   103  	value := []byte("foo")
   104  	expected := &cb.Metadata{Value: value}
   105  	actual := utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), value), cb.BlockMetadataIndex_ORDERER)
   106  	assert.NotNil(t, actual, "Block should have orderer metadata written")
   107  	assert.True(t, proto.Equal(expected, actual), "Orderer metadata not written to block correctly")
   108  }
   109  
   110  func TestSignature(t *testing.T) {
   111  	ml := &mockLedgerReadWriter{}
   112  	cm := &mockconfigtx.Manager{}
   113  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   114  
   115  	message := []byte("Darth Vader")
   116  	signed, _ := cs.Sign(message)
   117  	assert.Equal(t, message, signed, "Should sign the message")
   118  
   119  	signatureHeader, _ := cs.NewSignatureHeader()
   120  	assert.Equal(t, crypto.FakeLocalSigner.Identity, signatureHeader.Creator)
   121  	assert.Equal(t, crypto.FakeLocalSigner.Nonce, signatureHeader.Nonce)
   122  }
   123  
   124  func TestWriteLastConfig(t *testing.T) {
   125  	ml := &mockLedgerReadWriter{}
   126  	cm := &mockconfigtx.Manager{}
   127  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   128  
   129  	expected := uint64(0)
   130  	lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil))
   131  	assert.Equal(t, expected, lc, "First block should have config block index of %d, but got %d", expected, lc)
   132  	lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(1, nil), nil))
   133  	assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   134  
   135  	cm.SequenceVal = 1
   136  	expected = uint64(2)
   137  	lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(2, nil), nil))
   138  	assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   139  
   140  	lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(3, nil), nil))
   141  	assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   142  
   143  	t.Run("ResetChainSupport", func(t *testing.T) {
   144  		cm.SequenceVal = 2
   145  		expected = uint64(4)
   146  
   147  		cs = &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   148  		lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(4, nil), nil))
   149  		assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   150  
   151  		lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(5, nil), nil))
   152  		assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d")
   153  	})
   154  }