github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/golang/protobuf/proto"
    24  	mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
    25  	"github.com/hyperledger/fabric/orderer/common/filter"
    26  	ordererledger "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  )
    31  
    32  type mockLedgerReadWriter struct {
    33  	data     [][]byte
    34  	metadata [][]byte
    35  	height   uint64
    36  }
    37  
    38  func (mlw *mockLedgerReadWriter) Append(block *cb.Block) error {
    39  	mlw.data = block.Data.Data
    40  	mlw.metadata = block.Metadata.Metadata
    41  	mlw.height++
    42  	return nil
    43  }
    44  
    45  func (mlw *mockLedgerReadWriter) Iterator(startType *ab.SeekPosition) (ordererledger.Iterator, uint64) {
    46  	panic("Unimplemented")
    47  }
    48  
    49  func (mlw *mockLedgerReadWriter) Height() uint64 {
    50  	return mlw.height
    51  }
    52  
    53  type mockCommitter struct {
    54  	committed int
    55  }
    56  
    57  func (mc *mockCommitter) Isolated() bool {
    58  	panic("Unimplemented")
    59  }
    60  
    61  func (mc *mockCommitter) Commit() {
    62  	mc.committed++
    63  }
    64  
    65  func TestCommitConfig(t *testing.T) {
    66  	ml := &mockLedgerReadWriter{}
    67  	cm := &mockconfigtx.Manager{}
    68  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
    69  	txs := []*cb.Envelope{makeNormalTx("foo", 0), makeNormalTx("bar", 1)}
    70  	committers := []filter.Committer{&mockCommitter{}, &mockCommitter{}}
    71  	block := cs.CreateNextBlock(txs)
    72  	cs.WriteBlock(block, committers, nil)
    73  
    74  	blockTXs := make([]*cb.Envelope, len(ml.data))
    75  	for i := range ml.data {
    76  		blockTXs[i] = utils.UnmarshalEnvelopeOrPanic(ml.data[i])
    77  	}
    78  
    79  	if !reflect.DeepEqual(blockTXs, txs) {
    80  		t.Errorf("Should have written input data to ledger but did not")
    81  	}
    82  
    83  	for _, c := range committers {
    84  		if c.(*mockCommitter).committed != 1 {
    85  			t.Errorf("Expected exactly 1 commits but got %d", c.(*mockCommitter).committed)
    86  		}
    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  	if utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, nil), cb.BlockMetadataIndex_SIGNATURES) == nil {
    96  		t.Fatalf("Block should have block signature")
    97  	}
    98  }
    99  
   100  func TestWriteBlockOrdererMetadata(t *testing.T) {
   101  	ml := &mockLedgerReadWriter{}
   102  	cm := &mockconfigtx.Manager{}
   103  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   104  
   105  	value := []byte("foo")
   106  	expected := &cb.Metadata{Value: value}
   107  	actual := utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, value), cb.BlockMetadataIndex_ORDERER)
   108  
   109  	if actual == nil {
   110  		t.Fatalf("Block should have orderer metadata written")
   111  	}
   112  	if !proto.Equal(expected, actual) {
   113  		t.Fatalf("Orderer metadata not written to block correctly")
   114  	}
   115  
   116  }
   117  
   118  func TestWriteLastConfig(t *testing.T) {
   119  	ml := &mockLedgerReadWriter{}
   120  	cm := &mockconfigtx.Manager{}
   121  	cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
   122  
   123  	expected := uint64(0)
   124  
   125  	if lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, nil)); lc != expected {
   126  		t.Fatalf("First block should have config block index of %d, but got %d", expected, lc)
   127  	}
   128  	if lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(1, nil), nil, nil)); lc != expected {
   129  		t.Fatalf("Second block should have config block index of %d, but got %d", expected, lc)
   130  	}
   131  
   132  	cm.SequenceVal = 1
   133  	expected = uint64(2)
   134  
   135  	if lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(2, nil), nil, nil)); lc != expected {
   136  		t.Fatalf("Second block should have config block index of %d, but got %d", expected, lc)
   137  	}
   138  
   139  	if lc := utils.GetLastConfigIndexFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(3, nil), nil, nil)); lc != expected {
   140  		t.Fatalf("Second block should have config block index of %d, but got %d", expected, lc)
   141  	}
   142  
   143  }