Basic blockchain

No preview image

1 collaborator

Default-person 彪 宋 (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.1.1 • Viewed 85 times • Downloaded 5 times • Run 0 times
Download the 'Basic blockchain' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;后面可以研究节点由于收益的生存和死亡变化

turtles-own [adress1 adress2 information question answer hashvalue  bitcoin life];定义了海龟的几个属性变量
  directed-link-breed [red-links red-link]
;;;定义了海龟的两个类型,矿工和区块
  breed [miners miner]            ;定义了矿工
  breed [blockchains blockchain]  ;定义了区块
blockchains-own [id];;定义了区块的属性变量
;;定义全局变量
globals [
  blocknum
  test
  computer
  startid
  endid
  goal
]

to setup-empty
   clear-all
   reset-ticks
end 

to setup
  clear-all
  set blocknum 0
  set test 0
   set startid  0

  createworld

  reset-ticks
end 

to go


  mine
  ask turtles[
    if breed = miners [
    rt 1]
  ]
  if computer = goal
  [ addblock;这里会循环太多次了,容易死循环
  ask links [ set color gray ]
    tick]
if energy?[
ask turtles [ reproduce ]
  death
  ]
  if supply?[supply]
  checkmap
tick
end 

to hash
;考虑 起始id和终止id时间转化为一个数值

  ;
end 

to createworld
   ;;把界面分成两个颜色,预计上面显示区块链,下面显示工人
  ask patches [

    ifelse (pycor > -1);;本来是0,为了区块变化时不越界改成了-1
    [set pcolor blue]
     [set pcolor white]
  ]
   ;;在上面先设置创世块1个
  create-blockchains 1 [
  set shape "square"
  set color yellow
  setxy -16 4
   set  adress2  1
  ]
  ;;在下面界面设置矿工,数量由滑动条决定
  ask n-of miner-number patches with [pycor < 0]
  [
    if (pycor < 0)
    [sprout-miners 1 [
      set shape "person construction"
    ]
    ]
  ]
end 

to supply
  if count miners < 50 [
  ask n-of supplementary patches with [pycor < 0]
  [
    if (pycor < 0)
    [sprout-miners 1 [
      set shape "person construction"
    ]
    ]
  ]
  ]
end 

to mine
;repeat 1 [
 ask miners [
    set answer  random difficult;;为了海龟和全局变量的切换
    set computer answer;;把海龟变量赋值给全局变量
   hide-turtle
   show-turtle


   set life life - cost
    ;if answer <= 3
    ;[addblock]
  ]

  ;;下面记录哪个矿工获得了比特币
  if computer = goal[
ask miners[
      if  answer = computer
      [set bitcoin bitcoin + 1
      set life life + 1]

      set information information + goal  ;;用来存储全部区块链的goal值,并且分配给每个矿工,这样实现全链存储
    ]
    set startid first   [who] of miners with  [answer = computer]  ;;取矿工的id准备做区块的开始id,first起到了list转化为数字的作用

  ]
end 

to addblock
  set test 0
  ;set goal 0

  set blocknum blocknum + 1
  create-blockchains 1 [

  set shape "square"
  set color yellow
    set adress2  startid ;;取矿工id做区块的尾地址
  setxy -16 + blocknum * 8 4 + random(12);区块链坐标
   set id id + blocknum ;记录每个区块的id
    set test test + first [who] of  blockchains with [id =  [id] of myself - 1 ];和比自己小的相连

    create-red-link-to blockchain  test
    [set color red]
    set adress1 first [adress2] of blockchains with [id =  [id] of myself - 1]  ;;生成的区块取前面块的地址做首地址
    set goal  adress1 +  adress2 ;;赋值给hash的功能值,这块想办法调通 ;first起到了list转化为number的作用,本来也只有一个元素

     if breed = blockchains [
      if layout?[layout]
      if onlyIoo?[onlyIoo]
  ]
  ]
end 

to reproduce
   if life > reproduceline [
    set life (life - reproduceline)
    ;; find an empty location to reproduce into
    let destination one-of neighbors4 with [not any? turtles-here]
    if destination != nobody [
      ;; if the location exists hatch a copy of the current turtle in the new location
      ;;  but mutate the child
      hatch-miners 1  [
         move-to destination]
      if ycor > 0
      [set ycor ycor - 16
      ]
      if ycor <= -16
      [set ycor ycor + 16
      ]
    ]
  ]
end 

to death
  ask turtles [
    if life < (0 - deadline) [die]
  ]
end 

to layout

  ;; the number 3 here is arbitrary; more repetitions slows down the
  ;; model, but too few gives poor layouts
  repeat 3 [
    ;; the more turtles we have to fit into the same amount of space,
    ;; the smaller the inputs to layout-spring we'll need to use
    let factor sqrt count blockchains
    ;; numbers here are arbitrarily chosen for pleasing appearance
    layout-spring blockchains links (1 / factor) (7 / factor) (1 / factor)
    display  ;; for smooth animation
  ]
  ;; don't bump the edges of the world
  let x-offset max [xcor] of blockchains + min [xcor] of blockchains
  ;let y-offset max [ycor] of blockchains + min [ycor] of blockchains
  let y-offset max [ycor] of blockchains;;加的相当于0,避免区块越界
  ;; big jumps look funny, so only adjust a little each time
  set x-offset limit-magnitude x-offset 0.1
  set y-offset limit-magnitude y-offset 0.1

  ask blockchains [ setxy (xcor - x-offset / 2) abs(ycor - y-offset / 2 ) ];加abs防止区块越界
end 

to-report limit-magnitude [number limit]
  if number > limit [ report limit ]
  if number < (- limit) [ report (- limit) ]
  report number
end 

to onlyIoo
  if count blockchains - onlynumber > 0[
     foreach [who] of turtles[
      if last [who] of turtles < (count blockchains - onlynumber)[
        hide-turtle
      ]
  ]
  ]
end 

to checkmap;;对出界的miners进行控制到相应窗口
  ask miners[
    if ycor >= 0[
      set ycor ycor - 16]
  ]
end 

There is only one version of this model, created 11 months ago by 彪 宋.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.