Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
bd582d91
Commit
bd582d91
authored
Dec 28, 2013
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(un)marshal blocks and transactions
parent
95d877f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
17 deletions
+68
-17
block.go
block.go
+67
-14
transaction.go
transaction.go
+1
-3
No files found.
block.go
View file @
bd582d91
...
...
@@ -3,19 +3,18 @@ package main
import
(
"fmt"
"time"
_
"bytes"
)
type
Block
struct
{
RlpSerializer
number
uint32
prevHash
string
uncles
[]
*
Block
coinbase
string
// state xxx
difficulty
int
difficulty
uint32
time
time
.
Time
nonce
int
nonce
uint32
transactions
[]
*
Transaction
}
...
...
@@ -23,6 +22,11 @@ func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block
:=
&
Block
{
// Slice of transactions to include in this block
transactions
:
transactions
,
number
:
1
,
prevHash
:
"1234"
,
coinbase
:
"me"
,
difficulty
:
10
,
nonce
:
0
,
time
:
time
.
Now
(),
}
...
...
@@ -44,28 +48,77 @@ func (block *Block) MarshalRlp() []byte {
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
{}{
block
.
number
,
//
block.prevHash,
block
.
prevHash
,
// Sha of uncles
//block.coinbase,
""
,
block
.
coinbase
,
// root state
//Sha256Bin([]byte(RlpEncode(encTx))),
//block.difficulty,
//block.time,
//block.nonce,
""
,
string
(
Sha256Bin
([]
byte
(
RlpEncode
(
encTx
)))),
block
.
difficulty
,
block
.
time
.
String
(),
block
.
nonce
,
// extra?
}
return
Encode
([]
interface
{}{
header
,
encTx
})
encoded
:=
Encode
([]
interface
{}{
header
,
encTx
})
return
encoded
}
func
(
block
*
Block
)
UnmarshalRlp
(
data
[]
byte
)
{
fmt
.
Printf
(
"%q
\n
"
,
data
)
t
,
_
:=
Decode
(
data
,
0
)
if
slice
,
ok
:=
t
.
([]
interface
{});
ok
{
if
txes
,
ok
:=
slice
[
1
]
.
([]
interface
{});
ok
{
fmt
.
Println
(
txes
[
0
])
if
header
,
ok
:=
slice
[
0
]
.
([]
interface
{});
ok
{
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
}
}
}
}
}
transaction.go
View file @
bd582d91
...
...
@@ -34,8 +34,6 @@ var Period3Reward *big.Int = new(big.Int)
var
Period4Reward
*
big
.
Int
=
new
(
big
.
Int
)
type
Transaction
struct
{
RlpSerializer
sender
string
recipient
string
value
uint32
...
...
@@ -83,7 +81,7 @@ func (tx *Transaction) MarshalRlp() []byte {
tx
.
data
,
}
return
[]
byte
(
Encode
(
preEnc
)
)
return
Encode
(
preEnc
)
}
func
(
tx
*
Transaction
)
UnmarshalRlp
(
data
[]
byte
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment