github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/multichain/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 multichain
    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/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{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
    70  	assert.Equal(t, uint64(0), cs.Height(), "Should has height of 0")
    71  
    72  	txs := []*cb.Envelope{makeNormalTx("foo", 0), makeNormalTx("bar", 1)}
    73  	committers := []filter.Committer{&mockCommitter{}, &mockCommitter{}}
    74  	block := cs.CreateNextBlock(txs)
    75  	cs.WriteBlock(block, committers, nil)
    76  	assert.Equal(t, uint64(1), cs.Height(), "Should has height of 1")
    77  
    78  	blockTXs := make([]*cb.Envelope, len(ml.data))
    79  	for i := range ml.data {
    80  		blockTXs[i] = utils.UnmarshalEnvelopeOrPanic(ml.data[i])
    81  	}
    82  
    83  	assert.Equal(t, txs, blockTXs, "Should have written input data to ledger but did not")
    84  
    85  	for _, c := range committers {
    86  		assert.EqualValues(t, 1, c.(*mockCommitter).committed, "Expected exactly 1 commits but got %d", c.(*mockCommitter).committed)
    87  	}
    88  }
    89  
    90  func TestWriteBlockSignatures(t *testing.T) {
    91  	ml := &mockLedgerReadWriter{}
    92  	cm := &mockconfigtx.Manager{}
    93  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
    94  
    95  	actual := utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, nil), cb.BlockMetadataIndex_SIGNATURES)
    96  	assert.NotNil(t, actual, "Block should have block signature")
    97  }
    98  
    99  func TestWriteBlockOrdererMetadata(t *testing.T) {
   100  	ml := &mockLedgerReadWriter{}
   101  	cm := &mockconfigtx.Manager{}
   102  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   103  
   104  	value := []byte("foo")
   105  	expected := &cb.Metadata{Value: value}
   106  	actual := utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, value), cb.BlockMetadataIndex_ORDERER)
   107  	assert.NotNil(t, actual, "Block should have orderer metadata written")
   108  	assert.True(t, proto.Equal(expected, actual), "Orderer metadata not written to block correctly")
   109  }
   110  
   111  func TestSignature(t *testing.T) {
   112  	ml := &mockLedgerReadWriter{}
   113  	cm := &mockconfigtx.Manager{}
   114  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   115  
   116  	message := []byte("Darth Vader")
   117  	signed, _ := cs.Sign(message)
   118  	assert.Equal(t, message, signed, "Should sign the message")
   119  
   120  	signatureHeader, _ := cs.NewSignatureHeader()
   121  	assert.Equal(t, crypto.FakeLocalSigner.Identity, signatureHeader.Creator)
   122  	assert.Equal(t, crypto.FakeLocalSigner.Nonce, signatureHeader.Nonce)
   123  }
   124  
   125  func TestWriteLastConfig(t *testing.T) {
   126  	ml := &mockLedgerReadWriter{}
   127  	cm := &mockconfigtx.Manager{}
   128  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   129  
   130  	expected := uint64(0)
   131  	lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, nil))
   132  	assert.Equal(t, expected, lc, "First block should have config block index of %d, but got %d", expected, lc)
   133  	lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(1, nil), nil, nil))
   134  	assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   135  
   136  	cm.SequenceVal = 1
   137  	expected = uint64(2)
   138  	lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(2, nil), nil, nil))
   139  	assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   140  
   141  	lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(3, nil), nil, nil))
   142  	assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   143  
   144  	t.Run("ResetChainSupport", func(t *testing.T) {
   145  		cm.SequenceVal = 2
   146  		expected = uint64(4)
   147  
   148  		cs = &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   149  		lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(4, nil), nil, nil))
   150  		assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d", expected, lc)
   151  
   152  		lc = utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(5, nil), nil, nil))
   153  		assert.Equal(t, expected, lc, "Second block should have config block index of %d, but got %d")
   154  	})
   155  }