Clear Filters
Clear Filters

Simple blockchain V2V Communication

32 views (last 30 days)
We are trying to setup basic blockchain in MATLAB.
This code sets up the basic structure for a blockchain in MATLAB. It defines a `Block` class to represent individual blocks, implements a hashing function, and provides functions to create blocks and simulate V2V communication. The code demonstrates the creation of a blockchain and the exchange of messages between two vehicles. The output displays the details of each V2V communication, including the sender, receiver, data, and the newly created block's index and hash.
But when we run this code:
classdef sample
properties
Index
Timestamp
PreviousHash
Data
CurrentHash
end
methods
function block = sample(index, timestamp, previousHash, data)
block.Index = index;
block.Timestamp = timestamp;
block.PreviousHash = previousHash;
block.Data = data;
block.CurrentHash = '';
end
function hash = calculateHash(~, data)
% Implement your chosen cryptographic hash function here
% For simplicity, let's assume a simple hash calculation using the ASCII values of the characters
hash = num2str(sum(double(data)));
end
function block = createBlock(obj, index, previousHash, data)
timestamp = datestr(now);
block = sample(index, timestamp, previousHash, data);
block.CurrentHash = obj.calculateHash(sprintf('%d%s%s%s', block.Index, block.Timestamp, block.PreviousHash, block.Data));
end
function blockchain = createBlockchain(obj)
blockchain = {};
genesisBlock = obj.createBlock(1, '0', 'Genesis Block');
blockchain{1} = genesisBlock;
end
function blockchain = V2VCommunication(obj, blockchain, sender, receiver, data)
previousBlock = blockchain{end};
newIndex = previousBlock.Index + 1;
newBlock = obj.createBlock(newIndex, previousBlock.CurrentHash, data);
fprintf('Sender: %s\n', sender);
fprintf('Receiver: %s\n', receiver);
fprintf('Data: %s\n', data);
fprintf('New Block Index: %d\n', newBlock.Index);
fprintf('New Block Hash: %s\n\n', newBlock.CurrentHash);
blockchain{end+1} = newBlock;
end
end
end
% Test the blockchain
s = sample(s);
blockchain = s.createBlockchain();
blockchain = s.V2VCommunication[blockchain, 'Vehicle1', 'Vehicle2', 'Hello'];
blockchain = s.V2VCommunication(blockchain, 'Vehicle2', 'Vehicle1', 'Hi');
We get the following error:
Error using sample
File: sample.m Line: 53 Column: 1
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
P.S: Here, sample is the file name as well.

Accepted Answer

Steven Lord
Steven Lord on 6 Jul 2023
You cannot store your % Test the blockchain code in the file after the end that closes the classdef block. If MATLAB hadn't thrown an error you still would not have been able to execute it. Move that test code to a different file or run it in the Command Window.
  4 Comments
Achyuth S.S
Achyuth S.S on 15 Jan 2024
Code was correct, just that I had to move certain parts to a different file and run it.
idris
idris on 5 Sep 2024
can you share the full procedure?

Sign in to comment.

More Answers (0)

Categories

Find more on Testing Frameworks in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!