Esempio n. 1
0
func newChainRequest(creationPolicy, newChainID string) *cb.Envelope {
	oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
	oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx)
	oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data)

	return utils.ChainCreationConfigurationTransaction(provisional.AcceptAllPolicyKey, newChainID, oldConfigEnv)
}
Esempio n. 2
0
// This test brings up the entire system, with the mock consenter, including the broadcasters etc. and creates a new chain
func TestNewChain(t *testing.T) {
	conf := config.Load()
	lf, rl := ramledger.New(10, genesisBlock)

	consenters := make(map[string]Consenter)
	consenters[conf.General.OrdererType] = &mockConsenter{}

	manager := NewManagerImpl(lf, consenters)

	oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
	oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx)
	oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data)

	newChainID := "TestNewChain"
	newChainMessage := utils.ChainCreationConfigurationTransaction(provisional.AcceptAllPolicyKey, newChainID, oldConfigEnv)

	status := manager.ProposeChain(newChainMessage)

	if status != cb.Status_SUCCESS {
		t.Fatalf("Error submitting chain creation request")
	}

	it, _ := rl.Iterator(ab.SeekInfo_SPECIFIED, 1)
	select {
	case <-it.ReadyChan():
		block, status := it.Next()
		if status != cb.Status_SUCCESS {
			t.Fatalf("Could not retrieve block")
		}
		if len(block.Data.Data) != 1 {
			t.Fatalf("Should have had only one message in the orderer transaction block")
		}
		genesisConfigTx := utils.UnmarshalEnvelopeOrPanic(utils.UnmarshalPayloadOrPanic(utils.ExtractEnvelopeOrPanic(block, 0).Payload).Data)
		if !reflect.DeepEqual(genesisConfigTx, newChainMessage) {
			t.Errorf("Orderer config block contains wrong transaction, expected %v got %v", genesisConfigTx, newChainMessage)
		}
	case <-time.After(time.Second):
		t.Fatalf("Block 1 not produced after timeout in system chain")
	}

	chainSupport, ok := manager.GetChain(newChainID)

	if !ok {
		t.Fatalf("Should have gotten new chain which was created")
	}

	messages := make([]*cb.Envelope, conf.General.BatchSize)
	for i := 0; i < int(conf.General.BatchSize); i++ {
		messages[i] = makeNormalTx(newChainID, i)
	}

	for _, message := range messages {
		chainSupport.Enqueue(message)
	}

	it, _ = chainSupport.Reader().Iterator(ab.SeekInfo_SPECIFIED, 0)
	select {
	case <-it.ReadyChan():
		block, status := it.Next()
		if status != cb.Status_SUCCESS {
			t.Fatalf("Could not retrieve new chain genesis block")
		}
		if len(block.Data.Data) != 1 {
			t.Fatalf("Should have had only one message in the new genesis block")
		}
		genesisConfigTx := utils.ExtractEnvelopeOrPanic(block, 0)
		if !reflect.DeepEqual(genesisConfigTx, newChainMessage) {
			t.Errorf("Genesis block contains wrong transaction, expected %v got %v", genesisConfigTx, newChainMessage)
		}
	case <-time.After(time.Second):
		t.Fatalf("Block 1 not produced after timeout in system chain")
	}

	select {
	case <-it.ReadyChan():
		block, status := it.Next()
		if status != cb.Status_SUCCESS {
			t.Fatalf("Could not retrieve block on new chain")
		}
		for i := 0; i < int(conf.General.BatchSize); i++ {
			if !reflect.DeepEqual(utils.ExtractEnvelopeOrPanic(block, i), messages[i]) {
				t.Errorf("Block contents wrong at index %d in new chain", i)
			}
		}
	case <-time.After(time.Second):
		t.Fatalf("Block 1 not produced after timeout on new chain")
	}
}