Beispiel #1
0
func (i *Noops) getBlockData() (*pb.Block, *statemgmt.StateDelta, error) {
	ledger, err := ledger.GetLedger()
	if err != nil {
		return nil, nil, fmt.Errorf("Fail to get the ledger: %v", err)
	}

	blockHeight := ledger.GetBlockchainSize()
	if logger.IsEnabledFor(logging.DEBUG) {
		logger.Debugf("Preparing to broadcast with block number %v", blockHeight)
	}
	block, err := ledger.GetBlockByNumber(blockHeight - 1)
	if nil != err {
		return nil, nil, err
	}
	//delta, err := ledger.GetStateDeltaBytes(blockHeight)
	delta, err := ledger.GetStateDelta(blockHeight - 1)
	if nil != err {
		return nil, nil, err
	}
	if logger.IsEnabledFor(logging.DEBUG) {
		logger.Debugf("Got the delta state of block number %v", blockHeight)
	}

	return block, delta, nil
}
Beispiel #2
0
// GetBlock returns a block from the chain
func (h *Helper) GetBlock(blockNumber uint64) (block *pb.Block, err error) {
	ledger, err := ledger.GetLedger()
	if err != nil {
		return nil, fmt.Errorf("Failed to get the ledger :%v", err)
	}
	return ledger.GetBlockByNumber(blockNumber)
}
Beispiel #3
0
// CommitTxBatch gets invoked when the current transaction-batch needs
// to be committed. This function returns successfully iff the
// transactions details and state changes (that may have happened
// during execution of this transaction-batch) have been committed to
// permanent storage.
func (h *Helper) CommitTxBatch(id interface{}, metadata []byte) (*pb.Block, error) {
	ledger, err := ledger.GetLedger()
	if err != nil {
		return nil, fmt.Errorf("Failed to get the ledger: %v", err)
	}
	// TODO fix this one the ledger has been fixed to implement
	if err := ledger.CommitTxBatch(id, h.curBatch, h.curBatchErrs, metadata); err != nil {
		return nil, fmt.Errorf("Failed to commit transaction to the ledger: %v", err)
	}

	size := ledger.GetBlockchainSize()
	defer func() {
		h.curBatch = nil     // TODO, remove after issue 579
		h.curBatchErrs = nil // TODO, remove after issue 579
	}()

	block, err := ledger.GetBlockByNumber(size - 1)
	if err != nil {
		return nil, fmt.Errorf("Failed to get the block at the head of the chain: %v", err)
	}

	logger.Debugf("Committed block with %d transactions, intended to include %d", len(block.Transactions), len(h.curBatch))

	return block, nil
}
Beispiel #4
0
// GetBlockHeadMetadata returns metadata from block at the head of the blockchain
func (h *Helper) GetBlockHeadMetadata() ([]byte, error) {
	ledger, err := ledger.GetLedger()
	if err != nil {
		return nil, err
	}
	head := ledger.GetBlockchainSize()
	block, err := ledger.GetBlockByNumber(head - 1)
	if err != nil {
		return nil, err
	}
	return block.ConsensusMetadata, nil
}
Beispiel #5
0
func TestServerOpenchainREST_API_GetTransactionByUUID(t *testing.T) {
	startTime := time.Now().Unix()

	// Construct a ledger with 3 blocks.
	ledger := ledger.InitTestLedger(t)
	buildTestLedger1(ledger, t)

	initGlobalServerOpenchain(t)

	// Start the HTTP REST test server
	httpServer := httptest.NewServer(buildOpenchainRESTRouter())
	defer httpServer.Close()

	body := performHTTPGet(t, httpServer.URL+"/transactions/NON-EXISTING-UUID")
	res := parseRESTResult(t, body)
	if res.Error == "" {
		t.Errorf("Expected an error when retrieving non-existing transaction, but got none")
	}

	block1, err := ledger.GetBlockByNumber(1)
	if err != nil {
		t.Fatalf("Can't fetch first block from ledger: %v", err)
	}
	firstTx := block1.Transactions[0]

	body1 := performHTTPGet(t, httpServer.URL+"/transactions/"+firstTx.Txid)
	var tx1 protos.Transaction
	err = json.Unmarshal(body1, &tx1)
	if err != nil {
		t.Fatalf("Invalid JSON response: %v", err)
	}
	if tx1.Txid != firstTx.Txid {
		t.Errorf("Expected transaction uuid to be '%v' but got '%v'", firstTx.Txid, tx1.Txid)
	}
	if tx1.Timestamp.Seconds < startTime {
		t.Errorf("Expected transaction timestamp (%v) to be after the start time (%v)", tx1.Timestamp.Seconds, startTime)
	}

	badBody := performHTTPGet(t, httpServer.URL+"/transactions/with-\"-chars-in-the-URL")
	badRes := parseRESTResult(t, badBody)
	if badRes.Error == "" {
		t.Errorf("Expected a proper error when retrieving transaction with bad UUID")
	}
}
Beispiel #6
0
// CommitTxBatch gets invoked when the current transaction-batch needs
// to be committed. This function returns successfully iff the
// transactions details and state changes (that may have happened
// during execution of this transaction-batch) have been committed to
// permanent storage.
func (h *Helper) CommitTxBatch(id interface{}, metadata []byte) (*pb.Block, error) {
	ledger, err := ledger.GetLedger()
	if err != nil {
		return nil, fmt.Errorf("Failed to get the ledger: %v", err)
	}
	// TODO fix this one the ledger has been fixed to implement
	if err := ledger.CommitTxBatch(id, h.curBatch, nil, metadata); err != nil {
		return nil, fmt.Errorf("Failed to commit transaction to the ledger: %v", err)
	}

	size := ledger.GetBlockchainSize()
	h.curBatch = nil // TODO, remove after issue 579

	block, err := ledger.GetBlockByNumber(size - 1)
	if err != nil {
		return nil, fmt.Errorf("Failed to get the block at the head of the chain: %v", err)
	}

	return block, nil
}