Fund:0.024 ETH / $13.88
Question is solved

How are traceability and scalability achieved in Ethereum Smart Contracts?

1 month ago

Say I want to track car ownership and have coded the following

struct Owner {
string ownerId;
string fName;
string lName;

struct Car {
string carId;
string model;
string manufacturer;
Owner owner;

mapping (address =>Car) carList;

I have 2 functions addCar which create a new car and adds it to the map, and changeOwner which updates the car with a new owner. I have the following queries –

a.Traceability – If I need to see the entire history of ownership of a car do I need to implement this in code, or is this out of the box i.e. in ethereum can we trace back the state of storage to any point?

b. Scalability – Is the above mapping scaleable i.e. if we need to store details of say a 1,000,000 cars will the gas cost go up as the length of the map increases?

Please advise.


1. Traceability:
Whenever the state of your contract is changed, that state change is permanently stored on the blockchain. You can use to view block info and examine each individual transactions.

However if you want to programmatically access this info, there are two ways. One is to use Events as a way to get the history of things that happened in your contract. You can use web3's event api to set filters on the type of events you want to listen to.

Another way is to send queries about the blockchain at a specific block. For example, web3.eth.getStorageAt() method takes a block number as parameter and you can query the specific state at that block.

Basically, in short, yes that state of your smart contract is stored on the blockchain and there are ways to extract that history out by manually examining it or by using code.

2. Scalability:
Yes the mapping is scalable, the mapping uses a hash function to calculate an index based on the key and store the data at that index. This an O(1) operation which means it takes constant time and doesn't matter on size of mapping.

Whenever you want to add new element to the mapping, the caller of the contract AddCar() has to pay the gas to add a new entry. The caller can be the contract owner or could be done by anyone depending on if you make that function public. Whoever calls the add function has to pay the gas.

The gas price per each new entry should be more or less the same depending on the demand of the network. In other words, the price per new entry into the mapping is not related to the size of the map. Basically, the price is more or less the same when your adding the first entry or the 1 millionth entry if the network demand is more or less the same.
1 month ago

Your question is successfully refund. You will be redirect in few seconds.