good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
bd582d91
Commit
bd582d91
authored
Dec 28, 2013
by
Jeffrey Wilcke
Browse files
Options
Downloads
Patches
Plain Diff
(un)marshal blocks and transactions
parent
95d877f7
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
block.go
+67
-14
67 additions, 14 deletions
block.go
transaction.go
+1
-3
1 addition, 3 deletions
transaction.go
with
68 additions
and
17 deletions
block.go
+
67
−
14
View file @
bd582d91
...
@@ -3,19 +3,18 @@ package main
...
@@ -3,19 +3,18 @@ package main
import
(
import
(
"fmt"
"fmt"
"time"
"time"
_
"bytes"
)
)
type
Block
struct
{
type
Block
struct
{
RlpSerializer
number
uint32
number
uint32
prevHash
string
prevHash
string
uncles
[]
*
Block
uncles
[]
*
Block
coinbase
string
coinbase
string
// state xxx
// state xxx
difficulty
int
difficulty
u
int
32
time
time
.
Time
time
time
.
Time
nonce
int
nonce
u
int
32
transactions
[]
*
Transaction
transactions
[]
*
Transaction
}
}
...
@@ -23,6 +22,11 @@ func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
...
@@ -23,6 +22,11 @@ func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block
:=
&
Block
{
block
:=
&
Block
{
// Slice of transactions to include in this block
// Slice of transactions to include in this block
transactions
:
transactions
,
transactions
:
transactions
,
number
:
1
,
prevHash
:
"1234"
,
coinbase
:
"me"
,
difficulty
:
10
,
nonce
:
0
,
time
:
time
.
Now
(),
time
:
time
.
Now
(),
}
}
...
@@ -44,28 +48,77 @@ func (block *Block) MarshalRlp() []byte {
...
@@ -44,28 +48,77 @@ func (block *Block) MarshalRlp() []byte {
encTx
[
i
]
=
string
(
tx
.
MarshalRlp
())
encTx
[
i
]
=
string
(
tx
.
MarshalRlp
())
}
}
/* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
header
:=
[]
interface
{}{
header
:=
[]
interface
{}{
block
.
number
,
block
.
number
,
//
block.prevHash,
block
.
prevHash
,
// Sha of uncles
// Sha of uncles
//block.coinbase,
""
,
block
.
coinbase
,
// root state
// root state
//Sha256Bin([]byte(RlpEncode(encTx))),
""
,
//block.difficulty,
string
(
Sha256Bin
([]
byte
(
RlpEncode
(
encTx
)))),
//block.time,
block
.
difficulty
,
//block.nonce,
block
.
time
.
String
(),
block
.
nonce
,
// extra?
// extra?
}
}
return
Encode
([]
interface
{}{
header
,
encTx
})
encoded
:=
Encode
([]
interface
{}{
header
,
encTx
})
return
encoded
}
}
func
(
block
*
Block
)
UnmarshalRlp
(
data
[]
byte
)
{
func
(
block
*
Block
)
UnmarshalRlp
(
data
[]
byte
)
{
fmt
.
Printf
(
"%q
\n
"
,
data
)
t
,
_
:=
Decode
(
data
,
0
)
t
,
_
:=
Decode
(
data
,
0
)
if
slice
,
ok
:=
t
.
([]
interface
{});
ok
{
if
slice
,
ok
:=
t
.
([]
interface
{});
ok
{
if
txes
,
ok
:=
slice
[
1
]
.
([]
interface
{});
ok
{
if
header
,
ok
:=
slice
[
0
]
.
([]
interface
{});
ok
{
fmt
.
Println
(
txes
[
0
])
if
number
,
ok
:=
header
[
0
]
.
(
uint8
);
ok
{
block
.
number
=
uint32
(
number
)
}
if
prevHash
,
ok
:=
header
[
1
]
.
([]
byte
);
ok
{
block
.
prevHash
=
string
(
prevHash
)
}
// sha of uncles is header[2]
if
coinbase
,
ok
:=
header
[
3
]
.
([]
byte
);
ok
{
block
.
coinbase
=
string
(
coinbase
)
}
// state is header[header[4]
// sha is header[5]
// It's either 8bit or 64
if
difficulty
,
ok
:=
header
[
6
]
.
(
uint8
);
ok
{
block
.
difficulty
=
uint32
(
difficulty
)
}
if
difficulty
,
ok
:=
header
[
6
]
.
(
uint64
);
ok
{
block
.
difficulty
=
uint32
(
difficulty
)
}
if
time
,
ok
:=
header
[
7
]
.
([]
byte
);
ok
{
fmt
.
Sprintf
(
"Time is: "
,
string
(
time
))
}
if
nonce
,
ok
:=
header
[
8
]
.
(
uint8
);
ok
{
block
.
nonce
=
uint32
(
nonce
)
}
}
if
txSlice
,
ok
:=
slice
[
1
]
.
([]
interface
{});
ok
{
block
.
transactions
=
make
([]
*
Transaction
,
len
(
txSlice
))
for
i
,
tx
:=
range
txSlice
{
if
t
,
ok
:=
tx
.
([]
byte
);
ok
{
tx
:=
&
Transaction
{}
tx
.
UnmarshalRlp
(
t
)
block
.
transactions
[
i
]
=
tx
}
}
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
transaction.go
+
1
−
3
View file @
bd582d91
...
@@ -34,8 +34,6 @@ var Period3Reward *big.Int = new(big.Int)
...
@@ -34,8 +34,6 @@ var Period3Reward *big.Int = new(big.Int)
var
Period4Reward
*
big
.
Int
=
new
(
big
.
Int
)
var
Period4Reward
*
big
.
Int
=
new
(
big
.
Int
)
type
Transaction
struct
{
type
Transaction
struct
{
RlpSerializer
sender
string
sender
string
recipient
string
recipient
string
value
uint32
value
uint32
...
@@ -83,7 +81,7 @@ func (tx *Transaction) MarshalRlp() []byte {
...
@@ -83,7 +81,7 @@ func (tx *Transaction) MarshalRlp() []byte {
tx
.
data
,
tx
.
data
,
}
}
return
[]
byte
(
Encode
(
preEnc
)
)
return
Encode
(
preEnc
)
}
}
func
(
tx
*
Transaction
)
UnmarshalRlp
(
data
[]
byte
)
{
func
(
tx
*
Transaction
)
UnmarshalRlp
(
data
[]
byte
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment