Have you coded a block chain for fun?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Have you coded a block chain for fun?

Post by Gunroar:Cannon() »

Yeah, that's the link I needed to see to understand a little bit more what blockchain is. Ah, and I meant even possible to implement in love2d.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Have you coded a block chain for fun?

Post by grump »

All you need is a timestamp and a cryptographically secure hash function.
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Have you coded a block chain for fun?

Post by togFox »

I have an idea in the back of my mind about a game that runs in a peer-to-peer network (no host). This might be a stock market game or something that happens in real time over a long time. Peers can join the network and then drop out over the space of days.

The world would need to persist over a long time but exchanging the whole database to every peer on every connection or transaction is not doable. A block chain might work so let's practice block chains. :)
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Have you coded a block chain for fun?

Post by togFox »

I read that LUA doesn't have native hash but, as you have linked, there are lots of libraries.

Strictly speaking, on a trusted network with gaming friends, is encryption needed?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Have you coded a block chain for fun?

Post by grump »

Here you go, a useless, buggy and insecure implementation of a basic blockchain algorithm.
Please don't ask further questions about this, or they will be coming after me, lol

Code: Select all

local function newBlock(head, payload)
	local time = os.date()
	local blockData = (head and head.hash or '') .. time .. payload
	local block = {
		time = time,
		payload = payload,
		hash = love.data.hash('sha256', blockData),
	}
	if head then head.next = block end
	return block
end

local function verifyBlock(block, prev)
	local blockData = (prev and prev.hash or '') .. block.time .. block.payload
	return love.data.hash('sha256', blockData) == block.hash
end

local function verify(head)
	local prev
	while head do
		print(head.time, head.payload, love.data.encode('string', 'hex', head.hash))
		if not verifyBlock(head, prev) then return false end
		prev, head = head, head.next
	end
	return true
end

local chain = newBlock(nil, 'GENESIS BLOCK')
local head = newBlock(chain, 'second block')
head = newBlock(head, 'and a third one')
print("Chain is valid: ", verify(chain) and 'yes' or 'nope')

-- change second block
chain.next.payload = 'block modified!'
-- the basic idea is that a valid hash for the modified block in the middle of the chain is practically impossible to compute
-- because all previous hashes contribute to all subsequent block's hashes
chain.next.hash = love.data.hash('sha256', chain.hash .. chain.next.time .. chain.next.payload)
print("Chain is valid: ", verify(chain) and 'yes' or 'nope')
Edit: This is of course just the basic principles of the algorithm. How to make a distributed blockchain is a different problem. And proof-of-work could be easily added by adding a nonce and putting constraints on what is considered a valid hash value. For example, accept only hash values that start with one or more 0 bytes.
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Have you coded a block chain for fun?

Post by togFox »

Thanks. I'll keep this one up my sleeve and extend. :)
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
4xEmpire
Prole
Posts: 26
Joined: Wed Aug 04, 2021 4:49 pm

Re: Have you coded a block chain for fun?

Post by 4xEmpire »

Gunroar:Cannon() wrote: Mon Nov 22, 2021 8:57 pm What is blockchain? Like security? Why was Slime all
Image
LOL. The funny thing is I don't even care about a blockchain API, I just wanted to have a discussion about the evolution of games with this developing technology. I felt like my post would be blocked if I didn't make it relevant to löve somehow, so I added that question at the end, entirely harmlessly! But apparently that question got someone a bit rattled.
-----
PS sorry for the off topic reply. This thread has been an interesting read
User avatar
slime
Solid Snayke
Posts: 3129
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Have you coded a block chain for fun?

Post by slime »

Gunroar:Cannon() wrote: Mon Nov 22, 2021 8:57 pm What is blockchain? Like security? Why was Slime all
https://i.kym-cdn.com/entries/icons/ori ... geFace.jpg
The other thread was talking about it in terms that end up being indistinguishable from one of either scammers, money launderers, or ignorant people who are being used by the first two categories to prop up those things (and would place future positive posts in those same categories).

The tech is a solution looking for a problem instead of being created to solve a real large-scale technical problem. In the past few years it's been used mostly by people who don't care about the environment to take money from people who don't know better under the guise of technobabble. Those kinds of people are not welcome here.

Because investors buy into the technobabble, it's getting some traction in the industry as a way to take investment money and inflate the dollar value of a temporary currency without creating anything of real value. That doesn't mean games are evolving, it just means people who don't care much about games or the environment but do care about money are using games to get what they want. It probably won't last long.



I understand people might not be as familiar with the topic as I am - but these forums are not a place to entice people who don't know better into it, or to have long-form Q&As about how these scams operate.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Have you coded a block chain for fun?

Post by ReFreezed »

Well said, slime!
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Have you coded a block chain for fun?

Post by grump »

Blockchain: an immutable data structure secured by cryptographic principles
Cryptocurrency: the thing most of you are talking about

Misconceptions from start to finish. Let's talk about tech, not ideology.
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests