Stateless Smart Contracts

  1. The Dumb Contract
  2. The Filter

Dumb Contract

contract DataStore {
mapping(address => mapping(bytes32 => string)) public store;

event Save(address indexed from, bytes32 indexed key, string value);

function save(bytes32 key, string value) {
store[msg.sender][key] = value;
Save(msg.sender, key, value);
}
}
contract DataStore {
function save(bytes32 key, string value) {}
}

Filter

const abi = [
{
constant: false,
inputs: [
{ name: "key", type: "bytes32" },
{ name: "value", type: "string" }
],
name: "save",
outputs: [],
payable: false,
type: "function"
}
];
const decoder = new InputDataDecoder(abi);
const decodeInput = input => decoder.decodeData(input);
const processArgs = input =>
input.inputs.map((arg, i) => {
const type = input.types[i];
if (type === "string") {
return arg;
}
if (type === "bytes32") {
const toHex = `0x${arg.toString("hex")}`;
return web3.toUtf8(toHex);
}
return arg;
});
const run = async () => {
const tx = "0xc9fdf51d...";
const transaction = await web3.eth.getTransaction(tx);
const input = decodeInput(transaction.input);
if (input.name === "save") {
const args = processArgs(input);
const address = transaction.from;
const key = args[0];
const value = args[1];
// save the address / key / value to a database
}
};

Extras Details

function registerUsername(bytes32 username) external {
var hasUsername = usernames[msg.sender];
var isOwned = addresses[username];
if (isOwned != 0) throw; // prevents registration of existing username
if (hasUsername != 0) throw; // prevents registered address from registering another username
if (!isLowercase(username)) throw; // username must be lowercase
usernames[msg.sender] = username;
addresses[username] = msg.sender;
}
function post(string data) external {
var username = usernames[msg.sender];
if (username == 0) throw; // user must be registered
}

Tampering

Trade-Offs

  1. A stateless design requires a mix of decentralization and centralization to provide the best user experience / gas cost.
  2. Other contracts cannot access your data.
  3. If your contract has no events you must processes every block to find relevant transactions. This can be mitigated by firing off empty events with marginal extra gas costs.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

RestSharp Extent API Automation Framework

All about GIT…..

Getting Started with Kubernetes | Access Control: A Security Measure in Kubernetes

Integrate Firebase into Python App

Neo4j & Elastic Sync

The Development Trends of Six Major Container Technologies in 2021

Extracting email addresses from an actual paper list

Direct Access and the NLS Service

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Childs-Maidment

James Childs-Maidment

More from Medium

Building your first Dapp with Solidity on the Polygon Blockchain

How to build an application in Node JS and deploy Smart Contracts in Solidity on EVM (Ethereum)…

Build your first DeFi Application — My experience

Integrating the Multicall contract into your ReactJS Dapp