Page 2 of 4

Re: Have you coded a block chain for fun?

Posted: Mon Nov 22, 2021 9:53 pm
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.

Re: Have you coded a block chain for fun?

Posted: Mon Nov 22, 2021 9:57 pm
by grump
All you need is a timestamp and a cryptographically secure hash function.

Re: Have you coded a block chain for fun?

Posted: Mon Nov 22, 2021 10:09 pm
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. :)

Re: Have you coded a block chain for fun?

Posted: Mon Nov 22, 2021 10:11 pm
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?

Re: Have you coded a block chain for fun?

Posted: Mon Nov 22, 2021 11:16 pm
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.

Re: Have you coded a block chain for fun?

Posted: Tue Nov 23, 2021 11:24 am
by togFox
Thanks. I'll keep this one up my sleeve and extend. :)

Re: Have you coded a block chain for fun?

Posted: Fri Nov 26, 2021 4:21 pm
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

Re: Have you coded a block chain for fun?

Posted: Fri Nov 26, 2021 8:43 pm
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.

Re: Have you coded a block chain for fun?

Posted: Fri Nov 26, 2021 9:30 pm
by ReFreezed
Well said, slime!

Re: Have you coded a block chain for fun?

Posted: Fri Nov 26, 2021 10:19 pm
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.