Blockchain Essentials

Introduction and Overview of Blockchain as a Technology

Updated: 03 September 2023

Based on this Cognitive Class Course

Discover Blockchain

The Business Backdrop

Businesses are always operating with external organizations and markets. These business networks are fundamental to the operation of blockchain in this environment

These networks consist of transferring of different types of assets

  • Tangible
  • Intangible
  • Cash

Ledgers are the key to recording the flow of assets through an organisational network, and these flows are governed by contracts which can be simple or complex

At a very high level Blockchain is a distributed ledger with a shared set of business processes across the network

The Problem Area

Every member of the business network has their own copy of the ledger, and this is updated each time an asset flows through the network, this system is inefficient, expensive, and vulnerable to mistakes or even fraud

By utilising Blockchain, each member utilises a shared ledger, but we just specify which users are able to see which specific transactions are relevant to them

When we use this we end up with

  • Consensus
  • Provenance
  • Immutability
  • Finality

Based on this we have a single source of truth for all parties and transactions within the network

Requirements in a Business Environment

Blockchain in a business environment requires four main components

  • Shared Ledger
  • Smart Contract
  • Privacy
  • Trust

Shared Ledger

Each participant has their own copy which is shared between them, this is based on permission and control. This becomes the shared system within the network

Smart contracts

Encoded version of business contracts. These are verifiable, and signed. Once these are distributed the contract will execute one the conditions are met.

Privacy

Participants need confidentiality within the blockchain, as well as a system in which transactions must be authenticated and immutable

Trust

Selected members endorse or validate transactions, once these are endorsed they are added to the blockchain. This gives us a verifiable audit trail, transactions cannot be modified in any way once they have been added

Benefits of Blockchain

  • Save Time
  • Remove Costs
  • Reduce Risk
  • Increase Trust

Asset Transfer Lab

Set up Hyperledger Composer Playground

Go to the Composer Playground

Hyperledger Composer Playground

Then Create a Business Network

Business Network Create Screen

Business Network has been created

We can click on Connect Now and start making transactions such as creating participants and vehicles

Creating Transactions

Create some members by navigating to the Test Section (at the top) and then Members from the Menu and clicking on Create New Participant

Participant Creation Screen

We can see our created Participants on the Member Screen

Participant Listing

Once this has been done we can do the same for Vehicles and Vehicle Listings

We can look at the transactions made from the All Transactions page

Transaction History

Explore the Definitions

Head over to the editor screen and you will be able to see the different configuration available in our blockchain

Define View - README

The different elements of our blockchain are defined with the following structures and rules

1
namespace org.acme.vehicle.auction
2
3
asset Vehicle identified by vin {
4
o String vin
5
--> Member owner
6
}
7
8
enum ListingState {
9
10
o FOR_SALE
11
o RESERVE_NOT_MET
12
o SOLD
13
}
14
15
asset VehicleListing identified by listingId {
16
o String listingId
17
o Double reservePrice
18
o String description
19
o ListingState state
20
o Offer[] offers optional
21
--> Vehicle vehicle
22
}
23
24
abstract participant User identified by email {
25
o String email
26
o String firstName
27
o String lastName
28
}
29
30
participant Member extends User {
31
o Double balance
32
}
33
34
participant Auctioneer extends User {
35
}
36
37
transaction Offer {
38
o Double bidPrice
39
--> VehicleListing listing
40
--> Member member
41
}
42
43
transaction CloseBidding {
44
--> VehicleListing listing
45
}
1
async function closeBidding(closeBidding) {
2
// eslint-disable-line no-unused-vars
3
const listing = closeBidding.listing
4
if (listing.state !== 'FOR_SALE') {
5
throw new Error('Listing is not FOR SALE')
6
}
7
// by default we mark the listing as RESERVE_NOT_MET
8
listing.state = 'RESERVE_NOT_MET'
9
let highestOffer = null
10
let buyer = null
11
let seller = null
12
if (listing.offers && listing.offers.length > 0) {
13
// sort the bids by bidPrice
14
listing.offers.sort(function (a, b) {
15
return b.bidPrice - a.bidPrice
16
})
17
highestOffer = listing.offers[0]
18
if (highestOffer.bidPrice >= listing.reservePrice) {
19
// mark the listing as SOLD
20
listing.state = 'SOLD'
21
buyer = highestOffer.member
22
seller = listing.vehicle.owner
23
// update the balance of the seller
24
console.log('### seller balance before: ' + seller.balance)
25
seller.balance += highestOffer.bidPrice
26
console.log('### seller balance after: ' + seller.balance)
27
// update the balance of the buyer
28
console.log('### buyer balance before: ' + buyer.balance)
29
buyer.balance -= highestOffer.bidPrice
30
console.log('### buyer balance after: ' + buyer.balance)
31
// transfer the vehicle to the buyer
32
listing.vehicle.owner = buyer
33
// clear the offers
34
listing.offers = null
35
}
36
}
37
38
if (highestOffer) {
39
// save the vehicle
40
const vehicleRegistry = await getAssetRegistry(
41
'org.acme.vehicle.auction.Vehicle'
42
)
43
await vehicleRegistry.update(listing.vehicle)
44
}
45
46
// save the vehicle listing
47
const vehicleListingRegistry = await getAssetRegistry(
48
'org.acme.vehicle.auction.VehicleListing'
49
)
50
await vehicleListingRegistry.update(listing)
51
52
if (listing.state === 'SOLD') {
53
// save the buyer
54
const userRegistry = await getParticipantRegistry(
55
'org.acme.vehicle.auction.Member'
56
)
57
await userRegistry.updateAll([buyer, seller])
58
}
59
}
60
61
/**
62
* Make an Offer for a VehicleListing
63
* @param {org.acme.vehicle.auction.Offer} offer - the offer
64
* @transaction
65
*/
66
async function makeOffer(offer) {
67
// eslint-disable-line no-unused-vars
68
let listing = offer.listing
69
if (listing.state !== 'FOR_SALE') {
70
throw new Error('Listing is not FOR SALE')
71
}
72
if (!listing.offers) {
73
listing.offers = []
74
}
75
listing.offers.push(offer)
76
77
// save the vehicle listing
78
const vehicleListingRegistry = await getAssetRegistry(
79
'org.acme.vehicle.auction.VehicleListing'
80
)
81
await vehicleListingRegistry.update(listing)
82
}
1
rule Auctioneer {
2
description: "Allow the auctioneer full access"
3
participant: "org.acme.vehicle.auction.Auctioneer"
4
operation: ALL
5
resource: "org.acme.vehicle.auction.*"
6
action: ALLOW
7
}
8
9
rule Member {
10
description: "Allow the member read access"
11
participant: "org.acme.vehicle.auction.Member"
12
operation: READ
13
resource: "org.acme.vehicle.auction.*"
14
action: ALLOW
15
}
16
17
rule VehicleOwner {
18
description: "Allow the owner of a vehicle total access"
19
participant(m): "org.acme.vehicle.auction.Member"
20
operation: ALL
21
resource(v): "org.acme.vehicle.auction.Vehicle"
22
condition: (v.owner.getIdentifier() == m.getIdentifier())
23
action: ALLOW
24
}
25
26
rule VehicleListingOwner {
27
description: "Allow the owner of a vehicle total access to their vehicle listing"
28
participant(m): "org.acme.vehicle.auction.Member"
29
operation: ALL
30
resource(v): "org.acme.vehicle.auction.VehicleListing"
31
condition: (v.vehicle.owner.getIdentifier() == m.getIdentifier())
32
action: ALLOW
33
}
34
35
rule SystemACL {
36
description: "System ACL to permit all access"
37
participant: "org.hyperledger.composer.system.Participant"
38
operation: ALL
39
resource: "org.hyperledger.composer.system.**"
40
action: ALLOW
41
}
42
43
rule NetworkAdminUser {
44
description: "Grant business network administrators full access to user resources"
45
participant: "org.hyperledger.composer.system.NetworkAdmin"
46
operation: ALL
47
resource: "**"
48
action: ALLOW
49
}
50
51
rule NetworkAdminSystem {
52
description: "Grant business network administrators full access to system resources"
53
participant: "org.hyperledger.composer.system.NetworkAdmin"
54
operation: ALL
55
resource: "org.hyperledger.composer.system.**"
56
action: ALLOW
57
}