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
5da78427
Commit
5da78427
authored
Jan 01, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added db query interface and moved memory database
parent
52952e27
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
158 additions
and
0 deletions
+158
-0
db_query_interface.go
db_query_interface.go
+97
-0
memory_database.go
memory_database.go
+25
-0
testing.go
testing.go
+36
-0
No files found.
db_query_interface.go
0 → 100644
View file @
5da78427
package
main
import
(
"fmt"
"bufio"
"strings"
"os"
"errors"
"encoding/hex"
)
type
DbInterface
struct
{
db
*
MemDatabase
trie
*
Trie
}
func
NewDBInterface
()
*
DbInterface
{
db
,
_
:=
NewMemDatabase
()
trie
:=
NewTrie
(
db
,
""
)
return
&
DbInterface
{
db
:
db
,
trie
:
trie
}
}
func
(
i
*
DbInterface
)
ValidateInput
(
action
string
,
argumentLength
int
)
error
{
err
:=
false
var
expArgCount
int
switch
{
case
action
==
"update"
&&
argumentLength
!=
2
:
err
=
true
expArgCount
=
2
case
action
==
"get"
&&
argumentLength
!=
1
:
err
=
true
expArgCount
=
1
case
(
action
==
"quit"
||
action
==
"exit"
)
&&
argumentLength
!=
0
:
err
=
true
expArgCount
=
0
}
if
err
{
return
errors
.
New
(
fmt
.
Sprintf
(
"'%s' requires %d args, got %d"
,
action
,
expArgCount
,
argumentLength
))
}
else
{
return
nil
}
}
func
(
i
*
DbInterface
)
ParseInput
(
input
string
)
bool
{
scanner
:=
bufio
.
NewScanner
(
strings
.
NewReader
(
input
))
scanner
.
Split
(
bufio
.
ScanWords
)
count
:=
0
var
tokens
[]
string
for
scanner
.
Scan
()
{
count
++
tokens
=
append
(
tokens
,
scanner
.
Text
())
}
if
err
:=
scanner
.
Err
();
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stderr
,
"reading input:"
,
err
)
}
if
len
(
tokens
)
==
0
{
return
true
}
err
:=
i
.
ValidateInput
(
tokens
[
0
],
count
-
1
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
else
{
switch
tokens
[
0
]
{
case
"update"
:
i
.
trie
.
Update
(
tokens
[
1
],
tokens
[
2
])
fmt
.
Println
(
hex
.
EncodeToString
([]
byte
(
i
.
trie
.
root
)))
case
"get"
:
fmt
.
Println
(
i
.
trie
.
Get
(
tokens
[
1
]))
case
"exit"
,
"quit"
,
"q"
:
return
false
default
:
fmt
.
Println
(
"Unknown command:"
,
tokens
[
0
])
}
}
return
true
}
func
(
i
*
DbInterface
)
Start
()
{
reader
:=
bufio
.
NewReader
(
os
.
Stdin
)
for
{
fmt
.
Printf
(
"db >>> "
)
str
,
_
,
err
:=
reader
.
ReadLine
()
if
err
!=
nil
{
fmt
.
Println
(
"Error reading input"
,
err
)
}
else
{
if
!
i
.
ParseInput
(
string
(
str
))
{
return
}
}
}
}
memory_database.go
0 → 100644
View file @
5da78427
package
main
import
(
)
/*
* This is a test memory database. Do not use for any production it does not get persisted
*/
type
MemDatabase
struct
{
db
map
[
string
][]
byte
}
func
NewMemDatabase
()
(
*
MemDatabase
,
error
)
{
db
:=
&
MemDatabase
{
db
:
make
(
map
[
string
][]
byte
)}
return
db
,
nil
}
func
(
db
*
MemDatabase
)
Put
(
key
[]
byte
,
value
[]
byte
)
{
db
.
db
[
string
(
key
)]
=
value
}
func
(
db
*
MemDatabase
)
Get
(
key
[]
byte
)
([]
byte
,
error
)
{
return
db
.
db
[
string
(
key
)],
nil
}
testing.go
0 → 100644
View file @
5da78427
package
main
import
(
"fmt"
)
func
Testing
()
{
bm
:=
NewBlockManager
()
tx
:=
NewTransaction
(
"
\x00
"
,
20
,
[]
string
{
"SET 10 6"
,
"LD 10 10"
,
"LT 10 1 20"
,
"SET 255 7"
,
"JMPI 20 255"
,
"STOP"
,
"SET 30 200"
,
"LD 30 31"
,
"SET 255 22"
,
"JMPI 31 255"
,
"SET 255 15"
,
"JMP 255"
,
})
txData
:=
tx
.
MarshalRlp
()
copyTx
:=
&
Transaction
{}
copyTx
.
UnmarshalRlp
(
txData
)
tx2
:=
NewTransaction
(
"
\x00
"
,
20
,
[]
string
{
"SET 10 6"
,
"LD 10 10"
})
blck
:=
CreateBlock
([]
*
Transaction
{
tx2
,
tx
})
bm
.
ProcessBlock
(
blck
)
fmt
.
Println
(
"GenesisBlock:"
,
GenisisBlock
,
"hashed"
,
GenisisBlock
.
Hash
())
}
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