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
946db8ba
Commit
946db8ba
authored
8 years ago
by
Péter Szilágyi
Committed by
Felix Lange
8 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
internal/guide: initial test suite to ensure guide snippets run ok (#3582)
parent
7814a8e1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
118 additions
and
0 deletions
+118
-0
guide.go
internal/guide/guide.go
+18
-0
guide_test.go
internal/guide/guide_test.go
+100
-0
No files found.
internal/guide/guide.go
0 → 100644
View file @
946db8ba
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package guide is a small test suite to ensure snippets in the dev guide work.
package
guide
This diff is collapsed.
Click to expand it.
internal/guide/guide_test.go
0 → 100644
View file @
946db8ba
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// This file contains the code snippets from the developer's guide embedded into
// Go tests. This ensures that any code published in out guides will not break
// accidentally via some code update. If some API changes nonetheless that needs
// modifying this file, please port any modification over into the developer's
// guide wiki pages too!
package
guide
import
(
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
)
// Tests that the account management snippets work correctly.
func
TestAccountManagement
(
t
*
testing
.
T
)
{
// Create a temporary folder to work with
workdir
,
err
:=
ioutil
.
TempDir
(
""
,
""
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to create temporary work dir: %v"
,
err
)
}
defer
os
.
RemoveAll
(
workdir
)
// Create an encrypted keystore manager with standard crypto parameters
am
:=
accounts
.
NewManager
(
filepath
.
Join
(
workdir
,
"keystore"
),
accounts
.
StandardScryptN
,
accounts
.
StandardScryptP
)
// Create a new account with the specified encryption passphrase
newAcc
,
err
:=
am
.
NewAccount
(
"Creation password"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to create new account: %v"
,
err
)
}
// Export the newly created account with a different passphrase. The returned
// data from this method invocation is a JSON encoded, encrypted key-file
jsonAcc
,
err
:=
am
.
Export
(
newAcc
,
"Creation password"
,
"Export password"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to export account: %v"
,
err
)
}
// Update the passphrase on the account created above inside the local keystore
if
err
:=
am
.
Update
(
newAcc
,
"Creation password"
,
"Update password"
);
err
!=
nil
{
t
.
Fatalf
(
"Failed to update account: %v"
,
err
)
}
// Delete the account updated above from the local keystore
if
err
:=
am
.
Delete
(
newAcc
,
"Update password"
);
err
!=
nil
{
t
.
Fatalf
(
"Failed to delete account: %v"
,
err
)
}
// Import back the account we've exported (and then deleted) above with yet
// again a fresh passphrase
if
_
,
err
:=
am
.
Import
(
jsonAcc
,
"Export password"
,
"Import password"
);
err
!=
nil
{
t
.
Fatalf
(
"Failed to import account: %v"
,
err
)
}
// Create a new account to sign transactions with
signer
,
err
:=
am
.
NewAccount
(
"Signer password"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to create signer account: %v"
,
err
)
}
txHash
:=
common
.
HexToHash
(
"0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
)
// Sign a transaction with a single authorization
if
_
,
err
:=
am
.
SignWithPassphrase
(
signer
,
"Signer password"
,
txHash
.
Bytes
());
err
!=
nil
{
t
.
Fatalf
(
"Failed to sign with passphrase: %v"
,
err
)
}
// Sign a transaction with multiple manually cancelled authorizations
if
err
:=
am
.
Unlock
(
signer
,
"Signer password"
);
err
!=
nil
{
t
.
Fatalf
(
"Failed to unlock account: %v"
,
err
)
}
if
_
,
err
:=
am
.
Sign
(
signer
.
Address
,
txHash
.
Bytes
());
err
!=
nil
{
t
.
Fatalf
(
"Failed to sign with unlocked account: %v"
,
err
)
}
if
err
:=
am
.
Lock
(
signer
.
Address
);
err
!=
nil
{
t
.
Fatalf
(
"Failed to lock account: %v"
,
err
)
}
// Sign a transaction with multiple automatically cancelled authorizations
if
err
:=
am
.
TimedUnlock
(
signer
,
"Signer password"
,
time
.
Second
);
err
!=
nil
{
t
.
Fatalf
(
"Failed to time unlock account: %v"
,
err
)
}
if
_
,
err
:=
am
.
Sign
(
signer
.
Address
,
txHash
.
Bytes
());
err
!=
nil
{
t
.
Fatalf
(
"Failed to sign with time unlocked account: %v"
,
err
)
}
}
This diff is collapsed.
Click to expand it.
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