Compare commits
5 Commits
9b68875d68
...
v1.10.25
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69568c5548 | ||
|
|
8f61fc8b73 | ||
|
|
972007a517 | ||
|
|
3b41be695e | ||
|
|
d0dc349fd3 |
44
.github/CODEOWNERS
vendored
44
.github/CODEOWNERS
vendored
@@ -1,36 +1,24 @@
|
|||||||
# Lines starting with '#' are comments.
|
# Lines starting with '#' are comments.
|
||||||
# Each line is a file pattern followed by one or more owners.
|
# Each line is a file pattern followed by one or more owners.
|
||||||
|
|
||||||
accounts/usbwallet/ @gballet
|
accounts/usbwallet @karalabe
|
||||||
accounts/scwallet/ @gballet
|
accounts/scwallet @gballet
|
||||||
accounts/abi/ @gballet @MariusVanDerWijden
|
accounts/abi @gballet @MariusVanDerWijden
|
||||||
beacon/engine/ @MariusVanDerWijden @lightclient @fjl
|
cmd/clef @holiman
|
||||||
beacon/light/ @zsfelfoldi
|
cmd/puppeth @karalabe
|
||||||
beacon/merkle/ @zsfelfoldi
|
consensus @karalabe
|
||||||
beacon/types/ @zsfelfoldi @fjl
|
core/ @karalabe @holiman @rjl493456442
|
||||||
beacon/params/ @zsfelfoldi @fjl
|
eth/ @karalabe @holiman @rjl493456442
|
||||||
cmd/clef/ @holiman
|
eth/catalyst/ @gballet
|
||||||
cmd/evm/ @holiman @MariusVanDerWijden @lightclient
|
|
||||||
core/state/ @rjl493456442 @holiman
|
|
||||||
crypto/ @gballet @jwasinger @holiman @fjl
|
|
||||||
core/ @holiman @rjl493456442
|
|
||||||
eth/ @holiman @rjl493456442
|
|
||||||
eth/catalyst/ @MariusVanDerWijden @lightclient @fjl @jwasinger
|
|
||||||
eth/tracers/ @s1na
|
eth/tracers/ @s1na
|
||||||
ethclient/ @fjl
|
graphql/ @gballet @s1na
|
||||||
ethdb/ @rjl493456442
|
les/ @zsfelfoldi @rjl493456442
|
||||||
event/ @fjl
|
light/ @zsfelfoldi @rjl493456442
|
||||||
trie/ @rjl493456442
|
mobile/ @karalabe @ligi
|
||||||
triedb/ @rjl493456442
|
|
||||||
core/tracing/ @s1na
|
|
||||||
graphql/ @s1na
|
|
||||||
internal/ethapi/ @fjl @s1na @lightclient
|
|
||||||
internal/era/ @lightclient
|
|
||||||
metrics/ @holiman
|
|
||||||
miner/ @MariusVanDerWijden @holiman @fjl @rjl493456442
|
|
||||||
node/ @fjl
|
node/ @fjl
|
||||||
p2p/ @fjl @zsfelfoldi
|
p2p/ @fjl @zsfelfoldi
|
||||||
rlp/ @fjl
|
|
||||||
params/ @fjl @holiman @karalabe @gballet @rjl493456442 @zsfelfoldi
|
|
||||||
rpc/ @fjl @holiman
|
rpc/ @fjl @holiman
|
||||||
|
p2p/simulations @fjl
|
||||||
|
p2p/protocols @fjl
|
||||||
|
p2p/testing @fjl
|
||||||
signer/ @holiman
|
signer/ @holiman
|
||||||
|
|||||||
2
.github/CONTRIBUTING.md
vendored
2
.github/CONTRIBUTING.md
vendored
@@ -35,6 +35,6 @@ and help.
|
|||||||
|
|
||||||
## Configuration, dependencies, and tests
|
## Configuration, dependencies, and tests
|
||||||
|
|
||||||
Please see the [Developers' Guide](https://geth.ethereum.org/docs/developers/geth-developer/dev-guide)
|
Please see the [Developers' Guide](https://geth.ethereum.org/docs/developers/devguide)
|
||||||
for more details on configuring your environment, managing project dependencies
|
for more details on configuring your environment, managing project dependencies
|
||||||
and testing procedures.
|
and testing procedures.
|
||||||
|
|||||||
1
.github/ISSUE_TEMPLATE/bug.md
vendored
1
.github/ISSUE_TEMPLATE/bug.md
vendored
@@ -9,7 +9,6 @@ assignees: ''
|
|||||||
#### System information
|
#### System information
|
||||||
|
|
||||||
Geth version: `geth version`
|
Geth version: `geth version`
|
||||||
CL client & version: e.g. lighthouse/nimbus/prysm@v1.0.0
|
|
||||||
OS & Version: Windows/Linux/OSX
|
OS & Version: Windows/Linux/OSX
|
||||||
Commit hash : (if `develop`)
|
Commit hash : (if `develop`)
|
||||||
|
|
||||||
|
|||||||
24
.github/workflows/go.yml
vendored
24
.github/workflows/go.yml
vendored
@@ -1,24 +0,0 @@
|
|||||||
name: i386 linux tests
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ master ]
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: self-hosted
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
go-version: 1.23.0
|
|
||||||
cache: false
|
|
||||||
- name: Run tests
|
|
||||||
run: go test -short ./...
|
|
||||||
env:
|
|
||||||
GOOS: linux
|
|
||||||
GOARCH: 386
|
|
||||||
24
.gitignore
vendored
24
.gitignore
vendored
@@ -4,11 +4,16 @@
|
|||||||
# or operating system, you probably want to add a global ignore instead:
|
# or operating system, you probably want to add a global ignore instead:
|
||||||
# git config --global core.excludesfile ~/.gitignore_global
|
# git config --global core.excludesfile ~/.gitignore_global
|
||||||
|
|
||||||
|
/tmp
|
||||||
*/**/*un~
|
*/**/*un~
|
||||||
*/**/*.test
|
*/**/*.test
|
||||||
*un~
|
*un~
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*/**/.DS_Store
|
*/**/.DS_Store
|
||||||
|
.ethtest
|
||||||
|
*/**/*tx_database*
|
||||||
|
*/**/*dapps*
|
||||||
|
build/_vendor/pkg
|
||||||
|
|
||||||
#*
|
#*
|
||||||
.#*
|
.#*
|
||||||
@@ -23,23 +28,22 @@
|
|||||||
/build/bin/
|
/build/bin/
|
||||||
/geth*.zip
|
/geth*.zip
|
||||||
|
|
||||||
# used by the build/ci.go archive + upload tool
|
|
||||||
/geth*.tar.gz
|
|
||||||
/geth*.tar.gz.sig
|
|
||||||
/geth*.tar.gz.asc
|
|
||||||
/geth*.zip.sig
|
|
||||||
/geth*.zip.asc
|
|
||||||
|
|
||||||
|
|
||||||
# travis
|
# travis
|
||||||
profile.tmp
|
profile.tmp
|
||||||
profile.cov
|
profile.cov
|
||||||
|
|
||||||
# IdeaIDE
|
# IdeaIDE
|
||||||
.idea
|
.idea
|
||||||
*.iml
|
|
||||||
|
|
||||||
# VS Code
|
# VS Code
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
tests/spec-tests/
|
# dashboard
|
||||||
|
/dashboard/assets/flow-typed
|
||||||
|
/dashboard/assets/node_modules
|
||||||
|
/dashboard/assets/stats.json
|
||||||
|
/dashboard/assets/bundle.js
|
||||||
|
/dashboard/assets/bundle.js.map
|
||||||
|
/dashboard/assets/package-lock.json
|
||||||
|
|
||||||
|
**/yarn-error.log
|
||||||
|
|||||||
@@ -3,31 +3,33 @@
|
|||||||
run:
|
run:
|
||||||
timeout: 20m
|
timeout: 20m
|
||||||
tests: true
|
tests: true
|
||||||
|
# default is true. Enables skipping of directories:
|
||||||
|
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
|
||||||
|
skip-dirs-use-default: true
|
||||||
|
skip-files:
|
||||||
|
- core/genesis_alloc.go
|
||||||
|
|
||||||
linters:
|
linters:
|
||||||
disable-all: true
|
disable-all: true
|
||||||
enable:
|
enable:
|
||||||
|
- deadcode
|
||||||
|
- goconst
|
||||||
- goimports
|
- goimports
|
||||||
- gosimple
|
- gosimple
|
||||||
- govet
|
- govet
|
||||||
- ineffassign
|
- ineffassign
|
||||||
- misspell
|
- misspell
|
||||||
- unconvert
|
- unconvert
|
||||||
|
- varcheck
|
||||||
- typecheck
|
- typecheck
|
||||||
- unused
|
- unused
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- bidichk
|
- bidichk
|
||||||
- durationcheck
|
- durationcheck
|
||||||
- copyloopvar
|
- exportloopref
|
||||||
|
- gosec
|
||||||
- whitespace
|
- whitespace
|
||||||
- revive # only certain checks enabled
|
|
||||||
- durationcheck
|
|
||||||
- gocheckcompilerdirectives
|
|
||||||
- reassign
|
|
||||||
- mirror
|
|
||||||
- tenv
|
|
||||||
### linters we tried and will not be using:
|
|
||||||
###
|
|
||||||
# - structcheck # lots of false positives
|
# - structcheck # lots of false positives
|
||||||
# - errcheck #lot of false positives
|
# - errcheck #lot of false positives
|
||||||
# - contextcheck
|
# - contextcheck
|
||||||
@@ -40,40 +42,32 @@ linters:
|
|||||||
linters-settings:
|
linters-settings:
|
||||||
gofmt:
|
gofmt:
|
||||||
simplify: true
|
simplify: true
|
||||||
revive:
|
goconst:
|
||||||
enable-all-rules: false
|
min-len: 3 # minimum length of string constant
|
||||||
# here we enable specific useful rules
|
min-occurrences: 6 # minimum number of occurrences
|
||||||
# see https://golangci-lint.run/usage/linters/#revive for supported rules
|
gosec:
|
||||||
rules:
|
excludes:
|
||||||
- name: receiver-naming
|
- G404 # Use of weak random number generator - lots of FP
|
||||||
severity: warning
|
- G107 # Potential http request -- those are intentional
|
||||||
disabled: false
|
- G306 # G306: Expect WriteFile permissions to be 0600 or less
|
||||||
exclude: [""]
|
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
# default is true. Enables skipping of directories:
|
|
||||||
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
|
|
||||||
exclude-dirs-use-default: true
|
|
||||||
exclude-files:
|
|
||||||
- core/genesis_alloc.go
|
|
||||||
exclude-rules:
|
exclude-rules:
|
||||||
- path: crypto/bn256/cloudflare/optate.go
|
- path: crypto/bn256/cloudflare/optate.go
|
||||||
linters:
|
linters:
|
||||||
- deadcode
|
- deadcode
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- path: crypto/bn256/
|
|
||||||
linters:
|
|
||||||
- revive
|
|
||||||
- path: cmd/utils/flags.go
|
|
||||||
text: "SA1019: cfg.TxLookupLimit is deprecated: use 'TransactionHistory' instead."
|
|
||||||
- path: cmd/utils/flags.go
|
|
||||||
text: "SA1019: ethconfig.Defaults.TxLookupLimit is deprecated: use 'TransactionHistory' instead."
|
|
||||||
- path: internal/build/pgp.go
|
- path: internal/build/pgp.go
|
||||||
text: 'SA1019: "golang.org/x/crypto/openpgp" is deprecated: this package is unmaintained except for security fixes.'
|
text: 'SA1019: package golang.org/x/crypto/openpgp is deprecated'
|
||||||
- path: core/vm/contracts.go
|
- path: core/vm/contracts.go
|
||||||
text: 'SA1019: "golang.org/x/crypto/ripemd160" is deprecated: RIPEMD-160 is a legacy hash and should not be used for new applications.'
|
text: 'SA1019: package golang.org/x/crypto/ripemd160 is deprecated'
|
||||||
|
- path: accounts/usbwallet/trezor.go
|
||||||
|
text: 'SA1019: package github.com/golang/protobuf/proto is deprecated'
|
||||||
|
- path: accounts/usbwallet/trezor/
|
||||||
|
text: 'SA1019: package github.com/golang/protobuf/proto is deprecated'
|
||||||
exclude:
|
exclude:
|
||||||
- 'SA1019: event.TypeMux is deprecated: use Feed'
|
- 'SA1019: event.TypeMux is deprecated: use Feed'
|
||||||
- 'SA1019: strings.Title is deprecated'
|
- 'SA1019: strings.Title is deprecated'
|
||||||
- 'SA1019: strings.Title has been deprecated since Go 1.18 and an alternative has been available since Go 1.0: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.'
|
- 'SA1019: strings.Title has been deprecated since Go 1.18 and an alternative has been available since Go 1.0: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.'
|
||||||
- 'SA1029: should not use built-in type string as key for value'
|
- 'SA1029: should not use built-in type string as key for value'
|
||||||
|
- 'G306: Expect WriteFile permissions to be 0600 or less'
|
||||||
|
|||||||
1
.mailmap
1
.mailmap
@@ -56,6 +56,7 @@ Diederik Loerakker <proto@protolambda.com>
|
|||||||
Dimitry Khokhlov <winsvega@mail.ru>
|
Dimitry Khokhlov <winsvega@mail.ru>
|
||||||
|
|
||||||
Domino Valdano <dominoplural@gmail.com>
|
Domino Valdano <dominoplural@gmail.com>
|
||||||
|
Domino Valdano <dominoplural@gmail.com> <jeff@okcupid.com>
|
||||||
|
|
||||||
Edgar Aroutiounian <edgar.factorial@gmail.com>
|
Edgar Aroutiounian <edgar.factorial@gmail.com>
|
||||||
|
|
||||||
|
|||||||
229
.travis.yml
229
.travis.yml
@@ -5,17 +5,33 @@ jobs:
|
|||||||
allow_failures:
|
allow_failures:
|
||||||
- stage: build
|
- stage: build
|
||||||
os: osx
|
os: osx
|
||||||
|
go: 1.17.x
|
||||||
env:
|
env:
|
||||||
- azure-osx
|
- azure-osx
|
||||||
|
- azure-ios
|
||||||
|
- cocoapods-ios
|
||||||
|
|
||||||
include:
|
include:
|
||||||
# This builder create and push the Docker images for all architectures
|
# This builder only tests code linters on latest version of Go
|
||||||
|
- stage: lint
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
go: 1.18.x
|
||||||
|
env:
|
||||||
|
- lint
|
||||||
|
git:
|
||||||
|
submodules: false # avoid cloning ethereum/tests
|
||||||
|
script:
|
||||||
|
- go run build/ci.go lint
|
||||||
|
|
||||||
|
# These builders create the Docker sub-images for multi-arch push and each
|
||||||
|
# will attempt to push the multi-arch image if they are the last builder
|
||||||
- stage: build
|
- stage: build
|
||||||
if: type = push
|
if: type = push
|
||||||
os: linux
|
os: linux
|
||||||
arch: amd64
|
arch: amd64
|
||||||
dist: focal
|
dist: bionic
|
||||||
go: 1.23.x
|
go: 1.18.x
|
||||||
env:
|
env:
|
||||||
- docker
|
- docker
|
||||||
services:
|
services:
|
||||||
@@ -25,27 +41,69 @@ jobs:
|
|||||||
before_install:
|
before_install:
|
||||||
- export DOCKER_CLI_EXPERIMENTAL=enabled
|
- export DOCKER_CLI_EXPERIMENTAL=enabled
|
||||||
script:
|
script:
|
||||||
- go run build/ci.go dockerx -platform "linux/amd64,linux/arm64,linux/riscv64" -upload ethereum/client-go
|
- go run build/ci.go docker -image -manifest amd64,arm64 -upload ethereum/client-go
|
||||||
|
|
||||||
|
- stage: build
|
||||||
|
if: type = push
|
||||||
|
os: linux
|
||||||
|
arch: arm64
|
||||||
|
dist: bionic
|
||||||
|
go: 1.18.x
|
||||||
|
env:
|
||||||
|
- docker
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
git:
|
||||||
|
submodules: false # avoid cloning ethereum/tests
|
||||||
|
before_install:
|
||||||
|
- export DOCKER_CLI_EXPERIMENTAL=enabled
|
||||||
|
script:
|
||||||
|
- go run build/ci.go docker -image -manifest amd64,arm64 -upload ethereum/client-go
|
||||||
|
|
||||||
|
# This builder does the Ubuntu PPA upload
|
||||||
|
- stage: build
|
||||||
|
if: type = push
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
go: 1.18.x
|
||||||
|
env:
|
||||||
|
- ubuntu-ppa
|
||||||
|
- GO111MODULE=on
|
||||||
|
git:
|
||||||
|
submodules: false # avoid cloning ethereum/tests
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- devscripts
|
||||||
|
- debhelper
|
||||||
|
- dput
|
||||||
|
- fakeroot
|
||||||
|
- python-bzrlib
|
||||||
|
- python-paramiko
|
||||||
|
script:
|
||||||
|
- echo '|1|7SiYPr9xl3uctzovOTj4gMwAC1M=|t6ReES75Bo/PxlOPJ6/GsGbTrM0= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0aKz5UTUndYgIGG7dQBV+HaeuEZJ2xPHo2DS2iSKvUL4xNMSAY4UguNW+pX56nAQmZKIZZ8MaEvSj6zMEDiq6HFfn5JcTlM80UwlnyKe8B8p7Nk06PPQLrnmQt5fh0HmEcZx+JU9TZsfCHPnX7MNz4ELfZE6cFsclClrKim3BHUIGq//t93DllB+h4O9LHjEUsQ1Sr63irDLSutkLJD6RXchjROXkNirlcNVHH/jwLWR5RcYilNX7S5bIkK8NlWPjsn/8Ua5O7I9/YoE97PpO6i73DTGLh5H9JN/SITwCKBkgSDWUt61uPK3Y11Gty7o2lWsBjhBUm2Y38CBsoGmBw==' >> ~/.ssh/known_hosts
|
||||||
|
- go run build/ci.go debsrc -upload ethereum/ethereum -sftp-user geth-ci -signer "Go Ethereum Linux Builder <geth-ci@ethereum.org>"
|
||||||
|
|
||||||
# This builder does the Linux Azure uploads
|
# This builder does the Linux Azure uploads
|
||||||
- stage: build
|
- stage: build
|
||||||
if: type = push
|
if: type = push
|
||||||
os: linux
|
os: linux
|
||||||
dist: focal
|
dist: bionic
|
||||||
sudo: required
|
sudo: required
|
||||||
go: 1.23.x
|
go: 1.18.x
|
||||||
env:
|
env:
|
||||||
- azure-linux
|
- azure-linux
|
||||||
|
- GO111MODULE=on
|
||||||
git:
|
git:
|
||||||
submodules: false # avoid cloning ethereum/tests
|
submodules: false # avoid cloning ethereum/tests
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- gcc-multilib
|
||||||
script:
|
script:
|
||||||
# build amd64
|
# Build for the primary platforms that Trusty can manage
|
||||||
- go run build/ci.go install -dlgo
|
- go run build/ci.go install -dlgo
|
||||||
- go run build/ci.go archive -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
- go run build/ci.go archive -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
||||||
|
|
||||||
# build 386
|
|
||||||
- sudo -E apt-get -yq --no-install-suggests --no-install-recommends install gcc-multilib
|
|
||||||
- git status --porcelain
|
|
||||||
- go run build/ci.go install -dlgo -arch 386
|
- go run build/ci.go install -dlgo -arch 386
|
||||||
- go run build/ci.go archive -arch 386 -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
- go run build/ci.go archive -arch 386 -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
||||||
|
|
||||||
@@ -62,65 +120,115 @@ jobs:
|
|||||||
- go run build/ci.go install -dlgo -arch arm64 -cc aarch64-linux-gnu-gcc
|
- go run build/ci.go install -dlgo -arch arm64 -cc aarch64-linux-gnu-gcc
|
||||||
- go run build/ci.go archive -arch arm64 -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
- go run build/ci.go archive -arch arm64 -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
||||||
|
|
||||||
# This builder does the OSX Azure uploads
|
# This builder does the Android Maven and Azure uploads
|
||||||
- stage: build
|
- stage: build
|
||||||
if: type = push
|
if: type = push
|
||||||
os: osx
|
os: linux
|
||||||
osx_image: xcode14.2
|
dist: bionic
|
||||||
go: 1.23.1 # See https://github.com/ethereum/go-ethereum/pull/30478
|
addons:
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- openjdk-8-jdk
|
||||||
env:
|
env:
|
||||||
- azure-osx
|
- azure-android
|
||||||
git:
|
- maven-android
|
||||||
submodules: false # avoid cloning ethereum/tests
|
- GO111MODULE=on
|
||||||
script:
|
|
||||||
- ln -sf /Users/travis/gopath/bin/go1.23.1 /usr/local/bin/go # Work around travis go-setup bug
|
|
||||||
- go run build/ci.go install -dlgo
|
|
||||||
- go run build/ci.go archive -type tar -signer OSX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
|
||||||
- go run build/ci.go install -dlgo -arch arm64
|
|
||||||
- go run build/ci.go archive -arch arm64 -type tar -signer OSX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
|
||||||
|
|
||||||
# These builders run the tests
|
|
||||||
- stage: build
|
|
||||||
if: type = push
|
|
||||||
os: linux
|
|
||||||
arch: amd64
|
|
||||||
dist: focal
|
|
||||||
go: 1.23.x
|
|
||||||
script:
|
|
||||||
- travis_wait 45 go run build/ci.go test $TEST_PACKAGES
|
|
||||||
|
|
||||||
- stage: build
|
|
||||||
if: type = push
|
|
||||||
os: linux
|
|
||||||
dist: focal
|
|
||||||
go: 1.22.x
|
|
||||||
script:
|
|
||||||
- travis_wait 45 go run build/ci.go test $TEST_PACKAGES
|
|
||||||
|
|
||||||
# This builder does the Ubuntu PPA nightly uploads
|
|
||||||
- stage: build
|
|
||||||
if: type = cron || (type = push && tag ~= /^v[0-9]/)
|
|
||||||
os: linux
|
|
||||||
dist: focal
|
|
||||||
go: 1.23.x
|
|
||||||
env:
|
|
||||||
- ubuntu-ppa
|
|
||||||
git:
|
git:
|
||||||
submodules: false # avoid cloning ethereum/tests
|
submodules: false # avoid cloning ethereum/tests
|
||||||
before_install:
|
before_install:
|
||||||
- sudo -E apt-get -yq --no-install-suggests --no-install-recommends install devscripts debhelper dput fakeroot
|
# Install Android and it's dependencies manually, Travis is stale
|
||||||
|
- export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
|
||||||
|
- curl https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip -o android.zip
|
||||||
|
- unzip -q android.zip -d $HOME/sdk && rm android.zip
|
||||||
|
- mv $HOME/sdk/cmdline-tools $HOME/sdk/latest && mkdir $HOME/sdk/cmdline-tools && mv $HOME/sdk/latest $HOME/sdk/cmdline-tools
|
||||||
|
- export PATH=$PATH:$HOME/sdk/cmdline-tools/latest/bin
|
||||||
|
- export ANDROID_HOME=$HOME/sdk
|
||||||
|
|
||||||
|
- yes | sdkmanager --licenses >/dev/null
|
||||||
|
- sdkmanager "platform-tools" "platforms;android-15" "platforms;android-19" "platforms;android-24" "ndk-bundle"
|
||||||
|
|
||||||
|
# Install Go to allow building with
|
||||||
|
- curl https://dl.google.com/go/go1.18.linux-amd64.tar.gz | tar -xz
|
||||||
|
- export PATH=`pwd`/go/bin:$PATH
|
||||||
|
- export GOROOT=`pwd`/go
|
||||||
|
- export GOPATH=$HOME/go
|
||||||
script:
|
script:
|
||||||
- echo '|1|7SiYPr9xl3uctzovOTj4gMwAC1M=|t6ReES75Bo/PxlOPJ6/GsGbTrM0= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0aKz5UTUndYgIGG7dQBV+HaeuEZJ2xPHo2DS2iSKvUL4xNMSAY4UguNW+pX56nAQmZKIZZ8MaEvSj6zMEDiq6HFfn5JcTlM80UwlnyKe8B8p7Nk06PPQLrnmQt5fh0HmEcZx+JU9TZsfCHPnX7MNz4ELfZE6cFsclClrKim3BHUIGq//t93DllB+h4O9LHjEUsQ1Sr63irDLSutkLJD6RXchjROXkNirlcNVHH/jwLWR5RcYilNX7S5bIkK8NlWPjsn/8Ua5O7I9/YoE97PpO6i73DTGLh5H9JN/SITwCKBkgSDWUt61uPK3Y11Gty7o2lWsBjhBUm2Y38CBsoGmBw==' >> ~/.ssh/known_hosts
|
# Build the Android archive and upload it to Maven Central and Azure
|
||||||
- go run build/ci.go debsrc -upload ethereum/ethereum -sftp-user geth-ci -signer "Go Ethereum Linux Builder <geth-ci@ethereum.org>"
|
- mkdir -p $GOPATH/src/github.com/ethereum
|
||||||
|
- ln -s `pwd` $GOPATH/src/github.com/ethereum/go-ethereum
|
||||||
|
- go run build/ci.go aar -signer ANDROID_SIGNING_KEY -signify SIGNIFY_KEY -deploy https://oss.sonatype.org -upload gethstore/builds
|
||||||
|
|
||||||
|
# This builder does the OSX Azure, iOS CocoaPods and iOS Azure uploads
|
||||||
|
- stage: build
|
||||||
|
if: type = push
|
||||||
|
os: osx
|
||||||
|
go: 1.18.x
|
||||||
|
env:
|
||||||
|
- azure-osx
|
||||||
|
- azure-ios
|
||||||
|
- cocoapods-ios
|
||||||
|
- GO111MODULE=on
|
||||||
|
git:
|
||||||
|
submodules: false # avoid cloning ethereum/tests
|
||||||
|
script:
|
||||||
|
- go run build/ci.go install -dlgo
|
||||||
|
- go run build/ci.go archive -type tar -signer OSX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds
|
||||||
|
|
||||||
|
# Build the iOS framework and upload it to CocoaPods and Azure
|
||||||
|
- gem uninstall cocoapods -a -x
|
||||||
|
- gem install cocoapods
|
||||||
|
|
||||||
|
- mv ~/.cocoapods/repos/master ~/.cocoapods/repos/master.bak
|
||||||
|
- sed -i '.bak' 's/repo.join/!repo.join/g' $(dirname `gem which cocoapods`)/cocoapods/sources_manager.rb
|
||||||
|
- if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then git clone --depth=1 https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/master && pod setup --verbose; fi
|
||||||
|
|
||||||
|
- xctool -version
|
||||||
|
- xcrun simctl list
|
||||||
|
|
||||||
|
# Workaround for https://github.com/golang/go/issues/23749
|
||||||
|
- export CGO_CFLAGS_ALLOW='-fmodules|-fblocks|-fobjc-arc'
|
||||||
|
- go run build/ci.go xcode -signer IOS_SIGNING_KEY -signify SIGNIFY_KEY -deploy trunk -upload gethstore/builds
|
||||||
|
|
||||||
|
# These builders run the tests
|
||||||
|
- stage: build
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
dist: bionic
|
||||||
|
go: 1.18.x
|
||||||
|
env:
|
||||||
|
- GO111MODULE=on
|
||||||
|
script:
|
||||||
|
- go run build/ci.go test -coverage $TEST_PACKAGES
|
||||||
|
|
||||||
|
- stage: build
|
||||||
|
if: type = pull_request
|
||||||
|
os: linux
|
||||||
|
arch: arm64
|
||||||
|
dist: bionic
|
||||||
|
go: 1.18.x
|
||||||
|
env:
|
||||||
|
- GO111MODULE=on
|
||||||
|
script:
|
||||||
|
- go run build/ci.go test -coverage $TEST_PACKAGES
|
||||||
|
|
||||||
|
- stage: build
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
go: 1.17.x
|
||||||
|
env:
|
||||||
|
- GO111MODULE=on
|
||||||
|
script:
|
||||||
|
- go run build/ci.go test -coverage $TEST_PACKAGES
|
||||||
|
|
||||||
# This builder does the Azure archive purges to avoid accumulating junk
|
# This builder does the Azure archive purges to avoid accumulating junk
|
||||||
- stage: build
|
- stage: build
|
||||||
if: type = cron
|
if: type = cron
|
||||||
os: linux
|
os: linux
|
||||||
dist: focal
|
dist: bionic
|
||||||
go: 1.23.x
|
go: 1.18.x
|
||||||
env:
|
env:
|
||||||
- azure-purge
|
- azure-purge
|
||||||
|
- GO111MODULE=on
|
||||||
git:
|
git:
|
||||||
submodules: false # avoid cloning ethereum/tests
|
submodules: false # avoid cloning ethereum/tests
|
||||||
script:
|
script:
|
||||||
@@ -130,9 +238,10 @@ jobs:
|
|||||||
- stage: build
|
- stage: build
|
||||||
if: type = cron
|
if: type = cron
|
||||||
os: linux
|
os: linux
|
||||||
dist: focal
|
dist: bionic
|
||||||
go: 1.23.x
|
go: 1.18.x
|
||||||
env:
|
env:
|
||||||
- racetests
|
- GO111MODULE=on
|
||||||
script:
|
script:
|
||||||
- travis_wait 60 go run build/ci.go test -race $TEST_PACKAGES
|
- go run build/ci.go test -race -coverage $TEST_PACKAGES
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ ARG VERSION=""
|
|||||||
ARG BUILDNUM=""
|
ARG BUILDNUM=""
|
||||||
|
|
||||||
# Build Geth in a stock Go builder container
|
# Build Geth in a stock Go builder container
|
||||||
FROM golang:1.23-alpine AS builder
|
FROM golang:1.18-alpine as builder
|
||||||
|
|
||||||
RUN apk add --no-cache gcc musl-dev linux-headers git
|
RUN apk add --no-cache gcc musl-dev linux-headers git
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/
|
|||||||
EXPOSE 8545 8546 30303 30303/udp
|
EXPOSE 8545 8546 30303 30303/udp
|
||||||
ENTRYPOINT ["geth"]
|
ENTRYPOINT ["geth"]
|
||||||
|
|
||||||
# Add some metadata labels to help programmatic image consumption
|
# Add some metadata labels to help programatic image consumption
|
||||||
ARG COMMIT=""
|
ARG COMMIT=""
|
||||||
ARG VERSION=""
|
ARG VERSION=""
|
||||||
ARG BUILDNUM=""
|
ARG BUILDNUM=""
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ ARG VERSION=""
|
|||||||
ARG BUILDNUM=""
|
ARG BUILDNUM=""
|
||||||
|
|
||||||
# Build Geth in a stock Go builder container
|
# Build Geth in a stock Go builder container
|
||||||
FROM golang:1.23-alpine AS builder
|
FROM golang:1.18-alpine as builder
|
||||||
|
|
||||||
RUN apk add --no-cache gcc musl-dev linux-headers git
|
RUN apk add --no-cache gcc musl-dev linux-headers git
|
||||||
|
|
||||||
@@ -14,13 +14,6 @@ COPY go.sum /go-ethereum/
|
|||||||
RUN cd /go-ethereum && go mod download
|
RUN cd /go-ethereum && go mod download
|
||||||
|
|
||||||
ADD . /go-ethereum
|
ADD . /go-ethereum
|
||||||
|
|
||||||
# This is not strictly necessary, but it matches the "Dockerfile" steps, thus
|
|
||||||
# makes it so that under certain circumstances, the docker layer can be cached,
|
|
||||||
# and the builder can jump to the next (build all) command, with the go cache fully loaded.
|
|
||||||
#
|
|
||||||
RUN cd /go-ethereum && go run build/ci.go install -static ./cmd/geth
|
|
||||||
|
|
||||||
RUN cd /go-ethereum && go run build/ci.go install -static
|
RUN cd /go-ethereum && go run build/ci.go install -static
|
||||||
|
|
||||||
# Pull all binaries into a second stage deploy alpine container
|
# Pull all binaries into a second stage deploy alpine container
|
||||||
@@ -31,7 +24,7 @@ COPY --from=builder /go-ethereum/build/bin/* /usr/local/bin/
|
|||||||
|
|
||||||
EXPOSE 8545 8546 30303 30303/udp
|
EXPOSE 8545 8546 30303 30303/udp
|
||||||
|
|
||||||
# Add some metadata labels to help programmatic image consumption
|
# Add some metadata labels to help programatic image consumption
|
||||||
ARG COMMIT=""
|
ARG COMMIT=""
|
||||||
ARG VERSION=""
|
ARG VERSION=""
|
||||||
ARG BUILDNUM=""
|
ARG BUILDNUM=""
|
||||||
|
|||||||
39
Makefile
39
Makefile
@@ -2,56 +2,49 @@
|
|||||||
# with Go source code. If you know what GOPATH is then you probably
|
# with Go source code. If you know what GOPATH is then you probably
|
||||||
# don't need to bother with make.
|
# don't need to bother with make.
|
||||||
|
|
||||||
.PHONY: geth all test lint fmt clean devtools help
|
.PHONY: geth android ios evm all test clean
|
||||||
|
|
||||||
GOBIN = ./build/bin
|
GOBIN = ./build/bin
|
||||||
GO ?= latest
|
GO ?= latest
|
||||||
GORUN = go run
|
GORUN = env GO111MODULE=on go run
|
||||||
|
|
||||||
#? geth: Build geth.
|
|
||||||
geth:
|
geth:
|
||||||
$(GORUN) build/ci.go install ./cmd/geth
|
$(GORUN) build/ci.go install ./cmd/geth
|
||||||
@echo "Done building."
|
@echo "Done building."
|
||||||
@echo "Run \"$(GOBIN)/geth\" to launch geth."
|
@echo "Run \"$(GOBIN)/geth\" to launch geth."
|
||||||
|
|
||||||
#? all: Build all packages and executables.
|
|
||||||
all:
|
all:
|
||||||
$(GORUN) build/ci.go install
|
$(GORUN) build/ci.go install
|
||||||
|
|
||||||
#? test: Run the tests.
|
android:
|
||||||
|
$(GORUN) build/ci.go aar --local
|
||||||
|
@echo "Done building."
|
||||||
|
@echo "Import \"$(GOBIN)/geth.aar\" to use the library."
|
||||||
|
@echo "Import \"$(GOBIN)/geth-sources.jar\" to add javadocs"
|
||||||
|
@echo "For more info see https://stackoverflow.com/questions/20994336/android-studio-how-to-attach-javadoc"
|
||||||
|
|
||||||
|
ios:
|
||||||
|
$(GORUN) build/ci.go xcode --local
|
||||||
|
@echo "Done building."
|
||||||
|
@echo "Import \"$(GOBIN)/Geth.framework\" to use the library."
|
||||||
|
|
||||||
test: all
|
test: all
|
||||||
$(GORUN) build/ci.go test
|
$(GORUN) build/ci.go test
|
||||||
|
|
||||||
#? lint: Run certain pre-selected linters.
|
|
||||||
lint: ## Run linters.
|
lint: ## Run linters.
|
||||||
$(GORUN) build/ci.go lint
|
$(GORUN) build/ci.go lint
|
||||||
|
|
||||||
#? fmt: Ensure consistent code formatting.
|
|
||||||
fmt:
|
|
||||||
gofmt -s -w $(shell find . -name "*.go")
|
|
||||||
|
|
||||||
#? clean: Clean go cache, built executables, and the auto generated folder.
|
|
||||||
clean:
|
clean:
|
||||||
go clean -cache
|
env GO111MODULE=on go clean -cache
|
||||||
rm -fr build/_workspace/pkg/ $(GOBIN)/*
|
rm -fr build/_workspace/pkg/ $(GOBIN)/*
|
||||||
|
|
||||||
# The devtools target installs tools required for 'go generate'.
|
# The devtools target installs tools required for 'go generate'.
|
||||||
# You need to put $GOBIN (or $GOPATH/bin) in your PATH to use 'go generate'.
|
# You need to put $GOBIN (or $GOPATH/bin) in your PATH to use 'go generate'.
|
||||||
|
|
||||||
#? devtools: Install recommended developer tools.
|
|
||||||
devtools:
|
devtools:
|
||||||
env GOBIN= go install golang.org/x/tools/cmd/stringer@latest
|
env GOBIN= go install golang.org/x/tools/cmd/stringer@latest
|
||||||
env GOBIN= go install github.com/fjl/gencodec@latest
|
env GOBIN= go install github.com/fjl/gencodec@latest
|
||||||
env GOBIN= go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
|
env GOBIN= go install github.com/golang/protobuf/protoc-gen-go@latest
|
||||||
env GOBIN= go install ./cmd/abigen
|
env GOBIN= go install ./cmd/abigen
|
||||||
@type "solc" 2> /dev/null || echo 'Please install solc'
|
@type "solc" 2> /dev/null || echo 'Please install solc'
|
||||||
@type "protoc" 2> /dev/null || echo 'Please install protoc'
|
@type "protoc" 2> /dev/null || echo 'Please install protoc'
|
||||||
|
|
||||||
#? help: Get more info on make commands.
|
|
||||||
help: Makefile
|
|
||||||
@echo ''
|
|
||||||
@echo 'Usage:'
|
|
||||||
@echo ' make [target]'
|
|
||||||
@echo ''
|
|
||||||
@echo 'Targets:'
|
|
||||||
@sed -n 's/^#?//p' $< | column -t -s ':' | sort | sed -e 's/^/ /'
|
|
||||||
|
|||||||
228
README.md
228
README.md
@@ -1,12 +1,12 @@
|
|||||||
## Go Ethereum
|
## Go Ethereum
|
||||||
|
|
||||||
Golang execution layer implementation of the Ethereum protocol.
|
Official Golang implementation of the Ethereum protocol.
|
||||||
|
|
||||||
[](https://pkg.go.dev/github.com/ethereum/go-ethereum?tab=doc)
|
)](https://pkg.go.dev/github.com/ethereum/go-ethereum?tab=doc)
|
||||||
[](https://goreportcard.com/report/github.com/ethereum/go-ethereum)
|
[](https://goreportcard.com/report/github.com/ethereum/go-ethereum)
|
||||||
[](https://app.travis-ci.com/github/ethereum/go-ethereum)
|
[](https://travis-ci.com/ethereum/go-ethereum)
|
||||||
[](https://discord.gg/nthXNEv)
|
[](https://discord.gg/nthXNEv)
|
||||||
|
|
||||||
Automated builds are available for stable releases and the unstable master branch. Binary
|
Automated builds are available for stable releases and the unstable master branch. Binary
|
||||||
@@ -14,9 +14,9 @@ archives are published at https://geth.ethereum.org/downloads/.
|
|||||||
|
|
||||||
## Building the source
|
## Building the source
|
||||||
|
|
||||||
For prerequisites and detailed build instructions please read the [Installation Instructions](https://geth.ethereum.org/docs/getting-started/installing-geth).
|
For prerequisites and detailed build instructions please read the [Installation Instructions](https://geth.ethereum.org/docs/install-and-build/installing-geth).
|
||||||
|
|
||||||
Building `geth` requires both a Go (version 1.22 or later) and a C compiler. You can install
|
Building `geth` requires both a Go (version 1.16 or later) and a C compiler. You can install
|
||||||
them using your favourite package manager. Once the dependencies are installed, run
|
them using your favourite package manager. Once the dependencies are installed, run
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
@@ -34,19 +34,21 @@ make all
|
|||||||
The go-ethereum project comes with several wrappers/executables found in the `cmd`
|
The go-ethereum project comes with several wrappers/executables found in the `cmd`
|
||||||
directory.
|
directory.
|
||||||
|
|
||||||
| Command | Description |
|
| Command | Description |
|
||||||
| :--------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| :-----------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| **`geth`** | Our main Ethereum CLI client. It is the entry point into the Ethereum network (main-, test- or private net), capable of running as a full node (default), archive node (retaining all historical state) or a light node (retrieving data live). It can be used by other processes as a gateway into the Ethereum network via JSON RPC endpoints exposed on top of HTTP, WebSocket and/or IPC transports. `geth --help` and the [CLI page](https://geth.ethereum.org/docs/fundamentals/command-line-options) for command line options. |
|
| **`geth`** | Our main Ethereum CLI client. It is the entry point into the Ethereum network (main-, test- or private net), capable of running as a full node (default), archive node (retaining all historical state) or a light node (retrieving data live). It can be used by other processes as a gateway into the Ethereum network via JSON RPC endpoints exposed on top of HTTP, WebSocket and/or IPC transports. `geth --help` and the [CLI page](https://geth.ethereum.org/docs/interface/command-line-options) for command line options. |
|
||||||
| `clef` | Stand-alone signing tool, which can be used as a backend signer for `geth`. |
|
| `clef` | Stand-alone signing tool, which can be used as a backend signer for `geth`. |
|
||||||
| `devp2p` | Utilities to interact with nodes on the networking layer, without running a full blockchain. |
|
| `devp2p` | Utilities to interact with nodes on the networking layer, without running a full blockchain. |
|
||||||
| `abigen` | Source code generator to convert Ethereum contract definitions into easy-to-use, compile-time type-safe Go packages. It operates on plain [Ethereum contract ABIs](https://docs.soliditylang.org/en/develop/abi-spec.html) with expanded functionality if the contract bytecode is also available. However, it also accepts Solidity source files, making development much more streamlined. Please see our [Native DApps](https://geth.ethereum.org/docs/developers/dapp-developer/native-bindings) page for details. |
|
| `abigen` | Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages. It operates on plain [Ethereum contract ABIs](https://docs.soliditylang.org/en/develop/abi-spec.html) with expanded functionality if the contract bytecode is also available. However, it also accepts Solidity source files, making development much more streamlined. Please see our [Native DApps](https://geth.ethereum.org/docs/dapp/native-bindings) page for details. |
|
||||||
| `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug run`). |
|
| `bootnode` | Stripped down version of our Ethereum client implementation that only takes part in the network node discovery protocol, but does not run any of the higher level application protocols. It can be used as a lightweight bootstrap node to aid in finding peers in private networks. |
|
||||||
| `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user-friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). |
|
| `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug run`). |
|
||||||
|
| `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user-friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). |
|
||||||
|
| `puppeth` | a CLI wizard that aids in creating a new Ethereum network. |
|
||||||
|
|
||||||
## Running `geth`
|
## Running `geth`
|
||||||
|
|
||||||
Going through all the possible command line flags is out of scope here (please consult our
|
Going through all the possible command line flags is out of scope here (please consult our
|
||||||
[CLI Wiki page](https://geth.ethereum.org/docs/fundamentals/command-line-options)),
|
[CLI Wiki page](https://geth.ethereum.org/docs/interface/command-line-options)),
|
||||||
but we've enumerated a few common parameter combos to get you up to speed quickly
|
but we've enumerated a few common parameter combos to get you up to speed quickly
|
||||||
on how you can run your own `geth` instance.
|
on how you can run your own `geth` instance.
|
||||||
|
|
||||||
@@ -54,23 +56,23 @@ on how you can run your own `geth` instance.
|
|||||||
|
|
||||||
Minimum:
|
Minimum:
|
||||||
|
|
||||||
* CPU with 4+ cores
|
* CPU with 2+ cores
|
||||||
* 8GB RAM
|
* 4GB RAM
|
||||||
* 1TB free storage space to sync the Mainnet
|
* 1TB free storage space to sync the Mainnet
|
||||||
* 8 MBit/sec download Internet service
|
* 8 MBit/sec download Internet service
|
||||||
|
|
||||||
Recommended:
|
Recommended:
|
||||||
|
|
||||||
* Fast CPU with 8+ cores
|
* Fast CPU with 4+ cores
|
||||||
* 16GB+ RAM
|
* 16GB+ RAM
|
||||||
* High-performance SSD with at least 1TB of free space
|
* High Performance SSD with at least 1TB free space
|
||||||
* 25+ MBit/sec download Internet service
|
* 25+ MBit/sec download Internet service
|
||||||
|
|
||||||
### Full node on the main Ethereum network
|
### Full node on the main Ethereum network
|
||||||
|
|
||||||
By far the most common scenario is people wanting to simply interact with the Ethereum
|
By far the most common scenario is people wanting to simply interact with the Ethereum
|
||||||
network: create accounts; transfer funds; deploy and interact with contracts. For this
|
network: create accounts; transfer funds; deploy and interact with contracts. For this
|
||||||
particular use case, the user doesn't care about years-old historical data, so we can
|
particular use-case the user doesn't care about years-old historical data, so we can
|
||||||
sync quickly to the current state of the network. To do so:
|
sync quickly to the current state of the network. To do so:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
@@ -81,14 +83,14 @@ This command will:
|
|||||||
* Start `geth` in snap sync mode (default, can be changed with the `--syncmode` flag),
|
* Start `geth` in snap sync mode (default, can be changed with the `--syncmode` flag),
|
||||||
causing it to download more data in exchange for avoiding processing the entire history
|
causing it to download more data in exchange for avoiding processing the entire history
|
||||||
of the Ethereum network, which is very CPU intensive.
|
of the Ethereum network, which is very CPU intensive.
|
||||||
* Start the built-in interactive [JavaScript console](https://geth.ethereum.org/docs/interacting-with-geth/javascript-console),
|
* Start up `geth`'s built-in interactive [JavaScript console](https://geth.ethereum.org/docs/interface/javascript-console),
|
||||||
(via the trailing `console` subcommand) through which you can interact using [`web3` methods](https://github.com/ChainSafe/web3.js/blob/0.20.7/DOCUMENTATION.md)
|
(via the trailing `console` subcommand) through which you can interact using [`web3` methods](https://github.com/ChainSafe/web3.js/blob/0.20.7/DOCUMENTATION.md)
|
||||||
(note: the `web3` version bundled within `geth` is very old, and not up to date with official docs),
|
(note: the `web3` version bundled within `geth` is very old, and not up to date with official docs),
|
||||||
as well as `geth`'s own [management APIs](https://geth.ethereum.org/docs/interacting-with-geth/rpc).
|
as well as `geth`'s own [management APIs](https://geth.ethereum.org/docs/rpc/server).
|
||||||
This tool is optional and if you leave it out you can always attach it to an already running
|
This tool is optional and if you leave it out you can always attach to an already running
|
||||||
`geth` instance with `geth attach`.
|
`geth` instance with `geth attach`.
|
||||||
|
|
||||||
### A Full node on the Holesky test network
|
### A Full node on the Görli test network
|
||||||
|
|
||||||
Transitioning towards developers, if you'd like to play around with creating Ethereum
|
Transitioning towards developers, if you'd like to play around with creating Ethereum
|
||||||
contracts, you almost certainly would like to do that without any real money involved until
|
contracts, you almost certainly would like to do that without any real money involved until
|
||||||
@@ -97,31 +99,53 @@ network, you want to join the **test** network with your node, which is fully eq
|
|||||||
the main network, but with play-Ether only.
|
the main network, but with play-Ether only.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ geth --holesky console
|
$ geth --goerli console
|
||||||
```
|
```
|
||||||
|
|
||||||
The `console` subcommand has the same meaning as above and is equally
|
The `console` subcommand has the exact same meaning as above and they are equally
|
||||||
useful on the testnet too.
|
useful on the testnet too. Please, see above for their explanations if you've skipped here.
|
||||||
|
|
||||||
Specifying the `--holesky` flag, however, will reconfigure your `geth` instance a bit:
|
Specifying the `--goerli` flag, however, will reconfigure your `geth` instance a bit:
|
||||||
|
|
||||||
* Instead of connecting to the main Ethereum network, the client will connect to the Holesky
|
* Instead of connecting the main Ethereum network, the client will connect to the Görli
|
||||||
test network, which uses different P2P bootnodes, different network IDs and genesis
|
test network, which uses different P2P bootnodes, different network IDs and genesis
|
||||||
states.
|
states.
|
||||||
* Instead of using the default data directory (`~/.ethereum` on Linux for example), `geth`
|
* Instead of using the default data directory (`~/.ethereum` on Linux for example), `geth`
|
||||||
will nest itself one level deeper into a `holesky` subfolder (`~/.ethereum/holesky` on
|
will nest itself one level deeper into a `goerli` subfolder (`~/.ethereum/goerli` on
|
||||||
Linux). Note, on OSX and Linux this also means that attaching to a running testnet node
|
Linux). Note, on OSX and Linux this also means that attaching to a running testnet node
|
||||||
requires the use of a custom endpoint since `geth attach` will try to attach to a
|
requires the use of a custom endpoint since `geth attach` will try to attach to a
|
||||||
production node endpoint by default, e.g.,
|
production node endpoint by default, e.g.,
|
||||||
`geth attach <datadir>/holesky/geth.ipc`. Windows users are not affected by
|
`geth attach <datadir>/goerli/geth.ipc`. Windows users are not affected by
|
||||||
this.
|
this.
|
||||||
|
|
||||||
*Note: Although some internal protective measures prevent transactions from
|
*Note: Although there are some internal protective measures to prevent transactions from
|
||||||
crossing over between the main network and test network, you should always
|
crossing over between the main network and test network, you should make sure to always
|
||||||
use separate accounts for play and real money. Unless you manually move
|
use separate accounts for play-money and real-money. Unless you manually move
|
||||||
accounts, `geth` will by default correctly separate the two networks and will not make any
|
accounts, `geth` will by default correctly separate the two networks and will not make any
|
||||||
accounts available between them.*
|
accounts available between them.*
|
||||||
|
|
||||||
|
### Full node on the Rinkeby test network
|
||||||
|
|
||||||
|
Go Ethereum also supports connecting to the older proof-of-authority based test network
|
||||||
|
called [*Rinkeby*](https://www.rinkeby.io) which is operated by members of the community.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ geth --rinkeby console
|
||||||
|
```
|
||||||
|
|
||||||
|
### Full node on the Ropsten test network
|
||||||
|
|
||||||
|
In addition to Görli and Rinkeby, Geth also supports the ancient Ropsten testnet. The
|
||||||
|
Ropsten test network is based on the Ethash proof-of-work consensus algorithm. As such,
|
||||||
|
it has certain extra overhead and is more susceptible to reorganization attacks due to the
|
||||||
|
network's low difficulty/security.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ geth --ropsten console
|
||||||
|
```
|
||||||
|
|
||||||
|
*Note: Older Geth configurations store the Ropsten database in the `testnet` subdirectory.*
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
As an alternative to passing the numerous flags to the `geth` binary, you can also pass a
|
As an alternative to passing the numerous flags to the `geth` binary, you can also pass a
|
||||||
@@ -131,13 +155,15 @@ configuration file via:
|
|||||||
$ geth --config /path/to/your_config.toml
|
$ geth --config /path/to/your_config.toml
|
||||||
```
|
```
|
||||||
|
|
||||||
To get an idea of how the file should look like you can use the `dumpconfig` subcommand to
|
To get an idea how the file should look like you can use the `dumpconfig` subcommand to
|
||||||
export your existing configuration:
|
export your existing configuration:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ geth --your-favourite-flags dumpconfig
|
$ geth --your-favourite-flags dumpconfig
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Note: This works only with `geth` v1.6.0 and above.*
|
||||||
|
|
||||||
#### Docker quick start
|
#### Docker quick start
|
||||||
|
|
||||||
One of the quickest ways to get Ethereum up and running on your machine is by using
|
One of the quickest ways to get Ethereum up and running on your machine is by using
|
||||||
@@ -149,7 +175,7 @@ docker run -d --name ethereum-node -v /Users/alice/ethereum:/root \
|
|||||||
ethereum/client-go
|
ethereum/client-go
|
||||||
```
|
```
|
||||||
|
|
||||||
This will start `geth` in snap-sync mode with a DB memory allowance of 1GB, as the
|
This will start `geth` in snap-sync mode with a DB memory allowance of 1GB just as the
|
||||||
above command does. It will also create a persistent volume in your home directory for
|
above command does. It will also create a persistent volume in your home directory for
|
||||||
saving your blockchain as well as map the default ports. There is also an `alpine` tag
|
saving your blockchain as well as map the default ports. There is also an `alpine` tag
|
||||||
available for a slim version of the image.
|
available for a slim version of the image.
|
||||||
@@ -163,7 +189,7 @@ accessible from the outside.
|
|||||||
As a developer, sooner rather than later you'll want to start interacting with `geth` and the
|
As a developer, sooner rather than later you'll want to start interacting with `geth` and the
|
||||||
Ethereum network via your own programs and not manually through the console. To aid
|
Ethereum network via your own programs and not manually through the console. To aid
|
||||||
this, `geth` has built-in support for a JSON-RPC based APIs ([standard APIs](https://ethereum.github.io/execution-apis/api-documentation/)
|
this, `geth` has built-in support for a JSON-RPC based APIs ([standard APIs](https://ethereum.github.io/execution-apis/api-documentation/)
|
||||||
and [`geth` specific APIs](https://geth.ethereum.org/docs/interacting-with-geth/rpc)).
|
and [`geth` specific APIs](https://geth.ethereum.org/docs/rpc/server)).
|
||||||
These can be exposed via HTTP, WebSockets and IPC (UNIX sockets on UNIX based
|
These can be exposed via HTTP, WebSockets and IPC (UNIX sockets on UNIX based
|
||||||
platforms, and named pipes on Windows).
|
platforms, and named pipes on Windows).
|
||||||
|
|
||||||
@@ -183,8 +209,9 @@ HTTP based JSON-RPC API options:
|
|||||||
* `--ws.addr` WS-RPC server listening interface (default: `localhost`)
|
* `--ws.addr` WS-RPC server listening interface (default: `localhost`)
|
||||||
* `--ws.port` WS-RPC server listening port (default: `8546`)
|
* `--ws.port` WS-RPC server listening port (default: `8546`)
|
||||||
* `--ws.api` API's offered over the WS-RPC interface (default: `eth,net,web3`)
|
* `--ws.api` API's offered over the WS-RPC interface (default: `eth,net,web3`)
|
||||||
* `--ws.origins` Origins from which to accept WebSocket requests
|
* `--ws.origins` Origins from which to accept websockets requests
|
||||||
* `--ipcdisable` Disable the IPC-RPC server
|
* `--ipcdisable` Disable the IPC-RPC server
|
||||||
|
* `--ipcapi` API's offered over the IPC-RPC interface (default: `admin,debug,eth,miner,net,personal,txpool,web3`)
|
||||||
* `--ipcpath` Filename for IPC socket/pipe within the datadir (explicit paths escape it)
|
* `--ipcpath` Filename for IPC socket/pipe within the datadir (explicit paths escape it)
|
||||||
|
|
||||||
You'll need to use your own programming environments' capabilities (libraries, tools, etc) to
|
You'll need to use your own programming environments' capabilities (libraries, tools, etc) to
|
||||||
@@ -203,18 +230,121 @@ APIs!**
|
|||||||
Maintaining your own private network is more involved as a lot of configurations taken for
|
Maintaining your own private network is more involved as a lot of configurations taken for
|
||||||
granted in the official networks need to be manually set up.
|
granted in the official networks need to be manually set up.
|
||||||
|
|
||||||
Unfortunately since [the Merge](https://ethereum.org/en/roadmap/merge/) it is no longer possible
|
#### Defining the private genesis state
|
||||||
to easily set up a network of geth nodes without also setting up a corresponding beacon chain.
|
|
||||||
|
|
||||||
There are three different solutions depending on your use case:
|
First, you'll need to create the genesis state of your networks, which all nodes need to be
|
||||||
|
aware of and agree upon. This consists of a small JSON file (e.g. call it `genesis.json`):
|
||||||
|
|
||||||
* If you are looking for a simple way to test smart contracts from go in your CI, you can use the [Simulated Backend](https://geth.ethereum.org/docs/developers/dapp-developer/native-bindings#blockchain-simulator).
|
```json
|
||||||
* If you want a convenient single node environment for testing, you can use our [Dev Mode](https://geth.ethereum.org/docs/developers/dapp-developer/dev-mode).
|
{
|
||||||
* If you are looking for a multiple node test network, you can set one up quite easily with [Kurtosis](https://geth.ethereum.org/docs/fundamentals/kurtosis).
|
"config": {
|
||||||
|
"chainId": <arbitrary positive integer>,
|
||||||
|
"homesteadBlock": 0,
|
||||||
|
"eip150Block": 0,
|
||||||
|
"eip155Block": 0,
|
||||||
|
"eip158Block": 0,
|
||||||
|
"byzantiumBlock": 0,
|
||||||
|
"constantinopleBlock": 0,
|
||||||
|
"petersburgBlock": 0,
|
||||||
|
"istanbulBlock": 0,
|
||||||
|
"berlinBlock": 0,
|
||||||
|
"londonBlock": 0
|
||||||
|
},
|
||||||
|
"alloc": {},
|
||||||
|
"coinbase": "0x0000000000000000000000000000000000000000",
|
||||||
|
"difficulty": "0x20000",
|
||||||
|
"extraData": "",
|
||||||
|
"gasLimit": "0x2fefd8",
|
||||||
|
"nonce": "0x0000000000000042",
|
||||||
|
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
|
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
|
"timestamp": "0x00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The above fields should be fine for most purposes, although we'd recommend changing
|
||||||
|
the `nonce` to some random value so you prevent unknown remote nodes from being able
|
||||||
|
to connect to you. If you'd like to pre-fund some accounts for easier testing, create
|
||||||
|
the accounts and populate the `alloc` field with their addresses.
|
||||||
|
|
||||||
|
```json
|
||||||
|
"alloc": {
|
||||||
|
"0x0000000000000000000000000000000000000001": {
|
||||||
|
"balance": "111111111"
|
||||||
|
},
|
||||||
|
"0x0000000000000000000000000000000000000002": {
|
||||||
|
"balance": "222222222"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
With the genesis state defined in the above JSON file, you'll need to initialize **every**
|
||||||
|
`geth` node with it prior to starting it up to ensure all blockchain parameters are correctly
|
||||||
|
set:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ geth init path/to/genesis.json
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Creating the rendezvous point
|
||||||
|
|
||||||
|
With all nodes that you want to run initialized to the desired genesis state, you'll need to
|
||||||
|
start a bootstrap node that others can use to find each other in your network and/or over
|
||||||
|
the internet. The clean way is to configure and run a dedicated bootnode:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ bootnode --genkey=boot.key
|
||||||
|
$ bootnode --nodekey=boot.key
|
||||||
|
```
|
||||||
|
|
||||||
|
With the bootnode online, it will display an [`enode` URL](https://ethereum.org/en/developers/docs/networking-layer/network-addresses/#enode)
|
||||||
|
that other nodes can use to connect to it and exchange peer information. Make sure to
|
||||||
|
replace the displayed IP address information (most probably `[::]`) with your externally
|
||||||
|
accessible IP to get the actual `enode` URL.
|
||||||
|
|
||||||
|
*Note: You could also use a full-fledged `geth` node as a bootnode, but it's the less
|
||||||
|
recommended way.*
|
||||||
|
|
||||||
|
#### Starting up your member nodes
|
||||||
|
|
||||||
|
With the bootnode operational and externally reachable (you can try
|
||||||
|
`telnet <ip> <port>` to ensure it's indeed reachable), start every subsequent `geth`
|
||||||
|
node pointed to the bootnode for peer discovery via the `--bootnodes` flag. It will
|
||||||
|
probably also be desirable to keep the data directory of your private network separated, so
|
||||||
|
do also specify a custom `--datadir` flag.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ geth --datadir=path/to/custom/data/folder --bootnodes=<bootnode-enode-url-from-above>
|
||||||
|
```
|
||||||
|
|
||||||
|
*Note: Since your network will be completely cut off from the main and test networks, you'll
|
||||||
|
also need to configure a miner to process transactions and create new blocks for you.*
|
||||||
|
|
||||||
|
#### Running a private miner
|
||||||
|
|
||||||
|
Mining on the public Ethereum network is a complex task as it's only feasible using GPUs,
|
||||||
|
requiring an OpenCL or CUDA enabled `ethminer` instance. For information on such a
|
||||||
|
setup, please consult the [EtherMining subreddit](https://www.reddit.com/r/EtherMining/)
|
||||||
|
and the [ethminer](https://github.com/ethereum-mining/ethminer) repository.
|
||||||
|
|
||||||
|
In a private network setting, however a single CPU miner instance is more than enough for
|
||||||
|
practical purposes as it can produce a stable stream of blocks at the correct intervals
|
||||||
|
without needing heavy resources (consider running on a single thread, no need for multiple
|
||||||
|
ones either). To start a `geth` instance for mining, run it with all your usual flags, extended
|
||||||
|
by:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ geth <usual-flags> --mine --miner.threads=1 --miner.etherbase=0x0000000000000000000000000000000000000000
|
||||||
|
```
|
||||||
|
|
||||||
|
Which will start mining blocks and transactions on a single CPU thread, crediting all
|
||||||
|
proceedings to the account specified by `--miner.etherbase`. You can further tune the mining
|
||||||
|
by changing the default gas limit blocks converge to (`--miner.targetgaslimit`) and the price
|
||||||
|
transactions are accepted at (`--miner.gasprice`).
|
||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
|
|
||||||
Thank you for considering helping out with the source code! We welcome contributions
|
Thank you for considering to help out with the source code! We welcome contributions
|
||||||
from anyone on the internet, and are grateful for even the smallest of fixes!
|
from anyone on the internet, and are grateful for even the smallest of fixes!
|
||||||
|
|
||||||
If you'd like to contribute to go-ethereum, please fork, fix, commit and send a pull request
|
If you'd like to contribute to go-ethereum, please fork, fix, commit and send a pull request
|
||||||
@@ -234,22 +364,16 @@ Please make sure your contributions adhere to our coding guidelines:
|
|||||||
* Commit messages should be prefixed with the package(s) they modify.
|
* Commit messages should be prefixed with the package(s) they modify.
|
||||||
* E.g. "eth, rpc: make trace configs optional"
|
* E.g. "eth, rpc: make trace configs optional"
|
||||||
|
|
||||||
Please see the [Developers' Guide](https://geth.ethereum.org/docs/developers/geth-developer/dev-guide)
|
Please see the [Developers' Guide](https://geth.ethereum.org/docs/developers/devguide)
|
||||||
for more details on configuring your environment, managing project dependencies, and
|
for more details on configuring your environment, managing project dependencies, and
|
||||||
testing procedures.
|
testing procedures.
|
||||||
|
|
||||||
### Contributing to geth.ethereum.org
|
|
||||||
|
|
||||||
For contributions to the [go-ethereum website](https://geth.ethereum.org), please checkout and raise pull requests against the `website` branch.
|
|
||||||
For more detailed instructions please see the `website` branch [README](https://github.com/ethereum/go-ethereum/tree/website#readme) or the
|
|
||||||
[contributing](https://geth.ethereum.org/docs/developers/geth-developer/contributing) page of the website.
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
The go-ethereum library (i.e. all code outside of the `cmd` directory) is licensed under the
|
The go-ethereum library (i.e. all code outside of the `cmd` directory) is licensed under the
|
||||||
[GNU Lesser General Public License v3.0](https://www.gnu.org/licenses/lgpl-3.0.en.html),
|
[GNU Lesser General Public License v3.0](https://www.gnu.org/licenses/lgpl-3.0.en.html),
|
||||||
also included in our repository in the `COPYING.LESSER` file.
|
also included in our repository in the `COPYING.LESSER` file.
|
||||||
|
|
||||||
The go-ethereum binaries (i.e. all code inside of the `cmd` directory) are licensed under the
|
The go-ethereum binaries (i.e. all code inside of the `cmd` directory) is licensed under the
|
||||||
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html), also
|
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html), also
|
||||||
included in our repository in the `COPYING` file.
|
included in our repository in the `COPYING` file.
|
||||||
|
|||||||
206
SECURITY.md
206
SECURITY.md
@@ -29,69 +29,147 @@ Fingerprint: `AE96 ED96 9E47 9B00 84F3 E17F E88D 3334 FA5F 6A0A`
|
|||||||
|
|
||||||
```
|
```
|
||||||
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||||
|
Version: SKS 1.1.6
|
||||||
|
Comment: Hostname: pgp.mit.edu
|
||||||
|
|
||||||
mQINBFgl3tgBEAC8A1tUBkD9YV+eLrOmtgy+/JS/H9RoZvkg3K1WZ8IYfj6iIRaY
|
mQINBFgl3tgBEAC8A1tUBkD9YV+eLrOmtgy+/JS/H9RoZvkg3K1WZ8IYfj6iIRaYneAk3Bp1
|
||||||
neAk3Bp182GUPVz/zhKr2g0tMXIScDR3EnaDsY+Qg+JqQl8NOG+Cikr1nnkG2on9
|
82GUPVz/zhKr2g0tMXIScDR3EnaDsY+Qg+JqQl8NOG+Cikr1nnkG2on9L8c8yiqry1ZTCmYM
|
||||||
L8c8yiqry1ZTCmYMqCa2acTFqnyuXJ482aZNtB4QG2BpzfhW4k8YThpegk/EoRUi
|
qCa2acTFqnyuXJ482aZNtB4QG2BpzfhW4k8YThpegk/EoRUim+y7buJDtoNf7YILlhDQXN8q
|
||||||
m+y7buJDtoNf7YILlhDQXN8qlHB02DWOVUihph9tUIFsPK6BvTr9SIr/eG6j6k0b
|
lHB02DWOVUihph9tUIFsPK6BvTr9SIr/eG6j6k0bfUo9pexOn7LS4SojoJmsm/5dp6AoKlac
|
||||||
fUo9pexOn7LS4SojoJmsm/5dp6AoKlac48cZU5zwR9AYcq/nvkrfmf2WkObg/xRd
|
48cZU5zwR9AYcq/nvkrfmf2WkObg/xRdEvKZzn05jRopmAIwmoC3CiLmqCHPmT5a29vEob/y
|
||||||
EvKZzn05jRopmAIwmoC3CiLmqCHPmT5a29vEob/yPFE335k+ujjZCPOu7OwjzDk7
|
PFE335k+ujjZCPOu7OwjzDk7M0zMSfnNfDq8bXh16nn+ueBxJ0NzgD1oC6c2PhM+XRQCXCho
|
||||||
M0zMSfnNfDq8bXh16nn+ueBxJ0NzgD1oC6c2PhM+XRQCXChoyI8vbfp4dGvCvYqv
|
yI8vbfp4dGvCvYqvQAE1bWjqnumZ/7vUPgZN6gDfiAzG2mUxC2SeFBhacgzDvtQls+uuvm+F
|
||||||
QAE1bWjqnumZ/7vUPgZN6gDfiAzG2mUxC2SeFBhacgzDvtQls+uuvm+FnQOUgg2H
|
nQOUgg2Hh8x2zgoZ7kqV29wjaUPFREuew7e+Th5BxielnzOfVycVXeSuvvIn6cd3g/s8mX1c
|
||||||
h8x2zgoZ7kqV29wjaUPFREuew7e+Th5BxielnzOfVycVXeSuvvIn6cd3g/s8mX1c
|
2kLSXJR7+KdWDrIrR5Az0kwAqFZt6B6QTlDrPswu3mxsm5TzMbny0PsbL/HBM+GZEZCjMXxB
|
||||||
2kLSXJR7+KdWDrIrR5Az0kwAqFZt6B6QTlDrPswu3mxsm5TzMbny0PsbL/HBM+GZ
|
8bqV2eSaktjnSlUNX1VXxyOxXA+ZG2jwpr51egi57riVRXokrQARAQABtDRFdGhlcmV1bSBG
|
||||||
EZCjMXxB8bqV2eSaktjnSlUNX1VXxyOxXA+ZG2jwpr51egi57riVRXokrQARAQAB
|
b3VuZGF0aW9uIEJ1ZyBCb3VudHkgPGJvdW50eUBldGhlcmV1bS5vcmc+iQIcBBEBCAAGBQJa
|
||||||
tDRFdGhlcmV1bSBGb3VuZGF0aW9uIEJ1ZyBCb3VudHkgPGJvdW50eUBldGhlcmV1
|
FCY6AAoJEHoMA3Q0/nfveH8P+gJBPo9BXZL8isUfbUWjwLi81Yi70hZqIJUnz64SWTqBzg5b
|
||||||
bS5vcmc+iQJVBBMBCAA/AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgBYhBK6W
|
mCZ69Ji5637THsxQetS2ARabz0DybQ779FhD/IWnqV9T3KuBM/9RzJtuhLzKCyMrAINPMo28
|
||||||
7ZaeR5sAhPPhf+iNMzT6X2oKBQJl2LD9BQkRdTklAAoJEOiNMzT6X2oKYYYQALkV
|
rKWdunHHarpuR4m3tL2zWJkle5QVYb+vkZXJJE98PJw+N4IYeKKeCs2ubeqZu636GA0sMzzB
|
||||||
wJjWYoVoMuw9D1ybQo4Sqyp6D/XYHXSpqZDO9RlADQisYBfuO7EW75evgZ+54Ajc
|
Jn3m/dRRA2va+/zzbr6F6b51ynzbMxWKTsJnstjC8gs8EeI+Zcd6otSyelLtCUkk3h5sTvpV
|
||||||
8gZ2BUkFcSR9z2t0TEkUyjmPDZsaElTTP2Boa2GG5pyziEM6t1cMMY1sP1aotx9H
|
Wv67BNSU0BYsMkxyFi9PUyy07Wixgeas89K5jG1oOtDva/FkpRHrTE/WA5OXDRcLrHJM+SwD
|
||||||
DYwCeMmDv0wTMi6v0C6+/in2hBxbGALRbQKWKd/5ss4OEPe37hG9zAJcBYZg2tes
|
CwqcLQqJd09NxwUW1iKeBmPptTiOGu1Gv2o7aEyoaWrHRBO7JuYrQrj6q2B3H1Je0zjAd2qt
|
||||||
O7ceg7LHZpNC1zvMUrBY6os74FJ437f8bankqvVE83/dvTcCDhMsei9LiWS2uo26
|
09ni2bLwLn4LA+VDpprNTO+eZDprv09s2oFSU6NwziHybovu0y7X4pADGkK2evOM7c86PohX
|
||||||
qiyqeR9lZEj8W5F6UgkQH+UOhamJ9UB3N/h//ipKrwtiv0+jQm9oNG7aIAi3UJgD
|
QRQ1M1T16xLj6wP8/Ykwl6v/LUk7iDPXP3GPILnh4YOkwBR3DsCOPn8098xy7FxEELmupRzt
|
||||||
CvSod87H0l7/U8RWzyam/r8eh4KFM75hIVtqEy5jFV2z7x2SibXQi7WRfAysjFLp
|
Cj9oC7YAoweeShgUjBPzb+nGY1m6OcFfbUPBgFyMMfwF6joHbiVIO+39+Ut2g2ysZa7KF+yp
|
||||||
/li8ff6kLDR9IMATuMSF7Ol0O9JMRfSPjRZRtVOwYVIBla3BhfMrjvMMcZMAy/qS
|
XqVDqyEkYXsOLb25OC7brt8IJEPgBPwcHK5GNag6RfLxnQV+iVZ9KNH1yQgSiQI+BBMBAgAo
|
||||||
DWx2iFYDMGsswv7hp3lsFOaa1ju95ClZZk3q/z7u5gH7LFAxR0jPaW48ay3CFylW
|
AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAUCWglh+gUJBaNgWAAKCRDojTM0+l9qCgQ2
|
||||||
sDpQpO1DWb9uXBdhOU+MN18uSjqzocga3Wz2C8jhWRvxyFf3SNIybm3zk6W6IIoy
|
D/4udJpV4zGIZW1yNaVvtd3vfKsTLi7GIRJLUBqVb2Yx/uhnN8jTl/tAhCVosCQ1pzvi9kMl
|
||||||
6KmwSRZ30oxizy6zMYw1qJE89zjjumzlZAm0R/Q4Ui+WJhlSyrYbqzqdxYuLgdEL
|
s8qO1vu2kw5EWFFkwK96roI8pTql3VIjwhRVQrCkR7oAk/eUd1U/nt2q6J4UTYeVgqbq4dsI
|
||||||
lgKfbv9/t8tNXGGSuCe5L7quOv9k7l2+QmLlg+SJtDlFdGhlcmV1bSBGb3VuZGF0
|
ZZTRyPJMD667YpuAIcaah+w9j/E5xksYQdMeprnDrQkkBCb4FIMqfDzBPKvEa8DcQr949K85
|
||||||
aW9uIFNlY3VyaXR5IFRlYW0gPHNlY3VyaXR5QGV0aGVyZXVtLm9yZz6JAlUEEwEI
|
kxhr6LDq9i5l4Egxt2JdH8DaR4GLca6+oHy0MyPs/bZOsfmZUObfM2oZgPpqYM96JanhzO1j
|
||||||
AD8CGwMGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAFiEErpbtlp5HmwCE8+F/6I0z
|
dpnItyBii2pc+kNx5nMOf4eikE/MBv+WUJ0TttWzApGGmFUzDhtuEvRH9NBjtJ/pMrYspIGu
|
||||||
NPpfagoFAmXYsP4FCRF1OSUACgkQ6I0zNPpfagoUGA/+LVzXUJrsfi8+ADMF1hru
|
O/QNY5KKOKQTvVIlwGcm8dTsSkqtBDSUwZyWbfKfKOI1/RhM9dC3gj5/BOY57DYYV4rdTK01
|
||||||
wFDcY1r+vM4Ovbk1NhCc/DnV5VG40j5FiQpE81BNiH59sYeZkQm9jFbwevK7Zpuq
|
ZtYjuhdfs2bhuP1uF/cgnSSZlv8azvf7Egh7tHPnYxvLjfq1bJAhCIX0hNg0a81/ndPAEFky
|
||||||
RZaG2WGiwU/11xrt5/Qjq7T+vEtd94546kFcBnP8uexZqP4dTi4LHa2on8aRbwzN
|
fSko+JPKvdSvsUcSi2QQ4U2HX//jNBjXRfG4F0utgbJnhXzEckz6gqt7wSDZH2oddVuO8Ssc
|
||||||
7RjCpCQhy1TUuk47dyOR1y3ZHrpTwkHpuhwgffaWtxgSyCMYz7fsd5Ukh3eE+Ani
|
T7sK+CdXthSKnRyuI+sGUpG+6glpKWIfYkWFKNZWuQ+YUatY3QEDHXTIioycSmV8p4d/g/0S
|
||||||
90CIUieve2U3o+WPxBD9PRaIPg6LmBhfGxGvC/6tqY9W3Z9xEOVDxC4wdYppQzsg
|
V6TegidLxY8bXMkbqz+3n6FArRffv5MH7qt3cYkCPgQTAQIAKAUCWCXhOwIbAwUJAeEzgAYL
|
||||||
Pg7bNnVmlFWHsEk8FuMfY8nTqY3/ojhJxikWKz2V3Y2AbsLEXCvrEg6b4FvmsS97
|
CQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQ6I0zNPpfagrN/w/+Igp3vtYdNunikw3yHnYf
|
||||||
8ifEBbFXU8hvMSpMLtO7vLamWyOHq41IXWH6HLNLhDfDzTfpAJ8iYDKGj72YsMzF
|
Jkm0MmaMDUM9mtsaXVN6xb9n25N3Xa3GWCpmdsbYZ8334tI/oQ4/NHq/bEI5WFH5F1aFkMkm
|
||||||
0fIjPa6mniMB2RmREAM0Jas3M/6DUw1EzwK1iQofIBoCRPIkR5mxmzjcRB6tVdQa
|
5AJVLuUkipCtmCZ5NkbRPJA9l0uNUUE6uuFXBhf4ddu7jb0jMetRF/kifJHVCCo5fISUNhLp
|
||||||
on20/9YTKKBUQAdK0OWW8j1euuULDgNdkN2LBXdQLy/JcQiggU8kOCKL/Lmj5HWP
|
7bwcWq9qgDQNZNYMOo4s9WX5Tl+5x4gTZdd2/cAYt49h/wnkw+huM+Jm0GojpLqIQ1jZiffm
|
||||||
FNT9rYfnjmCuux3UfJGfhPryujEA0CdIfq1Qf4ldOVzpWYjsMn+yQxAQTorAzF3z
|
otf5rF4L+JhIIdW0W4IIh1v9BhHVllXw+z9oj0PALstT5h8/DuKoIiirFJ4DejU85GR1KKAS
|
||||||
iYddP2cw/Nvookay8xywKJnDsaRaWqdQ8Ceox3qSB4LCjQRNR5c3HfvGm3EBdEyI
|
DeO19G/lSpWj1rSgFv2N2gAOxq0X+BbQTua2jdcY6JpHR4H1JJ2wzfHsHPgDQcgY1rGlmjVF
|
||||||
zEEpjZ6GHa05DCajqKjtjlm5Ag0EWCXe2AEQAJuCrodM3mAQGLSWQP8xp8ieY2L7
|
aqU73WV4/hzXc/HshK/k4Zd8uD4zypv6rFsZ3UemK0aL2zXLVpV8SPWQ61nS03x675SmDlYr
|
||||||
n1TmBEZiqTjpaV9GOEe51eMOmAPSWiUZviFiie2QxopGUKDZG+CO+Tdm97Q8paMr
|
A80ENfdqvsn00JQuBVIv4Tv0Ub7NfDraDGJCst8rObjBT/0vnBWTBCebb2EsnS2iStIFkWdz
|
||||||
DuCvxgFr18wVjwGEBcjfY53Ij2sWHERkV9YB/ApWZPX0F14BBEW9x937zDx/VdVz
|
/WXs4L4Yzre1iJwqRjiuqahZR5jHsjAUf2a0O29HVHE7zlFtCFmLPClml2lGQfQOpm5klGZF
|
||||||
7N11QswkUFOv7EoOUhFbBOR0s9B5ZuOjR4eX+Di24uIutPFVuePbpt/7b7UNsz/D
|
rmvus+qZ9rt35UgWHPZezykkwtWrFOwspwuCWaPDto6tgbRJZ4ftitpdYYM3dKW9IGJXBwrt
|
||||||
lVq/M+uS+Ieq8p79A/+BrLhANWJa8WAtv3SJ18Ach2w+B+WnRUNLmtUcUvoPvetJ
|
BQrMsu+lp0vDF+yJAlUEEwEIAD8CGwMGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAFiEErpbt
|
||||||
F0hGjcjxzyZig2NJHhcO6+A6QApb0tHem+i4UceOnoWvQZ6xFuttvYQbrqI+xH30
|
lp5HmwCE8+F/6I0zNPpfagoFAmEAEJwFCQycmLgACgkQ6I0zNPpfagpWoBAAhOcbMAUw6Zt0
|
||||||
xDsWogv1Uwc+baa1H5e9ucqQfatjISpoxtJ2Tb2MZqmQBaV7iwiFIqTvj0Di0aQe
|
GYzT3sR5/c0iatezPzXEXJf9ebzR8M5uPElXcxcnMx1dvXZmGPXPJKCPa99WCu1NZYy8F+Wj
|
||||||
XTwpnY32joat9R6E/9XZ4ktqmHYOKgUvUfFGvKsrLzRBAoswlF6TGKCryCt5bzEH
|
GTOY9tfIkvSxhys1p/giPAmvid6uQmD+bz7ivktnyzCkDWfMA+l8lsCSEqVlaq6y5T+a6SWB
|
||||||
jO5/0yS6i75Ec2ajw95seMWy0uKCIPr/M/Z77i1SatPT8lMY5KGgYyXxG3RVHF08
|
6TzC2S0MPb/RrC/7DpwyrNYWumvyVJh09adm1Mw/UGgst/sZ8eMaRYEd3X0yyT1CBpX4zp2E
|
||||||
iYq6f7gs5dt87ECs5KRjqLfn6CyCSRLLWBMkTQFjdL1q5Pr5iuCVj9NY9D0gnFZU
|
qQj9IEOTizvzv1x2jkHe5ZUeU3+nTBNlhSA+WFHUi0pfBdo2qog3Mv2EC1P2qMKoSdD5tPbA
|
||||||
4qVP7dYinnAm7ZsEpDjbRUuoNjOShbK16X9szUAJS2KkyIhV5Sza4WJGOnMDVbLR
|
zql1yKoHHnXOMsqdftGwbiv2sYXWvrYvmaCd3Ys/viOyt3HOy9uV2ZEtBd9Yqo9x/NZj8QMA
|
||||||
Aco9N1K4aUk9Gt9xABEBAAGJAjwEGAEIACYCGwwWIQSulu2WnkebAITz4X/ojTM0
|
nY5k8jjrIXbUC89MqrJsQ6xxWQIg5ikMT7DvY0Ln89ev4oJyVvwIQAwCm4jUzFNm9bZLYDOP
|
||||||
+l9qCgUCZdiwoAUJEXU4yAAKCRDojTM0+l9qCj2PD/9pbIPRMZtvKIIE+OhOAl/s
|
5lGJCV7tF5NYVU7NxNM8vescKc40mVNK/pygS5mxhK9QYOUjZsIv8gddrl1TkqrFMuxFnTyN
|
||||||
qfZJXByAM40ELpUhDHqwbOplIEyvXtWfQ5c+kWlG/LPJ2CgLkHyFQDn6tuat82rH
|
WvzE29wFu/n4N1DkF+ZBqS70SlRvB+Hjz5LrDgEzF1Wf1eA/wq1dZbvMjjDVIc2VGlYp8Cp2
|
||||||
/5VoZyxp16CBAwEgYdycOr9hMGSVKNIJDfV9Bu6VtZnn6fa/swBzGE7eVpXsIoNr
|
8ob23c1seTtYXTNYgSR5go4EpH+xi+bIWv01bQQ9xGwBbT5sm4WUeWOcmX4QewzLZ3T/wK9+
|
||||||
jeqsogBtzLecG1oHMXRMq7oUqu9c6VNoCx2uxRUOeWW8YuP7h9j6mxIuKKbcpmQ5
|
N4Ye/hmU9O34FwWJOY58EIe0OUV0aGVyZXVtIEZvdW5kYXRpb24gU2VjdXJpdHkgVGVhbSA8
|
||||||
RSLNEhJZJsMMFLf8RAQPXmshG1ZixY2ZliNe/TTm6eEfFCw0KcQxoX9LmurLWE9w
|
c2VjdXJpdHlAZXRoZXJldW0ub3JnPokCHAQRAQgABgUCWhQmOgAKCRB6DAN0NP5372LSEACT
|
||||||
dIKgn1/nQ04GFnmtcq3hVxY/m9BvzY1jmZXNd4TdpfrPXhi0W/GDn53ERFPJmw5L
|
wZk1TASWZj5QF7rmkIM1GEyBxLE+PundNcMgM9Ktj1315ED8SmiukNI4knVS1MY99OIgXhQl
|
||||||
F8ogxzD/ekxzyd9nCCgtzkamtBKDJk35x/MoVWMLjD5k6P+yW7YY4xMQliSJHKss
|
D1foF2GKdTomrwwC4012zTNyUYCY60LnPZ6Z511HG+rZgZtZrbkz0IiUpwAlhGQND77lBqem
|
||||||
leLnaPpgDBi4KPtLxPswgFqObcy4TNse07rFO4AyHf11FBwMTEfuODCOMnQTpi3z
|
J3K+CFX2XpDA/ojui/kqrY4cwMT5P8xPJkwgpRgw/jgdcZyJTsXdHblV9IGU4H1Vd1SgcfAf
|
||||||
Zx6KxvS3BEY36abjvwrqsmt8dJ/+/QXT0e82fo2kJ65sXIszez3e0VUZ8KrMp+wd
|
Db3YxDUlBtzlp0NkZqxen8irLIXUQvsfuIfRUbUSkWoK/n3U/gOCajAe8ZNF07iX4OWjH4Sw
|
||||||
X0GWYWAfqXws6HrQFYfIpEE0Vz9gXDxEOTFZ2FoVIvIHyRfyDrAIz3wZLmnLGk1h
|
NDA841WhFWcGE+d8+pfMVfPASU3UPKH72uw86b2VgR46Av6voyMFd1pj+yCA+YAhJuOpV4yL
|
||||||
l3CDjHF0Wigv0CacIQ1V1aYp3NhIVwAvShQ+qS5nFgik6UZnjjWibobOm3yQDzll
|
QaGg2Z0kVOjuNWK/kBzp1F58DWGh4YBatbhE/UyQOqAAtR7lNf0M3QF9AdrHTxX8oZeqVW3V
|
||||||
6F7hEeTW+gnXEI2gPjfb5w==
|
Fmi2mk0NwCIUv8SSrZr1dTchp04OtyXe5gZBXSfzncCSRQIUDC8OgNWaOzAaUmK299v4bvye
|
||||||
=b5eA
|
uSCxOysxC7Q1hZtjzFPKdljS81mRlYeUL4fHlJU9R57bg8mriSXLmn7eKrSEDm/EG5T8nRx7
|
||||||
-----END PGP PUBLIC KEY BLOCK-----
|
TgX2MqJs8sWFxD2+bboVEu75yuFmZ//nmCBApAit9Hr2/sCshGIEpa9MQ6xJCYUxyqeJH+Cc
|
||||||
|
Aja0UfXhnK2uvPClpJLIl4RE3gm4OXeE1IkCPgQTAQIAKAIbAwYLCQgHAwIGFQgCCQoLBBYC
|
||||||
|
AwECHgECF4AFAloJYfoFCQWjYFgACgkQ6I0zNPpfagr4MQ//cfp3GSbSG8dkqgctW67Fy7cQ
|
||||||
|
diiTmx3cwxY+tlI3yrNmdjtrIQMzGdqtY6LNz7aN87F8mXNf+DyVHX9+wd1Y8U+E+hVCTzKC
|
||||||
|
sefUfxTz6unD9TTcGqaoelgIPMn4IiKz1RZE6eKpfDWe6q78W1Y6x1bE0qGNSjqT/QSxpezF
|
||||||
|
E/OAm/t8RRxVxDtqz8LfH2zLea5zaC+ADj8EqgY9vX9TQa4DyVV8MgOyECCCadJQCD5O5hIA
|
||||||
|
B2gVDWwrAUw+KBwskXZ7Iq4reJTKLEmt5z9zgtJ/fABwaCFt66ojwg0/RjbO9cNA3ZwHLGwU
|
||||||
|
C6hkb6bRzIoZoMfYxVS84opiqf/Teq+t/XkBYCxbSXTJDA5MKjcVuw3N6YKWbkGP/EfQThe7
|
||||||
|
BfAKFwwIw5YmsWjHK8IQj6R6hBxzTz9rz8y1Lu8EAAFfA7OJKaboI2qbOlauH98OuOUmVtr1
|
||||||
|
TczHO+pTcgWVN0ytq2/pX5KBf4vbmULNbg3HFRq+gHx8CW+jyXGkcqjbgU/5FwtDxeqRTdGJ
|
||||||
|
SyBGNBEU6pBNolyynyaKaaJjJ/biY27pvjymL5rlz95BH3Dn16Z4RRmqwlT6eq/wFYginujg
|
||||||
|
CCE1icqOSE+Vjl7V8tV8AcgANkXKdbBE+Q8wlKsGI/kS1w4XFAYcaNHFT8qNeS8TSFXFhvU8
|
||||||
|
HylYxO79t56JAj4EEwECACgFAlgl3tgCGwMFCQHhM4AGCwkIBwMCBhUIAgkKCwQWAgMBAh4B
|
||||||
|
AheAAAoJEOiNMzT6X2oKmUMP/0hnaL6bVyepAq2LIdvIUbHfagt/Oo/KVfZs4bkM+xJOitJR
|
||||||
|
0kwZV9PTihXFdzhL/YNWc2+LtEBtKItqkJZKmWC0E6OPXGVuU6hfFPebuzVccYJfm0Q3Ej19
|
||||||
|
VJI9Uomf59Bpak8HYyEED7WVQjoYn7XVPsonwus/9+LDX+c5vutbrUdbjga3KjHbewD93X4O
|
||||||
|
wVVoXyHEmU2Plyg8qvzFbNDylCWO7N2McO6SN6+7DitGZGr2+jO+P2R4RT1cnl2V3IRVcWZ0
|
||||||
|
OTspPSnRGVr2fFiHN/+v8G/wHPLQcJZFvYPfUGNdcYbTmhWdiY0bEYXFiNrgzCCsyad7eKUR
|
||||||
|
WN9QmxqmyqLDjUEDJCAh19ES6Vg3tqGwXk+uNUCoF30ga0TxQt6UXZJDEQFAGeASQ/RqE/q1
|
||||||
|
EAuLv8IGM8o7IqKO2pWfLuqsY6dTbKBwDzz9YOJt7EOGuPPQbHxaYStTushZmJnm7hi8lhVG
|
||||||
|
jT7qsEJdE95Il+I/mHWnXsCevaXjZugBiyV9yvOq4Hwwe2s1zKfrnQ4u0cadvGAh2eIqum7M
|
||||||
|
Y3o6nD47aJ3YmEPX/WnhI56bACa2GmWvUwjI4c0/er3esSPYnuHnM9L8Am4qQwMVSmyU80tC
|
||||||
|
MI7A9e13Mvv+RRkYFLJ7PVPdNpbW5jqX1doklFpKf6/XM+B+ngYneU+zgCUBiQJVBBMBCAA/
|
||||||
|
AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgBYhBK6W7ZaeR5sAhPPhf+iNMzT6X2oKBQJh
|
||||||
|
ABCQBQkMnJi4AAoJEOiNMzT6X2oKAv0P+gJ3twBp5efNWyVLcIg4h4cOo9uD0NPvz8/fm2gX
|
||||||
|
FoOJL3MeigtPuSVfE9kuTaTuRbArzuFtdvH6G/kcRQvOlO4zyiIRHCk1gDHoIvvtn6RbRhVm
|
||||||
|
/Xo4uGIsFHst7n4A7BjicwEK5Op6Ih5Hoq19xz83YSBgBVk2fYEJIRyJiKFbyPjH0eSYe8v+
|
||||||
|
Ra5/F85ugLx1P6mMVkW+WPzULns89riW7BGTnZmXFHZp8nO2pkUlcI7F3KRG7l4kmlC50ox6
|
||||||
|
DiG/6AJCVulbAClky9C68TmJ/R1RazQxU/9IqVywsydq66tbJQbm5Z7GEti0C5jjbSRJL2oT
|
||||||
|
1xC7Rilr85PMREkPL3vegJdgj5PKlffZ/MocD/0EohiQ7wFpejFD4iTljeh0exRUwCRb6655
|
||||||
|
9ib34JSQgU8Hl4JJu+mEgd9v0ZHD0/1mMD6fnAR84zca+O3cdASbnQmzTOKcGzLIrkE8TEnU
|
||||||
|
+2UZ8Ol7SAAqmBgzY1gKOilUho6dkyCAwNL+QDpvrITDPLEFPsjyB/M2KudZSVEn+Rletju1
|
||||||
|
qkMW31qFMNlsbwzMZw+0USeGcs31Cs0B2/WQsro99CExlhS9auUFkmoVjJmYVTIYOM0zuPa4
|
||||||
|
OyGspqPhRu5hEsmMDPDWD7Aad5k4GTqogQNnuKyRliZjXXrDZqFD5nfsJSL8Ky/sJGEMuQIN
|
||||||
|
BFgl3tgBEACbgq6HTN5gEBi0lkD/MafInmNi+59U5gRGYqk46WlfRjhHudXjDpgD0lolGb4h
|
||||||
|
YontkMaKRlCg2Rvgjvk3Zve0PKWjKw7gr8YBa9fMFY8BhAXI32OdyI9rFhxEZFfWAfwKVmT1
|
||||||
|
9BdeAQRFvcfd+8w8f1XVc+zddULMJFBTr+xKDlIRWwTkdLPQeWbjo0eHl/g4tuLiLrTxVbnj
|
||||||
|
26bf+2+1DbM/w5VavzPrkviHqvKe/QP/gay4QDViWvFgLb90idfAHIdsPgflp0VDS5rVHFL6
|
||||||
|
D73rSRdIRo3I8c8mYoNjSR4XDuvgOkAKW9LR3pvouFHHjp6Fr0GesRbrbb2EG66iPsR99MQ7
|
||||||
|
FqIL9VMHPm2mtR+XvbnKkH2rYyEqaMbSdk29jGapkAWle4sIhSKk749A4tGkHl08KZ2N9o6G
|
||||||
|
rfUehP/V2eJLaph2DioFL1HxRryrKy80QQKLMJRekxigq8greW8xB4zuf9Mkuou+RHNmo8Pe
|
||||||
|
bHjFstLigiD6/zP2e+4tUmrT0/JTGOShoGMl8Rt0VRxdPImKun+4LOXbfOxArOSkY6i35+gs
|
||||||
|
gkkSy1gTJE0BY3S9auT6+YrglY/TWPQ9IJxWVOKlT+3WIp5wJu2bBKQ420VLqDYzkoWytel/
|
||||||
|
bM1ACUtipMiIVeUs2uFiRjpzA1Wy0QHKPTdSuGlJPRrfcQARAQABiQIlBBgBAgAPAhsMBQJa
|
||||||
|
CWIIBQkFo2BYAAoJEOiNMzT6X2oKgSwQAKKs7BGF8TyZeIEO2EUK7R2bdQDCdSGZY06tqLFg
|
||||||
|
3IHMGxDMb/7FVoa2AEsFgv6xpoebxBB5zkhUk7lslgxvKiSLYjxfNjTBltfiFJ+eQnf+OTs8
|
||||||
|
KeR51lLa66rvIH2qUzkNDCCTF45H4wIDpV05AXhBjKYkrDCrtey1rQyFp5fxI+0IQ1UKKXvz
|
||||||
|
ZK4GdxhxDbOUSd38MYy93nqcmclGSGK/gF8XiyuVjeifDCM6+T1NQTX0K9lneidcqtBDvlgg
|
||||||
|
JTLJtQPO33o5EHzXSiud+dKth1uUhZOFEaYRZoye1YE3yB0TNOOE8fXlvu8iuIAMBSDL9ep6
|
||||||
|
sEIaXYwoD60I2gHdWD0lkP0DOjGQpi4ouXM3Edsd5MTi0MDRNTij431kn8T/D0LCgmoUmYYM
|
||||||
|
BgbwFhXr67axPZlKjrqR0z3F/Elv0ZPPcVg1tNznsALYQ9Ovl6b5M3cJ5GapbbvNWC7yEE1q
|
||||||
|
Scl9HiMxjt/H6aPastH63/7wcN0TslW+zRBy05VNJvpWGStQXcngsSUeJtI1Gd992YNjUJq4
|
||||||
|
/Lih6Z1TlwcFVap+cTcDptoUvXYGg/9mRNNPZwErSfIJ0Ibnx9wPVuRN6NiCLOt2mtKp2F1p
|
||||||
|
M6AOQPpZ85vEh6I8i6OaO0w/Z0UHBwvpY6jDUliaROsWUQsqz78Z34CVj4cy6vPW2EF4iQIl
|
||||||
|
BBgBAgAPBQJYJd7YAhsMBQkB4TOAAAoJEOiNMzT6X2oKTjgP/1ojCVyGyvHMLUgnX0zwrR5Q
|
||||||
|
1M5RKFz6kHwKjODVLR3Isp8I935oTQt3DY7yFDI4t0GqbYRQMtxcNEb7maianhK2trCXfhPs
|
||||||
|
6/L04igjDf5iTcmzamXN6xnh5xkz06hZJJCMuu4MvKxC9MQHCVKAwjswl/9H9JqIBXAY3E2l
|
||||||
|
LpX5P+5jDZuPxS86p3+k4Rrdp9KTGXjiuEleM3zGlz5BLWydqovOck7C2aKh27ETFpDYY0z3
|
||||||
|
yQ5AsPJyk1rAr0wrH6+ywmwWlzuQewavnrLnJ2M8iMFXpIhyHeEIU/f7o8f+dQk72rZ9CGzd
|
||||||
|
cqig2za/BS3zawZWgbv2vB2elNsIllYLdir45jxBOxx2yvJvEuu4glz78y4oJTCTAYAbMlle
|
||||||
|
5gVdPkVcGyvvVS9tinnSaiIzuvWrYHKWll1uYPm2Q1CDs06P5I7bUGAXpgQLUh/XQguy/0sX
|
||||||
|
GWqW3FS5JzP+XgcR/7UASvwBdHylubKbeqEpB7G1s+m+8C67qOrc7EQv3Jmy1YDOkhEyNig1
|
||||||
|
rmjplLuir3tC1X+D7dHpn7NJe7nMwFx2b2MpMkLA9jPPAGPp/ekcu5sxCe+E0J/4UF++K+CR
|
||||||
|
XIxgtzU2UJfp8p9x+ygbx5qHinR0tVRdIzv3ZnGsXrfxnWfSOaB582cU3VRN9INzHHax8ETa
|
||||||
|
QVDnGO5uQa+FiQI8BBgBCAAmAhsMFiEErpbtlp5HmwCE8+F/6I0zNPpfagoFAmEAELYFCQyc
|
||||||
|
mN4ACgkQ6I0zNPpfagoqAQ/+MnDjBx8JWMd/XjeFoYKx/Oo0ntkInV+ME61JTBls4PdVk+TB
|
||||||
|
8PWZdPQHw9SnTvRmykFeznXIRzuxkowjrZYXdPXBxY2b1WyD5V3Ati1TM9vqpaR4osyPs2xy
|
||||||
|
I4dzDssh9YvUsIRL99O04/65lGiYeBNuACq+yK/7nD/ErzBkDYJHhMCdadbVWUACxvVIDvro
|
||||||
|
yQeVLKMsHqMCd8BTGD7VDs79NXskPnN77pAFnkzS4Z2b8SNzrlgTc5pUiuZHIXPIpEYmsYzh
|
||||||
|
ucTU6uI3dN1PbSFHK5tG2pHb4ZrPxY3L20Dgc2Tfu5/SDApZzwvvKTqjdO891MEJ++H+ssOz
|
||||||
|
i4O1UeWKs9owWttan9+PI47ozBSKOTxmMqLSQ0f56Np9FJsV0ilGxRKfjhzJ4KniOMUBA7mP
|
||||||
|
+m+TmXfVtthJred4sHlJMTJNpt+sCcT6wLMmyc3keIEAu33gsJj3LTpkEA2q+V+ZiP6Q8HRB
|
||||||
|
402ITklABSArrPSE/fQU9L8hZ5qmy0Z96z0iyILgVMLuRCCfQOMWhwl8yQWIIaf1yPI07xur
|
||||||
|
epy6lH7HmxjjOR7eo0DaSxQGQpThAtFGwkWkFh8yki8j3E42kkrxvEyyYZDXn2YcI3bpqhJx
|
||||||
|
PtwCMZUJ3kc/skOrs6bOI19iBNaEoNX5Dllm7UHjOgWNDQkcCuOCxucKano=
|
||||||
|
=arte
|
||||||
|
-----END PGP PUBLIC KEY BLOCK------
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -22,14 +22,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The ABI holds information about a contract's context and available
|
// The ABI holds information about a contract's context and available
|
||||||
// invocable methods. It will allow you to type check function calls and
|
// invokable methods. It will allow you to type check function calls and
|
||||||
// packs data accordingly.
|
// packs data accordingly.
|
||||||
type ABI struct {
|
type ABI struct {
|
||||||
Constructor Method
|
Constructor Method
|
||||||
@@ -84,22 +83,19 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
|
|||||||
|
|
||||||
func (abi ABI) getArguments(name string, data []byte) (Arguments, error) {
|
func (abi ABI) getArguments(name string, data []byte) (Arguments, error) {
|
||||||
// since there can't be naming collisions with contracts and events,
|
// since there can't be naming collisions with contracts and events,
|
||||||
// we need to decide whether we're calling a method, event or an error
|
// we need to decide whether we're calling a method or an event
|
||||||
var args Arguments
|
var args Arguments
|
||||||
if method, ok := abi.Methods[name]; ok {
|
if method, ok := abi.Methods[name]; ok {
|
||||||
if len(data)%32 != 0 {
|
if len(data)%32 != 0 {
|
||||||
return nil, fmt.Errorf("abi: improperly formatted output: %q - Bytes: %+v", data, data)
|
return nil, fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(data), data)
|
||||||
}
|
}
|
||||||
args = method.Outputs
|
args = method.Outputs
|
||||||
}
|
}
|
||||||
if event, ok := abi.Events[name]; ok {
|
if event, ok := abi.Events[name]; ok {
|
||||||
args = event.Inputs
|
args = event.Inputs
|
||||||
}
|
}
|
||||||
if err, ok := abi.Errors[name]; ok {
|
|
||||||
args = err.Inputs
|
|
||||||
}
|
|
||||||
if args == nil {
|
if args == nil {
|
||||||
return nil, fmt.Errorf("abi: could not locate named method, event or error: %s", name)
|
return nil, fmt.Errorf("abi: could not locate named method or event: %s", name)
|
||||||
}
|
}
|
||||||
return args, nil
|
return args, nil
|
||||||
}
|
}
|
||||||
@@ -226,17 +222,6 @@ func (abi *ABI) EventByID(topic common.Hash) (*Event, error) {
|
|||||||
return nil, fmt.Errorf("no event with id: %#x", topic.Hex())
|
return nil, fmt.Errorf("no event with id: %#x", topic.Hex())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorByID looks up an error by the 4-byte id,
|
|
||||||
// returns nil if none found.
|
|
||||||
func (abi *ABI) ErrorByID(sigdata [4]byte) (*Error, error) {
|
|
||||||
for _, errABI := range abi.Errors {
|
|
||||||
if bytes.Equal(errABI.ID[:4], sigdata[:]) {
|
|
||||||
return &errABI, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("no error with id: %#x", sigdata[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasFallback returns an indicator whether a fallback function is included.
|
// HasFallback returns an indicator whether a fallback function is included.
|
||||||
func (abi *ABI) HasFallback() bool {
|
func (abi *ABI) HasFallback() bool {
|
||||||
return abi.Fallback.Type == Fallback
|
return abi.Fallback.Type == Fallback
|
||||||
@@ -250,65 +235,21 @@ func (abi *ABI) HasReceive() bool {
|
|||||||
// revertSelector is a special function selector for revert reason unpacking.
|
// revertSelector is a special function selector for revert reason unpacking.
|
||||||
var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]
|
var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]
|
||||||
|
|
||||||
// panicSelector is a special function selector for panic reason unpacking.
|
|
||||||
var panicSelector = crypto.Keccak256([]byte("Panic(uint256)"))[:4]
|
|
||||||
|
|
||||||
// panicReasons map is for readable panic codes
|
|
||||||
// see this linkage for the details
|
|
||||||
// https://docs.soliditylang.org/en/v0.8.21/control-structures.html#panic-via-assert-and-error-via-require
|
|
||||||
// the reason string list is copied from ether.js
|
|
||||||
// https://github.com/ethers-io/ethers.js/blob/fa3a883ff7c88611ce766f58bdd4b8ac90814470/src.ts/abi/interface.ts#L207-L218
|
|
||||||
var panicReasons = map[uint64]string{
|
|
||||||
0x00: "generic panic",
|
|
||||||
0x01: "assert(false)",
|
|
||||||
0x11: "arithmetic underflow or overflow",
|
|
||||||
0x12: "division or modulo by zero",
|
|
||||||
0x21: "enum overflow",
|
|
||||||
0x22: "invalid encoded storage byte array accessed",
|
|
||||||
0x31: "out-of-bounds array access; popping on an empty array",
|
|
||||||
0x32: "out-of-bounds access of an array or bytesN",
|
|
||||||
0x41: "out of memory",
|
|
||||||
0x51: "uninitialized function",
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnpackRevert resolves the abi-encoded revert reason. According to the solidity
|
// UnpackRevert resolves the abi-encoded revert reason. According to the solidity
|
||||||
// spec https://solidity.readthedocs.io/en/latest/control-structures.html#revert,
|
// spec https://solidity.readthedocs.io/en/latest/control-structures.html#revert,
|
||||||
// the provided revert reason is abi-encoded as if it were a call to function
|
// the provided revert reason is abi-encoded as if it were a call to a function
|
||||||
// `Error(string)` or `Panic(uint256)`. So it's a special tool for it.
|
// `Error(string)`. So it's a special tool for it.
|
||||||
func UnpackRevert(data []byte) (string, error) {
|
func UnpackRevert(data []byte) (string, error) {
|
||||||
if len(data) < 4 {
|
if len(data) < 4 {
|
||||||
return "", errors.New("invalid data for unpacking")
|
return "", errors.New("invalid data for unpacking")
|
||||||
}
|
}
|
||||||
switch {
|
if !bytes.Equal(data[:4], revertSelector) {
|
||||||
case bytes.Equal(data[:4], revertSelector):
|
|
||||||
typ, err := NewType("string", "", nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:])
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return unpacked[0].(string), nil
|
|
||||||
case bytes.Equal(data[:4], panicSelector):
|
|
||||||
typ, err := NewType("uint256", "", nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:])
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
pCode := unpacked[0].(*big.Int)
|
|
||||||
// uint64 safety check for future
|
|
||||||
// but the code is not bigger than MAX(uint64) now
|
|
||||||
if pCode.IsUint64() {
|
|
||||||
if reason, ok := panicReasons[pCode.Uint64()]; ok {
|
|
||||||
return reason, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("unknown panic code: %#x", pCode), nil
|
|
||||||
default:
|
|
||||||
return "", errors.New("invalid data for unpacking")
|
return "", errors.New("invalid data for unpacking")
|
||||||
}
|
}
|
||||||
|
typ, _ := NewType("string", "", nil)
|
||||||
|
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:])
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return unpacked[0].(string), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/internal/testrand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const jsondata = `
|
const jsondata = `
|
||||||
@@ -121,7 +120,6 @@ var methods = map[string]Method{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReader(t *testing.T) {
|
func TestReader(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi := ABI{
|
abi := ABI{
|
||||||
Methods: methods,
|
Methods: methods,
|
||||||
}
|
}
|
||||||
@@ -153,7 +151,6 @@ func TestReader(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidABI(t *testing.T) {
|
func TestInvalidABI(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
json := `[{ "type" : "function", "name" : "", "constant" : fals }]`
|
json := `[{ "type" : "function", "name" : "", "constant" : fals }]`
|
||||||
_, err := JSON(strings.NewReader(json))
|
_, err := JSON(strings.NewReader(json))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -168,12 +165,10 @@ func TestInvalidABI(t *testing.T) {
|
|||||||
|
|
||||||
// TestConstructor tests a constructor function.
|
// TestConstructor tests a constructor function.
|
||||||
// The test is based on the following contract:
|
// The test is based on the following contract:
|
||||||
//
|
// contract TestConstructor {
|
||||||
// contract TestConstructor {
|
// constructor(uint256 a, uint256 b) public{}
|
||||||
// constructor(uint256 a, uint256 b) public{}
|
|
||||||
// }
|
// }
|
||||||
func TestConstructor(t *testing.T) {
|
func TestConstructor(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
json := `[{ "inputs": [{"internalType": "uint256","name": "a","type": "uint256" },{ "internalType": "uint256","name": "b","type": "uint256"}],"stateMutability": "nonpayable","type": "constructor"}]`
|
json := `[{ "inputs": [{"internalType": "uint256","name": "a","type": "uint256" },{ "internalType": "uint256","name": "b","type": "uint256"}],"stateMutability": "nonpayable","type": "constructor"}]`
|
||||||
method := NewMethod("", "", Constructor, "nonpayable", false, false, []Argument{{"a", Uint256, false}, {"b", Uint256, false}}, nil)
|
method := NewMethod("", "", Constructor, "nonpayable", false, false, []Argument{{"a", Uint256, false}, {"b", Uint256, false}}, nil)
|
||||||
// Test from JSON
|
// Test from JSON
|
||||||
@@ -203,7 +198,6 @@ func TestConstructor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTestNumbers(t *testing.T) {
|
func TestTestNumbers(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(jsondata))
|
abi, err := JSON(strings.NewReader(jsondata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -241,7 +235,6 @@ func TestTestNumbers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMethodSignature(t *testing.T) {
|
func TestMethodSignature(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
m := NewMethod("foo", "foo", Function, "", false, false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil)
|
m := NewMethod("foo", "foo", Function, "", false, false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil)
|
||||||
exp := "foo(string,string)"
|
exp := "foo(string,string)"
|
||||||
if m.Sig != exp {
|
if m.Sig != exp {
|
||||||
@@ -280,7 +273,6 @@ func TestMethodSignature(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOverloadedMethodSignature(t *testing.T) {
|
func TestOverloadedMethodSignature(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
json := `[{"constant":true,"inputs":[{"name":"i","type":"uint256"},{"name":"j","type":"uint256"}],"name":"foo","outputs":[],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"i","type":"uint256"}],"name":"foo","outputs":[],"payable":false,"stateMutability":"pure","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"i","type":"uint256"}],"name":"bar","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"i","type":"uint256"},{"indexed":false,"name":"j","type":"uint256"}],"name":"bar","type":"event"}]`
|
json := `[{"constant":true,"inputs":[{"name":"i","type":"uint256"},{"name":"j","type":"uint256"}],"name":"foo","outputs":[],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"i","type":"uint256"}],"name":"foo","outputs":[],"payable":false,"stateMutability":"pure","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"i","type":"uint256"}],"name":"bar","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"i","type":"uint256"},{"indexed":false,"name":"j","type":"uint256"}],"name":"bar","type":"event"}]`
|
||||||
abi, err := JSON(strings.NewReader(json))
|
abi, err := JSON(strings.NewReader(json))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -304,7 +296,6 @@ func TestOverloadedMethodSignature(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomErrors(t *testing.T) {
|
func TestCustomErrors(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
json := `[{ "inputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ],"name": "MyError", "type": "error"} ]`
|
json := `[{ "inputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ],"name": "MyError", "type": "error"} ]`
|
||||||
abi, err := JSON(strings.NewReader(json))
|
abi, err := JSON(strings.NewReader(json))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -318,40 +309,7 @@ func TestCustomErrors(t *testing.T) {
|
|||||||
check("MyError", "MyError(uint256)")
|
check("MyError", "MyError(uint256)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomErrorUnpackIntoInterface(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
errorName := "MyError"
|
|
||||||
json := fmt.Sprintf(`[{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"balance","type":"uint256"}],"name":"%s","type":"error"}]`, errorName)
|
|
||||||
abi, err := JSON(strings.NewReader(json))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
type MyError struct {
|
|
||||||
Sender common.Address
|
|
||||||
Balance *big.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
sender := testrand.Address()
|
|
||||||
balance := new(big.Int).SetBytes(testrand.Bytes(8))
|
|
||||||
encoded, err := abi.Errors[errorName].Inputs.Pack(sender, balance)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
result := MyError{}
|
|
||||||
err = abi.UnpackIntoInterface(&result, errorName, encoded)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result.Sender != sender {
|
|
||||||
t.Errorf("expected %x got %x", sender, result.Sender)
|
|
||||||
}
|
|
||||||
if result.Balance.Cmp(balance) != 0 {
|
|
||||||
t.Errorf("expected %v got %v", balance, result.Balance)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMultiPack(t *testing.T) {
|
func TestMultiPack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(jsondata))
|
abi, err := JSON(strings.NewReader(jsondata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -389,7 +347,6 @@ func ExampleJSON() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInputVariableInputLength(t *testing.T) {
|
func TestInputVariableInputLength(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[
|
const definition = `[
|
||||||
{ "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
|
{ "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
|
||||||
{ "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
|
{ "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
|
||||||
@@ -518,7 +475,6 @@ func TestInputVariableInputLength(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(jsondata))
|
abi, err := JSON(strings.NewReader(jsondata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -693,7 +649,6 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultFunctionParsing(t *testing.T) {
|
func TestDefaultFunctionParsing(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[{ "name" : "balance", "type" : "function" }]`
|
const definition = `[{ "name" : "balance", "type" : "function" }]`
|
||||||
|
|
||||||
abi, err := JSON(strings.NewReader(definition))
|
abi, err := JSON(strings.NewReader(definition))
|
||||||
@@ -707,7 +662,6 @@ func TestDefaultFunctionParsing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBareEvents(t *testing.T) {
|
func TestBareEvents(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[
|
const definition = `[
|
||||||
{ "type" : "event", "name" : "balance" },
|
{ "type" : "event", "name" : "balance" },
|
||||||
{ "type" : "event", "name" : "anon", "anonymous" : true},
|
{ "type" : "event", "name" : "anon", "anonymous" : true},
|
||||||
@@ -770,21 +724,17 @@ func TestBareEvents(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestUnpackEvent is based on this contract:
|
// TestUnpackEvent is based on this contract:
|
||||||
//
|
// contract T {
|
||||||
// contract T {
|
// event received(address sender, uint amount, bytes memo);
|
||||||
// event received(address sender, uint amount, bytes memo);
|
// event receivedAddr(address sender);
|
||||||
// event receivedAddr(address sender);
|
// function receive(bytes memo) external payable {
|
||||||
// function receive(bytes memo) external payable {
|
// received(msg.sender, msg.value, memo);
|
||||||
// received(msg.sender, msg.value, memo);
|
// receivedAddr(msg.sender);
|
||||||
// receivedAddr(msg.sender);
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
|
||||||
//
|
|
||||||
// When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
|
// When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
|
||||||
//
|
// receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
|
||||||
// receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
|
|
||||||
func TestUnpackEvent(t *testing.T) {
|
func TestUnpackEvent(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
|
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
|
||||||
abi, err := JSON(strings.NewReader(abiJSON))
|
abi, err := JSON(strings.NewReader(abiJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -823,7 +773,6 @@ func TestUnpackEvent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackEventIntoMap(t *testing.T) {
|
func TestUnpackEventIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
|
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
|
||||||
abi, err := JSON(strings.NewReader(abiJSON))
|
abi, err := JSON(strings.NewReader(abiJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -874,7 +823,6 @@ func TestUnpackEventIntoMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackMethodIntoMap(t *testing.T) {
|
func TestUnpackMethodIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"send","outputs":[{"name":"amount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"get","outputs":[{"name":"hash","type":"bytes"}],"payable":true,"stateMutability":"payable","type":"function"}]`
|
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"send","outputs":[{"name":"amount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"get","outputs":[{"name":"hash","type":"bytes"}],"payable":true,"stateMutability":"payable","type":"function"}]`
|
||||||
abi, err := JSON(strings.NewReader(abiJSON))
|
abi, err := JSON(strings.NewReader(abiJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -925,7 +873,6 @@ func TestUnpackMethodIntoMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackIntoMapNamingConflict(t *testing.T) {
|
func TestUnpackIntoMapNamingConflict(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Two methods have the same name
|
// Two methods have the same name
|
||||||
var abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"get","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"send","outputs":[{"name":"amount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"get","outputs":[{"name":"hash","type":"bytes"}],"payable":true,"stateMutability":"payable","type":"function"}]`
|
var abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"get","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"send","outputs":[{"name":"amount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"get","outputs":[{"name":"hash","type":"bytes"}],"payable":true,"stateMutability":"payable","type":"function"}]`
|
||||||
abi, err := JSON(strings.NewReader(abiJSON))
|
abi, err := JSON(strings.NewReader(abiJSON))
|
||||||
@@ -1009,7 +956,6 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestABI_MethodById(t *testing.T) {
|
func TestABI_MethodById(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(jsondata))
|
abi, err := JSON(strings.NewReader(jsondata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -1042,7 +988,6 @@ func TestABI_MethodById(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestABI_EventById(t *testing.T) {
|
func TestABI_EventById(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
json string
|
json string
|
||||||
@@ -1108,39 +1053,9 @@ func TestABI_EventById(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestABI_ErrorByID(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(`[
|
|
||||||
{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"MyError1","type":"error"},
|
|
||||||
{"inputs":[{"components":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"string","name":"b","type":"string"},{"internalType":"address","name":"c","type":"address"}],"internalType":"struct MyError.MyStruct","name":"x","type":"tuple"},{"internalType":"address","name":"y","type":"address"},{"components":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"string","name":"b","type":"string"},{"internalType":"address","name":"c","type":"address"}],"internalType":"struct MyError.MyStruct","name":"z","type":"tuple"}],"name":"MyError2","type":"error"},
|
|
||||||
{"inputs":[{"internalType":"uint256[]","name":"x","type":"uint256[]"}],"name":"MyError3","type":"error"}
|
|
||||||
]`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for name, m := range abi.Errors {
|
|
||||||
a := fmt.Sprintf("%v", &m)
|
|
||||||
var id [4]byte
|
|
||||||
copy(id[:], m.ID[:4])
|
|
||||||
m2, err := abi.ErrorByID(id)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to look up ABI error: %v", err)
|
|
||||||
}
|
|
||||||
b := fmt.Sprintf("%v", m2)
|
|
||||||
if a != b {
|
|
||||||
t.Errorf("Error %v (id %x) not 'findable' by id in ABI", name, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// test unsuccessful lookups
|
|
||||||
if _, err = abi.ErrorByID([4]byte{}); err == nil {
|
|
||||||
t.Error("Expected error: no error with this id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDoubleDuplicateMethodNames checks that if transfer0 already exists, there won't be a name
|
// TestDoubleDuplicateMethodNames checks that if transfer0 already exists, there won't be a name
|
||||||
// conflict and that the second transfer method will be renamed transfer1.
|
// conflict and that the second transfer method will be renamed transfer1.
|
||||||
func TestDoubleDuplicateMethodNames(t *testing.T) {
|
func TestDoubleDuplicateMethodNames(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abiJSON := `[{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"}],"name":"transfer0","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"},{"name":"customFallback","type":"string"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
|
abiJSON := `[{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"}],"name":"transfer0","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"},{"name":"customFallback","type":"string"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
|
||||||
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1163,14 +1078,12 @@ func TestDoubleDuplicateMethodNames(t *testing.T) {
|
|||||||
// TestDoubleDuplicateEventNames checks that if send0 already exists, there won't be a name
|
// TestDoubleDuplicateEventNames checks that if send0 already exists, there won't be a name
|
||||||
// conflict and that the second send event will be renamed send1.
|
// conflict and that the second send event will be renamed send1.
|
||||||
// The test runs the abi of the following contract.
|
// The test runs the abi of the following contract.
|
||||||
//
|
// contract DuplicateEvent {
|
||||||
// contract DuplicateEvent {
|
// event send(uint256 a);
|
||||||
// event send(uint256 a);
|
|
||||||
// event send0();
|
// event send0();
|
||||||
// event send();
|
// event send();
|
||||||
// }
|
// }
|
||||||
func TestDoubleDuplicateEventNames(t *testing.T) {
|
func TestDoubleDuplicateEventNames(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abiJSON := `[{"anonymous": false,"inputs": [{"indexed": false,"internalType": "uint256","name": "a","type": "uint256"}],"name": "send","type": "event"},{"anonymous": false,"inputs": [],"name": "send0","type": "event"},{ "anonymous": false, "inputs": [],"name": "send","type": "event"}]`
|
abiJSON := `[{"anonymous": false,"inputs": [{"indexed": false,"internalType": "uint256","name": "a","type": "uint256"}],"name": "send","type": "event"},{"anonymous": false,"inputs": [],"name": "send0","type": "event"},{ "anonymous": false, "inputs": [],"name": "send","type": "event"}]`
|
||||||
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1193,12 +1106,10 @@ func TestDoubleDuplicateEventNames(t *testing.T) {
|
|||||||
// TestUnnamedEventParam checks that an event with unnamed parameters is
|
// TestUnnamedEventParam checks that an event with unnamed parameters is
|
||||||
// correctly handled.
|
// correctly handled.
|
||||||
// The test runs the abi of the following contract.
|
// The test runs the abi of the following contract.
|
||||||
//
|
// contract TestEvent {
|
||||||
// contract TestEvent {
|
|
||||||
// event send(uint256, uint256);
|
// event send(uint256, uint256);
|
||||||
// }
|
// }
|
||||||
func TestUnnamedEventParam(t *testing.T) {
|
func TestUnnamedEventParam(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abiJSON := `[{ "anonymous": false, "inputs": [{ "indexed": false,"internalType": "uint256", "name": "","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "","type": "uint256"}],"name": "send","type": "event"}]`
|
abiJSON := `[{ "anonymous": false, "inputs": [{ "indexed": false,"internalType": "uint256", "name": "","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "","type": "uint256"}],"name": "send","type": "event"}]`
|
||||||
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1228,12 +1139,9 @@ func TestUnpackRevert(t *testing.T) {
|
|||||||
{"", "", errors.New("invalid data for unpacking")},
|
{"", "", errors.New("invalid data for unpacking")},
|
||||||
{"08c379a1", "", errors.New("invalid data for unpacking")},
|
{"08c379a1", "", errors.New("invalid data for unpacking")},
|
||||||
{"08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000", "revert reason", nil},
|
{"08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000", "revert reason", nil},
|
||||||
{"4e487b710000000000000000000000000000000000000000000000000000000000000000", "generic panic", nil},
|
|
||||||
{"4e487b7100000000000000000000000000000000000000000000000000000000000000ff", "unknown panic code: 0xff", nil},
|
|
||||||
}
|
}
|
||||||
for index, c := range cases {
|
for index, c := range cases {
|
||||||
t.Run(fmt.Sprintf("case %d", index), func(t *testing.T) {
|
t.Run(fmt.Sprintf("case %d", index), func(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
got, err := UnpackRevert(common.Hex2Bytes(c.input))
|
got, err := UnpackRevert(common.Hex2Bytes(c.input))
|
||||||
if c.expectErr != nil {
|
if c.expectErr != nil {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -1250,10 +1158,3 @@ func TestUnpackRevert(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInternalContractType(t *testing.T) {
|
|
||||||
jsonData := `[{"inputs":[{"components":[{"internalType":"uint256","name":"dailyLimit","type":"uint256"},{"internalType":"uint256","name":"txLimit","type":"uint256"},{"internalType":"uint256","name":"accountDailyLimit","type":"uint256"},{"internalType":"uint256","name":"minAmount","type":"uint256"},{"internalType":"bool","name":"onlyWhitelisted","type":"bool"}],"internalType":"struct IMessagePassingBridge.BridgeLimits","name":"bridgeLimits","type":"tuple"},{"components":[{"internalType":"uint256","name":"lastTransferReset","type":"uint256"},{"internalType":"uint256","name":"bridged24Hours","type":"uint256"}],"internalType":"struct IMessagePassingBridge.AccountLimit","name":"accountDailyLimit","type":"tuple"},{"components":[{"internalType":"uint256","name":"lastTransferReset","type":"uint256"},{"internalType":"uint256","name":"bridged24Hours","type":"uint256"}],"internalType":"struct IMessagePassingBridge.BridgeDailyLimit","name":"bridgeDailyLimit","type":"tuple"},{"internalType":"contract INameService","name":"nameService","type":"INameService"},{"internalType":"bool","name":"isClosed","type":"bool"},{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"canBridge","outputs":[{"internalType":"bool","name":"isWithinLimit","type":"bool"},{"internalType":"string","name":"error","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"name":"normalizeFrom18ToTokenDecimals","outputs":[{"internalType":"uint256","name":"normalized","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"name":"normalizeFromTokenTo18Decimals","outputs":[{"internalType":"uint256","name":"normalized","type":"uint256"}],"stateMutability":"pure","type":"function"}]`
|
|
||||||
if _, err := JSON(strings.NewReader(jsonData)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ func (arguments Arguments) isTuple() bool {
|
|||||||
func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
|
func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
if len(arguments.NonIndexed()) != 0 {
|
if len(arguments.NonIndexed()) != 0 {
|
||||||
return nil, errors.New("abi: attempting to unmarshal an empty string while arguments are expected")
|
return nil, errors.New("abi: attempting to unmarshall an empty string while arguments are expected")
|
||||||
}
|
}
|
||||||
return make([]interface{}, 0), nil
|
return make([]interface{}, 0), nil
|
||||||
}
|
}
|
||||||
@@ -95,7 +95,7 @@ func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte)
|
|||||||
}
|
}
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
if len(arguments.NonIndexed()) != 0 {
|
if len(arguments.NonIndexed()) != 0 {
|
||||||
return errors.New("abi: attempting to unmarshal an empty string while arguments are expected")
|
return errors.New("abi: attempting to unmarshall an empty string while arguments are expected")
|
||||||
}
|
}
|
||||||
return nil // Nothing to unmarshal, return
|
return nil // Nothing to unmarshal, return
|
||||||
}
|
}
|
||||||
@@ -127,7 +127,7 @@ func (arguments Arguments) Copy(v interface{}, values []interface{}) error {
|
|||||||
return arguments.copyAtomic(v, values[0])
|
return arguments.copyAtomic(v, values[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// copyAtomic copies ( hexdata -> go ) a single value
|
// unpackAtomic unpacks ( hexdata -> go ) a single value
|
||||||
func (arguments Arguments) copyAtomic(v interface{}, marshalledValues interface{}) error {
|
func (arguments Arguments) copyAtomic(v interface{}, marshalledValues interface{}) error {
|
||||||
dst := reflect.ValueOf(v).Elem()
|
dst := reflect.ValueOf(v).Elem()
|
||||||
src := reflect.ValueOf(marshalledValues)
|
src := reflect.ValueOf(marshalledValues)
|
||||||
@@ -187,9 +187,6 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
|
|||||||
virtualArgs := 0
|
virtualArgs := 0
|
||||||
for index, arg := range nonIndexedArgs {
|
for index, arg := range nonIndexedArgs {
|
||||||
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
|
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if arg.Type.T == ArrayTy && !isDynamicType(arg.Type) {
|
if arg.Type.T == ArrayTy && !isDynamicType(arg.Type) {
|
||||||
// If we have a static array, like [3]uint256, these are coded as
|
// If we have a static array, like [3]uint256, these are coded as
|
||||||
// just like uint256,uint256,uint256.
|
// just like uint256,uint256,uint256.
|
||||||
@@ -207,6 +204,9 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
|
|||||||
// coded as just like uint256,bool,uint256
|
// coded as just like uint256,bool,uint256
|
||||||
virtualArgs += getTypeSize(arg.Type)/32 - 1
|
virtualArgs += getTypeSize(arg.Type)/32 - 1
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
retval = append(retval, marshalledValue)
|
retval = append(retval, marshalledValue)
|
||||||
}
|
}
|
||||||
return retval, nil
|
return retval, nil
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyStoreTransactor is a utility method to easily create a transaction signer from
|
// NewKeyStoreTransactor is a utility method to easily create a transaction signer from
|
||||||
// a decrypted key from a keystore.
|
// an decrypted key from a keystore.
|
||||||
//
|
//
|
||||||
// Deprecated: Use NewKeyStoreTransactorWithChainID instead.
|
// Deprecated: Use NewKeyStoreTransactorWithChainID instead.
|
||||||
func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
|
func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
|
||||||
@@ -117,7 +117,7 @@ func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.I
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
|
// NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
|
||||||
// a decrypted key from a keystore.
|
// an decrypted key from a keystore.
|
||||||
func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
|
func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
|
||||||
if chainID == nil {
|
if chainID == nil {
|
||||||
return nil, ErrNoChainID
|
return nil, ErrNoChainID
|
||||||
@@ -142,10 +142,10 @@ func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accou
|
|||||||
// NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
|
// NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
|
||||||
// from a single private key.
|
// from a single private key.
|
||||||
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
|
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
|
||||||
|
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
|
||||||
if chainID == nil {
|
if chainID == nil {
|
||||||
return nil, ErrNoChainID
|
return nil, ErrNoChainID
|
||||||
}
|
}
|
||||||
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
|
|
||||||
signer := types.LatestSignerForChainID(chainID)
|
signer := types.LatestSignerForChainID(chainID)
|
||||||
return &TransactOpts{
|
return &TransactOpts{
|
||||||
From: keyAddr,
|
From: keyAddr,
|
||||||
|
|||||||
@@ -29,17 +29,13 @@ import (
|
|||||||
var (
|
var (
|
||||||
// ErrNoCode is returned by call and transact operations for which the requested
|
// ErrNoCode is returned by call and transact operations for which the requested
|
||||||
// recipient contract to operate on does not exist in the state db or does not
|
// recipient contract to operate on does not exist in the state db or does not
|
||||||
// have any code associated with it (i.e. self-destructed).
|
// have any code associated with it (i.e. suicided).
|
||||||
ErrNoCode = errors.New("no contract code at given address")
|
ErrNoCode = errors.New("no contract code at given address")
|
||||||
|
|
||||||
// ErrNoPendingState is raised when attempting to perform a pending state action
|
// ErrNoPendingState is raised when attempting to perform a pending state action
|
||||||
// on a backend that doesn't implement PendingContractCaller.
|
// on a backend that doesn't implement PendingContractCaller.
|
||||||
ErrNoPendingState = errors.New("backend does not support pending state")
|
ErrNoPendingState = errors.New("backend does not support pending state")
|
||||||
|
|
||||||
// ErrNoBlockHashState is raised when attempting to perform a block hash action
|
|
||||||
// on a backend that doesn't implement BlockHashContractCaller.
|
|
||||||
ErrNoBlockHashState = errors.New("backend does not support block hash state")
|
|
||||||
|
|
||||||
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
|
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
|
||||||
// an empty contract behind.
|
// an empty contract behind.
|
||||||
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
|
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
|
||||||
@@ -68,27 +64,11 @@ type PendingContractCaller interface {
|
|||||||
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
|
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlockHashContractCaller defines methods to perform contract calls on a specific block hash.
|
|
||||||
// Call will try to discover this interface when access to a block by hash is requested.
|
|
||||||
// If the backend does not support the block hash state, Call returns ErrNoBlockHashState.
|
|
||||||
type BlockHashContractCaller interface {
|
|
||||||
// CodeAtHash returns the code of the given account in the state at the specified block hash.
|
|
||||||
CodeAtHash(ctx context.Context, contract common.Address, blockHash common.Hash) ([]byte, error)
|
|
||||||
|
|
||||||
// CallContractAtHash executes an Ethereum contract call against the state at the specified block hash.
|
|
||||||
CallContractAtHash(ctx context.Context, call ethereum.CallMsg, blockHash common.Hash) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContractTransactor defines the methods needed to allow operating with a contract
|
// ContractTransactor defines the methods needed to allow operating with a contract
|
||||||
// on a write only basis. Besides the transacting method, the remainder are helpers
|
// on a write only basis. Besides the transacting method, the remainder are helpers
|
||||||
// used when the user does not provide some needed values, but rather leaves it up
|
// used when the user does not provide some needed values, but rather leaves it up
|
||||||
// to the transactor to decide.
|
// to the transactor to decide.
|
||||||
type ContractTransactor interface {
|
type ContractTransactor interface {
|
||||||
ethereum.GasEstimator
|
|
||||||
ethereum.GasPricer
|
|
||||||
ethereum.GasPricer1559
|
|
||||||
ethereum.TransactionSender
|
|
||||||
|
|
||||||
// HeaderByNumber returns a block header from the current canonical chain. If
|
// HeaderByNumber returns a block header from the current canonical chain. If
|
||||||
// number is nil, the latest known header is returned.
|
// number is nil, the latest known header is returned.
|
||||||
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
||||||
@@ -98,6 +78,38 @@ type ContractTransactor interface {
|
|||||||
|
|
||||||
// PendingNonceAt retrieves the current pending nonce associated with an account.
|
// PendingNonceAt retrieves the current pending nonce associated with an account.
|
||||||
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
||||||
|
|
||||||
|
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
|
||||||
|
// execution of a transaction.
|
||||||
|
SuggestGasPrice(ctx context.Context) (*big.Int, error)
|
||||||
|
|
||||||
|
// SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
|
||||||
|
// a timely execution of a transaction.
|
||||||
|
SuggestGasTipCap(ctx context.Context) (*big.Int, error)
|
||||||
|
|
||||||
|
// EstimateGas tries to estimate the gas needed to execute a specific
|
||||||
|
// transaction based on the current pending state of the backend blockchain.
|
||||||
|
// There is no guarantee that this is the true gas limit requirement as other
|
||||||
|
// transactions may be added or removed by miners, but it should provide a basis
|
||||||
|
// for setting a reasonable default.
|
||||||
|
EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
|
||||||
|
|
||||||
|
// SendTransaction injects the transaction into the pending pool for execution.
|
||||||
|
SendTransaction(ctx context.Context, tx *types.Transaction) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContractFilterer defines the methods needed to access log events using one-off
|
||||||
|
// queries or continuous event subscriptions.
|
||||||
|
type ContractFilterer interface {
|
||||||
|
// FilterLogs executes a log filter operation, blocking during execution and
|
||||||
|
// returning all the results in one batch.
|
||||||
|
//
|
||||||
|
// TODO(karalabe): Deprecate when the subscription one can return past data too.
|
||||||
|
FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
|
||||||
|
|
||||||
|
// SubscribeFilterLogs creates a background log filtering operation, returning
|
||||||
|
// a subscription immediately, which can be used to stream the found events.
|
||||||
|
SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
|
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
|
||||||
@@ -106,12 +118,6 @@ type DeployBackend interface {
|
|||||||
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
|
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContractFilterer defines the methods needed to access log events using one-off
|
|
||||||
// queries or continuous event subscriptions.
|
|
||||||
type ContractFilterer interface {
|
|
||||||
ethereum.LogFilterer
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContractBackend defines the methods needed to work with contracts on a read-write basis.
|
// ContractBackend defines the methods needed to work with contracts on a read-write basis.
|
||||||
type ContractBackend interface {
|
type ContractBackend interface {
|
||||||
ContractCaller
|
ContractCaller
|
||||||
|
|||||||
@@ -18,35 +18,881 @@ package backends
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
"github.com/ethereum/go-ethereum/core/bloombits"
|
||||||
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||||
|
"github.com/ethereum/go-ethereum/core/state"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethclient/simulated"
|
"github.com/ethereum/go-ethereum/core/vm"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/filters"
|
||||||
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
|
"github.com/ethereum/go-ethereum/event"
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SimulatedBackend is a simulated blockchain.
|
// This nil assignment ensures at compile time that SimulatedBackend implements bind.ContractBackend.
|
||||||
// Deprecated: use package github.com/ethereum/go-ethereum/ethclient/simulated instead.
|
var _ bind.ContractBackend = (*SimulatedBackend)(nil)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errBlockNumberUnsupported = errors.New("simulatedBackend cannot access blocks other than the latest block")
|
||||||
|
errBlockDoesNotExist = errors.New("block does not exist in blockchain")
|
||||||
|
errTransactionDoesNotExist = errors.New("transaction does not exist")
|
||||||
|
)
|
||||||
|
|
||||||
|
// SimulatedBackend implements bind.ContractBackend, simulating a blockchain in
|
||||||
|
// the background. Its main purpose is to allow for easy testing of contract bindings.
|
||||||
|
// Simulated backend implements the following interfaces:
|
||||||
|
// ChainReader, ChainStateReader, ContractBackend, ContractCaller, ContractFilterer, ContractTransactor,
|
||||||
|
// DeployBackend, GasEstimator, GasPricer, LogFilterer, PendingContractCaller, TransactionReader, and TransactionSender
|
||||||
type SimulatedBackend struct {
|
type SimulatedBackend struct {
|
||||||
*simulated.Backend
|
database ethdb.Database // In memory database to store our testing data
|
||||||
simulated.Client
|
blockchain *core.BlockChain // Ethereum blockchain to handle the consensus
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
pendingBlock *types.Block // Currently pending block that will be imported on request
|
||||||
|
pendingState *state.StateDB // Currently pending state that will be the active on request
|
||||||
|
pendingReceipts types.Receipts // Currently receipts for the pending block
|
||||||
|
|
||||||
|
events *filters.EventSystem // for filtering log events live
|
||||||
|
filterSystem *filters.FilterSystem // for filtering database logs
|
||||||
|
|
||||||
|
config *params.ChainConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fork sets the head to a new block, which is based on the provided parentHash.
|
// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
|
||||||
func (b *SimulatedBackend) Fork(ctx context.Context, parentHash common.Hash) error {
|
// and uses a simulated blockchain for testing purposes.
|
||||||
return b.Backend.Fork(parentHash)
|
// A simulated backend always uses chainID 1337.
|
||||||
|
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
|
||||||
|
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
|
||||||
|
genesis.MustCommit(database)
|
||||||
|
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
|
||||||
|
|
||||||
|
backend := &SimulatedBackend{
|
||||||
|
database: database,
|
||||||
|
blockchain: blockchain,
|
||||||
|
config: genesis.Config,
|
||||||
|
}
|
||||||
|
|
||||||
|
filterBackend := &filterBackend{database, blockchain, backend}
|
||||||
|
backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{})
|
||||||
|
backend.events = filters.NewEventSystem(backend.filterSystem, false)
|
||||||
|
|
||||||
|
backend.rollback(blockchain.CurrentBlock())
|
||||||
|
return backend
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
|
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
|
||||||
// for testing purposes.
|
// for testing purposes.
|
||||||
//
|
|
||||||
// A simulated backend always uses chainID 1337.
|
// A simulated backend always uses chainID 1337.
|
||||||
|
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
|
||||||
|
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close terminates the underlying blockchain's update loop.
|
||||||
|
func (b *SimulatedBackend) Close() error {
|
||||||
|
b.blockchain.Stop()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit imports all the pending transactions as a single block and starts a
|
||||||
|
// fresh new state.
|
||||||
|
func (b *SimulatedBackend) Commit() common.Hash {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if _, err := b.blockchain.InsertChain([]*types.Block{b.pendingBlock}); err != nil {
|
||||||
|
panic(err) // This cannot happen unless the simulator is wrong, fail in that case
|
||||||
|
}
|
||||||
|
blockHash := b.pendingBlock.Hash()
|
||||||
|
|
||||||
|
// Using the last inserted block here makes it possible to build on a side
|
||||||
|
// chain after a fork.
|
||||||
|
b.rollback(b.pendingBlock)
|
||||||
|
|
||||||
|
return blockHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rollback aborts all pending transactions, reverting to the last committed state.
|
||||||
|
func (b *SimulatedBackend) Rollback() {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
b.rollback(b.blockchain.CurrentBlock())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SimulatedBackend) rollback(parent *types.Block) {
|
||||||
|
blocks, _ := core.GenerateChain(b.config, parent, ethash.NewFaker(), b.database, 1, func(int, *core.BlockGen) {})
|
||||||
|
|
||||||
|
b.pendingBlock = blocks[0]
|
||||||
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.blockchain.StateCache(), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fork creates a side-chain that can be used to simulate reorgs.
|
||||||
//
|
//
|
||||||
// Deprecated: please use simulated.Backend from package
|
// This function should be called with the ancestor block where the new side
|
||||||
// github.com/ethereum/go-ethereum/ethclient/simulated instead.
|
// chain should be started. Transactions (old and new) can then be applied on
|
||||||
func NewSimulatedBackend(alloc types.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
|
// top and Commit-ed.
|
||||||
b := simulated.NewBackend(alloc, simulated.WithBlockGasLimit(gasLimit))
|
//
|
||||||
return &SimulatedBackend{
|
// Note, the side-chain will only become canonical (and trigger the events) when
|
||||||
Backend: b,
|
// it becomes longer. Until then CallContract will still operate on the current
|
||||||
Client: b.Client(),
|
// canonical chain.
|
||||||
|
//
|
||||||
|
// There is a % chance that the side chain becomes canonical at the same length
|
||||||
|
// to simulate live network behavior.
|
||||||
|
func (b *SimulatedBackend) Fork(ctx context.Context, parent common.Hash) error {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if len(b.pendingBlock.Transactions()) != 0 {
|
||||||
|
return errors.New("pending block dirty")
|
||||||
|
}
|
||||||
|
block, err := b.blockByHash(ctx, parent)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.rollback(block)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stateByBlockNumber retrieves a state by a given blocknumber.
|
||||||
|
func (b *SimulatedBackend) stateByBlockNumber(ctx context.Context, blockNumber *big.Int) (*state.StateDB, error) {
|
||||||
|
if blockNumber == nil || blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) == 0 {
|
||||||
|
return b.blockchain.State()
|
||||||
|
}
|
||||||
|
block, err := b.blockByNumber(ctx, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b.blockchain.StateAt(block.Root())
|
||||||
|
}
|
||||||
|
|
||||||
|
// CodeAt returns the code associated with a certain account in the blockchain.
|
||||||
|
func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
stateDB, err := b.stateByBlockNumber(ctx, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return stateDB.GetCode(contract), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BalanceAt returns the wei balance of a certain account in the blockchain.
|
||||||
|
func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (*big.Int, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
stateDB, err := b.stateByBlockNumber(ctx, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return stateDB.GetBalance(contract), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NonceAt returns the nonce of a certain account in the blockchain.
|
||||||
|
func (b *SimulatedBackend) NonceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (uint64, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
stateDB, err := b.stateByBlockNumber(ctx, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return stateDB.GetNonce(contract), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StorageAt returns the value of key in the storage of an account in the blockchain.
|
||||||
|
func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
stateDB, err := b.stateByBlockNumber(ctx, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
val := stateDB.GetState(contract, key)
|
||||||
|
return val[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransactionReceipt returns the receipt of a transaction.
|
||||||
|
func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash, b.config)
|
||||||
|
if receipt == nil {
|
||||||
|
return nil, ethereum.NotFound
|
||||||
|
}
|
||||||
|
return receipt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransactionByHash checks the pool of pending transactions in addition to the
|
||||||
|
// blockchain. The isPending return value indicates whether the transaction has been
|
||||||
|
// mined yet. Note that the transaction may not be part of the canonical chain even if
|
||||||
|
// it's not pending.
|
||||||
|
func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
tx := b.pendingBlock.Transaction(txHash)
|
||||||
|
if tx != nil {
|
||||||
|
return tx, true, nil
|
||||||
|
}
|
||||||
|
tx, _, _, _ = rawdb.ReadTransaction(b.database, txHash)
|
||||||
|
if tx != nil {
|
||||||
|
return tx, false, nil
|
||||||
|
}
|
||||||
|
return nil, false, ethereum.NotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockByHash retrieves a block based on the block hash.
|
||||||
|
func (b *SimulatedBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
return b.blockByHash(ctx, hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
// blockByHash retrieves a block based on the block hash without Locking.
|
||||||
|
func (b *SimulatedBackend) blockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
|
||||||
|
if hash == b.pendingBlock.Hash() {
|
||||||
|
return b.pendingBlock, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
block := b.blockchain.GetBlockByHash(hash)
|
||||||
|
if block != nil {
|
||||||
|
return block, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errBlockDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockByNumber retrieves a block from the database by number, caching it
|
||||||
|
// (associated with its hash) if found.
|
||||||
|
func (b *SimulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
return b.blockByNumber(ctx, number)
|
||||||
|
}
|
||||||
|
|
||||||
|
// blockByNumber retrieves a block from the database by number, caching it
|
||||||
|
// (associated with its hash) if found without Lock.
|
||||||
|
func (b *SimulatedBackend) blockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
|
||||||
|
if number == nil || number.Cmp(b.pendingBlock.Number()) == 0 {
|
||||||
|
return b.blockchain.CurrentBlock(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
block := b.blockchain.GetBlockByNumber(uint64(number.Int64()))
|
||||||
|
if block == nil {
|
||||||
|
return nil, errBlockDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return block, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeaderByHash returns a block header from the current canonical chain.
|
||||||
|
func (b *SimulatedBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if hash == b.pendingBlock.Hash() {
|
||||||
|
return b.pendingBlock.Header(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
header := b.blockchain.GetHeaderByHash(hash)
|
||||||
|
if header == nil {
|
||||||
|
return nil, errBlockDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return header, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeaderByNumber returns a block header from the current canonical chain. If number is
|
||||||
|
// nil, the latest known header is returned.
|
||||||
|
func (b *SimulatedBackend) HeaderByNumber(ctx context.Context, block *big.Int) (*types.Header, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if block == nil || block.Cmp(b.pendingBlock.Number()) == 0 {
|
||||||
|
return b.blockchain.CurrentHeader(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.blockchain.GetHeaderByNumber(uint64(block.Int64())), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransactionCount returns the number of transactions in a given block.
|
||||||
|
func (b *SimulatedBackend) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if blockHash == b.pendingBlock.Hash() {
|
||||||
|
return uint(b.pendingBlock.Transactions().Len()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
block := b.blockchain.GetBlockByHash(blockHash)
|
||||||
|
if block == nil {
|
||||||
|
return uint(0), errBlockDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return uint(block.Transactions().Len()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransactionInBlock returns the transaction for a specific block at a specific index.
|
||||||
|
func (b *SimulatedBackend) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if blockHash == b.pendingBlock.Hash() {
|
||||||
|
transactions := b.pendingBlock.Transactions()
|
||||||
|
if uint(len(transactions)) < index+1 {
|
||||||
|
return nil, errTransactionDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return transactions[index], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
block := b.blockchain.GetBlockByHash(blockHash)
|
||||||
|
if block == nil {
|
||||||
|
return nil, errBlockDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions := block.Transactions()
|
||||||
|
if uint(len(transactions)) < index+1 {
|
||||||
|
return nil, errTransactionDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return transactions[index], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PendingCodeAt returns the code associated with an account in the pending state.
|
||||||
|
func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
return b.pendingState.GetCode(contract), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRevertError(result *core.ExecutionResult) *revertError {
|
||||||
|
reason, errUnpack := abi.UnpackRevert(result.Revert())
|
||||||
|
err := errors.New("execution reverted")
|
||||||
|
if errUnpack == nil {
|
||||||
|
err = fmt.Errorf("execution reverted: %v", reason)
|
||||||
|
}
|
||||||
|
return &revertError{
|
||||||
|
error: err,
|
||||||
|
reason: hexutil.Encode(result.Revert()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// revertError is an API error that encompasses an EVM revert with JSON error
|
||||||
|
// code and a binary data blob.
|
||||||
|
type revertError struct {
|
||||||
|
error
|
||||||
|
reason string // revert reason hex encoded
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorCode returns the JSON error code for a revert.
|
||||||
|
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
|
||||||
|
func (e *revertError) ErrorCode() int {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorData returns the hex encoded revert reason.
|
||||||
|
func (e *revertError) ErrorData() interface{} {
|
||||||
|
return e.reason
|
||||||
|
}
|
||||||
|
|
||||||
|
// CallContract executes a contract call.
|
||||||
|
func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
|
||||||
|
return nil, errBlockNumberUnsupported
|
||||||
|
}
|
||||||
|
stateDB, err := b.blockchain.State()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), stateDB)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// If the result contains a revert reason, try to unpack and return it.
|
||||||
|
if len(res.Revert()) > 0 {
|
||||||
|
return nil, newRevertError(res)
|
||||||
|
}
|
||||||
|
return res.Return(), res.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PendingCallContract executes a contract call on the pending state.
|
||||||
|
func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
|
||||||
|
|
||||||
|
res, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// If the result contains a revert reason, try to unpack and return it.
|
||||||
|
if len(res.Revert()) > 0 {
|
||||||
|
return nil, newRevertError(res)
|
||||||
|
}
|
||||||
|
return res.Return(), res.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
|
||||||
|
// the nonce currently pending for the account.
|
||||||
|
func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
|
||||||
|
// chain doesn't have miners, we just return a gas price of 1 for any call.
|
||||||
|
func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if b.pendingBlock.Header().BaseFee != nil {
|
||||||
|
return b.pendingBlock.Header().BaseFee, nil
|
||||||
|
}
|
||||||
|
return big.NewInt(1), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SuggestGasTipCap implements ContractTransactor.SuggestGasTipCap. Since the simulated
|
||||||
|
// chain doesn't have miners, we just return a gas tip of 1 for any call.
|
||||||
|
func (b *SimulatedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
|
||||||
|
return big.NewInt(1), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EstimateGas executes the requested code against the currently pending block/state and
|
||||||
|
// returns the used amount of gas.
|
||||||
|
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
// Determine the lowest and highest possible gas limits to binary search in between
|
||||||
|
var (
|
||||||
|
lo uint64 = params.TxGas - 1
|
||||||
|
hi uint64
|
||||||
|
cap uint64
|
||||||
|
)
|
||||||
|
if call.Gas >= params.TxGas {
|
||||||
|
hi = call.Gas
|
||||||
|
} else {
|
||||||
|
hi = b.pendingBlock.GasLimit()
|
||||||
|
}
|
||||||
|
// Normalize the max fee per gas the call is willing to spend.
|
||||||
|
var feeCap *big.Int
|
||||||
|
if call.GasPrice != nil && (call.GasFeeCap != nil || call.GasTipCap != nil) {
|
||||||
|
return 0, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
||||||
|
} else if call.GasPrice != nil {
|
||||||
|
feeCap = call.GasPrice
|
||||||
|
} else if call.GasFeeCap != nil {
|
||||||
|
feeCap = call.GasFeeCap
|
||||||
|
} else {
|
||||||
|
feeCap = common.Big0
|
||||||
|
}
|
||||||
|
// Recap the highest gas allowance with account's balance.
|
||||||
|
if feeCap.BitLen() != 0 {
|
||||||
|
balance := b.pendingState.GetBalance(call.From) // from can't be nil
|
||||||
|
available := new(big.Int).Set(balance)
|
||||||
|
if call.Value != nil {
|
||||||
|
if call.Value.Cmp(available) >= 0 {
|
||||||
|
return 0, errors.New("insufficient funds for transfer")
|
||||||
|
}
|
||||||
|
available.Sub(available, call.Value)
|
||||||
|
}
|
||||||
|
allowance := new(big.Int).Div(available, feeCap)
|
||||||
|
if allowance.IsUint64() && hi > allowance.Uint64() {
|
||||||
|
transfer := call.Value
|
||||||
|
if transfer == nil {
|
||||||
|
transfer = new(big.Int)
|
||||||
|
}
|
||||||
|
log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
|
||||||
|
"sent", transfer, "feecap", feeCap, "fundable", allowance)
|
||||||
|
hi = allowance.Uint64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cap = hi
|
||||||
|
|
||||||
|
// Create a helper to check if a gas allowance results in an executable transaction
|
||||||
|
executable := func(gas uint64) (bool, *core.ExecutionResult, error) {
|
||||||
|
call.Gas = gas
|
||||||
|
|
||||||
|
snapshot := b.pendingState.Snapshot()
|
||||||
|
res, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
||||||
|
b.pendingState.RevertToSnapshot(snapshot)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, core.ErrIntrinsicGas) {
|
||||||
|
return true, nil, nil // Special case, raise gas limit
|
||||||
|
}
|
||||||
|
return true, nil, err // Bail out
|
||||||
|
}
|
||||||
|
return res.Failed(), res, nil
|
||||||
|
}
|
||||||
|
// Execute the binary search and hone in on an executable gas limit
|
||||||
|
for lo+1 < hi {
|
||||||
|
mid := (hi + lo) / 2
|
||||||
|
failed, _, err := executable(mid)
|
||||||
|
|
||||||
|
// If the error is not nil(consensus error), it means the provided message
|
||||||
|
// call or transaction will never be accepted no matter how much gas it is
|
||||||
|
// assigned. Return the error directly, don't struggle any more
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if failed {
|
||||||
|
lo = mid
|
||||||
|
} else {
|
||||||
|
hi = mid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Reject the transaction as invalid if it still fails at the highest allowance
|
||||||
|
if hi == cap {
|
||||||
|
failed, result, err := executable(hi)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if failed {
|
||||||
|
if result != nil && result.Err != vm.ErrOutOfGas {
|
||||||
|
if len(result.Revert()) > 0 {
|
||||||
|
return 0, newRevertError(result)
|
||||||
|
}
|
||||||
|
return 0, result.Err
|
||||||
|
}
|
||||||
|
// Otherwise, the specified gas cap is too low
|
||||||
|
return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hi, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// callContract implements common code between normal and pending contract calls.
|
||||||
|
// state is modified during execution, make sure to copy it if necessary.
|
||||||
|
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, stateDB *state.StateDB) (*core.ExecutionResult, error) {
|
||||||
|
// Gas prices post 1559 need to be initialized
|
||||||
|
if call.GasPrice != nil && (call.GasFeeCap != nil || call.GasTipCap != nil) {
|
||||||
|
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
||||||
|
}
|
||||||
|
head := b.blockchain.CurrentHeader()
|
||||||
|
if !b.blockchain.Config().IsLondon(head.Number) {
|
||||||
|
// If there's no basefee, then it must be a non-1559 execution
|
||||||
|
if call.GasPrice == nil {
|
||||||
|
call.GasPrice = new(big.Int)
|
||||||
|
}
|
||||||
|
call.GasFeeCap, call.GasTipCap = call.GasPrice, call.GasPrice
|
||||||
|
} else {
|
||||||
|
// A basefee is provided, necessitating 1559-type execution
|
||||||
|
if call.GasPrice != nil {
|
||||||
|
// User specified the legacy gas field, convert to 1559 gas typing
|
||||||
|
call.GasFeeCap, call.GasTipCap = call.GasPrice, call.GasPrice
|
||||||
|
} else {
|
||||||
|
// User specified 1559 gas fields (or none), use those
|
||||||
|
if call.GasFeeCap == nil {
|
||||||
|
call.GasFeeCap = new(big.Int)
|
||||||
|
}
|
||||||
|
if call.GasTipCap == nil {
|
||||||
|
call.GasTipCap = new(big.Int)
|
||||||
|
}
|
||||||
|
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
|
||||||
|
call.GasPrice = new(big.Int)
|
||||||
|
if call.GasFeeCap.BitLen() > 0 || call.GasTipCap.BitLen() > 0 {
|
||||||
|
call.GasPrice = math.BigMin(new(big.Int).Add(call.GasTipCap, head.BaseFee), call.GasFeeCap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Ensure message is initialized properly.
|
||||||
|
if call.Gas == 0 {
|
||||||
|
call.Gas = 50000000
|
||||||
|
}
|
||||||
|
if call.Value == nil {
|
||||||
|
call.Value = new(big.Int)
|
||||||
|
}
|
||||||
|
// Set infinite balance to the fake caller account.
|
||||||
|
from := stateDB.GetOrNewStateObject(call.From)
|
||||||
|
from.SetBalance(math.MaxBig256)
|
||||||
|
// Execute the call.
|
||||||
|
msg := callMsg{call}
|
||||||
|
|
||||||
|
txContext := core.NewEVMTxContext(msg)
|
||||||
|
evmContext := core.NewEVMBlockContext(block.Header(), b.blockchain, nil)
|
||||||
|
// Create a new environment which holds all relevant information
|
||||||
|
// about the transaction and calling mechanisms.
|
||||||
|
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
|
||||||
|
gasPool := new(core.GasPool).AddGas(math.MaxUint64)
|
||||||
|
|
||||||
|
return core.NewStateTransition(vmEnv, msg, gasPool).TransitionDb()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendTransaction updates the pending block to include the given transaction.
|
||||||
|
func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
// Get the last block
|
||||||
|
block, err := b.blockByHash(ctx, b.pendingBlock.ParentHash())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not fetch parent")
|
||||||
|
}
|
||||||
|
// Check transaction validity
|
||||||
|
signer := types.MakeSigner(b.blockchain.Config(), block.Number())
|
||||||
|
sender, err := types.Sender(signer, tx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid transaction: %v", err)
|
||||||
|
}
|
||||||
|
nonce := b.pendingState.GetNonce(sender)
|
||||||
|
if tx.Nonce() != nonce {
|
||||||
|
return fmt.Errorf("invalid transaction nonce: got %d, want %d", tx.Nonce(), nonce)
|
||||||
|
}
|
||||||
|
// Include tx in chain
|
||||||
|
blocks, receipts := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
|
||||||
|
for _, tx := range b.pendingBlock.Transactions() {
|
||||||
|
block.AddTxWithChain(b.blockchain, tx)
|
||||||
|
}
|
||||||
|
block.AddTxWithChain(b.blockchain, tx)
|
||||||
|
})
|
||||||
|
stateDB, _ := b.blockchain.State()
|
||||||
|
|
||||||
|
b.pendingBlock = blocks[0]
|
||||||
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil)
|
||||||
|
b.pendingReceipts = receipts[0]
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterLogs executes a log filter operation, blocking during execution and
|
||||||
|
// returning all the results in one batch.
|
||||||
|
//
|
||||||
|
// TODO(karalabe): Deprecate when the subscription one can return past data too.
|
||||||
|
func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
|
||||||
|
var filter *filters.Filter
|
||||||
|
if query.BlockHash != nil {
|
||||||
|
// Block filter requested, construct a single-shot filter
|
||||||
|
filter = b.filterSystem.NewBlockFilter(*query.BlockHash, query.Addresses, query.Topics)
|
||||||
|
} else {
|
||||||
|
// Initialize unset filter boundaries to run from genesis to chain head
|
||||||
|
from := int64(0)
|
||||||
|
if query.FromBlock != nil {
|
||||||
|
from = query.FromBlock.Int64()
|
||||||
|
}
|
||||||
|
to := int64(-1)
|
||||||
|
if query.ToBlock != nil {
|
||||||
|
to = query.ToBlock.Int64()
|
||||||
|
}
|
||||||
|
// Construct the range filter
|
||||||
|
filter = b.filterSystem.NewRangeFilter(from, to, query.Addresses, query.Topics)
|
||||||
|
}
|
||||||
|
// Run the filter and return all the logs
|
||||||
|
logs, err := filter.Logs(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := make([]types.Log, len(logs))
|
||||||
|
for i, nLog := range logs {
|
||||||
|
res[i] = *nLog
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeFilterLogs creates a background log filtering operation, returning a
|
||||||
|
// subscription immediately, which can be used to stream the found events.
|
||||||
|
func (b *SimulatedBackend) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
|
||||||
|
// Subscribe to contract events
|
||||||
|
sink := make(chan []*types.Log)
|
||||||
|
|
||||||
|
sub, err := b.events.SubscribeLogs(query, sink)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Since we're getting logs in batches, we need to flatten them into a plain stream
|
||||||
|
return event.NewSubscription(func(quit <-chan struct{}) error {
|
||||||
|
defer sub.Unsubscribe()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case logs := <-sink:
|
||||||
|
for _, nlog := range logs {
|
||||||
|
select {
|
||||||
|
case ch <- *nlog:
|
||||||
|
case err := <-sub.Err():
|
||||||
|
return err
|
||||||
|
case <-quit:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err := <-sub.Err():
|
||||||
|
return err
|
||||||
|
case <-quit:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeNewHead returns an event subscription for a new header.
|
||||||
|
func (b *SimulatedBackend) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
|
||||||
|
// subscribe to a new head
|
||||||
|
sink := make(chan *types.Header)
|
||||||
|
sub := b.events.SubscribeNewHeads(sink)
|
||||||
|
|
||||||
|
return event.NewSubscription(func(quit <-chan struct{}) error {
|
||||||
|
defer sub.Unsubscribe()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case head := <-sink:
|
||||||
|
select {
|
||||||
|
case ch <- head:
|
||||||
|
case err := <-sub.Err():
|
||||||
|
return err
|
||||||
|
case <-quit:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case err := <-sub.Err():
|
||||||
|
return err
|
||||||
|
case <-quit:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AdjustTime adds a time shift to the simulated clock.
|
||||||
|
// It can only be called on empty blocks.
|
||||||
|
func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if len(b.pendingBlock.Transactions()) != 0 {
|
||||||
|
return errors.New("Could not adjust time on non-empty block")
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
|
||||||
|
block.OffsetTime(int64(adjustment.Seconds()))
|
||||||
|
})
|
||||||
|
stateDB, _ := b.blockchain.State()
|
||||||
|
|
||||||
|
b.pendingBlock = blocks[0]
|
||||||
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blockchain returns the underlying blockchain.
|
||||||
|
func (b *SimulatedBackend) Blockchain() *core.BlockChain {
|
||||||
|
return b.blockchain
|
||||||
|
}
|
||||||
|
|
||||||
|
// callMsg implements core.Message to allow passing it as a transaction simulator.
|
||||||
|
type callMsg struct {
|
||||||
|
ethereum.CallMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m callMsg) From() common.Address { return m.CallMsg.From }
|
||||||
|
func (m callMsg) Nonce() uint64 { return 0 }
|
||||||
|
func (m callMsg) IsFake() bool { return true }
|
||||||
|
func (m callMsg) To() *common.Address { return m.CallMsg.To }
|
||||||
|
func (m callMsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
|
||||||
|
func (m callMsg) GasFeeCap() *big.Int { return m.CallMsg.GasFeeCap }
|
||||||
|
func (m callMsg) GasTipCap() *big.Int { return m.CallMsg.GasTipCap }
|
||||||
|
func (m callMsg) Gas() uint64 { return m.CallMsg.Gas }
|
||||||
|
func (m callMsg) Value() *big.Int { return m.CallMsg.Value }
|
||||||
|
func (m callMsg) Data() []byte { return m.CallMsg.Data }
|
||||||
|
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
|
||||||
|
|
||||||
|
// filterBackend implements filters.Backend to support filtering for logs without
|
||||||
|
// taking bloom-bits acceleration structures into account.
|
||||||
|
type filterBackend struct {
|
||||||
|
db ethdb.Database
|
||||||
|
bc *core.BlockChain
|
||||||
|
backend *SimulatedBackend
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
|
||||||
|
|
||||||
|
func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") }
|
||||||
|
|
||||||
|
func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
|
||||||
|
if block == rpc.LatestBlockNumber {
|
||||||
|
return fb.bc.CurrentHeader(), nil
|
||||||
|
}
|
||||||
|
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
|
||||||
|
return fb.bc.GetHeaderByHash(hash), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
|
||||||
|
return fb.backend.pendingBlock, fb.backend.pendingReceipts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
|
||||||
|
number := rawdb.ReadHeaderNumber(fb.db, hash)
|
||||||
|
if number == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) {
|
||||||
|
logs := rawdb.ReadLogs(fb.db, hash, number, fb.bc.Config())
|
||||||
|
return logs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
|
||||||
|
return nullSubscription()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
|
||||||
|
return fb.bc.SubscribeChainEvent(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
|
||||||
|
return fb.bc.SubscribeRemovedLogsEvent(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
|
||||||
|
return fb.bc.SubscribeLogsEvent(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
|
||||||
|
return nullSubscription()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 }
|
||||||
|
|
||||||
|
func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) {
|
||||||
|
panic("not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func nullSubscription() event.Subscription {
|
||||||
|
return event.NewSubscription(func(quit <-chan struct{}) error {
|
||||||
|
<-quit
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
1376
accounts/abi/bind/backends/simulated_test.go
Normal file
1376
accounts/abi/bind/backends/simulated_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -32,13 +32,6 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
)
|
)
|
||||||
|
|
||||||
const basefeeWiggleMultiplier = 2
|
|
||||||
|
|
||||||
var (
|
|
||||||
errNoEventSignature = errors.New("no event signature")
|
|
||||||
errEventSignatureMismatch = errors.New("event signature mismatch")
|
|
||||||
)
|
|
||||||
|
|
||||||
// SignerFn is a signer function callback when a contract requires a method to
|
// SignerFn is a signer function callback when a contract requires a method to
|
||||||
// sign the transaction before submission.
|
// sign the transaction before submission.
|
||||||
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
|
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
|
||||||
@@ -48,7 +41,6 @@ type CallOpts struct {
|
|||||||
Pending bool // Whether to operate on the pending state or the last known one
|
Pending bool // Whether to operate on the pending state or the last known one
|
||||||
From common.Address // Optional the sender address, otherwise the first account is used
|
From common.Address // Optional the sender address, otherwise the first account is used
|
||||||
BlockNumber *big.Int // Optional the block number on which the call should be performed
|
BlockNumber *big.Int // Optional the block number on which the call should be performed
|
||||||
BlockHash common.Hash // Optional the block hash on which the call should be performed
|
|
||||||
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +51,11 @@ type TransactOpts struct {
|
|||||||
Nonce *big.Int // Nonce to use for the transaction execution (nil = use pending state)
|
Nonce *big.Int // Nonce to use for the transaction execution (nil = use pending state)
|
||||||
Signer SignerFn // Method to use for signing the transaction (mandatory)
|
Signer SignerFn // Method to use for signing the transaction (mandatory)
|
||||||
|
|
||||||
Value *big.Int // Funds to transfer along the transaction (nil = 0 = no funds)
|
Value *big.Int // Funds to transfer along the transaction (nil = 0 = no funds)
|
||||||
GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
|
GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
|
||||||
GasFeeCap *big.Int // Gas fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
GasFeeCap *big.Int // Gas fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
||||||
GasTipCap *big.Int // Gas priority fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
GasTipCap *big.Int // Gas priority fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
||||||
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
|
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
|
||||||
AccessList types.AccessList // Access list to set for the transaction execution (nil = no access list)
|
|
||||||
|
|
||||||
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
||||||
|
|
||||||
@@ -191,23 +182,6 @@ func (c *BoundContract) Call(opts *CallOpts, results *[]interface{}, method stri
|
|||||||
return ErrNoCode
|
return ErrNoCode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if opts.BlockHash != (common.Hash{}) {
|
|
||||||
bh, ok := c.caller.(BlockHashContractCaller)
|
|
||||||
if !ok {
|
|
||||||
return ErrNoBlockHashState
|
|
||||||
}
|
|
||||||
output, err = bh.CallContractAtHash(ctx, msg, opts.BlockHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(output) == 0 {
|
|
||||||
// Make sure we have a contract to operate on, and bail out otherwise.
|
|
||||||
if code, err = bh.CodeAtHash(ctx, c.address, opts.BlockHash); err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(code) == 0 {
|
|
||||||
return ErrNoCode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
output, err = c.caller.CallContract(ctx, msg, opts.BlockNumber)
|
output, err = c.caller.CallContract(ctx, msg, opts.BlockNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -239,7 +213,7 @@ func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...in
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// todo(rjl493456442) check whether the method is payable or not,
|
// todo(rjl493456442) check the method is payable or not,
|
||||||
// reject invalid transaction at the first place
|
// reject invalid transaction at the first place
|
||||||
return c.transact(opts, &c.address, input)
|
return c.transact(opts, &c.address, input)
|
||||||
}
|
}
|
||||||
@@ -247,7 +221,7 @@ func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...in
|
|||||||
// RawTransact initiates a transaction with the given raw calldata as the input.
|
// RawTransact initiates a transaction with the given raw calldata as the input.
|
||||||
// It's usually used to initiate transactions for invoking **Fallback** function.
|
// It's usually used to initiate transactions for invoking **Fallback** function.
|
||||||
func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (*types.Transaction, error) {
|
func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (*types.Transaction, error) {
|
||||||
// todo(rjl493456442) check whether the method is payable or not,
|
// todo(rjl493456442) check the method is payable or not,
|
||||||
// reject invalid transaction at the first place
|
// reject invalid transaction at the first place
|
||||||
return c.transact(opts, &c.address, calldata)
|
return c.transact(opts, &c.address, calldata)
|
||||||
}
|
}
|
||||||
@@ -280,7 +254,7 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add
|
|||||||
if gasFeeCap == nil {
|
if gasFeeCap == nil {
|
||||||
gasFeeCap = new(big.Int).Add(
|
gasFeeCap = new(big.Int).Add(
|
||||||
gasTipCap,
|
gasTipCap,
|
||||||
new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)),
|
new(big.Int).Mul(head.BaseFee, big.NewInt(2)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if gasFeeCap.Cmp(gasTipCap) < 0 {
|
if gasFeeCap.Cmp(gasTipCap) < 0 {
|
||||||
@@ -301,21 +275,20 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
baseTx := &types.DynamicFeeTx{
|
baseTx := &types.DynamicFeeTx{
|
||||||
To: contract,
|
To: contract,
|
||||||
Nonce: nonce,
|
Nonce: nonce,
|
||||||
GasFeeCap: gasFeeCap,
|
GasFeeCap: gasFeeCap,
|
||||||
GasTipCap: gasTipCap,
|
GasTipCap: gasTipCap,
|
||||||
Gas: gasLimit,
|
Gas: gasLimit,
|
||||||
Value: value,
|
Value: value,
|
||||||
Data: input,
|
Data: input,
|
||||||
AccessList: opts.AccessList,
|
|
||||||
}
|
}
|
||||||
return types.NewTx(baseTx), nil
|
return types.NewTx(baseTx), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
|
func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
|
||||||
if opts.GasFeeCap != nil || opts.GasTipCap != nil || opts.AccessList != nil {
|
if opts.GasFeeCap != nil || opts.GasTipCap != nil {
|
||||||
return nil, errors.New("maxFeePerGas or maxPriorityFeePerGas or accessList specified but london is not active yet")
|
return nil, errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet")
|
||||||
}
|
}
|
||||||
// Normalize value
|
// Normalize value
|
||||||
value := opts.Value
|
value := opts.Value
|
||||||
@@ -398,8 +371,6 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
|
|||||||
)
|
)
|
||||||
if opts.GasPrice != nil {
|
if opts.GasPrice != nil {
|
||||||
rawTx, err = c.createLegacyTx(opts, contract, input)
|
rawTx, err = c.createLegacyTx(opts, contract, input)
|
||||||
} else if opts.GasFeeCap != nil && opts.GasTipCap != nil {
|
|
||||||
rawTx, err = c.createDynamicTx(opts, contract, input, nil)
|
|
||||||
} else {
|
} else {
|
||||||
// Only query for basefee if gasPrice not specified
|
// Only query for basefee if gasPrice not specified
|
||||||
if head, errHead := c.transactor.HeaderByNumber(ensureContext(opts.Context), nil); errHead != nil {
|
if head, errHead := c.transactor.HeaderByNumber(ensureContext(opts.Context), nil); errHead != nil {
|
||||||
@@ -463,7 +434,7 @@ func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]int
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
sub := event.NewSubscription(func(quit <-chan struct{}) error {
|
sub, err := event.NewSubscription(func(quit <-chan struct{}) error {
|
||||||
for _, log := range buff {
|
for _, log := range buff {
|
||||||
select {
|
select {
|
||||||
case logs <- log:
|
case logs <- log:
|
||||||
@@ -472,8 +443,11 @@ func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
}), nil
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
return logs, sub, nil
|
return logs, sub, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,12 +484,8 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
|
|||||||
|
|
||||||
// UnpackLog unpacks a retrieved log into the provided output structure.
|
// UnpackLog unpacks a retrieved log into the provided output structure.
|
||||||
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error {
|
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error {
|
||||||
// Anonymous events are not supported.
|
|
||||||
if len(log.Topics) == 0 {
|
|
||||||
return errNoEventSignature
|
|
||||||
}
|
|
||||||
if log.Topics[0] != c.abi.Events[event].ID {
|
if log.Topics[0] != c.abi.Events[event].ID {
|
||||||
return errEventSignatureMismatch
|
return fmt.Errorf("event signature mismatch")
|
||||||
}
|
}
|
||||||
if len(log.Data) > 0 {
|
if len(log.Data) > 0 {
|
||||||
if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil {
|
if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil {
|
||||||
@@ -533,12 +503,8 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log)
|
|||||||
|
|
||||||
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
|
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
|
||||||
func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
|
func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
|
||||||
// Anonymous events are not supported.
|
|
||||||
if len(log.Topics) == 0 {
|
|
||||||
return errNoEventSignature
|
|
||||||
}
|
|
||||||
if log.Topics[0] != c.abi.Events[event].ID {
|
if log.Topics[0] != c.abi.Events[event].ID {
|
||||||
return errEventSignatureMismatch
|
return fmt.Errorf("event signature mismatch")
|
||||||
}
|
}
|
||||||
if len(log.Data) > 0 {
|
if len(log.Data) > 0 {
|
||||||
if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil {
|
if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil {
|
||||||
|
|||||||
@@ -114,28 +114,7 @@ func (mc *mockPendingCaller) PendingCallContract(ctx context.Context, call ether
|
|||||||
return mc.pendingCallContractBytes, mc.pendingCallContractErr
|
return mc.pendingCallContractBytes, mc.pendingCallContractErr
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockBlockHashCaller struct {
|
|
||||||
*mockCaller
|
|
||||||
codeAtHashBytes []byte
|
|
||||||
codeAtHashErr error
|
|
||||||
codeAtHashCalled bool
|
|
||||||
callContractAtHashCalled bool
|
|
||||||
callContractAtHashBytes []byte
|
|
||||||
callContractAtHashErr error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mc *mockBlockHashCaller) CodeAtHash(ctx context.Context, contract common.Address, hash common.Hash) ([]byte, error) {
|
|
||||||
mc.codeAtHashCalled = true
|
|
||||||
return mc.codeAtHashBytes, mc.codeAtHashErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mc *mockBlockHashCaller) CallContractAtHash(ctx context.Context, call ethereum.CallMsg, hash common.Hash) ([]byte, error) {
|
|
||||||
mc.callContractAtHashCalled = true
|
|
||||||
return mc.callContractAtHashBytes, mc.callContractAtHashErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPassingBlockNumber(t *testing.T) {
|
func TestPassingBlockNumber(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
mc := &mockPendingCaller{
|
mc := &mockPendingCaller{
|
||||||
mockCaller: &mockCaller{
|
mockCaller: &mockCaller{
|
||||||
codeAtBytes: []byte{1, 2, 3},
|
codeAtBytes: []byte{1, 2, 3},
|
||||||
@@ -187,7 +166,6 @@ func TestPassingBlockNumber(t *testing.T) {
|
|||||||
const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158"
|
const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158"
|
||||||
|
|
||||||
func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
|
func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
hash := crypto.Keccak256Hash([]byte("testName"))
|
hash := crypto.Keccak256Hash([]byte("testName"))
|
||||||
topics := []common.Hash{
|
topics := []common.Hash{
|
||||||
crypto.Keccak256Hash([]byte("received(string,address,uint256,bytes)")),
|
crypto.Keccak256Hash([]byte("received(string,address,uint256,bytes)")),
|
||||||
@@ -208,26 +186,7 @@ func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
|
|||||||
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackAnonymousLogIntoMap(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
mockLog := newMockLog(nil, common.HexToHash("0x0"))
|
|
||||||
|
|
||||||
abiString := `[{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"received","type":"event"}]`
|
|
||||||
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
|
|
||||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
|
|
||||||
|
|
||||||
var received map[string]interface{}
|
|
||||||
err := bc.UnpackLogIntoMap(received, "received", mockLog)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("unpacking anonymous event is not supported")
|
|
||||||
}
|
|
||||||
if err.Error() != "no event signature" {
|
|
||||||
t.Errorf("expected error 'no event signature', got '%s'", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
|
func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
|
sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -253,7 +212,6 @@ func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
|
func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")})
|
arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -279,7 +237,6 @@ func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
|
func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
|
mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
|
||||||
addrBytes := mockAddress.Bytes()
|
addrBytes := mockAddress.Bytes()
|
||||||
hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)"))
|
hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)"))
|
||||||
@@ -306,7 +263,6 @@ func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
|
func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
bytes := []byte{1, 2, 3, 4, 5}
|
bytes := []byte{1, 2, 3, 4, 5}
|
||||||
hash := crypto.Keccak256Hash(bytes)
|
hash := crypto.Keccak256Hash(bytes)
|
||||||
topics := []common.Hash{
|
topics := []common.Hash{
|
||||||
@@ -329,7 +285,6 @@ func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTransactGasFee(t *testing.T) {
|
func TestTransactGasFee(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
// GasTipCap and GasFeeCap
|
// GasTipCap and GasFeeCap
|
||||||
@@ -405,7 +360,6 @@ func newMockLog(topics []common.Hash, txHash common.Hash) types.Log {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCall(t *testing.T) {
|
func TestCall(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var method, methodWithArg = "something", "somethingArrrrg"
|
var method, methodWithArg = "something", "somethingArrrrg"
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name, method string
|
name, method string
|
||||||
@@ -429,15 +383,6 @@ func TestCall(t *testing.T) {
|
|||||||
Pending: true,
|
Pending: true,
|
||||||
},
|
},
|
||||||
method: method,
|
method: method,
|
||||||
}, {
|
|
||||||
name: "ok hash",
|
|
||||||
mc: &mockBlockHashCaller{
|
|
||||||
codeAtHashBytes: []byte{0},
|
|
||||||
},
|
|
||||||
opts: &bind.CallOpts{
|
|
||||||
BlockHash: common.Hash{0xaa},
|
|
||||||
},
|
|
||||||
method: method,
|
|
||||||
}, {
|
}, {
|
||||||
name: "pack error, no method",
|
name: "pack error, no method",
|
||||||
mc: new(mockCaller),
|
mc: new(mockCaller),
|
||||||
@@ -451,14 +396,6 @@ func TestCall(t *testing.T) {
|
|||||||
},
|
},
|
||||||
method: method,
|
method: method,
|
||||||
wantErrExact: bind.ErrNoPendingState,
|
wantErrExact: bind.ErrNoPendingState,
|
||||||
}, {
|
|
||||||
name: "interface error, blockHash but not a BlockHashContractCaller",
|
|
||||||
mc: new(mockCaller),
|
|
||||||
opts: &bind.CallOpts{
|
|
||||||
BlockHash: common.Hash{0xaa},
|
|
||||||
},
|
|
||||||
method: method,
|
|
||||||
wantErrExact: bind.ErrNoBlockHashState,
|
|
||||||
}, {
|
}, {
|
||||||
name: "pending call canceled",
|
name: "pending call canceled",
|
||||||
mc: &mockPendingCaller{
|
mc: &mockPendingCaller{
|
||||||
@@ -506,34 +443,6 @@ func TestCall(t *testing.T) {
|
|||||||
mc: new(mockCaller),
|
mc: new(mockCaller),
|
||||||
method: method,
|
method: method,
|
||||||
wantErrExact: bind.ErrNoCode,
|
wantErrExact: bind.ErrNoCode,
|
||||||
}, {
|
|
||||||
name: "call contract at hash error",
|
|
||||||
mc: &mockBlockHashCaller{
|
|
||||||
callContractAtHashErr: context.DeadlineExceeded,
|
|
||||||
},
|
|
||||||
opts: &bind.CallOpts{
|
|
||||||
BlockHash: common.Hash{0xaa},
|
|
||||||
},
|
|
||||||
method: method,
|
|
||||||
wantErrExact: context.DeadlineExceeded,
|
|
||||||
}, {
|
|
||||||
name: "code at error",
|
|
||||||
mc: &mockBlockHashCaller{
|
|
||||||
codeAtHashErr: errors.New(""),
|
|
||||||
},
|
|
||||||
opts: &bind.CallOpts{
|
|
||||||
BlockHash: common.Hash{0xaa},
|
|
||||||
},
|
|
||||||
method: method,
|
|
||||||
wantErr: true,
|
|
||||||
}, {
|
|
||||||
name: "no code at hash",
|
|
||||||
mc: new(mockBlockHashCaller),
|
|
||||||
opts: &bind.CallOpts{
|
|
||||||
BlockHash: common.Hash{0xaa},
|
|
||||||
},
|
|
||||||
method: method,
|
|
||||||
wantErrExact: bind.ErrNoCode,
|
|
||||||
}, {
|
}, {
|
||||||
name: "unpack error missing arg",
|
name: "unpack error missing arg",
|
||||||
mc: &mockCaller{
|
mc: &mockCaller{
|
||||||
@@ -581,7 +490,6 @@ func TestCall(t *testing.T) {
|
|||||||
|
|
||||||
// TestCrashers contains some strings which previously caused the abi codec to crash.
|
// TestCrashers contains some strings which previously caused the abi codec to crash.
|
||||||
func TestCrashers(t *testing.T) {
|
func TestCrashers(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"_1"}]}]}]`))
|
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"_1"}]}]}]`))
|
||||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"&"}]}]}]`))
|
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"&"}]}]}]`))
|
||||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"----"}]}]}]`))
|
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"----"}]}]}]`))
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ package bind
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/format"
|
"go/format"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -38,6 +39,8 @@ type Lang int
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
LangGo Lang = iota
|
LangGo Lang = iota
|
||||||
|
LangJava
|
||||||
|
LangObjC
|
||||||
)
|
)
|
||||||
|
|
||||||
func isKeyWord(arg string) bool {
|
func isKeyWord(arg string) bool {
|
||||||
@@ -79,7 +82,7 @@ func isKeyWord(arg string) bool {
|
|||||||
|
|
||||||
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
|
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
|
||||||
// to be used as is in client code, but rather as an intermediate struct which
|
// to be used as is in client code, but rather as an intermediate struct which
|
||||||
// enforces compile time type safety and naming convention as opposed to having to
|
// enforces compile time type safety and naming convention opposed to having to
|
||||||
// manually maintain hard coded strings that break on runtime.
|
// manually maintain hard coded strings that break on runtime.
|
||||||
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) {
|
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) {
|
||||||
var (
|
var (
|
||||||
@@ -133,19 +136,12 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
|
|||||||
// Normalize the method for capital cases and non-anonymous inputs/outputs
|
// Normalize the method for capital cases and non-anonymous inputs/outputs
|
||||||
normalized := original
|
normalized := original
|
||||||
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
|
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
|
||||||
|
|
||||||
// Ensure there is no duplicated identifier
|
// Ensure there is no duplicated identifier
|
||||||
var identifiers = callIdentifiers
|
var identifiers = callIdentifiers
|
||||||
if !original.IsConstant() {
|
if !original.IsConstant() {
|
||||||
identifiers = transactIdentifiers
|
identifiers = transactIdentifiers
|
||||||
}
|
}
|
||||||
// Name shouldn't start with a digit. It will make the generated code invalid.
|
|
||||||
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
|
|
||||||
normalizedName = fmt.Sprintf("M%s", normalizedName)
|
|
||||||
normalizedName = abi.ResolveNameConflict(normalizedName, func(name string) bool {
|
|
||||||
_, ok := identifiers[name]
|
|
||||||
return ok
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if identifiers[normalizedName] {
|
if identifiers[normalizedName] {
|
||||||
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
|
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
|
||||||
}
|
}
|
||||||
@@ -189,14 +185,6 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
|
|||||||
|
|
||||||
// Ensure there is no duplicated identifier
|
// Ensure there is no duplicated identifier
|
||||||
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
|
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
|
||||||
// Name shouldn't start with a digit. It will make the generated code invalid.
|
|
||||||
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
|
|
||||||
normalizedName = fmt.Sprintf("E%s", normalizedName)
|
|
||||||
normalizedName = abi.ResolveNameConflict(normalizedName, func(name string) bool {
|
|
||||||
_, ok := eventIdentifiers[name]
|
|
||||||
return ok
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if eventIdentifiers[normalizedName] {
|
if eventIdentifiers[normalizedName] {
|
||||||
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
|
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
|
||||||
}
|
}
|
||||||
@@ -233,6 +221,11 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
|
|||||||
if evmABI.HasReceive() {
|
if evmABI.HasReceive() {
|
||||||
receive = &tmplMethod{Original: evmABI.Receive}
|
receive = &tmplMethod{Original: evmABI.Receive}
|
||||||
}
|
}
|
||||||
|
// There is no easy way to pass arbitrary java objects to the Go side.
|
||||||
|
if len(structs) > 0 && lang == LangJava {
|
||||||
|
return "", errors.New("java binding for tuple arguments is not supported yet")
|
||||||
|
}
|
||||||
|
|
||||||
contracts[types[i]] = &tmplContract{
|
contracts[types[i]] = &tmplContract{
|
||||||
Type: capitalise(types[i]),
|
Type: capitalise(types[i]),
|
||||||
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
|
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
|
||||||
@@ -252,7 +245,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
|
|||||||
}
|
}
|
||||||
// Parse library references.
|
// Parse library references.
|
||||||
for pattern, name := range libs {
|
for pattern, name := range libs {
|
||||||
matched, err := regexp.MatchString("__\\$"+pattern+"\\$__", contracts[types[i]].InputBin)
|
matched, err := regexp.Match("__\\$"+pattern+"\\$__", []byte(contracts[types[i]].InputBin))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not search for pattern", "pattern", pattern, "contract", contracts[types[i]], "err", err)
|
log.Error("Could not search for pattern", "pattern", pattern, "contract", contracts[types[i]], "err", err)
|
||||||
}
|
}
|
||||||
@@ -305,7 +298,8 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
|
|||||||
// bindType is a set of type binders that convert Solidity types to some supported
|
// bindType is a set of type binders that convert Solidity types to some supported
|
||||||
// programming language types.
|
// programming language types.
|
||||||
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
||||||
LangGo: bindTypeGo,
|
LangGo: bindTypeGo,
|
||||||
|
LangJava: bindTypeJava,
|
||||||
}
|
}
|
||||||
|
|
||||||
// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
|
// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
|
||||||
@@ -348,10 +342,86 @@ func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bindBasicTypeJava converts basic solidity types(except array, slice and tuple) to Java ones.
|
||||||
|
func bindBasicTypeJava(kind abi.Type) string {
|
||||||
|
switch kind.T {
|
||||||
|
case abi.AddressTy:
|
||||||
|
return "Address"
|
||||||
|
case abi.IntTy, abi.UintTy:
|
||||||
|
// Note that uint and int (without digits) are also matched,
|
||||||
|
// these are size 256, and will translate to BigInt (the default).
|
||||||
|
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return kind.String()
|
||||||
|
}
|
||||||
|
// All unsigned integers should be translated to BigInt since gomobile doesn't
|
||||||
|
// support them.
|
||||||
|
if parts[1] == "u" {
|
||||||
|
return "BigInt"
|
||||||
|
}
|
||||||
|
|
||||||
|
namedSize := map[string]string{
|
||||||
|
"8": "byte",
|
||||||
|
"16": "short",
|
||||||
|
"32": "int",
|
||||||
|
"64": "long",
|
||||||
|
}[parts[2]]
|
||||||
|
|
||||||
|
// default to BigInt
|
||||||
|
if namedSize == "" {
|
||||||
|
namedSize = "BigInt"
|
||||||
|
}
|
||||||
|
return namedSize
|
||||||
|
case abi.FixedBytesTy, abi.BytesTy:
|
||||||
|
return "byte[]"
|
||||||
|
case abi.BoolTy:
|
||||||
|
return "boolean"
|
||||||
|
case abi.StringTy:
|
||||||
|
return "String"
|
||||||
|
case abi.FunctionTy:
|
||||||
|
return "byte[24]"
|
||||||
|
default:
|
||||||
|
return kind.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pluralizeJavaType explicitly converts multidimensional types to predefined
|
||||||
|
// types in go side.
|
||||||
|
func pluralizeJavaType(typ string) string {
|
||||||
|
switch typ {
|
||||||
|
case "boolean":
|
||||||
|
return "Bools"
|
||||||
|
case "String":
|
||||||
|
return "Strings"
|
||||||
|
case "Address":
|
||||||
|
return "Addresses"
|
||||||
|
case "byte[]":
|
||||||
|
return "Binaries"
|
||||||
|
case "BigInt":
|
||||||
|
return "BigInts"
|
||||||
|
}
|
||||||
|
return typ + "[]"
|
||||||
|
}
|
||||||
|
|
||||||
|
// bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
|
||||||
|
// from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
|
||||||
|
// mapped will use an upscaled type (e.g. BigDecimal).
|
||||||
|
func bindTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
|
||||||
|
switch kind.T {
|
||||||
|
case abi.TupleTy:
|
||||||
|
return structs[kind.TupleRawName+kind.String()].Name
|
||||||
|
case abi.ArrayTy, abi.SliceTy:
|
||||||
|
return pluralizeJavaType(bindTypeJava(*kind.Elem, structs))
|
||||||
|
default:
|
||||||
|
return bindBasicTypeJava(kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// bindTopicType is a set of type binders that convert Solidity types to some
|
// bindTopicType is a set of type binders that convert Solidity types to some
|
||||||
// supported programming language topic types.
|
// supported programming language topic types.
|
||||||
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
||||||
LangGo: bindTopicTypeGo,
|
LangGo: bindTopicTypeGo,
|
||||||
|
LangJava: bindTopicTypeJava,
|
||||||
}
|
}
|
||||||
|
|
||||||
// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
|
// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
|
||||||
@@ -363,7 +433,7 @@ func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
|||||||
// parameters that are not value types i.e. arrays and structs are not
|
// parameters that are not value types i.e. arrays and structs are not
|
||||||
// stored directly but instead a keccak256-hash of an encoding is stored.
|
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||||
//
|
//
|
||||||
// We only convert strings and bytes to hash, still need to deal with
|
// We only convert stringS and bytes to hash, still need to deal with
|
||||||
// array(both fixed-size and dynamic-size) and struct.
|
// array(both fixed-size and dynamic-size) and struct.
|
||||||
if bound == "string" || bound == "[]byte" {
|
if bound == "string" || bound == "[]byte" {
|
||||||
bound = "common.Hash"
|
bound = "common.Hash"
|
||||||
@@ -371,10 +441,28 @@ func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
|||||||
return bound
|
return bound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bindTopicTypeJava converts a Solidity topic type to a Java one. It is almost the same
|
||||||
|
// functionality as for simple types, but dynamic types get converted to hashes.
|
||||||
|
func bindTopicTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
|
||||||
|
bound := bindTypeJava(kind, structs)
|
||||||
|
|
||||||
|
// todo(rjl493456442) according solidity documentation, indexed event
|
||||||
|
// parameters that are not value types i.e. arrays and structs are not
|
||||||
|
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||||
|
//
|
||||||
|
// We only convert strings and bytes to hash, still need to deal with
|
||||||
|
// array(both fixed-size and dynamic-size) and struct.
|
||||||
|
if bound == "String" || bound == "byte[]" {
|
||||||
|
bound = "Hash"
|
||||||
|
}
|
||||||
|
return bound
|
||||||
|
}
|
||||||
|
|
||||||
// bindStructType is a set of type binders that convert Solidity tuple types to some supported
|
// bindStructType is a set of type binders that convert Solidity tuple types to some supported
|
||||||
// programming language struct definition.
|
// programming language struct definition.
|
||||||
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
||||||
LangGo: bindStructTypeGo,
|
LangGo: bindStructTypeGo,
|
||||||
|
LangJava: bindStructTypeJava,
|
||||||
}
|
}
|
||||||
|
|
||||||
// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
|
// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
|
||||||
@@ -423,10 +511,74 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bindStructTypeJava converts a Solidity tuple type to a Java one and records the mapping
|
||||||
|
// in the given map.
|
||||||
|
// Notably, this function will resolve and record nested struct recursively.
|
||||||
|
func bindStructTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
|
||||||
|
switch kind.T {
|
||||||
|
case abi.TupleTy:
|
||||||
|
// We compose a raw struct name and a canonical parameter expression
|
||||||
|
// together here. The reason is before solidity v0.5.11, kind.TupleRawName
|
||||||
|
// is empty, so we use canonical parameter expression to distinguish
|
||||||
|
// different struct definition. From the consideration of backward
|
||||||
|
// compatibility, we concat these two together so that if kind.TupleRawName
|
||||||
|
// is not empty, it can have unique id.
|
||||||
|
id := kind.TupleRawName + kind.String()
|
||||||
|
if s, exist := structs[id]; exist {
|
||||||
|
return s.Name
|
||||||
|
}
|
||||||
|
var fields []*tmplField
|
||||||
|
for i, elem := range kind.TupleElems {
|
||||||
|
field := bindStructTypeJava(*elem, structs)
|
||||||
|
fields = append(fields, &tmplField{Type: field, Name: decapitalise(kind.TupleRawNames[i]), SolKind: *elem})
|
||||||
|
}
|
||||||
|
name := kind.TupleRawName
|
||||||
|
if name == "" {
|
||||||
|
name = fmt.Sprintf("Class%d", len(structs))
|
||||||
|
}
|
||||||
|
structs[id] = &tmplStruct{
|
||||||
|
Name: name,
|
||||||
|
Fields: fields,
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
case abi.ArrayTy, abi.SliceTy:
|
||||||
|
return pluralizeJavaType(bindStructTypeJava(*kind.Elem, structs))
|
||||||
|
default:
|
||||||
|
return bindBasicTypeJava(kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// namedType is a set of functions that transform language specific types to
|
// namedType is a set of functions that transform language specific types to
|
||||||
// named versions that may be used inside method names.
|
// named versions that may be used inside method names.
|
||||||
var namedType = map[Lang]func(string, abi.Type) string{
|
var namedType = map[Lang]func(string, abi.Type) string{
|
||||||
LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") },
|
LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") },
|
||||||
|
LangJava: namedTypeJava,
|
||||||
|
}
|
||||||
|
|
||||||
|
// namedTypeJava converts some primitive data types to named variants that can
|
||||||
|
// be used as parts of method names.
|
||||||
|
func namedTypeJava(javaKind string, solKind abi.Type) string {
|
||||||
|
switch javaKind {
|
||||||
|
case "byte[]":
|
||||||
|
return "Binary"
|
||||||
|
case "boolean":
|
||||||
|
return "Bool"
|
||||||
|
default:
|
||||||
|
parts := regexp.MustCompile(`(u)?int([0-9]*)(\[[0-9]*\])?`).FindStringSubmatch(solKind.String())
|
||||||
|
if len(parts) != 4 {
|
||||||
|
return javaKind
|
||||||
|
}
|
||||||
|
switch parts[2] {
|
||||||
|
case "8", "16", "32", "64":
|
||||||
|
if parts[3] == "" {
|
||||||
|
return capitalise(fmt.Sprintf("%sint%s", parts[1], parts[2]))
|
||||||
|
}
|
||||||
|
return capitalise(fmt.Sprintf("%sint%ss", parts[1], parts[2]))
|
||||||
|
|
||||||
|
default:
|
||||||
|
return javaKind
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// alias returns an alias of the given string based on the aliasing rules
|
// alias returns an alias of the given string based on the aliasing rules
|
||||||
@@ -441,7 +593,8 @@ func alias(aliases map[string]string, n string) string {
|
|||||||
// methodNormalizer is a name transformer that modifies Solidity method names to
|
// methodNormalizer is a name transformer that modifies Solidity method names to
|
||||||
// conform to target language naming conventions.
|
// conform to target language naming conventions.
|
||||||
var methodNormalizer = map[Lang]func(string) string{
|
var methodNormalizer = map[Lang]func(string) string{
|
||||||
LangGo: abi.ToCamelCase,
|
LangGo: abi.ToCamelCase,
|
||||||
|
LangJava: decapitalise,
|
||||||
}
|
}
|
||||||
|
|
||||||
// capitalise makes a camel-case string which starts with an upper case character.
|
// capitalise makes a camel-case string which starts with an upper case character.
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,487 +0,0 @@
|
|||||||
// Code generated - DO NOT EDIT.
|
|
||||||
// This file is a generated binding and any manual changes will be lost.
|
|
||||||
|
|
||||||
package {{.Package}}
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/big"
|
|
||||||
"strings"
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
ethereum "github.com/ethereum/go-ethereum"
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"github.com/ethereum/go-ethereum/event"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var (
|
|
||||||
_ = errors.New
|
|
||||||
_ = big.NewInt
|
|
||||||
_ = strings.NewReader
|
|
||||||
_ = ethereum.NotFound
|
|
||||||
_ = bind.Bind
|
|
||||||
_ = common.Big1
|
|
||||||
_ = types.BloomLookup
|
|
||||||
_ = event.NewSubscription
|
|
||||||
_ = abi.ConvertType
|
|
||||||
)
|
|
||||||
|
|
||||||
{{$structs := .Structs}}
|
|
||||||
{{range $structs}}
|
|
||||||
// {{.Name}} is an auto generated low-level Go binding around an user-defined struct.
|
|
||||||
type {{.Name}} struct {
|
|
||||||
{{range $field := .Fields}}
|
|
||||||
{{$field.Name}} {{$field.Type}}{{end}}
|
|
||||||
}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{range $contract := .Contracts}}
|
|
||||||
// {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract.
|
|
||||||
var {{.Type}}MetaData = &bind.MetaData{
|
|
||||||
ABI: "{{.InputABI}}",
|
|
||||||
{{if $contract.FuncSigs -}}
|
|
||||||
Sigs: map[string]string{
|
|
||||||
{{range $strsig, $binsig := .FuncSigs}}"{{$binsig}}": "{{$strsig}}",
|
|
||||||
{{end}}
|
|
||||||
},
|
|
||||||
{{end -}}
|
|
||||||
{{if .InputBin -}}
|
|
||||||
Bin: "0x{{.InputBin}}",
|
|
||||||
{{end}}
|
|
||||||
}
|
|
||||||
// {{.Type}}ABI is the input ABI used to generate the binding from.
|
|
||||||
// Deprecated: Use {{.Type}}MetaData.ABI instead.
|
|
||||||
var {{.Type}}ABI = {{.Type}}MetaData.ABI
|
|
||||||
|
|
||||||
{{if $contract.FuncSigs}}
|
|
||||||
// Deprecated: Use {{.Type}}MetaData.Sigs instead.
|
|
||||||
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
|
|
||||||
var {{.Type}}FuncSigs = {{.Type}}MetaData.Sigs
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{if .InputBin}}
|
|
||||||
// {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
|
|
||||||
// Deprecated: Use {{.Type}}MetaData.Bin instead.
|
|
||||||
var {{.Type}}Bin = {{.Type}}MetaData.Bin
|
|
||||||
|
|
||||||
// Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
|
|
||||||
func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) {
|
|
||||||
parsed, err := {{.Type}}MetaData.GetAbi()
|
|
||||||
if err != nil {
|
|
||||||
return common.Address{}, nil, nil, err
|
|
||||||
}
|
|
||||||
if parsed == nil {
|
|
||||||
return common.Address{}, nil, nil, errors.New("GetABI returned nil")
|
|
||||||
}
|
|
||||||
{{range $pattern, $name := .Libraries}}
|
|
||||||
{{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend)
|
|
||||||
{{$contract.Type}}Bin = strings.ReplaceAll({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:])
|
|
||||||
{{end}}
|
|
||||||
address, tx, contract, err := bind.DeployContract(auth, *parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
|
|
||||||
if err != nil {
|
|
||||||
return common.Address{}, nil, nil, err
|
|
||||||
}
|
|
||||||
return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
|
|
||||||
}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
// {{.Type}} is an auto generated Go binding around an Ethereum contract.
|
|
||||||
type {{.Type}} struct {
|
|
||||||
{{.Type}}Caller // Read-only binding to the contract
|
|
||||||
{{.Type}}Transactor // Write-only binding to the contract
|
|
||||||
{{.Type}}Filterer // Log filterer for contract events
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
|
|
||||||
type {{.Type}}Caller struct {
|
|
||||||
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
|
|
||||||
type {{.Type}}Transactor struct {
|
|
||||||
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}Filterer is an auto generated log filtering Go binding around an Ethereum contract events.
|
|
||||||
type {{.Type}}Filterer struct {
|
|
||||||
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
|
|
||||||
// with pre-set call and transact options.
|
|
||||||
type {{.Type}}Session struct {
|
|
||||||
Contract *{{.Type}} // Generic contract binding to set the session for
|
|
||||||
CallOpts bind.CallOpts // Call options to use throughout this session
|
|
||||||
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
|
|
||||||
// with pre-set call options.
|
|
||||||
type {{.Type}}CallerSession struct {
|
|
||||||
Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
|
|
||||||
CallOpts bind.CallOpts // Call options to use throughout this session
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
|
|
||||||
// with pre-set transact options.
|
|
||||||
type {{.Type}}TransactorSession struct {
|
|
||||||
Contract *{{.Type}}Transactor // Generic contract transactor binding to set the session for
|
|
||||||
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
|
|
||||||
type {{.Type}}Raw struct {
|
|
||||||
Contract *{{.Type}} // Generic contract binding to access the raw methods on
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
|
|
||||||
type {{.Type}}CallerRaw struct {
|
|
||||||
Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
|
|
||||||
type {{.Type}}TransactorRaw struct {
|
|
||||||
Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
|
|
||||||
}
|
|
||||||
|
|
||||||
// New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
|
|
||||||
func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) {
|
|
||||||
contract, err := bind{{.Type}}(address, backend, backend, backend)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
|
|
||||||
func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) {
|
|
||||||
contract, err := bind{{.Type}}(address, caller, nil, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &{{.Type}}Caller{contract: contract}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
|
|
||||||
func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) {
|
|
||||||
contract, err := bind{{.Type}}(address, nil, transactor, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &{{.Type}}Transactor{contract: contract}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// New{{.Type}}Filterer creates a new log filterer instance of {{.Type}}, bound to a specific deployed contract.
|
|
||||||
func New{{.Type}}Filterer(address common.Address, filterer bind.ContractFilterer) (*{{.Type}}Filterer, error) {
|
|
||||||
contract, err := bind{{.Type}}(address, nil, nil, filterer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &{{.Type}}Filterer{contract: contract}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// bind{{.Type}} binds a generic wrapper to an already deployed contract.
|
|
||||||
func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) {
|
|
||||||
parsed, err := {{.Type}}MetaData.GetAbi()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return bind.NewBoundContract(address, *parsed, caller, transactor, filterer), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call invokes the (constant) contract method with params as input values and
|
|
||||||
// sets the output to result. The result type might be a single field for simple
|
|
||||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
|
||||||
// returns.
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error {
|
|
||||||
return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transfer initiates a plain transaction to move funds to the contract, calling
|
|
||||||
// its default method if one is available.
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transact invokes the (paid) contract method with params as input values.
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call invokes the (constant) contract method with params as input values and
|
|
||||||
// sets the output to result. The result type might be a single field for simple
|
|
||||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
|
||||||
// returns.
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error {
|
|
||||||
return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transfer initiates a plain transaction to move funds to the contract, calling
|
|
||||||
// its default method if one is available.
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.contract.Transfer(opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transact invokes the (paid) contract method with params as input values.
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...)
|
|
||||||
}
|
|
||||||
|
|
||||||
{{range .Calls}}
|
|
||||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Caller) {{.Normalized.Name}}(opts *bind.CallOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} },{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}}{{end}} error) {
|
|
||||||
var out []interface{}
|
|
||||||
err := _{{$contract.Type}}.contract.Call(opts, &out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
|
||||||
{{if .Structured}}
|
|
||||||
outstruct := new(struct{ {{range .Normalized.Outputs}} {{.Name}} {{bindtype .Type $structs}}; {{end}} })
|
|
||||||
if err != nil {
|
|
||||||
return *outstruct, err
|
|
||||||
}
|
|
||||||
{{range $i, $t := .Normalized.Outputs}}
|
|
||||||
outstruct.{{.Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}}
|
|
||||||
|
|
||||||
return *outstruct, err
|
|
||||||
{{else}}
|
|
||||||
if err != nil {
|
|
||||||
return {{range $i, $_ := .Normalized.Outputs}}*new({{bindtype .Type $structs}}), {{end}} err
|
|
||||||
}
|
|
||||||
{{range $i, $t := .Normalized.Outputs}}
|
|
||||||
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}}
|
|
||||||
|
|
||||||
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} err
|
|
||||||
{{end}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) {
|
|
||||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}CallerSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) {
|
|
||||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
|
||||||
}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{range .Transacts}}
|
|
||||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
|
|
||||||
}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{if .Fallback}}
|
|
||||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Fallback.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Fallback(opts *bind.TransactOpts, calldata []byte) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.contract.RawTransact(opts, calldata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Fallback.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) Fallback(calldata []byte) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Fallback.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Fallback(calldata []byte) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata)
|
|
||||||
}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{if .Receive}}
|
|
||||||
// Receive is a paid mutator transaction binding the contract receive function.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Receive.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Receive(opts *bind.TransactOpts) (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.contract.RawTransact(opts, nil) // calldata is disallowed for receive function
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receive is a paid mutator transaction binding the contract receive function.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Receive.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) Receive() (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receive is a paid mutator transaction binding the contract receive function.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Receive.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Receive() (*types.Transaction, error) {
|
|
||||||
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts)
|
|
||||||
}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{range .Events}}
|
|
||||||
// {{$contract.Type}}{{.Normalized.Name}}Iterator is returned from Filter{{.Normalized.Name}} and is used to iterate over the raw logs and unpacked data for {{.Normalized.Name}} events raised by the {{$contract.Type}} contract.
|
|
||||||
type {{$contract.Type}}{{.Normalized.Name}}Iterator struct {
|
|
||||||
Event *{{$contract.Type}}{{.Normalized.Name}} // Event containing the contract specifics and raw log
|
|
||||||
|
|
||||||
contract *bind.BoundContract // Generic contract to use for unpacking event data
|
|
||||||
event string // Event name to use for unpacking event data
|
|
||||||
|
|
||||||
logs chan types.Log // Log channel receiving the found contract events
|
|
||||||
sub ethereum.Subscription // Subscription for errors, completion and termination
|
|
||||||
done bool // Whether the subscription completed delivering logs
|
|
||||||
fail error // Occurred error to stop iteration
|
|
||||||
}
|
|
||||||
// Next advances the iterator to the subsequent event, returning whether there
|
|
||||||
// are any more events found. In case of a retrieval or parsing error, false is
|
|
||||||
// returned and Error() can be queried for the exact failure.
|
|
||||||
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Next() bool {
|
|
||||||
// If the iterator failed, stop iterating
|
|
||||||
if (it.fail != nil) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// If the iterator completed, deliver directly whatever's available
|
|
||||||
if (it.done) {
|
|
||||||
select {
|
|
||||||
case log := <-it.logs:
|
|
||||||
it.Event = new({{$contract.Type}}{{.Normalized.Name}})
|
|
||||||
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
|
|
||||||
it.fail = err
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
it.Event.Raw = log
|
|
||||||
return true
|
|
||||||
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Iterator still in progress, wait for either a data or an error event
|
|
||||||
select {
|
|
||||||
case log := <-it.logs:
|
|
||||||
it.Event = new({{$contract.Type}}{{.Normalized.Name}})
|
|
||||||
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
|
|
||||||
it.fail = err
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
it.Event.Raw = log
|
|
||||||
return true
|
|
||||||
|
|
||||||
case err := <-it.sub.Err():
|
|
||||||
it.done = true
|
|
||||||
it.fail = err
|
|
||||||
return it.Next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Error returns any retrieval or parsing error occurred during filtering.
|
|
||||||
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Error() error {
|
|
||||||
return it.fail
|
|
||||||
}
|
|
||||||
// Close terminates the iteration process, releasing any pending underlying
|
|
||||||
// resources.
|
|
||||||
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Close() error {
|
|
||||||
it.sub.Unsubscribe()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract.
|
|
||||||
type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}
|
|
||||||
{{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}}
|
|
||||||
Raw types.Log // Blockchain specific contextual infos
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Filter{{.Normalized.Name}}(opts *bind.FilterOpts{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (*{{$contract.Type}}{{.Normalized.Name}}Iterator, error) {
|
|
||||||
{{range .Normalized.Inputs}}
|
|
||||||
{{if .Indexed}}var {{.Name}}Rule []interface{}
|
|
||||||
for _, {{.Name}}Item := range {{.Name}} {
|
|
||||||
{{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
|
|
||||||
}{{end}}{{end}}
|
|
||||||
|
|
||||||
logs, sub, err := _{{$contract.Type}}.contract.FilterLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &{{$contract.Type}}{{.Normalized.Name}}Iterator{contract: _{{$contract.Type}}.contract, event: "{{.Original.Name}}", logs: logs, sub: sub}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Watch{{.Normalized.Name}} is a free log subscription operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Watch{{.Normalized.Name}}(opts *bind.WatchOpts, sink chan<- *{{$contract.Type}}{{.Normalized.Name}}{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (event.Subscription, error) {
|
|
||||||
{{range .Normalized.Inputs}}
|
|
||||||
{{if .Indexed}}var {{.Name}}Rule []interface{}
|
|
||||||
for _, {{.Name}}Item := range {{.Name}} {
|
|
||||||
{{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
|
|
||||||
}{{end}}{{end}}
|
|
||||||
|
|
||||||
logs, sub, err := _{{$contract.Type}}.contract.WatchLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return event.NewSubscription(func(quit <-chan struct{}) error {
|
|
||||||
defer sub.Unsubscribe()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case log := <-logs:
|
|
||||||
// New log arrived, parse the event and forward to the user
|
|
||||||
event := new({{$contract.Type}}{{.Normalized.Name}})
|
|
||||||
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
event.Raw = log
|
|
||||||
|
|
||||||
select {
|
|
||||||
case sink <- event:
|
|
||||||
case err := <-sub.Err():
|
|
||||||
return err
|
|
||||||
case <-quit:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
case err := <-sub.Err():
|
|
||||||
return err
|
|
||||||
case <-quit:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
|
||||||
//
|
|
||||||
// Solidity: {{.Original.String}}
|
|
||||||
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
|
|
||||||
event := new({{$contract.Type}}{{.Normalized.Name}})
|
|
||||||
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
event.Raw = log
|
|
||||||
return event, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
{{end}}
|
|
||||||
{{end}}
|
|
||||||
@@ -16,11 +16,7 @@
|
|||||||
|
|
||||||
package bind
|
package bind
|
||||||
|
|
||||||
import (
|
import "github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
_ "embed"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
||||||
)
|
|
||||||
|
|
||||||
// tmplData is the data structure required to fill the binding template.
|
// tmplData is the data structure required to fill the binding template.
|
||||||
type tmplData struct {
|
type tmplData struct {
|
||||||
@@ -79,11 +75,634 @@ type tmplStruct struct {
|
|||||||
// tmplSource is language to template mapping containing all the supported
|
// tmplSource is language to template mapping containing all the supported
|
||||||
// programming languages the package can generate to.
|
// programming languages the package can generate to.
|
||||||
var tmplSource = map[Lang]string{
|
var tmplSource = map[Lang]string{
|
||||||
LangGo: tmplSourceGo,
|
LangGo: tmplSourceGo,
|
||||||
|
LangJava: tmplSourceJava,
|
||||||
}
|
}
|
||||||
|
|
||||||
// tmplSourceGo is the Go source template that the generated Go contract binding
|
// tmplSourceGo is the Go source template that the generated Go contract binding
|
||||||
// is based on.
|
// is based on.
|
||||||
//
|
const tmplSourceGo = `
|
||||||
//go:embed source.go.tpl
|
// Code generated - DO NOT EDIT.
|
||||||
var tmplSourceGo string
|
// This file is a generated binding and any manual changes will be lost.
|
||||||
|
|
||||||
|
package {{.Package}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
ethereum "github.com/ethereum/go-ethereum"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/event"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var (
|
||||||
|
_ = errors.New
|
||||||
|
_ = big.NewInt
|
||||||
|
_ = strings.NewReader
|
||||||
|
_ = ethereum.NotFound
|
||||||
|
_ = bind.Bind
|
||||||
|
_ = common.Big1
|
||||||
|
_ = types.BloomLookup
|
||||||
|
_ = event.NewSubscription
|
||||||
|
)
|
||||||
|
|
||||||
|
{{$structs := .Structs}}
|
||||||
|
{{range $structs}}
|
||||||
|
// {{.Name}} is an auto generated low-level Go binding around an user-defined struct.
|
||||||
|
type {{.Name}} struct {
|
||||||
|
{{range $field := .Fields}}
|
||||||
|
{{$field.Name}} {{$field.Type}}{{end}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{range $contract := .Contracts}}
|
||||||
|
// {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract.
|
||||||
|
var {{.Type}}MetaData = &bind.MetaData{
|
||||||
|
ABI: "{{.InputABI}}",
|
||||||
|
{{if $contract.FuncSigs -}}
|
||||||
|
Sigs: map[string]string{
|
||||||
|
{{range $strsig, $binsig := .FuncSigs}}"{{$binsig}}": "{{$strsig}}",
|
||||||
|
{{end}}
|
||||||
|
},
|
||||||
|
{{end -}}
|
||||||
|
{{if .InputBin -}}
|
||||||
|
Bin: "0x{{.InputBin}}",
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
// {{.Type}}ABI is the input ABI used to generate the binding from.
|
||||||
|
// Deprecated: Use {{.Type}}MetaData.ABI instead.
|
||||||
|
var {{.Type}}ABI = {{.Type}}MetaData.ABI
|
||||||
|
|
||||||
|
{{if $contract.FuncSigs}}
|
||||||
|
// Deprecated: Use {{.Type}}MetaData.Sigs instead.
|
||||||
|
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
|
||||||
|
var {{.Type}}FuncSigs = {{.Type}}MetaData.Sigs
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .InputBin}}
|
||||||
|
// {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
|
||||||
|
// Deprecated: Use {{.Type}}MetaData.Bin instead.
|
||||||
|
var {{.Type}}Bin = {{.Type}}MetaData.Bin
|
||||||
|
|
||||||
|
// Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
|
||||||
|
func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) {
|
||||||
|
parsed, err := {{.Type}}MetaData.GetAbi()
|
||||||
|
if err != nil {
|
||||||
|
return common.Address{}, nil, nil, err
|
||||||
|
}
|
||||||
|
if parsed == nil {
|
||||||
|
return common.Address{}, nil, nil, errors.New("GetABI returned nil")
|
||||||
|
}
|
||||||
|
{{range $pattern, $name := .Libraries}}
|
||||||
|
{{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend)
|
||||||
|
{{$contract.Type}}Bin = strings.ReplaceAll({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:])
|
||||||
|
{{end}}
|
||||||
|
address, tx, contract, err := bind.DeployContract(auth, *parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
|
||||||
|
if err != nil {
|
||||||
|
return common.Address{}, nil, nil, err
|
||||||
|
}
|
||||||
|
return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
// {{.Type}} is an auto generated Go binding around an Ethereum contract.
|
||||||
|
type {{.Type}} struct {
|
||||||
|
{{.Type}}Caller // Read-only binding to the contract
|
||||||
|
{{.Type}}Transactor // Write-only binding to the contract
|
||||||
|
{{.Type}}Filterer // Log filterer for contract events
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
|
||||||
|
type {{.Type}}Caller struct {
|
||||||
|
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
|
||||||
|
type {{.Type}}Transactor struct {
|
||||||
|
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}Filterer is an auto generated log filtering Go binding around an Ethereum contract events.
|
||||||
|
type {{.Type}}Filterer struct {
|
||||||
|
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
|
||||||
|
// with pre-set call and transact options.
|
||||||
|
type {{.Type}}Session struct {
|
||||||
|
Contract *{{.Type}} // Generic contract binding to set the session for
|
||||||
|
CallOpts bind.CallOpts // Call options to use throughout this session
|
||||||
|
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
|
||||||
|
// with pre-set call options.
|
||||||
|
type {{.Type}}CallerSession struct {
|
||||||
|
Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
|
||||||
|
CallOpts bind.CallOpts // Call options to use throughout this session
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
|
||||||
|
// with pre-set transact options.
|
||||||
|
type {{.Type}}TransactorSession struct {
|
||||||
|
Contract *{{.Type}}Transactor // Generic contract transactor binding to set the session for
|
||||||
|
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
|
||||||
|
type {{.Type}}Raw struct {
|
||||||
|
Contract *{{.Type}} // Generic contract binding to access the raw methods on
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
|
||||||
|
type {{.Type}}CallerRaw struct {
|
||||||
|
Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
|
||||||
|
type {{.Type}}TransactorRaw struct {
|
||||||
|
Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
|
||||||
|
}
|
||||||
|
|
||||||
|
// New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
|
||||||
|
func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) {
|
||||||
|
contract, err := bind{{.Type}}(address, backend, backend, backend)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
|
||||||
|
func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) {
|
||||||
|
contract, err := bind{{.Type}}(address, caller, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &{{.Type}}Caller{contract: contract}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
|
||||||
|
func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) {
|
||||||
|
contract, err := bind{{.Type}}(address, nil, transactor, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &{{.Type}}Transactor{contract: contract}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// New{{.Type}}Filterer creates a new log filterer instance of {{.Type}}, bound to a specific deployed contract.
|
||||||
|
func New{{.Type}}Filterer(address common.Address, filterer bind.ContractFilterer) (*{{.Type}}Filterer, error) {
|
||||||
|
contract, err := bind{{.Type}}(address, nil, nil, filterer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &{{.Type}}Filterer{contract: contract}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bind{{.Type}} binds a generic wrapper to an already deployed contract.
|
||||||
|
func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) {
|
||||||
|
parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call invokes the (constant) contract method with params as input values and
|
||||||
|
// sets the output to result. The result type might be a single field for simple
|
||||||
|
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||||
|
// returns.
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error {
|
||||||
|
return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transfer initiates a plain transaction to move funds to the contract, calling
|
||||||
|
// its default method if one is available.
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transact invokes the (paid) contract method with params as input values.
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call invokes the (constant) contract method with params as input values and
|
||||||
|
// sets the output to result. The result type might be a single field for simple
|
||||||
|
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||||
|
// returns.
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error {
|
||||||
|
return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transfer initiates a plain transaction to move funds to the contract, calling
|
||||||
|
// its default method if one is available.
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.contract.Transfer(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transact invokes the (paid) contract method with params as input values.
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...)
|
||||||
|
}
|
||||||
|
|
||||||
|
{{range .Calls}}
|
||||||
|
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Caller) {{.Normalized.Name}}(opts *bind.CallOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} },{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}}{{end}} error) {
|
||||||
|
var out []interface{}
|
||||||
|
err := _{{$contract.Type}}.contract.Call(opts, &out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
||||||
|
{{if .Structured}}
|
||||||
|
outstruct := new(struct{ {{range .Normalized.Outputs}} {{.Name}} {{bindtype .Type $structs}}; {{end}} })
|
||||||
|
if err != nil {
|
||||||
|
return *outstruct, err
|
||||||
|
}
|
||||||
|
{{range $i, $t := .Normalized.Outputs}}
|
||||||
|
outstruct.{{.Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}}
|
||||||
|
|
||||||
|
return *outstruct, err
|
||||||
|
{{else}}
|
||||||
|
if err != nil {
|
||||||
|
return {{range $i, $_ := .Normalized.Outputs}}*new({{bindtype .Type $structs}}), {{end}} err
|
||||||
|
}
|
||||||
|
{{range $i, $t := .Normalized.Outputs}}
|
||||||
|
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}}
|
||||||
|
|
||||||
|
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} err
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) {
|
||||||
|
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}CallerSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) {
|
||||||
|
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{range .Transacts}}
|
||||||
|
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .Fallback}}
|
||||||
|
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Fallback.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Fallback(opts *bind.TransactOpts, calldata []byte) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.contract.RawTransact(opts, calldata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Fallback.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Session) Fallback(calldata []byte) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Fallback.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Fallback(calldata []byte) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata)
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .Receive}}
|
||||||
|
// Receive is a paid mutator transaction binding the contract receive function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Receive.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Receive(opts *bind.TransactOpts) (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.contract.RawTransact(opts, nil) // calldata is disallowed for receive function
|
||||||
|
}
|
||||||
|
|
||||||
|
// Receive is a paid mutator transaction binding the contract receive function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Receive.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Session) Receive() (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Receive is a paid mutator transaction binding the contract receive function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Receive.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Receive() (*types.Transaction, error) {
|
||||||
|
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts)
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{range .Events}}
|
||||||
|
// {{$contract.Type}}{{.Normalized.Name}}Iterator is returned from Filter{{.Normalized.Name}} and is used to iterate over the raw logs and unpacked data for {{.Normalized.Name}} events raised by the {{$contract.Type}} contract.
|
||||||
|
type {{$contract.Type}}{{.Normalized.Name}}Iterator struct {
|
||||||
|
Event *{{$contract.Type}}{{.Normalized.Name}} // Event containing the contract specifics and raw log
|
||||||
|
|
||||||
|
contract *bind.BoundContract // Generic contract to use for unpacking event data
|
||||||
|
event string // Event name to use for unpacking event data
|
||||||
|
|
||||||
|
logs chan types.Log // Log channel receiving the found contract events
|
||||||
|
sub ethereum.Subscription // Subscription for errors, completion and termination
|
||||||
|
done bool // Whether the subscription completed delivering logs
|
||||||
|
fail error // Occurred error to stop iteration
|
||||||
|
}
|
||||||
|
// Next advances the iterator to the subsequent event, returning whether there
|
||||||
|
// are any more events found. In case of a retrieval or parsing error, false is
|
||||||
|
// returned and Error() can be queried for the exact failure.
|
||||||
|
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Next() bool {
|
||||||
|
// If the iterator failed, stop iterating
|
||||||
|
if (it.fail != nil) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// If the iterator completed, deliver directly whatever's available
|
||||||
|
if (it.done) {
|
||||||
|
select {
|
||||||
|
case log := <-it.logs:
|
||||||
|
it.Event = new({{$contract.Type}}{{.Normalized.Name}})
|
||||||
|
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
|
||||||
|
it.fail = err
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
it.Event.Raw = log
|
||||||
|
return true
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Iterator still in progress, wait for either a data or an error event
|
||||||
|
select {
|
||||||
|
case log := <-it.logs:
|
||||||
|
it.Event = new({{$contract.Type}}{{.Normalized.Name}})
|
||||||
|
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
|
||||||
|
it.fail = err
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
it.Event.Raw = log
|
||||||
|
return true
|
||||||
|
|
||||||
|
case err := <-it.sub.Err():
|
||||||
|
it.done = true
|
||||||
|
it.fail = err
|
||||||
|
return it.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Error returns any retrieval or parsing error occurred during filtering.
|
||||||
|
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Error() error {
|
||||||
|
return it.fail
|
||||||
|
}
|
||||||
|
// Close terminates the iteration process, releasing any pending underlying
|
||||||
|
// resources.
|
||||||
|
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Close() error {
|
||||||
|
it.sub.Unsubscribe()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract.
|
||||||
|
type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}
|
||||||
|
{{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}}
|
||||||
|
Raw types.Log // Blockchain specific contextual infos
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Filter{{.Normalized.Name}}(opts *bind.FilterOpts{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (*{{$contract.Type}}{{.Normalized.Name}}Iterator, error) {
|
||||||
|
{{range .Normalized.Inputs}}
|
||||||
|
{{if .Indexed}}var {{.Name}}Rule []interface{}
|
||||||
|
for _, {{.Name}}Item := range {{.Name}} {
|
||||||
|
{{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
|
||||||
|
}{{end}}{{end}}
|
||||||
|
|
||||||
|
logs, sub, err := _{{$contract.Type}}.contract.FilterLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &{{$contract.Type}}{{.Normalized.Name}}Iterator{contract: _{{$contract.Type}}.contract, event: "{{.Original.Name}}", logs: logs, sub: sub}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch{{.Normalized.Name}} is a free log subscription operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Watch{{.Normalized.Name}}(opts *bind.WatchOpts, sink chan<- *{{$contract.Type}}{{.Normalized.Name}}{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (event.Subscription, error) {
|
||||||
|
{{range .Normalized.Inputs}}
|
||||||
|
{{if .Indexed}}var {{.Name}}Rule []interface{}
|
||||||
|
for _, {{.Name}}Item := range {{.Name}} {
|
||||||
|
{{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
|
||||||
|
}{{end}}{{end}}
|
||||||
|
|
||||||
|
logs, sub, err := _{{$contract.Type}}.contract.WatchLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return event.NewSubscription(func(quit <-chan struct{}) error {
|
||||||
|
defer sub.Unsubscribe()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case log := <-logs:
|
||||||
|
// New log arrived, parse the event and forward to the user
|
||||||
|
event := new({{$contract.Type}}{{.Normalized.Name}})
|
||||||
|
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
event.Raw = log
|
||||||
|
|
||||||
|
select {
|
||||||
|
case sink <- event:
|
||||||
|
case err := <-sub.Err():
|
||||||
|
return err
|
||||||
|
case <-quit:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case err := <-sub.Err():
|
||||||
|
return err
|
||||||
|
case <-quit:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
|
||||||
|
event := new({{$contract.Type}}{{.Normalized.Name}})
|
||||||
|
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
event.Raw = log
|
||||||
|
return event, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
`
|
||||||
|
|
||||||
|
// tmplSourceJava is the Java source template that the generated Java contract binding
|
||||||
|
// is based on.
|
||||||
|
const tmplSourceJava = `
|
||||||
|
// This file is an automatically generated Java binding. Do not modify as any
|
||||||
|
// change will likely be lost upon the next re-generation!
|
||||||
|
|
||||||
|
package {{.Package}};
|
||||||
|
|
||||||
|
import org.ethereum.geth.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
{{$structs := .Structs}}
|
||||||
|
{{range $contract := .Contracts}}
|
||||||
|
{{if not .Library}}public {{end}}class {{.Type}} {
|
||||||
|
// ABI is the input ABI used to generate the binding from.
|
||||||
|
public final static String ABI = "{{.InputABI}}";
|
||||||
|
{{if $contract.FuncSigs}}
|
||||||
|
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
|
||||||
|
public final static Map<String, String> {{.Type}}FuncSigs;
|
||||||
|
static {
|
||||||
|
Hashtable<String, String> temp = new Hashtable<String, String>();
|
||||||
|
{{range $strsig, $binsig := .FuncSigs}}temp.put("{{$binsig}}", "{{$strsig}}");
|
||||||
|
{{end}}
|
||||||
|
{{.Type}}FuncSigs = Collections.unmodifiableMap(temp);
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
{{if .InputBin}}
|
||||||
|
// BYTECODE is the compiled bytecode used for deploying new contracts.
|
||||||
|
public final static String BYTECODE = "0x{{.InputBin}}";
|
||||||
|
|
||||||
|
// deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
|
||||||
|
public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception {
|
||||||
|
Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
|
||||||
|
String bytecode = BYTECODE;
|
||||||
|
{{if .Libraries}}
|
||||||
|
|
||||||
|
// "link" contract to dependent libraries by deploying them first.
|
||||||
|
{{range $pattern, $name := .Libraries}}
|
||||||
|
{{capitalise $name}} {{decapitalise $name}}Inst = {{capitalise $name}}.deploy(auth, client);
|
||||||
|
bytecode = bytecode.replace("__${{$pattern}}$__", {{decapitalise $name}}Inst.Address.getHex().substring(2));
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
{{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
|
||||||
|
{{end}}
|
||||||
|
return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(bytecode), client, args));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal constructor used by contract deployment.
|
||||||
|
private {{.Type}}(BoundContract deployment) {
|
||||||
|
this.Address = deployment.getAddress();
|
||||||
|
this.Deployer = deployment.getDeployer();
|
||||||
|
this.Contract = deployment;
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
// Ethereum address where this contract is located at.
|
||||||
|
public final Address Address;
|
||||||
|
|
||||||
|
// Ethereum transaction in which this contract was deployed (if known!).
|
||||||
|
public final Transaction Deployer;
|
||||||
|
|
||||||
|
// Contract instance bound to a blockchain address.
|
||||||
|
private final BoundContract Contract;
|
||||||
|
|
||||||
|
// Creates a new instance of {{.Type}}, bound to a specific deployed contract.
|
||||||
|
public {{.Type}}(Address address, EthereumClient client) throws Exception {
|
||||||
|
this(Geth.bindContract(address, ABI, client));
|
||||||
|
}
|
||||||
|
|
||||||
|
{{range .Calls}}
|
||||||
|
{{if gt (len .Normalized.Outputs) 1}}
|
||||||
|
// {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.
|
||||||
|
public class {{capitalise .Normalized.Name}}Results {
|
||||||
|
{{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type $structs}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}};
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
public {{if gt (len .Normalized.Outputs) 1}}{{capitalise .Normalized.Name}}Results{{else if eq (len .Normalized.Outputs) 0}}void{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}}{{end}}{{end}} {{.Normalized.Name}}(CallOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception {
|
||||||
|
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
|
||||||
|
{{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}});
|
||||||
|
{{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type $structs) .Type}}(); results.set({{$index}}, result{{$index}});
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
if (opts == null) {
|
||||||
|
opts = Geth.newCallOpts();
|
||||||
|
}
|
||||||
|
this.Contract.call(opts, results, "{{.Original.Name}}", args);
|
||||||
|
{{if gt (len .Normalized.Outputs) 1}}
|
||||||
|
{{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results();
|
||||||
|
{{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type $structs) .Type}}();
|
||||||
|
{{end}}
|
||||||
|
return result;
|
||||||
|
{{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type $structs) .Type}}();{{end}}
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{range .Transacts}}
|
||||||
|
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Original.String}}
|
||||||
|
public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception {
|
||||||
|
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
|
||||||
|
{{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
|
||||||
|
{{end}}
|
||||||
|
return this.Contract.transact(opts, "{{.Original.Name}}" , args);
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .Fallback}}
|
||||||
|
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Fallback.Original.String}}
|
||||||
|
public Transaction Fallback(TransactOpts opts, byte[] calldata) throws Exception {
|
||||||
|
return this.Contract.rawTransact(opts, calldata);
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .Receive}}
|
||||||
|
// Receive is a paid mutator transaction binding the contract receive function.
|
||||||
|
//
|
||||||
|
// Solidity: {{.Receive.Original.String}}
|
||||||
|
public Transaction Receive(TransactOpts opts) throws Exception {
|
||||||
|
return this.Contract.rawTransact(opts, null);
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
`
|
||||||
|
|||||||
@@ -30,18 +30,12 @@ import (
|
|||||||
// WaitMined waits for tx to be mined on the blockchain.
|
// WaitMined waits for tx to be mined on the blockchain.
|
||||||
// It stops waiting when the context is canceled.
|
// It stops waiting when the context is canceled.
|
||||||
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
|
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
|
||||||
return WaitMinedHash(ctx, b, tx.Hash())
|
|
||||||
}
|
|
||||||
|
|
||||||
// WaitMinedHash waits for a transaction with the provided hash to be mined on the blockchain.
|
|
||||||
// It stops waiting when the context is canceled.
|
|
||||||
func WaitMinedHash(ctx context.Context, b DeployBackend, hash common.Hash) (*types.Receipt, error) {
|
|
||||||
queryTicker := time.NewTicker(time.Second)
|
queryTicker := time.NewTicker(time.Second)
|
||||||
defer queryTicker.Stop()
|
defer queryTicker.Stop()
|
||||||
|
|
||||||
logger := log.New("hash", hash)
|
logger := log.New("hash", tx.Hash())
|
||||||
for {
|
for {
|
||||||
receipt, err := b.TransactionReceipt(ctx, hash)
|
receipt, err := b.TransactionReceipt(ctx, tx.Hash())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return receipt, nil
|
return receipt, nil
|
||||||
}
|
}
|
||||||
@@ -67,13 +61,7 @@ func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (
|
|||||||
if tx.To() != nil {
|
if tx.To() != nil {
|
||||||
return common.Address{}, errors.New("tx is not contract creation")
|
return common.Address{}, errors.New("tx is not contract creation")
|
||||||
}
|
}
|
||||||
return WaitDeployedHash(ctx, b, tx.Hash())
|
receipt, err := WaitMined(ctx, b, tx)
|
||||||
}
|
|
||||||
|
|
||||||
// WaitDeployedHash waits for a contract deployment transaction with the provided hash and returns the on-chain
|
|
||||||
// contract address when it is mined. It stops waiting when ctx is canceled.
|
|
||||||
func WaitDeployedHash(ctx context.Context, b DeployBackend, hash common.Hash) (common.Address, error) {
|
|
||||||
receipt, err := WaitMinedHash(ctx, b, hash)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.Address{}, err
|
return common.Address{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,11 +24,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/ethclient/simulated"
|
|
||||||
"github.com/ethereum/go-ethereum/params"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
@@ -53,21 +53,21 @@ var waitDeployedTests = map[string]struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWaitDeployed(t *testing.T) {
|
func TestWaitDeployed(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
for name, test := range waitDeployedTests {
|
for name, test := range waitDeployedTests {
|
||||||
backend := simulated.NewBackend(
|
backend := backends.NewSimulatedBackend(
|
||||||
types.GenesisAlloc{
|
core.GenesisAlloc{
|
||||||
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},
|
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},
|
||||||
},
|
},
|
||||||
|
10000000,
|
||||||
)
|
)
|
||||||
defer backend.Close()
|
defer backend.Close()
|
||||||
|
|
||||||
// Create the transaction
|
// Create the transaction
|
||||||
head, _ := backend.Client().HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
||||||
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(params.GWei))
|
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
|
||||||
|
|
||||||
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, gasPrice, common.FromHex(test.code))
|
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, gasPrice, common.FromHex(test.code))
|
||||||
tx, _ = types.SignTx(tx, types.LatestSignerForChainID(big.NewInt(1337)), testKey)
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
||||||
|
|
||||||
// Wait for it to get mined in the background.
|
// Wait for it to get mined in the background.
|
||||||
var (
|
var (
|
||||||
@@ -77,14 +77,12 @@ func TestWaitDeployed(t *testing.T) {
|
|||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
address, err = bind.WaitDeployed(ctx, backend.Client(), tx)
|
address, err = bind.WaitDeployed(ctx, backend, tx)
|
||||||
close(mined)
|
close(mined)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Send and mine the transaction.
|
// Send and mine the transaction.
|
||||||
if err := backend.Client().SendTransaction(ctx, tx); err != nil {
|
backend.SendTransaction(ctx, tx)
|
||||||
t.Errorf("test %q: failed to send transaction: %v", name, err)
|
|
||||||
}
|
|
||||||
backend.Commit()
|
backend.Commit()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -102,44 +100,41 @@ func TestWaitDeployed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWaitDeployedCornerCases(t *testing.T) {
|
func TestWaitDeployedCornerCases(t *testing.T) {
|
||||||
backend := simulated.NewBackend(
|
backend := backends.NewSimulatedBackend(
|
||||||
types.GenesisAlloc{
|
core.GenesisAlloc{
|
||||||
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},
|
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},
|
||||||
},
|
},
|
||||||
|
10000000,
|
||||||
)
|
)
|
||||||
defer backend.Close()
|
defer backend.Close()
|
||||||
|
|
||||||
head, _ := backend.Client().HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
||||||
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
|
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
|
||||||
|
|
||||||
// Create a transaction to an account.
|
// Create a transaction to an account.
|
||||||
code := "6060604052600a8060106000396000f360606040526008565b00"
|
code := "6060604052600a8060106000396000f360606040526008565b00"
|
||||||
tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
|
tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
|
||||||
tx, _ = types.SignTx(tx, types.LatestSigner(params.AllDevChainProtocolChanges), testKey)
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := backend.Client().SendTransaction(ctx, tx); err != nil {
|
backend.SendTransaction(ctx, tx)
|
||||||
t.Errorf("failed to send transaction: %q", err)
|
|
||||||
}
|
|
||||||
backend.Commit()
|
backend.Commit()
|
||||||
notContractCreation := errors.New("tx is not contract creation")
|
notContentCreation := errors.New("tx is not contract creation")
|
||||||
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != notContractCreation.Error() {
|
if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() {
|
||||||
t.Errorf("error mismatch: want %q, got %q, ", notContractCreation, err)
|
t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a transaction that is not mined.
|
// Create a transaction that is not mined.
|
||||||
tx = types.NewContractCreation(1, big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
|
tx = types.NewContractCreation(1, big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
|
||||||
tx, _ = types.SignTx(tx, types.LatestSigner(params.AllDevChainProtocolChanges), testKey)
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
contextCanceled := errors.New("context canceled")
|
contextCanceled := errors.New("context canceled")
|
||||||
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != contextCanceled.Error() {
|
if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() {
|
||||||
t.Errorf("error mismatch: want %q, got %q, ", contextCanceled, err)
|
t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := backend.Client().SendTransaction(ctx, tx); err != nil {
|
backend.SendTransaction(ctx, tx)
|
||||||
t.Errorf("failed to send transaction: %q", err)
|
|
||||||
}
|
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package abi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ type Error struct {
|
|||||||
str string
|
str string
|
||||||
|
|
||||||
// Sig contains the string signature according to the ABI spec.
|
// Sig contains the string signature according to the ABI spec.
|
||||||
// e.g. error foo(uint32 a, int b) = "foo(uint32,int256)"
|
// e.g. error foo(uint32 a, int b) = "foo(uint32,int256)"
|
||||||
// Please note that "int" is substitute for its canonical representation "int256"
|
// Please note that "int" is substitute for its canonical representation "int256"
|
||||||
Sig string
|
Sig string
|
||||||
|
|
||||||
@@ -77,16 +78,16 @@ func NewError(name string, inputs Arguments) Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Error) String() string {
|
func (e *Error) String() string {
|
||||||
return e.str
|
return e.str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Error) Unpack(data []byte) (interface{}, error) {
|
func (e *Error) Unpack(data []byte) (interface{}, error) {
|
||||||
if len(data) < 4 {
|
if len(data) < 4 {
|
||||||
return "", fmt.Errorf("insufficient data for unpacking: have %d, want at least 4", len(data))
|
return "", errors.New("invalid data for unpacking")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(data[:4], e.ID[:4]) {
|
if !bytes.Equal(data[:4], e.ID[:4]) {
|
||||||
return "", fmt.Errorf("invalid identifier, have %#x want %#x", data[:4], e.ID[:4])
|
return "", errors.New("invalid data for unpacking")
|
||||||
}
|
}
|
||||||
return e.Inputs.Unpack(data[4:])
|
return e.Inputs.Unpack(data[4:])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,15 +23,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errBadBool = errors.New("abi: improperly encoded boolean value")
|
errBadBool = errors.New("abi: improperly encoded boolean value")
|
||||||
errBadUint8 = errors.New("abi: improperly encoded uint8 value")
|
|
||||||
errBadUint16 = errors.New("abi: improperly encoded uint16 value")
|
|
||||||
errBadUint32 = errors.New("abi: improperly encoded uint32 value")
|
|
||||||
errBadUint64 = errors.New("abi: improperly encoded uint64 value")
|
|
||||||
errBadInt8 = errors.New("abi: improperly encoded int8 value")
|
|
||||||
errBadInt16 = errors.New("abi: improperly encoded int16 value")
|
|
||||||
errBadInt32 = errors.New("abi: improperly encoded int32 value")
|
|
||||||
errBadInt64 = errors.New("abi: improperly encoded int64 value")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// formatSliceString formats the reflection kind with the given slice size
|
// formatSliceString formats the reflection kind with the given slice size
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa
|
|||||||
var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241"
|
var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241"
|
||||||
|
|
||||||
func TestEventId(t *testing.T) {
|
func TestEventId(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var table = []struct {
|
var table = []struct {
|
||||||
definition string
|
definition string
|
||||||
expectations map[string]common.Hash
|
expectations map[string]common.Hash
|
||||||
@@ -113,7 +112,6 @@ func TestEventId(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEventString(t *testing.T) {
|
func TestEventString(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var table = []struct {
|
var table = []struct {
|
||||||
definition string
|
definition string
|
||||||
expectations map[string]string
|
expectations map[string]string
|
||||||
@@ -148,7 +146,6 @@ func TestEventString(t *testing.T) {
|
|||||||
|
|
||||||
// TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array.
|
// TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array.
|
||||||
func TestEventMultiValueWithArrayUnpack(t *testing.T) {
|
func TestEventMultiValueWithArrayUnpack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
|
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
|
||||||
abi, err := JSON(strings.NewReader(definition))
|
abi, err := JSON(strings.NewReader(definition))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -164,7 +161,6 @@ func TestEventMultiValueWithArrayUnpack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEventTupleUnpack(t *testing.T) {
|
func TestEventTupleUnpack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
type EventTransfer struct {
|
type EventTransfer struct {
|
||||||
Value *big.Int
|
Value *big.Int
|
||||||
}
|
}
|
||||||
@@ -331,6 +327,7 @@ func TestEventTupleUnpack(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
tc := tc
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert)
|
err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert)
|
||||||
if tc.error == "" {
|
if tc.error == "" {
|
||||||
@@ -354,7 +351,6 @@ func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, ass
|
|||||||
|
|
||||||
// TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder.
|
// TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder.
|
||||||
func TestEventUnpackIndexed(t *testing.T) {
|
func TestEventUnpackIndexed(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
|
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
|
||||||
type testStruct struct {
|
type testStruct struct {
|
||||||
Value1 uint8 // indexed
|
Value1 uint8 // indexed
|
||||||
@@ -372,7 +368,6 @@ func TestEventUnpackIndexed(t *testing.T) {
|
|||||||
|
|
||||||
// TestEventIndexedWithArrayUnpack verifies that decoder will not overflow when static array is indexed input.
|
// TestEventIndexedWithArrayUnpack verifies that decoder will not overflow when static array is indexed input.
|
||||||
func TestEventIndexedWithArrayUnpack(t *testing.T) {
|
func TestEventIndexedWithArrayUnpack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]`
|
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]`
|
||||||
type testStruct struct {
|
type testStruct struct {
|
||||||
Value1 [2]uint8 // indexed
|
Value1 [2]uint8 // indexed
|
||||||
|
|||||||
@@ -117,23 +117,24 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str
|
|||||||
sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
|
sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
|
||||||
id = crypto.Keccak256([]byte(sig))[:4]
|
id = crypto.Keccak256([]byte(sig))[:4]
|
||||||
}
|
}
|
||||||
|
// Extract meaningful state mutability of solidity method.
|
||||||
|
// If it's default value, never print it.
|
||||||
|
state := mutability
|
||||||
|
if state == "nonpayable" {
|
||||||
|
state = ""
|
||||||
|
}
|
||||||
|
if state != "" {
|
||||||
|
state = state + " "
|
||||||
|
}
|
||||||
identity := fmt.Sprintf("function %v", rawName)
|
identity := fmt.Sprintf("function %v", rawName)
|
||||||
switch funType {
|
if funType == Fallback {
|
||||||
case Fallback:
|
|
||||||
identity = "fallback"
|
identity = "fallback"
|
||||||
case Receive:
|
} else if funType == Receive {
|
||||||
identity = "receive"
|
identity = "receive"
|
||||||
case Constructor:
|
} else if funType == Constructor {
|
||||||
identity = "constructor"
|
identity = "constructor"
|
||||||
}
|
}
|
||||||
var str string
|
str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", "))
|
||||||
// Extract meaningful state mutability of solidity method.
|
|
||||||
// If it's empty string or default value "nonpayable", never print it.
|
|
||||||
if mutability == "" || mutability == "nonpayable" {
|
|
||||||
str = fmt.Sprintf("%v(%v) returns(%v)", identity, strings.Join(inputNames, ", "), strings.Join(outputNames, ", "))
|
|
||||||
} else {
|
|
||||||
str = fmt.Sprintf("%v(%v) %s returns(%v)", identity, strings.Join(inputNames, ", "), mutability, strings.Join(outputNames, ", "))
|
|
||||||
}
|
|
||||||
|
|
||||||
return Method{
|
return Method{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ const methoddata = `
|
|||||||
]`
|
]`
|
||||||
|
|
||||||
func TestMethodString(t *testing.T) {
|
func TestMethodString(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var table = []struct {
|
var table = []struct {
|
||||||
method string
|
method string
|
||||||
expectation string
|
expectation string
|
||||||
@@ -85,12 +84,11 @@ func TestMethodString(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range table {
|
for _, test := range table {
|
||||||
var got string
|
var got string
|
||||||
switch test.method {
|
if test.method == "fallback" {
|
||||||
case "fallback":
|
|
||||||
got = abi.Fallback.String()
|
got = abi.Fallback.String()
|
||||||
case "receive":
|
} else if test.method == "receive" {
|
||||||
got = abi.Receive.String()
|
got = abi.Receive.String()
|
||||||
default:
|
} else {
|
||||||
got = abi.Methods[test.method].String()
|
got = abi.Methods[test.method].String()
|
||||||
}
|
}
|
||||||
if got != test.expectation {
|
if got != test.expectation {
|
||||||
@@ -100,7 +98,6 @@ func TestMethodString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMethodSig(t *testing.T) {
|
func TestMethodSig(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var cases = []struct {
|
var cases = []struct {
|
||||||
method string
|
method string
|
||||||
expect string
|
expect string
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
|||||||
reflectValue = mustArrayToByteSlice(reflectValue)
|
reflectValue = mustArrayToByteSlice(reflectValue)
|
||||||
}
|
}
|
||||||
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
|
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
|
||||||
return []byte{}, errors.New("bytes type is neither slice nor array")
|
return []byte{}, errors.New("Bytes type is neither slice nor array")
|
||||||
}
|
}
|
||||||
return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil
|
return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil
|
||||||
case FixedBytesTy, FunctionTy:
|
case FixedBytesTy, FunctionTy:
|
||||||
@@ -66,7 +66,7 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return common.RightPadBytes(reflectValue.Bytes(), 32), nil
|
return common.RightPadBytes(reflectValue.Bytes(), 32), nil
|
||||||
default:
|
default:
|
||||||
return []byte{}, fmt.Errorf("could not pack element, unknown type: %v", t.T)
|
return []byte{}, fmt.Errorf("Could not pack element, unknown type: %v", t.T)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,10 +32,8 @@ import (
|
|||||||
|
|
||||||
// TestPack tests the general pack/unpack tests in packing_test.go
|
// TestPack tests the general pack/unpack tests in packing_test.go
|
||||||
func TestPack(t *testing.T) {
|
func TestPack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
for i, test := range packUnpackTests {
|
for i, test := range packUnpackTests {
|
||||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
encb, err := hex.DecodeString(test.packed)
|
encb, err := hex.DecodeString(test.packed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("invalid hex %s: %v", test.packed, err)
|
t.Fatalf("invalid hex %s: %v", test.packed, err)
|
||||||
@@ -59,7 +57,6 @@ func TestPack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMethodPack(t *testing.T) {
|
func TestMethodPack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(jsondata))
|
abi, err := JSON(strings.NewReader(jsondata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -180,7 +177,6 @@ func TestMethodPack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPackNumber(t *testing.T) {
|
func TestPackNumber(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
value reflect.Value
|
value reflect.Value
|
||||||
packed []byte
|
packed []byte
|
||||||
|
|||||||
@@ -24,20 +24,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConvertType converts an interface of a runtime type into an interface of the
|
// ConvertType converts an interface of a runtime type into a interface of the
|
||||||
// given type, e.g. turn this code:
|
// given type
|
||||||
//
|
// e.g. turn
|
||||||
// var fields []reflect.StructField
|
// var fields []reflect.StructField
|
||||||
//
|
// fields = append(fields, reflect.StructField{
|
||||||
// fields = append(fields, reflect.StructField{
|
// Name: "X",
|
||||||
// Name: "X",
|
// Type: reflect.TypeOf(new(big.Int)),
|
||||||
// Type: reflect.TypeOf(new(big.Int)),
|
// Tag: reflect.StructTag("json:\"" + "x" + "\""),
|
||||||
// Tag: reflect.StructTag("json:\"" + "x" + "\""),
|
// }
|
||||||
// })
|
// into
|
||||||
//
|
// type TupleT struct { X *big.Int }
|
||||||
// into:
|
|
||||||
//
|
|
||||||
// type TupleT struct { X *big.Int }
|
|
||||||
func ConvertType(in interface{}, proto interface{}) interface{} {
|
func ConvertType(in interface{}, proto interface{}) interface{} {
|
||||||
protoType := reflect.TypeOf(proto)
|
protoType := reflect.TypeOf(proto)
|
||||||
if reflect.TypeOf(in).ConvertibleTo(protoType) {
|
if reflect.TypeOf(in).ConvertibleTo(protoType) {
|
||||||
@@ -134,7 +131,7 @@ func setSlice(dst, src reflect.Value) error {
|
|||||||
dst.Set(slice)
|
dst.Set(slice)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("cannot set slice, destination not settable")
|
return errors.New("Cannot set slice, destination not settable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func setArray(dst, src reflect.Value) error {
|
func setArray(dst, src reflect.Value) error {
|
||||||
@@ -155,7 +152,7 @@ func setArray(dst, src reflect.Value) error {
|
|||||||
dst.Set(array)
|
dst.Set(array)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("cannot set array, destination not settable")
|
return errors.New("Cannot set array, destination not settable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func setStruct(dst, src reflect.Value) error {
|
func setStruct(dst, src reflect.Value) error {
|
||||||
@@ -163,7 +160,7 @@ func setStruct(dst, src reflect.Value) error {
|
|||||||
srcField := src.Field(i)
|
srcField := src.Field(i)
|
||||||
dstField := dst.Field(i)
|
dstField := dst.Field(i)
|
||||||
if !dstField.IsValid() || !srcField.IsValid() {
|
if !dstField.IsValid() || !srcField.IsValid() {
|
||||||
return fmt.Errorf("could not find src field: %v value: %v in destination", srcField.Type().Name(), srcField)
|
return fmt.Errorf("Could not find src field: %v value: %v in destination", srcField.Type().Name(), srcField)
|
||||||
}
|
}
|
||||||
if err := set(dstField, srcField); err != nil {
|
if err := set(dstField, srcField); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -173,13 +170,11 @@ func setStruct(dst, src reflect.Value) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mapArgNamesToStructFields maps a slice of argument names to struct fields.
|
// mapArgNamesToStructFields maps a slice of argument names to struct fields.
|
||||||
//
|
// first round: for each Exportable field that contains a `abi:""` tag
|
||||||
// first round: for each Exportable field that contains a `abi:""` tag and this field name
|
// and this field name exists in the given argument name list, pair them together.
|
||||||
// exists in the given argument name list, pair them together.
|
// second round: for each argument name that has not been already linked,
|
||||||
//
|
// find what variable is expected to be mapped into, if it exists and has not been
|
||||||
// second round: for each argument name that has not been already linked, find what
|
// used, pair them.
|
||||||
// variable is expected to be mapped into, if it exists and has not been used, pair them.
|
|
||||||
//
|
|
||||||
// Note this function assumes the given value is a struct value.
|
// Note this function assumes the given value is a struct value.
|
||||||
func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) {
|
func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) {
|
||||||
typ := value.Type()
|
typ := value.Type()
|
||||||
@@ -228,7 +223,7 @@ func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[stri
|
|||||||
structFieldName := ToCamelCase(argName)
|
structFieldName := ToCamelCase(argName)
|
||||||
|
|
||||||
if structFieldName == "" {
|
if structFieldName == "" {
|
||||||
return nil, errors.New("abi: purely underscored output cannot unpack to struct")
|
return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct")
|
||||||
}
|
}
|
||||||
|
|
||||||
// this abi has already been paired, skip it... unless there exists another, yet unassigned
|
// this abi has already been paired, skip it... unless there exists another, yet unassigned
|
||||||
|
|||||||
@@ -170,10 +170,8 @@ var reflectTests = []reflectTest{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReflectNameToStruct(t *testing.T) {
|
func TestReflectNameToStruct(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
for _, test := range reflectTests {
|
for _, test := range reflectTests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
m, err := mapArgNamesToStructFields(test.args, reflect.ValueOf(test.struc))
|
m, err := mapArgNamesToStructFields(test.args, reflect.ValueOf(test.struc))
|
||||||
if len(test.err) > 0 {
|
if len(test.err) > 0 {
|
||||||
if err == nil || err.Error() != test.err {
|
if err == nil || err.Error() != test.err {
|
||||||
@@ -194,7 +192,6 @@ func TestReflectNameToStruct(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertType(t *testing.T) {
|
func TestConvertType(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Test Basic Struct
|
// Test Basic Struct
|
||||||
type T struct {
|
type T struct {
|
||||||
X *big.Int
|
X *big.Int
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
package abi
|
package abi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ func isIdentifierSymbol(c byte) bool {
|
|||||||
|
|
||||||
func parseToken(unescapedSelector string, isIdent bool) (string, string, error) {
|
func parseToken(unescapedSelector string, isIdent bool) (string, string, error) {
|
||||||
if len(unescapedSelector) == 0 {
|
if len(unescapedSelector) == 0 {
|
||||||
return "", "", errors.New("empty token")
|
return "", "", fmt.Errorf("empty token")
|
||||||
}
|
}
|
||||||
firstChar := unescapedSelector[0]
|
firstChar := unescapedSelector[0]
|
||||||
position := 1
|
position := 1
|
||||||
@@ -111,7 +110,7 @@ func parseCompositeType(unescapedSelector string) ([]interface{}, string, error)
|
|||||||
|
|
||||||
func parseType(unescapedSelector string) (interface{}, string, error) {
|
func parseType(unescapedSelector string) (interface{}, string, error) {
|
||||||
if len(unescapedSelector) == 0 {
|
if len(unescapedSelector) == 0 {
|
||||||
return nil, "", errors.New("empty type")
|
return nil, "", fmt.Errorf("empty type")
|
||||||
}
|
}
|
||||||
if unescapedSelector[0] == '(' {
|
if unescapedSelector[0] == '(' {
|
||||||
return parseCompositeType(unescapedSelector)
|
return parseCompositeType(unescapedSelector)
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestParseSelector(t *testing.T) {
|
func TestParseSelector(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
mkType := func(types ...interface{}) []ArgumentMarshaling {
|
mkType := func(types ...interface{}) []ArgumentMarshaling {
|
||||||
var result []ArgumentMarshaling
|
var result []ArgumentMarshaling
|
||||||
for i, typeOrComponents := range types {
|
for i, typeOrComponents := range types {
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,7 +41,8 @@ func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) {
|
|||||||
case common.Address:
|
case common.Address:
|
||||||
copy(topic[common.HashLength-common.AddressLength:], rule[:])
|
copy(topic[common.HashLength-common.AddressLength:], rule[:])
|
||||||
case *big.Int:
|
case *big.Int:
|
||||||
copy(topic[:], math.U256Bytes(new(big.Int).Set(rule)))
|
blob := rule.Bytes()
|
||||||
|
copy(topic[common.HashLength-len(blob):], blob)
|
||||||
case bool:
|
case bool:
|
||||||
if rule {
|
if rule {
|
||||||
topic[common.HashLength-1] = 1
|
topic[common.HashLength-1] = 1
|
||||||
@@ -75,7 +75,7 @@ func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) {
|
|||||||
copy(topic[:], hash[:])
|
copy(topic[:], hash[:])
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// todo(rjl493456442) according to solidity documentation, indexed event
|
// todo(rjl493456442) according solidity documentation, indexed event
|
||||||
// parameters that are not value types i.e. arrays and structs are not
|
// parameters that are not value types i.e. arrays and structs are not
|
||||||
// stored directly but instead a keccak256-hash of an encoding is stored.
|
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
package abi
|
package abi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -27,7 +26,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMakeTopics(t *testing.T) {
|
func TestMakeTopics(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
type args struct {
|
type args struct {
|
||||||
query [][]interface{}
|
query [][]interface{}
|
||||||
}
|
}
|
||||||
@@ -56,27 +54,9 @@ func TestMakeTopics(t *testing.T) {
|
|||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"support positive *big.Int types in topics",
|
"support *big.Int types in topics",
|
||||||
args{[][]interface{}{
|
args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}},
|
||||||
{big.NewInt(1)},
|
[][]common.Hash{{common.Hash{128}}},
|
||||||
{big.NewInt(1).Lsh(big.NewInt(2), 254)},
|
|
||||||
}},
|
|
||||||
[][]common.Hash{
|
|
||||||
{common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001")},
|
|
||||||
{common.Hash{128}},
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"support negative *big.Int types in topics",
|
|
||||||
args{[][]interface{}{
|
|
||||||
{big.NewInt(-1)},
|
|
||||||
{big.NewInt(math.MinInt64)},
|
|
||||||
}},
|
|
||||||
[][]common.Hash{
|
|
||||||
{common.MaxHash},
|
|
||||||
{common.HexToHash("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
|
|
||||||
},
|
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -138,7 +118,6 @@ func TestMakeTopics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
got, err := MakeTopics(tt.args.query...)
|
got, err := MakeTopics(tt.args.query...)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
@@ -149,23 +128,6 @@ func TestMakeTopics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("does not mutate big.Int", func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
want := [][]common.Hash{{common.HexToHash("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}}
|
|
||||||
|
|
||||||
in := big.NewInt(-1)
|
|
||||||
got, err := MakeTopics([]interface{}{in})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("makeTopics() error = %v", err)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(got, want) {
|
|
||||||
t.Fatalf("makeTopics() = %v, want %v", got, want)
|
|
||||||
}
|
|
||||||
if orig := big.NewInt(-1); in.Cmp(orig) != 0 {
|
|
||||||
t.Fatalf("makeTopics() mutated an input parameter from %v to %v", orig, in)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
@@ -385,12 +347,10 @@ func setupTopicsTests() []topicTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseTopics(t *testing.T) {
|
func TestParseTopics(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := setupTopicsTests()
|
tests := setupTopicsTests()
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
createObj := tt.args.createObj()
|
createObj := tt.args.createObj()
|
||||||
if err := ParseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
|
if err := ParseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
|
||||||
t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
@@ -404,12 +364,10 @@ func TestParseTopics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseTopicsIntoMap(t *testing.T) {
|
func TestParseTopicsIntoMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := setupTopicsTests()
|
tests := setupTopicsTests()
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
outMap := make(map[string]interface{})
|
outMap := make(map[string]interface{})
|
||||||
if err := ParseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
|
if err := ParseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
|
||||||
t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
|||||||
@@ -64,16 +64,13 @@ type Type struct {
|
|||||||
var (
|
var (
|
||||||
// typeRegex parses the abi sub types
|
// typeRegex parses the abi sub types
|
||||||
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")
|
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")
|
||||||
|
|
||||||
// sliceSizeRegex grab the slice size
|
|
||||||
sliceSizeRegex = regexp.MustCompile("[0-9]+")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewType creates a new reflection type of abi type given in t.
|
// NewType creates a new reflection type of abi type given in t.
|
||||||
func NewType(t string, internalType string, components []ArgumentMarshaling) (typ Type, err error) {
|
func NewType(t string, internalType string, components []ArgumentMarshaling) (typ Type, err error) {
|
||||||
// check that array brackets are equal if they exist
|
// check that array brackets are equal if they exist
|
||||||
if strings.Count(t, "[") != strings.Count(t, "]") {
|
if strings.Count(t, "[") != strings.Count(t, "]") {
|
||||||
return Type{}, errors.New("invalid arg type in abi")
|
return Type{}, fmt.Errorf("invalid arg type in abi")
|
||||||
}
|
}
|
||||||
typ.stringKind = t
|
typ.stringKind = t
|
||||||
|
|
||||||
@@ -94,7 +91,8 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
|
|||||||
// grab the last cell and create a type from there
|
// grab the last cell and create a type from there
|
||||||
sliced := t[i:]
|
sliced := t[i:]
|
||||||
// grab the slice size with regexp
|
// grab the slice size with regexp
|
||||||
intz := sliceSizeRegex.FindAllString(sliced, -1)
|
re := regexp.MustCompile("[0-9]+")
|
||||||
|
intz := re.FindAllString(sliced, -1)
|
||||||
|
|
||||||
if len(intz) == 0 {
|
if len(intz) == 0 {
|
||||||
// is a slice
|
// is a slice
|
||||||
@@ -111,7 +109,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
|
|||||||
}
|
}
|
||||||
typ.stringKind = embeddedType.stringKind + sliced
|
typ.stringKind = embeddedType.stringKind + sliced
|
||||||
} else {
|
} else {
|
||||||
return Type{}, errors.New("invalid formatting of array type")
|
return Type{}, fmt.Errorf("invalid formatting of array type")
|
||||||
}
|
}
|
||||||
return typ, err
|
return typ, err
|
||||||
}
|
}
|
||||||
@@ -156,9 +154,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
|
|||||||
if varSize == 0 {
|
if varSize == 0 {
|
||||||
typ.T = BytesTy
|
typ.T = BytesTy
|
||||||
} else {
|
} else {
|
||||||
if varSize > 32 {
|
|
||||||
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
|
|
||||||
}
|
|
||||||
typ.T = FixedBytesTy
|
typ.T = FixedBytesTy
|
||||||
typ.Size = varSize
|
typ.Size = varSize
|
||||||
}
|
}
|
||||||
@@ -181,6 +176,9 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
|
|||||||
return Type{}, errors.New("abi: purely anonymous or underscored field is not supported")
|
return Type{}, errors.New("abi: purely anonymous or underscored field is not supported")
|
||||||
}
|
}
|
||||||
fieldName := ResolveNameConflict(name, func(s string) bool { return used[s] })
|
fieldName := ResolveNameConflict(name, func(s string) bool { return used[s] })
|
||||||
|
if err != nil {
|
||||||
|
return Type{}, err
|
||||||
|
}
|
||||||
used[fieldName] = true
|
used[fieldName] = true
|
||||||
if !isValidFieldName(fieldName) {
|
if !isValidFieldName(fieldName) {
|
||||||
return Type{}, fmt.Errorf("field %d has invalid name", idx)
|
return Type{}, fmt.Errorf("field %d has invalid name", idx)
|
||||||
@@ -219,12 +217,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
|
|||||||
typ.T = FunctionTy
|
typ.T = FunctionTy
|
||||||
typ.Size = 24
|
typ.Size = 24
|
||||||
default:
|
default:
|
||||||
if strings.HasPrefix(internalType, "contract ") {
|
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
|
||||||
typ.Size = 20
|
|
||||||
typ.T = AddressTy
|
|
||||||
} else {
|
|
||||||
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -352,7 +345,7 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// requiresLengthPrefix returns whether the type requires any sort of length
|
// requireLengthPrefix returns whether the type requires any sort of length
|
||||||
// prefixing.
|
// prefixing.
|
||||||
func (t Type) requiresLengthPrefix() bool {
|
func (t Type) requiresLengthPrefix() bool {
|
||||||
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy
|
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy
|
||||||
|
|||||||
@@ -25,13 +25,12 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// typeWithoutStringer is an alias for the Type type which simply doesn't implement
|
// typeWithoutStringer is a alias for the Type type which simply doesn't implement
|
||||||
// the stringer interface to allow printing type details in the tests below.
|
// the stringer interface to allow printing type details in the tests below.
|
||||||
type typeWithoutStringer Type
|
type typeWithoutStringer Type
|
||||||
|
|
||||||
// Tests that all allowed types get recognized by the type parser.
|
// Tests that all allowed types get recognized by the type parser.
|
||||||
func TestTypeRegexp(t *testing.T) {
|
func TestTypeRegexp(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
blob string
|
blob string
|
||||||
components []ArgumentMarshaling
|
components []ArgumentMarshaling
|
||||||
@@ -118,7 +117,6 @@ func TestTypeRegexp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTypeCheck(t *testing.T) {
|
func TestTypeCheck(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
typ string
|
typ string
|
||||||
components []ArgumentMarshaling
|
components []ArgumentMarshaling
|
||||||
@@ -310,7 +308,6 @@ func TestTypeCheck(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInternalType(t *testing.T) {
|
func TestInternalType(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
components := []ArgumentMarshaling{{Name: "a", Type: "int64"}}
|
components := []ArgumentMarshaling{{Name: "a", Type: "int64"}}
|
||||||
internalType := "struct a.b[]"
|
internalType := "struct a.b[]"
|
||||||
kind := Type{
|
kind := Type{
|
||||||
@@ -335,7 +332,6 @@ func TestInternalType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetTypeSize(t *testing.T) {
|
func TestGetTypeSize(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var testCases = []struct {
|
var testCases = []struct {
|
||||||
typ string
|
typ string
|
||||||
components []ArgumentMarshaling
|
components []ArgumentMarshaling
|
||||||
@@ -370,11 +366,3 @@ func TestGetTypeSize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewFixedBytesOver32(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
_, err := NewType("bytes4096", "", nil)
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("fixed bytes with size over 32 is not spec'd")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,9 +18,7 @@ package abi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
@@ -35,72 +33,43 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ReadInteger reads the integer based on its kind and returns the appropriate value.
|
// ReadInteger reads the integer based on its kind and returns the appropriate value.
|
||||||
func ReadInteger(typ Type, b []byte) (interface{}, error) {
|
func ReadInteger(typ Type, b []byte) interface{} {
|
||||||
ret := new(big.Int).SetBytes(b)
|
|
||||||
|
|
||||||
if typ.T == UintTy {
|
if typ.T == UintTy {
|
||||||
u64, isu64 := ret.Uint64(), ret.IsUint64()
|
|
||||||
switch typ.Size {
|
switch typ.Size {
|
||||||
case 8:
|
case 8:
|
||||||
if !isu64 || u64 > math.MaxUint8 {
|
return b[len(b)-1]
|
||||||
return nil, errBadUint8
|
|
||||||
}
|
|
||||||
return byte(u64), nil
|
|
||||||
case 16:
|
case 16:
|
||||||
if !isu64 || u64 > math.MaxUint16 {
|
return binary.BigEndian.Uint16(b[len(b)-2:])
|
||||||
return nil, errBadUint16
|
|
||||||
}
|
|
||||||
return uint16(u64), nil
|
|
||||||
case 32:
|
case 32:
|
||||||
if !isu64 || u64 > math.MaxUint32 {
|
return binary.BigEndian.Uint32(b[len(b)-4:])
|
||||||
return nil, errBadUint32
|
|
||||||
}
|
|
||||||
return uint32(u64), nil
|
|
||||||
case 64:
|
case 64:
|
||||||
if !isu64 {
|
return binary.BigEndian.Uint64(b[len(b)-8:])
|
||||||
return nil, errBadUint64
|
|
||||||
}
|
|
||||||
return u64, nil
|
|
||||||
default:
|
default:
|
||||||
// the only case left for unsigned integer is uint256.
|
// the only case left for unsigned integer is uint256.
|
||||||
return ret, nil
|
return new(big.Int).SetBytes(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// big.SetBytes can't tell if a number is negative or positive in itself.
|
|
||||||
// On EVM, if the returned number > max int256, it is negative.
|
|
||||||
// A number is > max int256 if the bit at position 255 is set.
|
|
||||||
if ret.Bit(255) == 1 {
|
|
||||||
ret.Add(MaxUint256, new(big.Int).Neg(ret))
|
|
||||||
ret.Add(ret, common.Big1)
|
|
||||||
ret.Neg(ret)
|
|
||||||
}
|
|
||||||
i64, isi64 := ret.Int64(), ret.IsInt64()
|
|
||||||
switch typ.Size {
|
switch typ.Size {
|
||||||
case 8:
|
case 8:
|
||||||
if !isi64 || i64 < math.MinInt8 || i64 > math.MaxInt8 {
|
return int8(b[len(b)-1])
|
||||||
return nil, errBadInt8
|
|
||||||
}
|
|
||||||
return int8(i64), nil
|
|
||||||
case 16:
|
case 16:
|
||||||
if !isi64 || i64 < math.MinInt16 || i64 > math.MaxInt16 {
|
return int16(binary.BigEndian.Uint16(b[len(b)-2:]))
|
||||||
return nil, errBadInt16
|
|
||||||
}
|
|
||||||
return int16(i64), nil
|
|
||||||
case 32:
|
case 32:
|
||||||
if !isi64 || i64 < math.MinInt32 || i64 > math.MaxInt32 {
|
return int32(binary.BigEndian.Uint32(b[len(b)-4:]))
|
||||||
return nil, errBadInt32
|
|
||||||
}
|
|
||||||
return int32(i64), nil
|
|
||||||
case 64:
|
case 64:
|
||||||
if !isi64 {
|
return int64(binary.BigEndian.Uint64(b[len(b)-8:]))
|
||||||
return nil, errBadInt64
|
|
||||||
}
|
|
||||||
return i64, nil
|
|
||||||
default:
|
default:
|
||||||
// the only case left for integer is int256
|
// the only case left for integer is int256
|
||||||
|
// big.SetBytes can't tell if a number is negative or positive in itself.
|
||||||
return ret, nil
|
// On EVM, if the returned number > max int256, it is negative.
|
||||||
|
// A number is > max int256 if the bit at position 255 is set.
|
||||||
|
ret := new(big.Int).SetBytes(b)
|
||||||
|
if ret.Bit(255) == 1 {
|
||||||
|
ret.Add(MaxUint256, new(big.Int).Neg(ret))
|
||||||
|
ret.Add(ret, common.Big1)
|
||||||
|
ret.Neg(ret)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +95,7 @@ func readBool(word []byte) (bool, error) {
|
|||||||
// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
|
// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
|
||||||
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
|
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
|
||||||
if t.T != FunctionTy {
|
if t.T != FunctionTy {
|
||||||
return [24]byte{}, errors.New("abi: invalid type in call to make function type byte array")
|
return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array")
|
||||||
}
|
}
|
||||||
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
|
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
|
||||||
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
|
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
|
||||||
@@ -139,7 +108,7 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
|
|||||||
// ReadFixedBytes uses reflection to create a fixed array to be read from.
|
// ReadFixedBytes uses reflection to create a fixed array to be read from.
|
||||||
func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
|
func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
|
||||||
if t.T != FixedBytesTy {
|
if t.T != FixedBytesTy {
|
||||||
return nil, errors.New("abi: invalid type in call to make fixed byte array")
|
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
|
||||||
}
|
}
|
||||||
// convert
|
// convert
|
||||||
array := reflect.New(t.GetType()).Elem()
|
array := reflect.New(t.GetType()).Elem()
|
||||||
@@ -154,21 +123,20 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)
|
|||||||
return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size)
|
return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size)
|
||||||
}
|
}
|
||||||
if start+32*size > len(output) {
|
if start+32*size > len(output) {
|
||||||
return nil, fmt.Errorf("abi: cannot marshal into go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size)
|
return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size)
|
||||||
}
|
}
|
||||||
|
|
||||||
// this value will become our slice or our array, depending on the type
|
// this value will become our slice or our array, depending on the type
|
||||||
var refSlice reflect.Value
|
var refSlice reflect.Value
|
||||||
|
|
||||||
switch t.T {
|
if t.T == SliceTy {
|
||||||
case SliceTy:
|
|
||||||
// declare our slice
|
// declare our slice
|
||||||
refSlice = reflect.MakeSlice(t.GetType(), size, size)
|
refSlice = reflect.MakeSlice(t.GetType(), size, size)
|
||||||
case ArrayTy:
|
} else if t.T == ArrayTy {
|
||||||
// declare our array
|
// declare our array
|
||||||
refSlice = reflect.New(t.GetType()).Elem()
|
refSlice = reflect.New(t.GetType()).Elem()
|
||||||
default:
|
} else {
|
||||||
return nil, errors.New("abi: invalid type in array/slice unpacking stage")
|
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arrays have packed elements, resulting in longer unpack steps.
|
// Arrays have packed elements, resulting in longer unpack steps.
|
||||||
@@ -194,9 +162,6 @@ func forTupleUnpack(t Type, output []byte) (interface{}, error) {
|
|||||||
virtualArgs := 0
|
virtualArgs := 0
|
||||||
for index, elem := range t.TupleElems {
|
for index, elem := range t.TupleElems {
|
||||||
marshalledValue, err := toGoType((index+virtualArgs)*32, *elem, output)
|
marshalledValue, err := toGoType((index+virtualArgs)*32, *elem, output)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if elem.T == ArrayTy && !isDynamicType(*elem) {
|
if elem.T == ArrayTy && !isDynamicType(*elem) {
|
||||||
// If we have a static array, like [3]uint256, these are coded as
|
// If we have a static array, like [3]uint256, these are coded as
|
||||||
// just like uint256,uint256,uint256.
|
// just like uint256,uint256,uint256.
|
||||||
@@ -214,6 +179,9 @@ func forTupleUnpack(t Type, output []byte) (interface{}, error) {
|
|||||||
// coded as just like uint256,bool,uint256
|
// coded as just like uint256,bool,uint256
|
||||||
virtualArgs += getTypeSize(*elem)/32 - 1
|
virtualArgs += getTypeSize(*elem)/32 - 1
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
retval.Field(index).Set(reflect.ValueOf(marshalledValue))
|
retval.Field(index).Set(reflect.ValueOf(marshalledValue))
|
||||||
}
|
}
|
||||||
return retval.Interface(), nil
|
return retval.Interface(), nil
|
||||||
@@ -266,7 +234,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
|
|||||||
case StringTy: // variable arrays are written at the end of the return bytes
|
case StringTy: // variable arrays are written at the end of the return bytes
|
||||||
return string(output[begin : begin+length]), nil
|
return string(output[begin : begin+length]), nil
|
||||||
case IntTy, UintTy:
|
case IntTy, UintTy:
|
||||||
return ReadInteger(t, returnOutput)
|
return ReadInteger(t, returnOutput), nil
|
||||||
case BoolTy:
|
case BoolTy:
|
||||||
return readBool(returnOutput)
|
return readBool(returnOutput)
|
||||||
case AddressTy:
|
case AddressTy:
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -33,7 +32,6 @@ import (
|
|||||||
|
|
||||||
// TestUnpack tests the general pack/unpack tests in packing_test.go
|
// TestUnpack tests the general pack/unpack tests in packing_test.go
|
||||||
func TestUnpack(t *testing.T) {
|
func TestUnpack(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
for i, test := range packUnpackTests {
|
for i, test := range packUnpackTests {
|
||||||
t.Run(strconv.Itoa(i)+" "+test.def, func(t *testing.T) {
|
t.Run(strconv.Itoa(i)+" "+test.def, func(t *testing.T) {
|
||||||
//Unpack
|
//Unpack
|
||||||
@@ -207,13 +205,13 @@ var unpackTests = []unpackTest{
|
|||||||
def: `[{"type":"bool"}]`,
|
def: `[{"type":"bool"}]`,
|
||||||
enc: "",
|
enc: "",
|
||||||
want: false,
|
want: false,
|
||||||
err: "abi: attempting to unmarshal an empty string while arguments are expected",
|
err: "abi: attempting to unmarshall an empty string while arguments are expected",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
def: `[{"type":"bytes32","indexed":true},{"type":"uint256","indexed":false}]`,
|
def: `[{"type":"bytes32","indexed":true},{"type":"uint256","indexed":false}]`,
|
||||||
enc: "",
|
enc: "",
|
||||||
want: false,
|
want: false,
|
||||||
err: "abi: attempting to unmarshal an empty string while arguments are expected",
|
err: "abi: attempting to unmarshall an empty string while arguments are expected",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
def: `[{"type":"bool","indexed":true},{"type":"uint64","indexed":true}]`,
|
def: `[{"type":"bool","indexed":true},{"type":"uint64","indexed":true}]`,
|
||||||
@@ -225,7 +223,6 @@ var unpackTests = []unpackTest{
|
|||||||
// TestLocalUnpackTests runs test specially designed only for unpacking.
|
// TestLocalUnpackTests runs test specially designed only for unpacking.
|
||||||
// All test cases that can be used to test packing and unpacking should move to packing_test.go
|
// All test cases that can be used to test packing and unpacking should move to packing_test.go
|
||||||
func TestLocalUnpackTests(t *testing.T) {
|
func TestLocalUnpackTests(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
for i, test := range unpackTests {
|
for i, test := range unpackTests {
|
||||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
//Unpack
|
//Unpack
|
||||||
@@ -253,7 +250,6 @@ func TestLocalUnpackTests(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) {
|
func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
|
abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -324,7 +320,6 @@ func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOut
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMethodMultiReturn(t *testing.T) {
|
func TestMethodMultiReturn(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
type reversed struct {
|
type reversed struct {
|
||||||
String string
|
String string
|
||||||
Int *big.Int
|
Int *big.Int
|
||||||
@@ -389,6 +384,7 @@ func TestMethodMultiReturn(t *testing.T) {
|
|||||||
"Can not unpack into a slice with wrong types",
|
"Can not unpack into a slice with wrong types",
|
||||||
}}
|
}}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
err := abi.UnpackIntoInterface(tc.dest, "multi", data)
|
err := abi.UnpackIntoInterface(tc.dest, "multi", data)
|
||||||
@@ -403,7 +399,6 @@ func TestMethodMultiReturn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiReturnWithArray(t *testing.T) {
|
func TestMultiReturnWithArray(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
|
||||||
abi, err := JSON(strings.NewReader(definition))
|
abi, err := JSON(strings.NewReader(definition))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -427,7 +422,6 @@ func TestMultiReturnWithArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiReturnWithStringArray(t *testing.T) {
|
func TestMultiReturnWithStringArray(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
|
||||||
abi, err := JSON(strings.NewReader(definition))
|
abi, err := JSON(strings.NewReader(definition))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -458,7 +452,6 @@ func TestMultiReturnWithStringArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiReturnWithStringSlice(t *testing.T) {
|
func TestMultiReturnWithStringSlice(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
|
||||||
abi, err := JSON(strings.NewReader(definition))
|
abi, err := JSON(strings.NewReader(definition))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -491,7 +484,6 @@ func TestMultiReturnWithStringSlice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
|
func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Similar to TestMultiReturnWithArray, but with a special case in mind:
|
// Similar to TestMultiReturnWithArray, but with a special case in mind:
|
||||||
// values of nested static arrays count towards the size as well, and any element following
|
// values of nested static arrays count towards the size as well, and any element following
|
||||||
// after such nested array argument should be read with the correct offset,
|
// after such nested array argument should be read with the correct offset,
|
||||||
@@ -532,7 +524,6 @@ func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal(t *testing.T) {
|
func TestUnmarshal(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const definition = `[
|
const definition = `[
|
||||||
{ "name" : "int", "type": "function", "outputs": [ { "type": "uint256" } ] },
|
{ "name" : "int", "type": "function", "outputs": [ { "type": "uint256" } ] },
|
||||||
{ "name" : "bool", "type": "function", "outputs": [ { "type": "bool" } ] },
|
{ "name" : "bool", "type": "function", "outputs": [ { "type": "bool" } ] },
|
||||||
@@ -782,7 +773,6 @@ func TestUnmarshal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackTuple(t *testing.T) {
|
func TestUnpackTuple(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
const simpleTuple = `[{"name":"tuple","type":"function","outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
|
const simpleTuple = `[{"name":"tuple","type":"function","outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
|
||||||
abi, err := JSON(strings.NewReader(simpleTuple))
|
abi, err := JSON(strings.NewReader(simpleTuple))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -885,7 +875,6 @@ func TestUnpackTuple(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOOMMaliciousInput(t *testing.T) {
|
func TestOOMMaliciousInput(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
oomTests := []unpackTest{
|
oomTests := []unpackTest{
|
||||||
{
|
{
|
||||||
def: `[{"type": "uint8[]"}]`,
|
def: `[{"type": "uint8[]"}]`,
|
||||||
@@ -946,7 +935,7 @@ func TestOOMMaliciousInput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
encb, err := hex.DecodeString(test.enc)
|
encb, err := hex.DecodeString(test.enc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("invalid hex: %s", test.enc)
|
t.Fatalf("invalid hex: %s" + test.enc)
|
||||||
}
|
}
|
||||||
_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
|
_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -954,165 +943,3 @@ func TestOOMMaliciousInput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
var encodeABI Arguments
|
|
||||||
uint256Ty, err := NewType("uint256", "", nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
encodeABI = Arguments{
|
|
||||||
{Type: uint256Ty},
|
|
||||||
}
|
|
||||||
|
|
||||||
maxU64, ok := new(big.Int).SetString(strconv.FormatUint(math.MaxUint64, 10), 10)
|
|
||||||
if !ok {
|
|
||||||
panic("bug")
|
|
||||||
}
|
|
||||||
maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1))
|
|
||||||
cases := []struct {
|
|
||||||
decodeType string
|
|
||||||
inputValue *big.Int
|
|
||||||
err error
|
|
||||||
expectValue interface{}
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
decodeType: "uint8",
|
|
||||||
inputValue: big.NewInt(math.MaxUint8 + 1),
|
|
||||||
err: errBadUint8,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint8",
|
|
||||||
inputValue: big.NewInt(math.MaxUint8),
|
|
||||||
err: nil,
|
|
||||||
expectValue: uint8(math.MaxUint8),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint16",
|
|
||||||
inputValue: big.NewInt(math.MaxUint16 + 1),
|
|
||||||
err: errBadUint16,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint16",
|
|
||||||
inputValue: big.NewInt(math.MaxUint16),
|
|
||||||
err: nil,
|
|
||||||
expectValue: uint16(math.MaxUint16),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint32",
|
|
||||||
inputValue: big.NewInt(math.MaxUint32 + 1),
|
|
||||||
err: errBadUint32,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint32",
|
|
||||||
inputValue: big.NewInt(math.MaxUint32),
|
|
||||||
err: nil,
|
|
||||||
expectValue: uint32(math.MaxUint32),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint64",
|
|
||||||
inputValue: maxU64Plus1,
|
|
||||||
err: errBadUint64,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint64",
|
|
||||||
inputValue: maxU64,
|
|
||||||
err: nil,
|
|
||||||
expectValue: uint64(math.MaxUint64),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "uint256",
|
|
||||||
inputValue: maxU64Plus1,
|
|
||||||
err: nil,
|
|
||||||
expectValue: maxU64Plus1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int8",
|
|
||||||
inputValue: big.NewInt(math.MaxInt8 + 1),
|
|
||||||
err: errBadInt8,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int8",
|
|
||||||
inputValue: big.NewInt(math.MinInt8 - 1),
|
|
||||||
err: errBadInt8,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int8",
|
|
||||||
inputValue: big.NewInt(math.MaxInt8),
|
|
||||||
err: nil,
|
|
||||||
expectValue: int8(math.MaxInt8),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int16",
|
|
||||||
inputValue: big.NewInt(math.MaxInt16 + 1),
|
|
||||||
err: errBadInt16,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int16",
|
|
||||||
inputValue: big.NewInt(math.MinInt16 - 1),
|
|
||||||
err: errBadInt16,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int16",
|
|
||||||
inputValue: big.NewInt(math.MaxInt16),
|
|
||||||
err: nil,
|
|
||||||
expectValue: int16(math.MaxInt16),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int32",
|
|
||||||
inputValue: big.NewInt(math.MaxInt32 + 1),
|
|
||||||
err: errBadInt32,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int32",
|
|
||||||
inputValue: big.NewInt(math.MinInt32 - 1),
|
|
||||||
err: errBadInt32,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int32",
|
|
||||||
inputValue: big.NewInt(math.MaxInt32),
|
|
||||||
err: nil,
|
|
||||||
expectValue: int32(math.MaxInt32),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int64",
|
|
||||||
inputValue: new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)),
|
|
||||||
err: errBadInt64,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int64",
|
|
||||||
inputValue: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)),
|
|
||||||
err: errBadInt64,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
decodeType: "int64",
|
|
||||||
inputValue: big.NewInt(math.MaxInt64),
|
|
||||||
err: nil,
|
|
||||||
expectValue: int64(math.MaxInt64),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for i, testCase := range cases {
|
|
||||||
packed, err := encodeABI.Pack(testCase.inputValue)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
ty, err := NewType(testCase.decodeType, "", nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
decodeABI := Arguments{
|
|
||||||
{Type: ty},
|
|
||||||
}
|
|
||||||
decoded, err := decodeABI.Unpack(packed)
|
|
||||||
if err != testCase.err {
|
|
||||||
t.Fatalf("Expected error %v, actual error %v. case %d", testCase.err, err, i)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(decoded[0], testCase.expectValue) {
|
|
||||||
t.Fatalf("Expected value %v, actual value %v", testCase.expectValue, decoded[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -21,14 +21,15 @@ import "fmt"
|
|||||||
// ResolveNameConflict returns the next available name for a given thing.
|
// ResolveNameConflict returns the next available name for a given thing.
|
||||||
// This helper can be used for lots of purposes:
|
// This helper can be used for lots of purposes:
|
||||||
//
|
//
|
||||||
// - In solidity function overloading is supported, this function can fix
|
// - In solidity function overloading is supported, this function can fix
|
||||||
// the name conflicts of overloaded functions.
|
// the name conflicts of overloaded functions.
|
||||||
// - In golang binding generation, the parameter(in function, event, error,
|
// - In golang binding generation, the parameter(in function, event, error,
|
||||||
// and struct definition) name will be converted to camelcase style which
|
// and struct definition) name will be converted to camelcase style which
|
||||||
// may eventually lead to name conflicts.
|
// may eventually lead to name conflicts.
|
||||||
//
|
//
|
||||||
// Name conflicts are mostly resolved by adding number suffix. e.g. if the abi contains
|
// Name conflicts are mostly resolved by adding number suffix.
|
||||||
// Methods "send" and "send1", ResolveNameConflict would return "send2" for input "send".
|
// e.g. if the abi contains Methods send, send1
|
||||||
|
// ResolveNameConflict would return send2 for input send.
|
||||||
func ResolveNameConflict(rawName string, used func(string) bool) string {
|
func ResolveNameConflict(rawName string, used func(string) bool) string {
|
||||||
name := rawName
|
name := rawName
|
||||||
ok := used(name)
|
ok := used(name)
|
||||||
|
|||||||
@@ -177,8 +177,7 @@ type Backend interface {
|
|||||||
// safely used to calculate a signature from.
|
// safely used to calculate a signature from.
|
||||||
//
|
//
|
||||||
// The hash is calculated as
|
// The hash is calculated as
|
||||||
//
|
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
||||||
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
|
||||||
//
|
//
|
||||||
// This gives context to the signed message and prevents signing of transactions.
|
// This gives context to the signed message and prevents signing of transactions.
|
||||||
func TextHash(data []byte) []byte {
|
func TextHash(data []byte) []byte {
|
||||||
@@ -190,12 +189,11 @@ func TextHash(data []byte) []byte {
|
|||||||
// safely used to calculate a signature from.
|
// safely used to calculate a signature from.
|
||||||
//
|
//
|
||||||
// The hash is calculated as
|
// The hash is calculated as
|
||||||
//
|
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
||||||
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
|
||||||
//
|
//
|
||||||
// This gives context to the signed message and prevents signing of transactions.
|
// This gives context to the signed message and prevents signing of transactions.
|
||||||
func TextAndHash(data []byte) ([]byte, string) {
|
func TextAndHash(data []byte) ([]byte, string) {
|
||||||
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), string(data))
|
||||||
hasher := sha3.NewLegacyKeccak256()
|
hasher := sha3.NewLegacyKeccak256()
|
||||||
hasher.Write([]byte(msg))
|
hasher.Write([]byte(msg))
|
||||||
return hasher.Sum(nil), msg
|
return hasher.Sum(nil), msg
|
||||||
@@ -214,9 +212,7 @@ const (
|
|||||||
// of starting any background processes such as automatic key derivation.
|
// of starting any background processes such as automatic key derivation.
|
||||||
WalletOpened
|
WalletOpened
|
||||||
|
|
||||||
// WalletDropped is fired when a wallet is removed or disconnected, either via USB
|
// WalletDropped
|
||||||
// or due to a filesystem event in the keystore. This event indicates that the wallet
|
|
||||||
// is no longer available for operations.
|
|
||||||
WalletDropped
|
WalletDropped
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestTextHash(t *testing.T) {
|
func TestTextHash(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
hash := TextHash([]byte("Hello Joe"))
|
hash := TextHash([]byte("Hello Joe"))
|
||||||
want := hexutil.MustDecode("0xa080337ae51c4e064c189e113edd0ba391df9206e2f49db658bb32cf2911730b")
|
want := hexutil.MustDecode("0xa080337ae51c4e064c189e113edd0ba391df9206e2f49db658bb32cf2911730b")
|
||||||
if !bytes.Equal(hash, want) {
|
if !bytes.Equal(hash, want) {
|
||||||
|
|||||||
28
accounts/external/backend.go
vendored
28
accounts/external/backend.go
vendored
@@ -17,7 +17,6 @@
|
|||||||
package external
|
package external
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -99,11 +98,11 @@ func (api *ExternalSigner) Status() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) Open(passphrase string) error {
|
func (api *ExternalSigner) Open(passphrase string) error {
|
||||||
return errors.New("operation not supported on external signers")
|
return fmt.Errorf("operation not supported on external signers")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) Close() error {
|
func (api *ExternalSigner) Close() error {
|
||||||
return errors.New("operation not supported on external signers")
|
return fmt.Errorf("operation not supported on external signers")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) Accounts() []accounts.Account {
|
func (api *ExternalSigner) Accounts() []accounts.Account {
|
||||||
@@ -146,7 +145,7 @@ func (api *ExternalSigner) Contains(account accounts.Account) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
|
func (api *ExternalSigner) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
|
||||||
return accounts.Account{}, errors.New("operation not supported on external signers")
|
return accounts.Account{}, fmt.Errorf("operation not supported on external signers")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
|
func (api *ExternalSigner) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
|
||||||
@@ -205,7 +204,7 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
|
|||||||
to = &t
|
to = &t
|
||||||
}
|
}
|
||||||
args := &apitypes.SendTxArgs{
|
args := &apitypes.SendTxArgs{
|
||||||
Input: &data,
|
Data: &data,
|
||||||
Nonce: hexutil.Uint64(tx.Nonce()),
|
Nonce: hexutil.Uint64(tx.Nonce()),
|
||||||
Value: hexutil.Big(*tx.Value()),
|
Value: hexutil.Big(*tx.Value()),
|
||||||
Gas: hexutil.Uint64(tx.Gas()),
|
Gas: hexutil.Uint64(tx.Gas()),
|
||||||
@@ -215,7 +214,7 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
|
|||||||
switch tx.Type() {
|
switch tx.Type() {
|
||||||
case types.LegacyTxType, types.AccessListTxType:
|
case types.LegacyTxType, types.AccessListTxType:
|
||||||
args.GasPrice = (*hexutil.Big)(tx.GasPrice())
|
args.GasPrice = (*hexutil.Big)(tx.GasPrice())
|
||||||
case types.DynamicFeeTxType, types.BlobTxType, types.SetCodeTxType:
|
case types.DynamicFeeTxType:
|
||||||
args.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
|
args.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
|
||||||
args.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap())
|
args.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap())
|
||||||
default:
|
default:
|
||||||
@@ -235,17 +234,6 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
|
|||||||
accessList := tx.AccessList()
|
accessList := tx.AccessList()
|
||||||
args.AccessList = &accessList
|
args.AccessList = &accessList
|
||||||
}
|
}
|
||||||
if tx.Type() == types.BlobTxType {
|
|
||||||
args.BlobHashes = tx.BlobHashes()
|
|
||||||
sidecar := tx.BlobTxSidecar()
|
|
||||||
if sidecar == nil {
|
|
||||||
return nil, errors.New("blobs must be present for signing")
|
|
||||||
}
|
|
||||||
args.Blobs = sidecar.Blobs
|
|
||||||
args.Commitments = sidecar.Commitments
|
|
||||||
args.Proofs = sidecar.Proofs
|
|
||||||
}
|
|
||||||
|
|
||||||
var res signTransactionResult
|
var res signTransactionResult
|
||||||
if err := api.client.Call(&res, "account_signTransaction", args); err != nil {
|
if err := api.client.Call(&res, "account_signTransaction", args); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -254,14 +242,14 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
|
func (api *ExternalSigner) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
|
||||||
return []byte{}, errors.New("password-operations not supported on external signers")
|
return []byte{}, fmt.Errorf("password-operations not supported on external signers")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
|
func (api *ExternalSigner) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
|
||||||
return nil, errors.New("password-operations not supported on external signers")
|
return nil, fmt.Errorf("password-operations not supported on external signers")
|
||||||
}
|
}
|
||||||
func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
|
func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
|
||||||
return nil, errors.New("password-operations not supported on external signers")
|
return nil, fmt.Errorf("password-operations not supported on external signers")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
|
func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ var LegacyLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000
|
|||||||
// The BIP-32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
|
// The BIP-32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
|
||||||
// defines derivation paths to be of the form:
|
// defines derivation paths to be of the form:
|
||||||
//
|
//
|
||||||
// m / purpose' / coin_type' / account' / change / address_index
|
// m / purpose' / coin_type' / account' / change / address_index
|
||||||
//
|
//
|
||||||
// The BIP-44 spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
// The BIP-44 spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
||||||
// defines that the `purpose` be 44' (or 0x8000002C) for crypto currencies, and
|
// defines that the `purpose` be 44' (or 0x8000002C) for crypto currencies, and
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import (
|
|||||||
// Tests that HD derivation paths can be correctly parsed into our internal binary
|
// Tests that HD derivation paths can be correctly parsed into our internal binary
|
||||||
// representation.
|
// representation.
|
||||||
func TestHDPathParsing(t *testing.T) {
|
func TestHDPathParsing(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
output DerivationPath
|
output DerivationPath
|
||||||
@@ -90,7 +89,6 @@ func testDerive(t *testing.T, next func() DerivationPath, expected []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHdPathIteration(t *testing.T) {
|
func TestHdPathIteration(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
testDerive(t, DefaultIterator(DefaultBaseDerivationPath),
|
testDerive(t, DefaultIterator(DefaultBaseDerivationPath),
|
||||||
[]string{
|
[]string{
|
||||||
"m/44'/60'/0'/0/0", "m/44'/60'/0'/0/1",
|
"m/44'/60'/0'/0/0", "m/44'/60'/0'/0/1",
|
||||||
|
|||||||
@@ -22,13 +22,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mapset "github.com/deckarep/golang-set/v2"
|
mapset "github.com/deckarep/golang-set"
|
||||||
"github.com/ethereum/go-ethereum/accounts"
|
"github.com/ethereum/go-ethereum/accounts"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
@@ -39,12 +38,14 @@ import (
|
|||||||
// exist yet, the code will attempt to create a watcher at most this often.
|
// exist yet, the code will attempt to create a watcher at most this often.
|
||||||
const minReloadInterval = 2 * time.Second
|
const minReloadInterval = 2 * time.Second
|
||||||
|
|
||||||
// byURL defines the sorting order for accounts.
|
type accountsByURL []accounts.Account
|
||||||
func byURL(a, b accounts.Account) int {
|
|
||||||
return a.URL.Cmp(b.URL)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AmbiguousAddrError is returned when an address matches multiple files.
|
func (s accountsByURL) Len() int { return len(s) }
|
||||||
|
func (s accountsByURL) Less(i, j int) bool { return s[i].URL.Cmp(s[j].URL) < 0 }
|
||||||
|
func (s accountsByURL) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
|
||||||
|
// AmbiguousAddrError is returned when attempting to unlock
|
||||||
|
// an address for which more than one file exists.
|
||||||
type AmbiguousAddrError struct {
|
type AmbiguousAddrError struct {
|
||||||
Addr common.Address
|
Addr common.Address
|
||||||
Matches []accounts.Account
|
Matches []accounts.Account
|
||||||
@@ -66,7 +67,7 @@ type accountCache struct {
|
|||||||
keydir string
|
keydir string
|
||||||
watcher *watcher
|
watcher *watcher
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
all []accounts.Account
|
all accountsByURL
|
||||||
byAddr map[common.Address][]accounts.Account
|
byAddr map[common.Address][]accounts.Account
|
||||||
throttle *time.Timer
|
throttle *time.Timer
|
||||||
notify chan struct{}
|
notify chan struct{}
|
||||||
@@ -78,7 +79,7 @@ func newAccountCache(keydir string) (*accountCache, chan struct{}) {
|
|||||||
keydir: keydir,
|
keydir: keydir,
|
||||||
byAddr: make(map[common.Address][]accounts.Account),
|
byAddr: make(map[common.Address][]accounts.Account),
|
||||||
notify: make(chan struct{}, 1),
|
notify: make(chan struct{}, 1),
|
||||||
fileC: fileCache{all: mapset.NewThreadUnsafeSet[string]()},
|
fileC: fileCache{all: mapset.NewThreadUnsafeSet()},
|
||||||
}
|
}
|
||||||
ac.watcher = newWatcher(ac)
|
ac.watcher = newWatcher(ac)
|
||||||
return ac, ac.notify
|
return ac, ac.notify
|
||||||
@@ -145,14 +146,6 @@ func (ac *accountCache) deleteByFile(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// watcherStarted returns true if the watcher loop started running (even if it
|
|
||||||
// has since also ended).
|
|
||||||
func (ac *accountCache) watcherStarted() bool {
|
|
||||||
ac.mu.Lock()
|
|
||||||
defer ac.mu.Unlock()
|
|
||||||
return ac.watcher.running || ac.watcher.runEnded
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account {
|
func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account {
|
||||||
for i := range slice {
|
for i := range slice {
|
||||||
if slice[i] == elem {
|
if slice[i] == elem {
|
||||||
@@ -193,7 +186,7 @@ func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
|
|||||||
default:
|
default:
|
||||||
err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))}
|
err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))}
|
||||||
copy(err.Matches, matches)
|
copy(err.Matches, matches)
|
||||||
slices.SortFunc(err.Matches, byURL)
|
sort.Sort(accountsByURL(err.Matches))
|
||||||
return accounts.Account{}, err
|
return accounts.Account{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -282,15 +275,16 @@ func (ac *accountCache) scanAccounts() error {
|
|||||||
// Process all the file diffs
|
// Process all the file diffs
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
for _, path := range creates.ToSlice() {
|
for _, p := range creates.ToSlice() {
|
||||||
if a := readAccount(path); a != nil {
|
if a := readAccount(p.(string)); a != nil {
|
||||||
ac.add(*a)
|
ac.add(*a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, path := range deletes.ToSlice() {
|
for _, p := range deletes.ToSlice() {
|
||||||
ac.deleteByFile(path)
|
ac.deleteByFile(p.(string))
|
||||||
}
|
}
|
||||||
for _, path := range updates.ToSlice() {
|
for _, p := range updates.ToSlice() {
|
||||||
|
path := p.(string)
|
||||||
ac.deleteByFile(path)
|
ac.deleteByFile(path)
|
||||||
if a := readAccount(path); a != nil {
|
if a := readAccount(path); a != nil {
|
||||||
ac.add(*a)
|
ac.add(*a)
|
||||||
|
|||||||
@@ -17,13 +17,12 @@
|
|||||||
package keystore
|
package keystore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"slices"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -51,48 +50,15 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// waitWatcherStart waits up to 1s for the keystore watcher to start.
|
|
||||||
func waitWatcherStart(ks *KeyStore) bool {
|
|
||||||
// On systems where file watch is not supported, just return "ok".
|
|
||||||
if !ks.cache.watcher.enabled() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// The watcher should start, and then exit.
|
|
||||||
for t0 := time.Now(); time.Since(t0) < 1*time.Second; time.Sleep(100 * time.Millisecond) {
|
|
||||||
if ks.cache.watcherStarted() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
|
|
||||||
var list []accounts.Account
|
|
||||||
for t0 := time.Now(); time.Since(t0) < 5*time.Second; time.Sleep(100 * time.Millisecond) {
|
|
||||||
list = ks.Accounts()
|
|
||||||
if reflect.DeepEqual(list, wantAccounts) {
|
|
||||||
// ks should have also received change notifications
|
|
||||||
select {
|
|
||||||
case <-ks.changes:
|
|
||||||
default:
|
|
||||||
return errors.New("wasn't notified of new accounts")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("\ngot %v\nwant %v", list, wantAccounts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWatchNewFile(t *testing.T) {
|
func TestWatchNewFile(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
dir, ks := tmpKeyStore(t)
|
dir, ks := tmpKeyStore(t, false)
|
||||||
|
|
||||||
// Ensure the watcher is started before adding any files.
|
// Ensure the watcher is started before adding any files.
|
||||||
ks.Accounts()
|
ks.Accounts()
|
||||||
if !waitWatcherStart(ks) {
|
time.Sleep(1000 * time.Millisecond)
|
||||||
t.Fatal("keystore watcher didn't start in time")
|
|
||||||
}
|
|
||||||
// Move in the files.
|
// Move in the files.
|
||||||
wantAccounts := make([]accounts.Account, len(cachetestAccounts))
|
wantAccounts := make([]accounts.Account, len(cachetestAccounts))
|
||||||
for i := range cachetestAccounts {
|
for i := range cachetestAccounts {
|
||||||
@@ -106,26 +72,40 @@ func TestWatchNewFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ks should see the accounts.
|
// ks should see the accounts.
|
||||||
if err := waitForAccounts(wantAccounts, ks); err != nil {
|
var list []accounts.Account
|
||||||
t.Error(err)
|
for d := 200 * time.Millisecond; d < 5*time.Second; d *= 2 {
|
||||||
|
list = ks.Accounts()
|
||||||
|
if reflect.DeepEqual(list, wantAccounts) {
|
||||||
|
// ks should have also received change notifications
|
||||||
|
select {
|
||||||
|
case <-ks.changes:
|
||||||
|
default:
|
||||||
|
t.Fatalf("wasn't notified of new accounts")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(d)
|
||||||
}
|
}
|
||||||
|
t.Errorf("got %s, want %s", spew.Sdump(list), spew.Sdump(wantAccounts))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWatchNoDir(t *testing.T) {
|
func TestWatchNoDir(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create ks but not the directory that it watches.
|
// Create ks but not the directory that it watches.
|
||||||
dir := filepath.Join(t.TempDir(), fmt.Sprintf("eth-keystore-watchnodir-test-%d-%d", os.Getpid(), rand.Int()))
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watchnodir-test-%d-%d", os.Getpid(), rand.Int()))
|
||||||
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
|
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
|
||||||
|
|
||||||
list := ks.Accounts()
|
list := ks.Accounts()
|
||||||
if len(list) > 0 {
|
if len(list) > 0 {
|
||||||
t.Error("initial account list not empty:", list)
|
t.Error("initial account list not empty:", list)
|
||||||
}
|
}
|
||||||
// The watcher should start, and then exit.
|
time.Sleep(100 * time.Millisecond)
|
||||||
if !waitWatcherStart(ks) {
|
|
||||||
t.Fatal("keystore watcher didn't start in time")
|
|
||||||
}
|
|
||||||
// Create the directory and copy a key file into it.
|
// Create the directory and copy a key file into it.
|
||||||
os.MkdirAll(dir, 0700)
|
os.MkdirAll(dir, 0700)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
file := filepath.Join(dir, "aaa")
|
file := filepath.Join(dir, "aaa")
|
||||||
if err := cp.CopyFile(file, cachetestAccounts[0].URL.Path); err != nil {
|
if err := cp.CopyFile(file, cachetestAccounts[0].URL.Path); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -151,7 +131,6 @@ func TestWatchNoDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCacheInitialReload(t *testing.T) {
|
func TestCacheInitialReload(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
cache, _ := newAccountCache(cachetestDir)
|
cache, _ := newAccountCache(cachetestDir)
|
||||||
accounts := cache.accounts()
|
accounts := cache.accounts()
|
||||||
if !reflect.DeepEqual(accounts, cachetestAccounts) {
|
if !reflect.DeepEqual(accounts, cachetestAccounts) {
|
||||||
@@ -160,7 +139,6 @@ func TestCacheInitialReload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCacheAddDeleteOrder(t *testing.T) {
|
func TestCacheAddDeleteOrder(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
cache, _ := newAccountCache("testdata/no-such-dir")
|
cache, _ := newAccountCache("testdata/no-such-dir")
|
||||||
cache.watcher.running = true // prevent unexpected reloads
|
cache.watcher.running = true // prevent unexpected reloads
|
||||||
|
|
||||||
@@ -204,7 +182,7 @@ func TestCacheAddDeleteOrder(t *testing.T) {
|
|||||||
// Check that the account list is sorted by filename.
|
// Check that the account list is sorted by filename.
|
||||||
wantAccounts := make([]accounts.Account, len(accs))
|
wantAccounts := make([]accounts.Account, len(accs))
|
||||||
copy(wantAccounts, accs)
|
copy(wantAccounts, accs)
|
||||||
slices.SortFunc(wantAccounts, byURL)
|
sort.Sort(accountsByURL(wantAccounts))
|
||||||
list := cache.accounts()
|
list := cache.accounts()
|
||||||
if !reflect.DeepEqual(list, wantAccounts) {
|
if !reflect.DeepEqual(list, wantAccounts) {
|
||||||
t.Fatalf("got accounts: %s\nwant %s", spew.Sdump(accs), spew.Sdump(wantAccounts))
|
t.Fatalf("got accounts: %s\nwant %s", spew.Sdump(accs), spew.Sdump(wantAccounts))
|
||||||
@@ -245,7 +223,6 @@ func TestCacheAddDeleteOrder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCacheFind(t *testing.T) {
|
func TestCacheFind(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
dir := filepath.Join("testdata", "dir")
|
dir := filepath.Join("testdata", "dir")
|
||||||
cache, _ := newAccountCache(dir)
|
cache, _ := newAccountCache(dir)
|
||||||
cache.watcher.running = true // prevent unexpected reloads
|
cache.watcher.running = true // prevent unexpected reloads
|
||||||
@@ -318,24 +295,43 @@ func TestCacheFind(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
|
||||||
|
var list []accounts.Account
|
||||||
|
for d := 200 * time.Millisecond; d < 8*time.Second; d *= 2 {
|
||||||
|
list = ks.Accounts()
|
||||||
|
if reflect.DeepEqual(list, wantAccounts) {
|
||||||
|
// ks should have also received change notifications
|
||||||
|
select {
|
||||||
|
case <-ks.changes:
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("wasn't notified of new accounts")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
time.Sleep(d)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("\ngot %v\nwant %v", list, wantAccounts)
|
||||||
|
}
|
||||||
|
|
||||||
// TestUpdatedKeyfileContents tests that updating the contents of a keystore file
|
// TestUpdatedKeyfileContents tests that updating the contents of a keystore file
|
||||||
// is noticed by the watcher, and the account cache is updated accordingly
|
// is noticed by the watcher, and the account cache is updated accordingly
|
||||||
func TestUpdatedKeyfileContents(t *testing.T) {
|
func TestUpdatedKeyfileContents(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create a temporary keystore to test with
|
// Create a temporary keystore to test with
|
||||||
dir := t.TempDir()
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int()))
|
||||||
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
|
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
|
||||||
|
|
||||||
list := ks.Accounts()
|
list := ks.Accounts()
|
||||||
if len(list) > 0 {
|
if len(list) > 0 {
|
||||||
t.Error("initial account list not empty:", list)
|
t.Error("initial account list not empty:", list)
|
||||||
}
|
}
|
||||||
if !waitWatcherStart(ks) {
|
time.Sleep(100 * time.Millisecond)
|
||||||
t.Fatal("keystore watcher didn't start in time")
|
|
||||||
}
|
// Create the directory and copy a key file into it.
|
||||||
// Copy a key file into it
|
os.MkdirAll(dir, 0700)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
file := filepath.Join(dir, "aaa")
|
file := filepath.Join(dir, "aaa")
|
||||||
|
|
||||||
// Place one of our testfiles in there
|
// Place one of our testfiles in there
|
||||||
@@ -350,8 +346,9 @@ func TestUpdatedKeyfileContents(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// needed so that modTime of `file` is different to its current value after forceCopyFile
|
// needed so that modTime of `file` is different to its current value after forceCopyFile
|
||||||
os.Chtimes(file, time.Now().Add(-time.Second), time.Now().Add(-time.Second))
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
|
||||||
// Now replace file contents
|
// Now replace file contents
|
||||||
if err := forceCopyFile(file, cachetestAccounts[1].URL.Path); err != nil {
|
if err := forceCopyFile(file, cachetestAccounts[1].URL.Path); err != nil {
|
||||||
@@ -367,7 +364,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// needed so that modTime of `file` is different to its current value after forceCopyFile
|
// needed so that modTime of `file` is different to its current value after forceCopyFile
|
||||||
os.Chtimes(file, time.Now().Add(-time.Second), time.Now().Add(-time.Second))
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
|
||||||
// Now replace file contents again
|
// Now replace file contents again
|
||||||
if err := forceCopyFile(file, cachetestAccounts[2].URL.Path); err != nil {
|
if err := forceCopyFile(file, cachetestAccounts[2].URL.Path); err != nil {
|
||||||
@@ -383,7 +380,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// needed so that modTime of `file` is different to its current value after os.WriteFile
|
// needed so that modTime of `file` is different to its current value after os.WriteFile
|
||||||
os.Chtimes(file, time.Now().Add(-time.Second), time.Now().Add(-time.Second))
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
|
||||||
// Now replace file contents with crap
|
// Now replace file contents with crap
|
||||||
if err := os.WriteFile(file, []byte("foo"), 0600); err != nil {
|
if err := os.WriteFile(file, []byte("foo"), 0600); err != nil {
|
||||||
|
|||||||
@@ -23,20 +23,20 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mapset "github.com/deckarep/golang-set/v2"
|
mapset "github.com/deckarep/golang-set"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fileCache is a cache of files seen during scan of keystore.
|
// fileCache is a cache of files seen during scan of keystore.
|
||||||
type fileCache struct {
|
type fileCache struct {
|
||||||
all mapset.Set[string] // Set of all files from the keystore folder
|
all mapset.Set // Set of all files from the keystore folder
|
||||||
lastMod time.Time // Last time instance when a file was modified
|
lastMod time.Time // Last time instance when a file was modified
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// scan performs a new scan on the given directory, compares against the already
|
// scan performs a new scan on the given directory, compares against the already
|
||||||
// cached filenames, and returns file sets: creates, deletes, updates.
|
// cached filenames, and returns file sets: creates, deletes, updates.
|
||||||
func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) {
|
func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) {
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
// List all the files from the keystore folder
|
// List all the files from the keystore folder
|
||||||
@@ -50,8 +50,8 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string]
|
|||||||
defer fc.mu.Unlock()
|
defer fc.mu.Unlock()
|
||||||
|
|
||||||
// Iterate all the files and gather their metadata
|
// Iterate all the files and gather their metadata
|
||||||
all := mapset.NewThreadUnsafeSet[string]()
|
all := mapset.NewThreadUnsafeSet()
|
||||||
mods := mapset.NewThreadUnsafeSet[string]()
|
mods := mapset.NewThreadUnsafeSet()
|
||||||
|
|
||||||
var newLastMod time.Time
|
var newLastMod time.Time
|
||||||
for _, fi := range files {
|
for _, fi := range files {
|
||||||
|
|||||||
@@ -87,6 +87,15 @@ func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore {
|
|||||||
return ks
|
return ks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPlaintextKeyStore creates a keystore for the given directory.
|
||||||
|
// Deprecated: Use NewKeyStore.
|
||||||
|
func NewPlaintextKeyStore(keydir string) *KeyStore {
|
||||||
|
keydir, _ = filepath.Abs(keydir)
|
||||||
|
ks := &KeyStore{storage: &keyStorePlain{keydir}}
|
||||||
|
ks.init(keydir)
|
||||||
|
return ks
|
||||||
|
}
|
||||||
|
|
||||||
func (ks *KeyStore) init(keydir string) {
|
func (ks *KeyStore) init(keydir string) {
|
||||||
// Lock the mutex since the account cache might call back with events
|
// Lock the mutex since the account cache might call back with events
|
||||||
ks.mu.Lock()
|
ks.mu.Lock()
|
||||||
@@ -312,10 +321,11 @@ func (ks *KeyStore) Unlock(a accounts.Account, passphrase string) error {
|
|||||||
// Lock removes the private key with the given address from memory.
|
// Lock removes the private key with the given address from memory.
|
||||||
func (ks *KeyStore) Lock(addr common.Address) error {
|
func (ks *KeyStore) Lock(addr common.Address) error {
|
||||||
ks.mu.Lock()
|
ks.mu.Lock()
|
||||||
unl, found := ks.unlocked[addr]
|
if unl, found := ks.unlocked[addr]; found {
|
||||||
ks.mu.Unlock()
|
ks.mu.Unlock()
|
||||||
if found {
|
|
||||||
ks.expire(addr, unl, time.Duration(0)*time.Nanosecond)
|
ks.expire(addr, unl, time.Duration(0)*time.Nanosecond)
|
||||||
|
} else {
|
||||||
|
ks.mu.Unlock()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -488,16 +498,10 @@ func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (account
|
|||||||
return a, nil
|
return a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isUpdating returns whether the event notification loop is running.
|
|
||||||
// This method is mainly meant for tests.
|
|
||||||
func (ks *KeyStore) isUpdating() bool {
|
|
||||||
ks.mu.RLock()
|
|
||||||
defer ks.mu.RUnlock()
|
|
||||||
return ks.updating
|
|
||||||
}
|
|
||||||
|
|
||||||
// zeroKey zeroes a private key in memory.
|
// zeroKey zeroes a private key in memory.
|
||||||
func zeroKey(k *ecdsa.PrivateKey) {
|
func zeroKey(k *ecdsa.PrivateKey) {
|
||||||
b := k.D.Bits()
|
b := k.D.Bits()
|
||||||
clear(b)
|
for i := range b {
|
||||||
|
b[i] = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"slices"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -36,8 +36,7 @@ import (
|
|||||||
var testSigData = make([]byte, 32)
|
var testSigData = make([]byte, 32)
|
||||||
|
|
||||||
func TestKeyStore(t *testing.T) {
|
func TestKeyStore(t *testing.T) {
|
||||||
t.Parallel()
|
dir, ks := tmpKeyStore(t, true)
|
||||||
dir, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
a, err := ks.NewAccount("foo")
|
a, err := ks.NewAccount("foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -71,8 +70,7 @@ func TestKeyStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSign(t *testing.T) {
|
func TestSign(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, true)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
pass := "" // not used but required by API
|
pass := "" // not used but required by API
|
||||||
a1, err := ks.NewAccount(pass)
|
a1, err := ks.NewAccount(pass)
|
||||||
@@ -88,8 +86,7 @@ func TestSign(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSignWithPassphrase(t *testing.T) {
|
func TestSignWithPassphrase(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, true)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
pass := "passwd"
|
pass := "passwd"
|
||||||
acc, err := ks.NewAccount(pass)
|
acc, err := ks.NewAccount(pass)
|
||||||
@@ -116,8 +113,7 @@ func TestSignWithPassphrase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTimedUnlock(t *testing.T) {
|
func TestTimedUnlock(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, true)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
pass := "foo"
|
pass := "foo"
|
||||||
a1, err := ks.NewAccount(pass)
|
a1, err := ks.NewAccount(pass)
|
||||||
@@ -151,8 +147,7 @@ func TestTimedUnlock(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOverrideUnlock(t *testing.T) {
|
func TestOverrideUnlock(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, false)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
pass := "foo"
|
pass := "foo"
|
||||||
a1, err := ks.NewAccount(pass)
|
a1, err := ks.NewAccount(pass)
|
||||||
@@ -192,8 +187,7 @@ func TestOverrideUnlock(t *testing.T) {
|
|||||||
|
|
||||||
// This test should fail under -race if signing races the expiration goroutine.
|
// This test should fail under -race if signing races the expiration goroutine.
|
||||||
func TestSignRace(t *testing.T) {
|
func TestSignRace(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, false)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
// Create a test account.
|
// Create a test account.
|
||||||
a1, err := ks.NewAccount("")
|
a1, err := ks.NewAccount("")
|
||||||
@@ -217,33 +211,19 @@ func TestSignRace(t *testing.T) {
|
|||||||
t.Errorf("Account did not lock within the timeout")
|
t.Errorf("Account did not lock within the timeout")
|
||||||
}
|
}
|
||||||
|
|
||||||
// waitForKsUpdating waits until the updating-status of the ks reaches the
|
|
||||||
// desired wantStatus.
|
|
||||||
// It waits for a maximum time of maxTime, and returns false if it does not
|
|
||||||
// finish in time
|
|
||||||
func waitForKsUpdating(t *testing.T, ks *KeyStore, wantStatus bool, maxTime time.Duration) bool {
|
|
||||||
t.Helper()
|
|
||||||
// Wait max 250 ms, then return false
|
|
||||||
for t0 := time.Now(); time.Since(t0) < maxTime; {
|
|
||||||
if ks.isUpdating() == wantStatus {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
time.Sleep(25 * time.Millisecond)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests that the wallet notifier loop starts and stops correctly based on the
|
// Tests that the wallet notifier loop starts and stops correctly based on the
|
||||||
// addition and removal of wallet event subscriptions.
|
// addition and removal of wallet event subscriptions.
|
||||||
func TestWalletNotifierLifecycle(t *testing.T) {
|
func TestWalletNotifierLifecycle(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Create a temporary keystore to test with
|
// Create a temporary keystore to test with
|
||||||
_, ks := tmpKeyStore(t)
|
_, ks := tmpKeyStore(t, false)
|
||||||
|
|
||||||
// Ensure that the notification updater is not running yet
|
// Ensure that the notification updater is not running yet
|
||||||
time.Sleep(250 * time.Millisecond)
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
ks.mu.RLock()
|
||||||
|
updating := ks.updating
|
||||||
|
ks.mu.RUnlock()
|
||||||
|
|
||||||
if ks.isUpdating() {
|
if updating {
|
||||||
t.Errorf("wallet notifier running without subscribers")
|
t.Errorf("wallet notifier running without subscribers")
|
||||||
}
|
}
|
||||||
// Subscribe to the wallet feed and ensure the updater boots up
|
// Subscribe to the wallet feed and ensure the updater boots up
|
||||||
@@ -253,26 +233,38 @@ func TestWalletNotifierLifecycle(t *testing.T) {
|
|||||||
for i := 0; i < len(subs); i++ {
|
for i := 0; i < len(subs); i++ {
|
||||||
// Create a new subscription
|
// Create a new subscription
|
||||||
subs[i] = ks.Subscribe(updates)
|
subs[i] = ks.Subscribe(updates)
|
||||||
if !waitForKsUpdating(t, ks, true, 250*time.Millisecond) {
|
|
||||||
|
// Ensure the notifier comes online
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
ks.mu.RLock()
|
||||||
|
updating = ks.updating
|
||||||
|
ks.mu.RUnlock()
|
||||||
|
|
||||||
|
if !updating {
|
||||||
t.Errorf("sub %d: wallet notifier not running after subscription", i)
|
t.Errorf("sub %d: wallet notifier not running after subscription", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Close all but one sub
|
// Unsubscribe and ensure the updater terminates eventually
|
||||||
for i := 0; i < len(subs)-1; i++ {
|
for i := 0; i < len(subs); i++ {
|
||||||
// Close an existing subscription
|
// Close an existing subscription
|
||||||
subs[i].Unsubscribe()
|
subs[i].Unsubscribe()
|
||||||
}
|
|
||||||
// Check that it is still running
|
|
||||||
time.Sleep(250 * time.Millisecond)
|
|
||||||
|
|
||||||
if !ks.isUpdating() {
|
// Ensure the notifier shuts down at and only at the last close
|
||||||
t.Fatal("event notifier stopped prematurely")
|
for k := 0; k < int(walletRefreshCycle/(250*time.Millisecond))+2; k++ {
|
||||||
}
|
ks.mu.RLock()
|
||||||
// Unsubscribe the last one and ensure the updater terminates eventually.
|
updating = ks.updating
|
||||||
subs[len(subs)-1].Unsubscribe()
|
ks.mu.RUnlock()
|
||||||
if !waitForKsUpdating(t, ks, false, 4*time.Second) {
|
|
||||||
t.Errorf("wallet notifier didn't terminate after unsubscribe")
|
if i < len(subs)-1 && !updating {
|
||||||
|
t.Fatalf("sub %d: event notifier stopped prematurely", i)
|
||||||
|
}
|
||||||
|
if i == len(subs)-1 && !updating {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
t.Errorf("wallet notifier didn't terminate after unsubscribe")
|
||||||
}
|
}
|
||||||
|
|
||||||
type walletEvent struct {
|
type walletEvent struct {
|
||||||
@@ -283,8 +275,7 @@ type walletEvent struct {
|
|||||||
// Tests that wallet notifications and correctly fired when accounts are added
|
// Tests that wallet notifications and correctly fired when accounts are added
|
||||||
// or deleted from the keystore.
|
// or deleted from the keystore.
|
||||||
func TestWalletNotifications(t *testing.T) {
|
func TestWalletNotifications(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, false)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
|
|
||||||
// Subscribe to the wallet feed and collect events.
|
// Subscribe to the wallet feed and collect events.
|
||||||
var (
|
var (
|
||||||
@@ -343,10 +334,9 @@ func TestWalletNotifications(t *testing.T) {
|
|||||||
checkEvents(t, wantEvents, events)
|
checkEvents(t, wantEvents, events)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestImportECDSA tests the import functionality of a keystore.
|
// TestImportExport tests the import functionality of a keystore.
|
||||||
func TestImportECDSA(t *testing.T) {
|
func TestImportECDSA(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, true)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
key, err := crypto.GenerateKey()
|
key, err := crypto.GenerateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to generate key: %v", key)
|
t.Fatalf("failed to generate key: %v", key)
|
||||||
@@ -362,10 +352,9 @@ func TestImportECDSA(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestImportExport tests the import and export functionality of a keystore.
|
// TestImportECDSA tests the import and export functionality of a keystore.
|
||||||
func TestImportExport(t *testing.T) {
|
func TestImportExport(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, true)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
acc, err := ks.NewAccount("old")
|
acc, err := ks.NewAccount("old")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create account: %v", acc)
|
t.Fatalf("failed to create account: %v", acc)
|
||||||
@@ -374,7 +363,7 @@ func TestImportExport(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to export account: %v", acc)
|
t.Fatalf("failed to export account: %v", acc)
|
||||||
}
|
}
|
||||||
_, ks2 := tmpKeyStore(t)
|
_, ks2 := tmpKeyStore(t, true)
|
||||||
if _, err = ks2.Import(json, "old", "old"); err == nil {
|
if _, err = ks2.Import(json, "old", "old"); err == nil {
|
||||||
t.Errorf("importing with invalid password succeeded")
|
t.Errorf("importing with invalid password succeeded")
|
||||||
}
|
}
|
||||||
@@ -393,8 +382,7 @@ func TestImportExport(t *testing.T) {
|
|||||||
// TestImportRace tests the keystore on races.
|
// TestImportRace tests the keystore on races.
|
||||||
// This test should fail under -race if importing races.
|
// This test should fail under -race if importing races.
|
||||||
func TestImportRace(t *testing.T) {
|
func TestImportRace(t *testing.T) {
|
||||||
t.Parallel()
|
_, ks := tmpKeyStore(t, true)
|
||||||
_, ks := tmpKeyStore(t)
|
|
||||||
acc, err := ks.NewAccount("old")
|
acc, err := ks.NewAccount("old")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create account: %v", acc)
|
t.Fatalf("failed to create account: %v", acc)
|
||||||
@@ -403,20 +391,20 @@ func TestImportRace(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to export account: %v", acc)
|
t.Fatalf("failed to export account: %v", acc)
|
||||||
}
|
}
|
||||||
_, ks2 := tmpKeyStore(t)
|
_, ks2 := tmpKeyStore(t, true)
|
||||||
var atom atomic.Uint32
|
var atom uint32
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if _, err := ks2.Import(json, "new", "new"); err != nil {
|
if _, err := ks2.Import(json, "new", "new"); err != nil {
|
||||||
atom.Add(1)
|
atomic.AddUint32(&atom, 1)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if atom.Load() != 1 {
|
if atom != 1 {
|
||||||
t.Errorf("Import is racy")
|
t.Errorf("Import is racy")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -431,7 +419,7 @@ func checkAccounts(t *testing.T, live map[common.Address]accounts.Account, walle
|
|||||||
for _, account := range live {
|
for _, account := range live {
|
||||||
liveList = append(liveList, account)
|
liveList = append(liveList, account)
|
||||||
}
|
}
|
||||||
slices.SortFunc(liveList, byURL)
|
sort.Sort(accountsByURL(liveList))
|
||||||
for j, wallet := range wallets {
|
for j, wallet := range wallets {
|
||||||
if accs := wallet.Accounts(); len(accs) != 1 {
|
if accs := wallet.Accounts(); len(accs) != 1 {
|
||||||
t.Errorf("wallet %d: contains invalid number of accounts: have %d, want 1", j, len(accs))
|
t.Errorf("wallet %d: contains invalid number of accounts: have %d, want 1", j, len(accs))
|
||||||
@@ -457,7 +445,11 @@ func checkEvents(t *testing.T, want []walletEvent, have []walletEvent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tmpKeyStore(t *testing.T) (string, *KeyStore) {
|
func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) {
|
||||||
d := t.TempDir()
|
d := t.TempDir()
|
||||||
return d, NewKeyStore(d, veryLightScryptN, veryLightScryptP)
|
newKs := NewPlaintextKeyStore
|
||||||
|
if encrypted {
|
||||||
|
newKs = func(kd string) *KeyStore { return NewKeyStore(kd, veryLightScryptN, veryLightScryptP) }
|
||||||
|
}
|
||||||
|
return d, newKs(d)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ func (ks keyStorePassphrase) JoinPath(filename string) string {
|
|||||||
return filepath.Join(ks.keysDirPath, filename)
|
return filepath.Join(ks.keysDirPath, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncryptDataV3 encrypts the data given as 'data' with the password 'auth'.
|
// Encryptdata encrypts the data given as 'data' with the password 'auth'.
|
||||||
func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error) {
|
func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error) {
|
||||||
salt := make([]byte, 32)
|
salt := make([]byte, 32)
|
||||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
||||||
@@ -225,13 +225,10 @@ func DecryptKey(keyjson []byte, auth string) (*Key, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
key, err := crypto.ToECDSA(keyBytes)
|
key := crypto.ToECDSAUnsafe(keyBytes)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid key: %w", err)
|
|
||||||
}
|
|
||||||
id, err := uuid.FromBytes(keyId)
|
id, err := uuid.FromBytes(keyId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid UUID: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Key{
|
return &Key{
|
||||||
Id: id,
|
Id: id,
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ const (
|
|||||||
|
|
||||||
// Tests that a json key file can be decrypted and encrypted in multiple rounds.
|
// Tests that a json key file can be decrypted and encrypted in multiple rounds.
|
||||||
func TestKeyEncryptDecrypt(t *testing.T) {
|
func TestKeyEncryptDecrypt(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
keyjson, err := os.ReadFile("testdata/very-light-scrypt.json")
|
keyjson, err := os.ReadFile("testdata/very-light-scrypt.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -55,7 +54,7 @@ func TestKeyEncryptDecrypt(t *testing.T) {
|
|||||||
// Recrypt with a new password and start over
|
// Recrypt with a new password and start over
|
||||||
password += "new data appended" // nolint: gosec
|
password += "new data appended" // nolint: gosec
|
||||||
if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil {
|
if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil {
|
||||||
t.Errorf("test %d: failed to re-encrypt key %v", i, err)
|
t.Errorf("test %d: failed to recrypt key %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyStorePlain(t *testing.T) {
|
func TestKeyStorePlain(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
_, ks := tmpKeyStoreIface(t, false)
|
_, ks := tmpKeyStoreIface(t, false)
|
||||||
|
|
||||||
pass := "" // not used but required by API
|
pass := "" // not used but required by API
|
||||||
@@ -61,7 +60,6 @@ func TestKeyStorePlain(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyStorePassphrase(t *testing.T) {
|
func TestKeyStorePassphrase(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
_, ks := tmpKeyStoreIface(t, true)
|
_, ks := tmpKeyStoreIface(t, true)
|
||||||
|
|
||||||
pass := "foo"
|
pass := "foo"
|
||||||
@@ -82,7 +80,6 @@ func TestKeyStorePassphrase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
_, ks := tmpKeyStoreIface(t, true)
|
_, ks := tmpKeyStoreIface(t, true)
|
||||||
|
|
||||||
pass := "foo"
|
pass := "foo"
|
||||||
@@ -96,7 +93,6 @@ func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestImportPreSaleKey(t *testing.T) {
|
func TestImportPreSaleKey(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
dir, ks := tmpKeyStoreIface(t, true)
|
dir, ks := tmpKeyStoreIface(t, true)
|
||||||
|
|
||||||
// file content of a presale key file generated with:
|
// file content of a presale key file generated with:
|
||||||
|
|||||||
1
accounts/keystore/testdata/dupes/1
vendored
Normal file
1
accounts/keystore/testdata/dupes/1
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3}
|
||||||
1
accounts/keystore/testdata/dupes/2
vendored
Normal file
1
accounts/keystore/testdata/dupes/2
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3}
|
||||||
1
accounts/keystore/testdata/dupes/foo
vendored
Normal file
1
accounts/keystore/testdata/dupes/foo
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"address":"7ef5a6135f1fd6a02593eedc869c6d41d934aef8","crypto":{"cipher":"aes-128-ctr","ciphertext":"1d0839166e7a15b9c1333fc865d69858b22df26815ccf601b28219b6192974e1","cipherparams":{"iv":"8df6caa7ff1b00c4e871f002cb7921ed"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"e5e6ef3f4ea695f496b643ebd3f75c0aa58ef4070e90c80c5d3fb0241bf1595c"},"mac":"6d16dfde774845e4585357f24bce530528bc69f4f84e1e22880d34fa45c273e5"},"id":"950077c7-71e3-4c44-a4a1-143919141ed4","version":3}
|
||||||
@@ -20,31 +20,28 @@
|
|||||||
package keystore
|
package keystore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/rjeczalik/notify"
|
||||||
)
|
)
|
||||||
|
|
||||||
type watcher struct {
|
type watcher struct {
|
||||||
ac *accountCache
|
ac *accountCache
|
||||||
running bool // set to true when runloop begins
|
starting bool
|
||||||
runEnded bool // set to true when runloop ends
|
running bool
|
||||||
starting bool // set to true prior to runloop starting
|
ev chan notify.EventInfo
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWatcher(ac *accountCache) *watcher {
|
func newWatcher(ac *accountCache) *watcher {
|
||||||
return &watcher{
|
return &watcher{
|
||||||
ac: ac,
|
ac: ac,
|
||||||
|
ev: make(chan notify.EventInfo, 10),
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// enabled returns false on systems not supported.
|
|
||||||
func (*watcher) enabled() bool { return true }
|
|
||||||
|
|
||||||
// starts the watcher loop in the background.
|
// starts the watcher loop in the background.
|
||||||
// Start a watcher in the background if that's not already in progress.
|
// Start a watcher in the background if that's not already in progress.
|
||||||
// The caller must hold w.ac.mu.
|
// The caller must hold w.ac.mu.
|
||||||
@@ -65,26 +62,16 @@ func (w *watcher) loop() {
|
|||||||
w.ac.mu.Lock()
|
w.ac.mu.Lock()
|
||||||
w.running = false
|
w.running = false
|
||||||
w.starting = false
|
w.starting = false
|
||||||
w.runEnded = true
|
|
||||||
w.ac.mu.Unlock()
|
w.ac.mu.Unlock()
|
||||||
}()
|
}()
|
||||||
logger := log.New("path", w.ac.keydir)
|
logger := log.New("path", w.ac.keydir)
|
||||||
|
|
||||||
// Create new watcher.
|
if err := notify.Watch(w.ac.keydir, w.ev, notify.All); err != nil {
|
||||||
watcher, err := fsnotify.NewWatcher()
|
logger.Trace("Failed to watch keystore folder", "err", err)
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to start filesystem watcher", "err", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer watcher.Close()
|
defer notify.Stop(w.ev)
|
||||||
if err := watcher.Add(w.ac.keydir); err != nil {
|
logger.Trace("Started watching keystore folder")
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
logger.Warn("Failed to watch keystore folder", "err", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Trace("Started watching keystore folder", "folder", w.ac.keydir)
|
|
||||||
defer logger.Trace("Stopped watching keystore folder")
|
defer logger.Trace("Stopped watching keystore folder")
|
||||||
|
|
||||||
w.ac.mu.Lock()
|
w.ac.mu.Lock()
|
||||||
@@ -108,24 +95,12 @@ func (w *watcher) loop() {
|
|||||||
select {
|
select {
|
||||||
case <-w.quit:
|
case <-w.quit:
|
||||||
return
|
return
|
||||||
case _, ok := <-watcher.Events:
|
case <-w.ev:
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Trigger the scan (with delay), if not already triggered
|
// Trigger the scan (with delay), if not already triggered
|
||||||
if !rescanTriggered {
|
if !rescanTriggered {
|
||||||
debounce.Reset(debounceDuration)
|
debounce.Reset(debounceDuration)
|
||||||
rescanTriggered = true
|
rescanTriggered = true
|
||||||
}
|
}
|
||||||
// The fsnotify library does provide more granular event-info, it
|
|
||||||
// would be possible to refresh individual affected files instead
|
|
||||||
// of scheduling a full rescan. For most cases though, the
|
|
||||||
// full rescan is quick and obviously simplest.
|
|
||||||
case err, ok := <-watcher.Errors:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Info("Filesystem watcher error", "err", err)
|
|
||||||
case <-debounce.C:
|
case <-debounce.C:
|
||||||
w.ac.scanAccounts()
|
w.ac.scanAccounts()
|
||||||
rescanTriggered = false
|
rescanTriggered = false
|
||||||
|
|||||||
@@ -22,14 +22,8 @@
|
|||||||
|
|
||||||
package keystore
|
package keystore
|
||||||
|
|
||||||
type watcher struct {
|
type watcher struct{ running bool }
|
||||||
running bool
|
|
||||||
runEnded bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func newWatcher(*accountCache) *watcher { return new(watcher) }
|
func newWatcher(*accountCache) *watcher { return new(watcher) }
|
||||||
func (*watcher) start() {}
|
func (*watcher) start() {}
|
||||||
func (*watcher) close() {}
|
func (*watcher) close() {}
|
||||||
|
|
||||||
// enabled returns false on systems not supported.
|
|
||||||
func (*watcher) enabled() bool { return false }
|
|
||||||
|
|||||||
@@ -29,9 +29,12 @@ import (
|
|||||||
// the manager will buffer in its channel.
|
// the manager will buffer in its channel.
|
||||||
const managerSubBufferSize = 50
|
const managerSubBufferSize = 50
|
||||||
|
|
||||||
// Config is a legacy struct which is not used
|
// Config contains the settings of the global account manager.
|
||||||
|
//
|
||||||
|
// TODO(rjl493456442, karalabe, holiman): Get rid of this when account management
|
||||||
|
// is removed in favor of Clef.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
InsecureUnlockAllowed bool // Unused legacy-parameter
|
InsecureUnlockAllowed bool // Whether account unlocking in insecure environment is allowed
|
||||||
}
|
}
|
||||||
|
|
||||||
// newBackendEvent lets the manager know it should
|
// newBackendEvent lets the manager know it should
|
||||||
@@ -44,6 +47,7 @@ type newBackendEvent struct {
|
|||||||
// Manager is an overarching account manager that can communicate with various
|
// Manager is an overarching account manager that can communicate with various
|
||||||
// backends for signing transactions.
|
// backends for signing transactions.
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
|
config *Config // Global account manager configurations
|
||||||
backends map[reflect.Type][]Backend // Index of backends currently registered
|
backends map[reflect.Type][]Backend // Index of backends currently registered
|
||||||
updaters []event.Subscription // Wallet update subscriptions for all backends
|
updaters []event.Subscription // Wallet update subscriptions for all backends
|
||||||
updates chan WalletEvent // Subscription sink for backend wallet changes
|
updates chan WalletEvent // Subscription sink for backend wallet changes
|
||||||
@@ -74,6 +78,7 @@ func NewManager(config *Config, backends ...Backend) *Manager {
|
|||||||
}
|
}
|
||||||
// Assemble the account manager and return
|
// Assemble the account manager and return
|
||||||
am := &Manager{
|
am := &Manager{
|
||||||
|
config: config,
|
||||||
backends: make(map[reflect.Type][]Backend),
|
backends: make(map[reflect.Type][]Backend),
|
||||||
updaters: subs,
|
updaters: subs,
|
||||||
updates: updates,
|
updates: updates,
|
||||||
@@ -93,14 +98,16 @@ func NewManager(config *Config, backends ...Backend) *Manager {
|
|||||||
|
|
||||||
// Close terminates the account manager's internal notification processes.
|
// Close terminates the account manager's internal notification processes.
|
||||||
func (am *Manager) Close() error {
|
func (am *Manager) Close() error {
|
||||||
for _, w := range am.wallets {
|
|
||||||
w.Close()
|
|
||||||
}
|
|
||||||
errc := make(chan error)
|
errc := make(chan error)
|
||||||
am.quit <- errc
|
am.quit <- errc
|
||||||
return <-errc
|
return <-errc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config returns the configuration of account manager.
|
||||||
|
func (am *Manager) Config() *Config {
|
||||||
|
return am.config
|
||||||
|
}
|
||||||
|
|
||||||
// AddBackend starts the tracking of an additional backend for wallet updates.
|
// AddBackend starts the tracking of an additional backend for wallet updates.
|
||||||
// cmd/geth assumes once this func returns the backends have been already integrated.
|
// cmd/geth assumes once this func returns the backends have been already integrated.
|
||||||
func (am *Manager) AddBackend(backend Backend) {
|
func (am *Manager) AddBackend(backend Backend) {
|
||||||
@@ -250,7 +257,7 @@ func merge(slice []Wallet, wallets ...Wallet) []Wallet {
|
|||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// drop is the counterpart of merge, which looks up wallets from within the sorted
|
// drop is the couterpart of merge, which looks up wallets from within the sorted
|
||||||
// cache and removes the ones specified.
|
// cache and removes the ones specified.
|
||||||
func drop(slice []Wallet, wallets ...Wallet) []Wallet {
|
func drop(slice []Wallet, wallets ...Wallet) []Wallet {
|
||||||
for _, wallet := range wallets {
|
for _, wallet := range wallets {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
## Preparing the smartcard
|
## Preparing the smartcard
|
||||||
|
|
||||||
**WARNING: FOLLOWING THESE INSTRUCTIONS WILL DESTROY THE MASTER KEY ON YOUR CARD. ONLY PROCEED IF NO FUNDS ARE ASSOCIATED WITH THESE ACCOUNTS**
|
**WARNING: FOILLOWING THESE INSTRUCTIONS WILL DESTROY THE MASTER KEY ON YOUR CARD. ONLY PROCEED IF NO FUNDS ARE ASSOCIATED WITH THESE ACCOUNTS**
|
||||||
|
|
||||||
You can use status' [keycard-cli](https://github.com/status-im/keycard-cli) and you should get _at least_ version 2.1.1 of their [smartcard application](https://github.com/status-im/status-keycard/releases/download/2.2.1/keycard_v2.2.1.cap)
|
You can use status' [keycard-cli](https://github.com/status-im/keycard-cli) and you should get _at least_ version 2.1.1 of their [smartcard application](https://github.com/status-im/status-keycard/releases/download/2.2.1/keycard_v2.2.1.cap)
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,6 @@ func (hub *Hub) readPairings() error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer pairingFile.Close()
|
|
||||||
|
|
||||||
pairingData, err := io.ReadAll(pairingFile)
|
pairingData, err := io.ReadAll(pairingFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -242,7 +241,7 @@ func (hub *Hub) refreshWallets() {
|
|||||||
card.Disconnect(pcsc.LeaveCard)
|
card.Disconnect(pcsc.LeaveCard)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Card connected, start tracking among the wallets
|
// Card connected, start tracking in amongs the wallets
|
||||||
hub.wallets[reader] = wallet
|
hub.wallets[reader] = wallet
|
||||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
|
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,10 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
@@ -71,11 +71,11 @@ func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSes
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not unmarshal public key from card: %v", err)
|
return nil, fmt.Errorf("could not unmarshal public key from card: %v", err)
|
||||||
}
|
}
|
||||||
secret, _ := crypto.S256().ScalarMult(cardPublic.X, cardPublic.Y, key.D.Bytes())
|
secret, _ := key.Curve.ScalarMult(cardPublic.X, cardPublic.Y, key.D.Bytes())
|
||||||
return &SecureChannelSession{
|
return &SecureChannelSession{
|
||||||
card: card,
|
card: card,
|
||||||
secret: secret.Bytes(),
|
secret: secret.Bytes(),
|
||||||
publicKey: crypto.FromECDSAPub(&key.PublicKey),
|
publicKey: elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ func (s *SecureChannelSession) Pair(pairingPassword []byte) error {
|
|||||||
// Unpair disestablishes an existing pairing.
|
// Unpair disestablishes an existing pairing.
|
||||||
func (s *SecureChannelSession) Unpair() error {
|
func (s *SecureChannelSession) Unpair() error {
|
||||||
if s.PairingKey == nil {
|
if s.PairingKey == nil {
|
||||||
return errors.New("cannot unpair: not paired")
|
return fmt.Errorf("cannot unpair: not paired")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := s.transmitEncrypted(claSCWallet, insUnpair, s.PairingIndex, 0, []byte{})
|
_, err := s.transmitEncrypted(claSCWallet, insUnpair, s.PairingIndex, 0, []byte{})
|
||||||
@@ -141,7 +141,7 @@ func (s *SecureChannelSession) Unpair() error {
|
|||||||
// Open initializes the secure channel.
|
// Open initializes the secure channel.
|
||||||
func (s *SecureChannelSession) Open() error {
|
func (s *SecureChannelSession) Open() error {
|
||||||
if s.iv != nil {
|
if s.iv != nil {
|
||||||
return errors.New("session already opened")
|
return fmt.Errorf("session already opened")
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := s.open()
|
response, err := s.open()
|
||||||
@@ -215,7 +215,7 @@ func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*responseAPDU, error
|
|||||||
// transmitEncrypted sends an encrypted message, and decrypts and returns the response.
|
// transmitEncrypted sends an encrypted message, and decrypts and returns the response.
|
||||||
func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) {
|
func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) {
|
||||||
if s.iv == nil {
|
if s.iv == nil {
|
||||||
return nil, errors.New("channel not open")
|
return nil, fmt.Errorf("channel not open")
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := s.encryptAPDU(data)
|
data, err := s.encryptAPDU(data)
|
||||||
@@ -254,7 +254,7 @@ func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []b
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !bytes.Equal(s.iv, rmac) {
|
if !bytes.Equal(s.iv, rmac) {
|
||||||
return nil, errors.New("invalid MAC in response")
|
return nil, fmt.Errorf("invalid MAC in response")
|
||||||
}
|
}
|
||||||
|
|
||||||
rapdu := &responseAPDU{}
|
rapdu := &responseAPDU{}
|
||||||
@@ -319,7 +319,7 @@ func unpad(data []byte, terminator byte) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("expected end of padding, got %d", data[len(data)-i])
|
return nil, fmt.Errorf("expected end of padding, got %d", data[len(data)-i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, errors.New("expected end of padding, got 0")
|
return nil, fmt.Errorf("expected end of padding, got 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateIV is an internal method that updates the initialization vector after
|
// updateIV is an internal method that updates the initialization vector after
|
||||||
|
|||||||
@@ -73,14 +73,6 @@ var (
|
|||||||
DerivationSignatureHash = sha256.Sum256(common.Hash{}.Bytes())
|
DerivationSignatureHash = sha256.Sum256(common.Hash{}.Bytes())
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// PinRegexp is the regular expression used to validate PIN codes.
|
|
||||||
pinRegexp = regexp.MustCompile(`^[0-9]{6,}$`)
|
|
||||||
|
|
||||||
// PukRegexp is the regular expression used to validate PUK codes.
|
|
||||||
pukRegexp = regexp.MustCompile(`^[0-9]{12,}$`)
|
|
||||||
)
|
|
||||||
|
|
||||||
// List of APDU command-related constants
|
// List of APDU command-related constants
|
||||||
const (
|
const (
|
||||||
claISO7816 = 0
|
claISO7816 = 0
|
||||||
@@ -107,8 +99,8 @@ const (
|
|||||||
P1DeriveKeyFromCurrent = uint8(0x10)
|
P1DeriveKeyFromCurrent = uint8(0x10)
|
||||||
statusP1WalletStatus = uint8(0x00)
|
statusP1WalletStatus = uint8(0x00)
|
||||||
statusP1Path = uint8(0x01)
|
statusP1Path = uint8(0x01)
|
||||||
signP1PrecomputedHash = uint8(0x00)
|
signP1PrecomputedHash = uint8(0x01)
|
||||||
signP2OnlyBlock = uint8(0x00)
|
signP2OnlyBlock = uint8(0x81)
|
||||||
exportP1Any = uint8(0x00)
|
exportP1Any = uint8(0x00)
|
||||||
exportP2Pubkey = uint8(0x01)
|
exportP2Pubkey = uint8(0x01)
|
||||||
)
|
)
|
||||||
@@ -260,7 +252,7 @@ func (w *Wallet) release() error {
|
|||||||
// with the wallet.
|
// with the wallet.
|
||||||
func (w *Wallet) pair(puk []byte) error {
|
func (w *Wallet) pair(puk []byte) error {
|
||||||
if w.session.paired() {
|
if w.session.paired() {
|
||||||
return errors.New("wallet already paired")
|
return fmt.Errorf("wallet already paired")
|
||||||
}
|
}
|
||||||
pairing, err := w.session.pair(puk)
|
pairing, err := w.session.pair(puk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -388,7 +380,7 @@ func (w *Wallet) Open(passphrase string) error {
|
|||||||
case passphrase == "":
|
case passphrase == "":
|
||||||
return ErrPINUnblockNeeded
|
return ErrPINUnblockNeeded
|
||||||
case status.PinRetryCount > 0:
|
case status.PinRetryCount > 0:
|
||||||
if !pinRegexp.MatchString(passphrase) {
|
if !regexp.MustCompile(`^[0-9]{6,}$`).MatchString(passphrase) {
|
||||||
w.log.Error("PIN needs to be at least 6 digits")
|
w.log.Error("PIN needs to be at least 6 digits")
|
||||||
return ErrPINNeeded
|
return ErrPINNeeded
|
||||||
}
|
}
|
||||||
@@ -396,7 +388,7 @@ func (w *Wallet) Open(passphrase string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if !pukRegexp.MatchString(passphrase) {
|
if !regexp.MustCompile(`^[0-9]{12,}$`).MatchString(passphrase) {
|
||||||
w.log.Error("PUK needs to be at least 12 digits")
|
w.log.Error("PUK needs to be at least 12 digits")
|
||||||
return ErrPINUnblockNeeded
|
return ErrPINUnblockNeeded
|
||||||
}
|
}
|
||||||
@@ -784,16 +776,16 @@ func (w *Wallet) findAccountPath(account accounts.Account) (accounts.DerivationP
|
|||||||
return nil, fmt.Errorf("scheme %s does not match wallet scheme %s", account.URL.Scheme, w.Hub.scheme)
|
return nil, fmt.Errorf("scheme %s does not match wallet scheme %s", account.URL.Scheme, w.Hub.scheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
url, path, found := strings.Cut(account.URL.Path, "/")
|
parts := strings.SplitN(account.URL.Path, "/", 2)
|
||||||
if !found {
|
if len(parts) != 2 {
|
||||||
return nil, fmt.Errorf("invalid URL format: %s", account.URL)
|
return nil, fmt.Errorf("invalid URL format: %s", account.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
if url != fmt.Sprintf("%x", w.PublicKey[1:3]) {
|
if parts[0] != fmt.Sprintf("%x", w.PublicKey[1:3]) {
|
||||||
return nil, fmt.Errorf("URL %s is not for this wallet", account.URL)
|
return nil, fmt.Errorf("URL %s is not for this wallet", account.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
return accounts.ParseDerivationPath(path)
|
return accounts.ParseDerivationPath(parts[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Session represents a secured communication session with the wallet.
|
// Session represents a secured communication session with the wallet.
|
||||||
@@ -821,7 +813,7 @@ func (s *Session) pair(secret []byte) (smartcardPairing, error) {
|
|||||||
// unpair deletes an existing pairing.
|
// unpair deletes an existing pairing.
|
||||||
func (s *Session) unpair() error {
|
func (s *Session) unpair() error {
|
||||||
if !s.verified {
|
if !s.verified {
|
||||||
return errors.New("unpair requires that the PIN be verified")
|
return fmt.Errorf("unpair requires that the PIN be verified")
|
||||||
}
|
}
|
||||||
return s.Channel.Unpair()
|
return s.Channel.Unpair()
|
||||||
}
|
}
|
||||||
@@ -887,7 +879,6 @@ func (s *Session) walletStatus() (*walletStatus, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// derivationPath fetches the wallet's current derivation path from the card.
|
// derivationPath fetches the wallet's current derivation path from the card.
|
||||||
//
|
|
||||||
//lint:ignore U1000 needs to be added to the console interface
|
//lint:ignore U1000 needs to be added to the console interface
|
||||||
func (s *Session) derivationPath() (accounts.DerivationPath, error) {
|
func (s *Session) derivationPath() (accounts.DerivationPath, error) {
|
||||||
response, err := s.Channel.transmitEncrypted(claSCWallet, insStatus, statusP1Path, 0, nil)
|
response, err := s.Channel.transmitEncrypted(claSCWallet, insStatus, statusP1Path, 0, nil)
|
||||||
@@ -915,7 +906,7 @@ func (s *Session) initialize(seed []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if status == "Online" {
|
if status == "Online" {
|
||||||
return errors.New("card is already initialized, cowardly refusing to proceed")
|
return fmt.Errorf("card is already initialized, cowardly refusing to proceed")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Wallet.lock.Lock()
|
s.Wallet.lock.Lock()
|
||||||
@@ -1003,7 +994,6 @@ func (s *Session) derive(path accounts.DerivationPath) (accounts.Account, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keyExport contains information on an exported keypair.
|
// keyExport contains information on an exported keypair.
|
||||||
//
|
|
||||||
//lint:ignore U1000 needs to be added to the console interface
|
//lint:ignore U1000 needs to be added to the console interface
|
||||||
type keyExport struct {
|
type keyExport struct {
|
||||||
PublicKey []byte `asn1:"tag:0"`
|
PublicKey []byte `asn1:"tag:0"`
|
||||||
@@ -1011,7 +1001,6 @@ type keyExport struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// publicKey returns the public key for the current derivation path.
|
// publicKey returns the public key for the current derivation path.
|
||||||
//
|
|
||||||
//lint:ignore U1000 needs to be added to the console interface
|
//lint:ignore U1000 needs to be added to the console interface
|
||||||
func (s *Session) publicKey() ([]byte, error) {
|
func (s *Session) publicKey() ([]byte, error) {
|
||||||
response, err := s.Channel.transmitEncrypted(claSCWallet, insExportKey, exportP1Any, exportP2Pubkey, nil)
|
response, err := s.Channel.transmitEncrypted(claSCWallet, insExportKey, exportP1Any, exportP2Pubkey, nil)
|
||||||
|
|||||||
@@ -92,9 +92,10 @@ func (u *URL) UnmarshalJSON(input []byte) error {
|
|||||||
|
|
||||||
// Cmp compares x and y and returns:
|
// Cmp compares x and y and returns:
|
||||||
//
|
//
|
||||||
// -1 if x < y
|
// -1 if x < y
|
||||||
// 0 if x == y
|
// 0 if x == y
|
||||||
// +1 if x > y
|
// +1 if x > y
|
||||||
|
//
|
||||||
func (u URL) Cmp(url URL) int {
|
func (u URL) Cmp(url URL) int {
|
||||||
if u.Scheme == url.Scheme {
|
if u.Scheme == url.Scheme {
|
||||||
return strings.Compare(u.Path, url.Path)
|
return strings.Compare(u.Path, url.Path)
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestURLParsing(t *testing.T) {
|
func TestURLParsing(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
url, err := parseURL("https://ethereum.org")
|
url, err := parseURL("https://ethereum.org")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@@ -41,7 +40,6 @@ func TestURLParsing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestURLString(t *testing.T) {
|
func TestURLString(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
url := URL{Scheme: "https", Path: "ethereum.org"}
|
url := URL{Scheme: "https", Path: "ethereum.org"}
|
||||||
if url.String() != "https://ethereum.org" {
|
if url.String() != "https://ethereum.org" {
|
||||||
t.Errorf("expected: %v, got: %v", "https://ethereum.org", url.String())
|
t.Errorf("expected: %v, got: %v", "https://ethereum.org", url.String())
|
||||||
@@ -54,11 +52,10 @@ func TestURLString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestURLMarshalJSON(t *testing.T) {
|
func TestURLMarshalJSON(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
url := URL{Scheme: "https", Path: "ethereum.org"}
|
url := URL{Scheme: "https", Path: "ethereum.org"}
|
||||||
json, err := url.MarshalJSON()
|
json, err := url.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpcted error: %v", err)
|
||||||
}
|
}
|
||||||
if string(json) != "\"https://ethereum.org\"" {
|
if string(json) != "\"https://ethereum.org\"" {
|
||||||
t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\"", string(json))
|
t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\"", string(json))
|
||||||
@@ -66,11 +63,10 @@ func TestURLMarshalJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestURLUnmarshalJSON(t *testing.T) {
|
func TestURLUnmarshalJSON(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
url := &URL{}
|
url := &URL{}
|
||||||
err := url.UnmarshalJSON([]byte("\"https://ethereum.org\""))
|
err := url.UnmarshalJSON([]byte("\"https://ethereum.org\""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpcted error: %v", err)
|
||||||
}
|
}
|
||||||
if url.Scheme != "https" {
|
if url.Scheme != "https" {
|
||||||
t.Errorf("expected: %v, got: %v", "https", url.Scheme)
|
t.Errorf("expected: %v, got: %v", "https", url.Scheme)
|
||||||
@@ -81,7 +77,6 @@ func TestURLUnmarshalJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestURLComparison(t *testing.T) {
|
func TestURLComparison(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
urlA URL
|
urlA URL
|
||||||
urlB URL
|
urlB URL
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/accounts"
|
"github.com/ethereum/go-ethereum/accounts"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/karalabe/hid"
|
"github.com/karalabe/usb"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LedgerScheme is the protocol scheme prefixing account and wallet URLs.
|
// LedgerScheme is the protocol scheme prefixing account and wallet URLs.
|
||||||
@@ -63,36 +63,26 @@ type Hub struct {
|
|||||||
stateLock sync.RWMutex // Protects the internals of the hub from racey access
|
stateLock sync.RWMutex // Protects the internals of the hub from racey access
|
||||||
|
|
||||||
// TODO(karalabe): remove if hotplug lands on Windows
|
// TODO(karalabe): remove if hotplug lands on Windows
|
||||||
commsPend int // Number of operations blocking enumeration
|
commsPend int // Number of operations blocking enumeration
|
||||||
commsLock sync.Mutex // Lock protecting the pending counter and enumeration
|
commsLock sync.Mutex // Lock protecting the pending counter and enumeration
|
||||||
enumFails atomic.Uint32 // Number of times enumeration has failed
|
enumFails uint32 // Number of times enumeration has failed
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLedgerHub creates a new hardware wallet manager for Ledger devices.
|
// NewLedgerHub creates a new hardware wallet manager for Ledger devices.
|
||||||
func NewLedgerHub() (*Hub, error) {
|
func NewLedgerHub() (*Hub, error) {
|
||||||
return newHub(LedgerScheme, 0x2c97, []uint16{
|
return newHub(LedgerScheme, 0x2c97, []uint16{
|
||||||
|
|
||||||
// Device definitions taken from
|
|
||||||
// https://github.com/LedgerHQ/ledger-live/blob/38012bc8899e0f07149ea9cfe7e64b2c146bc92b/libs/ledgerjs/packages/devices/src/index.ts
|
|
||||||
|
|
||||||
// Original product IDs
|
// Original product IDs
|
||||||
0x0000, /* Ledger Blue */
|
0x0000, /* Ledger Blue */
|
||||||
0x0001, /* Ledger Nano S */
|
0x0001, /* Ledger Nano S */
|
||||||
0x0004, /* Ledger Nano X */
|
0x0004, /* Ledger Nano X */
|
||||||
0x0005, /* Ledger Nano S Plus */
|
|
||||||
0x0006, /* Ledger Nano FTS */
|
|
||||||
|
|
||||||
|
// Upcoming product IDs: https://www.ledger.com/2019/05/17/windows-10-update-sunsetting-u2f-tunnel-transport-for-ledger-devices/
|
||||||
0x0015, /* HID + U2F + WebUSB Ledger Blue */
|
0x0015, /* HID + U2F + WebUSB Ledger Blue */
|
||||||
0x1015, /* HID + U2F + WebUSB Ledger Nano S */
|
0x1015, /* HID + U2F + WebUSB Ledger Nano S */
|
||||||
0x4015, /* HID + U2F + WebUSB Ledger Nano X */
|
0x4015, /* HID + U2F + WebUSB Ledger Nano X */
|
||||||
0x5015, /* HID + U2F + WebUSB Ledger Nano S Plus */
|
|
||||||
0x6015, /* HID + U2F + WebUSB Ledger Nano FTS */
|
|
||||||
|
|
||||||
0x0011, /* HID + WebUSB Ledger Blue */
|
0x0011, /* HID + WebUSB Ledger Blue */
|
||||||
0x1011, /* HID + WebUSB Ledger Nano S */
|
0x1011, /* HID + WebUSB Ledger Nano S */
|
||||||
0x4011, /* HID + WebUSB Ledger Nano X */
|
0x4011, /* HID + WebUSB Ledger Nano X */
|
||||||
0x5011, /* HID + WebUSB Ledger Nano S Plus */
|
|
||||||
0x6011, /* HID + WebUSB Ledger Nano FTS */
|
|
||||||
}, 0xffa0, 0, newLedgerDriver)
|
}, 0xffa0, 0, newLedgerDriver)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +99,7 @@ func NewTrezorHubWithWebUSB() (*Hub, error) {
|
|||||||
|
|
||||||
// newHub creates a new hardware wallet manager for generic USB devices.
|
// newHub creates a new hardware wallet manager for generic USB devices.
|
||||||
func newHub(scheme string, vendorID uint16, productIDs []uint16, usageID uint16, endpointID int, makeDriver func(log.Logger) driver) (*Hub, error) {
|
func newHub(scheme string, vendorID uint16, productIDs []uint16, usageID uint16, endpointID int, makeDriver func(log.Logger) driver) (*Hub, error) {
|
||||||
if !hid.Supported() {
|
if !usb.Supported() {
|
||||||
return nil, errors.New("unsupported platform")
|
return nil, errors.New("unsupported platform")
|
||||||
}
|
}
|
||||||
hub := &Hub{
|
hub := &Hub{
|
||||||
@@ -151,11 +141,11 @@ func (hub *Hub) refreshWallets() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// If USB enumeration is continually failing, don't keep trying indefinitely
|
// If USB enumeration is continually failing, don't keep trying indefinitely
|
||||||
if hub.enumFails.Load() > 2 {
|
if atomic.LoadUint32(&hub.enumFails) > 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Retrieve the current list of USB wallet devices
|
// Retrieve the current list of USB wallet devices
|
||||||
var devices []hid.DeviceInfo
|
var devices []usb.DeviceInfo
|
||||||
|
|
||||||
if runtime.GOOS == "linux" {
|
if runtime.GOOS == "linux" {
|
||||||
// hidapi on Linux opens the device during enumeration to retrieve some infos,
|
// hidapi on Linux opens the device during enumeration to retrieve some infos,
|
||||||
@@ -170,9 +160,9 @@ func (hub *Hub) refreshWallets() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
infos, err := hid.Enumerate(hub.vendorID, 0)
|
infos, err := usb.Enumerate(hub.vendorID, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
failcount := hub.enumFails.Add(1)
|
failcount := atomic.AddUint32(&hub.enumFails, 1)
|
||||||
if runtime.GOOS == "linux" {
|
if runtime.GOOS == "linux" {
|
||||||
// See rationale before the enumeration why this is needed and only on Linux.
|
// See rationale before the enumeration why this is needed and only on Linux.
|
||||||
hub.commsLock.Unlock()
|
hub.commsLock.Unlock()
|
||||||
@@ -181,7 +171,7 @@ func (hub *Hub) refreshWallets() {
|
|||||||
"vendor", hub.vendorID, "failcount", failcount, "err", err)
|
"vendor", hub.vendorID, "failcount", failcount, "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hub.enumFails.Store(0)
|
atomic.StoreUint32(&hub.enumFails, 0)
|
||||||
|
|
||||||
for _, info := range infos {
|
for _, info := range infos {
|
||||||
for _, id := range hub.productIDs {
|
for _, id := range hub.productIDs {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
// This file contains the implementation for interacting with the Ledger hardware
|
// This file contains the implementation for interacting with the Ledger hardware
|
||||||
// wallets. The wire protocol spec can be found in the Ledger Blue GitHub repo:
|
// wallets. The wire protocol spec can be found in the Ledger Blue GitHub repo:
|
||||||
// https://github.com/LedgerHQ/app-ethereum/blob/develop/doc/ethapp.adoc
|
// https://raw.githubusercontent.com/LedgerHQ/blue-app-eth/master/doc/ethapp.asc
|
||||||
|
|
||||||
package usbwallet
|
package usbwallet
|
||||||
|
|
||||||
@@ -59,8 +59,6 @@ const (
|
|||||||
ledgerP1InitTransactionData ledgerParam1 = 0x00 // First transaction data block for signing
|
ledgerP1InitTransactionData ledgerParam1 = 0x00 // First transaction data block for signing
|
||||||
ledgerP1ContTransactionData ledgerParam1 = 0x80 // Subsequent transaction data block for signing
|
ledgerP1ContTransactionData ledgerParam1 = 0x80 // Subsequent transaction data block for signing
|
||||||
ledgerP2DiscardAddressChainCode ledgerParam2 = 0x00 // Do not return the chain code along with the address
|
ledgerP2DiscardAddressChainCode ledgerParam2 = 0x00 // Do not return the chain code along with the address
|
||||||
|
|
||||||
ledgerEip155Size int = 3 // Size of the EIP-155 chain_id,r,s in unsigned transactions
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// errLedgerReplyInvalidHeader is the error message returned by a Ledger data exchange
|
// errLedgerReplyInvalidHeader is the error message returned by a Ledger data exchange
|
||||||
@@ -197,18 +195,18 @@ func (w *ledgerDriver) SignTypedMessage(path accounts.DerivationPath, domainHash
|
|||||||
//
|
//
|
||||||
// The version retrieval protocol is defined as follows:
|
// The version retrieval protocol is defined as follows:
|
||||||
//
|
//
|
||||||
// CLA | INS | P1 | P2 | Lc | Le
|
// CLA | INS | P1 | P2 | Lc | Le
|
||||||
// ----+-----+----+----+----+---
|
// ----+-----+----+----+----+---
|
||||||
// E0 | 06 | 00 | 00 | 00 | 04
|
// E0 | 06 | 00 | 00 | 00 | 04
|
||||||
//
|
//
|
||||||
// With no input data, and the output data being:
|
// With no input data, and the output data being:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// ---------------------------------------------------+--------
|
// ---------------------------------------------------+--------
|
||||||
// Flags 01: arbitrary data signature enabled by user | 1 byte
|
// Flags 01: arbitrary data signature enabled by user | 1 byte
|
||||||
// Application major version | 1 byte
|
// Application major version | 1 byte
|
||||||
// Application minor version | 1 byte
|
// Application minor version | 1 byte
|
||||||
// Application patch version | 1 byte
|
// Application patch version | 1 byte
|
||||||
func (w *ledgerDriver) ledgerVersion() ([3]byte, error) {
|
func (w *ledgerDriver) ledgerVersion() ([3]byte, error) {
|
||||||
// Send the request and wait for the response
|
// Send the request and wait for the response
|
||||||
reply, err := w.ledgerExchange(ledgerOpGetConfiguration, 0, 0, nil)
|
reply, err := w.ledgerExchange(ledgerOpGetConfiguration, 0, 0, nil)
|
||||||
@@ -229,32 +227,32 @@ func (w *ledgerDriver) ledgerVersion() ([3]byte, error) {
|
|||||||
//
|
//
|
||||||
// The address derivation protocol is defined as follows:
|
// The address derivation protocol is defined as follows:
|
||||||
//
|
//
|
||||||
// CLA | INS | P1 | P2 | Lc | Le
|
// CLA | INS | P1 | P2 | Lc | Le
|
||||||
// ----+-----+----+----+-----+---
|
// ----+-----+----+----+-----+---
|
||||||
// E0 | 02 | 00 return address
|
// E0 | 02 | 00 return address
|
||||||
// 01 display address and confirm before returning
|
// 01 display address and confirm before returning
|
||||||
// | 00: do not return the chain code
|
// | 00: do not return the chain code
|
||||||
// | 01: return the chain code
|
// | 01: return the chain code
|
||||||
// | var | 00
|
// | var | 00
|
||||||
//
|
//
|
||||||
// Where the input data is:
|
// Where the input data is:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// -------------------------------------------------+--------
|
// -------------------------------------------------+--------
|
||||||
// Number of BIP 32 derivations to perform (max 10) | 1 byte
|
// Number of BIP 32 derivations to perform (max 10) | 1 byte
|
||||||
// First derivation index (big endian) | 4 bytes
|
// First derivation index (big endian) | 4 bytes
|
||||||
// ... | 4 bytes
|
// ... | 4 bytes
|
||||||
// Last derivation index (big endian) | 4 bytes
|
// Last derivation index (big endian) | 4 bytes
|
||||||
//
|
//
|
||||||
// And the output data is:
|
// And the output data is:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// ------------------------+-------------------
|
// ------------------------+-------------------
|
||||||
// Public Key length | 1 byte
|
// Public Key length | 1 byte
|
||||||
// Uncompressed Public Key | arbitrary
|
// Uncompressed Public Key | arbitrary
|
||||||
// Ethereum address length | 1 byte
|
// Ethereum address length | 1 byte
|
||||||
// Ethereum address | 40 bytes hex ascii
|
// Ethereum address | 40 bytes hex ascii
|
||||||
// Chain code if requested | 32 bytes
|
// Chain code if requested | 32 bytes
|
||||||
func (w *ledgerDriver) ledgerDerive(derivationPath []uint32) (common.Address, error) {
|
func (w *ledgerDriver) ledgerDerive(derivationPath []uint32) (common.Address, error) {
|
||||||
// Flatten the derivation path into the Ledger request
|
// Flatten the derivation path into the Ledger request
|
||||||
path := make([]byte, 1+4*len(derivationPath))
|
path := make([]byte, 1+4*len(derivationPath))
|
||||||
@@ -279,7 +277,7 @@ func (w *ledgerDriver) ledgerDerive(derivationPath []uint32) (common.Address, er
|
|||||||
}
|
}
|
||||||
hexstr := reply[1 : 1+int(reply[0])]
|
hexstr := reply[1 : 1+int(reply[0])]
|
||||||
|
|
||||||
// Decode the hex string into an Ethereum address and return
|
// Decode the hex sting into an Ethereum address and return
|
||||||
var address common.Address
|
var address common.Address
|
||||||
if _, err = hex.Decode(address[:], hexstr); err != nil {
|
if _, err = hex.Decode(address[:], hexstr); err != nil {
|
||||||
return common.Address{}, err
|
return common.Address{}, err
|
||||||
@@ -292,35 +290,35 @@ func (w *ledgerDriver) ledgerDerive(derivationPath []uint32) (common.Address, er
|
|||||||
//
|
//
|
||||||
// The transaction signing protocol is defined as follows:
|
// The transaction signing protocol is defined as follows:
|
||||||
//
|
//
|
||||||
// CLA | INS | P1 | P2 | Lc | Le
|
// CLA | INS | P1 | P2 | Lc | Le
|
||||||
// ----+-----+----+----+-----+---
|
// ----+-----+----+----+-----+---
|
||||||
// E0 | 04 | 00: first transaction data block
|
// E0 | 04 | 00: first transaction data block
|
||||||
// 80: subsequent transaction data block
|
// 80: subsequent transaction data block
|
||||||
// | 00 | variable | variable
|
// | 00 | variable | variable
|
||||||
//
|
//
|
||||||
// Where the input for the first transaction block (first 255 bytes) is:
|
// Where the input for the first transaction block (first 255 bytes) is:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// -------------------------------------------------+----------
|
// -------------------------------------------------+----------
|
||||||
// Number of BIP 32 derivations to perform (max 10) | 1 byte
|
// Number of BIP 32 derivations to perform (max 10) | 1 byte
|
||||||
// First derivation index (big endian) | 4 bytes
|
// First derivation index (big endian) | 4 bytes
|
||||||
// ... | 4 bytes
|
// ... | 4 bytes
|
||||||
// Last derivation index (big endian) | 4 bytes
|
// Last derivation index (big endian) | 4 bytes
|
||||||
// RLP transaction chunk | arbitrary
|
// RLP transaction chunk | arbitrary
|
||||||
//
|
//
|
||||||
// And the input for subsequent transaction blocks (first 255 bytes) are:
|
// And the input for subsequent transaction blocks (first 255 bytes) are:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// ----------------------+----------
|
// ----------------------+----------
|
||||||
// RLP transaction chunk | arbitrary
|
// RLP transaction chunk | arbitrary
|
||||||
//
|
//
|
||||||
// And the output data is:
|
// And the output data is:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// ------------+---------
|
// ------------+---------
|
||||||
// signature V | 1 byte
|
// signature V | 1 byte
|
||||||
// signature R | 32 bytes
|
// signature R | 32 bytes
|
||||||
// signature S | 32 bytes
|
// signature S | 32 bytes
|
||||||
func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
|
func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
|
||||||
// Flatten the derivation path into the Ledger request
|
// Flatten the derivation path into the Ledger request
|
||||||
path := make([]byte, 1+4*len(derivationPath))
|
path := make([]byte, 1+4*len(derivationPath))
|
||||||
@@ -338,22 +336,8 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
|
|||||||
return common.Address{}, nil, err
|
return common.Address{}, nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if tx.Type() == types.DynamicFeeTxType {
|
if txrlp, err = rlp.EncodeToBytes([]interface{}{tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), chainID, big.NewInt(0), big.NewInt(0)}); err != nil {
|
||||||
if txrlp, err = rlp.EncodeToBytes([]interface{}{chainID, tx.Nonce(), tx.GasTipCap(), tx.GasFeeCap(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), tx.AccessList()}); err != nil {
|
return common.Address{}, nil, err
|
||||||
return common.Address{}, nil, err
|
|
||||||
}
|
|
||||||
// append type to transaction
|
|
||||||
txrlp = append([]byte{tx.Type()}, txrlp...)
|
|
||||||
} else if tx.Type() == types.AccessListTxType {
|
|
||||||
if txrlp, err = rlp.EncodeToBytes([]interface{}{chainID, tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), tx.AccessList()}); err != nil {
|
|
||||||
return common.Address{}, nil, err
|
|
||||||
}
|
|
||||||
// append type to transaction
|
|
||||||
txrlp = append([]byte{tx.Type()}, txrlp...)
|
|
||||||
} else if tx.Type() == types.LegacyTxType {
|
|
||||||
if txrlp, err = rlp.EncodeToBytes([]interface{}{tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), chainID, big.NewInt(0), big.NewInt(0)}); err != nil {
|
|
||||||
return common.Address{}, nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
payload := append(path, txrlp...)
|
payload := append(path, txrlp...)
|
||||||
@@ -363,17 +347,9 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
|
|||||||
op = ledgerP1InitTransactionData
|
op = ledgerP1InitTransactionData
|
||||||
reply []byte
|
reply []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
// Chunk size selection to mitigate an underlying RLP deserialization issue on the ledger app.
|
|
||||||
// https://github.com/LedgerHQ/app-ethereum/issues/409
|
|
||||||
chunk := 255
|
|
||||||
if tx.Type() == types.LegacyTxType {
|
|
||||||
for ; len(payload)%chunk <= ledgerEip155Size; chunk-- {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for len(payload) > 0 {
|
for len(payload) > 0 {
|
||||||
// Calculate the size of the next data chunk
|
// Calculate the size of the next data chunk
|
||||||
|
chunk := 255
|
||||||
if chunk > len(payload) {
|
if chunk > len(payload) {
|
||||||
chunk = len(payload)
|
chunk = len(payload)
|
||||||
}
|
}
|
||||||
@@ -397,11 +373,8 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
|
|||||||
if chainID == nil {
|
if chainID == nil {
|
||||||
signer = new(types.HomesteadSigner)
|
signer = new(types.HomesteadSigner)
|
||||||
} else {
|
} else {
|
||||||
signer = types.LatestSignerForChainID(chainID)
|
signer = types.NewEIP155Signer(chainID)
|
||||||
// For non-legacy transactions, V is 0 or 1, no need to subtract here.
|
signature[64] -= byte(chainID.Uint64()*2 + 35)
|
||||||
if tx.Type() == types.LegacyTxType {
|
|
||||||
signature[64] -= byte(chainID.Uint64()*2 + 35)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
signed, err := tx.WithSignature(signer, signature)
|
signed, err := tx.WithSignature(signer, signature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -419,28 +392,30 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
|
|||||||
//
|
//
|
||||||
// The signing protocol is defined as follows:
|
// The signing protocol is defined as follows:
|
||||||
//
|
//
|
||||||
// CLA | INS | P1 | P2 | Lc | Le
|
// CLA | INS | P1 | P2 | Lc | Le
|
||||||
// ----+-----+----+-----------------------------+-----+---
|
// ----+-----+----+-----------------------------+-----+---
|
||||||
// E0 | 0C | 00 | implementation version : 00 | variable | variable
|
// E0 | 0C | 00 | implementation version : 00 | variable | variable
|
||||||
//
|
//
|
||||||
// Where the input is:
|
// Where the input is:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// -------------------------------------------------+----------
|
// -------------------------------------------------+----------
|
||||||
// Number of BIP 32 derivations to perform (max 10) | 1 byte
|
// Number of BIP 32 derivations to perform (max 10) | 1 byte
|
||||||
// First derivation index (big endian) | 4 bytes
|
// First derivation index (big endian) | 4 bytes
|
||||||
// ... | 4 bytes
|
// ... | 4 bytes
|
||||||
// Last derivation index (big endian) | 4 bytes
|
// Last derivation index (big endian) | 4 bytes
|
||||||
// domain hash | 32 bytes
|
// domain hash | 32 bytes
|
||||||
// message hash | 32 bytes
|
// message hash | 32 bytes
|
||||||
|
//
|
||||||
|
//
|
||||||
//
|
//
|
||||||
// And the output data is:
|
// And the output data is:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// ------------+---------
|
// ------------+---------
|
||||||
// signature V | 1 byte
|
// signature V | 1 byte
|
||||||
// signature R | 32 bytes
|
// signature R | 32 bytes
|
||||||
// signature S | 32 bytes
|
// signature S | 32 bytes
|
||||||
func (w *ledgerDriver) ledgerSignTypedMessage(derivationPath []uint32, domainHash []byte, messageHash []byte) ([]byte, error) {
|
func (w *ledgerDriver) ledgerSignTypedMessage(derivationPath []uint32, domainHash []byte, messageHash []byte) ([]byte, error) {
|
||||||
// Flatten the derivation path into the Ledger request
|
// Flatten the derivation path into the Ledger request
|
||||||
path := make([]byte, 1+4*len(derivationPath))
|
path := make([]byte, 1+4*len(derivationPath))
|
||||||
@@ -479,12 +454,12 @@ func (w *ledgerDriver) ledgerSignTypedMessage(derivationPath []uint32, domainHas
|
|||||||
//
|
//
|
||||||
// The common transport header is defined as follows:
|
// The common transport header is defined as follows:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// --------------------------------------+----------
|
// --------------------------------------+----------
|
||||||
// Communication channel ID (big endian) | 2 bytes
|
// Communication channel ID (big endian) | 2 bytes
|
||||||
// Command tag | 1 byte
|
// Command tag | 1 byte
|
||||||
// Packet sequence index (big endian) | 2 bytes
|
// Packet sequence index (big endian) | 2 bytes
|
||||||
// Payload | arbitrary
|
// Payload | arbitrary
|
||||||
//
|
//
|
||||||
// The Communication channel ID allows commands multiplexing over the same
|
// The Communication channel ID allows commands multiplexing over the same
|
||||||
// physical link. It is not used for the time being, and should be set to 0101
|
// physical link. It is not used for the time being, and should be set to 0101
|
||||||
@@ -498,15 +473,15 @@ func (w *ledgerDriver) ledgerSignTypedMessage(derivationPath []uint32, domainHas
|
|||||||
//
|
//
|
||||||
// APDU Command payloads are encoded as follows:
|
// APDU Command payloads are encoded as follows:
|
||||||
//
|
//
|
||||||
// Description | Length
|
// Description | Length
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// APDU length (big endian) | 2 bytes
|
// APDU length (big endian) | 2 bytes
|
||||||
// APDU CLA | 1 byte
|
// APDU CLA | 1 byte
|
||||||
// APDU INS | 1 byte
|
// APDU INS | 1 byte
|
||||||
// APDU P1 | 1 byte
|
// APDU P1 | 1 byte
|
||||||
// APDU P2 | 1 byte
|
// APDU P2 | 1 byte
|
||||||
// APDU length | 1 byte
|
// APDU length | 1 byte
|
||||||
// Optional APDU data | arbitrary
|
// Optional APDU data | arbitrary
|
||||||
func (w *ledgerDriver) ledgerExchange(opcode ledgerOpcode, p1 ledgerParam1, p2 ledgerParam2, data []byte) ([]byte, error) {
|
func (w *ledgerDriver) ledgerExchange(opcode ledgerOpcode, p1 ledgerParam1, p2 ledgerParam2, data []byte) ([]byte, error) {
|
||||||
// Construct the message payload, possibly split into multiple chunks
|
// Construct the message payload, possibly split into multiple chunks
|
||||||
apdu := make([]byte, 2, 7+len(data))
|
apdu := make([]byte, 2, 7+len(data))
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"google.golang.org/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrTrezorPINNeeded is returned if opening the trezor requires a PIN code. In
|
// ErrTrezorPINNeeded is returned if opening the trezor requires a PIN code. In
|
||||||
@@ -84,15 +84,15 @@ func (w *trezorDriver) Status() (string, error) {
|
|||||||
|
|
||||||
// Open implements usbwallet.driver, attempting to initialize the connection to
|
// Open implements usbwallet.driver, attempting to initialize the connection to
|
||||||
// the Trezor hardware wallet. Initializing the Trezor is a two or three phase operation:
|
// the Trezor hardware wallet. Initializing the Trezor is a two or three phase operation:
|
||||||
// - The first phase is to initialize the connection and read the wallet's
|
// * The first phase is to initialize the connection and read the wallet's
|
||||||
// features. This phase is invoked if the provided passphrase is empty. The
|
// features. This phase is invoked if the provided passphrase is empty. The
|
||||||
// device will display the pinpad as a result and will return an appropriate
|
// device will display the pinpad as a result and will return an appropriate
|
||||||
// error to notify the user that a second open phase is needed.
|
// error to notify the user that a second open phase is needed.
|
||||||
// - The second phase is to unlock access to the Trezor, which is done by the
|
// * The second phase is to unlock access to the Trezor, which is done by the
|
||||||
// user actually providing a passphrase mapping a keyboard keypad to the pin
|
// user actually providing a passphrase mapping a keyboard keypad to the pin
|
||||||
// number of the user (shuffled according to the pinpad displayed).
|
// number of the user (shuffled according to the pinpad displayed).
|
||||||
// - If needed the device will ask for passphrase which will require calling
|
// * If needed the device will ask for passphrase which will require calling
|
||||||
// open again with the actual passphrase (3rd phase)
|
// open again with the actual passphrase (3rd phase)
|
||||||
func (w *trezorDriver) Open(device io.ReadWriter, passphrase string) error {
|
func (w *trezorDriver) Open(device io.ReadWriter, passphrase string) error {
|
||||||
w.device, w.failure = device, nil
|
w.device, w.failure = device, nil
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,6 @@
|
|||||||
syntax = "proto2";
|
syntax = "proto2";
|
||||||
package hw.trezor.messages.common;
|
package hw.trezor.messages.common;
|
||||||
|
|
||||||
option go_package = "github.com/ethereum/go-ethereum/accounts/usbwallet/trezor";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response: Success of the previous request
|
* Response: Success of the previous request
|
||||||
* @end
|
* @end
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,6 @@
|
|||||||
syntax = "proto2";
|
syntax = "proto2";
|
||||||
package hw.trezor.messages.ethereum;
|
package hw.trezor.messages.ethereum;
|
||||||
|
|
||||||
option go_package = "github.com/ethereum/go-ethereum/accounts/usbwallet/trezor";
|
|
||||||
|
|
||||||
// Sugar for easier handling in Java
|
// Sugar for easier handling in Java
|
||||||
option java_package = "com.satoshilabs.trezor.lib.protobuf";
|
option java_package = "com.satoshilabs.trezor.lib.protobuf";
|
||||||
option java_outer_classname = "TrezorMessageEthereum";
|
option java_outer_classname = "TrezorMessageEthereum";
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,6 @@
|
|||||||
syntax = "proto2";
|
syntax = "proto2";
|
||||||
package hw.trezor.messages.management;
|
package hw.trezor.messages.management;
|
||||||
|
|
||||||
option go_package = "github.com/ethereum/go-ethereum/accounts/usbwallet/trezor";
|
|
||||||
|
|
||||||
// Sugar for easier handling in Java
|
// Sugar for easier handling in Java
|
||||||
option java_package = "com.satoshilabs.trezor.lib.protobuf";
|
option java_package = "com.satoshilabs.trezor.lib.protobuf";
|
||||||
option java_outer_classname = "TrezorMessageManagement";
|
option java_outer_classname = "TrezorMessageManagement";
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -9,13 +9,10 @@ package hw.trezor.messages;
|
|||||||
* Messages for TREZOR communication
|
* Messages for TREZOR communication
|
||||||
*/
|
*/
|
||||||
|
|
||||||
option go_package = "github.com/ethereum/go-ethereum/accounts/usbwallet/trezor";
|
|
||||||
|
|
||||||
// Sugar for easier handling in Java
|
// Sugar for easier handling in Java
|
||||||
option java_package = "com.satoshilabs.trezor.lib.protobuf";
|
option java_package = "com.satoshilabs.trezor.lib.protobuf";
|
||||||
option java_outer_classname = "TrezorMessage";
|
option java_outer_classname = "TrezorMessage";
|
||||||
|
|
||||||
|
|
||||||
import "google/protobuf/descriptor.proto";
|
import "google/protobuf/descriptor.proto";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
// This file contains the implementation for interacting with the Trezor hardware
|
// This file contains the implementation for interacting with the Trezor hardware
|
||||||
// wallets. The wire protocol spec can be found on the SatoshiLabs website:
|
// wallets. The wire protocol spec can be found on the SatoshiLabs website:
|
||||||
// https://docs.trezor.io/trezor-firmware/common/message-workflows.html
|
// https://wiki.trezor.io/Developers_guide-Message_Workflows
|
||||||
|
|
||||||
// !!! STAHP !!!
|
// !!! STAHP !!!
|
||||||
//
|
//
|
||||||
@@ -39,10 +39,10 @@
|
|||||||
// - Download the latest protoc https://github.com/protocolbuffers/protobuf/releases
|
// - Download the latest protoc https://github.com/protocolbuffers/protobuf/releases
|
||||||
// - Build with the usual `./configure && make` and ensure it's on your $PATH
|
// - Build with the usual `./configure && make` and ensure it's on your $PATH
|
||||||
// - Delete all the .proto and .pb.go files, pull in fresh ones from Trezor
|
// - Delete all the .proto and .pb.go files, pull in fresh ones from Trezor
|
||||||
// - Grab the latest Go plugin `go get -u google.golang.org/protobuf/cmd/protoc-gen-go`
|
// - Grab the latest Go plugin `go get -u github.com/golang/protobuf/protoc-gen-go`
|
||||||
// - Vendor in the latest Go plugin `govendor fetch google.golang.org/protobuf/...`
|
// - Vendor in the latest Go plugin `govendor fetch github.com/golang/protobuf/...`
|
||||||
|
|
||||||
//go:generate protoc -I/usr/local/include:. --go_out=paths=source_relative:. messages.proto messages-common.proto messages-management.proto messages-ethereum.proto
|
//go:generate protoc -I/usr/local/include:. --go_out=import_path=trezor:. messages.proto messages-common.proto messages-management.proto messages-ethereum.proto
|
||||||
|
|
||||||
// Package trezor contains the wire protocol.
|
// Package trezor contains the wire protocol.
|
||||||
package trezor
|
package trezor
|
||||||
@@ -50,7 +50,7 @@ package trezor
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"google.golang.org/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Type returns the protocol buffer type number of a specific message. If the
|
// Type returns the protocol buffer type number of a specific message. If the
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/karalabe/hid"
|
"github.com/karalabe/usb"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Maximum time between wallet health checks to detect USB unplugs.
|
// Maximum time between wallet health checks to detect USB unplugs.
|
||||||
@@ -79,8 +79,8 @@ type wallet struct {
|
|||||||
driver driver // Hardware implementation of the low level device operations
|
driver driver // Hardware implementation of the low level device operations
|
||||||
url *accounts.URL // Textual URL uniquely identifying this wallet
|
url *accounts.URL // Textual URL uniquely identifying this wallet
|
||||||
|
|
||||||
info hid.DeviceInfo // Known USB device infos about the wallet
|
info usb.DeviceInfo // Known USB device infos about the wallet
|
||||||
device hid.Device // USB device advertising itself as a hardware wallet
|
device usb.Device // USB device advertising itself as a hardware wallet
|
||||||
|
|
||||||
accounts []accounts.Account // List of derive accounts pinned on the hardware wallet
|
accounts []accounts.Account // List of derive accounts pinned on the hardware wallet
|
||||||
paths map[common.Address]accounts.DerivationPath // Known derivation paths for signing operations
|
paths map[common.Address]accounts.DerivationPath // Known derivation paths for signing operations
|
||||||
@@ -483,10 +483,6 @@ func (w *wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Accoun
|
|||||||
w.stateLock.Lock()
|
w.stateLock.Lock()
|
||||||
defer w.stateLock.Unlock()
|
defer w.stateLock.Unlock()
|
||||||
|
|
||||||
if w.device == nil {
|
|
||||||
return accounts.Account{}, accounts.ErrWalletClosed
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := w.paths[address]; !ok {
|
if _, ok := w.paths[address]; !ok {
|
||||||
w.accounts = append(w.accounts, account)
|
w.accounts = append(w.accounts, account)
|
||||||
w.paths[address] = make(accounts.DerivationPath, len(path))
|
w.paths[address] = make(accounts.DerivationPath, len(path))
|
||||||
@@ -628,7 +624,7 @@ func (w *wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID
|
|||||||
return signed, nil
|
return signed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignTextWithPassphrase implements accounts.Wallet, however signing arbitrary
|
// SignHashWithPassphrase implements accounts.Wallet, however signing arbitrary
|
||||||
// data is not supported for Ledger wallets, so this method will always return
|
// data is not supported for Ledger wallets, so this method will always return
|
||||||
// an error.
|
// an error.
|
||||||
func (w *wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
|
func (w *wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
|
||||||
|
|||||||
@@ -24,12 +24,9 @@ for:
|
|||||||
- image: Ubuntu
|
- image: Ubuntu
|
||||||
build_script:
|
build_script:
|
||||||
- go run build/ci.go lint
|
- go run build/ci.go lint
|
||||||
- go run build/ci.go check_tidy
|
|
||||||
- go run build/ci.go check_generate
|
|
||||||
- go run build/ci.go check_baddeps
|
|
||||||
- go run build/ci.go install -dlgo
|
- go run build/ci.go install -dlgo
|
||||||
test_script:
|
test_script:
|
||||||
- go run build/ci.go test -dlgo -short
|
- go run build/ci.go test -dlgo -coverage
|
||||||
|
|
||||||
# linux/386 is disabled.
|
# linux/386 is disabled.
|
||||||
- matrix:
|
- matrix:
|
||||||
@@ -57,4 +54,4 @@ for:
|
|||||||
- go run build/ci.go archive -arch %GETH_ARCH% -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds
|
- go run build/ci.go archive -arch %GETH_ARCH% -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds
|
||||||
- go run build/ci.go nsis -arch %GETH_ARCH% -signer WINDOWS_SIGNING_KEY -upload gethstore/builds
|
- go run build/ci.go nsis -arch %GETH_ARCH% -signer WINDOWS_SIGNING_KEY -upload gethstore/builds
|
||||||
test_script:
|
test_script:
|
||||||
- go run build/ci.go test -dlgo -arch %GETH_ARCH% -cc %GETH_CC% -short
|
- go run build/ci.go test -dlgo -arch %GETH_ARCH% -cc %GETH_CC% -coverage
|
||||||
|
|||||||
@@ -1,163 +0,0 @@
|
|||||||
// Copyright 2023 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 blsync
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/request"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/sync"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/params"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/lru"
|
|
||||||
"github.com/ethereum/go-ethereum/event"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// beaconBlockSync implements request.Module; it fetches the beacon blocks belonging
|
|
||||||
// to the validated and prefetch heads.
|
|
||||||
type beaconBlockSync struct {
|
|
||||||
recentBlocks *lru.Cache[common.Hash, *types.BeaconBlock]
|
|
||||||
locked map[common.Hash]request.ServerAndID
|
|
||||||
serverHeads map[request.Server]common.Hash
|
|
||||||
headTracker headTracker
|
|
||||||
|
|
||||||
lastHeadInfo types.HeadInfo
|
|
||||||
chainHeadFeed event.FeedOf[types.ChainHeadEvent]
|
|
||||||
}
|
|
||||||
|
|
||||||
type headTracker interface {
|
|
||||||
PrefetchHead() types.HeadInfo
|
|
||||||
ValidatedOptimistic() (types.OptimisticUpdate, bool)
|
|
||||||
ValidatedFinality() (types.FinalityUpdate, bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
// newBeaconBlockSync returns a new beaconBlockSync.
|
|
||||||
func newBeaconBlockSync(headTracker headTracker) *beaconBlockSync {
|
|
||||||
return &beaconBlockSync{
|
|
||||||
headTracker: headTracker,
|
|
||||||
recentBlocks: lru.NewCache[common.Hash, *types.BeaconBlock](10),
|
|
||||||
locked: make(map[common.Hash]request.ServerAndID),
|
|
||||||
serverHeads: make(map[request.Server]common.Hash),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *beaconBlockSync) SubscribeChainHead(ch chan<- types.ChainHeadEvent) event.Subscription {
|
|
||||||
return s.chainHeadFeed.Subscribe(ch)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process implements request.Module.
|
|
||||||
func (s *beaconBlockSync) Process(requester request.Requester, events []request.Event) {
|
|
||||||
for _, event := range events {
|
|
||||||
switch event.Type {
|
|
||||||
case request.EvResponse, request.EvFail, request.EvTimeout:
|
|
||||||
sid, req, resp := event.RequestInfo()
|
|
||||||
blockRoot := common.Hash(req.(sync.ReqBeaconBlock))
|
|
||||||
log.Debug("Beacon block event", "type", event.Type.Name, "hash", blockRoot)
|
|
||||||
if resp != nil {
|
|
||||||
s.recentBlocks.Add(blockRoot, resp.(*types.BeaconBlock))
|
|
||||||
}
|
|
||||||
if s.locked[blockRoot] == sid {
|
|
||||||
delete(s.locked, blockRoot)
|
|
||||||
}
|
|
||||||
case sync.EvNewHead:
|
|
||||||
s.serverHeads[event.Server] = event.Data.(types.HeadInfo).BlockRoot
|
|
||||||
case request.EvUnregistered:
|
|
||||||
delete(s.serverHeads, event.Server)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.updateEventFeed()
|
|
||||||
// request validated head block if unavailable and not yet requested
|
|
||||||
if vh, ok := s.headTracker.ValidatedOptimistic(); ok {
|
|
||||||
s.tryRequestBlock(requester, vh.Attested.Hash(), false)
|
|
||||||
}
|
|
||||||
// request prefetch head if the given server has announced it
|
|
||||||
if prefetchHead := s.headTracker.PrefetchHead().BlockRoot; prefetchHead != (common.Hash{}) {
|
|
||||||
s.tryRequestBlock(requester, prefetchHead, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *beaconBlockSync) tryRequestBlock(requester request.Requester, blockRoot common.Hash, needSameHead bool) {
|
|
||||||
if _, ok := s.recentBlocks.Get(blockRoot); ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if _, ok := s.locked[blockRoot]; ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, server := range requester.CanSendTo() {
|
|
||||||
if needSameHead && (s.serverHeads[server] != blockRoot) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
id := requester.Send(server, sync.ReqBeaconBlock(blockRoot))
|
|
||||||
s.locked[blockRoot] = request.ServerAndID{Server: server, ID: id}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func blockHeadInfo(block *types.BeaconBlock) types.HeadInfo {
|
|
||||||
if block == nil {
|
|
||||||
return types.HeadInfo{}
|
|
||||||
}
|
|
||||||
return types.HeadInfo{Slot: block.Slot(), BlockRoot: block.Root()}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *beaconBlockSync) updateEventFeed() {
|
|
||||||
optimistic, ok := s.headTracker.ValidatedOptimistic()
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
validatedHead := optimistic.Attested.Hash()
|
|
||||||
headBlock, ok := s.recentBlocks.Get(validatedHead)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var finalizedHash common.Hash
|
|
||||||
if finality, ok := s.headTracker.ValidatedFinality(); ok {
|
|
||||||
he := optimistic.Attested.Epoch()
|
|
||||||
fe := finality.Attested.Header.Epoch()
|
|
||||||
switch {
|
|
||||||
case he == fe:
|
|
||||||
finalizedHash = finality.Finalized.PayloadHeader.BlockHash()
|
|
||||||
case he < fe:
|
|
||||||
return
|
|
||||||
case he == fe+1:
|
|
||||||
parent, ok := s.recentBlocks.Get(optimistic.Attested.ParentRoot)
|
|
||||||
if !ok || parent.Slot()/params.EpochLength == fe {
|
|
||||||
return // head is at first slot of next epoch, wait for finality update
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
headInfo := blockHeadInfo(headBlock)
|
|
||||||
if headInfo == s.lastHeadInfo {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.lastHeadInfo = headInfo
|
|
||||||
|
|
||||||
// new head block and finality info available; extract executable data and send event to feed
|
|
||||||
execBlock, err := headBlock.ExecutionPayload()
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Error extracting execution block from validated beacon block", "error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.chainHeadFeed.Send(types.ChainHeadEvent{
|
|
||||||
BeaconHead: optimistic.Attested.Header,
|
|
||||||
Block: execBlock,
|
|
||||||
Finalized: finalizedHash,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,163 +0,0 @@
|
|||||||
// Copyright 2023 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 blsync
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/request"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/sync"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common"
|
|
||||||
"github.com/protolambda/zrnt/eth2/beacon/deneb"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
testServer1 = testServer("testServer1")
|
|
||||||
testServer2 = testServer("testServer2")
|
|
||||||
|
|
||||||
testBlock1 = types.NewBeaconBlock(&deneb.BeaconBlock{
|
|
||||||
Slot: 123,
|
|
||||||
Body: deneb.BeaconBlockBody{
|
|
||||||
ExecutionPayload: deneb.ExecutionPayload{
|
|
||||||
BlockNumber: 456,
|
|
||||||
BlockHash: zrntcommon.Hash32(common.HexToHash("905ac721c4058d9ed40b27b6b9c1bdd10d4333e4f3d9769100bf9dfb80e5d1f6")),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
testBlock2 = types.NewBeaconBlock(&deneb.BeaconBlock{
|
|
||||||
Slot: 124,
|
|
||||||
Body: deneb.BeaconBlockBody{
|
|
||||||
ExecutionPayload: deneb.ExecutionPayload{
|
|
||||||
BlockNumber: 457,
|
|
||||||
BlockHash: zrntcommon.Hash32(common.HexToHash("011703f39c664efc1c6cf5f49ca09b595581eec572d4dfddd3d6179a9e63e655")),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
type testServer string
|
|
||||||
|
|
||||||
func (t testServer) Name() string {
|
|
||||||
return string(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBlockSync(t *testing.T) {
|
|
||||||
ht := &testHeadTracker{}
|
|
||||||
blockSync := newBeaconBlockSync(ht)
|
|
||||||
headCh := make(chan types.ChainHeadEvent, 16)
|
|
||||||
blockSync.SubscribeChainHead(headCh)
|
|
||||||
ts := sync.NewTestScheduler(t, blockSync)
|
|
||||||
ts.AddServer(testServer1, 1)
|
|
||||||
ts.AddServer(testServer2, 1)
|
|
||||||
|
|
||||||
expHeadBlock := func(expHead *types.BeaconBlock) {
|
|
||||||
t.Helper()
|
|
||||||
var expNumber, headNumber uint64
|
|
||||||
if expHead != nil {
|
|
||||||
p, err := expHead.ExecutionPayload()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expHead.ExecutionPayload() failed: %v", err)
|
|
||||||
}
|
|
||||||
expNumber = p.NumberU64()
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case event := <-headCh:
|
|
||||||
headNumber = event.Block.NumberU64()
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
if headNumber != expNumber {
|
|
||||||
t.Errorf("Wrong head block, expected block number %d, got %d)", expNumber, headNumber)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no block requests expected until head tracker knows about a head
|
|
||||||
ts.Run(1)
|
|
||||||
expHeadBlock(nil)
|
|
||||||
|
|
||||||
// set block 1 as prefetch head, announced by server 2
|
|
||||||
head1 := blockHeadInfo(testBlock1)
|
|
||||||
ht.prefetch = head1
|
|
||||||
ts.ServerEvent(sync.EvNewHead, testServer2, head1)
|
|
||||||
|
|
||||||
// expect request to server 2 which has announced the head
|
|
||||||
ts.Run(2, testServer2, sync.ReqBeaconBlock(head1.BlockRoot))
|
|
||||||
|
|
||||||
// valid response
|
|
||||||
ts.RequestEvent(request.EvResponse, ts.Request(2, 1), testBlock1)
|
|
||||||
ts.AddAllowance(testServer2, 1)
|
|
||||||
ts.Run(3)
|
|
||||||
// head block still not expected as the fetched block is not the validated head yet
|
|
||||||
expHeadBlock(nil)
|
|
||||||
|
|
||||||
// set as validated head, expect no further requests but block 1 set as head block
|
|
||||||
ht.validated.Header = testBlock1.Header()
|
|
||||||
ts.Run(4)
|
|
||||||
expHeadBlock(testBlock1)
|
|
||||||
|
|
||||||
// set block 2 as prefetch head, announced by server 1
|
|
||||||
head2 := blockHeadInfo(testBlock2)
|
|
||||||
ht.prefetch = head2
|
|
||||||
ts.ServerEvent(sync.EvNewHead, testServer1, head2)
|
|
||||||
// expect request to server 1
|
|
||||||
ts.Run(5, testServer1, sync.ReqBeaconBlock(head2.BlockRoot))
|
|
||||||
|
|
||||||
// req2 fails, no further requests expected because server 2 has not announced it
|
|
||||||
ts.RequestEvent(request.EvFail, ts.Request(5, 1), nil)
|
|
||||||
ts.Run(6)
|
|
||||||
|
|
||||||
// set as validated head before retrieving block; now it's assumed to be available from server 2 too
|
|
||||||
ht.validated.Header = testBlock2.Header()
|
|
||||||
// expect req2 retry to server 2
|
|
||||||
ts.Run(7, testServer2, sync.ReqBeaconBlock(head2.BlockRoot))
|
|
||||||
// now head block should be unavailable again
|
|
||||||
expHeadBlock(nil)
|
|
||||||
|
|
||||||
// valid response, now head block should be block 2 immediately as it is already validated
|
|
||||||
ts.RequestEvent(request.EvResponse, ts.Request(7, 1), testBlock2)
|
|
||||||
ts.Run(8)
|
|
||||||
expHeadBlock(testBlock2)
|
|
||||||
}
|
|
||||||
|
|
||||||
type testHeadTracker struct {
|
|
||||||
prefetch types.HeadInfo
|
|
||||||
validated types.SignedHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *testHeadTracker) PrefetchHead() types.HeadInfo {
|
|
||||||
return h.prefetch
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *testHeadTracker) ValidatedOptimistic() (types.OptimisticUpdate, bool) {
|
|
||||||
return types.OptimisticUpdate{
|
|
||||||
Attested: types.HeaderWithExecProof{Header: h.validated.Header},
|
|
||||||
Signature: h.validated.Signature,
|
|
||||||
SignatureSlot: h.validated.SignatureSlot,
|
|
||||||
}, h.validated.Header != (types.Header{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO add test case for finality
|
|
||||||
func (h *testHeadTracker) ValidatedFinality() (types.FinalityUpdate, bool) {
|
|
||||||
finalized := types.NewExecutionHeader(new(deneb.ExecutionPayloadHeader))
|
|
||||||
return types.FinalityUpdate{
|
|
||||||
Attested: types.HeaderWithExecProof{Header: h.validated.Header},
|
|
||||||
Finalized: types.HeaderWithExecProof{PayloadHeader: finalized},
|
|
||||||
Signature: h.validated.Signature,
|
|
||||||
SignatureSlot: h.validated.SignatureSlot,
|
|
||||||
}, h.validated.Header != (types.Header{})
|
|
||||||
}
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
// Copyright 2024 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 blsync
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/api"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/request"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/sync"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/params"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common/mclock"
|
|
||||||
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
|
||||||
"github.com/ethereum/go-ethereum/event"
|
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
urls []string
|
|
||||||
customHeader map[string]string
|
|
||||||
config *params.ClientConfig
|
|
||||||
scheduler *request.Scheduler
|
|
||||||
blockSync *beaconBlockSync
|
|
||||||
engineRPC *rpc.Client
|
|
||||||
|
|
||||||
chainHeadSub event.Subscription
|
|
||||||
engineClient *engineClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClient(config params.ClientConfig) *Client {
|
|
||||||
// create data structures
|
|
||||||
var (
|
|
||||||
db = memorydb.New()
|
|
||||||
committeeChain = light.NewCommitteeChain(db, &config.ChainConfig, config.Threshold, !config.NoFilter)
|
|
||||||
headTracker = light.NewHeadTracker(committeeChain, config.Threshold)
|
|
||||||
)
|
|
||||||
headSync := sync.NewHeadSync(headTracker, committeeChain)
|
|
||||||
|
|
||||||
// set up scheduler and sync modules
|
|
||||||
scheduler := request.NewScheduler()
|
|
||||||
checkpointInit := sync.NewCheckpointInit(committeeChain, config.Checkpoint)
|
|
||||||
forwardSync := sync.NewForwardUpdateSync(committeeChain)
|
|
||||||
beaconBlockSync := newBeaconBlockSync(headTracker)
|
|
||||||
scheduler.RegisterTarget(headTracker)
|
|
||||||
scheduler.RegisterTarget(committeeChain)
|
|
||||||
scheduler.RegisterModule(checkpointInit, "checkpointInit")
|
|
||||||
scheduler.RegisterModule(forwardSync, "forwardSync")
|
|
||||||
scheduler.RegisterModule(headSync, "headSync")
|
|
||||||
scheduler.RegisterModule(beaconBlockSync, "beaconBlockSync")
|
|
||||||
|
|
||||||
return &Client{
|
|
||||||
scheduler: scheduler,
|
|
||||||
urls: config.Apis,
|
|
||||||
customHeader: config.CustomHeader,
|
|
||||||
config: &config,
|
|
||||||
blockSync: beaconBlockSync,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) SetEngineRPC(engine *rpc.Client) {
|
|
||||||
c.engineRPC = engine
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Start() error {
|
|
||||||
headCh := make(chan types.ChainHeadEvent, 16)
|
|
||||||
c.chainHeadSub = c.blockSync.SubscribeChainHead(headCh)
|
|
||||||
c.engineClient = startEngineClient(c.config, c.engineRPC, headCh)
|
|
||||||
|
|
||||||
c.scheduler.Start()
|
|
||||||
for _, url := range c.urls {
|
|
||||||
beaconApi := api.NewBeaconLightApi(url, c.customHeader)
|
|
||||||
c.scheduler.RegisterServer(request.NewServer(api.NewApiServer(beaconApi), &mclock.System{}))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Stop() error {
|
|
||||||
c.engineClient.stop()
|
|
||||||
c.chainHeadSub.Unsubscribe()
|
|
||||||
c.scheduler.Stop()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,151 +0,0 @@
|
|||||||
// Copyright 2024 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 blsync
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/engine"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/params"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
ctypes "github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type engineClient struct {
|
|
||||||
config *params.ClientConfig
|
|
||||||
rpc *rpc.Client
|
|
||||||
rootCtx context.Context
|
|
||||||
cancelRoot context.CancelFunc
|
|
||||||
wg sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
func startEngineClient(config *params.ClientConfig, rpc *rpc.Client, headCh <-chan types.ChainHeadEvent) *engineClient {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
ec := &engineClient{
|
|
||||||
config: config,
|
|
||||||
rpc: rpc,
|
|
||||||
rootCtx: ctx,
|
|
||||||
cancelRoot: cancel,
|
|
||||||
}
|
|
||||||
ec.wg.Add(1)
|
|
||||||
go ec.updateLoop(headCh)
|
|
||||||
return ec
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ec *engineClient) stop() {
|
|
||||||
ec.cancelRoot()
|
|
||||||
ec.wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ec *engineClient) updateLoop(headCh <-chan types.ChainHeadEvent) {
|
|
||||||
defer ec.wg.Done()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ec.rootCtx.Done():
|
|
||||||
log.Debug("Stopping engine API update loop")
|
|
||||||
return
|
|
||||||
|
|
||||||
case event := <-headCh:
|
|
||||||
if ec.rpc == nil { // dry run, no engine API specified
|
|
||||||
log.Info("New execution block retrieved", "number", event.Block.NumberU64(), "hash", event.Block.Hash(), "finalized", event.Finalized)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fork := ec.config.ForkAtEpoch(event.BeaconHead.Epoch())
|
|
||||||
forkName := strings.ToLower(fork.Name)
|
|
||||||
|
|
||||||
log.Debug("Calling NewPayload", "number", event.Block.NumberU64(), "hash", event.Block.Hash())
|
|
||||||
if status, err := ec.callNewPayload(forkName, event); err == nil {
|
|
||||||
log.Info("Successful NewPayload", "number", event.Block.NumberU64(), "hash", event.Block.Hash(), "status", status)
|
|
||||||
} else {
|
|
||||||
log.Error("Failed NewPayload", "number", event.Block.NumberU64(), "hash", event.Block.Hash(), "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug("Calling ForkchoiceUpdated", "head", event.Block.Hash())
|
|
||||||
if status, err := ec.callForkchoiceUpdated(forkName, event); err == nil {
|
|
||||||
log.Info("Successful ForkchoiceUpdated", "head", event.Block.Hash(), "status", status)
|
|
||||||
} else {
|
|
||||||
log.Error("Failed ForkchoiceUpdated", "head", event.Block.Hash(), "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ec *engineClient) callNewPayload(fork string, event types.ChainHeadEvent) (string, error) {
|
|
||||||
execData := engine.BlockToExecutableData(event.Block, nil, nil, nil).ExecutionPayload
|
|
||||||
|
|
||||||
var (
|
|
||||||
method string
|
|
||||||
params = []any{execData}
|
|
||||||
)
|
|
||||||
switch fork {
|
|
||||||
case "deneb":
|
|
||||||
method = "engine_newPayloadV3"
|
|
||||||
parentBeaconRoot := event.BeaconHead.ParentRoot
|
|
||||||
blobHashes := collectBlobHashes(event.Block)
|
|
||||||
params = append(params, blobHashes, parentBeaconRoot)
|
|
||||||
case "capella":
|
|
||||||
method = "engine_newPayloadV2"
|
|
||||||
default:
|
|
||||||
method = "engine_newPayloadV1"
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ec.rootCtx, time.Second*5)
|
|
||||||
defer cancel()
|
|
||||||
var resp engine.PayloadStatusV1
|
|
||||||
err := ec.rpc.CallContext(ctx, &resp, method, params...)
|
|
||||||
return resp.Status, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func collectBlobHashes(b *ctypes.Block) []common.Hash {
|
|
||||||
list := make([]common.Hash, 0)
|
|
||||||
for _, tx := range b.Transactions() {
|
|
||||||
list = append(list, tx.BlobHashes()...)
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ec *engineClient) callForkchoiceUpdated(fork string, event types.ChainHeadEvent) (string, error) {
|
|
||||||
update := engine.ForkchoiceStateV1{
|
|
||||||
HeadBlockHash: event.Block.Hash(),
|
|
||||||
SafeBlockHash: event.Finalized,
|
|
||||||
FinalizedBlockHash: event.Finalized,
|
|
||||||
}
|
|
||||||
|
|
||||||
var method string
|
|
||||||
switch fork {
|
|
||||||
case "deneb":
|
|
||||||
method = "engine_forkchoiceUpdatedV3"
|
|
||||||
case "capella":
|
|
||||||
method = "engine_forkchoiceUpdatedV2"
|
|
||||||
default:
|
|
||||||
method = "engine_forkchoiceUpdatedV1"
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ec.rootCtx, time.Second*5)
|
|
||||||
defer cancel()
|
|
||||||
var resp engine.ForkChoiceResponse
|
|
||||||
err := ec.rpc.CallContext(ctx, &resp, method, update, nil)
|
|
||||||
return resp.PayloadStatus.Status, err
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
|
||||||
|
|
||||||
package engine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ = (*payloadAttributesMarshaling)(nil)
|
|
||||||
|
|
||||||
// MarshalJSON marshals as JSON.
|
|
||||||
func (p PayloadAttributes) MarshalJSON() ([]byte, error) {
|
|
||||||
type PayloadAttributes struct {
|
|
||||||
Timestamp hexutil.Uint64 `json:"timestamp" gencodec:"required"`
|
|
||||||
Random common.Hash `json:"prevRandao" gencodec:"required"`
|
|
||||||
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient" gencodec:"required"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
BeaconRoot *common.Hash `json:"parentBeaconBlockRoot"`
|
|
||||||
}
|
|
||||||
var enc PayloadAttributes
|
|
||||||
enc.Timestamp = hexutil.Uint64(p.Timestamp)
|
|
||||||
enc.Random = p.Random
|
|
||||||
enc.SuggestedFeeRecipient = p.SuggestedFeeRecipient
|
|
||||||
enc.Withdrawals = p.Withdrawals
|
|
||||||
enc.BeaconRoot = p.BeaconRoot
|
|
||||||
return json.Marshal(&enc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON unmarshals from JSON.
|
|
||||||
func (p *PayloadAttributes) UnmarshalJSON(input []byte) error {
|
|
||||||
type PayloadAttributes struct {
|
|
||||||
Timestamp *hexutil.Uint64 `json:"timestamp" gencodec:"required"`
|
|
||||||
Random *common.Hash `json:"prevRandao" gencodec:"required"`
|
|
||||||
SuggestedFeeRecipient *common.Address `json:"suggestedFeeRecipient" gencodec:"required"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
BeaconRoot *common.Hash `json:"parentBeaconBlockRoot"`
|
|
||||||
}
|
|
||||||
var dec PayloadAttributes
|
|
||||||
if err := json.Unmarshal(input, &dec); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if dec.Timestamp == nil {
|
|
||||||
return errors.New("missing required field 'timestamp' for PayloadAttributes")
|
|
||||||
}
|
|
||||||
p.Timestamp = uint64(*dec.Timestamp)
|
|
||||||
if dec.Random == nil {
|
|
||||||
return errors.New("missing required field 'prevRandao' for PayloadAttributes")
|
|
||||||
}
|
|
||||||
p.Random = *dec.Random
|
|
||||||
if dec.SuggestedFeeRecipient == nil {
|
|
||||||
return errors.New("missing required field 'suggestedFeeRecipient' for PayloadAttributes")
|
|
||||||
}
|
|
||||||
p.SuggestedFeeRecipient = *dec.SuggestedFeeRecipient
|
|
||||||
if dec.Withdrawals != nil {
|
|
||||||
p.Withdrawals = dec.Withdrawals
|
|
||||||
}
|
|
||||||
if dec.BeaconRoot != nil {
|
|
||||||
p.BeaconRoot = dec.BeaconRoot
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
|
||||||
|
|
||||||
package engine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ = (*executableDataMarshaling)(nil)
|
|
||||||
|
|
||||||
// MarshalJSON marshals as JSON.
|
|
||||||
func (e ExecutableData) MarshalJSON() ([]byte, error) {
|
|
||||||
type ExecutableData struct {
|
|
||||||
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
|
|
||||||
FeeRecipient common.Address `json:"feeRecipient" gencodec:"required"`
|
|
||||||
StateRoot common.Hash `json:"stateRoot" gencodec:"required"`
|
|
||||||
ReceiptsRoot common.Hash `json:"receiptsRoot" gencodec:"required"`
|
|
||||||
LogsBloom hexutil.Bytes `json:"logsBloom" gencodec:"required"`
|
|
||||||
Random common.Hash `json:"prevRandao" gencodec:"required"`
|
|
||||||
Number hexutil.Uint64 `json:"blockNumber" gencodec:"required"`
|
|
||||||
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
|
|
||||||
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
|
|
||||||
Timestamp hexutil.Uint64 `json:"timestamp" gencodec:"required"`
|
|
||||||
ExtraData hexutil.Bytes `json:"extraData" gencodec:"required"`
|
|
||||||
BaseFeePerGas *hexutil.Big `json:"baseFeePerGas" gencodec:"required"`
|
|
||||||
BlockHash common.Hash `json:"blockHash" gencodec:"required"`
|
|
||||||
Transactions []hexutil.Bytes `json:"transactions" gencodec:"required"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed"`
|
|
||||||
ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"`
|
|
||||||
ExecutionWitness *types.ExecutionWitness `json:"executionWitness,omitempty"`
|
|
||||||
}
|
|
||||||
var enc ExecutableData
|
|
||||||
enc.ParentHash = e.ParentHash
|
|
||||||
enc.FeeRecipient = e.FeeRecipient
|
|
||||||
enc.StateRoot = e.StateRoot
|
|
||||||
enc.ReceiptsRoot = e.ReceiptsRoot
|
|
||||||
enc.LogsBloom = e.LogsBloom
|
|
||||||
enc.Random = e.Random
|
|
||||||
enc.Number = hexutil.Uint64(e.Number)
|
|
||||||
enc.GasLimit = hexutil.Uint64(e.GasLimit)
|
|
||||||
enc.GasUsed = hexutil.Uint64(e.GasUsed)
|
|
||||||
enc.Timestamp = hexutil.Uint64(e.Timestamp)
|
|
||||||
enc.ExtraData = e.ExtraData
|
|
||||||
enc.BaseFeePerGas = (*hexutil.Big)(e.BaseFeePerGas)
|
|
||||||
enc.BlockHash = e.BlockHash
|
|
||||||
if e.Transactions != nil {
|
|
||||||
enc.Transactions = make([]hexutil.Bytes, len(e.Transactions))
|
|
||||||
for k, v := range e.Transactions {
|
|
||||||
enc.Transactions[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
enc.Withdrawals = e.Withdrawals
|
|
||||||
enc.BlobGasUsed = (*hexutil.Uint64)(e.BlobGasUsed)
|
|
||||||
enc.ExcessBlobGas = (*hexutil.Uint64)(e.ExcessBlobGas)
|
|
||||||
enc.ExecutionWitness = e.ExecutionWitness
|
|
||||||
return json.Marshal(&enc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON unmarshals from JSON.
|
|
||||||
func (e *ExecutableData) UnmarshalJSON(input []byte) error {
|
|
||||||
type ExecutableData struct {
|
|
||||||
ParentHash *common.Hash `json:"parentHash" gencodec:"required"`
|
|
||||||
FeeRecipient *common.Address `json:"feeRecipient" gencodec:"required"`
|
|
||||||
StateRoot *common.Hash `json:"stateRoot" gencodec:"required"`
|
|
||||||
ReceiptsRoot *common.Hash `json:"receiptsRoot" gencodec:"required"`
|
|
||||||
LogsBloom *hexutil.Bytes `json:"logsBloom" gencodec:"required"`
|
|
||||||
Random *common.Hash `json:"prevRandao" gencodec:"required"`
|
|
||||||
Number *hexutil.Uint64 `json:"blockNumber" gencodec:"required"`
|
|
||||||
GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
|
|
||||||
GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
|
|
||||||
Timestamp *hexutil.Uint64 `json:"timestamp" gencodec:"required"`
|
|
||||||
ExtraData *hexutil.Bytes `json:"extraData" gencodec:"required"`
|
|
||||||
BaseFeePerGas *hexutil.Big `json:"baseFeePerGas" gencodec:"required"`
|
|
||||||
BlockHash *common.Hash `json:"blockHash" gencodec:"required"`
|
|
||||||
Transactions []hexutil.Bytes `json:"transactions" gencodec:"required"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed"`
|
|
||||||
ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"`
|
|
||||||
ExecutionWitness *types.ExecutionWitness `json:"executionWitness,omitempty"`
|
|
||||||
}
|
|
||||||
var dec ExecutableData
|
|
||||||
if err := json.Unmarshal(input, &dec); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if dec.ParentHash == nil {
|
|
||||||
return errors.New("missing required field 'parentHash' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.ParentHash = *dec.ParentHash
|
|
||||||
if dec.FeeRecipient == nil {
|
|
||||||
return errors.New("missing required field 'feeRecipient' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.FeeRecipient = *dec.FeeRecipient
|
|
||||||
if dec.StateRoot == nil {
|
|
||||||
return errors.New("missing required field 'stateRoot' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.StateRoot = *dec.StateRoot
|
|
||||||
if dec.ReceiptsRoot == nil {
|
|
||||||
return errors.New("missing required field 'receiptsRoot' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.ReceiptsRoot = *dec.ReceiptsRoot
|
|
||||||
if dec.LogsBloom == nil {
|
|
||||||
return errors.New("missing required field 'logsBloom' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.LogsBloom = *dec.LogsBloom
|
|
||||||
if dec.Random == nil {
|
|
||||||
return errors.New("missing required field 'prevRandao' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.Random = *dec.Random
|
|
||||||
if dec.Number == nil {
|
|
||||||
return errors.New("missing required field 'blockNumber' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.Number = uint64(*dec.Number)
|
|
||||||
if dec.GasLimit == nil {
|
|
||||||
return errors.New("missing required field 'gasLimit' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.GasLimit = uint64(*dec.GasLimit)
|
|
||||||
if dec.GasUsed == nil {
|
|
||||||
return errors.New("missing required field 'gasUsed' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.GasUsed = uint64(*dec.GasUsed)
|
|
||||||
if dec.Timestamp == nil {
|
|
||||||
return errors.New("missing required field 'timestamp' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.Timestamp = uint64(*dec.Timestamp)
|
|
||||||
if dec.ExtraData == nil {
|
|
||||||
return errors.New("missing required field 'extraData' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.ExtraData = *dec.ExtraData
|
|
||||||
if dec.BaseFeePerGas == nil {
|
|
||||||
return errors.New("missing required field 'baseFeePerGas' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.BaseFeePerGas = (*big.Int)(dec.BaseFeePerGas)
|
|
||||||
if dec.BlockHash == nil {
|
|
||||||
return errors.New("missing required field 'blockHash' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.BlockHash = *dec.BlockHash
|
|
||||||
if dec.Transactions == nil {
|
|
||||||
return errors.New("missing required field 'transactions' for ExecutableData")
|
|
||||||
}
|
|
||||||
e.Transactions = make([][]byte, len(dec.Transactions))
|
|
||||||
for k, v := range dec.Transactions {
|
|
||||||
e.Transactions[k] = v
|
|
||||||
}
|
|
||||||
if dec.Withdrawals != nil {
|
|
||||||
e.Withdrawals = dec.Withdrawals
|
|
||||||
}
|
|
||||||
if dec.BlobGasUsed != nil {
|
|
||||||
e.BlobGasUsed = (*uint64)(dec.BlobGasUsed)
|
|
||||||
}
|
|
||||||
if dec.ExcessBlobGas != nil {
|
|
||||||
e.ExcessBlobGas = (*uint64)(dec.ExcessBlobGas)
|
|
||||||
}
|
|
||||||
if dec.ExecutionWitness != nil {
|
|
||||||
e.ExecutionWitness = dec.ExecutionWitness
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
|
||||||
|
|
||||||
package engine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ = (*executionPayloadEnvelopeMarshaling)(nil)
|
|
||||||
|
|
||||||
// MarshalJSON marshals as JSON.
|
|
||||||
func (e ExecutionPayloadEnvelope) MarshalJSON() ([]byte, error) {
|
|
||||||
type ExecutionPayloadEnvelope struct {
|
|
||||||
ExecutionPayload *ExecutableData `json:"executionPayload" gencodec:"required"`
|
|
||||||
BlockValue *hexutil.Big `json:"blockValue" gencodec:"required"`
|
|
||||||
BlobsBundle *BlobsBundleV1 `json:"blobsBundle"`
|
|
||||||
Requests []hexutil.Bytes `json:"executionRequests"`
|
|
||||||
Override bool `json:"shouldOverrideBuilder"`
|
|
||||||
Witness *hexutil.Bytes `json:"witness,omitempty"`
|
|
||||||
}
|
|
||||||
var enc ExecutionPayloadEnvelope
|
|
||||||
enc.ExecutionPayload = e.ExecutionPayload
|
|
||||||
enc.BlockValue = (*hexutil.Big)(e.BlockValue)
|
|
||||||
enc.BlobsBundle = e.BlobsBundle
|
|
||||||
if e.Requests != nil {
|
|
||||||
enc.Requests = make([]hexutil.Bytes, len(e.Requests))
|
|
||||||
for k, v := range e.Requests {
|
|
||||||
enc.Requests[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
enc.Override = e.Override
|
|
||||||
enc.Witness = e.Witness
|
|
||||||
return json.Marshal(&enc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON unmarshals from JSON.
|
|
||||||
func (e *ExecutionPayloadEnvelope) UnmarshalJSON(input []byte) error {
|
|
||||||
type ExecutionPayloadEnvelope struct {
|
|
||||||
ExecutionPayload *ExecutableData `json:"executionPayload" gencodec:"required"`
|
|
||||||
BlockValue *hexutil.Big `json:"blockValue" gencodec:"required"`
|
|
||||||
BlobsBundle *BlobsBundleV1 `json:"blobsBundle"`
|
|
||||||
Requests []hexutil.Bytes `json:"executionRequests"`
|
|
||||||
Override *bool `json:"shouldOverrideBuilder"`
|
|
||||||
Witness *hexutil.Bytes `json:"witness,omitempty"`
|
|
||||||
}
|
|
||||||
var dec ExecutionPayloadEnvelope
|
|
||||||
if err := json.Unmarshal(input, &dec); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if dec.ExecutionPayload == nil {
|
|
||||||
return errors.New("missing required field 'executionPayload' for ExecutionPayloadEnvelope")
|
|
||||||
}
|
|
||||||
e.ExecutionPayload = dec.ExecutionPayload
|
|
||||||
if dec.BlockValue == nil {
|
|
||||||
return errors.New("missing required field 'blockValue' for ExecutionPayloadEnvelope")
|
|
||||||
}
|
|
||||||
e.BlockValue = (*big.Int)(dec.BlockValue)
|
|
||||||
if dec.BlobsBundle != nil {
|
|
||||||
e.BlobsBundle = dec.BlobsBundle
|
|
||||||
}
|
|
||||||
if dec.Requests != nil {
|
|
||||||
e.Requests = make([][]byte, len(dec.Requests))
|
|
||||||
for k, v := range dec.Requests {
|
|
||||||
e.Requests[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if dec.Override != nil {
|
|
||||||
e.Override = *dec.Override
|
|
||||||
}
|
|
||||||
if dec.Witness != nil {
|
|
||||||
e.Witness = dec.Witness
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,369 +0,0 @@
|
|||||||
// Copyright 2022 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 engine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
"slices"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"github.com/ethereum/go-ethereum/params"
|
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
|
||||||
)
|
|
||||||
|
|
||||||
// PayloadVersion denotes the version of PayloadAttributes used to request the
|
|
||||||
// building of the payload to commence.
|
|
||||||
type PayloadVersion byte
|
|
||||||
|
|
||||||
var (
|
|
||||||
PayloadV1 PayloadVersion = 0x1
|
|
||||||
PayloadV2 PayloadVersion = 0x2
|
|
||||||
PayloadV3 PayloadVersion = 0x3
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:generate go run github.com/fjl/gencodec -type PayloadAttributes -field-override payloadAttributesMarshaling -out gen_blockparams.go
|
|
||||||
|
|
||||||
// PayloadAttributes describes the environment context in which a block should
|
|
||||||
// be built.
|
|
||||||
type PayloadAttributes struct {
|
|
||||||
Timestamp uint64 `json:"timestamp" gencodec:"required"`
|
|
||||||
Random common.Hash `json:"prevRandao" gencodec:"required"`
|
|
||||||
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient" gencodec:"required"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
BeaconRoot *common.Hash `json:"parentBeaconBlockRoot"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON type overrides for PayloadAttributes.
|
|
||||||
type payloadAttributesMarshaling struct {
|
|
||||||
Timestamp hexutil.Uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:generate go run github.com/fjl/gencodec -type ExecutableData -field-override executableDataMarshaling -out gen_ed.go
|
|
||||||
|
|
||||||
// ExecutableData is the data necessary to execute an EL payload.
|
|
||||||
type ExecutableData struct {
|
|
||||||
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
|
|
||||||
FeeRecipient common.Address `json:"feeRecipient" gencodec:"required"`
|
|
||||||
StateRoot common.Hash `json:"stateRoot" gencodec:"required"`
|
|
||||||
ReceiptsRoot common.Hash `json:"receiptsRoot" gencodec:"required"`
|
|
||||||
LogsBloom []byte `json:"logsBloom" gencodec:"required"`
|
|
||||||
Random common.Hash `json:"prevRandao" gencodec:"required"`
|
|
||||||
Number uint64 `json:"blockNumber" gencodec:"required"`
|
|
||||||
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
|
|
||||||
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
|
|
||||||
Timestamp uint64 `json:"timestamp" gencodec:"required"`
|
|
||||||
ExtraData []byte `json:"extraData" gencodec:"required"`
|
|
||||||
BaseFeePerGas *big.Int `json:"baseFeePerGas" gencodec:"required"`
|
|
||||||
BlockHash common.Hash `json:"blockHash" gencodec:"required"`
|
|
||||||
Transactions [][]byte `json:"transactions" gencodec:"required"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
BlobGasUsed *uint64 `json:"blobGasUsed"`
|
|
||||||
ExcessBlobGas *uint64 `json:"excessBlobGas"`
|
|
||||||
ExecutionWitness *types.ExecutionWitness `json:"executionWitness,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON type overrides for executableData.
|
|
||||||
type executableDataMarshaling struct {
|
|
||||||
Number hexutil.Uint64
|
|
||||||
GasLimit hexutil.Uint64
|
|
||||||
GasUsed hexutil.Uint64
|
|
||||||
Timestamp hexutil.Uint64
|
|
||||||
BaseFeePerGas *hexutil.Big
|
|
||||||
ExtraData hexutil.Bytes
|
|
||||||
LogsBloom hexutil.Bytes
|
|
||||||
Transactions []hexutil.Bytes
|
|
||||||
BlobGasUsed *hexutil.Uint64
|
|
||||||
ExcessBlobGas *hexutil.Uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// StatelessPayloadStatusV1 is the result of a stateless payload execution.
|
|
||||||
type StatelessPayloadStatusV1 struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
StateRoot common.Hash `json:"stateRoot"`
|
|
||||||
ReceiptsRoot common.Hash `json:"receiptsRoot"`
|
|
||||||
ValidationError *string `json:"validationError"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:generate go run github.com/fjl/gencodec -type ExecutionPayloadEnvelope -field-override executionPayloadEnvelopeMarshaling -out gen_epe.go
|
|
||||||
|
|
||||||
type ExecutionPayloadEnvelope struct {
|
|
||||||
ExecutionPayload *ExecutableData `json:"executionPayload" gencodec:"required"`
|
|
||||||
BlockValue *big.Int `json:"blockValue" gencodec:"required"`
|
|
||||||
BlobsBundle *BlobsBundleV1 `json:"blobsBundle"`
|
|
||||||
Requests [][]byte `json:"executionRequests"`
|
|
||||||
Override bool `json:"shouldOverrideBuilder"`
|
|
||||||
Witness *hexutil.Bytes `json:"witness,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type BlobsBundleV1 struct {
|
|
||||||
Commitments []hexutil.Bytes `json:"commitments"`
|
|
||||||
Proofs []hexutil.Bytes `json:"proofs"`
|
|
||||||
Blobs []hexutil.Bytes `json:"blobs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type BlobAndProofV1 struct {
|
|
||||||
Blob hexutil.Bytes `json:"blob"`
|
|
||||||
Proof hexutil.Bytes `json:"proof"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON type overrides for ExecutionPayloadEnvelope.
|
|
||||||
type executionPayloadEnvelopeMarshaling struct {
|
|
||||||
BlockValue *hexutil.Big
|
|
||||||
Requests []hexutil.Bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
type PayloadStatusV1 struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
Witness *hexutil.Bytes `json:"witness"`
|
|
||||||
LatestValidHash *common.Hash `json:"latestValidHash"`
|
|
||||||
ValidationError *string `json:"validationError"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TransitionConfigurationV1 struct {
|
|
||||||
TerminalTotalDifficulty *hexutil.Big `json:"terminalTotalDifficulty"`
|
|
||||||
TerminalBlockHash common.Hash `json:"terminalBlockHash"`
|
|
||||||
TerminalBlockNumber hexutil.Uint64 `json:"terminalBlockNumber"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// PayloadID is an identifier of the payload build process
|
|
||||||
type PayloadID [8]byte
|
|
||||||
|
|
||||||
// Version returns the payload version associated with the identifier.
|
|
||||||
func (b PayloadID) Version() PayloadVersion {
|
|
||||||
return PayloadVersion(b[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is returns whether the identifier matches any of provided payload versions.
|
|
||||||
func (b PayloadID) Is(versions ...PayloadVersion) bool {
|
|
||||||
return slices.Contains(versions, b.Version())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b PayloadID) String() string {
|
|
||||||
return hexutil.Encode(b[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b PayloadID) MarshalText() ([]byte, error) {
|
|
||||||
return hexutil.Bytes(b[:]).MarshalText()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *PayloadID) UnmarshalText(input []byte) error {
|
|
||||||
err := hexutil.UnmarshalFixedText("PayloadID", input, b[:])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid payload id %q: %w", input, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ForkChoiceResponse struct {
|
|
||||||
PayloadStatus PayloadStatusV1 `json:"payloadStatus"`
|
|
||||||
PayloadID *PayloadID `json:"payloadId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ForkchoiceStateV1 struct {
|
|
||||||
HeadBlockHash common.Hash `json:"headBlockHash"`
|
|
||||||
SafeBlockHash common.Hash `json:"safeBlockHash"`
|
|
||||||
FinalizedBlockHash common.Hash `json:"finalizedBlockHash"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeTransactions(txs []*types.Transaction) [][]byte {
|
|
||||||
var enc = make([][]byte, len(txs))
|
|
||||||
for i, tx := range txs {
|
|
||||||
enc[i], _ = tx.MarshalBinary()
|
|
||||||
}
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
|
|
||||||
var txs = make([]*types.Transaction, len(enc))
|
|
||||||
for i, encTx := range enc {
|
|
||||||
var tx types.Transaction
|
|
||||||
if err := tx.UnmarshalBinary(encTx); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid transaction %d: %v", i, err)
|
|
||||||
}
|
|
||||||
txs[i] = &tx
|
|
||||||
}
|
|
||||||
return txs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecutableDataToBlock constructs a block from executable data.
|
|
||||||
// It verifies that the following fields:
|
|
||||||
//
|
|
||||||
// len(extraData) <= 32
|
|
||||||
// uncleHash = emptyUncleHash
|
|
||||||
// difficulty = 0
|
|
||||||
// if versionedHashes != nil, versionedHashes match to blob transactions
|
|
||||||
//
|
|
||||||
// and that the blockhash of the constructed block matches the parameters. Nil
|
|
||||||
// Withdrawals value will propagate through the returned block. Empty
|
|
||||||
// Withdrawals value must be passed via non-nil, length 0 value in data.
|
|
||||||
func ExecutableDataToBlock(data ExecutableData, versionedHashes []common.Hash, beaconRoot *common.Hash, requests [][]byte) (*types.Block, error) {
|
|
||||||
block, err := ExecutableDataToBlockNoHash(data, versionedHashes, beaconRoot, requests)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if block.Hash() != data.BlockHash {
|
|
||||||
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", data.BlockHash, block.Hash())
|
|
||||||
}
|
|
||||||
return block, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecutableDataToBlockNoHash is analogous to ExecutableDataToBlock, but is used
|
|
||||||
// for stateless execution, so it skips checking if the executable data hashes to
|
|
||||||
// the requested hash (stateless has to *compute* the root hash, it's not given).
|
|
||||||
func ExecutableDataToBlockNoHash(data ExecutableData, versionedHashes []common.Hash, beaconRoot *common.Hash, requests [][]byte) (*types.Block, error) {
|
|
||||||
txs, err := decodeTransactions(data.Transactions)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(data.ExtraData) > int(params.MaximumExtraDataSize) {
|
|
||||||
return nil, fmt.Errorf("invalid extradata length: %v", len(data.ExtraData))
|
|
||||||
}
|
|
||||||
if len(data.LogsBloom) != 256 {
|
|
||||||
return nil, fmt.Errorf("invalid logsBloom length: %v", len(data.LogsBloom))
|
|
||||||
}
|
|
||||||
// Check that baseFeePerGas is not negative or too big
|
|
||||||
if data.BaseFeePerGas != nil && (data.BaseFeePerGas.Sign() == -1 || data.BaseFeePerGas.BitLen() > 256) {
|
|
||||||
return nil, fmt.Errorf("invalid baseFeePerGas: %v", data.BaseFeePerGas)
|
|
||||||
}
|
|
||||||
var blobHashes = make([]common.Hash, 0, len(txs))
|
|
||||||
for _, tx := range txs {
|
|
||||||
blobHashes = append(blobHashes, tx.BlobHashes()...)
|
|
||||||
}
|
|
||||||
if len(blobHashes) != len(versionedHashes) {
|
|
||||||
return nil, fmt.Errorf("invalid number of versionedHashes: %v blobHashes: %v", versionedHashes, blobHashes)
|
|
||||||
}
|
|
||||||
for i := 0; i < len(blobHashes); i++ {
|
|
||||||
if blobHashes[i] != versionedHashes[i] {
|
|
||||||
return nil, fmt.Errorf("invalid versionedHash at %v: %v blobHashes: %v", i, versionedHashes, blobHashes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Only set withdrawalsRoot if it is non-nil. This allows CLs to use
|
|
||||||
// ExecutableData before withdrawals are enabled by marshaling
|
|
||||||
// Withdrawals as the json null value.
|
|
||||||
var withdrawalsRoot *common.Hash
|
|
||||||
if data.Withdrawals != nil {
|
|
||||||
h := types.DeriveSha(types.Withdrawals(data.Withdrawals), trie.NewStackTrie(nil))
|
|
||||||
withdrawalsRoot = &h
|
|
||||||
}
|
|
||||||
|
|
||||||
var requestsHash *common.Hash
|
|
||||||
if requests != nil {
|
|
||||||
h := types.CalcRequestsHash(requests)
|
|
||||||
requestsHash = &h
|
|
||||||
}
|
|
||||||
|
|
||||||
header := &types.Header{
|
|
||||||
ParentHash: data.ParentHash,
|
|
||||||
UncleHash: types.EmptyUncleHash,
|
|
||||||
Coinbase: data.FeeRecipient,
|
|
||||||
Root: data.StateRoot,
|
|
||||||
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
|
|
||||||
ReceiptHash: data.ReceiptsRoot,
|
|
||||||
Bloom: types.BytesToBloom(data.LogsBloom),
|
|
||||||
Difficulty: common.Big0,
|
|
||||||
Number: new(big.Int).SetUint64(data.Number),
|
|
||||||
GasLimit: data.GasLimit,
|
|
||||||
GasUsed: data.GasUsed,
|
|
||||||
Time: data.Timestamp,
|
|
||||||
BaseFee: data.BaseFeePerGas,
|
|
||||||
Extra: data.ExtraData,
|
|
||||||
MixDigest: data.Random,
|
|
||||||
WithdrawalsHash: withdrawalsRoot,
|
|
||||||
ExcessBlobGas: data.ExcessBlobGas,
|
|
||||||
BlobGasUsed: data.BlobGasUsed,
|
|
||||||
ParentBeaconRoot: beaconRoot,
|
|
||||||
RequestsHash: requestsHash,
|
|
||||||
}
|
|
||||||
return types.NewBlockWithHeader(header).
|
|
||||||
WithBody(types.Body{Transactions: txs, Uncles: nil, Withdrawals: data.Withdrawals}).
|
|
||||||
WithWitness(data.ExecutionWitness),
|
|
||||||
nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BlockToExecutableData constructs the ExecutableData structure by filling the
|
|
||||||
// fields from the given block. It assumes the given block is post-merge block.
|
|
||||||
func BlockToExecutableData(block *types.Block, fees *big.Int, sidecars []*types.BlobTxSidecar, requests [][]byte) *ExecutionPayloadEnvelope {
|
|
||||||
data := &ExecutableData{
|
|
||||||
BlockHash: block.Hash(),
|
|
||||||
ParentHash: block.ParentHash(),
|
|
||||||
FeeRecipient: block.Coinbase(),
|
|
||||||
StateRoot: block.Root(),
|
|
||||||
Number: block.NumberU64(),
|
|
||||||
GasLimit: block.GasLimit(),
|
|
||||||
GasUsed: block.GasUsed(),
|
|
||||||
BaseFeePerGas: block.BaseFee(),
|
|
||||||
Timestamp: block.Time(),
|
|
||||||
ReceiptsRoot: block.ReceiptHash(),
|
|
||||||
LogsBloom: block.Bloom().Bytes(),
|
|
||||||
Transactions: encodeTransactions(block.Transactions()),
|
|
||||||
Random: block.MixDigest(),
|
|
||||||
ExtraData: block.Extra(),
|
|
||||||
Withdrawals: block.Withdrawals(),
|
|
||||||
BlobGasUsed: block.BlobGasUsed(),
|
|
||||||
ExcessBlobGas: block.ExcessBlobGas(),
|
|
||||||
ExecutionWitness: block.ExecutionWitness(),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add blobs.
|
|
||||||
bundle := BlobsBundleV1{
|
|
||||||
Commitments: make([]hexutil.Bytes, 0),
|
|
||||||
Blobs: make([]hexutil.Bytes, 0),
|
|
||||||
Proofs: make([]hexutil.Bytes, 0),
|
|
||||||
}
|
|
||||||
for _, sidecar := range sidecars {
|
|
||||||
for j := range sidecar.Blobs {
|
|
||||||
bundle.Blobs = append(bundle.Blobs, hexutil.Bytes(sidecar.Blobs[j][:]))
|
|
||||||
bundle.Commitments = append(bundle.Commitments, hexutil.Bytes(sidecar.Commitments[j][:]))
|
|
||||||
bundle.Proofs = append(bundle.Proofs, hexutil.Bytes(sidecar.Proofs[j][:]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ExecutionPayloadEnvelope{
|
|
||||||
ExecutionPayload: data,
|
|
||||||
BlockValue: fees,
|
|
||||||
BlobsBundle: &bundle,
|
|
||||||
Requests: requests,
|
|
||||||
Override: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecutionPayloadBody is used in the response to GetPayloadBodiesByHash and GetPayloadBodiesByRange
|
|
||||||
type ExecutionPayloadBody struct {
|
|
||||||
TransactionData []hexutil.Bytes `json:"transactions"`
|
|
||||||
Withdrawals []*types.Withdrawal `json:"withdrawals"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Client identifiers to support ClientVersionV1.
|
|
||||||
const (
|
|
||||||
ClientCode = "GE"
|
|
||||||
ClientName = "go-ethereum"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ClientVersionV1 contains information which identifies a client implementation.
|
|
||||||
type ClientVersionV1 struct {
|
|
||||||
Code string `json:"code"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Version string `json:"version"`
|
|
||||||
Commit string `json:"commit"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *ClientVersionV1) String() string {
|
|
||||||
return fmt.Sprintf("%s-%s-%s-%s", v.Code, v.Name, v.Version, v.Commit)
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
// Copyright 2023 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 api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/request"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/light/sync"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ApiServer is a wrapper around BeaconLightApi that implements request.requestServer.
|
|
||||||
type ApiServer struct {
|
|
||||||
api *BeaconLightApi
|
|
||||||
eventCallback func(event request.Event)
|
|
||||||
unsubscribe func()
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewApiServer creates a new ApiServer.
|
|
||||||
func NewApiServer(api *BeaconLightApi) *ApiServer {
|
|
||||||
return &ApiServer{api: api}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe implements request.requestServer.
|
|
||||||
func (s *ApiServer) Subscribe(eventCallback func(event request.Event)) {
|
|
||||||
s.eventCallback = eventCallback
|
|
||||||
listener := HeadEventListener{
|
|
||||||
OnNewHead: func(slot uint64, blockRoot common.Hash) {
|
|
||||||
log.Debug("New head received", "slot", slot, "blockRoot", blockRoot)
|
|
||||||
eventCallback(request.Event{Type: sync.EvNewHead, Data: types.HeadInfo{Slot: slot, BlockRoot: blockRoot}})
|
|
||||||
},
|
|
||||||
OnOptimistic: func(update types.OptimisticUpdate) {
|
|
||||||
log.Debug("New optimistic update received", "slot", update.Attested.Slot, "blockRoot", update.Attested.Hash(), "signerCount", update.Signature.SignerCount())
|
|
||||||
eventCallback(request.Event{Type: sync.EvNewOptimisticUpdate, Data: update})
|
|
||||||
},
|
|
||||||
OnFinality: func(update types.FinalityUpdate) {
|
|
||||||
log.Debug("New finality update received", "slot", update.Attested.Slot, "blockRoot", update.Attested.Hash(), "signerCount", update.Signature.SignerCount())
|
|
||||||
eventCallback(request.Event{Type: sync.EvNewFinalityUpdate, Data: update})
|
|
||||||
},
|
|
||||||
OnError: func(err error) {
|
|
||||||
log.Warn("Head event stream error", "err", err)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
s.unsubscribe = s.api.StartHeadListener(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendRequest implements request.requestServer.
|
|
||||||
func (s *ApiServer) SendRequest(id request.ID, req request.Request) {
|
|
||||||
go func() {
|
|
||||||
var resp request.Response
|
|
||||||
var err error
|
|
||||||
switch data := req.(type) {
|
|
||||||
case sync.ReqUpdates:
|
|
||||||
log.Debug("Beacon API: requesting light client update", "reqid", id, "period", data.FirstPeriod, "count", data.Count)
|
|
||||||
var r sync.RespUpdates
|
|
||||||
r.Updates, r.Committees, err = s.api.GetBestUpdatesAndCommittees(data.FirstPeriod, data.Count)
|
|
||||||
resp = r
|
|
||||||
case sync.ReqHeader:
|
|
||||||
var r sync.RespHeader
|
|
||||||
log.Debug("Beacon API: requesting header", "reqid", id, "hash", common.Hash(data))
|
|
||||||
r.Header, r.Canonical, r.Finalized, err = s.api.GetHeader(common.Hash(data))
|
|
||||||
resp = r
|
|
||||||
case sync.ReqCheckpointData:
|
|
||||||
log.Debug("Beacon API: requesting checkpoint data", "reqid", id, "hash", common.Hash(data))
|
|
||||||
resp, err = s.api.GetCheckpointData(common.Hash(data))
|
|
||||||
case sync.ReqBeaconBlock:
|
|
||||||
log.Debug("Beacon API: requesting block", "reqid", id, "hash", common.Hash(data))
|
|
||||||
resp, err = s.api.GetBeaconBlock(common.Hash(data))
|
|
||||||
case sync.ReqFinality:
|
|
||||||
log.Debug("Beacon API: requesting finality update")
|
|
||||||
resp, err = s.api.GetFinalityUpdate()
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("Beacon API request failed", "type", reflect.TypeOf(req), "reqid", id, "err", err)
|
|
||||||
s.eventCallback(request.Event{Type: request.EvFail, Data: request.RequestResponse{ID: id, Request: req}})
|
|
||||||
} else {
|
|
||||||
log.Debug("Beacon API request answered", "type", reflect.TypeOf(req), "reqid", id)
|
|
||||||
s.eventCallback(request.Event{Type: request.EvResponse, Data: request.RequestResponse{ID: id, Request: req, Response: resp}})
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsubscribe implements request.requestServer.
|
|
||||||
// Note: Unsubscribe should not be called concurrently with Subscribe.
|
|
||||||
func (s *ApiServer) Unsubscribe() {
|
|
||||||
if s.unsubscribe != nil {
|
|
||||||
s.unsubscribe()
|
|
||||||
s.unsubscribe = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Name implements request.Server
|
|
||||||
func (s *ApiServer) Name() string {
|
|
||||||
return s.api.url
|
|
||||||
}
|
|
||||||
@@ -1,599 +0,0 @@
|
|||||||
// Copyright 2022 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 detaiapi.
|
|
||||||
//
|
|
||||||
// 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 api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strconv"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/donovanhide/eventsource"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/merkle"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/params"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNotFound = errors.New("404 Not Found")
|
|
||||||
ErrInternal = errors.New("500 Internal Server Error")
|
|
||||||
)
|
|
||||||
|
|
||||||
type CommitteeUpdate struct {
|
|
||||||
Version string
|
|
||||||
Update types.LightClientUpdate
|
|
||||||
NextSyncCommittee types.SerializedSyncCommittee
|
|
||||||
}
|
|
||||||
|
|
||||||
// See data structure definition here:
|
|
||||||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientupdate
|
|
||||||
type committeeUpdateJson struct {
|
|
||||||
Version string `json:"version"`
|
|
||||||
Data committeeUpdateData `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type committeeUpdateData struct {
|
|
||||||
Header jsonBeaconHeader `json:"attested_header"`
|
|
||||||
NextSyncCommittee types.SerializedSyncCommittee `json:"next_sync_committee"`
|
|
||||||
NextSyncCommitteeBranch merkle.Values `json:"next_sync_committee_branch"`
|
|
||||||
FinalizedHeader *jsonBeaconHeader `json:"finalized_header,omitempty"`
|
|
||||||
FinalityBranch merkle.Values `json:"finality_branch,omitempty"`
|
|
||||||
SyncAggregate types.SyncAggregate `json:"sync_aggregate"`
|
|
||||||
SignatureSlot common.Decimal `json:"signature_slot"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type jsonBeaconHeader struct {
|
|
||||||
Beacon types.Header `json:"beacon"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type jsonHeaderWithExecProof struct {
|
|
||||||
Beacon types.Header `json:"beacon"`
|
|
||||||
Execution json.RawMessage `json:"execution"`
|
|
||||||
ExecutionBranch merkle.Values `json:"execution_branch"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON unmarshals from JSON.
|
|
||||||
func (u *CommitteeUpdate) UnmarshalJSON(input []byte) error {
|
|
||||||
var dec committeeUpdateJson
|
|
||||||
if err := json.Unmarshal(input, &dec); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
u.Version = dec.Version
|
|
||||||
u.NextSyncCommittee = dec.Data.NextSyncCommittee
|
|
||||||
u.Update = types.LightClientUpdate{
|
|
||||||
AttestedHeader: types.SignedHeader{
|
|
||||||
Header: dec.Data.Header.Beacon,
|
|
||||||
Signature: dec.Data.SyncAggregate,
|
|
||||||
SignatureSlot: uint64(dec.Data.SignatureSlot),
|
|
||||||
},
|
|
||||||
NextSyncCommitteeRoot: u.NextSyncCommittee.Root(),
|
|
||||||
NextSyncCommitteeBranch: dec.Data.NextSyncCommitteeBranch,
|
|
||||||
FinalityBranch: dec.Data.FinalityBranch,
|
|
||||||
}
|
|
||||||
if dec.Data.FinalizedHeader != nil {
|
|
||||||
u.Update.FinalizedHeader = &dec.Data.FinalizedHeader.Beacon
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fetcher is an interface useful for debug-harnessing the http api.
|
|
||||||
type fetcher interface {
|
|
||||||
Do(req *http.Request) (*http.Response, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeaconLightApi requests light client information from a beacon node REST API.
|
|
||||||
// Note: all required API endpoints are currently only implemented by Lodestar.
|
|
||||||
type BeaconLightApi struct {
|
|
||||||
url string
|
|
||||||
client fetcher
|
|
||||||
customHeaders map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBeaconLightApi(url string, customHeaders map[string]string) *BeaconLightApi {
|
|
||||||
return &BeaconLightApi{
|
|
||||||
url: url,
|
|
||||||
client: &http.Client{
|
|
||||||
Timeout: time.Second * 10,
|
|
||||||
},
|
|
||||||
customHeaders: customHeaders,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *BeaconLightApi) httpGet(path string, params url.Values) ([]byte, error) {
|
|
||||||
uri, err := api.buildURL(path, params)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("GET", uri, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for k, v := range api.customHeaders {
|
|
||||||
req.Header.Set(k, v)
|
|
||||||
}
|
|
||||||
resp, err := api.client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
switch resp.StatusCode {
|
|
||||||
case 200:
|
|
||||||
return io.ReadAll(resp.Body)
|
|
||||||
case 404:
|
|
||||||
return nil, ErrNotFound
|
|
||||||
case 500:
|
|
||||||
return nil, ErrInternal
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unexpected error from API endpoint \"%s\": status code %d", path, resp.StatusCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBestUpdatesAndCommittees fetches and validates LightClientUpdate for given
|
|
||||||
// period and full serialized committee for the next period (committee root hash
|
|
||||||
// equals update.NextSyncCommitteeRoot).
|
|
||||||
// Note that the results are validated but the update signature should be verified
|
|
||||||
// by the caller as its validity depends on the update chain.
|
|
||||||
func (api *BeaconLightApi) GetBestUpdatesAndCommittees(firstPeriod, count uint64) ([]*types.LightClientUpdate, []*types.SerializedSyncCommittee, error) {
|
|
||||||
resp, err := api.httpGet("/eth/v1/beacon/light_client/updates", map[string][]string{
|
|
||||||
"start_period": {strconv.FormatUint(firstPeriod, 10)},
|
|
||||||
"count": {strconv.FormatUint(count, 10)},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var data []CommitteeUpdate
|
|
||||||
if err := json.Unmarshal(resp, &data); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
if len(data) != int(count) {
|
|
||||||
return nil, nil, errors.New("invalid number of committee updates")
|
|
||||||
}
|
|
||||||
updates := make([]*types.LightClientUpdate, int(count))
|
|
||||||
committees := make([]*types.SerializedSyncCommittee, int(count))
|
|
||||||
for i, d := range data {
|
|
||||||
if d.Update.AttestedHeader.Header.SyncPeriod() != firstPeriod+uint64(i) {
|
|
||||||
return nil, nil, errors.New("wrong committee update header period")
|
|
||||||
}
|
|
||||||
if err := d.Update.Validate(); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
if d.NextSyncCommittee.Root() != d.Update.NextSyncCommitteeRoot {
|
|
||||||
return nil, nil, errors.New("wrong sync committee root")
|
|
||||||
}
|
|
||||||
updates[i], committees[i] = new(types.LightClientUpdate), new(types.SerializedSyncCommittee)
|
|
||||||
*updates[i], *committees[i] = d.Update, d.NextSyncCommittee
|
|
||||||
}
|
|
||||||
return updates, committees, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetOptimisticUpdate fetches the latest available optimistic update.
|
|
||||||
// Note that the signature should be verified by the caller as its validity
|
|
||||||
// depends on the update chain.
|
|
||||||
//
|
|
||||||
// See data structure definition here:
|
|
||||||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientoptimisticupdate
|
|
||||||
func (api *BeaconLightApi) GetOptimisticUpdate() (types.OptimisticUpdate, error) {
|
|
||||||
resp, err := api.httpGet("/eth/v1/beacon/light_client/optimistic_update", nil)
|
|
||||||
if err != nil {
|
|
||||||
return types.OptimisticUpdate{}, err
|
|
||||||
}
|
|
||||||
return decodeOptimisticUpdate(resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeOptimisticUpdate(enc []byte) (types.OptimisticUpdate, error) {
|
|
||||||
var data struct {
|
|
||||||
Version string
|
|
||||||
Data struct {
|
|
||||||
Attested jsonHeaderWithExecProof `json:"attested_header"`
|
|
||||||
Aggregate types.SyncAggregate `json:"sync_aggregate"`
|
|
||||||
SignatureSlot common.Decimal `json:"signature_slot"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(enc, &data); err != nil {
|
|
||||||
return types.OptimisticUpdate{}, err
|
|
||||||
}
|
|
||||||
// Decode the execution payload headers.
|
|
||||||
attestedExecHeader, err := types.ExecutionHeaderFromJSON(data.Version, data.Data.Attested.Execution)
|
|
||||||
if err != nil {
|
|
||||||
return types.OptimisticUpdate{}, fmt.Errorf("invalid attested header: %v", err)
|
|
||||||
}
|
|
||||||
if data.Data.Attested.Beacon.StateRoot == (common.Hash{}) {
|
|
||||||
// workaround for different event encoding format in Lodestar
|
|
||||||
if err := json.Unmarshal(enc, &data.Data); err != nil {
|
|
||||||
return types.OptimisticUpdate{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data.Data.Aggregate.Signers) != params.SyncCommitteeBitmaskSize {
|
|
||||||
return types.OptimisticUpdate{}, errors.New("invalid sync_committee_bits length")
|
|
||||||
}
|
|
||||||
if len(data.Data.Aggregate.Signature) != params.BLSSignatureSize {
|
|
||||||
return types.OptimisticUpdate{}, errors.New("invalid sync_committee_signature length")
|
|
||||||
}
|
|
||||||
return types.OptimisticUpdate{
|
|
||||||
Attested: types.HeaderWithExecProof{
|
|
||||||
Header: data.Data.Attested.Beacon,
|
|
||||||
PayloadHeader: attestedExecHeader,
|
|
||||||
PayloadBranch: data.Data.Attested.ExecutionBranch,
|
|
||||||
},
|
|
||||||
Signature: data.Data.Aggregate,
|
|
||||||
SignatureSlot: uint64(data.Data.SignatureSlot),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFinalityUpdate fetches the latest available finality update.
|
|
||||||
//
|
|
||||||
// See data structure definition here:
|
|
||||||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientfinalityupdate
|
|
||||||
func (api *BeaconLightApi) GetFinalityUpdate() (types.FinalityUpdate, error) {
|
|
||||||
resp, err := api.httpGet("/eth/v1/beacon/light_client/finality_update", nil)
|
|
||||||
if err != nil {
|
|
||||||
return types.FinalityUpdate{}, err
|
|
||||||
}
|
|
||||||
return decodeFinalityUpdate(resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeFinalityUpdate(enc []byte) (types.FinalityUpdate, error) {
|
|
||||||
var data struct {
|
|
||||||
Version string
|
|
||||||
Data struct {
|
|
||||||
Attested jsonHeaderWithExecProof `json:"attested_header"`
|
|
||||||
Finalized jsonHeaderWithExecProof `json:"finalized_header"`
|
|
||||||
FinalityBranch merkle.Values `json:"finality_branch"`
|
|
||||||
Aggregate types.SyncAggregate `json:"sync_aggregate"`
|
|
||||||
SignatureSlot common.Decimal `json:"signature_slot"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(enc, &data); err != nil {
|
|
||||||
return types.FinalityUpdate{}, err
|
|
||||||
}
|
|
||||||
// Decode the execution payload headers.
|
|
||||||
attestedExecHeader, err := types.ExecutionHeaderFromJSON(data.Version, data.Data.Attested.Execution)
|
|
||||||
if err != nil {
|
|
||||||
return types.FinalityUpdate{}, fmt.Errorf("invalid attested header: %v", err)
|
|
||||||
}
|
|
||||||
finalizedExecHeader, err := types.ExecutionHeaderFromJSON(data.Version, data.Data.Finalized.Execution)
|
|
||||||
if err != nil {
|
|
||||||
return types.FinalityUpdate{}, fmt.Errorf("invalid finalized header: %v", err)
|
|
||||||
}
|
|
||||||
// Perform sanity checks.
|
|
||||||
if len(data.Data.Aggregate.Signers) != params.SyncCommitteeBitmaskSize {
|
|
||||||
return types.FinalityUpdate{}, errors.New("invalid sync_committee_bits length")
|
|
||||||
}
|
|
||||||
if len(data.Data.Aggregate.Signature) != params.BLSSignatureSize {
|
|
||||||
return types.FinalityUpdate{}, errors.New("invalid sync_committee_signature length")
|
|
||||||
}
|
|
||||||
|
|
||||||
return types.FinalityUpdate{
|
|
||||||
Attested: types.HeaderWithExecProof{
|
|
||||||
Header: data.Data.Attested.Beacon,
|
|
||||||
PayloadHeader: attestedExecHeader,
|
|
||||||
PayloadBranch: data.Data.Attested.ExecutionBranch,
|
|
||||||
},
|
|
||||||
Finalized: types.HeaderWithExecProof{
|
|
||||||
Header: data.Data.Finalized.Beacon,
|
|
||||||
PayloadHeader: finalizedExecHeader,
|
|
||||||
PayloadBranch: data.Data.Finalized.ExecutionBranch,
|
|
||||||
},
|
|
||||||
FinalityBranch: data.Data.FinalityBranch,
|
|
||||||
Signature: data.Data.Aggregate,
|
|
||||||
SignatureSlot: uint64(data.Data.SignatureSlot),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetHeader fetches and validates the beacon header with the given blockRoot.
|
|
||||||
// If blockRoot is null hash then the latest head header is fetched.
|
|
||||||
// The values of the canonical and finalized flags are also returned. Note that
|
|
||||||
// these flags are not validated.
|
|
||||||
func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, bool, bool, error) {
|
|
||||||
var blockId string
|
|
||||||
if blockRoot == (common.Hash{}) {
|
|
||||||
blockId = "head"
|
|
||||||
} else {
|
|
||||||
blockId = blockRoot.Hex()
|
|
||||||
}
|
|
||||||
resp, err := api.httpGet(fmt.Sprintf("/eth/v1/beacon/headers/%s", blockId), nil)
|
|
||||||
if err != nil {
|
|
||||||
return types.Header{}, false, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var data struct {
|
|
||||||
Finalized bool `json:"finalized"`
|
|
||||||
Data struct {
|
|
||||||
Root common.Hash `json:"root"`
|
|
||||||
Canonical bool `json:"canonical"`
|
|
||||||
Header struct {
|
|
||||||
Message types.Header `json:"message"`
|
|
||||||
Signature hexutil.Bytes `json:"signature"`
|
|
||||||
} `json:"header"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(resp, &data); err != nil {
|
|
||||||
return types.Header{}, false, false, err
|
|
||||||
}
|
|
||||||
header := data.Data.Header.Message
|
|
||||||
if blockRoot == (common.Hash{}) {
|
|
||||||
blockRoot = data.Data.Root
|
|
||||||
}
|
|
||||||
if header.Hash() != blockRoot {
|
|
||||||
return types.Header{}, false, false, errors.New("retrieved beacon header root does not match")
|
|
||||||
}
|
|
||||||
return header, data.Data.Canonical, data.Finalized, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCheckpointData fetches and validates bootstrap data belonging to the given checkpoint.
|
|
||||||
func (api *BeaconLightApi) GetCheckpointData(checkpointHash common.Hash) (*types.BootstrapData, error) {
|
|
||||||
resp, err := api.httpGet(fmt.Sprintf("/eth/v1/beacon/light_client/bootstrap/0x%x", checkpointHash[:]), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// See data structure definition here:
|
|
||||||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientbootstrap
|
|
||||||
type bootstrapData struct {
|
|
||||||
Data struct {
|
|
||||||
Header jsonBeaconHeader `json:"header"`
|
|
||||||
Committee *types.SerializedSyncCommittee `json:"current_sync_committee"`
|
|
||||||
CommitteeBranch merkle.Values `json:"current_sync_committee_branch"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var data bootstrapData
|
|
||||||
if err := json.Unmarshal(resp, &data); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if data.Data.Committee == nil {
|
|
||||||
return nil, errors.New("sync committee is missing")
|
|
||||||
}
|
|
||||||
header := data.Data.Header.Beacon
|
|
||||||
if header.Hash() != checkpointHash {
|
|
||||||
return nil, fmt.Errorf("invalid checkpoint block header, have %v want %v", header.Hash(), checkpointHash)
|
|
||||||
}
|
|
||||||
checkpoint := &types.BootstrapData{
|
|
||||||
Header: header,
|
|
||||||
CommitteeBranch: data.Data.CommitteeBranch,
|
|
||||||
CommitteeRoot: data.Data.Committee.Root(),
|
|
||||||
Committee: data.Data.Committee,
|
|
||||||
}
|
|
||||||
if err := checkpoint.Validate(); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid checkpoint: %w", err)
|
|
||||||
}
|
|
||||||
if checkpoint.Header.Hash() != checkpointHash {
|
|
||||||
return nil, errors.New("wrong checkpoint hash")
|
|
||||||
}
|
|
||||||
return checkpoint, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *BeaconLightApi) GetBeaconBlock(blockRoot common.Hash) (*types.BeaconBlock, error) {
|
|
||||||
resp, err := api.httpGet(fmt.Sprintf("/eth/v2/beacon/blocks/0x%x", blockRoot), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var beaconBlockMessage struct {
|
|
||||||
Version string
|
|
||||||
Data struct {
|
|
||||||
Message json.RawMessage `json:"message"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(resp, &beaconBlockMessage); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid block json data: %v", err)
|
|
||||||
}
|
|
||||||
block, err := types.BlockFromJSON(beaconBlockMessage.Version, beaconBlockMessage.Data.Message)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
computedRoot := block.Root()
|
|
||||||
if computedRoot != blockRoot {
|
|
||||||
return nil, fmt.Errorf("Beacon block root hash mismatch (expected: %x, got: %x)", blockRoot, computedRoot)
|
|
||||||
}
|
|
||||||
return block, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeHeadEvent(enc []byte) (uint64, common.Hash, error) {
|
|
||||||
var data struct {
|
|
||||||
Slot common.Decimal `json:"slot"`
|
|
||||||
Block common.Hash `json:"block"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(enc, &data); err != nil {
|
|
||||||
return 0, common.Hash{}, err
|
|
||||||
}
|
|
||||||
return uint64(data.Slot), data.Block, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type HeadEventListener struct {
|
|
||||||
OnNewHead func(slot uint64, blockRoot common.Hash)
|
|
||||||
OnOptimistic func(head types.OptimisticUpdate)
|
|
||||||
OnFinality func(head types.FinalityUpdate)
|
|
||||||
OnError func(err error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartHeadListener creates an event subscription for heads and signed (optimistic)
|
|
||||||
// head updates and calls the specified callback functions when they are received.
|
|
||||||
// The callbacks are also called for the current head and optimistic head at startup.
|
|
||||||
// They are never called concurrently.
|
|
||||||
func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func() {
|
|
||||||
var (
|
|
||||||
ctx, closeCtx = context.WithCancel(context.Background())
|
|
||||||
streamCh = make(chan *eventsource.Stream, 1)
|
|
||||||
wg sync.WaitGroup
|
|
||||||
)
|
|
||||||
|
|
||||||
// When connected to a Lodestar node the subscription blocks until the first actual
|
|
||||||
// event arrives; therefore we create the subscription in a separate goroutine while
|
|
||||||
// letting the main goroutine sync up to the current head.
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
stream := api.startEventStream(ctx, &listener)
|
|
||||||
if stream == nil {
|
|
||||||
// This case happens when the context was closed.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Stream was opened, wait for close signal.
|
|
||||||
streamCh <- stream
|
|
||||||
<-ctx.Done()
|
|
||||||
stream.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
// Request initial data.
|
|
||||||
log.Trace("Requesting initial head header")
|
|
||||||
if head, _, _, err := api.GetHeader(common.Hash{}); err == nil {
|
|
||||||
log.Trace("Retrieved initial head header", "slot", head.Slot, "hash", head.Hash())
|
|
||||||
listener.OnNewHead(head.Slot, head.Hash())
|
|
||||||
} else {
|
|
||||||
log.Debug("Failed to retrieve initial head header", "error", err)
|
|
||||||
}
|
|
||||||
log.Trace("Requesting initial optimistic update")
|
|
||||||
if optimisticUpdate, err := api.GetOptimisticUpdate(); err == nil {
|
|
||||||
log.Trace("Retrieved initial optimistic update", "slot", optimisticUpdate.Attested.Slot, "hash", optimisticUpdate.Attested.Hash())
|
|
||||||
listener.OnOptimistic(optimisticUpdate)
|
|
||||||
} else {
|
|
||||||
log.Debug("Failed to retrieve initial optimistic update", "error", err)
|
|
||||||
}
|
|
||||||
log.Trace("Requesting initial finality update")
|
|
||||||
if finalityUpdate, err := api.GetFinalityUpdate(); err == nil {
|
|
||||||
log.Trace("Retrieved initial finality update", "slot", finalityUpdate.Finalized.Slot, "hash", finalityUpdate.Finalized.Hash())
|
|
||||||
listener.OnFinality(finalityUpdate)
|
|
||||||
} else {
|
|
||||||
log.Debug("Failed to retrieve initial finality update", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Trace("Starting event stream processing loop")
|
|
||||||
// Receive the stream.
|
|
||||||
var stream *eventsource.Stream
|
|
||||||
select {
|
|
||||||
case stream = <-streamCh:
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Trace("Stopping event stream processing loop")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case event, ok := <-stream.Events:
|
|
||||||
if !ok {
|
|
||||||
log.Trace("Event stream closed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Trace("New event received from event stream", "type", event.Event())
|
|
||||||
switch event.Event() {
|
|
||||||
case "head":
|
|
||||||
slot, blockRoot, err := decodeHeadEvent([]byte(event.Data()))
|
|
||||||
if err == nil {
|
|
||||||
listener.OnNewHead(slot, blockRoot)
|
|
||||||
} else {
|
|
||||||
listener.OnError(fmt.Errorf("error decoding head event: %v", err))
|
|
||||||
}
|
|
||||||
case "light_client_optimistic_update":
|
|
||||||
optimisticUpdate, err := decodeOptimisticUpdate([]byte(event.Data()))
|
|
||||||
if err == nil {
|
|
||||||
listener.OnOptimistic(optimisticUpdate)
|
|
||||||
} else {
|
|
||||||
listener.OnError(fmt.Errorf("error decoding optimistic update event: %v", err))
|
|
||||||
}
|
|
||||||
case "light_client_finality_update":
|
|
||||||
finalityUpdate, err := decodeFinalityUpdate([]byte(event.Data()))
|
|
||||||
if err == nil {
|
|
||||||
listener.OnFinality(finalityUpdate)
|
|
||||||
} else {
|
|
||||||
listener.OnError(fmt.Errorf("error decoding finality update event: %v", err))
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
listener.OnError(fmt.Errorf("unexpected event: %s", event.Event()))
|
|
||||||
}
|
|
||||||
|
|
||||||
case err, ok := <-stream.Errors:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
listener.OnError(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return func() {
|
|
||||||
closeCtx()
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// startEventStream establishes an event stream. This will keep retrying until the stream has been
|
|
||||||
// established. It can only return nil when the context is canceled.
|
|
||||||
func (api *BeaconLightApi) startEventStream(ctx context.Context, listener *HeadEventListener) *eventsource.Stream {
|
|
||||||
for retry := true; retry; retry = ctxSleep(ctx, 5*time.Second) {
|
|
||||||
log.Trace("Sending event subscription request")
|
|
||||||
uri, err := api.buildURL("/eth/v1/events", map[string][]string{"topics": {"head", "light_client_finality_update", "light_client_optimistic_update"}})
|
|
||||||
if err != nil {
|
|
||||||
listener.OnError(fmt.Errorf("error creating event subscription URL: %v", err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
|
|
||||||
if err != nil {
|
|
||||||
listener.OnError(fmt.Errorf("error creating event subscription request: %v", err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for k, v := range api.customHeaders {
|
|
||||||
req.Header.Set(k, v)
|
|
||||||
}
|
|
||||||
stream, err := eventsource.SubscribeWithRequest("", req)
|
|
||||||
if err != nil {
|
|
||||||
listener.OnError(fmt.Errorf("error creating event subscription: %v", err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Trace("Successfully created event stream")
|
|
||||||
return stream
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ctxSleep(ctx context.Context, timeout time.Duration) (ok bool) {
|
|
||||||
timer := time.NewTimer(timeout)
|
|
||||||
defer timer.Stop()
|
|
||||||
select {
|
|
||||||
case <-timer.C:
|
|
||||||
return true
|
|
||||||
case <-ctx.Done():
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *BeaconLightApi) buildURL(path string, params url.Values) (string, error) {
|
|
||||||
uri, err := url.Parse(api.url)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
uri = uri.JoinPath(path)
|
|
||||||
if params != nil {
|
|
||||||
uri.RawQuery = params.Encode()
|
|
||||||
}
|
|
||||||
return uri.String(), nil
|
|
||||||
}
|
|
||||||
@@ -1,125 +0,0 @@
|
|||||||
// Copyright 2023 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 light
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/lru"
|
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// canonicalStore stores instances of the given type in a database and caches
|
|
||||||
// them in memory, associated with a continuous range of period numbers.
|
|
||||||
// Note: canonicalStore is not thread safe and it is the caller's responsibility
|
|
||||||
// to avoid concurrent access.
|
|
||||||
type canonicalStore[T any] struct {
|
|
||||||
keyPrefix []byte
|
|
||||||
periods periodRange
|
|
||||||
cache *lru.Cache[uint64, T]
|
|
||||||
}
|
|
||||||
|
|
||||||
// newCanonicalStore creates a new canonicalStore and loads all keys associated
|
|
||||||
// with the keyPrefix in order to determine the ranges available in the database.
|
|
||||||
func newCanonicalStore[T any](db ethdb.Iteratee, keyPrefix []byte) (*canonicalStore[T], error) {
|
|
||||||
cs := &canonicalStore[T]{
|
|
||||||
keyPrefix: keyPrefix,
|
|
||||||
cache: lru.NewCache[uint64, T](100),
|
|
||||||
}
|
|
||||||
var (
|
|
||||||
iter = db.NewIterator(keyPrefix, nil)
|
|
||||||
kl = len(keyPrefix)
|
|
||||||
first = true
|
|
||||||
)
|
|
||||||
defer iter.Release()
|
|
||||||
|
|
||||||
for iter.Next() {
|
|
||||||
if len(iter.Key()) != kl+8 {
|
|
||||||
log.Warn("Invalid key length in the canonical chain database", "key", fmt.Sprintf("%#x", iter.Key()))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
period := binary.BigEndian.Uint64(iter.Key()[kl : kl+8])
|
|
||||||
if first {
|
|
||||||
cs.periods.Start = period
|
|
||||||
} else if cs.periods.End != period {
|
|
||||||
return nil, fmt.Errorf("gap in the canonical chain database between periods %d and %d", cs.periods.End, period-1)
|
|
||||||
}
|
|
||||||
first = false
|
|
||||||
cs.periods.End = period + 1
|
|
||||||
}
|
|
||||||
return cs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// databaseKey returns the database key belonging to the given period.
|
|
||||||
func (cs *canonicalStore[T]) databaseKey(period uint64) []byte {
|
|
||||||
return binary.BigEndian.AppendUint64(append([]byte{}, cs.keyPrefix...), period)
|
|
||||||
}
|
|
||||||
|
|
||||||
// add adds the given item to the database. It also ensures that the range remains
|
|
||||||
// continuous. Can be used either with a batch or database backend.
|
|
||||||
func (cs *canonicalStore[T]) add(backend ethdb.KeyValueWriter, period uint64, value T) error {
|
|
||||||
if !cs.periods.canExpand(period) {
|
|
||||||
return fmt.Errorf("period expansion is not allowed, first: %d, next: %d, period: %d", cs.periods.Start, cs.periods.End, period)
|
|
||||||
}
|
|
||||||
enc, err := rlp.EncodeToBytes(value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := backend.Put(cs.databaseKey(period), enc); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cs.cache.Add(period, value)
|
|
||||||
cs.periods.expand(period)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// deleteFrom removes items starting from the given period.
|
|
||||||
func (cs *canonicalStore[T]) deleteFrom(db ethdb.KeyValueWriter, fromPeriod uint64) (deleted periodRange) {
|
|
||||||
keepRange, deleteRange := cs.periods.split(fromPeriod)
|
|
||||||
deleteRange.each(func(period uint64) {
|
|
||||||
db.Delete(cs.databaseKey(period))
|
|
||||||
cs.cache.Remove(period)
|
|
||||||
})
|
|
||||||
cs.periods = keepRange
|
|
||||||
return deleteRange
|
|
||||||
}
|
|
||||||
|
|
||||||
// get returns the item at the given period or the null value of the given type
|
|
||||||
// if no item is present.
|
|
||||||
func (cs *canonicalStore[T]) get(backend ethdb.KeyValueReader, period uint64) (T, bool) {
|
|
||||||
var null, value T
|
|
||||||
if !cs.periods.contains(period) {
|
|
||||||
return null, false
|
|
||||||
}
|
|
||||||
if value, ok := cs.cache.Get(period); ok {
|
|
||||||
return value, true
|
|
||||||
}
|
|
||||||
enc, err := backend.Get(cs.databaseKey(period))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Canonical store value not found", "period", period, "start", cs.periods.Start, "end", cs.periods.End)
|
|
||||||
return null, false
|
|
||||||
}
|
|
||||||
if err := rlp.DecodeBytes(enc, &value); err != nil {
|
|
||||||
log.Error("Error decoding canonical store value", "error", err)
|
|
||||||
return null, false
|
|
||||||
}
|
|
||||||
cs.cache.Add(period, value)
|
|
||||||
return value, true
|
|
||||||
}
|
|
||||||
@@ -1,527 +0,0 @@
|
|||||||
// Copyright 2023 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 light
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/params"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/lru"
|
|
||||||
"github.com/ethereum/go-ethereum/common/mclock"
|
|
||||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNeedCommittee = errors.New("sync committee required")
|
|
||||||
ErrInvalidUpdate = errors.New("invalid committee update")
|
|
||||||
ErrInvalidPeriod = errors.New("invalid update period")
|
|
||||||
ErrWrongCommitteeRoot = errors.New("wrong committee root")
|
|
||||||
ErrCannotReorg = errors.New("can not reorg committee chain")
|
|
||||||
)
|
|
||||||
|
|
||||||
// CommitteeChain is a passive data structure that can validate, hold and update
|
|
||||||
// a chain of beacon light sync committees and updates. It requires at least one
|
|
||||||
// externally set fixed committee root at the beginning of the chain which can
|
|
||||||
// be set either based on a BootstrapData or a trusted source (a local beacon
|
|
||||||
// full node). This makes the structure useful for both light client and light
|
|
||||||
// server setups.
|
|
||||||
//
|
|
||||||
// It always maintains the following consistency constraints:
|
|
||||||
// - a committee can only be present if its root hash matches an existing fixed
|
|
||||||
// root or if it is proven by an update at the previous period
|
|
||||||
// - an update can only be present if a committee is present at the same period
|
|
||||||
// and the update signature is valid and has enough participants.
|
|
||||||
// The committee at the next period (proven by the update) should also be
|
|
||||||
// present (note that this means they can only be added together if neither
|
|
||||||
// is present yet). If a fixed root is present at the next period then the
|
|
||||||
// update can only be present if it proves the same committee root.
|
|
||||||
//
|
|
||||||
// Once synced to the current sync period, CommitteeChain can also validate
|
|
||||||
// signed beacon headers.
|
|
||||||
type CommitteeChain struct {
|
|
||||||
// chainmu guards against concurrent access to the canonicalStore structures
|
|
||||||
// (updates, committees, fixedCommitteeRoots) and ensures that they stay consistent
|
|
||||||
// with each other and with committeeCache.
|
|
||||||
chainmu sync.RWMutex
|
|
||||||
db ethdb.KeyValueStore
|
|
||||||
updates *canonicalStore[*types.LightClientUpdate]
|
|
||||||
committees *canonicalStore[*types.SerializedSyncCommittee]
|
|
||||||
fixedCommitteeRoots *canonicalStore[common.Hash]
|
|
||||||
committeeCache *lru.Cache[uint64, syncCommittee] // cache deserialized committees
|
|
||||||
changeCounter uint64
|
|
||||||
|
|
||||||
clock mclock.Clock // monotonic clock (simulated clock in tests)
|
|
||||||
unixNano func() int64 // system clock (simulated clock in tests)
|
|
||||||
sigVerifier committeeSigVerifier // BLS sig verifier (dummy verifier in tests)
|
|
||||||
|
|
||||||
config *params.ChainConfig
|
|
||||||
minimumUpdateScore types.UpdateScore
|
|
||||||
enforceTime bool // enforceTime specifies whether the age of a signed header should be checked
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewCommitteeChain creates a new CommitteeChain.
|
|
||||||
func NewCommitteeChain(db ethdb.KeyValueStore, config *params.ChainConfig, signerThreshold int, enforceTime bool) *CommitteeChain {
|
|
||||||
return newCommitteeChain(db, config, signerThreshold, enforceTime, blsVerifier{}, &mclock.System{}, func() int64 { return time.Now().UnixNano() })
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTestCommitteeChain creates a new CommitteeChain for testing.
|
|
||||||
func NewTestCommitteeChain(db ethdb.KeyValueStore, config *params.ChainConfig, signerThreshold int, enforceTime bool, clock *mclock.Simulated) *CommitteeChain {
|
|
||||||
return newCommitteeChain(db, config, signerThreshold, enforceTime, dummyVerifier{}, clock, func() int64 { return int64(clock.Now()) })
|
|
||||||
}
|
|
||||||
|
|
||||||
// newCommitteeChain creates a new CommitteeChain with the option of replacing the
|
|
||||||
// clock source and signature verification for testing purposes.
|
|
||||||
func newCommitteeChain(db ethdb.KeyValueStore, config *params.ChainConfig, signerThreshold int, enforceTime bool, sigVerifier committeeSigVerifier, clock mclock.Clock, unixNano func() int64) *CommitteeChain {
|
|
||||||
s := &CommitteeChain{
|
|
||||||
committeeCache: lru.NewCache[uint64, syncCommittee](10),
|
|
||||||
db: db,
|
|
||||||
sigVerifier: sigVerifier,
|
|
||||||
clock: clock,
|
|
||||||
unixNano: unixNano,
|
|
||||||
config: config,
|
|
||||||
enforceTime: enforceTime,
|
|
||||||
minimumUpdateScore: types.UpdateScore{
|
|
||||||
SignerCount: uint32(signerThreshold),
|
|
||||||
SubPeriodIndex: params.SyncPeriodLength / 16,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var err1, err2, err3 error
|
|
||||||
if s.fixedCommitteeRoots, err1 = newCanonicalStore[common.Hash](db, rawdb.FixedCommitteeRootKey); err1 != nil {
|
|
||||||
log.Error("Error creating fixed committee root store", "error", err1)
|
|
||||||
}
|
|
||||||
if s.committees, err2 = newCanonicalStore[*types.SerializedSyncCommittee](db, rawdb.SyncCommitteeKey); err2 != nil {
|
|
||||||
log.Error("Error creating committee store", "error", err2)
|
|
||||||
}
|
|
||||||
if s.updates, err3 = newCanonicalStore[*types.LightClientUpdate](db, rawdb.BestUpdateKey); err3 != nil {
|
|
||||||
log.Error("Error creating update store", "error", err3)
|
|
||||||
}
|
|
||||||
if err1 != nil || err2 != nil || err3 != nil || !s.checkConstraints() {
|
|
||||||
log.Info("Resetting invalid committee chain")
|
|
||||||
s.Reset()
|
|
||||||
}
|
|
||||||
// roll back invalid updates (might be necessary if forks have been changed since last time)
|
|
||||||
for !s.updates.periods.isEmpty() {
|
|
||||||
update, ok := s.updates.get(s.db, s.updates.periods.End-1)
|
|
||||||
if !ok {
|
|
||||||
log.Error("Sync committee update missing", "period", s.updates.periods.End-1)
|
|
||||||
s.Reset()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if valid, err := s.verifyUpdate(update); err != nil {
|
|
||||||
log.Error("Error validating update", "period", s.updates.periods.End-1, "error", err)
|
|
||||||
} else if valid {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err := s.rollback(s.updates.periods.End); err != nil {
|
|
||||||
log.Error("Error writing batch into chain database", "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !s.committees.periods.isEmpty() {
|
|
||||||
log.Trace("Sync committee chain loaded", "first period", s.committees.periods.Start, "last period", s.committees.periods.End-1)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// checkConstraints checks committee chain validity constraints
|
|
||||||
func (s *CommitteeChain) checkConstraints() bool {
|
|
||||||
isNotInFixedCommitteeRootRange := func(r periodRange) bool {
|
|
||||||
return s.fixedCommitteeRoots.periods.isEmpty() ||
|
|
||||||
r.Start < s.fixedCommitteeRoots.periods.Start ||
|
|
||||||
r.Start >= s.fixedCommitteeRoots.periods.End
|
|
||||||
}
|
|
||||||
|
|
||||||
valid := true
|
|
||||||
if !s.updates.periods.isEmpty() {
|
|
||||||
if isNotInFixedCommitteeRootRange(s.updates.periods) {
|
|
||||||
log.Error("Start update is not in the fixed roots range")
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
if s.committees.periods.Start > s.updates.periods.Start || s.committees.periods.End <= s.updates.periods.End {
|
|
||||||
log.Error("Missing committees in update range")
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !s.committees.periods.isEmpty() {
|
|
||||||
if isNotInFixedCommitteeRootRange(s.committees.periods) {
|
|
||||||
log.Error("Start committee is not in the fixed roots range")
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
if s.committees.periods.End > s.fixedCommitteeRoots.periods.End && s.committees.periods.End > s.updates.periods.End+1 {
|
|
||||||
log.Error("Last committee is neither in the fixed roots range nor proven by updates")
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return valid
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset resets the committee chain.
|
|
||||||
func (s *CommitteeChain) Reset() {
|
|
||||||
s.chainmu.Lock()
|
|
||||||
defer s.chainmu.Unlock()
|
|
||||||
|
|
||||||
if err := s.rollback(0); err != nil {
|
|
||||||
log.Error("Error writing batch into chain database", "error", err)
|
|
||||||
}
|
|
||||||
s.changeCounter++
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckpointInit initializes a CommitteeChain based on a checkpoint.
|
|
||||||
// Note: if the chain is already initialized and the committees proven by the
|
|
||||||
// checkpoint do match the existing chain then the chain is retained and the
|
|
||||||
// new checkpoint becomes fixed.
|
|
||||||
func (s *CommitteeChain) CheckpointInit(bootstrap types.BootstrapData) error {
|
|
||||||
s.chainmu.Lock()
|
|
||||||
defer s.chainmu.Unlock()
|
|
||||||
|
|
||||||
if err := bootstrap.Validate(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
period := bootstrap.Header.SyncPeriod()
|
|
||||||
if err := s.deleteFixedCommitteeRootsFrom(period + 2); err != nil {
|
|
||||||
s.Reset()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if s.addFixedCommitteeRoot(period, bootstrap.CommitteeRoot) != nil {
|
|
||||||
s.Reset()
|
|
||||||
if err := s.addFixedCommitteeRoot(period, bootstrap.CommitteeRoot); err != nil {
|
|
||||||
s.Reset()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := s.addFixedCommitteeRoot(period+1, common.Hash(bootstrap.CommitteeBranch[0])); err != nil {
|
|
||||||
s.Reset()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.addCommittee(period, bootstrap.Committee); err != nil {
|
|
||||||
s.Reset()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.changeCounter++
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// addFixedCommitteeRoot sets a fixed committee root at the given period.
|
|
||||||
// Note that the period where the first committee is added has to have a fixed
|
|
||||||
// root which can either come from a BootstrapData or a trusted source.
|
|
||||||
func (s *CommitteeChain) addFixedCommitteeRoot(period uint64, root common.Hash) error {
|
|
||||||
if root == (common.Hash{}) {
|
|
||||||
return ErrWrongCommitteeRoot
|
|
||||||
}
|
|
||||||
|
|
||||||
batch := s.db.NewBatch()
|
|
||||||
oldRoot := s.getCommitteeRoot(period)
|
|
||||||
if !s.fixedCommitteeRoots.periods.canExpand(period) {
|
|
||||||
// Note: the fixed committee root range should always be continuous and
|
|
||||||
// therefore the expected syncing method is to forward sync and optionally
|
|
||||||
// backward sync periods one by one, starting from a checkpoint. The only
|
|
||||||
// case when a root that is not adjacent to the already fixed ones can be
|
|
||||||
// fixed is when the same root has already been proven by an update chain.
|
|
||||||
// In this case the all roots in between can and should be fixed.
|
|
||||||
// This scenario makes sense when a new trusted checkpoint is added to an
|
|
||||||
// existing chain, ensuring that it will not be rolled back (might be
|
|
||||||
// important in case of low signer participation rate).
|
|
||||||
if root != oldRoot {
|
|
||||||
return ErrInvalidPeriod
|
|
||||||
}
|
|
||||||
// if the old root exists and matches the new one then it is guaranteed
|
|
||||||
// that the given period is after the existing fixed range and the roots
|
|
||||||
// in between can also be fixed.
|
|
||||||
for p := s.fixedCommitteeRoots.periods.End; p < period; p++ {
|
|
||||||
if err := s.fixedCommitteeRoots.add(batch, p, s.getCommitteeRoot(p)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if oldRoot != (common.Hash{}) && (oldRoot != root) {
|
|
||||||
// existing old root was different, we have to reorg the chain
|
|
||||||
if err := s.rollback(period); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := s.fixedCommitteeRoots.add(batch, period, root); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := batch.Write(); err != nil {
|
|
||||||
log.Error("Error writing batch into chain database", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// deleteFixedCommitteeRootsFrom deletes fixed roots starting from the given period.
|
|
||||||
// It also maintains chain consistency, meaning that it also deletes updates and
|
|
||||||
// committees if they are no longer supported by a valid update chain.
|
|
||||||
func (s *CommitteeChain) deleteFixedCommitteeRootsFrom(period uint64) error {
|
|
||||||
if period >= s.fixedCommitteeRoots.periods.End {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
batch := s.db.NewBatch()
|
|
||||||
s.fixedCommitteeRoots.deleteFrom(batch, period)
|
|
||||||
if s.updates.periods.isEmpty() || period <= s.updates.periods.Start {
|
|
||||||
// Note: the first period of the update chain should always be fixed so if
|
|
||||||
// the fixed root at the first update is removed then the entire update chain
|
|
||||||
// and the proven committees have to be removed. Earlier committees in the
|
|
||||||
// remaining fixed root range can stay.
|
|
||||||
s.updates.deleteFrom(batch, period)
|
|
||||||
s.deleteCommitteesFrom(batch, period)
|
|
||||||
} else {
|
|
||||||
// The update chain stays intact, some previously fixed committee roots might
|
|
||||||
// get unfixed but are still proven by the update chain. If there were
|
|
||||||
// committees present after the range proven by updates, those should be
|
|
||||||
// removed if the belonging fixed roots are also removed.
|
|
||||||
fromPeriod := s.updates.periods.End + 1 // not proven by updates
|
|
||||||
if period > fromPeriod {
|
|
||||||
fromPeriod = period // also not justified by fixed roots
|
|
||||||
}
|
|
||||||
s.deleteCommitteesFrom(batch, fromPeriod)
|
|
||||||
}
|
|
||||||
if err := batch.Write(); err != nil {
|
|
||||||
log.Error("Error writing batch into chain database", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// deleteCommitteesFrom deletes committees starting from the given period.
|
|
||||||
func (s *CommitteeChain) deleteCommitteesFrom(batch ethdb.Batch, period uint64) {
|
|
||||||
deleted := s.committees.deleteFrom(batch, period)
|
|
||||||
for period := deleted.Start; period < deleted.End; period++ {
|
|
||||||
s.committeeCache.Remove(period)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// addCommittee adds a committee at the given period if possible.
|
|
||||||
func (s *CommitteeChain) addCommittee(period uint64, committee *types.SerializedSyncCommittee) error {
|
|
||||||
if !s.committees.periods.canExpand(period) {
|
|
||||||
return ErrInvalidPeriod
|
|
||||||
}
|
|
||||||
root := s.getCommitteeRoot(period)
|
|
||||||
if root == (common.Hash{}) {
|
|
||||||
return ErrInvalidPeriod
|
|
||||||
}
|
|
||||||
if root != committee.Root() {
|
|
||||||
return ErrWrongCommitteeRoot
|
|
||||||
}
|
|
||||||
if !s.committees.periods.contains(period) {
|
|
||||||
if err := s.committees.add(s.db, period, committee); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.committeeCache.Remove(period)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// InsertUpdate adds a new update if possible.
|
|
||||||
func (s *CommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommittee *types.SerializedSyncCommittee) error {
|
|
||||||
s.chainmu.Lock()
|
|
||||||
defer s.chainmu.Unlock()
|
|
||||||
|
|
||||||
period := update.AttestedHeader.Header.SyncPeriod()
|
|
||||||
if !s.updates.periods.canExpand(period) || !s.committees.periods.contains(period) {
|
|
||||||
return ErrInvalidPeriod
|
|
||||||
}
|
|
||||||
if s.minimumUpdateScore.BetterThan(update.Score()) {
|
|
||||||
return ErrInvalidUpdate
|
|
||||||
}
|
|
||||||
oldRoot := s.getCommitteeRoot(period + 1)
|
|
||||||
reorg := oldRoot != (common.Hash{}) && oldRoot != update.NextSyncCommitteeRoot
|
|
||||||
if oldUpdate, ok := s.updates.get(s.db, period); ok && !update.Score().BetterThan(oldUpdate.Score()) {
|
|
||||||
// a better or equal update already exists; no changes, only fail if new one tried to reorg
|
|
||||||
if reorg {
|
|
||||||
return ErrCannotReorg
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if s.fixedCommitteeRoots.periods.contains(period+1) && reorg {
|
|
||||||
return ErrCannotReorg
|
|
||||||
}
|
|
||||||
if ok, err := s.verifyUpdate(update); err != nil {
|
|
||||||
return err
|
|
||||||
} else if !ok {
|
|
||||||
return ErrInvalidUpdate
|
|
||||||
}
|
|
||||||
addCommittee := !s.committees.periods.contains(period+1) || reorg
|
|
||||||
if addCommittee {
|
|
||||||
if nextCommittee == nil {
|
|
||||||
return ErrNeedCommittee
|
|
||||||
}
|
|
||||||
if nextCommittee.Root() != update.NextSyncCommitteeRoot {
|
|
||||||
return ErrWrongCommitteeRoot
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.changeCounter++
|
|
||||||
if reorg {
|
|
||||||
if err := s.rollback(period + 1); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
batch := s.db.NewBatch()
|
|
||||||
if addCommittee {
|
|
||||||
if err := s.committees.add(batch, period+1, nextCommittee); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.committeeCache.Remove(period + 1)
|
|
||||||
}
|
|
||||||
if err := s.updates.add(batch, period, update); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := batch.Write(); err != nil {
|
|
||||||
log.Error("Error writing batch into chain database", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Info("Inserted new committee update", "period", period, "next committee root", update.NextSyncCommitteeRoot)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NextSyncPeriod returns the next period where an update can be added and also
|
|
||||||
// whether the chain is initialized at all.
|
|
||||||
func (s *CommitteeChain) NextSyncPeriod() (uint64, bool) {
|
|
||||||
s.chainmu.RLock()
|
|
||||||
defer s.chainmu.RUnlock()
|
|
||||||
|
|
||||||
if s.committees.periods.isEmpty() {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
if !s.updates.periods.isEmpty() {
|
|
||||||
return s.updates.periods.End, true
|
|
||||||
}
|
|
||||||
return s.committees.periods.End - 1, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *CommitteeChain) ChangeCounter() uint64 {
|
|
||||||
s.chainmu.RLock()
|
|
||||||
defer s.chainmu.RUnlock()
|
|
||||||
|
|
||||||
return s.changeCounter
|
|
||||||
}
|
|
||||||
|
|
||||||
// rollback removes all committees and fixed roots from the given period and updates
|
|
||||||
// starting from the previous period.
|
|
||||||
func (s *CommitteeChain) rollback(period uint64) error {
|
|
||||||
max := s.updates.periods.End + 1
|
|
||||||
if s.committees.periods.End > max {
|
|
||||||
max = s.committees.periods.End
|
|
||||||
}
|
|
||||||
if s.fixedCommitteeRoots.periods.End > max {
|
|
||||||
max = s.fixedCommitteeRoots.periods.End
|
|
||||||
}
|
|
||||||
for max > period {
|
|
||||||
max--
|
|
||||||
batch := s.db.NewBatch()
|
|
||||||
s.deleteCommitteesFrom(batch, max)
|
|
||||||
s.fixedCommitteeRoots.deleteFrom(batch, max)
|
|
||||||
if max > 0 {
|
|
||||||
s.updates.deleteFrom(batch, max-1)
|
|
||||||
}
|
|
||||||
if err := batch.Write(); err != nil {
|
|
||||||
log.Error("Error writing batch into chain database", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// getCommitteeRoot returns the committee root at the given period, either fixed,
|
|
||||||
// proven by a previous update or both. It returns an empty hash if the committee
|
|
||||||
// root is unknown.
|
|
||||||
func (s *CommitteeChain) getCommitteeRoot(period uint64) common.Hash {
|
|
||||||
if root, ok := s.fixedCommitteeRoots.get(s.db, period); ok || period == 0 {
|
|
||||||
return root
|
|
||||||
}
|
|
||||||
if update, ok := s.updates.get(s.db, period-1); ok {
|
|
||||||
return update.NextSyncCommitteeRoot
|
|
||||||
}
|
|
||||||
return common.Hash{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSyncCommittee returns the deserialized sync committee at the given period.
|
|
||||||
func (s *CommitteeChain) getSyncCommittee(period uint64) (syncCommittee, error) {
|
|
||||||
if c, ok := s.committeeCache.Get(period); ok {
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
if sc, ok := s.committees.get(s.db, period); ok {
|
|
||||||
c, err := s.sigVerifier.deserializeSyncCommittee(sc)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("sync committee #%d deserialization error: %v", period, err)
|
|
||||||
}
|
|
||||||
s.committeeCache.Add(period, c)
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("missing serialized sync committee #%d", period)
|
|
||||||
}
|
|
||||||
|
|
||||||
// VerifySignedHeader returns true if the given signed header has a valid signature
|
|
||||||
// according to the local committee chain. The caller should ensure that the
|
|
||||||
// committees advertised by the same source where the signed header came from are
|
|
||||||
// synced before verifying the signature.
|
|
||||||
// The age of the header is also returned (the time elapsed since the beginning
|
|
||||||
// of the given slot, according to the local system clock). If enforceTime is
|
|
||||||
// true then negative age (future) headers are rejected.
|
|
||||||
func (s *CommitteeChain) VerifySignedHeader(head types.SignedHeader) (bool, time.Duration, error) {
|
|
||||||
s.chainmu.RLock()
|
|
||||||
defer s.chainmu.RUnlock()
|
|
||||||
|
|
||||||
return s.verifySignedHeader(head)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *CommitteeChain) verifySignedHeader(head types.SignedHeader) (bool, time.Duration, error) {
|
|
||||||
var age time.Duration
|
|
||||||
now := s.unixNano()
|
|
||||||
if head.Header.Slot < (uint64(now-math.MinInt64)/uint64(time.Second)-s.config.GenesisTime)/12 {
|
|
||||||
age = time.Duration(now - int64(time.Second)*int64(s.config.GenesisTime+head.Header.Slot*12))
|
|
||||||
} else {
|
|
||||||
age = time.Duration(math.MinInt64)
|
|
||||||
}
|
|
||||||
if s.enforceTime && age < 0 {
|
|
||||||
return false, age, nil
|
|
||||||
}
|
|
||||||
committee, err := s.getSyncCommittee(types.SyncPeriod(head.SignatureSlot))
|
|
||||||
if err != nil {
|
|
||||||
return false, 0, err
|
|
||||||
}
|
|
||||||
if committee == nil {
|
|
||||||
return false, age, nil
|
|
||||||
}
|
|
||||||
if signingRoot, err := s.config.Forks.SigningRoot(head.Header.Epoch(), head.Header.Hash()); err == nil {
|
|
||||||
return s.sigVerifier.verifySignature(committee, signingRoot, &head.Signature), age, nil
|
|
||||||
}
|
|
||||||
return false, age, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// verifyUpdate checks whether the header signature is correct and the update
|
|
||||||
// fits into the specified constraints (assumes that the update has been
|
|
||||||
// successfully validated previously)
|
|
||||||
func (s *CommitteeChain) verifyUpdate(update *types.LightClientUpdate) (bool, error) {
|
|
||||||
// Note: SignatureSlot determines the sync period of the committee used for signature
|
|
||||||
// verification. Though in reality SignatureSlot is always bigger than update.Header.Slot,
|
|
||||||
// setting them as equal here enforces the rule that they have to be in the same sync
|
|
||||||
// period in order for the light client update proof to be meaningful.
|
|
||||||
ok, age, err := s.verifySignedHeader(update.AttestedHeader)
|
|
||||||
if age < 0 {
|
|
||||||
log.Warn("Future committee update received", "age", age)
|
|
||||||
}
|
|
||||||
return ok, err
|
|
||||||
}
|
|
||||||
@@ -1,356 +0,0 @@
|
|||||||
// Copyright 2022 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 light
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/params"
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common/mclock"
|
|
||||||
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
testGenesis = newTestGenesis()
|
|
||||||
testGenesis2 = newTestGenesis()
|
|
||||||
|
|
||||||
tfBase = newTestForks(testGenesis, params.Forks{
|
|
||||||
¶ms.Fork{Epoch: 0, Version: []byte{0}},
|
|
||||||
})
|
|
||||||
tfAlternative = newTestForks(testGenesis, params.Forks{
|
|
||||||
¶ms.Fork{Epoch: 0, Version: []byte{0}},
|
|
||||||
¶ms.Fork{Epoch: 0x700, Version: []byte{1}},
|
|
||||||
})
|
|
||||||
tfAnotherGenesis = newTestForks(testGenesis2, params.Forks{
|
|
||||||
¶ms.Fork{Epoch: 0, Version: []byte{0}},
|
|
||||||
})
|
|
||||||
|
|
||||||
tcBase = newTestCommitteeChain(nil, tfBase, true, 0, 10, 400, false)
|
|
||||||
tcBaseWithInvalidUpdates = newTestCommitteeChain(tcBase, tfBase, false, 5, 10, 200, false) // signer count too low
|
|
||||||
tcBaseWithBetterUpdates = newTestCommitteeChain(tcBase, tfBase, false, 5, 10, 440, false)
|
|
||||||
tcReorgWithWorseUpdates = newTestCommitteeChain(tcBase, tfBase, true, 5, 10, 400, false)
|
|
||||||
tcReorgWithWorseUpdates2 = newTestCommitteeChain(tcBase, tfBase, true, 5, 10, 380, false)
|
|
||||||
tcReorgWithBetterUpdates = newTestCommitteeChain(tcBase, tfBase, true, 5, 10, 420, false)
|
|
||||||
tcReorgWithFinalizedUpdates = newTestCommitteeChain(tcBase, tfBase, true, 5, 10, 400, true)
|
|
||||||
tcFork = newTestCommitteeChain(tcBase, tfAlternative, true, 7, 10, 400, false)
|
|
||||||
tcAnotherGenesis = newTestCommitteeChain(nil, tfAnotherGenesis, true, 0, 10, 400, false)
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommitteeChainFixedCommitteeRoots(t *testing.T) {
|
|
||||||
for _, reload := range []bool{false, true} {
|
|
||||||
c := newCommitteeChainTest(t, tfBase, 300, true)
|
|
||||||
c.setClockPeriod(7)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 4, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 5, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 6, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 8, ErrInvalidPeriod) // range has to be continuous
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 3, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 2, nil)
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.addCommittee(tcBase, 4, nil)
|
|
||||||
c.addCommittee(tcBase, 6, ErrInvalidPeriod) // range has to be continuous
|
|
||||||
c.addCommittee(tcBase, 5, nil)
|
|
||||||
c.addCommittee(tcBase, 6, nil)
|
|
||||||
c.addCommittee(tcAnotherGenesis, 3, ErrWrongCommitteeRoot)
|
|
||||||
c.addCommittee(tcBase, 3, nil)
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 3, 6)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommitteeChainCheckpointSync(t *testing.T) {
|
|
||||||
for _, enforceTime := range []bool{false, true} {
|
|
||||||
for _, reload := range []bool{false, true} {
|
|
||||||
c := newCommitteeChainTest(t, tfBase, 300, enforceTime)
|
|
||||||
if enforceTime {
|
|
||||||
c.setClockPeriod(6)
|
|
||||||
}
|
|
||||||
c.insertUpdate(tcBase, 3, true, ErrInvalidPeriod)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 3, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 4, nil)
|
|
||||||
c.insertUpdate(tcBase, 4, true, ErrInvalidPeriod) // still no committee
|
|
||||||
c.addCommittee(tcBase, 3, nil)
|
|
||||||
c.addCommittee(tcBase, 4, nil)
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 3, 4)
|
|
||||||
c.insertUpdate(tcBase, 3, false, nil) // update can be added without committee here
|
|
||||||
c.insertUpdate(tcBase, 4, false, ErrNeedCommittee) // but not here as committee 5 is not there yet
|
|
||||||
c.insertUpdate(tcBase, 4, true, nil)
|
|
||||||
c.verifyRange(tcBase, 3, 5)
|
|
||||||
c.insertUpdate(tcBaseWithInvalidUpdates, 5, true, ErrInvalidUpdate) // signer count too low
|
|
||||||
c.insertUpdate(tcBase, 5, true, nil)
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
if enforceTime {
|
|
||||||
c.insertUpdate(tcBase, 6, true, ErrInvalidUpdate) // future update rejected
|
|
||||||
c.setClockPeriod(7)
|
|
||||||
}
|
|
||||||
c.insertUpdate(tcBase, 6, true, nil) // when the time comes it's accepted
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
if enforceTime {
|
|
||||||
c.verifyRange(tcBase, 3, 6) // committee 7 is there but still in the future
|
|
||||||
c.setClockPeriod(8)
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 3, 7) // now period 7 can also be verified
|
|
||||||
// try reverse syncing an update
|
|
||||||
c.insertUpdate(tcBase, 2, false, ErrInvalidPeriod) // fixed committee is needed first
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 2, nil)
|
|
||||||
c.addCommittee(tcBase, 2, nil)
|
|
||||||
c.insertUpdate(tcBase, 2, false, nil)
|
|
||||||
c.verifyRange(tcBase, 2, 7)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommitteeChainReorg(t *testing.T) {
|
|
||||||
for _, reload := range []bool{false, true} {
|
|
||||||
for _, addBetterUpdates := range []bool{false, true} {
|
|
||||||
c := newCommitteeChainTest(t, tfBase, 300, true)
|
|
||||||
c.setClockPeriod(11)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 3, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 4, nil)
|
|
||||||
c.addCommittee(tcBase, 3, nil)
|
|
||||||
for period := uint64(3); period < 10; period++ {
|
|
||||||
c.insertUpdate(tcBase, period, true, nil)
|
|
||||||
}
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 3, 10)
|
|
||||||
c.insertUpdate(tcReorgWithWorseUpdates, 5, true, ErrCannotReorg)
|
|
||||||
c.insertUpdate(tcReorgWithWorseUpdates2, 5, true, ErrCannotReorg)
|
|
||||||
if addBetterUpdates {
|
|
||||||
// add better updates for the base chain and expect first reorg to fail
|
|
||||||
// (only add updates as committees should be the same)
|
|
||||||
for period := uint64(5); period < 10; period++ {
|
|
||||||
c.insertUpdate(tcBaseWithBetterUpdates, period, false, nil)
|
|
||||||
}
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 3, 10) // still on the same chain
|
|
||||||
c.insertUpdate(tcReorgWithBetterUpdates, 5, true, ErrCannotReorg)
|
|
||||||
} else {
|
|
||||||
// reorg with better updates
|
|
||||||
c.insertUpdate(tcReorgWithBetterUpdates, 5, false, ErrNeedCommittee)
|
|
||||||
c.verifyRange(tcBase, 3, 10) // no success yet, still on the base chain
|
|
||||||
c.verifyRange(tcReorgWithBetterUpdates, 3, 5)
|
|
||||||
c.insertUpdate(tcReorgWithBetterUpdates, 5, true, nil)
|
|
||||||
// successful reorg, base chain should only match before the reorg period
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 3, 5)
|
|
||||||
c.verifyRange(tcReorgWithBetterUpdates, 3, 6)
|
|
||||||
for period := uint64(6); period < 10; period++ {
|
|
||||||
c.insertUpdate(tcReorgWithBetterUpdates, period, true, nil)
|
|
||||||
}
|
|
||||||
c.verifyRange(tcReorgWithBetterUpdates, 3, 10)
|
|
||||||
}
|
|
||||||
// reorg with finalized updates; should succeed even if base chain updates
|
|
||||||
// have been improved because a finalized update beats everything else
|
|
||||||
c.insertUpdate(tcReorgWithFinalizedUpdates, 5, false, ErrNeedCommittee)
|
|
||||||
c.insertUpdate(tcReorgWithFinalizedUpdates, 5, true, nil)
|
|
||||||
if reload {
|
|
||||||
c.reloadChain()
|
|
||||||
}
|
|
||||||
c.verifyRange(tcReorgWithFinalizedUpdates, 3, 6)
|
|
||||||
for period := uint64(6); period < 10; period++ {
|
|
||||||
c.insertUpdate(tcReorgWithFinalizedUpdates, period, true, nil)
|
|
||||||
}
|
|
||||||
c.verifyRange(tcReorgWithFinalizedUpdates, 3, 10)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommitteeChainFork(t *testing.T) {
|
|
||||||
c := newCommitteeChainTest(t, tfAlternative, 300, true)
|
|
||||||
c.setClockPeriod(11)
|
|
||||||
// trying to sync a chain on an alternative fork with the base chain data
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 0, nil)
|
|
||||||
c.addFixedCommitteeRoot(tcBase, 1, nil)
|
|
||||||
c.addCommittee(tcBase, 0, nil)
|
|
||||||
// shared section should sync without errors
|
|
||||||
for period := uint64(0); period < 7; period++ {
|
|
||||||
c.insertUpdate(tcBase, period, true, nil)
|
|
||||||
}
|
|
||||||
c.insertUpdate(tcBase, 7, true, ErrInvalidUpdate) // wrong fork
|
|
||||||
// committee root #7 is still the same but signatures are already signed with
|
|
||||||
// a different fork id so period 7 should only verify on the alternative fork
|
|
||||||
c.verifyRange(tcBase, 0, 6)
|
|
||||||
c.verifyRange(tcFork, 0, 7)
|
|
||||||
for period := uint64(7); period < 10; period++ {
|
|
||||||
c.insertUpdate(tcFork, period, true, nil)
|
|
||||||
}
|
|
||||||
c.verifyRange(tcFork, 0, 10)
|
|
||||||
// reload the chain while switching to the base fork
|
|
||||||
c.config = tfBase
|
|
||||||
c.reloadChain()
|
|
||||||
// updates 7..9 should be rolled back now
|
|
||||||
c.verifyRange(tcFork, 0, 6) // again, period 7 only verifies on the right fork
|
|
||||||
c.verifyRange(tcBase, 0, 7)
|
|
||||||
c.insertUpdate(tcFork, 7, true, ErrInvalidUpdate) // wrong fork
|
|
||||||
for period := uint64(7); period < 10; period++ {
|
|
||||||
c.insertUpdate(tcBase, period, true, nil)
|
|
||||||
}
|
|
||||||
c.verifyRange(tcBase, 0, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
type committeeChainTest struct {
|
|
||||||
t *testing.T
|
|
||||||
db *memorydb.Database
|
|
||||||
clock *mclock.Simulated
|
|
||||||
config params.ChainConfig
|
|
||||||
signerThreshold int
|
|
||||||
enforceTime bool
|
|
||||||
chain *CommitteeChain
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCommitteeChainTest(t *testing.T, config params.ChainConfig, signerThreshold int, enforceTime bool) *committeeChainTest {
|
|
||||||
c := &committeeChainTest{
|
|
||||||
t: t,
|
|
||||||
db: memorydb.New(),
|
|
||||||
clock: &mclock.Simulated{},
|
|
||||||
config: config,
|
|
||||||
signerThreshold: signerThreshold,
|
|
||||||
enforceTime: enforceTime,
|
|
||||||
}
|
|
||||||
c.chain = NewTestCommitteeChain(c.db, &config, signerThreshold, enforceTime, c.clock)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) reloadChain() {
|
|
||||||
c.chain = NewTestCommitteeChain(c.db, &c.config, c.signerThreshold, c.enforceTime, c.clock)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) setClockPeriod(period float64) {
|
|
||||||
target := mclock.AbsTime(period * float64(time.Second*12*params.SyncPeriodLength))
|
|
||||||
wait := time.Duration(target - c.clock.Now())
|
|
||||||
if wait < 0 {
|
|
||||||
c.t.Fatalf("Invalid setClockPeriod")
|
|
||||||
}
|
|
||||||
c.clock.Run(wait)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) addFixedCommitteeRoot(tc *testCommitteeChain, period uint64, expErr error) {
|
|
||||||
if err := c.chain.addFixedCommitteeRoot(period, tc.periods[period].committee.Root()); err != expErr {
|
|
||||||
c.t.Errorf("Incorrect error output from addFixedCommitteeRoot at period %d (expected %v, got %v)", period, expErr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) addCommittee(tc *testCommitteeChain, period uint64, expErr error) {
|
|
||||||
if err := c.chain.addCommittee(period, tc.periods[period].committee); err != expErr {
|
|
||||||
c.t.Errorf("Incorrect error output from addCommittee at period %d (expected %v, got %v)", period, expErr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) insertUpdate(tc *testCommitteeChain, period uint64, addCommittee bool, expErr error) {
|
|
||||||
var committee *types.SerializedSyncCommittee
|
|
||||||
if addCommittee {
|
|
||||||
committee = tc.periods[period+1].committee
|
|
||||||
}
|
|
||||||
if err := c.chain.InsertUpdate(tc.periods[period].update, committee); err != expErr {
|
|
||||||
c.t.Errorf("Incorrect error output from InsertUpdate at period %d (expected %v, got %v)", period, expErr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) verifySignedHeader(tc *testCommitteeChain, period float64, expOk bool) {
|
|
||||||
slot := uint64(period * float64(params.SyncPeriodLength))
|
|
||||||
signedHead := GenerateTestSignedHeader(types.Header{Slot: slot}, &tc.config, tc.periods[types.SyncPeriod(slot)].committee, slot+1, 400)
|
|
||||||
if ok, _, _ := c.chain.VerifySignedHeader(signedHead); ok != expOk {
|
|
||||||
c.t.Errorf("Incorrect output from VerifySignedHeader at period %f (expected %v, got %v)", period, expOk, ok)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *committeeChainTest) verifyRange(tc *testCommitteeChain, begin, end uint64) {
|
|
||||||
if begin > 0 {
|
|
||||||
c.verifySignedHeader(tc, float64(begin)-0.5, false)
|
|
||||||
}
|
|
||||||
for period := begin; period <= end; period++ {
|
|
||||||
c.verifySignedHeader(tc, float64(period)+0.5, true)
|
|
||||||
}
|
|
||||||
c.verifySignedHeader(tc, float64(end)+1.5, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestGenesis() params.ChainConfig {
|
|
||||||
var config params.ChainConfig
|
|
||||||
rand.Read(config.GenesisValidatorsRoot[:])
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestForks(config params.ChainConfig, forks params.Forks) params.ChainConfig {
|
|
||||||
for _, fork := range forks {
|
|
||||||
config.AddFork(fork.Name, fork.Epoch, fork.Version)
|
|
||||||
}
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestCommitteeChain(parent *testCommitteeChain, config params.ChainConfig, newCommittees bool, begin, end int, signerCount int, finalizedHeader bool) *testCommitteeChain {
|
|
||||||
tc := &testCommitteeChain{
|
|
||||||
config: config,
|
|
||||||
}
|
|
||||||
if parent != nil {
|
|
||||||
tc.periods = make([]testPeriod, len(parent.periods))
|
|
||||||
copy(tc.periods, parent.periods)
|
|
||||||
}
|
|
||||||
if newCommittees {
|
|
||||||
if begin == 0 {
|
|
||||||
tc.fillCommittees(begin, end+1)
|
|
||||||
} else {
|
|
||||||
tc.fillCommittees(begin+1, end+1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tc.fillUpdates(begin, end, signerCount, finalizedHeader)
|
|
||||||
return tc
|
|
||||||
}
|
|
||||||
|
|
||||||
type testPeriod struct {
|
|
||||||
committee *types.SerializedSyncCommittee
|
|
||||||
update *types.LightClientUpdate
|
|
||||||
}
|
|
||||||
|
|
||||||
type testCommitteeChain struct {
|
|
||||||
periods []testPeriod
|
|
||||||
config params.ChainConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *testCommitteeChain) fillCommittees(begin, end int) {
|
|
||||||
if len(tc.periods) <= end {
|
|
||||||
tc.periods = append(tc.periods, make([]testPeriod, end+1-len(tc.periods))...)
|
|
||||||
}
|
|
||||||
for i := begin; i <= end; i++ {
|
|
||||||
tc.periods[i].committee = GenerateTestCommittee()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *testCommitteeChain) fillUpdates(begin, end int, signerCount int, finalizedHeader bool) {
|
|
||||||
for i := begin; i <= end; i++ {
|
|
||||||
tc.periods[i].update = GenerateTestUpdate(&tc.config, uint64(i), tc.periods[i].committee, tc.periods[i+1].committee, signerCount, finalizedHeader)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,163 +0,0 @@
|
|||||||
// Copyright 2023 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 light
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// HeadTracker keeps track of the latest validated head and the "prefetch" head
|
|
||||||
// which is the (not necessarily validated) head announced by the majority of
|
|
||||||
// servers.
|
|
||||||
type HeadTracker struct {
|
|
||||||
lock sync.RWMutex
|
|
||||||
committeeChain *CommitteeChain
|
|
||||||
minSignerCount int
|
|
||||||
optimisticUpdate types.OptimisticUpdate
|
|
||||||
hasOptimisticUpdate bool
|
|
||||||
finalityUpdate types.FinalityUpdate
|
|
||||||
hasFinalityUpdate bool
|
|
||||||
prefetchHead types.HeadInfo
|
|
||||||
changeCounter uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHeadTracker creates a new HeadTracker.
|
|
||||||
func NewHeadTracker(committeeChain *CommitteeChain, minSignerCount int) *HeadTracker {
|
|
||||||
return &HeadTracker{
|
|
||||||
committeeChain: committeeChain,
|
|
||||||
minSignerCount: minSignerCount,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidatedOptimistic returns the latest validated optimistic update.
|
|
||||||
func (h *HeadTracker) ValidatedOptimistic() (types.OptimisticUpdate, bool) {
|
|
||||||
h.lock.RLock()
|
|
||||||
defer h.lock.RUnlock()
|
|
||||||
|
|
||||||
return h.optimisticUpdate, h.hasOptimisticUpdate
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidatedFinality returns the latest validated finality update.
|
|
||||||
func (h *HeadTracker) ValidatedFinality() (types.FinalityUpdate, bool) {
|
|
||||||
h.lock.RLock()
|
|
||||||
defer h.lock.RUnlock()
|
|
||||||
|
|
||||||
return h.finalityUpdate, h.hasFinalityUpdate
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateOptimistic validates the given optimistic update. If the update is
|
|
||||||
// successfully validated and it is better than the old validated update (higher
|
|
||||||
// slot or same slot and more signers) then ValidatedOptimistic is updated.
|
|
||||||
// The boolean return flag signals if ValidatedOptimistic has been changed.
|
|
||||||
func (h *HeadTracker) ValidateOptimistic(update types.OptimisticUpdate) (bool, error) {
|
|
||||||
if err := update.Validate(); err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
h.lock.Lock()
|
|
||||||
defer h.lock.Unlock()
|
|
||||||
|
|
||||||
replace, err := h.validate(update.SignedHeader(), h.optimisticUpdate.SignedHeader())
|
|
||||||
if replace {
|
|
||||||
h.optimisticUpdate, h.hasOptimisticUpdate = update, true
|
|
||||||
h.changeCounter++
|
|
||||||
}
|
|
||||||
return replace, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateFinality validates the given finality update. If the update is
|
|
||||||
// successfully validated and it is better than the old validated update (higher
|
|
||||||
// slot or same slot and more signers) then ValidatedFinality is updated.
|
|
||||||
// The boolean return flag signals if ValidatedFinality has been changed.
|
|
||||||
func (h *HeadTracker) ValidateFinality(update types.FinalityUpdate) (bool, error) {
|
|
||||||
if err := update.Validate(); err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
h.lock.Lock()
|
|
||||||
defer h.lock.Unlock()
|
|
||||||
|
|
||||||
replace, err := h.validate(update.SignedHeader(), h.finalityUpdate.SignedHeader())
|
|
||||||
if replace {
|
|
||||||
h.finalityUpdate, h.hasFinalityUpdate = update, true
|
|
||||||
h.changeCounter++
|
|
||||||
}
|
|
||||||
return replace, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HeadTracker) validate(head, oldHead types.SignedHeader) (bool, error) {
|
|
||||||
signerCount := head.Signature.SignerCount()
|
|
||||||
if signerCount < h.minSignerCount {
|
|
||||||
return false, errors.New("low signer count")
|
|
||||||
}
|
|
||||||
if head.Header.Slot < oldHead.Header.Slot || (head.Header.Slot == oldHead.Header.Slot && signerCount <= oldHead.Signature.SignerCount()) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
sigOk, age, err := h.committeeChain.VerifySignedHeader(head)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if age < 0 {
|
|
||||||
log.Warn("Future signed head received", "age", age)
|
|
||||||
}
|
|
||||||
if age > time.Minute*2 {
|
|
||||||
log.Warn("Old signed head received", "age", age)
|
|
||||||
}
|
|
||||||
if !sigOk {
|
|
||||||
return false, errors.New("invalid header signature")
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrefetchHead returns the latest known prefetch head's head info.
|
|
||||||
// This head can be used to start fetching related data hoping that it will be
|
|
||||||
// validated soon.
|
|
||||||
// Note that the prefetch head cannot be validated cryptographically so it should
|
|
||||||
// only be used as a performance optimization hint.
|
|
||||||
func (h *HeadTracker) PrefetchHead() types.HeadInfo {
|
|
||||||
h.lock.RLock()
|
|
||||||
defer h.lock.RUnlock()
|
|
||||||
|
|
||||||
return h.prefetchHead
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPrefetchHead sets the prefetch head info.
|
|
||||||
// Note that HeadTracker does not verify the prefetch head, just acts as a thread
|
|
||||||
// safe bulletin board.
|
|
||||||
func (h *HeadTracker) SetPrefetchHead(head types.HeadInfo) {
|
|
||||||
h.lock.Lock()
|
|
||||||
defer h.lock.Unlock()
|
|
||||||
|
|
||||||
if head == h.prefetchHead {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.prefetchHead = head
|
|
||||||
h.changeCounter++
|
|
||||||
}
|
|
||||||
|
|
||||||
// ChangeCounter implements request.targetData
|
|
||||||
func (h *HeadTracker) ChangeCounter() uint64 {
|
|
||||||
h.lock.RLock()
|
|
||||||
defer h.lock.RUnlock()
|
|
||||||
|
|
||||||
return h.changeCounter
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
// Copyright 2023 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 light
|
|
||||||
|
|
||||||
// periodRange represents a (possibly zero-length) range of integers (sync periods).
|
|
||||||
type periodRange struct {
|
|
||||||
Start, End uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// isEmpty returns true if the length of the range is zero.
|
|
||||||
func (a periodRange) isEmpty() bool {
|
|
||||||
return a.End == a.Start
|
|
||||||
}
|
|
||||||
|
|
||||||
// contains returns true if the range includes the given period.
|
|
||||||
func (a periodRange) contains(period uint64) bool {
|
|
||||||
return period >= a.Start && period < a.End
|
|
||||||
}
|
|
||||||
|
|
||||||
// canExpand returns true if the range includes or can be expanded with the given
|
|
||||||
// period (either the range is empty or the given period is inside, right before or
|
|
||||||
// right after the range).
|
|
||||||
func (a periodRange) canExpand(period uint64) bool {
|
|
||||||
return a.isEmpty() || (period+1 >= a.Start && period <= a.End)
|
|
||||||
}
|
|
||||||
|
|
||||||
// expand expands the range with the given period.
|
|
||||||
// This method assumes that canExpand returned true: otherwise this is a no-op.
|
|
||||||
func (a *periodRange) expand(period uint64) {
|
|
||||||
if a.isEmpty() {
|
|
||||||
a.Start, a.End = period, period+1
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if a.Start == period+1 {
|
|
||||||
a.Start--
|
|
||||||
}
|
|
||||||
if a.End == period {
|
|
||||||
a.End++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// split splits the range into two ranges. The 'fromPeriod' will be the first
|
|
||||||
// element in the second range (if present).
|
|
||||||
// The original range is unchanged by this operation
|
|
||||||
func (a *periodRange) split(fromPeriod uint64) (periodRange, periodRange) {
|
|
||||||
if fromPeriod <= a.Start {
|
|
||||||
// First range empty, everything in second range,
|
|
||||||
return periodRange{}, *a
|
|
||||||
}
|
|
||||||
if fromPeriod >= a.End {
|
|
||||||
// Second range empty, everything in first range,
|
|
||||||
return *a, periodRange{}
|
|
||||||
}
|
|
||||||
x := periodRange{a.Start, fromPeriod}
|
|
||||||
y := periodRange{fromPeriod, a.End}
|
|
||||||
return x, y
|
|
||||||
}
|
|
||||||
|
|
||||||
// each invokes the supplied function fn once per period in range
|
|
||||||
func (a *periodRange) each(fn func(uint64)) {
|
|
||||||
for p := a.Start; p < a.End; p++ {
|
|
||||||
fn(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user