mirror of https://github.com/edouardparis/lntop
update dependencies, remove vendor directory
parent
6253b3cb5b
commit
0952c00860
@ -1,32 +1,24 @@
|
|||||||
module github.com/edouardparis/lntop
|
module github.com/edouardparis/lntop
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v0.3.1
|
github.com/BurntSushi/toml v0.3.1
|
||||||
github.com/btcsuite/btcwallet v0.0.0-20190313041134-68fc7c82e131 // indirect
|
|
||||||
github.com/fatih/color v1.7.0
|
github.com/fatih/color v1.7.0
|
||||||
github.com/gofrs/uuid v3.2.0+incompatible
|
github.com/gofrs/uuid v3.2.0+incompatible
|
||||||
github.com/grpc-ecosystem/grpc-gateway v1.8.5 // indirect
|
|
||||||
github.com/jroimartin/gocui v0.4.0
|
github.com/jroimartin/gocui v0.4.0
|
||||||
github.com/juju/clock v0.0.0-20190205081909-9c5c9712527c // indirect
|
github.com/juju/clock v0.0.0-20190205081909-9c5c9712527c // indirect
|
||||||
github.com/juju/errors v0.0.0-20190207033735-e65537c515d7 // indirect
|
github.com/lightningnetwork/lnd v0.12.1-beta
|
||||||
github.com/juju/loggo v0.0.0-20190212223446-d976af380377 // indirect
|
|
||||||
github.com/lightningnetwork/lnd v0.5.2-beta
|
|
||||||
github.com/mattn/go-colorable v0.1.1 // indirect
|
github.com/mattn/go-colorable v0.1.1 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.7 // indirect
|
github.com/mattn/go-isatty v0.0.7 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.4 // indirect
|
|
||||||
github.com/miekg/dns v1.1.6 // indirect
|
github.com/miekg/dns v1.1.6 // indirect
|
||||||
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
|
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/stretchr/testify v1.3.0 // indirect
|
go.uber.org/zap v1.14.1
|
||||||
go.etcd.io/bbolt v1.3.2 // indirect
|
|
||||||
go.uber.org/atomic v1.3.2 // indirect
|
|
||||||
go.uber.org/multierr v1.1.0 // indirect
|
|
||||||
go.uber.org/zap v1.9.1
|
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
|
|
||||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2
|
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2
|
||||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19 // indirect
|
google.golang.org/grpc v1.24.0
|
||||||
google.golang.org/grpc v1.19.0
|
|
||||||
gopkg.in/macaroon-bakery.v2 v2.1.0 // indirect
|
gopkg.in/macaroon-bakery.v2 v2.1.0 // indirect
|
||||||
gopkg.in/macaroon.v2 v2.1.0
|
gopkg.in/macaroon.v2 v2.1.0
|
||||||
|
gopkg.in/resty.v1 v1.12.0 // indirect
|
||||||
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
|
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
|
||||||
)
|
)
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
TAGS
|
|
||||||
tags
|
|
||||||
.*.swp
|
|
||||||
tomlcheck/tomlcheck
|
|
||||||
toml.test
|
|
@ -1,15 +0,0 @@
|
|||||||
language: go
|
|
||||||
go:
|
|
||||||
- 1.1
|
|
||||||
- 1.2
|
|
||||||
- 1.3
|
|
||||||
- 1.4
|
|
||||||
- 1.5
|
|
||||||
- 1.6
|
|
||||||
- tip
|
|
||||||
install:
|
|
||||||
- go install ./...
|
|
||||||
- go get github.com/BurntSushi/toml-test
|
|
||||||
script:
|
|
||||||
- export PATH="$PATH:$HOME/gopath/bin"
|
|
||||||
- make test
|
|
@ -1,3 +0,0 @@
|
|||||||
Compatible with TOML version
|
|
||||||
[v0.4.0](https://github.com/toml-lang/toml/blob/v0.4.0/versions/en/toml-v0.4.0.md)
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
|||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2013 TOML authors
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
@ -1,19 +0,0 @@
|
|||||||
install:
|
|
||||||
go install ./...
|
|
||||||
|
|
||||||
test: install
|
|
||||||
go test -v
|
|
||||||
toml-test toml-test-decoder
|
|
||||||
toml-test -encoder toml-test-encoder
|
|
||||||
|
|
||||||
fmt:
|
|
||||||
gofmt -w *.go */*.go
|
|
||||||
colcheck *.go */*.go
|
|
||||||
|
|
||||||
tags:
|
|
||||||
find ./ -name '*.go' -print0 | xargs -0 gotags > TAGS
|
|
||||||
|
|
||||||
push:
|
|
||||||
git push origin master
|
|
||||||
git push github master
|
|
||||||
|
|
@ -1,218 +0,0 @@
|
|||||||
## TOML parser and encoder for Go with reflection
|
|
||||||
|
|
||||||
TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
|
|
||||||
reflection interface similar to Go's standard library `json` and `xml`
|
|
||||||
packages. This package also supports the `encoding.TextUnmarshaler` and
|
|
||||||
`encoding.TextMarshaler` interfaces so that you can define custom data
|
|
||||||
representations. (There is an example of this below.)
|
|
||||||
|
|
||||||
Spec: https://github.com/toml-lang/toml
|
|
||||||
|
|
||||||
Compatible with TOML version
|
|
||||||
[v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
|
|
||||||
|
|
||||||
Documentation: https://godoc.org/github.com/BurntSushi/toml
|
|
||||||
|
|
||||||
Installation:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
go get github.com/BurntSushi/toml
|
|
||||||
```
|
|
||||||
|
|
||||||
Try the toml validator:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
go get github.com/BurntSushi/toml/cmd/tomlv
|
|
||||||
tomlv some-toml-file.toml
|
|
||||||
```
|
|
||||||
|
|
||||||
[![Build Status](https://travis-ci.org/BurntSushi/toml.svg?branch=master)](https://travis-ci.org/BurntSushi/toml) [![GoDoc](https://godoc.org/github.com/BurntSushi/toml?status.svg)](https://godoc.org/github.com/BurntSushi/toml)
|
|
||||||
|
|
||||||
### Testing
|
|
||||||
|
|
||||||
This package passes all tests in
|
|
||||||
[toml-test](https://github.com/BurntSushi/toml-test) for both the decoder
|
|
||||||
and the encoder.
|
|
||||||
|
|
||||||
### Examples
|
|
||||||
|
|
||||||
This package works similarly to how the Go standard library handles `XML`
|
|
||||||
and `JSON`. Namely, data is loaded into Go values via reflection.
|
|
||||||
|
|
||||||
For the simplest example, consider some TOML file as just a list of keys
|
|
||||||
and values:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
Age = 25
|
|
||||||
Cats = [ "Cauchy", "Plato" ]
|
|
||||||
Pi = 3.14
|
|
||||||
Perfection = [ 6, 28, 496, 8128 ]
|
|
||||||
DOB = 1987-07-05T05:45:00Z
|
|
||||||
```
|
|
||||||
|
|
||||||
Which could be defined in Go as:
|
|
||||||
|
|
||||||
```go
|
|
||||||
type Config struct {
|
|
||||||
Age int
|
|
||||||
Cats []string
|
|
||||||
Pi float64
|
|
||||||
Perfection []int
|
|
||||||
DOB time.Time // requires `import time`
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
And then decoded with:
|
|
||||||
|
|
||||||
```go
|
|
||||||
var conf Config
|
|
||||||
if _, err := toml.Decode(tomlData, &conf); err != nil {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also use struct tags if your struct field name doesn't map to a TOML
|
|
||||||
key value directly:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
some_key_NAME = "wat"
|
|
||||||
```
|
|
||||||
|
|
||||||
```go
|
|
||||||
type TOML struct {
|
|
||||||
ObscureKey string `toml:"some_key_NAME"`
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using the `encoding.TextUnmarshaler` interface
|
|
||||||
|
|
||||||
Here's an example that automatically parses duration strings into
|
|
||||||
`time.Duration` values:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[[song]]
|
|
||||||
name = "Thunder Road"
|
|
||||||
duration = "4m49s"
|
|
||||||
|
|
||||||
[[song]]
|
|
||||||
name = "Stairway to Heaven"
|
|
||||||
duration = "8m03s"
|
|
||||||
```
|
|
||||||
|
|
||||||
Which can be decoded with:
|
|
||||||
|
|
||||||
```go
|
|
||||||
type song struct {
|
|
||||||
Name string
|
|
||||||
Duration duration
|
|
||||||
}
|
|
||||||
type songs struct {
|
|
||||||
Song []song
|
|
||||||
}
|
|
||||||
var favorites songs
|
|
||||||
if _, err := toml.Decode(blob, &favorites); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, s := range favorites.Song {
|
|
||||||
fmt.Printf("%s (%s)\n", s.Name, s.Duration)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
And you'll also need a `duration` type that satisfies the
|
|
||||||
`encoding.TextUnmarshaler` interface:
|
|
||||||
|
|
||||||
```go
|
|
||||||
type duration struct {
|
|
||||||
time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *duration) UnmarshalText(text []byte) error {
|
|
||||||
var err error
|
|
||||||
d.Duration, err = time.ParseDuration(string(text))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### More complex usage
|
|
||||||
|
|
||||||
Here's an example of how to load the example from the official spec page:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
# This is a TOML document. Boom.
|
|
||||||
|
|
||||||
title = "TOML Example"
|
|
||||||
|
|
||||||
[owner]
|
|
||||||
name = "Tom Preston-Werner"
|
|
||||||
organization = "GitHub"
|
|
||||||
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
|
|
||||||
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
|
|
||||||
|
|
||||||
[database]
|
|
||||||
server = "192.168.1.1"
|
|
||||||
ports = [ 8001, 8001, 8002 ]
|
|
||||||
connection_max = 5000
|
|
||||||
enabled = true
|
|
||||||
|
|
||||||
[servers]
|
|
||||||
|
|
||||||
# You can indent as you please. Tabs or spaces. TOML don't care.
|
|
||||||
[servers.alpha]
|
|
||||||
ip = "10.0.0.1"
|
|
||||||
dc = "eqdc10"
|
|
||||||
|
|
||||||
[servers.beta]
|
|
||||||
ip = "10.0.0.2"
|
|
||||||
dc = "eqdc10"
|
|
||||||
|
|
||||||
[clients]
|
|
||||||
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
|
||||||
|
|
||||||
# Line breaks are OK when inside arrays
|
|
||||||
hosts = [
|
|
||||||
"alpha",
|
|
||||||
"omega"
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
And the corresponding Go types are:
|
|
||||||
|
|
||||||
```go
|
|
||||||
type tomlConfig struct {
|
|
||||||
Title string
|
|
||||||
Owner ownerInfo
|
|
||||||
DB database `toml:"database"`
|
|
||||||
Servers map[string]server
|
|
||||||
Clients clients
|
|
||||||
}
|
|
||||||
|
|
||||||
type ownerInfo struct {
|
|
||||||
Name string
|
|
||||||
Org string `toml:"organization"`
|
|
||||||
Bio string
|
|
||||||
DOB time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
type database struct {
|
|
||||||
Server string
|
|
||||||
Ports []int
|
|
||||||
ConnMax int `toml:"connection_max"`
|
|
||||||
Enabled bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type server struct {
|
|
||||||
IP string
|
|
||||||
DC string
|
|
||||||
}
|
|
||||||
|
|
||||||
type clients struct {
|
|
||||||
Data [][]interface{}
|
|
||||||
Hosts []string
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that a case insensitive match will be tried if an exact match can't be
|
|
||||||
found.
|
|
||||||
|
|
||||||
A working example of the above can be found in `_examples/example.{go,toml}`.
|
|
@ -1,509 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"math"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func e(format string, args ...interface{}) error {
|
|
||||||
return fmt.Errorf("toml: "+format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshaler is the interface implemented by objects that can unmarshal a
|
|
||||||
// TOML description of themselves.
|
|
||||||
type Unmarshaler interface {
|
|
||||||
UnmarshalTOML(interface{}) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshal decodes the contents of `p` in TOML format into a pointer `v`.
|
|
||||||
func Unmarshal(p []byte, v interface{}) error {
|
|
||||||
_, err := Decode(string(p), v)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Primitive is a TOML value that hasn't been decoded into a Go value.
|
|
||||||
// When using the various `Decode*` functions, the type `Primitive` may
|
|
||||||
// be given to any value, and its decoding will be delayed.
|
|
||||||
//
|
|
||||||
// A `Primitive` value can be decoded using the `PrimitiveDecode` function.
|
|
||||||
//
|
|
||||||
// The underlying representation of a `Primitive` value is subject to change.
|
|
||||||
// Do not rely on it.
|
|
||||||
//
|
|
||||||
// N.B. Primitive values are still parsed, so using them will only avoid
|
|
||||||
// the overhead of reflection. They can be useful when you don't know the
|
|
||||||
// exact type of TOML data until run time.
|
|
||||||
type Primitive struct {
|
|
||||||
undecoded interface{}
|
|
||||||
context Key
|
|
||||||
}
|
|
||||||
|
|
||||||
// DEPRECATED!
|
|
||||||
//
|
|
||||||
// Use MetaData.PrimitiveDecode instead.
|
|
||||||
func PrimitiveDecode(primValue Primitive, v interface{}) error {
|
|
||||||
md := MetaData{decoded: make(map[string]bool)}
|
|
||||||
return md.unify(primValue.undecoded, rvalue(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrimitiveDecode is just like the other `Decode*` functions, except it
|
|
||||||
// decodes a TOML value that has already been parsed. Valid primitive values
|
|
||||||
// can *only* be obtained from values filled by the decoder functions,
|
|
||||||
// including this method. (i.e., `v` may contain more `Primitive`
|
|
||||||
// values.)
|
|
||||||
//
|
|
||||||
// Meta data for primitive values is included in the meta data returned by
|
|
||||||
// the `Decode*` functions with one exception: keys returned by the Undecoded
|
|
||||||
// method will only reflect keys that were decoded. Namely, any keys hidden
|
|
||||||
// behind a Primitive will be considered undecoded. Executing this method will
|
|
||||||
// update the undecoded keys in the meta data. (See the example.)
|
|
||||||
func (md *MetaData) PrimitiveDecode(primValue Primitive, v interface{}) error {
|
|
||||||
md.context = primValue.context
|
|
||||||
defer func() { md.context = nil }()
|
|
||||||
return md.unify(primValue.undecoded, rvalue(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode will decode the contents of `data` in TOML format into a pointer
|
|
||||||
// `v`.
|
|
||||||
//
|
|
||||||
// TOML hashes correspond to Go structs or maps. (Dealer's choice. They can be
|
|
||||||
// used interchangeably.)
|
|
||||||
//
|
|
||||||
// TOML arrays of tables correspond to either a slice of structs or a slice
|
|
||||||
// of maps.
|
|
||||||
//
|
|
||||||
// TOML datetimes correspond to Go `time.Time` values.
|
|
||||||
//
|
|
||||||
// All other TOML types (float, string, int, bool and array) correspond
|
|
||||||
// to the obvious Go types.
|
|
||||||
//
|
|
||||||
// An exception to the above rules is if a type implements the
|
|
||||||
// encoding.TextUnmarshaler interface. In this case, any primitive TOML value
|
|
||||||
// (floats, strings, integers, booleans and datetimes) will be converted to
|
|
||||||
// a byte string and given to the value's UnmarshalText method. See the
|
|
||||||
// Unmarshaler example for a demonstration with time duration strings.
|
|
||||||
//
|
|
||||||
// Key mapping
|
|
||||||
//
|
|
||||||
// TOML keys can map to either keys in a Go map or field names in a Go
|
|
||||||
// struct. The special `toml` struct tag may be used to map TOML keys to
|
|
||||||
// struct fields that don't match the key name exactly. (See the example.)
|
|
||||||
// A case insensitive match to struct names will be tried if an exact match
|
|
||||||
// can't be found.
|
|
||||||
//
|
|
||||||
// The mapping between TOML values and Go values is loose. That is, there
|
|
||||||
// may exist TOML values that cannot be placed into your representation, and
|
|
||||||
// there may be parts of your representation that do not correspond to
|
|
||||||
// TOML values. This loose mapping can be made stricter by using the IsDefined
|
|
||||||
// and/or Undecoded methods on the MetaData returned.
|
|
||||||
//
|
|
||||||
// This decoder will not handle cyclic types. If a cyclic type is passed,
|
|
||||||
// `Decode` will not terminate.
|
|
||||||
func Decode(data string, v interface{}) (MetaData, error) {
|
|
||||||
rv := reflect.ValueOf(v)
|
|
||||||
if rv.Kind() != reflect.Ptr {
|
|
||||||
return MetaData{}, e("Decode of non-pointer %s", reflect.TypeOf(v))
|
|
||||||
}
|
|
||||||
if rv.IsNil() {
|
|
||||||
return MetaData{}, e("Decode of nil %s", reflect.TypeOf(v))
|
|
||||||
}
|
|
||||||
p, err := parse(data)
|
|
||||||
if err != nil {
|
|
||||||
return MetaData{}, err
|
|
||||||
}
|
|
||||||
md := MetaData{
|
|
||||||
p.mapping, p.types, p.ordered,
|
|
||||||
make(map[string]bool, len(p.ordered)), nil,
|
|
||||||
}
|
|
||||||
return md, md.unify(p.mapping, indirect(rv))
|
|
||||||
}
|
|
||||||
|
|
||||||
// DecodeFile is just like Decode, except it will automatically read the
|
|
||||||
// contents of the file at `fpath` and decode it for you.
|
|
||||||
func DecodeFile(fpath string, v interface{}) (MetaData, error) {
|
|
||||||
bs, err := ioutil.ReadFile(fpath)
|
|
||||||
if err != nil {
|
|
||||||
return MetaData{}, err
|
|
||||||
}
|
|
||||||
return Decode(string(bs), v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DecodeReader is just like Decode, except it will consume all bytes
|
|
||||||
// from the reader and decode it for you.
|
|
||||||
func DecodeReader(r io.Reader, v interface{}) (MetaData, error) {
|
|
||||||
bs, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return MetaData{}, err
|
|
||||||
}
|
|
||||||
return Decode(string(bs), v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// unify performs a sort of type unification based on the structure of `rv`,
|
|
||||||
// which is the client representation.
|
|
||||||
//
|
|
||||||
// Any type mismatch produces an error. Finding a type that we don't know
|
|
||||||
// how to handle produces an unsupported type error.
|
|
||||||
func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
|
|
||||||
|
|
||||||
// Special case. Look for a `Primitive` value.
|
|
||||||
if rv.Type() == reflect.TypeOf((*Primitive)(nil)).Elem() {
|
|
||||||
// Save the undecoded data and the key context into the primitive
|
|
||||||
// value.
|
|
||||||
context := make(Key, len(md.context))
|
|
||||||
copy(context, md.context)
|
|
||||||
rv.Set(reflect.ValueOf(Primitive{
|
|
||||||
undecoded: data,
|
|
||||||
context: context,
|
|
||||||
}))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special case. Unmarshaler Interface support.
|
|
||||||
if rv.CanAddr() {
|
|
||||||
if v, ok := rv.Addr().Interface().(Unmarshaler); ok {
|
|
||||||
return v.UnmarshalTOML(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special case. Handle time.Time values specifically.
|
|
||||||
// TODO: Remove this code when we decide to drop support for Go 1.1.
|
|
||||||
// This isn't necessary in Go 1.2 because time.Time satisfies the encoding
|
|
||||||
// interfaces.
|
|
||||||
if rv.Type().AssignableTo(rvalue(time.Time{}).Type()) {
|
|
||||||
return md.unifyDatetime(data, rv)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special case. Look for a value satisfying the TextUnmarshaler interface.
|
|
||||||
if v, ok := rv.Interface().(TextUnmarshaler); ok {
|
|
||||||
return md.unifyText(data, v)
|
|
||||||
}
|
|
||||||
// BUG(burntsushi)
|
|
||||||
// The behavior here is incorrect whenever a Go type satisfies the
|
|
||||||
// encoding.TextUnmarshaler interface but also corresponds to a TOML
|
|
||||||
// hash or array. In particular, the unmarshaler should only be applied
|
|
||||||
// to primitive TOML values. But at this point, it will be applied to
|
|
||||||
// all kinds of values and produce an incorrect error whenever those values
|
|
||||||
// are hashes or arrays (including arrays of tables).
|
|
||||||
|
|
||||||
k := rv.Kind()
|
|
||||||
|
|
||||||
// laziness
|
|
||||||
if k >= reflect.Int && k <= reflect.Uint64 {
|
|
||||||
return md.unifyInt(data, rv)
|
|
||||||
}
|
|
||||||
switch k {
|
|
||||||
case reflect.Ptr:
|
|
||||||
elem := reflect.New(rv.Type().Elem())
|
|
||||||
err := md.unify(data, reflect.Indirect(elem))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
rv.Set(elem)
|
|
||||||
return nil
|
|
||||||
case reflect.Struct:
|
|
||||||
return md.unifyStruct(data, rv)
|
|
||||||
case reflect.Map:
|
|
||||||
return md.unifyMap(data, rv)
|
|
||||||
case reflect.Array:
|
|
||||||
return md.unifyArray(data, rv)
|
|
||||||
case reflect.Slice:
|
|
||||||
return md.unifySlice(data, rv)
|
|
||||||
case reflect.String:
|
|
||||||
return md.unifyString(data, rv)
|
|
||||||
case reflect.Bool:
|
|
||||||
return md.unifyBool(data, rv)
|
|
||||||
case reflect.Interface:
|
|
||||||
// we only support empty interfaces.
|
|
||||||
if rv.NumMethod() > 0 {
|
|
||||||
return e("unsupported type %s", rv.Type())
|
|
||||||
}
|
|
||||||
return md.unifyAnything(data, rv)
|
|
||||||
case reflect.Float32:
|
|
||||||
fallthrough
|
|
||||||
case reflect.Float64:
|
|
||||||
return md.unifyFloat64(data, rv)
|
|
||||||
}
|
|
||||||
return e("unsupported type %s", rv.Kind())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyStruct(mapping interface{}, rv reflect.Value) error {
|
|
||||||
tmap, ok := mapping.(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
if mapping == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return e("type mismatch for %s: expected table but found %T",
|
|
||||||
rv.Type().String(), mapping)
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, datum := range tmap {
|
|
||||||
var f *field
|
|
||||||
fields := cachedTypeFields(rv.Type())
|
|
||||||
for i := range fields {
|
|
||||||
ff := &fields[i]
|
|
||||||
if ff.name == key {
|
|
||||||
f = ff
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if f == nil && strings.EqualFold(ff.name, key) {
|
|
||||||
f = ff
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if f != nil {
|
|
||||||
subv := rv
|
|
||||||
for _, i := range f.index {
|
|
||||||
subv = indirect(subv.Field(i))
|
|
||||||
}
|
|
||||||
if isUnifiable(subv) {
|
|
||||||
md.decoded[md.context.add(key).String()] = true
|
|
||||||
md.context = append(md.context, key)
|
|
||||||
if err := md.unify(datum, subv); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
md.context = md.context[0 : len(md.context)-1]
|
|
||||||
} else if f.name != "" {
|
|
||||||
// Bad user! No soup for you!
|
|
||||||
return e("cannot write unexported field %s.%s",
|
|
||||||
rv.Type().String(), f.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
|
|
||||||
tmap, ok := mapping.(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
if tmap == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("map", mapping)
|
|
||||||
}
|
|
||||||
if rv.IsNil() {
|
|
||||||
rv.Set(reflect.MakeMap(rv.Type()))
|
|
||||||
}
|
|
||||||
for k, v := range tmap {
|
|
||||||
md.decoded[md.context.add(k).String()] = true
|
|
||||||
md.context = append(md.context, k)
|
|
||||||
|
|
||||||
rvkey := indirect(reflect.New(rv.Type().Key()))
|
|
||||||
rvval := reflect.Indirect(reflect.New(rv.Type().Elem()))
|
|
||||||
if err := md.unify(v, rvval); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
md.context = md.context[0 : len(md.context)-1]
|
|
||||||
|
|
||||||
rvkey.SetString(k)
|
|
||||||
rv.SetMapIndex(rvkey, rvval)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyArray(data interface{}, rv reflect.Value) error {
|
|
||||||
datav := reflect.ValueOf(data)
|
|
||||||
if datav.Kind() != reflect.Slice {
|
|
||||||
if !datav.IsValid() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("slice", data)
|
|
||||||
}
|
|
||||||
sliceLen := datav.Len()
|
|
||||||
if sliceLen != rv.Len() {
|
|
||||||
return e("expected array length %d; got TOML array of length %d",
|
|
||||||
rv.Len(), sliceLen)
|
|
||||||
}
|
|
||||||
return md.unifySliceArray(datav, rv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifySlice(data interface{}, rv reflect.Value) error {
|
|
||||||
datav := reflect.ValueOf(data)
|
|
||||||
if datav.Kind() != reflect.Slice {
|
|
||||||
if !datav.IsValid() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("slice", data)
|
|
||||||
}
|
|
||||||
n := datav.Len()
|
|
||||||
if rv.IsNil() || rv.Cap() < n {
|
|
||||||
rv.Set(reflect.MakeSlice(rv.Type(), n, n))
|
|
||||||
}
|
|
||||||
rv.SetLen(n)
|
|
||||||
return md.unifySliceArray(datav, rv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifySliceArray(data, rv reflect.Value) error {
|
|
||||||
sliceLen := data.Len()
|
|
||||||
for i := 0; i < sliceLen; i++ {
|
|
||||||
v := data.Index(i).Interface()
|
|
||||||
sliceval := indirect(rv.Index(i))
|
|
||||||
if err := md.unify(v, sliceval); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyDatetime(data interface{}, rv reflect.Value) error {
|
|
||||||
if _, ok := data.(time.Time); ok {
|
|
||||||
rv.Set(reflect.ValueOf(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("time.Time", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyString(data interface{}, rv reflect.Value) error {
|
|
||||||
if s, ok := data.(string); ok {
|
|
||||||
rv.SetString(s)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("string", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyFloat64(data interface{}, rv reflect.Value) error {
|
|
||||||
if num, ok := data.(float64); ok {
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Float32:
|
|
||||||
fallthrough
|
|
||||||
case reflect.Float64:
|
|
||||||
rv.SetFloat(num)
|
|
||||||
default:
|
|
||||||
panic("bug")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("float", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyInt(data interface{}, rv reflect.Value) error {
|
|
||||||
if num, ok := data.(int64); ok {
|
|
||||||
if rv.Kind() >= reflect.Int && rv.Kind() <= reflect.Int64 {
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Int, reflect.Int64:
|
|
||||||
// No bounds checking necessary.
|
|
||||||
case reflect.Int8:
|
|
||||||
if num < math.MinInt8 || num > math.MaxInt8 {
|
|
||||||
return e("value %d is out of range for int8", num)
|
|
||||||
}
|
|
||||||
case reflect.Int16:
|
|
||||||
if num < math.MinInt16 || num > math.MaxInt16 {
|
|
||||||
return e("value %d is out of range for int16", num)
|
|
||||||
}
|
|
||||||
case reflect.Int32:
|
|
||||||
if num < math.MinInt32 || num > math.MaxInt32 {
|
|
||||||
return e("value %d is out of range for int32", num)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rv.SetInt(num)
|
|
||||||
} else if rv.Kind() >= reflect.Uint && rv.Kind() <= reflect.Uint64 {
|
|
||||||
unum := uint64(num)
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Uint, reflect.Uint64:
|
|
||||||
// No bounds checking necessary.
|
|
||||||
case reflect.Uint8:
|
|
||||||
if num < 0 || unum > math.MaxUint8 {
|
|
||||||
return e("value %d is out of range for uint8", num)
|
|
||||||
}
|
|
||||||
case reflect.Uint16:
|
|
||||||
if num < 0 || unum > math.MaxUint16 {
|
|
||||||
return e("value %d is out of range for uint16", num)
|
|
||||||
}
|
|
||||||
case reflect.Uint32:
|
|
||||||
if num < 0 || unum > math.MaxUint32 {
|
|
||||||
return e("value %d is out of range for uint32", num)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rv.SetUint(unum)
|
|
||||||
} else {
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("integer", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyBool(data interface{}, rv reflect.Value) error {
|
|
||||||
if b, ok := data.(bool); ok {
|
|
||||||
rv.SetBool(b)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return badtype("boolean", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyAnything(data interface{}, rv reflect.Value) error {
|
|
||||||
rv.Set(reflect.ValueOf(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (md *MetaData) unifyText(data interface{}, v TextUnmarshaler) error {
|
|
||||||
var s string
|
|
||||||
switch sdata := data.(type) {
|
|
||||||
case TextMarshaler:
|
|
||||||
text, err := sdata.MarshalText()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s = string(text)
|
|
||||||
case fmt.Stringer:
|
|
||||||
s = sdata.String()
|
|
||||||
case string:
|
|
||||||
s = sdata
|
|
||||||
case bool:
|
|
||||||
s = fmt.Sprintf("%v", sdata)
|
|
||||||
case int64:
|
|
||||||
s = fmt.Sprintf("%d", sdata)
|
|
||||||
case float64:
|
|
||||||
s = fmt.Sprintf("%f", sdata)
|
|
||||||
default:
|
|
||||||
return badtype("primitive (string-like)", data)
|
|
||||||
}
|
|
||||||
if err := v.UnmarshalText([]byte(s)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// rvalue returns a reflect.Value of `v`. All pointers are resolved.
|
|
||||||
func rvalue(v interface{}) reflect.Value {
|
|
||||||
return indirect(reflect.ValueOf(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
// indirect returns the value pointed to by a pointer.
|
|
||||||
// Pointers are followed until the value is not a pointer.
|
|
||||||
// New values are allocated for each nil pointer.
|
|
||||||
//
|
|
||||||
// An exception to this rule is if the value satisfies an interface of
|
|
||||||
// interest to us (like encoding.TextUnmarshaler).
|
|
||||||
func indirect(v reflect.Value) reflect.Value {
|
|
||||||
if v.Kind() != reflect.Ptr {
|
|
||||||
if v.CanSet() {
|
|
||||||
pv := v.Addr()
|
|
||||||
if _, ok := pv.Interface().(TextUnmarshaler); ok {
|
|
||||||
return pv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
if v.IsNil() {
|
|
||||||
v.Set(reflect.New(v.Type().Elem()))
|
|
||||||
}
|
|
||||||
return indirect(reflect.Indirect(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
func isUnifiable(rv reflect.Value) bool {
|
|
||||||
if rv.CanSet() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if _, ok := rv.Interface().(TextUnmarshaler); ok {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func badtype(expected string, data interface{}) error {
|
|
||||||
return e("cannot load TOML value of type %T into a Go %s", data, expected)
|
|
||||||
}
|
|
@ -1,121 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
// MetaData allows access to meta information about TOML data that may not
|
|
||||||
// be inferrable via reflection. In particular, whether a key has been defined
|
|
||||||
// and the TOML type of a key.
|
|
||||||
type MetaData struct {
|
|
||||||
mapping map[string]interface{}
|
|
||||||
types map[string]tomlType
|
|
||||||
keys []Key
|
|
||||||
decoded map[string]bool
|
|
||||||
context Key // Used only during decoding.
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsDefined returns true if the key given exists in the TOML data. The key
|
|
||||||
// should be specified hierarchially. e.g.,
|
|
||||||
//
|
|
||||||
// // access the TOML key 'a.b.c'
|
|
||||||
// IsDefined("a", "b", "c")
|
|
||||||
//
|
|
||||||
// IsDefined will return false if an empty key given. Keys are case sensitive.
|
|
||||||
func (md *MetaData) IsDefined(key ...string) bool {
|
|
||||||
if len(key) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
var hash map[string]interface{}
|
|
||||||
var ok bool
|
|
||||||
var hashOrVal interface{} = md.mapping
|
|
||||||
for _, k := range key {
|
|
||||||
if hash, ok = hashOrVal.(map[string]interface{}); !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if hashOrVal, ok = hash[k]; !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type returns a string representation of the type of the key specified.
|
|
||||||
//
|
|
||||||
// Type will return the empty string if given an empty key or a key that
|
|
||||||
// does not exist. Keys are case sensitive.
|
|
||||||
func (md *MetaData) Type(key ...string) string {
|
|
||||||
fullkey := strings.Join(key, ".")
|
|
||||||
if typ, ok := md.types[fullkey]; ok {
|
|
||||||
return typ.typeString()
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Key is the type of any TOML key, including key groups. Use (MetaData).Keys
|
|
||||||
// to get values of this type.
|
|
||||||
type Key []string
|
|
||||||
|
|
||||||
func (k Key) String() string {
|
|
||||||
return strings.Join(k, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k Key) maybeQuotedAll() string {
|
|
||||||
var ss []string
|
|
||||||
for i := range k {
|
|
||||||
ss = append(ss, k.maybeQuoted(i))
|
|
||||||
}
|
|
||||||
return strings.Join(ss, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k Key) maybeQuoted(i int) string {
|
|
||||||
quote := false
|
|
||||||
for _, c := range k[i] {
|
|
||||||
if !isBareKeyChar(c) {
|
|
||||||
quote = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if quote {
|
|
||||||
return "\"" + strings.Replace(k[i], "\"", "\\\"", -1) + "\""
|
|
||||||
}
|
|
||||||
return k[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k Key) add(piece string) Key {
|
|
||||||
newKey := make(Key, len(k)+1)
|
|
||||||
copy(newKey, k)
|
|
||||||
newKey[len(k)] = piece
|
|
||||||
return newKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keys returns a slice of every key in the TOML data, including key groups.
|
|
||||||
// Each key is itself a slice, where the first element is the top of the
|
|
||||||
// hierarchy and the last is the most specific.
|
|
||||||
//
|
|
||||||
// The list will have the same order as the keys appeared in the TOML data.
|
|
||||||
//
|
|
||||||
// All keys returned are non-empty.
|
|
||||||
func (md *MetaData) Keys() []Key {
|
|
||||||
return md.keys
|
|
||||||
}
|
|
||||||
|
|
||||||
// Undecoded returns all keys that have not been decoded in the order in which
|
|
||||||
// they appear in the original TOML document.
|
|
||||||
//
|
|
||||||
// This includes keys that haven't been decoded because of a Primitive value.
|
|
||||||
// Once the Primitive value is decoded, the keys will be considered decoded.
|
|
||||||
//
|
|
||||||
// Also note that decoding into an empty interface will result in no decoding,
|
|
||||||
// and so no keys will be considered decoded.
|
|
||||||
//
|
|
||||||
// In this sense, the Undecoded keys correspond to keys in the TOML document
|
|
||||||
// that do not have a concrete type in your representation.
|
|
||||||
func (md *MetaData) Undecoded() []Key {
|
|
||||||
undecoded := make([]Key, 0, len(md.keys))
|
|
||||||
for _, key := range md.keys {
|
|
||||||
if !md.decoded[key.String()] {
|
|
||||||
undecoded = append(undecoded, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undecoded
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
Package toml provides facilities for decoding and encoding TOML configuration
|
|
||||||
files via reflection. There is also support for delaying decoding with
|
|
||||||
the Primitive type, and querying the set of keys in a TOML document with the
|
|
||||||
MetaData type.
|
|
||||||
|
|
||||||
The specification implemented: https://github.com/toml-lang/toml
|
|
||||||
|
|
||||||
The sub-command github.com/BurntSushi/toml/cmd/tomlv can be used to verify
|
|
||||||
whether a file is a valid TOML document. It can also be used to print the
|
|
||||||
type of each key in a TOML document.
|
|
||||||
|
|
||||||
Testing
|
|
||||||
|
|
||||||
There are two important types of tests used for this package. The first is
|
|
||||||
contained inside '*_test.go' files and uses the standard Go unit testing
|
|
||||||
framework. These tests are primarily devoted to holistically testing the
|
|
||||||
decoder and encoder.
|
|
||||||
|
|
||||||
The second type of testing is used to verify the implementation's adherence
|
|
||||||
to the TOML specification. These tests have been factored into their own
|
|
||||||
project: https://github.com/BurntSushi/toml-test
|
|
||||||
|
|
||||||
The reason the tests are in a separate project is so that they can be used by
|
|
||||||
any implementation of TOML. Namely, it is language agnostic.
|
|
||||||
*/
|
|
||||||
package toml
|
|
@ -1,568 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type tomlEncodeError struct{ error }
|
|
||||||
|
|
||||||
var (
|
|
||||||
errArrayMixedElementTypes = errors.New(
|
|
||||||
"toml: cannot encode array with mixed element types")
|
|
||||||
errArrayNilElement = errors.New(
|
|
||||||
"toml: cannot encode array with nil element")
|
|
||||||
errNonString = errors.New(
|
|
||||||
"toml: cannot encode a map with non-string key type")
|
|
||||||
errAnonNonStruct = errors.New(
|
|
||||||
"toml: cannot encode an anonymous field that is not a struct")
|
|
||||||
errArrayNoTable = errors.New(
|
|
||||||
"toml: TOML array element cannot contain a table")
|
|
||||||
errNoKey = errors.New(
|
|
||||||
"toml: top-level values must be Go maps or structs")
|
|
||||||
errAnything = errors.New("") // used in testing
|
|
||||||
)
|
|
||||||
|
|
||||||
var quotedReplacer = strings.NewReplacer(
|
|
||||||
"\t", "\\t",
|
|
||||||
"\n", "\\n",
|
|
||||||
"\r", "\\r",
|
|
||||||
"\"", "\\\"",
|
|
||||||
"\\", "\\\\",
|
|
||||||
)
|
|
||||||
|
|
||||||
// Encoder controls the encoding of Go values to a TOML document to some
|
|
||||||
// io.Writer.
|
|
||||||
//
|
|
||||||
// The indentation level can be controlled with the Indent field.
|
|
||||||
type Encoder struct {
|
|
||||||
// A single indentation level. By default it is two spaces.
|
|
||||||
Indent string
|
|
||||||
|
|
||||||
// hasWritten is whether we have written any output to w yet.
|
|
||||||
hasWritten bool
|
|
||||||
w *bufio.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewEncoder returns a TOML encoder that encodes Go values to the io.Writer
|
|
||||||
// given. By default, a single indentation level is 2 spaces.
|
|
||||||
func NewEncoder(w io.Writer) *Encoder {
|
|
||||||
return &Encoder{
|
|
||||||
w: bufio.NewWriter(w),
|
|
||||||
Indent: " ",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode writes a TOML representation of the Go value to the underlying
|
|
||||||
// io.Writer. If the value given cannot be encoded to a valid TOML document,
|
|
||||||
// then an error is returned.
|
|
||||||
//
|
|
||||||
// The mapping between Go values and TOML values should be precisely the same
|
|
||||||
// as for the Decode* functions. Similarly, the TextMarshaler interface is
|
|
||||||
// supported by encoding the resulting bytes as strings. (If you want to write
|
|
||||||
// arbitrary binary data then you will need to use something like base64 since
|
|
||||||
// TOML does not have any binary types.)
|
|
||||||
//
|
|
||||||
// When encoding TOML hashes (i.e., Go maps or structs), keys without any
|
|
||||||
// sub-hashes are encoded first.
|
|
||||||
//
|
|
||||||
// If a Go map is encoded, then its keys are sorted alphabetically for
|
|
||||||
// deterministic output. More control over this behavior may be provided if
|
|
||||||
// there is demand for it.
|
|
||||||
//
|
|
||||||
// Encoding Go values without a corresponding TOML representation---like map
|
|
||||||
// types with non-string keys---will cause an error to be returned. Similarly
|
|
||||||
// for mixed arrays/slices, arrays/slices with nil elements, embedded
|
|
||||||
// non-struct types and nested slices containing maps or structs.
|
|
||||||
// (e.g., [][]map[string]string is not allowed but []map[string]string is OK
|
|
||||||
// and so is []map[string][]string.)
|
|
||||||
func (enc *Encoder) Encode(v interface{}) error {
|
|
||||||
rv := eindirect(reflect.ValueOf(v))
|
|
||||||
if err := enc.safeEncode(Key([]string{}), rv); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return enc.w.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) safeEncode(key Key, rv reflect.Value) (err error) {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
if terr, ok := r.(tomlEncodeError); ok {
|
|
||||||
err = terr.error
|
|
||||||
return
|
|
||||||
}
|
|
||||||
panic(r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
enc.encode(key, rv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) encode(key Key, rv reflect.Value) {
|
|
||||||
// Special case. Time needs to be in ISO8601 format.
|
|
||||||
// Special case. If we can marshal the type to text, then we used that.
|
|
||||||
// Basically, this prevents the encoder for handling these types as
|
|
||||||
// generic structs (or whatever the underlying type of a TextMarshaler is).
|
|
||||||
switch rv.Interface().(type) {
|
|
||||||
case time.Time, TextMarshaler:
|
|
||||||
enc.keyEqElement(key, rv)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
k := rv.Kind()
|
|
||||||
switch k {
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
|
|
||||||
reflect.Int64,
|
|
||||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
|
|
||||||
reflect.Uint64,
|
|
||||||
reflect.Float32, reflect.Float64, reflect.String, reflect.Bool:
|
|
||||||
enc.keyEqElement(key, rv)
|
|
||||||
case reflect.Array, reflect.Slice:
|
|
||||||
if typeEqual(tomlArrayHash, tomlTypeOfGo(rv)) {
|
|
||||||
enc.eArrayOfTables(key, rv)
|
|
||||||
} else {
|
|
||||||
enc.keyEqElement(key, rv)
|
|
||||||
}
|
|
||||||
case reflect.Interface:
|
|
||||||
if rv.IsNil() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enc.encode(key, rv.Elem())
|
|
||||||
case reflect.Map:
|
|
||||||
if rv.IsNil() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enc.eTable(key, rv)
|
|
||||||
case reflect.Ptr:
|
|
||||||
if rv.IsNil() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enc.encode(key, rv.Elem())
|
|
||||||
case reflect.Struct:
|
|
||||||
enc.eTable(key, rv)
|
|
||||||
default:
|
|
||||||
panic(e("unsupported type for key '%s': %s", key, k))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// eElement encodes any value that can be an array element (primitives and
|
|
||||||
// arrays).
|
|
||||||
func (enc *Encoder) eElement(rv reflect.Value) {
|
|
||||||
switch v := rv.Interface().(type) {
|
|
||||||
case time.Time:
|
|
||||||
// Special case time.Time as a primitive. Has to come before
|
|
||||||
// TextMarshaler below because time.Time implements
|
|
||||||
// encoding.TextMarshaler, but we need to always use UTC.
|
|
||||||
enc.wf(v.UTC().Format("2006-01-02T15:04:05Z"))
|
|
||||||
return
|
|
||||||
case TextMarshaler:
|
|
||||||
// Special case. Use text marshaler if it's available for this value.
|
|
||||||
if s, err := v.MarshalText(); err != nil {
|
|
||||||
encPanic(err)
|
|
||||||
} else {
|
|
||||||
enc.writeQuoted(string(s))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Bool:
|
|
||||||
enc.wf(strconv.FormatBool(rv.Bool()))
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
|
|
||||||
reflect.Int64:
|
|
||||||
enc.wf(strconv.FormatInt(rv.Int(), 10))
|
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16,
|
|
||||||
reflect.Uint32, reflect.Uint64:
|
|
||||||
enc.wf(strconv.FormatUint(rv.Uint(), 10))
|
|
||||||
case reflect.Float32:
|
|
||||||
enc.wf(floatAddDecimal(strconv.FormatFloat(rv.Float(), 'f', -1, 32)))
|
|
||||||
case reflect.Float64:
|
|
||||||
enc.wf(floatAddDecimal(strconv.FormatFloat(rv.Float(), 'f', -1, 64)))
|
|
||||||
case reflect.Array, reflect.Slice:
|
|
||||||
enc.eArrayOrSliceElement(rv)
|
|
||||||
case reflect.Interface:
|
|
||||||
enc.eElement(rv.Elem())
|
|
||||||
case reflect.String:
|
|
||||||
enc.writeQuoted(rv.String())
|
|
||||||
default:
|
|
||||||
panic(e("unexpected primitive type: %s", rv.Kind()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// By the TOML spec, all floats must have a decimal with at least one
|
|
||||||
// number on either side.
|
|
||||||
func floatAddDecimal(fstr string) string {
|
|
||||||
if !strings.Contains(fstr, ".") {
|
|
||||||
return fstr + ".0"
|
|
||||||
}
|
|
||||||
return fstr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) writeQuoted(s string) {
|
|
||||||
enc.wf("\"%s\"", quotedReplacer.Replace(s))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) eArrayOrSliceElement(rv reflect.Value) {
|
|
||||||
length := rv.Len()
|
|
||||||
enc.wf("[")
|
|
||||||
for i := 0; i < length; i++ {
|
|
||||||
elem := rv.Index(i)
|
|
||||||
enc.eElement(elem)
|
|
||||||
if i != length-1 {
|
|
||||||
enc.wf(", ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
enc.wf("]")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) eArrayOfTables(key Key, rv reflect.Value) {
|
|
||||||
if len(key) == 0 {
|
|
||||||
encPanic(errNoKey)
|
|
||||||
}
|
|
||||||
for i := 0; i < rv.Len(); i++ {
|
|
||||||
trv := rv.Index(i)
|
|
||||||
if isNil(trv) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
panicIfInvalidKey(key)
|
|
||||||
enc.newline()
|
|
||||||
enc.wf("%s[[%s]]", enc.indentStr(key), key.maybeQuotedAll())
|
|
||||||
enc.newline()
|
|
||||||
enc.eMapOrStruct(key, trv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) eTable(key Key, rv reflect.Value) {
|
|
||||||
panicIfInvalidKey(key)
|
|
||||||
if len(key) == 1 {
|
|
||||||
// Output an extra newline between top-level tables.
|
|
||||||
// (The newline isn't written if nothing else has been written though.)
|
|
||||||
enc.newline()
|
|
||||||
}
|
|
||||||
if len(key) > 0 {
|
|
||||||
enc.wf("%s[%s]", enc.indentStr(key), key.maybeQuotedAll())
|
|
||||||
enc.newline()
|
|
||||||
}
|
|
||||||
enc.eMapOrStruct(key, rv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) eMapOrStruct(key Key, rv reflect.Value) {
|
|
||||||
switch rv := eindirect(rv); rv.Kind() {
|
|
||||||
case reflect.Map:
|
|
||||||
enc.eMap(key, rv)
|
|
||||||
case reflect.Struct:
|
|
||||||
enc.eStruct(key, rv)
|
|
||||||
default:
|
|
||||||
panic("eTable: unhandled reflect.Value Kind: " + rv.Kind().String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) eMap(key Key, rv reflect.Value) {
|
|
||||||
rt := rv.Type()
|
|
||||||
if rt.Key().Kind() != reflect.String {
|
|
||||||
encPanic(errNonString)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort keys so that we have deterministic output. And write keys directly
|
|
||||||
// underneath this key first, before writing sub-structs or sub-maps.
|
|
||||||
var mapKeysDirect, mapKeysSub []string
|
|
||||||
for _, mapKey := range rv.MapKeys() {
|
|
||||||
k := mapKey.String()
|
|
||||||
if typeIsHash(tomlTypeOfGo(rv.MapIndex(mapKey))) {
|
|
||||||
mapKeysSub = append(mapKeysSub, k)
|
|
||||||
} else {
|
|
||||||
mapKeysDirect = append(mapKeysDirect, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var writeMapKeys = func(mapKeys []string) {
|
|
||||||
sort.Strings(mapKeys)
|
|
||||||
for _, mapKey := range mapKeys {
|
|
||||||
mrv := rv.MapIndex(reflect.ValueOf(mapKey))
|
|
||||||
if isNil(mrv) {
|
|
||||||
// Don't write anything for nil fields.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
enc.encode(key.add(mapKey), mrv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writeMapKeys(mapKeysDirect)
|
|
||||||
writeMapKeys(mapKeysSub)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) eStruct(key Key, rv reflect.Value) {
|
|
||||||
// Write keys for fields directly under this key first, because if we write
|
|
||||||
// a field that creates a new table, then all keys under it will be in that
|
|
||||||
// table (not the one we're writing here).
|
|
||||||
rt := rv.Type()
|
|
||||||
var fieldsDirect, fieldsSub [][]int
|
|
||||||
var addFields func(rt reflect.Type, rv reflect.Value, start []int)
|
|
||||||
addFields = func(rt reflect.Type, rv reflect.Value, start []int) {
|
|
||||||
for i := 0; i < rt.NumField(); i++ {
|
|
||||||
f := rt.Field(i)
|
|
||||||
// skip unexported fields
|
|
||||||
if f.PkgPath != "" && !f.Anonymous {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
frv := rv.Field(i)
|
|
||||||
if f.Anonymous {
|
|
||||||
t := f.Type
|
|
||||||
switch t.Kind() {
|
|
||||||
case reflect.Struct:
|
|
||||||
// Treat anonymous struct fields with
|
|
||||||
// tag names as though they are not
|
|
||||||
// anonymous, like encoding/json does.
|
|
||||||
if getOptions(f.Tag).name == "" {
|
|
||||||
addFields(t, frv, f.Index)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case reflect.Ptr:
|
|
||||||
if t.Elem().Kind() == reflect.Struct &&
|
|
||||||
getOptions(f.Tag).name == "" {
|
|
||||||
if !frv.IsNil() {
|
|
||||||
addFields(t.Elem(), frv.Elem(), f.Index)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Fall through to the normal field encoding logic below
|
|
||||||
// for non-struct anonymous fields.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if typeIsHash(tomlTypeOfGo(frv)) {
|
|
||||||
fieldsSub = append(fieldsSub, append(start, f.Index...))
|
|
||||||
} else {
|
|
||||||
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addFields(rt, rv, nil)
|
|
||||||
|
|
||||||
var writeFields = func(fields [][]int) {
|
|
||||||
for _, fieldIndex := range fields {
|
|
||||||
sft := rt.FieldByIndex(fieldIndex)
|
|
||||||
sf := rv.FieldByIndex(fieldIndex)
|
|
||||||
if isNil(sf) {
|
|
||||||
// Don't write anything for nil fields.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := getOptions(sft.Tag)
|
|
||||||
if opts.skip {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
keyName := sft.Name
|
|
||||||
if opts.name != "" {
|
|
||||||
keyName = opts.name
|
|
||||||
}
|
|
||||||
if opts.omitempty && isEmpty(sf) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if opts.omitzero && isZero(sf) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
enc.encode(key.add(keyName), sf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writeFields(fieldsDirect)
|
|
||||||
writeFields(fieldsSub)
|
|
||||||
}
|
|
||||||
|
|
||||||
// tomlTypeName returns the TOML type name of the Go value's type. It is
|
|
||||||
// used to determine whether the types of array elements are mixed (which is
|
|
||||||
// forbidden). If the Go value is nil, then it is illegal for it to be an array
|
|
||||||
// element, and valueIsNil is returned as true.
|
|
||||||
|
|
||||||
// Returns the TOML type of a Go value. The type may be `nil`, which means
|
|
||||||
// no concrete TOML type could be found.
|
|
||||||
func tomlTypeOfGo(rv reflect.Value) tomlType {
|
|
||||||
if isNil(rv) || !rv.IsValid() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Bool:
|
|
||||||
return tomlBool
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
|
|
||||||
reflect.Int64,
|
|
||||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
|
|
||||||
reflect.Uint64:
|
|
||||||
return tomlInteger
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
return tomlFloat
|
|
||||||
case reflect.Array, reflect.Slice:
|
|
||||||
if typeEqual(tomlHash, tomlArrayType(rv)) {
|
|
||||||
return tomlArrayHash
|
|
||||||
}
|
|
||||||
return tomlArray
|
|
||||||
case reflect.Ptr, reflect.Interface:
|
|
||||||
return tomlTypeOfGo(rv.Elem())
|
|
||||||
case reflect.String:
|
|
||||||
return tomlString
|
|
||||||
case reflect.Map:
|
|
||||||
return tomlHash
|
|
||||||
case reflect.Struct:
|
|
||||||
switch rv.Interface().(type) {
|
|
||||||
case time.Time:
|
|
||||||
return tomlDatetime
|
|
||||||
case TextMarshaler:
|
|
||||||
return tomlString
|
|
||||||
default:
|
|
||||||
return tomlHash
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
panic("unexpected reflect.Kind: " + rv.Kind().String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// tomlArrayType returns the element type of a TOML array. The type returned
|
|
||||||
// may be nil if it cannot be determined (e.g., a nil slice or a zero length
|
|
||||||
// slize). This function may also panic if it finds a type that cannot be
|
|
||||||
// expressed in TOML (such as nil elements, heterogeneous arrays or directly
|
|
||||||
// nested arrays of tables).
|
|
||||||
func tomlArrayType(rv reflect.Value) tomlType {
|
|
||||||
if isNil(rv) || !rv.IsValid() || rv.Len() == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
firstType := tomlTypeOfGo(rv.Index(0))
|
|
||||||
if firstType == nil {
|
|
||||||
encPanic(errArrayNilElement)
|
|
||||||
}
|
|
||||||
|
|
||||||
rvlen := rv.Len()
|
|
||||||
for i := 1; i < rvlen; i++ {
|
|
||||||
elem := rv.Index(i)
|
|
||||||
switch elemType := tomlTypeOfGo(elem); {
|
|
||||||
case elemType == nil:
|
|
||||||
encPanic(errArrayNilElement)
|
|
||||||
case !typeEqual(firstType, elemType):
|
|
||||||
encPanic(errArrayMixedElementTypes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If we have a nested array, then we must make sure that the nested
|
|
||||||
// array contains ONLY primitives.
|
|
||||||
// This checks arbitrarily nested arrays.
|
|
||||||
if typeEqual(firstType, tomlArray) || typeEqual(firstType, tomlArrayHash) {
|
|
||||||
nest := tomlArrayType(eindirect(rv.Index(0)))
|
|
||||||
if typeEqual(nest, tomlHash) || typeEqual(nest, tomlArrayHash) {
|
|
||||||
encPanic(errArrayNoTable)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return firstType
|
|
||||||
}
|
|
||||||
|
|
||||||
type tagOptions struct {
|
|
||||||
skip bool // "-"
|
|
||||||
name string
|
|
||||||
omitempty bool
|
|
||||||
omitzero bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func getOptions(tag reflect.StructTag) tagOptions {
|
|
||||||
t := tag.Get("toml")
|
|
||||||
if t == "-" {
|
|
||||||
return tagOptions{skip: true}
|
|
||||||
}
|
|
||||||
var opts tagOptions
|
|
||||||
parts := strings.Split(t, ",")
|
|
||||||
opts.name = parts[0]
|
|
||||||
for _, s := range parts[1:] {
|
|
||||||
switch s {
|
|
||||||
case "omitempty":
|
|
||||||
opts.omitempty = true
|
|
||||||
case "omitzero":
|
|
||||||
opts.omitzero = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func isZero(rv reflect.Value) bool {
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
||||||
return rv.Int() == 0
|
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
||||||
return rv.Uint() == 0
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
return rv.Float() == 0.0
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func isEmpty(rv reflect.Value) bool {
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
|
|
||||||
return rv.Len() == 0
|
|
||||||
case reflect.Bool:
|
|
||||||
return !rv.Bool()
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) newline() {
|
|
||||||
if enc.hasWritten {
|
|
||||||
enc.wf("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) keyEqElement(key Key, val reflect.Value) {
|
|
||||||
if len(key) == 0 {
|
|
||||||
encPanic(errNoKey)
|
|
||||||
}
|
|
||||||
panicIfInvalidKey(key)
|
|
||||||
enc.wf("%s%s = ", enc.indentStr(key), key.maybeQuoted(len(key)-1))
|
|
||||||
enc.eElement(val)
|
|
||||||
enc.newline()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) wf(format string, v ...interface{}) {
|
|
||||||
if _, err := fmt.Fprintf(enc.w, format, v...); err != nil {
|
|
||||||
encPanic(err)
|
|
||||||
}
|
|
||||||
enc.hasWritten = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (enc *Encoder) indentStr(key Key) string {
|
|
||||||
return strings.Repeat(enc.Indent, len(key)-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func encPanic(err error) {
|
|
||||||
panic(tomlEncodeError{err})
|
|
||||||
}
|
|
||||||
|
|
||||||
func eindirect(v reflect.Value) reflect.Value {
|
|
||||||
switch v.Kind() {
|
|
||||||
case reflect.Ptr, reflect.Interface:
|
|
||||||
return eindirect(v.Elem())
|
|
||||||
default:
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isNil(rv reflect.Value) bool {
|
|
||||||
switch rv.Kind() {
|
|
||||||
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
|
||||||
return rv.IsNil()
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func panicIfInvalidKey(key Key) {
|
|
||||||
for _, k := range key {
|
|
||||||
if len(k) == 0 {
|
|
||||||
encPanic(e("Key '%s' is not a valid table name. Key names "+
|
|
||||||
"cannot be empty.", key.maybeQuotedAll()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isValidKeyName(s string) bool {
|
|
||||||
return len(s) != 0
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
// +build go1.2
|
|
||||||
|
|
||||||
package toml
|
|
||||||
|
|
||||||
// In order to support Go 1.1, we define our own TextMarshaler and
|
|
||||||
// TextUnmarshaler types. For Go 1.2+, we just alias them with the
|
|
||||||
// standard library interfaces.
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TextMarshaler is a synonym for encoding.TextMarshaler. It is defined here
|
|
||||||
// so that Go 1.1 can be supported.
|
|
||||||
type TextMarshaler encoding.TextMarshaler
|
|
||||||
|
|
||||||
// TextUnmarshaler is a synonym for encoding.TextUnmarshaler. It is defined
|
|
||||||
// here so that Go 1.1 can be supported.
|
|
||||||
type TextUnmarshaler encoding.TextUnmarshaler
|
|
@ -1,18 +0,0 @@
|
|||||||
// +build !go1.2
|
|
||||||
|
|
||||||
package toml
|
|
||||||
|
|
||||||
// These interfaces were introduced in Go 1.2, so we add them manually when
|
|
||||||
// compiling for Go 1.1.
|
|
||||||
|
|
||||||
// TextMarshaler is a synonym for encoding.TextMarshaler. It is defined here
|
|
||||||
// so that Go 1.1 can be supported.
|
|
||||||
type TextMarshaler interface {
|
|
||||||
MarshalText() (text []byte, err error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TextUnmarshaler is a synonym for encoding.TextUnmarshaler. It is defined
|
|
||||||
// here so that Go 1.1 can be supported.
|
|
||||||
type TextUnmarshaler interface {
|
|
||||||
UnmarshalText(text []byte) error
|
|
||||||
}
|
|
@ -1,953 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
type itemType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
itemError itemType = iota
|
|
||||||
itemNIL // used in the parser to indicate no type
|
|
||||||
itemEOF
|
|
||||||
itemText
|
|
||||||
itemString
|
|
||||||
itemRawString
|
|
||||||
itemMultilineString
|
|
||||||
itemRawMultilineString
|
|
||||||
itemBool
|
|
||||||
itemInteger
|
|
||||||
itemFloat
|
|
||||||
itemDatetime
|
|
||||||
itemArray // the start of an array
|
|
||||||
itemArrayEnd
|
|
||||||
itemTableStart
|
|
||||||
itemTableEnd
|
|
||||||
itemArrayTableStart
|
|
||||||
itemArrayTableEnd
|
|
||||||
itemKeyStart
|
|
||||||
itemCommentStart
|
|
||||||
itemInlineTableStart
|
|
||||||
itemInlineTableEnd
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
eof = 0
|
|
||||||
comma = ','
|
|
||||||
tableStart = '['
|
|
||||||
tableEnd = ']'
|
|
||||||
arrayTableStart = '['
|
|
||||||
arrayTableEnd = ']'
|
|
||||||
tableSep = '.'
|
|
||||||
keySep = '='
|
|
||||||
arrayStart = '['
|
|
||||||
arrayEnd = ']'
|
|
||||||
commentStart = '#'
|
|
||||||
stringStart = '"'
|
|
||||||
stringEnd = '"'
|
|
||||||
rawStringStart = '\''
|
|
||||||
rawStringEnd = '\''
|
|
||||||
inlineTableStart = '{'
|
|
||||||
inlineTableEnd = '}'
|
|
||||||
)
|
|
||||||
|
|
||||||
type stateFn func(lx *lexer) stateFn
|
|
||||||
|
|
||||||
type lexer struct {
|
|
||||||
input string
|
|
||||||
start int
|
|
||||||
pos int
|
|
||||||
line int
|
|
||||||
state stateFn
|
|
||||||
items chan item
|
|
||||||
|
|
||||||
// Allow for backing up up to three runes.
|
|
||||||
// This is necessary because TOML contains 3-rune tokens (""" and ''').
|
|
||||||
prevWidths [3]int
|
|
||||||
nprev int // how many of prevWidths are in use
|
|
||||||
// If we emit an eof, we can still back up, but it is not OK to call
|
|
||||||
// next again.
|
|
||||||
atEOF bool
|
|
||||||
|
|
||||||
// A stack of state functions used to maintain context.
|
|
||||||
// The idea is to reuse parts of the state machine in various places.
|
|
||||||
// For example, values can appear at the top level or within arbitrarily
|
|
||||||
// nested arrays. The last state on the stack is used after a value has
|
|
||||||
// been lexed. Similarly for comments.
|
|
||||||
stack []stateFn
|
|
||||||
}
|
|
||||||
|
|
||||||
type item struct {
|
|
||||||
typ itemType
|
|
||||||
val string
|
|
||||||
line int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) nextItem() item {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case item := <-lx.items:
|
|
||||||
return item
|
|
||||||
default:
|
|
||||||
lx.state = lx.state(lx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func lex(input string) *lexer {
|
|
||||||
lx := &lexer{
|
|
||||||
input: input,
|
|
||||||
state: lexTop,
|
|
||||||
line: 1,
|
|
||||||
items: make(chan item, 10),
|
|
||||||
stack: make([]stateFn, 0, 10),
|
|
||||||
}
|
|
||||||
return lx
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) push(state stateFn) {
|
|
||||||
lx.stack = append(lx.stack, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) pop() stateFn {
|
|
||||||
if len(lx.stack) == 0 {
|
|
||||||
return lx.errorf("BUG in lexer: no states to pop")
|
|
||||||
}
|
|
||||||
last := lx.stack[len(lx.stack)-1]
|
|
||||||
lx.stack = lx.stack[0 : len(lx.stack)-1]
|
|
||||||
return last
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) current() string {
|
|
||||||
return lx.input[lx.start:lx.pos]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) emit(typ itemType) {
|
|
||||||
lx.items <- item{typ, lx.current(), lx.line}
|
|
||||||
lx.start = lx.pos
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) emitTrim(typ itemType) {
|
|
||||||
lx.items <- item{typ, strings.TrimSpace(lx.current()), lx.line}
|
|
||||||
lx.start = lx.pos
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lx *lexer) next() (r rune) {
|
|
||||||
if lx.atEOF {
|
|
||||||
panic("next called after EOF")
|
|
||||||
}
|
|
||||||
if lx.pos >= len(lx.input) {
|
|
||||||
lx.atEOF = true
|
|
||||||
return eof
|
|
||||||
}
|
|
||||||
|
|
||||||
if lx.input[lx.pos] == '\n' {
|
|
||||||
lx.line++
|
|
||||||
}
|
|
||||||
lx.prevWidths[2] = lx.prevWidths[1]
|
|
||||||
lx.prevWidths[1] = lx.prevWidths[0]
|
|
||||||
if lx.nprev < 3 {
|
|
||||||
lx.nprev++
|
|
||||||
}
|
|
||||||
r, w := utf8.DecodeRuneInString(lx.input[lx.pos:])
|
|
||||||
lx.prevWidths[0] = w
|
|
||||||
lx.pos += w
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignore skips over the pending input before this point.
|
|
||||||
func (lx *lexer) ignore() {
|
|
||||||
lx.start = lx.pos
|
|
||||||
}
|
|
||||||
|
|
||||||
// backup steps back one rune. Can be called only twice between calls to next.
|
|
||||||
func (lx *lexer) backup() {
|
|
||||||
if lx.atEOF {
|
|
||||||
lx.atEOF = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if lx.nprev < 1 {
|
|
||||||
panic("backed up too far")
|
|
||||||
}
|
|
||||||
w := lx.prevWidths[0]
|
|
||||||
lx.prevWidths[0] = lx.prevWidths[1]
|
|
||||||
lx.prevWidths[1] = lx.prevWidths[2]
|
|
||||||
lx.nprev--
|
|
||||||
lx.pos -= w
|
|
||||||
if lx.pos < len(lx.input) && lx.input[lx.pos] == '\n' {
|
|
||||||
lx.line--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// accept consumes the next rune if it's equal to `valid`.
|
|
||||||
func (lx *lexer) accept(valid rune) bool {
|
|
||||||
if lx.next() == valid {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// peek returns but does not consume the next rune in the input.
|
|
||||||
func (lx *lexer) peek() rune {
|
|
||||||
r := lx.next()
|
|
||||||
lx.backup()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// skip ignores all input that matches the given predicate.
|
|
||||||
func (lx *lexer) skip(pred func(rune) bool) {
|
|
||||||
for {
|
|
||||||
r := lx.next()
|
|
||||||
if pred(r) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
lx.ignore()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// errorf stops all lexing by emitting an error and returning `nil`.
|
|
||||||
// Note that any value that is a character is escaped if it's a special
|
|
||||||
// character (newlines, tabs, etc.).
|
|
||||||
func (lx *lexer) errorf(format string, values ...interface{}) stateFn {
|
|
||||||
lx.items <- item{
|
|
||||||
itemError,
|
|
||||||
fmt.Sprintf(format, values...),
|
|
||||||
lx.line,
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexTop consumes elements at the top level of TOML data.
|
|
||||||
func lexTop(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isWhitespace(r) || isNL(r) {
|
|
||||||
return lexSkip(lx, lexTop)
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case commentStart:
|
|
||||||
lx.push(lexTop)
|
|
||||||
return lexCommentStart
|
|
||||||
case tableStart:
|
|
||||||
return lexTableStart
|
|
||||||
case eof:
|
|
||||||
if lx.pos > lx.start {
|
|
||||||
return lx.errorf("unexpected EOF")
|
|
||||||
}
|
|
||||||
lx.emit(itemEOF)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// At this point, the only valid item can be a key, so we back up
|
|
||||||
// and let the key lexer do the rest.
|
|
||||||
lx.backup()
|
|
||||||
lx.push(lexTopEnd)
|
|
||||||
return lexKeyStart
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexTopEnd is entered whenever a top-level item has been consumed. (A value
|
|
||||||
// or a table.) It must see only whitespace, and will turn back to lexTop
|
|
||||||
// upon a newline. If it sees EOF, it will quit the lexer successfully.
|
|
||||||
func lexTopEnd(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case r == commentStart:
|
|
||||||
// a comment will read to a newline for us.
|
|
||||||
lx.push(lexTop)
|
|
||||||
return lexCommentStart
|
|
||||||
case isWhitespace(r):
|
|
||||||
return lexTopEnd
|
|
||||||
case isNL(r):
|
|
||||||
lx.ignore()
|
|
||||||
return lexTop
|
|
||||||
case r == eof:
|
|
||||||
lx.emit(itemEOF)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return lx.errorf("expected a top-level item to end with a newline, "+
|
|
||||||
"comment, or EOF, but got %q instead", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexTable lexes the beginning of a table. Namely, it makes sure that
|
|
||||||
// it starts with a character other than '.' and ']'.
|
|
||||||
// It assumes that '[' has already been consumed.
|
|
||||||
// It also handles the case that this is an item in an array of tables.
|
|
||||||
// e.g., '[[name]]'.
|
|
||||||
func lexTableStart(lx *lexer) stateFn {
|
|
||||||
if lx.peek() == arrayTableStart {
|
|
||||||
lx.next()
|
|
||||||
lx.emit(itemArrayTableStart)
|
|
||||||
lx.push(lexArrayTableEnd)
|
|
||||||
} else {
|
|
||||||
lx.emit(itemTableStart)
|
|
||||||
lx.push(lexTableEnd)
|
|
||||||
}
|
|
||||||
return lexTableNameStart
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexTableEnd(lx *lexer) stateFn {
|
|
||||||
lx.emit(itemTableEnd)
|
|
||||||
return lexTopEnd
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexArrayTableEnd(lx *lexer) stateFn {
|
|
||||||
if r := lx.next(); r != arrayTableEnd {
|
|
||||||
return lx.errorf("expected end of table array name delimiter %q, "+
|
|
||||||
"but got %q instead", arrayTableEnd, r)
|
|
||||||
}
|
|
||||||
lx.emit(itemArrayTableEnd)
|
|
||||||
return lexTopEnd
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexTableNameStart(lx *lexer) stateFn {
|
|
||||||
lx.skip(isWhitespace)
|
|
||||||
switch r := lx.peek(); {
|
|
||||||
case r == tableEnd || r == eof:
|
|
||||||
return lx.errorf("unexpected end of table name " +
|
|
||||||
"(table names cannot be empty)")
|
|
||||||
case r == tableSep:
|
|
||||||
return lx.errorf("unexpected table separator " +
|
|
||||||
"(table names cannot be empty)")
|
|
||||||
case r == stringStart || r == rawStringStart:
|
|
||||||
lx.ignore()
|
|
||||||
lx.push(lexTableNameEnd)
|
|
||||||
return lexValue // reuse string lexing
|
|
||||||
default:
|
|
||||||
return lexBareTableName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexBareTableName lexes the name of a table. It assumes that at least one
|
|
||||||
// valid character for the table has already been read.
|
|
||||||
func lexBareTableName(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isBareKeyChar(r) {
|
|
||||||
return lexBareTableName
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemText)
|
|
||||||
return lexTableNameEnd
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexTableNameEnd reads the end of a piece of a table name, optionally
|
|
||||||
// consuming whitespace.
|
|
||||||
func lexTableNameEnd(lx *lexer) stateFn {
|
|
||||||
lx.skip(isWhitespace)
|
|
||||||
switch r := lx.next(); {
|
|
||||||
case isWhitespace(r):
|
|
||||||
return lexTableNameEnd
|
|
||||||
case r == tableSep:
|
|
||||||
lx.ignore()
|
|
||||||
return lexTableNameStart
|
|
||||||
case r == tableEnd:
|
|
||||||
return lx.pop()
|
|
||||||
default:
|
|
||||||
return lx.errorf("expected '.' or ']' to end table name, "+
|
|
||||||
"but got %q instead", r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexKeyStart consumes a key name up until the first non-whitespace character.
|
|
||||||
// lexKeyStart will ignore whitespace.
|
|
||||||
func lexKeyStart(lx *lexer) stateFn {
|
|
||||||
r := lx.peek()
|
|
||||||
switch {
|
|
||||||
case r == keySep:
|
|
||||||
return lx.errorf("unexpected key separator %q", keySep)
|
|
||||||
case isWhitespace(r) || isNL(r):
|
|
||||||
lx.next()
|
|
||||||
return lexSkip(lx, lexKeyStart)
|
|
||||||
case r == stringStart || r == rawStringStart:
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemKeyStart)
|
|
||||||
lx.push(lexKeyEnd)
|
|
||||||
return lexValue // reuse string lexing
|
|
||||||
default:
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemKeyStart)
|
|
||||||
return lexBareKey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexBareKey consumes the text of a bare key. Assumes that the first character
|
|
||||||
// (which is not whitespace) has not yet been consumed.
|
|
||||||
func lexBareKey(lx *lexer) stateFn {
|
|
||||||
switch r := lx.next(); {
|
|
||||||
case isBareKeyChar(r):
|
|
||||||
return lexBareKey
|
|
||||||
case isWhitespace(r):
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemText)
|
|
||||||
return lexKeyEnd
|
|
||||||
case r == keySep:
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemText)
|
|
||||||
return lexKeyEnd
|
|
||||||
default:
|
|
||||||
return lx.errorf("bare keys cannot contain %q", r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexKeyEnd consumes the end of a key and trims whitespace (up to the key
|
|
||||||
// separator).
|
|
||||||
func lexKeyEnd(lx *lexer) stateFn {
|
|
||||||
switch r := lx.next(); {
|
|
||||||
case r == keySep:
|
|
||||||
return lexSkip(lx, lexValue)
|
|
||||||
case isWhitespace(r):
|
|
||||||
return lexSkip(lx, lexKeyEnd)
|
|
||||||
default:
|
|
||||||
return lx.errorf("expected key separator %q, but got %q instead",
|
|
||||||
keySep, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexValue starts the consumption of a value anywhere a value is expected.
|
|
||||||
// lexValue will ignore whitespace.
|
|
||||||
// After a value is lexed, the last state on the next is popped and returned.
|
|
||||||
func lexValue(lx *lexer) stateFn {
|
|
||||||
// We allow whitespace to precede a value, but NOT newlines.
|
|
||||||
// In array syntax, the array states are responsible for ignoring newlines.
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case isWhitespace(r):
|
|
||||||
return lexSkip(lx, lexValue)
|
|
||||||
case isDigit(r):
|
|
||||||
lx.backup() // avoid an extra state and use the same as above
|
|
||||||
return lexNumberOrDateStart
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case arrayStart:
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemArray)
|
|
||||||
return lexArrayValue
|
|
||||||
case inlineTableStart:
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemInlineTableStart)
|
|
||||||
return lexInlineTableValue
|
|
||||||
case stringStart:
|
|
||||||
if lx.accept(stringStart) {
|
|
||||||
if lx.accept(stringStart) {
|
|
||||||
lx.ignore() // Ignore """
|
|
||||||
return lexMultilineString
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
}
|
|
||||||
lx.ignore() // ignore the '"'
|
|
||||||
return lexString
|
|
||||||
case rawStringStart:
|
|
||||||
if lx.accept(rawStringStart) {
|
|
||||||
if lx.accept(rawStringStart) {
|
|
||||||
lx.ignore() // Ignore """
|
|
||||||
return lexMultilineRawString
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
}
|
|
||||||
lx.ignore() // ignore the "'"
|
|
||||||
return lexRawString
|
|
||||||
case '+', '-':
|
|
||||||
return lexNumberStart
|
|
||||||
case '.': // special error case, be kind to users
|
|
||||||
return lx.errorf("floats must start with a digit, not '.'")
|
|
||||||
}
|
|
||||||
if unicode.IsLetter(r) {
|
|
||||||
// Be permissive here; lexBool will give a nice error if the
|
|
||||||
// user wrote something like
|
|
||||||
// x = foo
|
|
||||||
// (i.e. not 'true' or 'false' but is something else word-like.)
|
|
||||||
lx.backup()
|
|
||||||
return lexBool
|
|
||||||
}
|
|
||||||
return lx.errorf("expected value but found %q instead", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexArrayValue consumes one value in an array. It assumes that '[' or ','
|
|
||||||
// have already been consumed. All whitespace and newlines are ignored.
|
|
||||||
func lexArrayValue(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case isWhitespace(r) || isNL(r):
|
|
||||||
return lexSkip(lx, lexArrayValue)
|
|
||||||
case r == commentStart:
|
|
||||||
lx.push(lexArrayValue)
|
|
||||||
return lexCommentStart
|
|
||||||
case r == comma:
|
|
||||||
return lx.errorf("unexpected comma")
|
|
||||||
case r == arrayEnd:
|
|
||||||
// NOTE(caleb): The spec isn't clear about whether you can have
|
|
||||||
// a trailing comma or not, so we'll allow it.
|
|
||||||
return lexArrayEnd
|
|
||||||
}
|
|
||||||
|
|
||||||
lx.backup()
|
|
||||||
lx.push(lexArrayValueEnd)
|
|
||||||
return lexValue
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexArrayValueEnd consumes everything between the end of an array value and
|
|
||||||
// the next value (or the end of the array): it ignores whitespace and newlines
|
|
||||||
// and expects either a ',' or a ']'.
|
|
||||||
func lexArrayValueEnd(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case isWhitespace(r) || isNL(r):
|
|
||||||
return lexSkip(lx, lexArrayValueEnd)
|
|
||||||
case r == commentStart:
|
|
||||||
lx.push(lexArrayValueEnd)
|
|
||||||
return lexCommentStart
|
|
||||||
case r == comma:
|
|
||||||
lx.ignore()
|
|
||||||
return lexArrayValue // move on to the next value
|
|
||||||
case r == arrayEnd:
|
|
||||||
return lexArrayEnd
|
|
||||||
}
|
|
||||||
return lx.errorf(
|
|
||||||
"expected a comma or array terminator %q, but got %q instead",
|
|
||||||
arrayEnd, r,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexArrayEnd finishes the lexing of an array.
|
|
||||||
// It assumes that a ']' has just been consumed.
|
|
||||||
func lexArrayEnd(lx *lexer) stateFn {
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemArrayEnd)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexInlineTableValue consumes one key/value pair in an inline table.
|
|
||||||
// It assumes that '{' or ',' have already been consumed. Whitespace is ignored.
|
|
||||||
func lexInlineTableValue(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case isWhitespace(r):
|
|
||||||
return lexSkip(lx, lexInlineTableValue)
|
|
||||||
case isNL(r):
|
|
||||||
return lx.errorf("newlines not allowed within inline tables")
|
|
||||||
case r == commentStart:
|
|
||||||
lx.push(lexInlineTableValue)
|
|
||||||
return lexCommentStart
|
|
||||||
case r == comma:
|
|
||||||
return lx.errorf("unexpected comma")
|
|
||||||
case r == inlineTableEnd:
|
|
||||||
return lexInlineTableEnd
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
lx.push(lexInlineTableValueEnd)
|
|
||||||
return lexKeyStart
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexInlineTableValueEnd consumes everything between the end of an inline table
|
|
||||||
// key/value pair and the next pair (or the end of the table):
|
|
||||||
// it ignores whitespace and expects either a ',' or a '}'.
|
|
||||||
func lexInlineTableValueEnd(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case isWhitespace(r):
|
|
||||||
return lexSkip(lx, lexInlineTableValueEnd)
|
|
||||||
case isNL(r):
|
|
||||||
return lx.errorf("newlines not allowed within inline tables")
|
|
||||||
case r == commentStart:
|
|
||||||
lx.push(lexInlineTableValueEnd)
|
|
||||||
return lexCommentStart
|
|
||||||
case r == comma:
|
|
||||||
lx.ignore()
|
|
||||||
return lexInlineTableValue
|
|
||||||
case r == inlineTableEnd:
|
|
||||||
return lexInlineTableEnd
|
|
||||||
}
|
|
||||||
return lx.errorf("expected a comma or an inline table terminator %q, "+
|
|
||||||
"but got %q instead", inlineTableEnd, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexInlineTableEnd finishes the lexing of an inline table.
|
|
||||||
// It assumes that a '}' has just been consumed.
|
|
||||||
func lexInlineTableEnd(lx *lexer) stateFn {
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemInlineTableEnd)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexString consumes the inner contents of a string. It assumes that the
|
|
||||||
// beginning '"' has already been consumed and ignored.
|
|
||||||
func lexString(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case r == eof:
|
|
||||||
return lx.errorf("unexpected EOF")
|
|
||||||
case isNL(r):
|
|
||||||
return lx.errorf("strings cannot contain newlines")
|
|
||||||
case r == '\\':
|
|
||||||
lx.push(lexString)
|
|
||||||
return lexStringEscape
|
|
||||||
case r == stringEnd:
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemString)
|
|
||||||
lx.next()
|
|
||||||
lx.ignore()
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
return lexString
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexMultilineString consumes the inner contents of a string. It assumes that
|
|
||||||
// the beginning '"""' has already been consumed and ignored.
|
|
||||||
func lexMultilineString(lx *lexer) stateFn {
|
|
||||||
switch lx.next() {
|
|
||||||
case eof:
|
|
||||||
return lx.errorf("unexpected EOF")
|
|
||||||
case '\\':
|
|
||||||
return lexMultilineStringEscape
|
|
||||||
case stringEnd:
|
|
||||||
if lx.accept(stringEnd) {
|
|
||||||
if lx.accept(stringEnd) {
|
|
||||||
lx.backup()
|
|
||||||
lx.backup()
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemMultilineString)
|
|
||||||
lx.next()
|
|
||||||
lx.next()
|
|
||||||
lx.next()
|
|
||||||
lx.ignore()
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lexMultilineString
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexRawString consumes a raw string. Nothing can be escaped in such a string.
|
|
||||||
// It assumes that the beginning "'" has already been consumed and ignored.
|
|
||||||
func lexRawString(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch {
|
|
||||||
case r == eof:
|
|
||||||
return lx.errorf("unexpected EOF")
|
|
||||||
case isNL(r):
|
|
||||||
return lx.errorf("strings cannot contain newlines")
|
|
||||||
case r == rawStringEnd:
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemRawString)
|
|
||||||
lx.next()
|
|
||||||
lx.ignore()
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
return lexRawString
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexMultilineRawString consumes a raw string. Nothing can be escaped in such
|
|
||||||
// a string. It assumes that the beginning "'''" has already been consumed and
|
|
||||||
// ignored.
|
|
||||||
func lexMultilineRawString(lx *lexer) stateFn {
|
|
||||||
switch lx.next() {
|
|
||||||
case eof:
|
|
||||||
return lx.errorf("unexpected EOF")
|
|
||||||
case rawStringEnd:
|
|
||||||
if lx.accept(rawStringEnd) {
|
|
||||||
if lx.accept(rawStringEnd) {
|
|
||||||
lx.backup()
|
|
||||||
lx.backup()
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemRawMultilineString)
|
|
||||||
lx.next()
|
|
||||||
lx.next()
|
|
||||||
lx.next()
|
|
||||||
lx.ignore()
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lexMultilineRawString
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexMultilineStringEscape consumes an escaped character. It assumes that the
|
|
||||||
// preceding '\\' has already been consumed.
|
|
||||||
func lexMultilineStringEscape(lx *lexer) stateFn {
|
|
||||||
// Handle the special case first:
|
|
||||||
if isNL(lx.next()) {
|
|
||||||
return lexMultilineString
|
|
||||||
}
|
|
||||||
lx.backup()
|
|
||||||
lx.push(lexMultilineString)
|
|
||||||
return lexStringEscape(lx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexStringEscape(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
switch r {
|
|
||||||
case 'b':
|
|
||||||
fallthrough
|
|
||||||
case 't':
|
|
||||||
fallthrough
|
|
||||||
case 'n':
|
|
||||||
fallthrough
|
|
||||||
case 'f':
|
|
||||||
fallthrough
|
|
||||||
case 'r':
|
|
||||||
fallthrough
|
|
||||||
case '"':
|
|
||||||
fallthrough
|
|
||||||
case '\\':
|
|
||||||
return lx.pop()
|
|
||||||
case 'u':
|
|
||||||
return lexShortUnicodeEscape
|
|
||||||
case 'U':
|
|
||||||
return lexLongUnicodeEscape
|
|
||||||
}
|
|
||||||
return lx.errorf("invalid escape character %q; only the following "+
|
|
||||||
"escape characters are allowed: "+
|
|
||||||
`\b, \t, \n, \f, \r, \", \\, \uXXXX, and \UXXXXXXXX`, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexShortUnicodeEscape(lx *lexer) stateFn {
|
|
||||||
var r rune
|
|
||||||
for i := 0; i < 4; i++ {
|
|
||||||
r = lx.next()
|
|
||||||
if !isHexadecimal(r) {
|
|
||||||
return lx.errorf(`expected four hexadecimal digits after '\u', `+
|
|
||||||
"but got %q instead", lx.current())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexLongUnicodeEscape(lx *lexer) stateFn {
|
|
||||||
var r rune
|
|
||||||
for i := 0; i < 8; i++ {
|
|
||||||
r = lx.next()
|
|
||||||
if !isHexadecimal(r) {
|
|
||||||
return lx.errorf(`expected eight hexadecimal digits after '\U', `+
|
|
||||||
"but got %q instead", lx.current())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexNumberOrDateStart consumes either an integer, a float, or datetime.
|
|
||||||
func lexNumberOrDateStart(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isDigit(r) {
|
|
||||||
return lexNumberOrDate
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case '_':
|
|
||||||
return lexNumber
|
|
||||||
case 'e', 'E':
|
|
||||||
return lexFloat
|
|
||||||
case '.':
|
|
||||||
return lx.errorf("floats must start with a digit, not '.'")
|
|
||||||
}
|
|
||||||
return lx.errorf("expected a digit but got %q", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexNumberOrDate consumes either an integer, float or datetime.
|
|
||||||
func lexNumberOrDate(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isDigit(r) {
|
|
||||||
return lexNumberOrDate
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case '-':
|
|
||||||
return lexDatetime
|
|
||||||
case '_':
|
|
||||||
return lexNumber
|
|
||||||
case '.', 'e', 'E':
|
|
||||||
return lexFloat
|
|
||||||
}
|
|
||||||
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemInteger)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexDatetime consumes a Datetime, to a first approximation.
|
|
||||||
// The parser validates that it matches one of the accepted formats.
|
|
||||||
func lexDatetime(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isDigit(r) {
|
|
||||||
return lexDatetime
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case '-', 'T', ':', '.', 'Z', '+':
|
|
||||||
return lexDatetime
|
|
||||||
}
|
|
||||||
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemDatetime)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexNumberStart consumes either an integer or a float. It assumes that a sign
|
|
||||||
// has already been read, but that *no* digits have been consumed.
|
|
||||||
// lexNumberStart will move to the appropriate integer or float states.
|
|
||||||
func lexNumberStart(lx *lexer) stateFn {
|
|
||||||
// We MUST see a digit. Even floats have to start with a digit.
|
|
||||||
r := lx.next()
|
|
||||||
if !isDigit(r) {
|
|
||||||
if r == '.' {
|
|
||||||
return lx.errorf("floats must start with a digit, not '.'")
|
|
||||||
}
|
|
||||||
return lx.errorf("expected a digit but got %q", r)
|
|
||||||
}
|
|
||||||
return lexNumber
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexNumber consumes an integer or a float after seeing the first digit.
|
|
||||||
func lexNumber(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isDigit(r) {
|
|
||||||
return lexNumber
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case '_':
|
|
||||||
return lexNumber
|
|
||||||
case '.', 'e', 'E':
|
|
||||||
return lexFloat
|
|
||||||
}
|
|
||||||
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemInteger)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexFloat consumes the elements of a float. It allows any sequence of
|
|
||||||
// float-like characters, so floats emitted by the lexer are only a first
|
|
||||||
// approximation and must be validated by the parser.
|
|
||||||
func lexFloat(lx *lexer) stateFn {
|
|
||||||
r := lx.next()
|
|
||||||
if isDigit(r) {
|
|
||||||
return lexFloat
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case '_', '.', '-', '+', 'e', 'E':
|
|
||||||
return lexFloat
|
|
||||||
}
|
|
||||||
|
|
||||||
lx.backup()
|
|
||||||
lx.emit(itemFloat)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexBool consumes a bool string: 'true' or 'false.
|
|
||||||
func lexBool(lx *lexer) stateFn {
|
|
||||||
var rs []rune
|
|
||||||
for {
|
|
||||||
r := lx.next()
|
|
||||||
if !unicode.IsLetter(r) {
|
|
||||||
lx.backup()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
rs = append(rs, r)
|
|
||||||
}
|
|
||||||
s := string(rs)
|
|
||||||
switch s {
|
|
||||||
case "true", "false":
|
|
||||||
lx.emit(itemBool)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
return lx.errorf("expected value but found %q instead", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexCommentStart begins the lexing of a comment. It will emit
|
|
||||||
// itemCommentStart and consume no characters, passing control to lexComment.
|
|
||||||
func lexCommentStart(lx *lexer) stateFn {
|
|
||||||
lx.ignore()
|
|
||||||
lx.emit(itemCommentStart)
|
|
||||||
return lexComment
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexComment lexes an entire comment. It assumes that '#' has been consumed.
|
|
||||||
// It will consume *up to* the first newline character, and pass control
|
|
||||||
// back to the last state on the stack.
|
|
||||||
func lexComment(lx *lexer) stateFn {
|
|
||||||
r := lx.peek()
|
|
||||||
if isNL(r) || r == eof {
|
|
||||||
lx.emit(itemText)
|
|
||||||
return lx.pop()
|
|
||||||
}
|
|
||||||
lx.next()
|
|
||||||
return lexComment
|
|
||||||
}
|
|
||||||
|
|
||||||
// lexSkip ignores all slurped input and moves on to the next state.
|
|
||||||
func lexSkip(lx *lexer, nextState stateFn) stateFn {
|
|
||||||
return func(lx *lexer) stateFn {
|
|
||||||
lx.ignore()
|
|
||||||
return nextState
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// isWhitespace returns true if `r` is a whitespace character according
|
|
||||||
// to the spec.
|
|
||||||
func isWhitespace(r rune) bool {
|
|
||||||
return r == '\t' || r == ' '
|
|
||||||
}
|
|
||||||
|
|
||||||
func isNL(r rune) bool {
|
|
||||||
return r == '\n' || r == '\r'
|
|
||||||
}
|
|
||||||
|
|
||||||
func isDigit(r rune) bool {
|
|
||||||
return r >= '0' && r <= '9'
|
|
||||||
}
|
|
||||||
|
|
||||||
func isHexadecimal(r rune) bool {
|
|
||||||
return (r >= '0' && r <= '9') ||
|
|
||||||
(r >= 'a' && r <= 'f') ||
|
|
||||||
(r >= 'A' && r <= 'F')
|
|
||||||
}
|
|
||||||
|
|
||||||
func isBareKeyChar(r rune) bool {
|
|
||||||
return (r >= 'A' && r <= 'Z') ||
|
|
||||||
(r >= 'a' && r <= 'z') ||
|
|
||||||
(r >= '0' && r <= '9') ||
|
|
||||||
r == '_' ||
|
|
||||||
r == '-'
|
|
||||||
}
|
|
||||||
|
|
||||||
func (itype itemType) String() string {
|
|
||||||
switch itype {
|
|
||||||
case itemError:
|
|
||||||
return "Error"
|
|
||||||
case itemNIL:
|
|
||||||
return "NIL"
|
|
||||||
case itemEOF:
|
|
||||||
return "EOF"
|
|
||||||
case itemText:
|
|
||||||
return "Text"
|
|
||||||
case itemString, itemRawString, itemMultilineString, itemRawMultilineString:
|
|
||||||
return "String"
|
|
||||||
case itemBool:
|
|
||||||
return "Bool"
|
|
||||||
case itemInteger:
|
|
||||||
return "Integer"
|
|
||||||
case itemFloat:
|
|
||||||
return "Float"
|
|
||||||
case itemDatetime:
|
|
||||||
return "DateTime"
|
|
||||||
case itemTableStart:
|
|
||||||
return "TableStart"
|
|
||||||
case itemTableEnd:
|
|
||||||
return "TableEnd"
|
|
||||||
case itemKeyStart:
|
|
||||||
return "KeyStart"
|
|
||||||
case itemArray:
|
|
||||||
return "Array"
|
|
||||||
case itemArrayEnd:
|
|
||||||
return "ArrayEnd"
|
|
||||||
case itemCommentStart:
|
|
||||||
return "CommentStart"
|
|
||||||
}
|
|
||||||
panic(fmt.Sprintf("BUG: Unknown type '%d'.", int(itype)))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (item item) String() string {
|
|
||||||
return fmt.Sprintf("(%s, %s)", item.typ.String(), item.val)
|
|
||||||
}
|
|
@ -1,592 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
type parser struct {
|
|
||||||
mapping map[string]interface{}
|
|
||||||
types map[string]tomlType
|
|
||||||
lx *lexer
|
|
||||||
|
|
||||||
// A list of keys in the order that they appear in the TOML data.
|
|
||||||
ordered []Key
|
|
||||||
|
|
||||||
// the full key for the current hash in scope
|
|
||||||
context Key
|
|
||||||
|
|
||||||
// the base key name for everything except hashes
|
|
||||||
currentKey string
|
|
||||||
|
|
||||||
// rough approximation of line number
|
|
||||||
approxLine int
|
|
||||||
|
|
||||||
// A map of 'key.group.names' to whether they were created implicitly.
|
|
||||||
implicits map[string]bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type parseError string
|
|
||||||
|
|
||||||
func (pe parseError) Error() string {
|
|
||||||
return string(pe)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parse(data string) (p *parser, err error) {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
var ok bool
|
|
||||||
if err, ok = r.(parseError); ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
panic(r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
p = &parser{
|
|
||||||
mapping: make(map[string]interface{}),
|
|
||||||
types: make(map[string]tomlType),
|
|
||||||
lx: lex(data),
|
|
||||||
ordered: make([]Key, 0),
|
|
||||||
implicits: make(map[string]bool),
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
item := p.next()
|
|
||||||
if item.typ == itemEOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
p.topLevel(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) panicf(format string, v ...interface{}) {
|
|
||||||
msg := fmt.Sprintf("Near line %d (last key parsed '%s'): %s",
|
|
||||||
p.approxLine, p.current(), fmt.Sprintf(format, v...))
|
|
||||||
panic(parseError(msg))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) next() item {
|
|
||||||
it := p.lx.nextItem()
|
|
||||||
if it.typ == itemError {
|
|
||||||
p.panicf("%s", it.val)
|
|
||||||
}
|
|
||||||
return it
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) bug(format string, v ...interface{}) {
|
|
||||||
panic(fmt.Sprintf("BUG: "+format+"\n\n", v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) expect(typ itemType) item {
|
|
||||||
it := p.next()
|
|
||||||
p.assertEqual(typ, it.typ)
|
|
||||||
return it
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) assertEqual(expected, got itemType) {
|
|
||||||
if expected != got {
|
|
||||||
p.bug("Expected '%s' but got '%s'.", expected, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) topLevel(item item) {
|
|
||||||
switch item.typ {
|
|
||||||
case itemCommentStart:
|
|
||||||
p.approxLine = item.line
|
|
||||||
p.expect(itemText)
|
|
||||||
case itemTableStart:
|
|
||||||
kg := p.next()
|
|
||||||
p.approxLine = kg.line
|
|
||||||
|
|
||||||
var key Key
|
|
||||||
for ; kg.typ != itemTableEnd && kg.typ != itemEOF; kg = p.next() {
|
|
||||||
key = append(key, p.keyString(kg))
|
|
||||||
}
|
|
||||||
p.assertEqual(itemTableEnd, kg.typ)
|
|
||||||
|
|
||||||
p.establishContext(key, false)
|
|
||||||
p.setType("", tomlHash)
|
|
||||||
p.ordered = append(p.ordered, key)
|
|
||||||
case itemArrayTableStart:
|
|
||||||
kg := p.next()
|
|
||||||
p.approxLine = kg.line
|
|
||||||
|
|
||||||
var key Key
|
|
||||||
for ; kg.typ != itemArrayTableEnd && kg.typ != itemEOF; kg = p.next() {
|
|
||||||
key = append(key, p.keyString(kg))
|
|
||||||
}
|
|
||||||
p.assertEqual(itemArrayTableEnd, kg.typ)
|
|
||||||
|
|
||||||
p.establishContext(key, true)
|
|
||||||
p.setType("", tomlArrayHash)
|
|
||||||
p.ordered = append(p.ordered, key)
|
|
||||||
case itemKeyStart:
|
|
||||||
kname := p.next()
|
|
||||||
p.approxLine = kname.line
|
|
||||||
p.currentKey = p.keyString(kname)
|
|
||||||
|
|
||||||
val, typ := p.value(p.next())
|
|
||||||
p.setValue(p.currentKey, val)
|
|
||||||
p.setType(p.currentKey, typ)
|
|
||||||
p.ordered = append(p.ordered, p.context.add(p.currentKey))
|
|
||||||
p.currentKey = ""
|
|
||||||
default:
|
|
||||||
p.bug("Unexpected type at top level: %s", item.typ)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets a string for a key (or part of a key in a table name).
|
|
||||||
func (p *parser) keyString(it item) string {
|
|
||||||
switch it.typ {
|
|
||||||
case itemText:
|
|
||||||
return it.val
|
|
||||||
case itemString, itemMultilineString,
|
|
||||||
itemRawString, itemRawMultilineString:
|
|
||||||
s, _ := p.value(it)
|
|
||||||
return s.(string)
|
|
||||||
default:
|
|
||||||
p.bug("Unexpected key type: %s", it.typ)
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// value translates an expected value from the lexer into a Go value wrapped
|
|
||||||
// as an empty interface.
|
|
||||||
func (p *parser) value(it item) (interface{}, tomlType) {
|
|
||||||
switch it.typ {
|
|
||||||
case itemString:
|
|
||||||
return p.replaceEscapes(it.val), p.typeOfPrimitive(it)
|
|
||||||
case itemMultilineString:
|
|
||||||
trimmed := stripFirstNewline(stripEscapedWhitespace(it.val))
|
|
||||||
return p.replaceEscapes(trimmed), p.typeOfPrimitive(it)
|
|
||||||
case itemRawString:
|
|
||||||
return it.val, p.typeOfPrimitive(it)
|
|
||||||
case itemRawMultilineString:
|
|
||||||
return stripFirstNewline(it.val), p.typeOfPrimitive(it)
|
|
||||||
case itemBool:
|
|
||||||
switch it.val {
|
|
||||||
case "true":
|
|
||||||
return true, p.typeOfPrimitive(it)
|
|
||||||
case "false":
|
|
||||||
return false, p.typeOfPrimitive(it)
|
|
||||||
}
|
|
||||||
p.bug("Expected boolean value, but got '%s'.", it.val)
|
|
||||||
case itemInteger:
|
|
||||||
if !numUnderscoresOK(it.val) {
|
|
||||||
p.panicf("Invalid integer %q: underscores must be surrounded by digits",
|
|
||||||
it.val)
|
|
||||||
}
|
|
||||||
val := strings.Replace(it.val, "_", "", -1)
|
|
||||||
num, err := strconv.ParseInt(val, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
// Distinguish integer values. Normally, it'd be a bug if the lexer
|
|
||||||
// provides an invalid integer, but it's possible that the number is
|
|
||||||
// out of range of valid values (which the lexer cannot determine).
|
|
||||||
// So mark the former as a bug but the latter as a legitimate user
|
|
||||||
// error.
|
|
||||||
if e, ok := err.(*strconv.NumError); ok &&
|
|
||||||
e.Err == strconv.ErrRange {
|
|
||||||
|
|
||||||
p.panicf("Integer '%s' is out of the range of 64-bit "+
|
|
||||||
"signed integers.", it.val)
|
|
||||||
} else {
|
|
||||||
p.bug("Expected integer value, but got '%s'.", it.val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return num, p.typeOfPrimitive(it)
|
|
||||||
case itemFloat:
|
|
||||||
parts := strings.FieldsFunc(it.val, func(r rune) bool {
|
|
||||||
switch r {
|
|
||||||
case '.', 'e', 'E':
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
for _, part := range parts {
|
|
||||||
if !numUnderscoresOK(part) {
|
|
||||||
p.panicf("Invalid float %q: underscores must be "+
|
|
||||||
"surrounded by digits", it.val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !numPeriodsOK(it.val) {
|
|
||||||
// As a special case, numbers like '123.' or '1.e2',
|
|
||||||
// which are valid as far as Go/strconv are concerned,
|
|
||||||
// must be rejected because TOML says that a fractional
|
|
||||||
// part consists of '.' followed by 1+ digits.
|
|
||||||
p.panicf("Invalid float %q: '.' must be followed "+
|
|
||||||
"by one or more digits", it.val)
|
|
||||||
}
|
|
||||||
val := strings.Replace(it.val, "_", "", -1)
|
|
||||||
num, err := strconv.ParseFloat(val, 64)
|
|
||||||
if err != nil {
|
|
||||||
if e, ok := err.(*strconv.NumError); ok &&
|
|
||||||
e.Err == strconv.ErrRange {
|
|
||||||
|
|
||||||
p.panicf("Float '%s' is out of the range of 64-bit "+
|
|
||||||
"IEEE-754 floating-point numbers.", it.val)
|
|
||||||
} else {
|
|
||||||
p.panicf("Invalid float value: %q", it.val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return num, p.typeOfPrimitive(it)
|
|
||||||
case itemDatetime:
|
|
||||||
var t time.Time
|
|
||||||
var ok bool
|
|
||||||
var err error
|
|
||||||
for _, format := range []string{
|
|
||||||
"2006-01-02T15:04:05Z07:00",
|
|
||||||
"2006-01-02T15:04:05",
|
|
||||||
"2006-01-02",
|
|
||||||
} {
|
|
||||||
t, err = time.ParseInLocation(format, it.val, time.Local)
|
|
||||||
if err == nil {
|
|
||||||
ok = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
p.panicf("Invalid TOML Datetime: %q.", it.val)
|
|
||||||
}
|
|
||||||
return t, p.typeOfPrimitive(it)
|
|
||||||
case itemArray:
|
|
||||||
array := make([]interface{}, 0)
|
|
||||||
types := make([]tomlType, 0)
|
|
||||||
|
|
||||||
for it = p.next(); it.typ != itemArrayEnd; it = p.next() {
|
|
||||||
if it.typ == itemCommentStart {
|
|
||||||
p.expect(itemText)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val, typ := p.value(it)
|
|
||||||
array = append(array, val)
|
|
||||||
types = append(types, typ)
|
|
||||||
}
|
|
||||||
return array, p.typeOfArray(types)
|
|
||||||
case itemInlineTableStart:
|
|
||||||
var (
|
|
||||||
hash = make(map[string]interface{})
|
|
||||||
outerContext = p.context
|
|
||||||
outerKey = p.currentKey
|
|
||||||
)
|
|
||||||
|
|
||||||
p.context = append(p.context, p.currentKey)
|
|
||||||
p.currentKey = ""
|
|
||||||
for it := p.next(); it.typ != itemInlineTableEnd; it = p.next() {
|
|
||||||
if it.typ != itemKeyStart {
|
|
||||||
p.bug("Expected key start but instead found %q, around line %d",
|
|
||||||
it.val, p.approxLine)
|
|
||||||
}
|
|
||||||
if it.typ == itemCommentStart {
|
|
||||||
p.expect(itemText)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// retrieve key
|
|
||||||
k := p.next()
|
|
||||||
p.approxLine = k.line
|
|
||||||
kname := p.keyString(k)
|
|
||||||
|
|
||||||
// retrieve value
|
|
||||||
p.currentKey = kname
|
|
||||||
val, typ := p.value(p.next())
|
|
||||||
// make sure we keep metadata up to date
|
|
||||||
p.setType(kname, typ)
|
|
||||||
p.ordered = append(p.ordered, p.context.add(p.currentKey))
|
|
||||||
hash[kname] = val
|
|
||||||
}
|
|
||||||
p.context = outerContext
|
|
||||||
p.currentKey = outerKey
|
|
||||||
return hash, tomlHash
|
|
||||||
}
|
|
||||||
p.bug("Unexpected value type: %s", it.typ)
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
|
|
||||||
// numUnderscoresOK checks whether each underscore in s is surrounded by
|
|
||||||
// characters that are not underscores.
|
|
||||||
func numUnderscoresOK(s string) bool {
|
|
||||||
accept := false
|
|
||||||
for _, r := range s {
|
|
||||||
if r == '_' {
|
|
||||||
if !accept {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
accept = false
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
accept = true
|
|
||||||
}
|
|
||||||
return accept
|
|
||||||
}
|
|
||||||
|
|
||||||
// numPeriodsOK checks whether every period in s is followed by a digit.
|
|
||||||
func numPeriodsOK(s string) bool {
|
|
||||||
period := false
|
|
||||||
for _, r := range s {
|
|
||||||
if period && !isDigit(r) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
period = r == '.'
|
|
||||||
}
|
|
||||||
return !period
|
|
||||||
}
|
|
||||||
|
|
||||||
// establishContext sets the current context of the parser,
|
|
||||||
// where the context is either a hash or an array of hashes. Which one is
|
|
||||||
// set depends on the value of the `array` parameter.
|
|
||||||
//
|
|
||||||
// Establishing the context also makes sure that the key isn't a duplicate, and
|
|
||||||
// will create implicit hashes automatically.
|
|
||||||
func (p *parser) establishContext(key Key, array bool) {
|
|
||||||
var ok bool
|
|
||||||
|
|
||||||
// Always start at the top level and drill down for our context.
|
|
||||||
hashContext := p.mapping
|
|
||||||
keyContext := make(Key, 0)
|
|
||||||
|
|
||||||
// We only need implicit hashes for key[0:-1]
|
|
||||||
for _, k := range key[0 : len(key)-1] {
|
|
||||||
_, ok = hashContext[k]
|
|
||||||
keyContext = append(keyContext, k)
|
|
||||||
|
|
||||||
// No key? Make an implicit hash and move on.
|
|
||||||
if !ok {
|
|
||||||
p.addImplicit(keyContext)
|
|
||||||
hashContext[k] = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the hash context is actually an array of tables, then set
|
|
||||||
// the hash context to the last element in that array.
|
|
||||||
//
|
|
||||||
// Otherwise, it better be a table, since this MUST be a key group (by
|
|
||||||
// virtue of it not being the last element in a key).
|
|
||||||
switch t := hashContext[k].(type) {
|
|
||||||
case []map[string]interface{}:
|
|
||||||
hashContext = t[len(t)-1]
|
|
||||||
case map[string]interface{}:
|
|
||||||
hashContext = t
|
|
||||||
default:
|
|
||||||
p.panicf("Key '%s' was already created as a hash.", keyContext)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
p.context = keyContext
|
|
||||||
if array {
|
|
||||||
// If this is the first element for this array, then allocate a new
|
|
||||||
// list of tables for it.
|
|
||||||
k := key[len(key)-1]
|
|
||||||
if _, ok := hashContext[k]; !ok {
|
|
||||||
hashContext[k] = make([]map[string]interface{}, 0, 5)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a new table. But make sure the key hasn't already been used
|
|
||||||
// for something else.
|
|
||||||
if hash, ok := hashContext[k].([]map[string]interface{}); ok {
|
|
||||||
hashContext[k] = append(hash, make(map[string]interface{}))
|
|
||||||
} else {
|
|
||||||
p.panicf("Key '%s' was already created and cannot be used as "+
|
|
||||||
"an array.", keyContext)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
p.setValue(key[len(key)-1], make(map[string]interface{}))
|
|
||||||
}
|
|
||||||
p.context = append(p.context, key[len(key)-1])
|
|
||||||
}
|
|
||||||
|
|
||||||
// setValue sets the given key to the given value in the current context.
|
|
||||||
// It will make sure that the key hasn't already been defined, account for
|
|
||||||
// implicit key groups.
|
|
||||||
func (p *parser) setValue(key string, value interface{}) {
|
|
||||||
var tmpHash interface{}
|
|
||||||
var ok bool
|
|
||||||
|
|
||||||
hash := p.mapping
|
|
||||||
keyContext := make(Key, 0)
|
|
||||||
for _, k := range p.context {
|
|
||||||
keyContext = append(keyContext, k)
|
|
||||||
if tmpHash, ok = hash[k]; !ok {
|
|
||||||
p.bug("Context for key '%s' has not been established.", keyContext)
|
|
||||||
}
|
|
||||||
switch t := tmpHash.(type) {
|
|
||||||
case []map[string]interface{}:
|
|
||||||
// The context is a table of hashes. Pick the most recent table
|
|
||||||
// defined as the current hash.
|
|
||||||
hash = t[len(t)-1]
|
|
||||||
case map[string]interface{}:
|
|
||||||
hash = t
|
|
||||||
default:
|
|
||||||
p.bug("Expected hash to have type 'map[string]interface{}', but "+
|
|
||||||
"it has '%T' instead.", tmpHash)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
keyContext = append(keyContext, key)
|
|
||||||
|
|
||||||
if _, ok := hash[key]; ok {
|
|
||||||
// Typically, if the given key has already been set, then we have
|
|
||||||
// to raise an error since duplicate keys are disallowed. However,
|
|
||||||
// it's possible that a key was previously defined implicitly. In this
|
|
||||||
// case, it is allowed to be redefined concretely. (See the
|
|
||||||
// `tests/valid/implicit-and-explicit-after.toml` test in `toml-test`.)
|
|
||||||
//
|
|
||||||
// But we have to make sure to stop marking it as an implicit. (So that
|
|
||||||
// another redefinition provokes an error.)
|
|
||||||
//
|
|
||||||
// Note that since it has already been defined (as a hash), we don't
|
|
||||||
// want to overwrite it. So our business is done.
|
|
||||||
if p.isImplicit(keyContext) {
|
|
||||||
p.removeImplicit(keyContext)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we have a concrete key trying to override a previous
|
|
||||||
// key, which is *always* wrong.
|
|
||||||
p.panicf("Key '%s' has already been defined.", keyContext)
|
|
||||||
}
|
|
||||||
hash[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// setType sets the type of a particular value at a given key.
|
|
||||||
// It should be called immediately AFTER setValue.
|
|
||||||
//
|
|
||||||
// Note that if `key` is empty, then the type given will be applied to the
|
|
||||||
// current context (which is either a table or an array of tables).
|
|
||||||
func (p *parser) setType(key string, typ tomlType) {
|
|
||||||
keyContext := make(Key, 0, len(p.context)+1)
|
|
||||||
for _, k := range p.context {
|
|
||||||
keyContext = append(keyContext, k)
|
|
||||||
}
|
|
||||||
if len(key) > 0 { // allow type setting for hashes
|
|
||||||
keyContext = append(keyContext, key)
|
|
||||||
}
|
|
||||||
p.types[keyContext.String()] = typ
|
|
||||||
}
|
|
||||||
|
|
||||||
// addImplicit sets the given Key as having been created implicitly.
|
|
||||||
func (p *parser) addImplicit(key Key) {
|
|
||||||
p.implicits[key.String()] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeImplicit stops tagging the given key as having been implicitly
|
|
||||||
// created.
|
|
||||||
func (p *parser) removeImplicit(key Key) {
|
|
||||||
p.implicits[key.String()] = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// isImplicit returns true if the key group pointed to by the key was created
|
|
||||||
// implicitly.
|
|
||||||
func (p *parser) isImplicit(key Key) bool {
|
|
||||||
return p.implicits[key.String()]
|
|
||||||
}
|
|
||||||
|
|
||||||
// current returns the full key name of the current context.
|
|
||||||
func (p *parser) current() string {
|
|
||||||
if len(p.currentKey) == 0 {
|
|
||||||
return p.context.String()
|
|
||||||
}
|
|
||||||
if len(p.context) == 0 {
|
|
||||||
return p.currentKey
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s.%s", p.context, p.currentKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripFirstNewline(s string) string {
|
|
||||||
if len(s) == 0 || s[0] != '\n' {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return s[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripEscapedWhitespace(s string) string {
|
|
||||||
esc := strings.Split(s, "\\\n")
|
|
||||||
if len(esc) > 1 {
|
|
||||||
for i := 1; i < len(esc); i++ {
|
|
||||||
esc[i] = strings.TrimLeftFunc(esc[i], unicode.IsSpace)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strings.Join(esc, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) replaceEscapes(str string) string {
|
|
||||||
var replaced []rune
|
|
||||||
s := []byte(str)
|
|
||||||
r := 0
|
|
||||||
for r < len(s) {
|
|
||||||
if s[r] != '\\' {
|
|
||||||
c, size := utf8.DecodeRune(s[r:])
|
|
||||||
r += size
|
|
||||||
replaced = append(replaced, c)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
r += 1
|
|
||||||
if r >= len(s) {
|
|
||||||
p.bug("Escape sequence at end of string.")
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
switch s[r] {
|
|
||||||
default:
|
|
||||||
p.bug("Expected valid escape code after \\, but got %q.", s[r])
|
|
||||||
return ""
|
|
||||||
case 'b':
|
|
||||||
replaced = append(replaced, rune(0x0008))
|
|
||||||
r += 1
|
|
||||||
case 't':
|
|
||||||
replaced = append(replaced, rune(0x0009))
|
|
||||||
r += 1
|
|
||||||
case 'n':
|
|
||||||
replaced = append(replaced, rune(0x000A))
|
|
||||||
r += 1
|
|
||||||
case 'f':
|
|
||||||
replaced = append(replaced, rune(0x000C))
|
|
||||||
r += 1
|
|
||||||
case 'r':
|
|
||||||
replaced = append(replaced, rune(0x000D))
|
|
||||||
r += 1
|
|
||||||
case '"':
|
|
||||||
replaced = append(replaced, rune(0x0022))
|
|
||||||
r += 1
|
|
||||||
case '\\':
|
|
||||||
replaced = append(replaced, rune(0x005C))
|
|
||||||
r += 1
|
|
||||||
case 'u':
|
|
||||||
// At this point, we know we have a Unicode escape of the form
|
|
||||||
// `uXXXX` at [r, r+5). (Because the lexer guarantees this
|
|
||||||
// for us.)
|
|
||||||
escaped := p.asciiEscapeToUnicode(s[r+1 : r+5])
|
|
||||||
replaced = append(replaced, escaped)
|
|
||||||
r += 5
|
|
||||||
case 'U':
|
|
||||||
// At this point, we know we have a Unicode escape of the form
|
|
||||||
// `uXXXX` at [r, r+9). (Because the lexer guarantees this
|
|
||||||
// for us.)
|
|
||||||
escaped := p.asciiEscapeToUnicode(s[r+1 : r+9])
|
|
||||||
replaced = append(replaced, escaped)
|
|
||||||
r += 9
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return string(replaced)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) asciiEscapeToUnicode(bs []byte) rune {
|
|
||||||
s := string(bs)
|
|
||||||
hex, err := strconv.ParseUint(strings.ToLower(s), 16, 32)
|
|
||||||
if err != nil {
|
|
||||||
p.bug("Could not parse '%s' as a hexadecimal number, but the "+
|
|
||||||
"lexer claims it's OK: %s", s, err)
|
|
||||||
}
|
|
||||||
if !utf8.ValidRune(rune(hex)) {
|
|
||||||
p.panicf("Escaped character '\\u%s' is not valid UTF-8.", s)
|
|
||||||
}
|
|
||||||
return rune(hex)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isStringType(ty itemType) bool {
|
|
||||||
return ty == itemString || ty == itemMultilineString ||
|
|
||||||
ty == itemRawString || ty == itemRawMultilineString
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
au BufWritePost *.go silent!make tags > /dev/null 2>&1
|
|
@ -1,91 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
// tomlType represents any Go type that corresponds to a TOML type.
|
|
||||||
// While the first draft of the TOML spec has a simplistic type system that
|
|
||||||
// probably doesn't need this level of sophistication, we seem to be militating
|
|
||||||
// toward adding real composite types.
|
|
||||||
type tomlType interface {
|
|
||||||
typeString() string
|
|
||||||
}
|
|
||||||
|
|
||||||
// typeEqual accepts any two types and returns true if they are equal.
|
|
||||||
func typeEqual(t1, t2 tomlType) bool {
|
|
||||||
if t1 == nil || t2 == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return t1.typeString() == t2.typeString()
|
|
||||||
}
|
|
||||||
|
|
||||||
func typeIsHash(t tomlType) bool {
|
|
||||||
return typeEqual(t, tomlHash) || typeEqual(t, tomlArrayHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
type tomlBaseType string
|
|
||||||
|
|
||||||
func (btype tomlBaseType) typeString() string {
|
|
||||||
return string(btype)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (btype tomlBaseType) String() string {
|
|
||||||
return btype.typeString()
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
tomlInteger tomlBaseType = "Integer"
|
|
||||||
tomlFloat tomlBaseType = "Float"
|
|
||||||
tomlDatetime tomlBaseType = "Datetime"
|
|
||||||
tomlString tomlBaseType = "String"
|
|
||||||
tomlBool tomlBaseType = "Bool"
|
|
||||||
tomlArray tomlBaseType = "Array"
|
|
||||||
tomlHash tomlBaseType = "Hash"
|
|
||||||
tomlArrayHash tomlBaseType = "ArrayHash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// typeOfPrimitive returns a tomlType of any primitive value in TOML.
|
|
||||||
// Primitive values are: Integer, Float, Datetime, String and Bool.
|
|
||||||
//
|
|
||||||
// Passing a lexer item other than the following will cause a BUG message
|
|
||||||
// to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime.
|
|
||||||
func (p *parser) typeOfPrimitive(lexItem item) tomlType {
|
|
||||||
switch lexItem.typ {
|
|
||||||
case itemInteger:
|
|
||||||
return tomlInteger
|
|
||||||
case itemFloat:
|
|
||||||
return tomlFloat
|
|
||||||
case itemDatetime:
|
|
||||||
return tomlDatetime
|
|
||||||
case itemString:
|
|
||||||
return tomlString
|
|
||||||
case itemMultilineString:
|
|
||||||
return tomlString
|
|
||||||
case itemRawString:
|
|
||||||
return tomlString
|
|
||||||
case itemRawMultilineString:
|
|
||||||
return tomlString
|
|
||||||
case itemBool:
|
|
||||||
return tomlBool
|
|
||||||
}
|
|
||||||
p.bug("Cannot infer primitive type of lex item '%s'.", lexItem)
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
|
|
||||||
// typeOfArray returns a tomlType for an array given a list of types of its
|
|
||||||
// values.
|
|
||||||
//
|
|
||||||
// In the current spec, if an array is homogeneous, then its type is always
|
|
||||||
// "Array". If the array is not homogeneous, an error is generated.
|
|
||||||
func (p *parser) typeOfArray(types []tomlType) tomlType {
|
|
||||||
// Empty arrays are cool.
|
|
||||||
if len(types) == 0 {
|
|
||||||
return tomlArray
|
|
||||||
}
|
|
||||||
|
|
||||||
theType := types[0]
|
|
||||||
for _, t := range types[1:] {
|
|
||||||
if !typeEqual(theType, t) {
|
|
||||||
p.panicf("Array contains values of type '%s' and '%s', but "+
|
|
||||||
"arrays must be homogeneous.", theType, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tomlArray
|
|
||||||
}
|
|
@ -1,242 +0,0 @@
|
|||||||
package toml
|
|
||||||
|
|
||||||
// Struct field handling is adapted from code in encoding/json:
|
|
||||||
//
|
|
||||||
// Copyright 2010 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the Go distribution.
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A field represents a single field found in a struct.
|
|
||||||
type field struct {
|
|
||||||
name string // the name of the field (`toml` tag included)
|
|
||||||
tag bool // whether field has a `toml` tag
|
|
||||||
index []int // represents the depth of an anonymous field
|
|
||||||
typ reflect.Type // the type of the field
|
|
||||||
}
|
|
||||||
|
|
||||||
// byName sorts field by name, breaking ties with depth,
|
|
||||||
// then breaking ties with "name came from toml tag", then
|
|
||||||
// breaking ties with index sequence.
|
|
||||||
type byName []field
|
|
||||||
|
|
||||||
func (x byName) Len() int { return len(x) }
|
|
||||||
|
|
||||||
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
||||||
|
|
||||||
func (x byName) Less(i, j int) bool {
|
|
||||||
if x[i].name != x[j].name {
|
|
||||||
return x[i].name < x[j].name
|
|
||||||
}
|
|
||||||
if len(x[i].index) != len(x[j].index) {
|
|
||||||
return len(x[i].index) < len(x[j].index)
|
|
||||||
}
|
|
||||||
if x[i].tag != x[j].tag {
|
|
||||||
return x[i].tag
|
|
||||||
}
|
|
||||||
return byIndex(x).Less(i, j)
|
|
||||||
}
|
|
||||||
|
|
||||||
// byIndex sorts field by index sequence.
|
|
||||||
type byIndex []field
|
|
||||||
|
|
||||||
func (x byIndex) Len() int { return len(x) }
|
|
||||||
|
|
||||||
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
||||||
|
|
||||||
func (x byIndex) Less(i, j int) bool {
|
|
||||||
for k, xik := range x[i].index {
|
|
||||||
if k >= len(x[j].index) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if xik != x[j].index[k] {
|
|
||||||
return xik < x[j].index[k]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return len(x[i].index) < len(x[j].index)
|
|
||||||
}
|
|
||||||
|
|
||||||
// typeFields returns a list of fields that TOML should recognize for the given
|
|
||||||
// type. The algorithm is breadth-first search over the set of structs to
|
|
||||||
// include - the top struct and then any reachable anonymous structs.
|
|
||||||
func typeFields(t reflect.Type) []field {
|
|
||||||
// Anonymous fields to explore at the current level and the next.
|
|
||||||
current := []field{}
|
|
||||||
next := []field{{typ: t}}
|
|
||||||
|
|
||||||
// Count of queued names for current level and the next.
|
|
||||||
count := map[reflect.Type]int{}
|
|
||||||
nextCount := map[reflect.Type]int{}
|
|
||||||
|
|
||||||
// Types already visited at an earlier level.
|
|
||||||
visited := map[reflect.Type]bool{}
|
|
||||||
|
|
||||||
// Fields found.
|
|
||||||
var fields []field
|
|
||||||
|
|
||||||
for len(next) > 0 {
|
|
||||||
current, next = next, current[:0]
|
|
||||||
count, nextCount = nextCount, map[reflect.Type]int{}
|
|
||||||
|
|
||||||
for _, f := range current {
|
|
||||||
if visited[f.typ] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
visited[f.typ] = true
|
|
||||||
|
|
||||||
// Scan f.typ for fields to include.
|
|
||||||
for i := 0; i < f.typ.NumField(); i++ {
|
|
||||||
sf := f.typ.Field(i)
|
|
||||||
if sf.PkgPath != "" && !sf.Anonymous { // unexported
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
opts := getOptions(sf.Tag)
|
|
||||||
if opts.skip {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
index := make([]int, len(f.index)+1)
|
|
||||||
copy(index, f.index)
|
|
||||||
index[len(f.index)] = i
|
|
||||||
|
|
||||||
ft := sf.Type
|
|
||||||
if ft.Name() == "" && ft.Kind() == reflect.Ptr {
|
|
||||||
// Follow pointer.
|
|
||||||
ft = ft.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record found field and index sequence.
|
|
||||||
if opts.name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
|
|
||||||
tagged := opts.name != ""
|
|
||||||
name := opts.name
|
|
||||||
if name == "" {
|
|
||||||
name = sf.Name
|
|
||||||
}
|
|
||||||
fields = append(fields, field{name, tagged, index, ft})
|
|
||||||
if count[f.typ] > 1 {
|
|
||||||
// If there were multiple instances, add a second,
|
|
||||||
// so that the annihilation code will see a duplicate.
|
|
||||||
// It only cares about the distinction between 1 or 2,
|
|
||||||
// so don't bother generating any more copies.
|
|
||||||
fields = append(fields, fields[len(fields)-1])
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record new anonymous struct to explore in next round.
|
|
||||||
nextCount[ft]++
|
|
||||||
if nextCount[ft] == 1 {
|
|
||||||
f := field{name: ft.Name(), index: index, typ: ft}
|
|
||||||
next = append(next, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Sort(byName(fields))
|
|
||||||
|
|
||||||
// Delete all fields that are hidden by the Go rules for embedded fields,
|
|
||||||
// except that fields with TOML tags are promoted.
|
|
||||||
|
|
||||||
// The fields are sorted in primary order of name, secondary order
|
|
||||||
// of field index length. Loop over names; for each name, delete
|
|
||||||
// hidden fields by choosing the one dominant field that survives.
|
|
||||||
out := fields[:0]
|
|
||||||
for advance, i := 0, 0; i < len(fields); i += advance {
|
|
||||||
// One iteration per name.
|
|
||||||
// Find the sequence of fields with the name of this first field.
|
|
||||||
fi := fields[i]
|
|
||||||
name := fi.name
|
|
||||||
for advance = 1; i+advance < len(fields); advance++ {
|
|
||||||
fj := fields[i+advance]
|
|
||||||
if fj.name != name {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if advance == 1 { // Only one field with this name
|
|
||||||
out = append(out, fi)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
dominant, ok := dominantField(fields[i : i+advance])
|
|
||||||
if ok {
|
|
||||||
out = append(out, dominant)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fields = out
|
|
||||||
sort.Sort(byIndex(fields))
|
|
||||||
|
|
||||||
return fields
|
|
||||||
}
|
|
||||||
|
|
||||||
// dominantField looks through the fields, all of which are known to
|
|
||||||
// have the same name, to find the single field that dominates the
|
|
||||||
// others using Go's embedding rules, modified by the presence of
|
|
||||||
// TOML tags. If there are multiple top-level fields, the boolean
|
|
||||||
// will be false: This condition is an error in Go and we skip all
|
|
||||||
// the fields.
|
|
||||||
func dominantField(fields []field) (field, bool) {
|
|
||||||
// The fields are sorted in increasing index-length order. The winner
|
|
||||||
// must therefore be one with the shortest index length. Drop all
|
|
||||||
// longer entries, which is easy: just truncate the slice.
|
|
||||||
length := len(fields[0].index)
|
|
||||||
tagged := -1 // Index of first tagged field.
|
|
||||||
for i, f := range fields {
|
|
||||||
if len(f.index) > length {
|
|
||||||
fields = fields[:i]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if f.tag {
|
|
||||||
if tagged >= 0 {
|
|
||||||
// Multiple tagged fields at the same level: conflict.
|
|
||||||
// Return no field.
|
|
||||||
return field{}, false
|
|
||||||
}
|
|
||||||
tagged = i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tagged >= 0 {
|
|
||||||
return fields[tagged], true
|
|
||||||
}
|
|
||||||
// All remaining fields have the same length. If there's more than one,
|
|
||||||
// we have a conflict (two fields named "X" at the same level) and we
|
|
||||||
// return no field.
|
|
||||||
if len(fields) > 1 {
|
|
||||||
return field{}, false
|
|
||||||
}
|
|
||||||
return fields[0], true
|
|
||||||
}
|
|
||||||
|
|
||||||
var fieldCache struct {
|
|
||||||
sync.RWMutex
|
|
||||||
m map[reflect.Type][]field
|
|
||||||
}
|
|
||||||
|
|
||||||
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
|
|
||||||
func cachedTypeFields(t reflect.Type) []field {
|
|
||||||
fieldCache.RLock()
|
|
||||||
f := fieldCache.m[t]
|
|
||||||
fieldCache.RUnlock()
|
|
||||||
if f != nil {
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute fields without lock.
|
|
||||||
// Might duplicate effort but won't hold other computations back.
|
|
||||||
f = typeFields(t)
|
|
||||||
if f == nil {
|
|
||||||
f = []field{}
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldCache.Lock()
|
|
||||||
if fieldCache.m == nil {
|
|
||||||
fieldCache.m = map[reflect.Type][]field{}
|
|
||||||
}
|
|
||||||
fieldCache.m[t] = f
|
|
||||||
fieldCache.Unlock()
|
|
||||||
return f
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
ISC License
|
|
||||||
|
|
||||||
Copyright (c) 2013-2017 The btcsuite developers
|
|
||||||
Copyright (c) 2015-2016 The Decred developers
|
|
||||||
|
|
||||||
Permission to use, copy, modify, and distribute this software for any
|
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
|
||||||
copyright notice and this permission notice appear in all copies.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@ -1,85 +0,0 @@
|
|||||||
chaincfg
|
|
||||||
========
|
|
||||||
|
|
||||||
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)](https://travis-ci.org/btcsuite/btcd)
|
|
||||||
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
|
||||||
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/chaincfg)
|
|
||||||
|
|
||||||
Package chaincfg defines chain configuration parameters for the three standard
|
|
||||||
Bitcoin networks and provides the ability for callers to define their own custom
|
|
||||||
Bitcoin networks.
|
|
||||||
|
|
||||||
Although this package was primarily written for btcd, it has intentionally been
|
|
||||||
designed so it can be used as a standalone package for any projects needing to
|
|
||||||
use parameters for the standard Bitcoin networks or for projects needing to
|
|
||||||
define their own network.
|
|
||||||
|
|
||||||
## Sample Use
|
|
||||||
|
|
||||||
```Go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcutil"
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
)
|
|
||||||
|
|
||||||
var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network")
|
|
||||||
|
|
||||||
// By default (without -testnet), use mainnet.
|
|
||||||
var chainParams = &chaincfg.MainNetParams
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// Modify active network parameters if operating on testnet.
|
|
||||||
if *testnet {
|
|
||||||
chainParams = &chaincfg.TestNet3Params
|
|
||||||
}
|
|
||||||
|
|
||||||
// later...
|
|
||||||
|
|
||||||
// Create and print new payment address, specific to the active network.
|
|
||||||
pubKeyHash := make([]byte, 20)
|
|
||||||
addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, chainParams)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
fmt.Println(addr)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Installation and Updating
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get -u github.com/btcsuite/btcd/chaincfg
|
|
||||||
```
|
|
||||||
|
|
||||||
## GPG Verification Key
|
|
||||||
|
|
||||||
All official release tags are signed by Conformal so users can ensure the code
|
|
||||||
has not been tampered with and is coming from the btcsuite developers. To
|
|
||||||
verify the signature perform the following:
|
|
||||||
|
|
||||||
- Download the public key from the Conformal website at
|
|
||||||
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
|
|
||||||
|
|
||||||
- Import the public key into your GPG keyring:
|
|
||||||
```bash
|
|
||||||
gpg --import GIT-GPG-KEY-conformal.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
- Verify the release tag with the following command where `TAG_NAME` is a
|
|
||||||
placeholder for the specific tag:
|
|
||||||
```bash
|
|
||||||
git tag -v TAG_NAME
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Package chaincfg is licensed under the [copyfree](http://copyfree.org) ISC
|
|
||||||
License.
|
|
@ -1,41 +0,0 @@
|
|||||||
chainhash
|
|
||||||
=========
|
|
||||||
|
|
||||||
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)](https://travis-ci.org/btcsuite/btcd)
|
|
||||||
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
|
||||||
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/chaincfg/chainhash)
|
|
||||||
=======
|
|
||||||
|
|
||||||
chainhash provides a generic hash type and associated functions that allows the
|
|
||||||
specific hash algorithm to be abstracted.
|
|
||||||
|
|
||||||
## Installation and Updating
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get -u github.com/btcsuite/btcd/chaincfg/chainhash
|
|
||||||
```
|
|
||||||
|
|
||||||
## GPG Verification Key
|
|
||||||
|
|
||||||
All official release tags are signed by Conformal so users can ensure the code
|
|
||||||
has not been tampered with and is coming from the btcsuite developers. To
|
|
||||||
verify the signature perform the following:
|
|
||||||
|
|
||||||
- Download the public key from the Conformal website at
|
|
||||||
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
|
|
||||||
|
|
||||||
- Import the public key into your GPG keyring:
|
|
||||||
```bash
|
|
||||||
gpg --import GIT-GPG-KEY-conformal.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
- Verify the release tag with the following command where `TAG_NAME` is a
|
|
||||||
placeholder for the specific tag:
|
|
||||||
```bash
|
|
||||||
git tag -v TAG_NAME
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Package chainhash is licensed under the [copyfree](http://copyfree.org) ISC
|
|
||||||
License.
|
|
@ -1,5 +0,0 @@
|
|||||||
// Package chainhash provides abstracted hash functionality.
|
|
||||||
//
|
|
||||||
// This package provides a generic hash type and associated functions that
|
|
||||||
// allows the specific hash algorithm to be abstracted.
|
|
||||||
package chainhash
|
|
@ -1,128 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Copyright (c) 2015 The Decred developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package chainhash
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// HashSize of array used to store hashes. See Hash.
|
|
||||||
const HashSize = 32
|
|
||||||
|
|
||||||
// MaxHashStringSize is the maximum length of a Hash hash string.
|
|
||||||
const MaxHashStringSize = HashSize * 2
|
|
||||||
|
|
||||||
// ErrHashStrSize describes an error that indicates the caller specified a hash
|
|
||||||
// string that has too many characters.
|
|
||||||
var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)
|
|
||||||
|
|
||||||
// Hash is used in several of the bitcoin messages and common structures. It
|
|
||||||
// typically represents the double sha256 of data.
|
|
||||||
type Hash [HashSize]byte
|
|
||||||
|
|
||||||
// String returns the Hash as the hexadecimal string of the byte-reversed
|
|
||||||
// hash.
|
|
||||||
func (hash Hash) String() string {
|
|
||||||
for i := 0; i < HashSize/2; i++ {
|
|
||||||
hash[i], hash[HashSize-1-i] = hash[HashSize-1-i], hash[i]
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(hash[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
// CloneBytes returns a copy of the bytes which represent the hash as a byte
|
|
||||||
// slice.
|
|
||||||
//
|
|
||||||
// NOTE: It is generally cheaper to just slice the hash directly thereby reusing
|
|
||||||
// the same bytes rather than calling this method.
|
|
||||||
func (hash *Hash) CloneBytes() []byte {
|
|
||||||
newHash := make([]byte, HashSize)
|
|
||||||
copy(newHash, hash[:])
|
|
||||||
|
|
||||||
return newHash
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetBytes sets the bytes which represent the hash. An error is returned if
|
|
||||||
// the number of bytes passed in is not HashSize.
|
|
||||||
func (hash *Hash) SetBytes(newHash []byte) error {
|
|
||||||
nhlen := len(newHash)
|
|
||||||
if nhlen != HashSize {
|
|
||||||
return fmt.Errorf("invalid hash length of %v, want %v", nhlen,
|
|
||||||
HashSize)
|
|
||||||
}
|
|
||||||
copy(hash[:], newHash)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsEqual returns true if target is the same as hash.
|
|
||||||
func (hash *Hash) IsEqual(target *Hash) bool {
|
|
||||||
if hash == nil && target == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if hash == nil || target == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return *hash == *target
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHash returns a new Hash from a byte slice. An error is returned if
|
|
||||||
// the number of bytes passed in is not HashSize.
|
|
||||||
func NewHash(newHash []byte) (*Hash, error) {
|
|
||||||
var sh Hash
|
|
||||||
err := sh.SetBytes(newHash)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &sh, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHashFromStr creates a Hash from a hash string. The string should be
|
|
||||||
// the hexadecimal string of a byte-reversed hash, but any missing characters
|
|
||||||
// result in zero padding at the end of the Hash.
|
|
||||||
func NewHashFromStr(hash string) (*Hash, error) {
|
|
||||||
ret := new(Hash)
|
|
||||||
err := Decode(ret, hash)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a
|
|
||||||
// destination.
|
|
||||||
func Decode(dst *Hash, src string) error {
|
|
||||||
// Return error if hash string is too long.
|
|
||||||
if len(src) > MaxHashStringSize {
|
|
||||||
return ErrHashStrSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hex decoder expects the hash to be a multiple of two. When not, pad
|
|
||||||
// with a leading zero.
|
|
||||||
var srcBytes []byte
|
|
||||||
if len(src)%2 == 0 {
|
|
||||||
srcBytes = []byte(src)
|
|
||||||
} else {
|
|
||||||
srcBytes = make([]byte, 1+len(src))
|
|
||||||
srcBytes[0] = '0'
|
|
||||||
copy(srcBytes[1:], src)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hex decode the source bytes to a temporary destination.
|
|
||||||
var reversedHash Hash
|
|
||||||
_, err := hex.Decode(reversedHash[HashSize-hex.DecodedLen(len(srcBytes)):], srcBytes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reverse copy from the temporary hash to destination. Because the
|
|
||||||
// temporary was zeroed, the written result will be correctly padded.
|
|
||||||
for i, b := range reversedHash[:HashSize/2] {
|
|
||||||
dst[i], dst[HashSize-1-i] = reversedHash[HashSize-1-i], b
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
// Copyright (c) 2015 The Decred developers
|
|
||||||
// Copyright (c) 2016-2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package chainhash
|
|
||||||
|
|
||||||
import "crypto/sha256"
|
|
||||||
|
|
||||||
// HashB calculates hash(b) and returns the resulting bytes.
|
|
||||||
func HashB(b []byte) []byte {
|
|
||||||
hash := sha256.Sum256(b)
|
|
||||||
return hash[:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// HashH calculates hash(b) and returns the resulting bytes as a Hash.
|
|
||||||
func HashH(b []byte) Hash {
|
|
||||||
return Hash(sha256.Sum256(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
// DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
|
|
||||||
func DoubleHashB(b []byte) []byte {
|
|
||||||
first := sha256.Sum256(b)
|
|
||||||
second := sha256.Sum256(first[:])
|
|
||||||
return second[:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a
|
|
||||||
// Hash.
|
|
||||||
func DoubleHashH(b []byte) Hash {
|
|
||||||
first := sha256.Sum256(b)
|
|
||||||
return Hash(sha256.Sum256(first[:]))
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
// Package chaincfg defines chain configuration parameters.
|
|
||||||
//
|
|
||||||
// In addition to the main Bitcoin network, which is intended for the transfer
|
|
||||||
// of monetary value, there also exists two currently active standard networks:
|
|
||||||
// regression test and testnet (version 3). These networks are incompatible
|
|
||||||
// with each other (each sharing a different genesis block) and software should
|
|
||||||
// handle errors where input intended for one network is used on an application
|
|
||||||
// instance running on a different network.
|
|
||||||
//
|
|
||||||
// For library packages, chaincfg provides the ability to lookup chain
|
|
||||||
// parameters and encoding magics when passed a *Params. Older APIs not updated
|
|
||||||
// to the new convention of passing a *Params may lookup the parameters for a
|
|
||||||
// wire.BitcoinNet using ParamsForNet, but be aware that this usage is
|
|
||||||
// deprecated and will be removed from chaincfg in the future.
|
|
||||||
//
|
|
||||||
// For main packages, a (typically global) var may be assigned the address of
|
|
||||||
// one of the standard Param vars for use as the application's "active" network.
|
|
||||||
// When a network parameter is needed, it may then be looked up through this
|
|
||||||
// variable (either directly, or hidden in a library call).
|
|
||||||
//
|
|
||||||
// package main
|
|
||||||
//
|
|
||||||
// import (
|
|
||||||
// "flag"
|
|
||||||
// "fmt"
|
|
||||||
// "log"
|
|
||||||
//
|
|
||||||
// "github.com/btcsuite/btcutil"
|
|
||||||
// "github.com/btcsuite/btcd/chaincfg"
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
// var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network")
|
|
||||||
//
|
|
||||||
// // By default (without -testnet), use mainnet.
|
|
||||||
// var chainParams = &chaincfg.MainNetParams
|
|
||||||
//
|
|
||||||
// func main() {
|
|
||||||
// flag.Parse()
|
|
||||||
//
|
|
||||||
// // Modify active network parameters if operating on testnet.
|
|
||||||
// if *testnet {
|
|
||||||
// chainParams = &chaincfg.TestNet3Params
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // later...
|
|
||||||
//
|
|
||||||
// // Create and print new payment address, specific to the active network.
|
|
||||||
// pubKeyHash := make([]byte, 20)
|
|
||||||
// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, chainParams)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
// fmt.Println(addr)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// If an application does not use one of the three standard Bitcoin networks,
|
|
||||||
// a new Params struct may be created which defines the parameters for the
|
|
||||||
// non-standard network. As a general rule of thumb, all network parameters
|
|
||||||
// should be unique to the network, but parameter collisions can still occur
|
|
||||||
// (unfortunately, this is the case with regtest and testnet3 sharing magics).
|
|
||||||
package chaincfg
|
|
@ -1,172 +0,0 @@
|
|||||||
// Copyright (c) 2014-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package chaincfg
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
"github.com/btcsuite/btcd/wire"
|
|
||||||
)
|
|
||||||
|
|
||||||
// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
|
|
||||||
// the main network, regression test network, and test network (version 3).
|
|
||||||
var genesisCoinbaseTx = wire.MsgTx{
|
|
||||||
Version: 1,
|
|
||||||
TxIn: []*wire.TxIn{
|
|
||||||
{
|
|
||||||
PreviousOutPoint: wire.OutPoint{
|
|
||||||
Hash: chainhash.Hash{},
|
|
||||||
Index: 0xffffffff,
|
|
||||||
},
|
|
||||||
SignatureScript: []byte{
|
|
||||||
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x45, /* |.......E| */
|
|
||||||
0x54, 0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, /* |The Time| */
|
|
||||||
0x73, 0x20, 0x30, 0x33, 0x2f, 0x4a, 0x61, 0x6e, /* |s 03/Jan| */
|
|
||||||
0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, /* |/2009 Ch| */
|
|
||||||
0x61, 0x6e, 0x63, 0x65, 0x6c, 0x6c, 0x6f, 0x72, /* |ancellor| */
|
|
||||||
0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, /* | on brin| */
|
|
||||||
0x6b, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x65, 0x63, /* |k of sec|*/
|
|
||||||
0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, /* |ond bail| */
|
|
||||||
0x6f, 0x75, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, /* |out for |*/
|
|
||||||
0x62, 0x61, 0x6e, 0x6b, 0x73, /* |banks| */
|
|
||||||
},
|
|
||||||
Sequence: 0xffffffff,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
TxOut: []*wire.TxOut{
|
|
||||||
{
|
|
||||||
Value: 0x12a05f200,
|
|
||||||
PkScript: []byte{
|
|
||||||
0x41, 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, /* |A.g....U| */
|
|
||||||
0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, /* |H'.g..q0| */
|
|
||||||
0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, /* |..\..(.9| */
|
|
||||||
0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, /* |..yb...a| */
|
|
||||||
0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, /* |..I..?L.| */
|
|
||||||
0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, /* |8..U....| */
|
|
||||||
0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, /* |..\8M...| */
|
|
||||||
0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, /* |.W.Lp+k.| */
|
|
||||||
0x1d, 0x5f, 0xac, /* |._.| */
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
LockTime: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
// genesisHash is the hash of the first block in the block chain for the main
|
|
||||||
// network (genesis block).
|
|
||||||
var genesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
|
|
||||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
|
||||||
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
|
||||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
|
||||||
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
})
|
|
||||||
|
|
||||||
// genesisMerkleRoot is the hash of the first transaction in the genesis block
|
|
||||||
// for the main network.
|
|
||||||
var genesisMerkleRoot = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
|
|
||||||
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
|
|
||||||
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
|
||||||
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
|
||||||
0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a,
|
|
||||||
})
|
|
||||||
|
|
||||||
// genesisBlock defines the genesis block of the block chain which serves as the
|
|
||||||
// public transaction ledger for the main network.
|
|
||||||
var genesisBlock = wire.MsgBlock{
|
|
||||||
Header: wire.BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
|
|
||||||
MerkleRoot: genesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
|
|
||||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 18:15:05 +0000 UTC
|
|
||||||
Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000]
|
|
||||||
Nonce: 0x7c2bac1d, // 2083236893
|
|
||||||
},
|
|
||||||
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
|
|
||||||
}
|
|
||||||
|
|
||||||
// regTestGenesisHash is the hash of the first block in the block chain for the
|
|
||||||
// regression test network (genesis block).
|
|
||||||
var regTestGenesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
|
|
||||||
0x06, 0x22, 0x6e, 0x46, 0x11, 0x1a, 0x0b, 0x59,
|
|
||||||
0xca, 0xaf, 0x12, 0x60, 0x43, 0xeb, 0x5b, 0xbf,
|
|
||||||
0x28, 0xc3, 0x4f, 0x3a, 0x5e, 0x33, 0x2a, 0x1f,
|
|
||||||
0xc7, 0xb2, 0xb7, 0x3c, 0xf1, 0x88, 0x91, 0x0f,
|
|
||||||
})
|
|
||||||
|
|
||||||
// regTestGenesisMerkleRoot is the hash of the first transaction in the genesis
|
|
||||||
// block for the regression test network. It is the same as the merkle root for
|
|
||||||
// the main network.
|
|
||||||
var regTestGenesisMerkleRoot = genesisMerkleRoot
|
|
||||||
|
|
||||||
// regTestGenesisBlock defines the genesis block of the block chain which serves
|
|
||||||
// as the public transaction ledger for the regression test network.
|
|
||||||
var regTestGenesisBlock = wire.MsgBlock{
|
|
||||||
Header: wire.BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
|
|
||||||
MerkleRoot: regTestGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
|
|
||||||
Timestamp: time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC
|
|
||||||
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
|
|
||||||
Nonce: 2,
|
|
||||||
},
|
|
||||||
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
|
|
||||||
}
|
|
||||||
|
|
||||||
// testNet3GenesisHash is the hash of the first block in the block chain for the
|
|
||||||
// test network (version 3).
|
|
||||||
var testNet3GenesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
|
|
||||||
0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71,
|
|
||||||
0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae,
|
|
||||||
0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad,
|
|
||||||
0x01, 0xea, 0x33, 0x09, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
})
|
|
||||||
|
|
||||||
// testNet3GenesisMerkleRoot is the hash of the first transaction in the genesis
|
|
||||||
// block for the test network (version 3). It is the same as the merkle root
|
|
||||||
// for the main network.
|
|
||||||
var testNet3GenesisMerkleRoot = genesisMerkleRoot
|
|
||||||
|
|
||||||
// testNet3GenesisBlock defines the genesis block of the block chain which
|
|
||||||
// serves as the public transaction ledger for the test network (version 3).
|
|
||||||
var testNet3GenesisBlock = wire.MsgBlock{
|
|
||||||
Header: wire.BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
|
|
||||||
MerkleRoot: testNet3GenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
|
|
||||||
Timestamp: time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC
|
|
||||||
Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000]
|
|
||||||
Nonce: 0x18aea41a, // 414098458
|
|
||||||
},
|
|
||||||
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
|
|
||||||
}
|
|
||||||
|
|
||||||
// simNetGenesisHash is the hash of the first block in the block chain for the
|
|
||||||
// simulation test network.
|
|
||||||
var simNetGenesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
|
|
||||||
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a,
|
|
||||||
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
|
||||||
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
|
||||||
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
|
||||||
})
|
|
||||||
|
|
||||||
// simNetGenesisMerkleRoot is the hash of the first transaction in the genesis
|
|
||||||
// block for the simulation test network. It is the same as the merkle root for
|
|
||||||
// the main network.
|
|
||||||
var simNetGenesisMerkleRoot = genesisMerkleRoot
|
|
||||||
|
|
||||||
// simNetGenesisBlock defines the genesis block of the block chain which serves
|
|
||||||
// as the public transaction ledger for the simulation test network.
|
|
||||||
var simNetGenesisBlock = wire.MsgBlock{
|
|
||||||
Header: wire.BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
|
|
||||||
MerkleRoot: simNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
|
|
||||||
Timestamp: time.Unix(1401292357, 0), // 2014-05-28 15:52:37 +0000 UTC
|
|
||||||
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
|
|
||||||
Nonce: 2,
|
|
||||||
},
|
|
||||||
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
|
|
||||||
}
|
|
@ -1,702 +0,0 @@
|
|||||||
// Copyright (c) 2014-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package chaincfg
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"math"
|
|
||||||
"math/big"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
"github.com/btcsuite/btcd/wire"
|
|
||||||
)
|
|
||||||
|
|
||||||
// These variables are the chain proof-of-work limit parameters for each default
|
|
||||||
// network.
|
|
||||||
var (
|
|
||||||
// bigOne is 1 represented as a big.Int. It is defined here to avoid
|
|
||||||
// the overhead of creating it multiple times.
|
|
||||||
bigOne = big.NewInt(1)
|
|
||||||
|
|
||||||
// mainPowLimit is the highest proof of work value a Bitcoin block can
|
|
||||||
// have for the main network. It is the value 2^224 - 1.
|
|
||||||
mainPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
|
|
||||||
|
|
||||||
// regressionPowLimit is the highest proof of work value a Bitcoin block
|
|
||||||
// can have for the regression test network. It is the value 2^255 - 1.
|
|
||||||
regressionPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
|
|
||||||
|
|
||||||
// testNet3PowLimit is the highest proof of work value a Bitcoin block
|
|
||||||
// can have for the test network (version 3). It is the value
|
|
||||||
// 2^224 - 1.
|
|
||||||
testNet3PowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
|
|
||||||
|
|
||||||
// simNetPowLimit is the highest proof of work value a Bitcoin block
|
|
||||||
// can have for the simulation test network. It is the value 2^255 - 1.
|
|
||||||
simNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Checkpoint identifies a known good point in the block chain. Using
|
|
||||||
// checkpoints allows a few optimizations for old blocks during initial download
|
|
||||||
// and also prevents forks from old blocks.
|
|
||||||
//
|
|
||||||
// Each checkpoint is selected based upon several factors. See the
|
|
||||||
// documentation for blockchain.IsCheckpointCandidate for details on the
|
|
||||||
// selection criteria.
|
|
||||||
type Checkpoint struct {
|
|
||||||
Height int32
|
|
||||||
Hash *chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// DNSSeed identifies a DNS seed.
|
|
||||||
type DNSSeed struct {
|
|
||||||
// Host defines the hostname of the seed.
|
|
||||||
Host string
|
|
||||||
|
|
||||||
// HasFiltering defines whether the seed supports filtering
|
|
||||||
// by service flags (wire.ServiceFlag).
|
|
||||||
HasFiltering bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConsensusDeployment defines details related to a specific consensus rule
|
|
||||||
// change that is voted in. This is part of BIP0009.
|
|
||||||
type ConsensusDeployment struct {
|
|
||||||
// BitNumber defines the specific bit number within the block version
|
|
||||||
// this particular soft-fork deployment refers to.
|
|
||||||
BitNumber uint8
|
|
||||||
|
|
||||||
// StartTime is the median block time after which voting on the
|
|
||||||
// deployment starts.
|
|
||||||
StartTime uint64
|
|
||||||
|
|
||||||
// ExpireTime is the median block time after which the attempted
|
|
||||||
// deployment expires.
|
|
||||||
ExpireTime uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Constants that define the deployment offset in the deployments field of the
|
|
||||||
// parameters for each deployment. This is useful to be able to get the details
|
|
||||||
// of a specific deployment by name.
|
|
||||||
const (
|
|
||||||
// DeploymentTestDummy defines the rule change deployment ID for testing
|
|
||||||
// purposes.
|
|
||||||
DeploymentTestDummy = iota
|
|
||||||
|
|
||||||
// DeploymentCSV defines the rule change deployment ID for the CSV
|
|
||||||
// soft-fork package. The CSV package includes the deployment of BIPS
|
|
||||||
// 68, 112, and 113.
|
|
||||||
DeploymentCSV
|
|
||||||
|
|
||||||
// DeploymentSegwit defines the rule change deployment ID for the
|
|
||||||
// Segregated Witness (segwit) soft-fork package. The segwit package
|
|
||||||
// includes the deployment of BIPS 141, 142, 144, 145, 147 and 173.
|
|
||||||
DeploymentSegwit
|
|
||||||
|
|
||||||
// NOTE: DefinedDeployments must always come last since it is used to
|
|
||||||
// determine how many defined deployments there currently are.
|
|
||||||
|
|
||||||
// DefinedDeployments is the number of currently defined deployments.
|
|
||||||
DefinedDeployments
|
|
||||||
)
|
|
||||||
|
|
||||||
// Params defines a Bitcoin network by its parameters. These parameters may be
|
|
||||||
// used by Bitcoin applications to differentiate networks as well as addresses
|
|
||||||
// and keys for one network from those intended for use on another network.
|
|
||||||
type Params struct {
|
|
||||||
// Name defines a human-readable identifier for the network.
|
|
||||||
Name string
|
|
||||||
|
|
||||||
// Net defines the magic bytes used to identify the network.
|
|
||||||
Net wire.BitcoinNet
|
|
||||||
|
|
||||||
// DefaultPort defines the default peer-to-peer port for the network.
|
|
||||||
DefaultPort string
|
|
||||||
|
|
||||||
// DNSSeeds defines a list of DNS seeds for the network that are used
|
|
||||||
// as one method to discover peers.
|
|
||||||
DNSSeeds []DNSSeed
|
|
||||||
|
|
||||||
// GenesisBlock defines the first block of the chain.
|
|
||||||
GenesisBlock *wire.MsgBlock
|
|
||||||
|
|
||||||
// GenesisHash is the starting block hash.
|
|
||||||
GenesisHash *chainhash.Hash
|
|
||||||
|
|
||||||
// PowLimit defines the highest allowed proof of work value for a block
|
|
||||||
// as a uint256.
|
|
||||||
PowLimit *big.Int
|
|
||||||
|
|
||||||
// PowLimitBits defines the highest allowed proof of work value for a
|
|
||||||
// block in compact form.
|
|
||||||
PowLimitBits uint32
|
|
||||||
|
|
||||||
// These fields define the block heights at which the specified softfork
|
|
||||||
// BIP became active.
|
|
||||||
BIP0034Height int32
|
|
||||||
BIP0065Height int32
|
|
||||||
BIP0066Height int32
|
|
||||||
|
|
||||||
// CoinbaseMaturity is the number of blocks required before newly mined
|
|
||||||
// coins (coinbase transactions) can be spent.
|
|
||||||
CoinbaseMaturity uint16
|
|
||||||
|
|
||||||
// SubsidyReductionInterval is the interval of blocks before the subsidy
|
|
||||||
// is reduced.
|
|
||||||
SubsidyReductionInterval int32
|
|
||||||
|
|
||||||
// TargetTimespan is the desired amount of time that should elapse
|
|
||||||
// before the block difficulty requirement is examined to determine how
|
|
||||||
// it should be changed in order to maintain the desired block
|
|
||||||
// generation rate.
|
|
||||||
TargetTimespan time.Duration
|
|
||||||
|
|
||||||
// TargetTimePerBlock is the desired amount of time to generate each
|
|
||||||
// block.
|
|
||||||
TargetTimePerBlock time.Duration
|
|
||||||
|
|
||||||
// RetargetAdjustmentFactor is the adjustment factor used to limit
|
|
||||||
// the minimum and maximum amount of adjustment that can occur between
|
|
||||||
// difficulty retargets.
|
|
||||||
RetargetAdjustmentFactor int64
|
|
||||||
|
|
||||||
// ReduceMinDifficulty defines whether the network should reduce the
|
|
||||||
// minimum required difficulty after a long enough period of time has
|
|
||||||
// passed without finding a block. This is really only useful for test
|
|
||||||
// networks and should not be set on a main network.
|
|
||||||
ReduceMinDifficulty bool
|
|
||||||
|
|
||||||
// MinDiffReductionTime is the amount of time after which the minimum
|
|
||||||
// required difficulty should be reduced when a block hasn't been found.
|
|
||||||
//
|
|
||||||
// NOTE: This only applies if ReduceMinDifficulty is true.
|
|
||||||
MinDiffReductionTime time.Duration
|
|
||||||
|
|
||||||
// GenerateSupported specifies whether or not CPU mining is allowed.
|
|
||||||
GenerateSupported bool
|
|
||||||
|
|
||||||
// Checkpoints ordered from oldest to newest.
|
|
||||||
Checkpoints []Checkpoint
|
|
||||||
|
|
||||||
// These fields are related to voting on consensus rule changes as
|
|
||||||
// defined by BIP0009.
|
|
||||||
//
|
|
||||||
// RuleChangeActivationThreshold is the number of blocks in a threshold
|
|
||||||
// state retarget window for which a positive vote for a rule change
|
|
||||||
// must be cast in order to lock in a rule change. It should typically
|
|
||||||
// be 95% for the main network and 75% for test networks.
|
|
||||||
//
|
|
||||||
// MinerConfirmationWindow is the number of blocks in each threshold
|
|
||||||
// state retarget window.
|
|
||||||
//
|
|
||||||
// Deployments define the specific consensus rule changes to be voted
|
|
||||||
// on.
|
|
||||||
RuleChangeActivationThreshold uint32
|
|
||||||
MinerConfirmationWindow uint32
|
|
||||||
Deployments [DefinedDeployments]ConsensusDeployment
|
|
||||||
|
|
||||||
// Mempool parameters
|
|
||||||
RelayNonStdTxs bool
|
|
||||||
|
|
||||||
// Human-readable part for Bech32 encoded segwit addresses, as defined
|
|
||||||
// in BIP 173.
|
|
||||||
Bech32HRPSegwit string
|
|
||||||
|
|
||||||
// Address encoding magics
|
|
||||||
PubKeyHashAddrID byte // First byte of a P2PKH address
|
|
||||||
ScriptHashAddrID byte // First byte of a P2SH address
|
|
||||||
PrivateKeyID byte // First byte of a WIF private key
|
|
||||||
WitnessPubKeyHashAddrID byte // First byte of a P2WPKH address
|
|
||||||
WitnessScriptHashAddrID byte // First byte of a P2WSH address
|
|
||||||
|
|
||||||
// BIP32 hierarchical deterministic extended key magics
|
|
||||||
HDPrivateKeyID [4]byte
|
|
||||||
HDPublicKeyID [4]byte
|
|
||||||
|
|
||||||
// BIP44 coin type used in the hierarchical deterministic path for
|
|
||||||
// address generation.
|
|
||||||
HDCoinType uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// MainNetParams defines the network parameters for the main Bitcoin network.
|
|
||||||
var MainNetParams = Params{
|
|
||||||
Name: "mainnet",
|
|
||||||
Net: wire.MainNet,
|
|
||||||
DefaultPort: "8333",
|
|
||||||
DNSSeeds: []DNSSeed{
|
|
||||||
{"seed.bitcoin.sipa.be", true},
|
|
||||||
{"dnsseed.bluematt.me", true},
|
|
||||||
{"dnsseed.bitcoin.dashjr.org", false},
|
|
||||||
{"seed.bitcoinstats.com", true},
|
|
||||||
{"seed.bitnodes.io", false},
|
|
||||||
{"seed.bitcoin.jonasschnelli.ch", true},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Chain parameters
|
|
||||||
GenesisBlock: &genesisBlock,
|
|
||||||
GenesisHash: &genesisHash,
|
|
||||||
PowLimit: mainPowLimit,
|
|
||||||
PowLimitBits: 0x1d00ffff,
|
|
||||||
BIP0034Height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
|
|
||||||
BIP0065Height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
|
|
||||||
BIP0066Height: 363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
|
|
||||||
CoinbaseMaturity: 100,
|
|
||||||
SubsidyReductionInterval: 210000,
|
|
||||||
TargetTimespan: time.Hour * 24 * 14, // 14 days
|
|
||||||
TargetTimePerBlock: time.Minute * 10, // 10 minutes
|
|
||||||
RetargetAdjustmentFactor: 4, // 25% less, 400% more
|
|
||||||
ReduceMinDifficulty: false,
|
|
||||||
MinDiffReductionTime: 0,
|
|
||||||
GenerateSupported: false,
|
|
||||||
|
|
||||||
// Checkpoints ordered from oldest to newest.
|
|
||||||
Checkpoints: []Checkpoint{
|
|
||||||
{11111, newHashFromStr("0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d")},
|
|
||||||
{33333, newHashFromStr("000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6")},
|
|
||||||
{74000, newHashFromStr("0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20")},
|
|
||||||
{105000, newHashFromStr("00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97")},
|
|
||||||
{134444, newHashFromStr("00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe")},
|
|
||||||
{168000, newHashFromStr("000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763")},
|
|
||||||
{193000, newHashFromStr("000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317")},
|
|
||||||
{210000, newHashFromStr("000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e")},
|
|
||||||
{216116, newHashFromStr("00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e")},
|
|
||||||
{225430, newHashFromStr("00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932")},
|
|
||||||
{250000, newHashFromStr("000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214")},
|
|
||||||
{267300, newHashFromStr("000000000000000a83fbd660e918f218bf37edd92b748ad940483c7c116179ac")},
|
|
||||||
{279000, newHashFromStr("0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40")},
|
|
||||||
{300255, newHashFromStr("0000000000000000162804527c6e9b9f0563a280525f9d08c12041def0a0f3b2")},
|
|
||||||
{319400, newHashFromStr("000000000000000021c6052e9becade189495d1c539aa37c58917305fd15f13b")},
|
|
||||||
{343185, newHashFromStr("0000000000000000072b8bf361d01a6ba7d445dd024203fafc78768ed4368554")},
|
|
||||||
{352940, newHashFromStr("000000000000000010755df42dba556bb72be6a32f3ce0b6941ce4430152c9ff")},
|
|
||||||
{382320, newHashFromStr("00000000000000000a8dc6ed5b133d0eb2fd6af56203e4159789b092defd8ab2")},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Consensus rule change deployments.
|
|
||||||
//
|
|
||||||
// The miner confirmation window is defined as:
|
|
||||||
// target proof of work timespan / target proof of work spacing
|
|
||||||
RuleChangeActivationThreshold: 1916, // 95% of MinerConfirmationWindow
|
|
||||||
MinerConfirmationWindow: 2016, //
|
|
||||||
Deployments: [DefinedDeployments]ConsensusDeployment{
|
|
||||||
DeploymentTestDummy: {
|
|
||||||
BitNumber: 28,
|
|
||||||
StartTime: 1199145601, // January 1, 2008 UTC
|
|
||||||
ExpireTime: 1230767999, // December 31, 2008 UTC
|
|
||||||
},
|
|
||||||
DeploymentCSV: {
|
|
||||||
BitNumber: 0,
|
|
||||||
StartTime: 1462060800, // May 1st, 2016
|
|
||||||
ExpireTime: 1493596800, // May 1st, 2017
|
|
||||||
},
|
|
||||||
DeploymentSegwit: {
|
|
||||||
BitNumber: 1,
|
|
||||||
StartTime: 1479168000, // November 15, 2016 UTC
|
|
||||||
ExpireTime: 1510704000, // November 15, 2017 UTC.
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Mempool parameters
|
|
||||||
RelayNonStdTxs: false,
|
|
||||||
|
|
||||||
// Human-readable part for Bech32 encoded segwit addresses, as defined in
|
|
||||||
// BIP 173.
|
|
||||||
Bech32HRPSegwit: "bc", // always bc for main net
|
|
||||||
|
|
||||||
// Address encoding magics
|
|
||||||
PubKeyHashAddrID: 0x00, // starts with 1
|
|
||||||
ScriptHashAddrID: 0x05, // starts with 3
|
|
||||||
PrivateKeyID: 0x80, // starts with 5 (uncompressed) or K (compressed)
|
|
||||||
WitnessPubKeyHashAddrID: 0x06, // starts with p2
|
|
||||||
WitnessScriptHashAddrID: 0x0A, // starts with 7Xh
|
|
||||||
|
|
||||||
// BIP32 hierarchical deterministic extended key magics
|
|
||||||
HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv
|
|
||||||
HDPublicKeyID: [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub
|
|
||||||
|
|
||||||
// BIP44 coin type used in the hierarchical deterministic path for
|
|
||||||
// address generation.
|
|
||||||
HDCoinType: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegressionNetParams defines the network parameters for the regression test
|
|
||||||
// Bitcoin network. Not to be confused with the test Bitcoin network (version
|
|
||||||
// 3), this network is sometimes simply called "testnet".
|
|
||||||
var RegressionNetParams = Params{
|
|
||||||
Name: "regtest",
|
|
||||||
Net: wire.TestNet,
|
|
||||||
DefaultPort: "18444",
|
|
||||||
DNSSeeds: []DNSSeed{},
|
|
||||||
|
|
||||||
// Chain parameters
|
|
||||||
GenesisBlock: ®TestGenesisBlock,
|
|
||||||
GenesisHash: ®TestGenesisHash,
|
|
||||||
PowLimit: regressionPowLimit,
|
|
||||||
PowLimitBits: 0x207fffff,
|
|
||||||
CoinbaseMaturity: 100,
|
|
||||||
BIP0034Height: 100000000, // Not active - Permit ver 1 blocks
|
|
||||||
BIP0065Height: 1351, // Used by regression tests
|
|
||||||
BIP0066Height: 1251, // Used by regression tests
|
|
||||||
SubsidyReductionInterval: 150,
|
|
||||||
TargetTimespan: time.Hour * 24 * 14, // 14 days
|
|
||||||
TargetTimePerBlock: time.Minute * 10, // 10 minutes
|
|
||||||
RetargetAdjustmentFactor: 4, // 25% less, 400% more
|
|
||||||
ReduceMinDifficulty: true,
|
|
||||||
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
|
|
||||||
GenerateSupported: true,
|
|
||||||
|
|
||||||
// Checkpoints ordered from oldest to newest.
|
|
||||||
Checkpoints: nil,
|
|
||||||
|
|
||||||
// Consensus rule change deployments.
|
|
||||||
//
|
|
||||||
// The miner confirmation window is defined as:
|
|
||||||
// target proof of work timespan / target proof of work spacing
|
|
||||||
RuleChangeActivationThreshold: 108, // 75% of MinerConfirmationWindow
|
|
||||||
MinerConfirmationWindow: 144,
|
|
||||||
Deployments: [DefinedDeployments]ConsensusDeployment{
|
|
||||||
DeploymentTestDummy: {
|
|
||||||
BitNumber: 28,
|
|
||||||
StartTime: 0, // Always available for vote
|
|
||||||
ExpireTime: math.MaxInt64, // Never expires
|
|
||||||
},
|
|
||||||
DeploymentCSV: {
|
|
||||||
BitNumber: 0,
|
|
||||||
StartTime: 0, // Always available for vote
|
|
||||||
ExpireTime: math.MaxInt64, // Never expires
|
|
||||||
},
|
|
||||||
DeploymentSegwit: {
|
|
||||||
BitNumber: 1,
|
|
||||||
StartTime: 0, // Always available for vote
|
|
||||||
ExpireTime: math.MaxInt64, // Never expires.
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Mempool parameters
|
|
||||||
RelayNonStdTxs: true,
|
|
||||||
|
|
||||||
// Human-readable part for Bech32 encoded segwit addresses, as defined in
|
|
||||||
// BIP 173.
|
|
||||||
Bech32HRPSegwit: "bcrt", // always bcrt for reg test net
|
|
||||||
|
|
||||||
// Address encoding magics
|
|
||||||
PubKeyHashAddrID: 0x6f, // starts with m or n
|
|
||||||
ScriptHashAddrID: 0xc4, // starts with 2
|
|
||||||
PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed)
|
|
||||||
|
|
||||||
// BIP32 hierarchical deterministic extended key magics
|
|
||||||
HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv
|
|
||||||
HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub
|
|
||||||
|
|
||||||
// BIP44 coin type used in the hierarchical deterministic path for
|
|
||||||
// address generation.
|
|
||||||
HDCoinType: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestNet3Params defines the network parameters for the test Bitcoin network
|
|
||||||
// (version 3). Not to be confused with the regression test network, this
|
|
||||||
// network is sometimes simply called "testnet".
|
|
||||||
var TestNet3Params = Params{
|
|
||||||
Name: "testnet3",
|
|
||||||
Net: wire.TestNet3,
|
|
||||||
DefaultPort: "18333",
|
|
||||||
DNSSeeds: []DNSSeed{
|
|
||||||
{"testnet-seed.bitcoin.jonasschnelli.ch", true},
|
|
||||||
{"testnet-seed.bitcoin.schildbach.de", false},
|
|
||||||
{"seed.tbtc.petertodd.org", true},
|
|
||||||
{"testnet-seed.bluematt.me", false},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Chain parameters
|
|
||||||
GenesisBlock: &testNet3GenesisBlock,
|
|
||||||
GenesisHash: &testNet3GenesisHash,
|
|
||||||
PowLimit: testNet3PowLimit,
|
|
||||||
PowLimitBits: 0x1d00ffff,
|
|
||||||
BIP0034Height: 21111, // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
|
|
||||||
BIP0065Height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
|
|
||||||
BIP0066Height: 330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
|
|
||||||
CoinbaseMaturity: 100,
|
|
||||||
SubsidyReductionInterval: 210000,
|
|
||||||
TargetTimespan: time.Hour * 24 * 14, // 14 days
|
|
||||||
TargetTimePerBlock: time.Minute * 10, // 10 minutes
|
|
||||||
RetargetAdjustmentFactor: 4, // 25% less, 400% more
|
|
||||||
ReduceMinDifficulty: true,
|
|
||||||
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
|
|
||||||
GenerateSupported: false,
|
|
||||||
|
|
||||||
// Checkpoints ordered from oldest to newest.
|
|
||||||
Checkpoints: []Checkpoint{
|
|
||||||
{546, newHashFromStr("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70")},
|
|
||||||
{100000, newHashFromStr("00000000009e2958c15ff9290d571bf9459e93b19765c6801ddeccadbb160a1e")},
|
|
||||||
{200000, newHashFromStr("0000000000287bffd321963ef05feab753ebe274e1d78b2fd4e2bfe9ad3aa6f2")},
|
|
||||||
{300001, newHashFromStr("0000000000004829474748f3d1bc8fcf893c88be255e6d7f571c548aff57abf4")},
|
|
||||||
{400002, newHashFromStr("0000000005e2c73b8ecb82ae2dbc2e8274614ebad7172b53528aba7501f5a089")},
|
|
||||||
{500011, newHashFromStr("00000000000929f63977fbac92ff570a9bd9e7715401ee96f2848f7b07750b02")},
|
|
||||||
{600002, newHashFromStr("000000000001f471389afd6ee94dcace5ccc44adc18e8bff402443f034b07240")},
|
|
||||||
{700000, newHashFromStr("000000000000406178b12a4dea3b27e13b3c4fe4510994fd667d7c1e6a3f4dc1")},
|
|
||||||
{800010, newHashFromStr("000000000017ed35296433190b6829db01e657d80631d43f5983fa403bfdb4c1")},
|
|
||||||
{900000, newHashFromStr("0000000000356f8d8924556e765b7a94aaebc6b5c8685dcfa2b1ee8b41acd89b")},
|
|
||||||
{1000007, newHashFromStr("00000000001ccb893d8a1f25b70ad173ce955e5f50124261bbbc50379a612ddf")},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Consensus rule change deployments.
|
|
||||||
//
|
|
||||||
// The miner confirmation window is defined as:
|
|
||||||
// target proof of work timespan / target proof of work spacing
|
|
||||||
RuleChangeActivationThreshold: 1512, // 75% of MinerConfirmationWindow
|
|
||||||
MinerConfirmationWindow: 2016,
|
|
||||||
Deployments: [DefinedDeployments]ConsensusDeployment{
|
|
||||||
DeploymentTestDummy: {
|
|
||||||
BitNumber: 28,
|
|
||||||
StartTime: 1199145601, // January 1, 2008 UTC
|
|
||||||
ExpireTime: 1230767999, // December 31, 2008 UTC
|
|
||||||
},
|
|
||||||
DeploymentCSV: {
|
|
||||||
BitNumber: 0,
|
|
||||||
StartTime: 1456790400, // March 1st, 2016
|
|
||||||
ExpireTime: 1493596800, // May 1st, 2017
|
|
||||||
},
|
|
||||||
DeploymentSegwit: {
|
|
||||||
BitNumber: 1,
|
|
||||||
StartTime: 1462060800, // May 1, 2016 UTC
|
|
||||||
ExpireTime: 1493596800, // May 1, 2017 UTC.
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Mempool parameters
|
|
||||||
RelayNonStdTxs: true,
|
|
||||||
|
|
||||||
// Human-readable part for Bech32 encoded segwit addresses, as defined in
|
|
||||||
// BIP 173.
|
|
||||||
Bech32HRPSegwit: "tb", // always tb for test net
|
|
||||||
|
|
||||||
// Address encoding magics
|
|
||||||
PubKeyHashAddrID: 0x6f, // starts with m or n
|
|
||||||
ScriptHashAddrID: 0xc4, // starts with 2
|
|
||||||
WitnessPubKeyHashAddrID: 0x03, // starts with QW
|
|
||||||
WitnessScriptHashAddrID: 0x28, // starts with T7n
|
|
||||||
PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed)
|
|
||||||
|
|
||||||
// BIP32 hierarchical deterministic extended key magics
|
|
||||||
HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv
|
|
||||||
HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub
|
|
||||||
|
|
||||||
// BIP44 coin type used in the hierarchical deterministic path for
|
|
||||||
// address generation.
|
|
||||||
HDCoinType: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
// SimNetParams defines the network parameters for the simulation test Bitcoin
|
|
||||||
// network. This network is similar to the normal test network except it is
|
|
||||||
// intended for private use within a group of individuals doing simulation
|
|
||||||
// testing. The functionality is intended to differ in that the only nodes
|
|
||||||
// which are specifically specified are used to create the network rather than
|
|
||||||
// following normal discovery rules. This is important as otherwise it would
|
|
||||||
// just turn into another public testnet.
|
|
||||||
var SimNetParams = Params{
|
|
||||||
Name: "simnet",
|
|
||||||
Net: wire.SimNet,
|
|
||||||
DefaultPort: "18555",
|
|
||||||
DNSSeeds: []DNSSeed{}, // NOTE: There must NOT be any seeds.
|
|
||||||
|
|
||||||
// Chain parameters
|
|
||||||
GenesisBlock: &simNetGenesisBlock,
|
|
||||||
GenesisHash: &simNetGenesisHash,
|
|
||||||
PowLimit: simNetPowLimit,
|
|
||||||
PowLimitBits: 0x207fffff,
|
|
||||||
BIP0034Height: 0, // Always active on simnet
|
|
||||||
BIP0065Height: 0, // Always active on simnet
|
|
||||||
BIP0066Height: 0, // Always active on simnet
|
|
||||||
CoinbaseMaturity: 100,
|
|
||||||
SubsidyReductionInterval: 210000,
|
|
||||||
TargetTimespan: time.Hour * 24 * 14, // 14 days
|
|
||||||
TargetTimePerBlock: time.Minute * 10, // 10 minutes
|
|
||||||
RetargetAdjustmentFactor: 4, // 25% less, 400% more
|
|
||||||
ReduceMinDifficulty: true,
|
|
||||||
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
|
|
||||||
GenerateSupported: true,
|
|
||||||
|
|
||||||
// Checkpoints ordered from oldest to newest.
|
|
||||||
Checkpoints: nil,
|
|
||||||
|
|
||||||
// Consensus rule change deployments.
|
|
||||||
//
|
|
||||||
// The miner confirmation window is defined as:
|
|
||||||
// target proof of work timespan / target proof of work spacing
|
|
||||||
RuleChangeActivationThreshold: 75, // 75% of MinerConfirmationWindow
|
|
||||||
MinerConfirmationWindow: 100,
|
|
||||||
Deployments: [DefinedDeployments]ConsensusDeployment{
|
|
||||||
DeploymentTestDummy: {
|
|
||||||
BitNumber: 28,
|
|
||||||
StartTime: 0, // Always available for vote
|
|
||||||
ExpireTime: math.MaxInt64, // Never expires
|
|
||||||
},
|
|
||||||
DeploymentCSV: {
|
|
||||||
BitNumber: 0,
|
|
||||||
StartTime: 0, // Always available for vote
|
|
||||||
ExpireTime: math.MaxInt64, // Never expires
|
|
||||||
},
|
|
||||||
DeploymentSegwit: {
|
|
||||||
BitNumber: 1,
|
|
||||||
StartTime: 0, // Always available for vote
|
|
||||||
ExpireTime: math.MaxInt64, // Never expires.
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Mempool parameters
|
|
||||||
RelayNonStdTxs: true,
|
|
||||||
|
|
||||||
// Human-readable part for Bech32 encoded segwit addresses, as defined in
|
|
||||||
// BIP 173.
|
|
||||||
Bech32HRPSegwit: "sb", // always sb for sim net
|
|
||||||
|
|
||||||
// Address encoding magics
|
|
||||||
PubKeyHashAddrID: 0x3f, // starts with S
|
|
||||||
ScriptHashAddrID: 0x7b, // starts with s
|
|
||||||
PrivateKeyID: 0x64, // starts with 4 (uncompressed) or F (compressed)
|
|
||||||
WitnessPubKeyHashAddrID: 0x19, // starts with Gg
|
|
||||||
WitnessScriptHashAddrID: 0x28, // starts with ?
|
|
||||||
|
|
||||||
// BIP32 hierarchical deterministic extended key magics
|
|
||||||
HDPrivateKeyID: [4]byte{0x04, 0x20, 0xb9, 0x00}, // starts with sprv
|
|
||||||
HDPublicKeyID: [4]byte{0x04, 0x20, 0xbd, 0x3a}, // starts with spub
|
|
||||||
|
|
||||||
// BIP44 coin type used in the hierarchical deterministic path for
|
|
||||||
// address generation.
|
|
||||||
HDCoinType: 115, // ASCII for s
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrDuplicateNet describes an error where the parameters for a Bitcoin
|
|
||||||
// network could not be set due to the network already being a standard
|
|
||||||
// network or previously-registered into this package.
|
|
||||||
ErrDuplicateNet = errors.New("duplicate Bitcoin network")
|
|
||||||
|
|
||||||
// ErrUnknownHDKeyID describes an error where the provided id which
|
|
||||||
// is intended to identify the network for a hierarchical deterministic
|
|
||||||
// private extended key is not registered.
|
|
||||||
ErrUnknownHDKeyID = errors.New("unknown hd private extended key bytes")
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
registeredNets = make(map[wire.BitcoinNet]struct{})
|
|
||||||
pubKeyHashAddrIDs = make(map[byte]struct{})
|
|
||||||
scriptHashAddrIDs = make(map[byte]struct{})
|
|
||||||
bech32SegwitPrefixes = make(map[string]struct{})
|
|
||||||
hdPrivToPubKeyIDs = make(map[[4]byte][]byte)
|
|
||||||
)
|
|
||||||
|
|
||||||
// String returns the hostname of the DNS seed in human-readable form.
|
|
||||||
func (d DNSSeed) String() string {
|
|
||||||
return d.Host
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register registers the network parameters for a Bitcoin network. This may
|
|
||||||
// error with ErrDuplicateNet if the network is already registered (either
|
|
||||||
// due to a previous Register call, or the network being one of the default
|
|
||||||
// networks).
|
|
||||||
//
|
|
||||||
// Network parameters should be registered into this package by a main package
|
|
||||||
// as early as possible. Then, library packages may lookup networks or network
|
|
||||||
// parameters based on inputs and work regardless of the network being standard
|
|
||||||
// or not.
|
|
||||||
func Register(params *Params) error {
|
|
||||||
if _, ok := registeredNets[params.Net]; ok {
|
|
||||||
return ErrDuplicateNet
|
|
||||||
}
|
|
||||||
registeredNets[params.Net] = struct{}{}
|
|
||||||
pubKeyHashAddrIDs[params.PubKeyHashAddrID] = struct{}{}
|
|
||||||
scriptHashAddrIDs[params.ScriptHashAddrID] = struct{}{}
|
|
||||||
hdPrivToPubKeyIDs[params.HDPrivateKeyID] = params.HDPublicKeyID[:]
|
|
||||||
|
|
||||||
// A valid Bech32 encoded segwit address always has as prefix the
|
|
||||||
// human-readable part for the given net followed by '1'.
|
|
||||||
bech32SegwitPrefixes[params.Bech32HRPSegwit+"1"] = struct{}{}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// mustRegister performs the same function as Register except it panics if there
|
|
||||||
// is an error. This should only be called from package init functions.
|
|
||||||
func mustRegister(params *Params) {
|
|
||||||
if err := Register(params); err != nil {
|
|
||||||
panic("failed to register network: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsPubKeyHashAddrID returns whether the id is an identifier known to prefix a
|
|
||||||
// pay-to-pubkey-hash address on any default or registered network. This is
|
|
||||||
// used when decoding an address string into a specific address type. It is up
|
|
||||||
// to the caller to check both this and IsScriptHashAddrID and decide whether an
|
|
||||||
// address is a pubkey hash address, script hash address, neither, or
|
|
||||||
// undeterminable (if both return true).
|
|
||||||
func IsPubKeyHashAddrID(id byte) bool {
|
|
||||||
_, ok := pubKeyHashAddrIDs[id]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsScriptHashAddrID returns whether the id is an identifier known to prefix a
|
|
||||||
// pay-to-script-hash address on any default or registered network. This is
|
|
||||||
// used when decoding an address string into a specific address type. It is up
|
|
||||||
// to the caller to check both this and IsPubKeyHashAddrID and decide whether an
|
|
||||||
// address is a pubkey hash address, script hash address, neither, or
|
|
||||||
// undeterminable (if both return true).
|
|
||||||
func IsScriptHashAddrID(id byte) bool {
|
|
||||||
_, ok := scriptHashAddrIDs[id]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsBech32SegwitPrefix returns whether the prefix is a known prefix for segwit
|
|
||||||
// addresses on any default or registered network. This is used when decoding
|
|
||||||
// an address string into a specific address type.
|
|
||||||
func IsBech32SegwitPrefix(prefix string) bool {
|
|
||||||
prefix = strings.ToLower(prefix)
|
|
||||||
_, ok := bech32SegwitPrefixes[prefix]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// HDPrivateKeyToPublicKeyID accepts a private hierarchical deterministic
|
|
||||||
// extended key id and returns the associated public key id. When the provided
|
|
||||||
// id is not registered, the ErrUnknownHDKeyID error will be returned.
|
|
||||||
func HDPrivateKeyToPublicKeyID(id []byte) ([]byte, error) {
|
|
||||||
if len(id) != 4 {
|
|
||||||
return nil, ErrUnknownHDKeyID
|
|
||||||
}
|
|
||||||
|
|
||||||
var key [4]byte
|
|
||||||
copy(key[:], id)
|
|
||||||
pubBytes, ok := hdPrivToPubKeyIDs[key]
|
|
||||||
if !ok {
|
|
||||||
return nil, ErrUnknownHDKeyID
|
|
||||||
}
|
|
||||||
|
|
||||||
return pubBytes, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newHashFromStr converts the passed big-endian hex string into a
|
|
||||||
// chainhash.Hash. It only differs from the one available in chainhash in that
|
|
||||||
// it panics on an error since it will only (and must only) be called with
|
|
||||||
// hard-coded, and therefore known good, hashes.
|
|
||||||
func newHashFromStr(hexStr string) *chainhash.Hash {
|
|
||||||
hash, err := chainhash.NewHashFromStr(hexStr)
|
|
||||||
if err != nil {
|
|
||||||
// Ordinarily I don't like panics in library code since it
|
|
||||||
// can take applications down without them having a chance to
|
|
||||||
// recover which is extremely annoying, however an exception is
|
|
||||||
// being made in this case because the only way this can panic
|
|
||||||
// is if there is an error in the hard-coded hashes. Thus it
|
|
||||||
// will only ever potentially panic on init and therefore is
|
|
||||||
// 100% predictable.
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return hash
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// Register all default networks when the package is initialized.
|
|
||||||
mustRegister(&MainNetParams)
|
|
||||||
mustRegister(&TestNet3Params)
|
|
||||||
mustRegister(&RegressionNetParams)
|
|
||||||
mustRegister(&SimNetParams)
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
connmgr
|
|
||||||
=======
|
|
||||||
|
|
||||||
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)](https://travis-ci.org/btcsuite/btcd)
|
|
||||||
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
|
||||||
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/connmgr)
|
|
||||||
|
|
||||||
Package connmgr implements a generic Bitcoin network connection manager.
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
Connection Manager handles all the general connection concerns such as
|
|
||||||
maintaining a set number of outbound connections, sourcing peers, banning,
|
|
||||||
limiting max connections, tor lookup, etc.
|
|
||||||
|
|
||||||
The package provides a generic connection manager which is able to accept
|
|
||||||
connection requests from a source or a set of given addresses, dial them and
|
|
||||||
notify the caller on connections. The main intended use is to initialize a pool
|
|
||||||
of active connections and maintain them to remain connected to the P2P network.
|
|
||||||
|
|
||||||
In addition the connection manager provides the following utilities:
|
|
||||||
|
|
||||||
- Notifications on connections or disconnections
|
|
||||||
- Handle failures and retry new addresses from the source
|
|
||||||
- Connect only to specified addresses
|
|
||||||
- Permanent connections with increasing backoff retry timers
|
|
||||||
- Disconnect or Remove an established connection
|
|
||||||
|
|
||||||
## Installation and Updating
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get -u github.com/btcsuite/btcd/connmgr
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Package connmgr is licensed under the [copyfree](http://copyfree.org) ISC License.
|
|
@ -1,569 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package connmgr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// maxFailedAttempts is the maximum number of successive failed connection
|
|
||||||
// attempts after which network failure is assumed and new connections will
|
|
||||||
// be delayed by the configured retry duration.
|
|
||||||
const maxFailedAttempts = 25
|
|
||||||
|
|
||||||
var (
|
|
||||||
//ErrDialNil is used to indicate that Dial cannot be nil in the configuration.
|
|
||||||
ErrDialNil = errors.New("Config: Dial cannot be nil")
|
|
||||||
|
|
||||||
// maxRetryDuration is the max duration of time retrying of a persistent
|
|
||||||
// connection is allowed to grow to. This is necessary since the retry
|
|
||||||
// logic uses a backoff mechanism which increases the interval base times
|
|
||||||
// the number of retries that have been done.
|
|
||||||
maxRetryDuration = time.Minute * 5
|
|
||||||
|
|
||||||
// defaultRetryDuration is the default duration of time for retrying
|
|
||||||
// persistent connections.
|
|
||||||
defaultRetryDuration = time.Second * 5
|
|
||||||
|
|
||||||
// defaultTargetOutbound is the default number of outbound connections to
|
|
||||||
// maintain.
|
|
||||||
defaultTargetOutbound = uint32(8)
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConnState represents the state of the requested connection.
|
|
||||||
type ConnState uint8
|
|
||||||
|
|
||||||
// ConnState can be either pending, established, disconnected or failed. When
|
|
||||||
// a new connection is requested, it is attempted and categorized as
|
|
||||||
// established or failed depending on the connection result. An established
|
|
||||||
// connection which was disconnected is categorized as disconnected.
|
|
||||||
const (
|
|
||||||
ConnPending ConnState = iota
|
|
||||||
ConnFailing
|
|
||||||
ConnCanceled
|
|
||||||
ConnEstablished
|
|
||||||
ConnDisconnected
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConnReq is the connection request to a network address. If permanent, the
|
|
||||||
// connection will be retried on disconnection.
|
|
||||||
type ConnReq struct {
|
|
||||||
// The following variables must only be used atomically.
|
|
||||||
id uint64
|
|
||||||
|
|
||||||
Addr net.Addr
|
|
||||||
Permanent bool
|
|
||||||
|
|
||||||
conn net.Conn
|
|
||||||
state ConnState
|
|
||||||
stateMtx sync.RWMutex
|
|
||||||
retryCount uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// updateState updates the state of the connection request.
|
|
||||||
func (c *ConnReq) updateState(state ConnState) {
|
|
||||||
c.stateMtx.Lock()
|
|
||||||
c.state = state
|
|
||||||
c.stateMtx.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ID returns a unique identifier for the connection request.
|
|
||||||
func (c *ConnReq) ID() uint64 {
|
|
||||||
return atomic.LoadUint64(&c.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// State is the connection state of the requested connection.
|
|
||||||
func (c *ConnReq) State() ConnState {
|
|
||||||
c.stateMtx.RLock()
|
|
||||||
state := c.state
|
|
||||||
c.stateMtx.RUnlock()
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns a human-readable string for the connection request.
|
|
||||||
func (c *ConnReq) String() string {
|
|
||||||
if c.Addr == nil || c.Addr.String() == "" {
|
|
||||||
return fmt.Sprintf("reqid %d", atomic.LoadUint64(&c.id))
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s (reqid %d)", c.Addr, atomic.LoadUint64(&c.id))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config holds the configuration options related to the connection manager.
|
|
||||||
type Config struct {
|
|
||||||
// Listeners defines a slice of listeners for which the connection
|
|
||||||
// manager will take ownership of and accept connections. When a
|
|
||||||
// connection is accepted, the OnAccept handler will be invoked with the
|
|
||||||
// connection. Since the connection manager takes ownership of these
|
|
||||||
// listeners, they will be closed when the connection manager is
|
|
||||||
// stopped.
|
|
||||||
//
|
|
||||||
// This field will not have any effect if the OnAccept field is not
|
|
||||||
// also specified. It may be nil if the caller does not wish to listen
|
|
||||||
// for incoming connections.
|
|
||||||
Listeners []net.Listener
|
|
||||||
|
|
||||||
// OnAccept is a callback that is fired when an inbound connection is
|
|
||||||
// accepted. It is the caller's responsibility to close the connection.
|
|
||||||
// Failure to close the connection will result in the connection manager
|
|
||||||
// believing the connection is still active and thus have undesirable
|
|
||||||
// side effects such as still counting toward maximum connection limits.
|
|
||||||
//
|
|
||||||
// This field will not have any effect if the Listeners field is not
|
|
||||||
// also specified since there couldn't possibly be any accepted
|
|
||||||
// connections in that case.
|
|
||||||
OnAccept func(net.Conn)
|
|
||||||
|
|
||||||
// TargetOutbound is the number of outbound network connections to
|
|
||||||
// maintain. Defaults to 8.
|
|
||||||
TargetOutbound uint32
|
|
||||||
|
|
||||||
// RetryDuration is the duration to wait before retrying connection
|
|
||||||
// requests. Defaults to 5s.
|
|
||||||
RetryDuration time.Duration
|
|
||||||
|
|
||||||
// OnConnection is a callback that is fired when a new outbound
|
|
||||||
// connection is established.
|
|
||||||
OnConnection func(*ConnReq, net.Conn)
|
|
||||||
|
|
||||||
// OnDisconnection is a callback that is fired when an outbound
|
|
||||||
// connection is disconnected.
|
|
||||||
OnDisconnection func(*ConnReq)
|
|
||||||
|
|
||||||
// GetNewAddress is a way to get an address to make a network connection
|
|
||||||
// to. If nil, no new connections will be made automatically.
|
|
||||||
GetNewAddress func() (net.Addr, error)
|
|
||||||
|
|
||||||
// Dial connects to the address on the named network. It cannot be nil.
|
|
||||||
Dial func(net.Addr) (net.Conn, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// registerPending is used to register a pending connection attempt. By
|
|
||||||
// registering pending connection attempts we allow callers to cancel pending
|
|
||||||
// connection attempts before their successful or in the case they're not
|
|
||||||
// longer wanted.
|
|
||||||
type registerPending struct {
|
|
||||||
c *ConnReq
|
|
||||||
done chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleConnected is used to queue a successful connection.
|
|
||||||
type handleConnected struct {
|
|
||||||
c *ConnReq
|
|
||||||
conn net.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleDisconnected is used to remove a connection.
|
|
||||||
type handleDisconnected struct {
|
|
||||||
id uint64
|
|
||||||
retry bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleFailed is used to remove a pending connection.
|
|
||||||
type handleFailed struct {
|
|
||||||
c *ConnReq
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConnManager provides a manager to handle network connections.
|
|
||||||
type ConnManager struct {
|
|
||||||
// The following variables must only be used atomically.
|
|
||||||
connReqCount uint64
|
|
||||||
start int32
|
|
||||||
stop int32
|
|
||||||
|
|
||||||
cfg Config
|
|
||||||
wg sync.WaitGroup
|
|
||||||
failedAttempts uint64
|
|
||||||
requests chan interface{}
|
|
||||||
quit chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleFailedConn handles a connection failed due to a disconnect or any
|
|
||||||
// other failure. If permanent, it retries the connection after the configured
|
|
||||||
// retry duration. Otherwise, if required, it makes a new connection request.
|
|
||||||
// After maxFailedConnectionAttempts new connections will be retried after the
|
|
||||||
// configured retry duration.
|
|
||||||
func (cm *ConnManager) handleFailedConn(c *ConnReq) {
|
|
||||||
if atomic.LoadInt32(&cm.stop) != 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if c.Permanent {
|
|
||||||
c.retryCount++
|
|
||||||
d := time.Duration(c.retryCount) * cm.cfg.RetryDuration
|
|
||||||
if d > maxRetryDuration {
|
|
||||||
d = maxRetryDuration
|
|
||||||
}
|
|
||||||
log.Debugf("Retrying connection to %v in %v", c, d)
|
|
||||||
time.AfterFunc(d, func() {
|
|
||||||
cm.Connect(c)
|
|
||||||
})
|
|
||||||
} else if cm.cfg.GetNewAddress != nil {
|
|
||||||
cm.failedAttempts++
|
|
||||||
if cm.failedAttempts >= maxFailedAttempts {
|
|
||||||
log.Debugf("Max failed connection attempts reached: [%d] "+
|
|
||||||
"-- retrying connection in: %v", maxFailedAttempts,
|
|
||||||
cm.cfg.RetryDuration)
|
|
||||||
time.AfterFunc(cm.cfg.RetryDuration, func() {
|
|
||||||
cm.NewConnReq()
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
go cm.NewConnReq()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// connHandler handles all connection related requests. It must be run as a
|
|
||||||
// goroutine.
|
|
||||||
//
|
|
||||||
// The connection handler makes sure that we maintain a pool of active outbound
|
|
||||||
// connections so that we remain connected to the network. Connection requests
|
|
||||||
// are processed and mapped by their assigned ids.
|
|
||||||
func (cm *ConnManager) connHandler() {
|
|
||||||
|
|
||||||
var (
|
|
||||||
// pending holds all registered conn requests that have yet to
|
|
||||||
// succeed.
|
|
||||||
pending = make(map[uint64]*ConnReq)
|
|
||||||
|
|
||||||
// conns represents the set of all actively connected peers.
|
|
||||||
conns = make(map[uint64]*ConnReq, cm.cfg.TargetOutbound)
|
|
||||||
)
|
|
||||||
|
|
||||||
out:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case req := <-cm.requests:
|
|
||||||
switch msg := req.(type) {
|
|
||||||
|
|
||||||
case registerPending:
|
|
||||||
connReq := msg.c
|
|
||||||
connReq.updateState(ConnPending)
|
|
||||||
pending[msg.c.id] = connReq
|
|
||||||
close(msg.done)
|
|
||||||
|
|
||||||
case handleConnected:
|
|
||||||
connReq := msg.c
|
|
||||||
|
|
||||||
if _, ok := pending[connReq.id]; !ok {
|
|
||||||
if msg.conn != nil {
|
|
||||||
msg.conn.Close()
|
|
||||||
}
|
|
||||||
log.Debugf("Ignoring connection for "+
|
|
||||||
"canceled connreq=%v", connReq)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
connReq.updateState(ConnEstablished)
|
|
||||||
connReq.conn = msg.conn
|
|
||||||
conns[connReq.id] = connReq
|
|
||||||
log.Debugf("Connected to %v", connReq)
|
|
||||||
connReq.retryCount = 0
|
|
||||||
cm.failedAttempts = 0
|
|
||||||
|
|
||||||
delete(pending, connReq.id)
|
|
||||||
|
|
||||||
if cm.cfg.OnConnection != nil {
|
|
||||||
go cm.cfg.OnConnection(connReq, msg.conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
case handleDisconnected:
|
|
||||||
connReq, ok := conns[msg.id]
|
|
||||||
if !ok {
|
|
||||||
connReq, ok = pending[msg.id]
|
|
||||||
if !ok {
|
|
||||||
log.Errorf("Unknown connid=%d",
|
|
||||||
msg.id)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pending connection was found, remove
|
|
||||||
// it from pending map if we should
|
|
||||||
// ignore a later, successful
|
|
||||||
// connection.
|
|
||||||
connReq.updateState(ConnCanceled)
|
|
||||||
log.Debugf("Canceling: %v", connReq)
|
|
||||||
delete(pending, msg.id)
|
|
||||||
continue
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// An existing connection was located, mark as
|
|
||||||
// disconnected and execute disconnection
|
|
||||||
// callback.
|
|
||||||
log.Debugf("Disconnected from %v", connReq)
|
|
||||||
delete(conns, msg.id)
|
|
||||||
|
|
||||||
if connReq.conn != nil {
|
|
||||||
connReq.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
if cm.cfg.OnDisconnection != nil {
|
|
||||||
go cm.cfg.OnDisconnection(connReq)
|
|
||||||
}
|
|
||||||
|
|
||||||
// All internal state has been cleaned up, if
|
|
||||||
// this connection is being removed, we will
|
|
||||||
// make no further attempts with this request.
|
|
||||||
if !msg.retry {
|
|
||||||
connReq.updateState(ConnDisconnected)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we will attempt a reconnection if
|
|
||||||
// we do not have enough peers, or if this is a
|
|
||||||
// persistent peer. The connection request is
|
|
||||||
// re added to the pending map, so that
|
|
||||||
// subsequent processing of connections and
|
|
||||||
// failures do not ignore the request.
|
|
||||||
if uint32(len(conns)) < cm.cfg.TargetOutbound ||
|
|
||||||
connReq.Permanent {
|
|
||||||
|
|
||||||
connReq.updateState(ConnPending)
|
|
||||||
log.Debugf("Reconnecting to %v",
|
|
||||||
connReq)
|
|
||||||
pending[msg.id] = connReq
|
|
||||||
cm.handleFailedConn(connReq)
|
|
||||||
}
|
|
||||||
|
|
||||||
case handleFailed:
|
|
||||||
connReq := msg.c
|
|
||||||
|
|
||||||
if _, ok := pending[connReq.id]; !ok {
|
|
||||||
log.Debugf("Ignoring connection for "+
|
|
||||||
"canceled conn req: %v", connReq)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
connReq.updateState(ConnFailing)
|
|
||||||
log.Debugf("Failed to connect to %v: %v",
|
|
||||||
connReq, msg.err)
|
|
||||||
cm.handleFailedConn(connReq)
|
|
||||||
}
|
|
||||||
|
|
||||||
case <-cm.quit:
|
|
||||||
break out
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cm.wg.Done()
|
|
||||||
log.Trace("Connection handler done")
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewConnReq creates a new connection request and connects to the
|
|
||||||
// corresponding address.
|
|
||||||
func (cm *ConnManager) NewConnReq() {
|
|
||||||
if atomic.LoadInt32(&cm.stop) != 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if cm.cfg.GetNewAddress == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c := &ConnReq{}
|
|
||||||
atomic.StoreUint64(&c.id, atomic.AddUint64(&cm.connReqCount, 1))
|
|
||||||
|
|
||||||
// Submit a request of a pending connection attempt to the connection
|
|
||||||
// manager. By registering the id before the connection is even
|
|
||||||
// established, we'll be able to later cancel the connection via the
|
|
||||||
// Remove method.
|
|
||||||
done := make(chan struct{})
|
|
||||||
select {
|
|
||||||
case cm.requests <- registerPending{c, done}:
|
|
||||||
case <-cm.quit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for the registration to successfully add the pending conn req to
|
|
||||||
// the conn manager's internal state.
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-cm.quit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
addr, err := cm.cfg.GetNewAddress()
|
|
||||||
if err != nil {
|
|
||||||
select {
|
|
||||||
case cm.requests <- handleFailed{c, err}:
|
|
||||||
case <-cm.quit:
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Addr = addr
|
|
||||||
|
|
||||||
cm.Connect(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect assigns an id and dials a connection to the address of the
|
|
||||||
// connection request.
|
|
||||||
func (cm *ConnManager) Connect(c *ConnReq) {
|
|
||||||
if atomic.LoadInt32(&cm.stop) != 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if atomic.LoadUint64(&c.id) == 0 {
|
|
||||||
atomic.StoreUint64(&c.id, atomic.AddUint64(&cm.connReqCount, 1))
|
|
||||||
|
|
||||||
// Submit a request of a pending connection attempt to the
|
|
||||||
// connection manager. By registering the id before the
|
|
||||||
// connection is even established, we'll be able to later
|
|
||||||
// cancel the connection via the Remove method.
|
|
||||||
done := make(chan struct{})
|
|
||||||
select {
|
|
||||||
case cm.requests <- registerPending{c, done}:
|
|
||||||
case <-cm.quit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for the registration to successfully add the pending
|
|
||||||
// conn req to the conn manager's internal state.
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-cm.quit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("Attempting to connect to %v", c)
|
|
||||||
|
|
||||||
conn, err := cm.cfg.Dial(c.Addr)
|
|
||||||
if err != nil {
|
|
||||||
select {
|
|
||||||
case cm.requests <- handleFailed{c, err}:
|
|
||||||
case <-cm.quit:
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case cm.requests <- handleConnected{c, conn}:
|
|
||||||
case <-cm.quit:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disconnect disconnects the connection corresponding to the given connection
|
|
||||||
// id. If permanent, the connection will be retried with an increasing backoff
|
|
||||||
// duration.
|
|
||||||
func (cm *ConnManager) Disconnect(id uint64) {
|
|
||||||
if atomic.LoadInt32(&cm.stop) != 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case cm.requests <- handleDisconnected{id, true}:
|
|
||||||
case <-cm.quit:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove removes the connection corresponding to the given connection id from
|
|
||||||
// known connections.
|
|
||||||
//
|
|
||||||
// NOTE: This method can also be used to cancel a lingering connection attempt
|
|
||||||
// that hasn't yet succeeded.
|
|
||||||
func (cm *ConnManager) Remove(id uint64) {
|
|
||||||
if atomic.LoadInt32(&cm.stop) != 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case cm.requests <- handleDisconnected{id, false}:
|
|
||||||
case <-cm.quit:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// listenHandler accepts incoming connections on a given listener. It must be
|
|
||||||
// run as a goroutine.
|
|
||||||
func (cm *ConnManager) listenHandler(listener net.Listener) {
|
|
||||||
log.Infof("Server listening on %s", listener.Addr())
|
|
||||||
for atomic.LoadInt32(&cm.stop) == 0 {
|
|
||||||
conn, err := listener.Accept()
|
|
||||||
if err != nil {
|
|
||||||
// Only log the error if not forcibly shutting down.
|
|
||||||
if atomic.LoadInt32(&cm.stop) == 0 {
|
|
||||||
log.Errorf("Can't accept connection: %v", err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
go cm.cfg.OnAccept(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
cm.wg.Done()
|
|
||||||
log.Tracef("Listener handler done for %s", listener.Addr())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start launches the connection manager and begins connecting to the network.
|
|
||||||
func (cm *ConnManager) Start() {
|
|
||||||
// Already started?
|
|
||||||
if atomic.AddInt32(&cm.start, 1) != 1 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Trace("Connection manager started")
|
|
||||||
cm.wg.Add(1)
|
|
||||||
go cm.connHandler()
|
|
||||||
|
|
||||||
// Start all the listeners so long as the caller requested them and
|
|
||||||
// provided a callback to be invoked when connections are accepted.
|
|
||||||
if cm.cfg.OnAccept != nil {
|
|
||||||
for _, listner := range cm.cfg.Listeners {
|
|
||||||
cm.wg.Add(1)
|
|
||||||
go cm.listenHandler(listner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := atomic.LoadUint64(&cm.connReqCount); i < uint64(cm.cfg.TargetOutbound); i++ {
|
|
||||||
go cm.NewConnReq()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait blocks until the connection manager halts gracefully.
|
|
||||||
func (cm *ConnManager) Wait() {
|
|
||||||
cm.wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop gracefully shuts down the connection manager.
|
|
||||||
func (cm *ConnManager) Stop() {
|
|
||||||
if atomic.AddInt32(&cm.stop, 1) != 1 {
|
|
||||||
log.Warnf("Connection manager already stopped")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop all the listeners. There will not be any listeners if
|
|
||||||
// listening is disabled.
|
|
||||||
for _, listener := range cm.cfg.Listeners {
|
|
||||||
// Ignore the error since this is shutdown and there is no way
|
|
||||||
// to recover anyways.
|
|
||||||
_ = listener.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
close(cm.quit)
|
|
||||||
log.Trace("Connection manager stopped")
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns a new connection manager.
|
|
||||||
// Use Start to start connecting to the network.
|
|
||||||
func New(cfg *Config) (*ConnManager, error) {
|
|
||||||
if cfg.Dial == nil {
|
|
||||||
return nil, ErrDialNil
|
|
||||||
}
|
|
||||||
// Default to sane values
|
|
||||||
if cfg.RetryDuration <= 0 {
|
|
||||||
cfg.RetryDuration = defaultRetryDuration
|
|
||||||
}
|
|
||||||
if cfg.TargetOutbound == 0 {
|
|
||||||
cfg.TargetOutbound = defaultTargetOutbound
|
|
||||||
}
|
|
||||||
cm := ConnManager{
|
|
||||||
cfg: *cfg, // Copy so caller can't mutate
|
|
||||||
requests: make(chan interface{}),
|
|
||||||
quit: make(chan struct{}),
|
|
||||||
}
|
|
||||||
return &cm, nil
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package connmgr implements a generic Bitcoin network connection manager.
|
|
||||||
|
|
||||||
Connection Manager Overview
|
|
||||||
|
|
||||||
Connection Manager handles all the general connection concerns such as
|
|
||||||
maintaining a set number of outbound connections, sourcing peers, banning,
|
|
||||||
limiting max connections, tor lookup, etc.
|
|
||||||
*/
|
|
||||||
package connmgr
|
|
@ -1,146 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package connmgr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// Halflife defines the time (in seconds) by which the transient part
|
|
||||||
// of the ban score decays to one half of it's original value.
|
|
||||||
Halflife = 60
|
|
||||||
|
|
||||||
// lambda is the decaying constant.
|
|
||||||
lambda = math.Ln2 / Halflife
|
|
||||||
|
|
||||||
// Lifetime defines the maximum age of the transient part of the ban
|
|
||||||
// score to be considered a non-zero score (in seconds).
|
|
||||||
Lifetime = 1800
|
|
||||||
|
|
||||||
// precomputedLen defines the amount of decay factors (one per second) that
|
|
||||||
// should be precomputed at initialization.
|
|
||||||
precomputedLen = 64
|
|
||||||
)
|
|
||||||
|
|
||||||
// precomputedFactor stores precomputed exponential decay factors for the first
|
|
||||||
// 'precomputedLen' seconds starting from t == 0.
|
|
||||||
var precomputedFactor [precomputedLen]float64
|
|
||||||
|
|
||||||
// init precomputes decay factors.
|
|
||||||
func init() {
|
|
||||||
for i := range precomputedFactor {
|
|
||||||
precomputedFactor[i] = math.Exp(-1.0 * float64(i) * lambda)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// decayFactor returns the decay factor at t seconds, using precalculated values
|
|
||||||
// if available, or calculating the factor if needed.
|
|
||||||
func decayFactor(t int64) float64 {
|
|
||||||
if t < precomputedLen {
|
|
||||||
return precomputedFactor[t]
|
|
||||||
}
|
|
||||||
return math.Exp(-1.0 * float64(t) * lambda)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DynamicBanScore provides dynamic ban scores consisting of a persistent and a
|
|
||||||
// decaying component. The persistent score could be utilized to create simple
|
|
||||||
// additive banning policies similar to those found in other bitcoin node
|
|
||||||
// implementations.
|
|
||||||
//
|
|
||||||
// The decaying score enables the creation of evasive logic which handles
|
|
||||||
// misbehaving peers (especially application layer DoS attacks) gracefully
|
|
||||||
// by disconnecting and banning peers attempting various kinds of flooding.
|
|
||||||
// DynamicBanScore allows these two approaches to be used in tandem.
|
|
||||||
//
|
|
||||||
// Zero value: Values of type DynamicBanScore are immediately ready for use upon
|
|
||||||
// declaration.
|
|
||||||
type DynamicBanScore struct {
|
|
||||||
lastUnix int64
|
|
||||||
transient float64
|
|
||||||
persistent uint32
|
|
||||||
mtx sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the ban score as a human-readable string.
|
|
||||||
func (s *DynamicBanScore) String() string {
|
|
||||||
s.mtx.Lock()
|
|
||||||
r := fmt.Sprintf("persistent %v + transient %v at %v = %v as of now",
|
|
||||||
s.persistent, s.transient, s.lastUnix, s.Int())
|
|
||||||
s.mtx.Unlock()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Int returns the current ban score, the sum of the persistent and decaying
|
|
||||||
// scores.
|
|
||||||
//
|
|
||||||
// This function is safe for concurrent access.
|
|
||||||
func (s *DynamicBanScore) Int() uint32 {
|
|
||||||
s.mtx.Lock()
|
|
||||||
r := s.int(time.Now())
|
|
||||||
s.mtx.Unlock()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Increase increases both the persistent and decaying scores by the values
|
|
||||||
// passed as parameters. The resulting score is returned.
|
|
||||||
//
|
|
||||||
// This function is safe for concurrent access.
|
|
||||||
func (s *DynamicBanScore) Increase(persistent, transient uint32) uint32 {
|
|
||||||
s.mtx.Lock()
|
|
||||||
r := s.increase(persistent, transient, time.Now())
|
|
||||||
s.mtx.Unlock()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset set both persistent and decaying scores to zero.
|
|
||||||
//
|
|
||||||
// This function is safe for concurrent access.
|
|
||||||
func (s *DynamicBanScore) Reset() {
|
|
||||||
s.mtx.Lock()
|
|
||||||
s.persistent = 0
|
|
||||||
s.transient = 0
|
|
||||||
s.lastUnix = 0
|
|
||||||
s.mtx.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// int returns the ban score, the sum of the persistent and decaying scores at a
|
|
||||||
// given point in time.
|
|
||||||
//
|
|
||||||
// This function is not safe for concurrent access. It is intended to be used
|
|
||||||
// internally and during testing.
|
|
||||||
func (s *DynamicBanScore) int(t time.Time) uint32 {
|
|
||||||
dt := t.Unix() - s.lastUnix
|
|
||||||
if s.transient < 1 || dt < 0 || Lifetime < dt {
|
|
||||||
return s.persistent
|
|
||||||
}
|
|
||||||
return s.persistent + uint32(s.transient*decayFactor(dt))
|
|
||||||
}
|
|
||||||
|
|
||||||
// increase increases the persistent, the decaying or both scores by the values
|
|
||||||
// passed as parameters. The resulting score is calculated as if the action was
|
|
||||||
// carried out at the point time represented by the third parameter. The
|
|
||||||
// resulting score is returned.
|
|
||||||
//
|
|
||||||
// This function is not safe for concurrent access.
|
|
||||||
func (s *DynamicBanScore) increase(persistent, transient uint32, t time.Time) uint32 {
|
|
||||||
s.persistent += persistent
|
|
||||||
tu := t.Unix()
|
|
||||||
dt := tu - s.lastUnix
|
|
||||||
|
|
||||||
if transient > 0 {
|
|
||||||
if Lifetime < dt {
|
|
||||||
s.transient = 0
|
|
||||||
} else if s.transient > 1 && dt > 0 {
|
|
||||||
s.transient *= decayFactor(dt)
|
|
||||||
}
|
|
||||||
s.transient += float64(transient)
|
|
||||||
s.lastUnix = tu
|
|
||||||
}
|
|
||||||
return s.persistent + uint32(s.transient)
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package connmgr
|
|
||||||
|
|
||||||
import "github.com/btcsuite/btclog"
|
|
||||||
|
|
||||||
// log is a logger that is initialized with no output filters. This
|
|
||||||
// means the package will not perform any logging by default until the caller
|
|
||||||
// requests it.
|
|
||||||
var log btclog.Logger
|
|
||||||
|
|
||||||
// The default amount of logging is none.
|
|
||||||
func init() {
|
|
||||||
DisableLog()
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
|
||||||
func DisableLog() {
|
|
||||||
log = btclog.Disabled
|
|
||||||
}
|
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
|
||||||
log = logger
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package connmgr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
mrand "math/rand"
|
|
||||||
"net"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
"github.com/btcsuite/btcd/wire"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// These constants are used by the DNS seed code to pick a random last
|
|
||||||
// seen time.
|
|
||||||
secondsIn3Days int32 = 24 * 60 * 60 * 3
|
|
||||||
secondsIn4Days int32 = 24 * 60 * 60 * 4
|
|
||||||
)
|
|
||||||
|
|
||||||
// OnSeed is the signature of the callback function which is invoked when DNS
|
|
||||||
// seeding is succesfull.
|
|
||||||
type OnSeed func(addrs []*wire.NetAddress)
|
|
||||||
|
|
||||||
// LookupFunc is the signature of the DNS lookup function.
|
|
||||||
type LookupFunc func(string) ([]net.IP, error)
|
|
||||||
|
|
||||||
// SeedFromDNS uses DNS seeding to populate the address manager with peers.
|
|
||||||
func SeedFromDNS(chainParams *chaincfg.Params, reqServices wire.ServiceFlag,
|
|
||||||
lookupFn LookupFunc, seedFn OnSeed) {
|
|
||||||
|
|
||||||
for _, dnsseed := range chainParams.DNSSeeds {
|
|
||||||
var host string
|
|
||||||
if !dnsseed.HasFiltering || reqServices == wire.SFNodeNetwork {
|
|
||||||
host = dnsseed.Host
|
|
||||||
} else {
|
|
||||||
host = fmt.Sprintf("x%x.%s", uint64(reqServices), dnsseed.Host)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func(host string) {
|
|
||||||
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
||||||
|
|
||||||
seedpeers, err := lookupFn(host)
|
|
||||||
if err != nil {
|
|
||||||
log.Infof("DNS discovery failed on seed %s: %v", host, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
numPeers := len(seedpeers)
|
|
||||||
|
|
||||||
log.Infof("%d addresses found from DNS seed %s", numPeers, host)
|
|
||||||
|
|
||||||
if numPeers == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
addresses := make([]*wire.NetAddress, len(seedpeers))
|
|
||||||
// if this errors then we have *real* problems
|
|
||||||
intPort, _ := strconv.Atoi(chainParams.DefaultPort)
|
|
||||||
for i, peer := range seedpeers {
|
|
||||||
addresses[i] = wire.NewNetAddressTimestamp(
|
|
||||||
// bitcoind seeds with addresses from
|
|
||||||
// a time randomly selected between 3
|
|
||||||
// and 7 days ago.
|
|
||||||
time.Now().Add(-1*time.Second*time.Duration(secondsIn3Days+
|
|
||||||
randSource.Int31n(secondsIn4Days))),
|
|
||||||
0, peer, uint16(intPort))
|
|
||||||
}
|
|
||||||
|
|
||||||
seedFn(addresses)
|
|
||||||
}(host)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,129 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package connmgr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
torSucceeded = 0x00
|
|
||||||
torGeneralError = 0x01
|
|
||||||
torNotAllowed = 0x02
|
|
||||||
torNetUnreachable = 0x03
|
|
||||||
torHostUnreachable = 0x04
|
|
||||||
torConnectionRefused = 0x05
|
|
||||||
torTTLExpired = 0x06
|
|
||||||
torCmdNotSupported = 0x07
|
|
||||||
torAddrNotSupported = 0x08
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrTorInvalidAddressResponse indicates an invalid address was
|
|
||||||
// returned by the Tor DNS resolver.
|
|
||||||
ErrTorInvalidAddressResponse = errors.New("invalid address response")
|
|
||||||
|
|
||||||
// ErrTorInvalidProxyResponse indicates the Tor proxy returned a
|
|
||||||
// response in an unexpected format.
|
|
||||||
ErrTorInvalidProxyResponse = errors.New("invalid proxy response")
|
|
||||||
|
|
||||||
// ErrTorUnrecognizedAuthMethod indicates the authentication method
|
|
||||||
// provided is not recognized.
|
|
||||||
ErrTorUnrecognizedAuthMethod = errors.New("invalid proxy authentication method")
|
|
||||||
|
|
||||||
torStatusErrors = map[byte]error{
|
|
||||||
torSucceeded: errors.New("tor succeeded"),
|
|
||||||
torGeneralError: errors.New("tor general error"),
|
|
||||||
torNotAllowed: errors.New("tor not allowed"),
|
|
||||||
torNetUnreachable: errors.New("tor network is unreachable"),
|
|
||||||
torHostUnreachable: errors.New("tor host is unreachable"),
|
|
||||||
torConnectionRefused: errors.New("tor connection refused"),
|
|
||||||
torTTLExpired: errors.New("tor TTL expired"),
|
|
||||||
torCmdNotSupported: errors.New("tor command not supported"),
|
|
||||||
torAddrNotSupported: errors.New("tor address type not supported"),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// TorLookupIP uses Tor to resolve DNS via the SOCKS extension they provide for
|
|
||||||
// resolution over the Tor network. Tor itself doesn't support ipv6 so this
|
|
||||||
// doesn't either.
|
|
||||||
func TorLookupIP(host, proxy string) ([]net.IP, error) {
|
|
||||||
conn, err := net.Dial("tcp", proxy)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
buf := []byte{'\x05', '\x01', '\x00'}
|
|
||||||
_, err = conn.Write(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = make([]byte, 2)
|
|
||||||
_, err = conn.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if buf[0] != '\x05' {
|
|
||||||
return nil, ErrTorInvalidProxyResponse
|
|
||||||
}
|
|
||||||
if buf[1] != '\x00' {
|
|
||||||
return nil, ErrTorUnrecognizedAuthMethod
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = make([]byte, 7+len(host))
|
|
||||||
buf[0] = 5 // protocol version
|
|
||||||
buf[1] = '\xF0' // Tor Resolve
|
|
||||||
buf[2] = 0 // reserved
|
|
||||||
buf[3] = 3 // Tor Resolve
|
|
||||||
buf[4] = byte(len(host))
|
|
||||||
copy(buf[5:], host)
|
|
||||||
buf[5+len(host)] = 0 // Port 0
|
|
||||||
|
|
||||||
_, err = conn.Write(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = make([]byte, 4)
|
|
||||||
_, err = conn.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if buf[0] != 5 {
|
|
||||||
return nil, ErrTorInvalidProxyResponse
|
|
||||||
}
|
|
||||||
if buf[1] != 0 {
|
|
||||||
if int(buf[1]) >= len(torStatusErrors) {
|
|
||||||
return nil, ErrTorInvalidProxyResponse
|
|
||||||
} else if err := torStatusErrors[buf[1]]; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, ErrTorInvalidProxyResponse
|
|
||||||
}
|
|
||||||
if buf[3] != 1 {
|
|
||||||
err := torStatusErrors[torGeneralError]
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = make([]byte, 4)
|
|
||||||
bytes, err := conn.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if bytes != 4 {
|
|
||||||
return nil, ErrTorInvalidAddressResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
r := binary.BigEndian.Uint32(buf)
|
|
||||||
|
|
||||||
addr := make([]net.IP, 1)
|
|
||||||
addr[0] = net.IPv4(byte(r>>24), byte(r>>16), byte(r>>8), byte(r))
|
|
||||||
|
|
||||||
return addr, nil
|
|
||||||
}
|
|
@ -1,113 +0,0 @@
|
|||||||
wire
|
|
||||||
====
|
|
||||||
|
|
||||||
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)](https://travis-ci.org/btcsuite/btcd)
|
|
||||||
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
|
||||||
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/wire)
|
|
||||||
=======
|
|
||||||
|
|
||||||
Package wire implements the bitcoin wire protocol. A comprehensive suite of
|
|
||||||
tests with 100% test coverage is provided to ensure proper functionality.
|
|
||||||
|
|
||||||
There is an associated blog post about the release of this package
|
|
||||||
[here](https://blog.conformal.com/btcwire-the-bitcoin-wire-protocol-package-from-btcd/).
|
|
||||||
|
|
||||||
This package has intentionally been designed so it can be used as a standalone
|
|
||||||
package for any projects needing to interface with bitcoin peers at the wire
|
|
||||||
protocol level.
|
|
||||||
|
|
||||||
## Installation and Updating
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get -u github.com/btcsuite/btcd/wire
|
|
||||||
```
|
|
||||||
|
|
||||||
## Bitcoin Message Overview
|
|
||||||
|
|
||||||
The bitcoin protocol consists of exchanging messages between peers. Each message
|
|
||||||
is preceded by a header which identifies information about it such as which
|
|
||||||
bitcoin network it is a part of, its type, how big it is, and a checksum to
|
|
||||||
verify validity. All encoding and decoding of message headers is handled by this
|
|
||||||
package.
|
|
||||||
|
|
||||||
To accomplish this, there is a generic interface for bitcoin messages named
|
|
||||||
`Message` which allows messages of any type to be read, written, or passed
|
|
||||||
around through channels, functions, etc. In addition, concrete implementations
|
|
||||||
of most of the currently supported bitcoin messages are provided. For these
|
|
||||||
supported messages, all of the details of marshalling and unmarshalling to and
|
|
||||||
from the wire using bitcoin encoding are handled so the caller doesn't have to
|
|
||||||
concern themselves with the specifics.
|
|
||||||
|
|
||||||
## Reading Messages Example
|
|
||||||
|
|
||||||
In order to unmarshal bitcoin messages from the wire, use the `ReadMessage`
|
|
||||||
function. It accepts any `io.Reader`, but typically this will be a `net.Conn`
|
|
||||||
to a remote node running a bitcoin peer. Example syntax is:
|
|
||||||
|
|
||||||
```Go
|
|
||||||
// Use the most recent protocol version supported by the package and the
|
|
||||||
// main bitcoin network.
|
|
||||||
pver := wire.ProtocolVersion
|
|
||||||
btcnet := wire.MainNet
|
|
||||||
|
|
||||||
// Reads and validates the next bitcoin message from conn using the
|
|
||||||
// protocol version pver and the bitcoin network btcnet. The returns
|
|
||||||
// are a wire.Message, a []byte which contains the unmarshalled
|
|
||||||
// raw payload, and a possible error.
|
|
||||||
msg, rawPayload, err := wire.ReadMessage(conn, pver, btcnet)
|
|
||||||
if err != nil {
|
|
||||||
// Log and handle the error
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
See the package documentation for details on determining the message type.
|
|
||||||
|
|
||||||
## Writing Messages Example
|
|
||||||
|
|
||||||
In order to marshal bitcoin messages to the wire, use the `WriteMessage`
|
|
||||||
function. It accepts any `io.Writer`, but typically this will be a `net.Conn`
|
|
||||||
to a remote node running a bitcoin peer. Example syntax to request addresses
|
|
||||||
from a remote peer is:
|
|
||||||
|
|
||||||
```Go
|
|
||||||
// Use the most recent protocol version supported by the package and the
|
|
||||||
// main bitcoin network.
|
|
||||||
pver := wire.ProtocolVersion
|
|
||||||
btcnet := wire.MainNet
|
|
||||||
|
|
||||||
// Create a new getaddr bitcoin message.
|
|
||||||
msg := wire.NewMsgGetAddr()
|
|
||||||
|
|
||||||
// Writes a bitcoin message msg to conn using the protocol version
|
|
||||||
// pver, and the bitcoin network btcnet. The return is a possible
|
|
||||||
// error.
|
|
||||||
err := wire.WriteMessage(conn, msg, pver, btcnet)
|
|
||||||
if err != nil {
|
|
||||||
// Log and handle the error
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## GPG Verification Key
|
|
||||||
|
|
||||||
All official release tags are signed by Conformal so users can ensure the code
|
|
||||||
has not been tampered with and is coming from the btcsuite developers. To
|
|
||||||
verify the signature perform the following:
|
|
||||||
|
|
||||||
- Download the public key from the Conformal website at
|
|
||||||
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
|
|
||||||
|
|
||||||
- Import the public key into your GPG keyring:
|
|
||||||
```bash
|
|
||||||
gpg --import GIT-GPG-KEY-conformal.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
- Verify the release tag with the following command where `TAG_NAME` is a
|
|
||||||
placeholder for the specific tag:
|
|
||||||
```bash
|
|
||||||
git tag -v TAG_NAME
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Package wire is licensed under the [copyfree](http://copyfree.org) ISC
|
|
||||||
License.
|
|
@ -1,128 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
|
|
||||||
// Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes +
|
|
||||||
// PrevBlock and MerkleRoot hashes.
|
|
||||||
const MaxBlockHeaderPayload = 16 + (chainhash.HashSize * 2)
|
|
||||||
|
|
||||||
// BlockHeader defines information about a block and is used in the bitcoin
|
|
||||||
// block (MsgBlock) and headers (MsgHeaders) messages.
|
|
||||||
type BlockHeader struct {
|
|
||||||
// Version of the block. This is not the same as the protocol version.
|
|
||||||
Version int32
|
|
||||||
|
|
||||||
// Hash of the previous block header in the block chain.
|
|
||||||
PrevBlock chainhash.Hash
|
|
||||||
|
|
||||||
// Merkle tree reference to hash of all transactions for the block.
|
|
||||||
MerkleRoot chainhash.Hash
|
|
||||||
|
|
||||||
// Time the block was created. This is, unfortunately, encoded as a
|
|
||||||
// uint32 on the wire and therefore is limited to 2106.
|
|
||||||
Timestamp time.Time
|
|
||||||
|
|
||||||
// Difficulty target for the block.
|
|
||||||
Bits uint32
|
|
||||||
|
|
||||||
// Nonce used to generate the block.
|
|
||||||
Nonce uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// blockHeaderLen is a constant that represents the number of bytes for a block
|
|
||||||
// header.
|
|
||||||
const blockHeaderLen = 80
|
|
||||||
|
|
||||||
// BlockHash computes the block identifier hash for the given block header.
|
|
||||||
func (h *BlockHeader) BlockHash() chainhash.Hash {
|
|
||||||
// Encode the header and double sha256 everything prior to the number of
|
|
||||||
// transactions. Ignore the error returns since there is no way the
|
|
||||||
// encode could fail except being out of memory which would cause a
|
|
||||||
// run-time panic.
|
|
||||||
buf := bytes.NewBuffer(make([]byte, 0, MaxBlockHeaderPayload))
|
|
||||||
_ = writeBlockHeader(buf, 0, h)
|
|
||||||
|
|
||||||
return chainhash.DoubleHashH(buf.Bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Deserialize for decoding block headers stored to disk, such as in a
|
|
||||||
// database, as opposed to decoding block headers from the wire.
|
|
||||||
func (h *BlockHeader) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
return readBlockHeader(r, pver, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Serialize for encoding block headers to be stored to disk, such as in a
|
|
||||||
// database, as opposed to encoding block headers for the wire.
|
|
||||||
func (h *BlockHeader) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
return writeBlockHeader(w, pver, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a block header from r into the receiver using a format
|
|
||||||
// that is suitable for long-term storage such as a database while respecting
|
|
||||||
// the Version field.
|
|
||||||
func (h *BlockHeader) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of readBlockHeader.
|
|
||||||
return readBlockHeader(r, 0, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize encodes a block header from r into the receiver using a format
|
|
||||||
// that is suitable for long-term storage such as a database while respecting
|
|
||||||
// the Version field.
|
|
||||||
func (h *BlockHeader) Serialize(w io.Writer) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of writeBlockHeader.
|
|
||||||
return writeBlockHeader(w, 0, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBlockHeader returns a new BlockHeader using the provided version, previous
|
|
||||||
// block hash, merkle root hash, difficulty bits, and nonce used to generate the
|
|
||||||
// block with defaults for the remaining fields.
|
|
||||||
func NewBlockHeader(version int32, prevHash, merkleRootHash *chainhash.Hash,
|
|
||||||
bits uint32, nonce uint32) *BlockHeader {
|
|
||||||
|
|
||||||
// Limit the timestamp to one second precision since the protocol
|
|
||||||
// doesn't support better.
|
|
||||||
return &BlockHeader{
|
|
||||||
Version: version,
|
|
||||||
PrevBlock: *prevHash,
|
|
||||||
MerkleRoot: *merkleRootHash,
|
|
||||||
Timestamp: time.Unix(time.Now().Unix(), 0),
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// readBlockHeader reads a bitcoin block header from r. See Deserialize for
|
|
||||||
// decoding block headers stored to disk, such as in a database, as opposed to
|
|
||||||
// decoding from the wire.
|
|
||||||
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
|
|
||||||
return readElements(r, &bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
|
|
||||||
(*uint32Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeBlockHeader writes a bitcoin block header to w. See Serialize for
|
|
||||||
// encoding block headers to be stored to disk, such as in a database, as
|
|
||||||
// opposed to encoding for the wire.
|
|
||||||
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
|
|
||||||
sec := uint32(bh.Timestamp.Unix())
|
|
||||||
return writeElements(w, bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
|
|
||||||
sec, bh.Bits, bh.Nonce)
|
|
||||||
}
|
|
@ -1,689 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"math"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// MaxVarIntPayload is the maximum payload size for a variable length integer.
|
|
||||||
MaxVarIntPayload = 9
|
|
||||||
|
|
||||||
// binaryFreeListMaxItems is the number of buffers to keep in the free
|
|
||||||
// list to use for binary serialization and deserialization.
|
|
||||||
binaryFreeListMaxItems = 1024
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// littleEndian is a convenience variable since binary.LittleEndian is
|
|
||||||
// quite long.
|
|
||||||
littleEndian = binary.LittleEndian
|
|
||||||
|
|
||||||
// bigEndian is a convenience variable since binary.BigEndian is quite
|
|
||||||
// long.
|
|
||||||
bigEndian = binary.BigEndian
|
|
||||||
)
|
|
||||||
|
|
||||||
// binaryFreeList defines a concurrent safe free list of byte slices (up to the
|
|
||||||
// maximum number defined by the binaryFreeListMaxItems constant) that have a
|
|
||||||
// cap of 8 (thus it supports up to a uint64). It is used to provide temporary
|
|
||||||
// buffers for serializing and deserializing primitive numbers to and from their
|
|
||||||
// binary encoding in order to greatly reduce the number of allocations
|
|
||||||
// required.
|
|
||||||
//
|
|
||||||
// For convenience, functions are provided for each of the primitive unsigned
|
|
||||||
// integers that automatically obtain a buffer from the free list, perform the
|
|
||||||
// necessary binary conversion, read from or write to the given io.Reader or
|
|
||||||
// io.Writer, and return the buffer to the free list.
|
|
||||||
type binaryFreeList chan []byte
|
|
||||||
|
|
||||||
// Borrow returns a byte slice from the free list with a length of 8. A new
|
|
||||||
// buffer is allocated if there are not any available on the free list.
|
|
||||||
func (l binaryFreeList) Borrow() []byte {
|
|
||||||
var buf []byte
|
|
||||||
select {
|
|
||||||
case buf = <-l:
|
|
||||||
default:
|
|
||||||
buf = make([]byte, 8)
|
|
||||||
}
|
|
||||||
return buf[:8]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return puts the provided byte slice back on the free list. The buffer MUST
|
|
||||||
// have been obtained via the Borrow function and therefore have a cap of 8.
|
|
||||||
func (l binaryFreeList) Return(buf []byte) {
|
|
||||||
select {
|
|
||||||
case l <- buf:
|
|
||||||
default:
|
|
||||||
// Let it go to the garbage collector.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint8 reads a single byte from the provided reader using a buffer from the
|
|
||||||
// free list and returns it as a uint8.
|
|
||||||
func (l binaryFreeList) Uint8(r io.Reader) (uint8, error) {
|
|
||||||
buf := l.Borrow()[:1]
|
|
||||||
if _, err := io.ReadFull(r, buf); err != nil {
|
|
||||||
l.Return(buf)
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv := buf[0]
|
|
||||||
l.Return(buf)
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint16 reads two bytes from the provided reader using a buffer from the
|
|
||||||
// free list, converts it to a number using the provided byte order, and returns
|
|
||||||
// the resulting uint16.
|
|
||||||
func (l binaryFreeList) Uint16(r io.Reader, byteOrder binary.ByteOrder) (uint16, error) {
|
|
||||||
buf := l.Borrow()[:2]
|
|
||||||
if _, err := io.ReadFull(r, buf); err != nil {
|
|
||||||
l.Return(buf)
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv := byteOrder.Uint16(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint32 reads four bytes from the provided reader using a buffer from the
|
|
||||||
// free list, converts it to a number using the provided byte order, and returns
|
|
||||||
// the resulting uint32.
|
|
||||||
func (l binaryFreeList) Uint32(r io.Reader, byteOrder binary.ByteOrder) (uint32, error) {
|
|
||||||
buf := l.Borrow()[:4]
|
|
||||||
if _, err := io.ReadFull(r, buf); err != nil {
|
|
||||||
l.Return(buf)
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv := byteOrder.Uint32(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint64 reads eight bytes from the provided reader using a buffer from the
|
|
||||||
// free list, converts it to a number using the provided byte order, and returns
|
|
||||||
// the resulting uint64.
|
|
||||||
func (l binaryFreeList) Uint64(r io.Reader, byteOrder binary.ByteOrder) (uint64, error) {
|
|
||||||
buf := l.Borrow()[:8]
|
|
||||||
if _, err := io.ReadFull(r, buf); err != nil {
|
|
||||||
l.Return(buf)
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv := byteOrder.Uint64(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// PutUint8 copies the provided uint8 into a buffer from the free list and
|
|
||||||
// writes the resulting byte to the given writer.
|
|
||||||
func (l binaryFreeList) PutUint8(w io.Writer, val uint8) error {
|
|
||||||
buf := l.Borrow()[:1]
|
|
||||||
buf[0] = val
|
|
||||||
_, err := w.Write(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PutUint16 serializes the provided uint16 using the given byte order into a
|
|
||||||
// buffer from the free list and writes the resulting two bytes to the given
|
|
||||||
// writer.
|
|
||||||
func (l binaryFreeList) PutUint16(w io.Writer, byteOrder binary.ByteOrder, val uint16) error {
|
|
||||||
buf := l.Borrow()[:2]
|
|
||||||
byteOrder.PutUint16(buf, val)
|
|
||||||
_, err := w.Write(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PutUint32 serializes the provided uint32 using the given byte order into a
|
|
||||||
// buffer from the free list and writes the resulting four bytes to the given
|
|
||||||
// writer.
|
|
||||||
func (l binaryFreeList) PutUint32(w io.Writer, byteOrder binary.ByteOrder, val uint32) error {
|
|
||||||
buf := l.Borrow()[:4]
|
|
||||||
byteOrder.PutUint32(buf, val)
|
|
||||||
_, err := w.Write(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PutUint64 serializes the provided uint64 using the given byte order into a
|
|
||||||
// buffer from the free list and writes the resulting eight bytes to the given
|
|
||||||
// writer.
|
|
||||||
func (l binaryFreeList) PutUint64(w io.Writer, byteOrder binary.ByteOrder, val uint64) error {
|
|
||||||
buf := l.Borrow()[:8]
|
|
||||||
byteOrder.PutUint64(buf, val)
|
|
||||||
_, err := w.Write(buf)
|
|
||||||
l.Return(buf)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// binarySerializer provides a free list of buffers to use for serializing and
|
|
||||||
// deserializing primitive integer values to and from io.Readers and io.Writers.
|
|
||||||
var binarySerializer binaryFreeList = make(chan []byte, binaryFreeListMaxItems)
|
|
||||||
|
|
||||||
// errNonCanonicalVarInt is the common format string used for non-canonically
|
|
||||||
// encoded variable length integer errors.
|
|
||||||
var errNonCanonicalVarInt = "non-canonical varint %x - discriminant %x must " +
|
|
||||||
"encode a value greater than %x"
|
|
||||||
|
|
||||||
// uint32Time represents a unix timestamp encoded with a uint32. It is used as
|
|
||||||
// a way to signal the readElement function how to decode a timestamp into a Go
|
|
||||||
// time.Time since it is otherwise ambiguous.
|
|
||||||
type uint32Time time.Time
|
|
||||||
|
|
||||||
// int64Time represents a unix timestamp encoded with an int64. It is used as
|
|
||||||
// a way to signal the readElement function how to decode a timestamp into a Go
|
|
||||||
// time.Time since it is otherwise ambiguous.
|
|
||||||
type int64Time time.Time
|
|
||||||
|
|
||||||
// readElement reads the next sequence of bytes from r using little endian
|
|
||||||
// depending on the concrete type of element pointed to.
|
|
||||||
func readElement(r io.Reader, element interface{}) error {
|
|
||||||
// Attempt to read the element based on the concrete type via fast
|
|
||||||
// type assertions first.
|
|
||||||
switch e := element.(type) {
|
|
||||||
case *int32:
|
|
||||||
rv, err := binarySerializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = int32(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *uint32:
|
|
||||||
rv, err := binarySerializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = rv
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *int64:
|
|
||||||
rv, err := binarySerializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = int64(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *uint64:
|
|
||||||
rv, err := binarySerializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = rv
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *bool:
|
|
||||||
rv, err := binarySerializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if rv == 0x00 {
|
|
||||||
*e = false
|
|
||||||
} else {
|
|
||||||
*e = true
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Unix timestamp encoded as a uint32.
|
|
||||||
case *uint32Time:
|
|
||||||
rv, err := binarySerializer.Uint32(r, binary.LittleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = uint32Time(time.Unix(int64(rv), 0))
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Unix timestamp encoded as an int64.
|
|
||||||
case *int64Time:
|
|
||||||
rv, err := binarySerializer.Uint64(r, binary.LittleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = int64Time(time.Unix(int64(rv), 0))
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header checksum.
|
|
||||||
case *[4]byte:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header command.
|
|
||||||
case *[CommandSize]uint8:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// IP address.
|
|
||||||
case *[16]byte:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *chainhash.Hash:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *ServiceFlag:
|
|
||||||
rv, err := binarySerializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = ServiceFlag(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *InvType:
|
|
||||||
rv, err := binarySerializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = InvType(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *BitcoinNet:
|
|
||||||
rv, err := binarySerializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = BitcoinNet(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *BloomUpdateType:
|
|
||||||
rv, err := binarySerializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = BloomUpdateType(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *RejectCode:
|
|
||||||
rv, err := binarySerializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = RejectCode(rv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall back to the slower binary.Read if a fast path was not available
|
|
||||||
// above.
|
|
||||||
return binary.Read(r, littleEndian, element)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readElements reads multiple items from r. It is equivalent to multiple
|
|
||||||
// calls to readElement.
|
|
||||||
func readElements(r io.Reader, elements ...interface{}) error {
|
|
||||||
for _, element := range elements {
|
|
||||||
err := readElement(r, element)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeElement writes the little endian representation of element to w.
|
|
||||||
func writeElement(w io.Writer, element interface{}) error {
|
|
||||||
// Attempt to write the element based on the concrete type via fast
|
|
||||||
// type assertions first.
|
|
||||||
switch e := element.(type) {
|
|
||||||
case int32:
|
|
||||||
err := binarySerializer.PutUint32(w, littleEndian, uint32(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case uint32:
|
|
||||||
err := binarySerializer.PutUint32(w, littleEndian, e)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case int64:
|
|
||||||
err := binarySerializer.PutUint64(w, littleEndian, uint64(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case uint64:
|
|
||||||
err := binarySerializer.PutUint64(w, littleEndian, e)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case bool:
|
|
||||||
var err error
|
|
||||||
if e {
|
|
||||||
err = binarySerializer.PutUint8(w, 0x01)
|
|
||||||
} else {
|
|
||||||
err = binarySerializer.PutUint8(w, 0x00)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header checksum.
|
|
||||||
case [4]byte:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header command.
|
|
||||||
case [CommandSize]uint8:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// IP address.
|
|
||||||
case [16]byte:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *chainhash.Hash:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case ServiceFlag:
|
|
||||||
err := binarySerializer.PutUint64(w, littleEndian, uint64(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case InvType:
|
|
||||||
err := binarySerializer.PutUint32(w, littleEndian, uint32(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case BitcoinNet:
|
|
||||||
err := binarySerializer.PutUint32(w, littleEndian, uint32(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case BloomUpdateType:
|
|
||||||
err := binarySerializer.PutUint8(w, uint8(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case RejectCode:
|
|
||||||
err := binarySerializer.PutUint8(w, uint8(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall back to the slower binary.Write if a fast path was not available
|
|
||||||
// above.
|
|
||||||
return binary.Write(w, littleEndian, element)
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeElements writes multiple items to w. It is equivalent to multiple
|
|
||||||
// calls to writeElement.
|
|
||||||
func writeElements(w io.Writer, elements ...interface{}) error {
|
|
||||||
for _, element := range elements {
|
|
||||||
err := writeElement(w, element)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarInt reads a variable length integer from r and returns it as a uint64.
|
|
||||||
func ReadVarInt(r io.Reader, pver uint32) (uint64, error) {
|
|
||||||
discriminant, err := binarySerializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var rv uint64
|
|
||||||
switch discriminant {
|
|
||||||
case 0xff:
|
|
||||||
sv, err := binarySerializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv = sv
|
|
||||||
|
|
||||||
// The encoding is not canonical if the value could have been
|
|
||||||
// encoded using fewer bytes.
|
|
||||||
min := uint64(0x100000000)
|
|
||||||
if rv < min {
|
|
||||||
return 0, messageError("ReadVarInt", fmt.Sprintf(
|
|
||||||
errNonCanonicalVarInt, rv, discriminant, min))
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0xfe:
|
|
||||||
sv, err := binarySerializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv = uint64(sv)
|
|
||||||
|
|
||||||
// The encoding is not canonical if the value could have been
|
|
||||||
// encoded using fewer bytes.
|
|
||||||
min := uint64(0x10000)
|
|
||||||
if rv < min {
|
|
||||||
return 0, messageError("ReadVarInt", fmt.Sprintf(
|
|
||||||
errNonCanonicalVarInt, rv, discriminant, min))
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0xfd:
|
|
||||||
sv, err := binarySerializer.Uint16(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv = uint64(sv)
|
|
||||||
|
|
||||||
// The encoding is not canonical if the value could have been
|
|
||||||
// encoded using fewer bytes.
|
|
||||||
min := uint64(0xfd)
|
|
||||||
if rv < min {
|
|
||||||
return 0, messageError("ReadVarInt", fmt.Sprintf(
|
|
||||||
errNonCanonicalVarInt, rv, discriminant, min))
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
rv = uint64(discriminant)
|
|
||||||
}
|
|
||||||
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarInt serializes val to w using a variable number of bytes depending
|
|
||||||
// on its value.
|
|
||||||
func WriteVarInt(w io.Writer, pver uint32, val uint64) error {
|
|
||||||
if val < 0xfd {
|
|
||||||
return binarySerializer.PutUint8(w, uint8(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
if val <= math.MaxUint16 {
|
|
||||||
err := binarySerializer.PutUint8(w, 0xfd)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binarySerializer.PutUint16(w, littleEndian, uint16(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
if val <= math.MaxUint32 {
|
|
||||||
err := binarySerializer.PutUint8(w, 0xfe)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binarySerializer.PutUint32(w, littleEndian, uint32(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
err := binarySerializer.PutUint8(w, 0xff)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binarySerializer.PutUint64(w, littleEndian, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// VarIntSerializeSize returns the number of bytes it would take to serialize
|
|
||||||
// val as a variable length integer.
|
|
||||||
func VarIntSerializeSize(val uint64) int {
|
|
||||||
// The value is small enough to be represented by itself, so it's
|
|
||||||
// just 1 byte.
|
|
||||||
if val < 0xfd {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discriminant 1 byte plus 2 bytes for the uint16.
|
|
||||||
if val <= math.MaxUint16 {
|
|
||||||
return 3
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discriminant 1 byte plus 4 bytes for the uint32.
|
|
||||||
if val <= math.MaxUint32 {
|
|
||||||
return 5
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discriminant 1 byte plus 8 bytes for the uint64.
|
|
||||||
return 9
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarString reads a variable length string from r and returns it as a Go
|
|
||||||
// string. A variable length string is encoded as a variable length integer
|
|
||||||
// containing the length of the string followed by the bytes that represent the
|
|
||||||
// string itself. An error is returned if the length is greater than the
|
|
||||||
// maximum block payload size since it helps protect against memory exhaustion
|
|
||||||
// attacks and forced panics through malformed messages.
|
|
||||||
func ReadVarString(r io.Reader, pver uint32) (string, error) {
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent variable length strings that are larger than the maximum
|
|
||||||
// message size. It would be possible to cause memory exhaustion and
|
|
||||||
// panics without a sane upper bound on this count.
|
|
||||||
if count > MaxMessagePayload {
|
|
||||||
str := fmt.Sprintf("variable length string is too long "+
|
|
||||||
"[count %d, max %d]", count, MaxMessagePayload)
|
|
||||||
return "", messageError("ReadVarString", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, count)
|
|
||||||
_, err = io.ReadFull(r, buf)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(buf), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarString serializes str to w as a variable length integer containing
|
|
||||||
// the length of the string followed by the bytes that represent the string
|
|
||||||
// itself.
|
|
||||||
func WriteVarString(w io.Writer, pver uint32, str string) error {
|
|
||||||
err := WriteVarInt(w, pver, uint64(len(str)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = w.Write([]byte(str))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarBytes reads a variable length byte array. A byte array is encoded
|
|
||||||
// as a varInt containing the length of the array followed by the bytes
|
|
||||||
// themselves. An error is returned if the length is greater than the
|
|
||||||
// passed maxAllowed parameter which helps protect against memory exhaustion
|
|
||||||
// attacks and forced panics through malformed messages. The fieldName
|
|
||||||
// parameter is only used for the error message so it provides more context in
|
|
||||||
// the error.
|
|
||||||
func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
|
|
||||||
fieldName string) ([]byte, error) {
|
|
||||||
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent byte array larger than the max message size. It would
|
|
||||||
// be possible to cause memory exhaustion and panics without a sane
|
|
||||||
// upper bound on this count.
|
|
||||||
if count > uint64(maxAllowed) {
|
|
||||||
str := fmt.Sprintf("%s is larger than the max allowed size "+
|
|
||||||
"[count %d, max %d]", fieldName, count, maxAllowed)
|
|
||||||
return nil, messageError("ReadVarBytes", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
b := make([]byte, count)
|
|
||||||
_, err = io.ReadFull(r, b)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarBytes serializes a variable length byte array to w as a varInt
|
|
||||||
// containing the number of bytes, followed by the bytes themselves.
|
|
||||||
func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
|
|
||||||
slen := uint64(len(bytes))
|
|
||||||
err := WriteVarInt(w, pver, slen)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write(bytes)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// randomUint64 returns a cryptographically random uint64 value. This
|
|
||||||
// unexported version takes a reader primarily to ensure the error paths
|
|
||||||
// can be properly tested by passing a fake reader in the tests.
|
|
||||||
func randomUint64(r io.Reader) (uint64, error) {
|
|
||||||
rv, err := binarySerializer.Uint64(r, bigEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomUint64 returns a cryptographically random uint64 value.
|
|
||||||
func RandomUint64() (uint64, error) {
|
|
||||||
return randomUint64(rand.Reader)
|
|
||||||
}
|
|
@ -1,162 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package wire implements the bitcoin wire protocol.
|
|
||||||
|
|
||||||
For the complete details of the bitcoin protocol, see the official wiki entry
|
|
||||||
at https://en.bitcoin.it/wiki/Protocol_specification. The following only serves
|
|
||||||
as a quick overview to provide information on how to use the package.
|
|
||||||
|
|
||||||
At a high level, this package provides support for marshalling and unmarshalling
|
|
||||||
supported bitcoin messages to and from the wire. This package does not deal
|
|
||||||
with the specifics of message handling such as what to do when a message is
|
|
||||||
received. This provides the caller with a high level of flexibility.
|
|
||||||
|
|
||||||
Bitcoin Message Overview
|
|
||||||
|
|
||||||
The bitcoin protocol consists of exchanging messages between peers. Each
|
|
||||||
message is preceded by a header which identifies information about it such as
|
|
||||||
which bitcoin network it is a part of, its type, how big it is, and a checksum
|
|
||||||
to verify validity. All encoding and decoding of message headers is handled by
|
|
||||||
this package.
|
|
||||||
|
|
||||||
To accomplish this, there is a generic interface for bitcoin messages named
|
|
||||||
Message which allows messages of any type to be read, written, or passed around
|
|
||||||
through channels, functions, etc. In addition, concrete implementations of most
|
|
||||||
of the currently supported bitcoin messages are provided. For these supported
|
|
||||||
messages, all of the details of marshalling and unmarshalling to and from the
|
|
||||||
wire using bitcoin encoding are handled so the caller doesn't have to concern
|
|
||||||
themselves with the specifics.
|
|
||||||
|
|
||||||
Message Interaction
|
|
||||||
|
|
||||||
The following provides a quick summary of how the bitcoin messages are intended
|
|
||||||
to interact with one another. As stated above, these interactions are not
|
|
||||||
directly handled by this package. For more in-depth details about the
|
|
||||||
appropriate interactions, see the official bitcoin protocol wiki entry at
|
|
||||||
https://en.bitcoin.it/wiki/Protocol_specification.
|
|
||||||
|
|
||||||
The initial handshake consists of two peers sending each other a version message
|
|
||||||
(MsgVersion) followed by responding with a verack message (MsgVerAck). Both
|
|
||||||
peers use the information in the version message (MsgVersion) to negotiate
|
|
||||||
things such as protocol version and supported services with each other. Once
|
|
||||||
the initial handshake is complete, the following chart indicates message
|
|
||||||
interactions in no particular order.
|
|
||||||
|
|
||||||
Peer A Sends Peer B Responds
|
|
||||||
----------------------------------------------------------------------------
|
|
||||||
getaddr message (MsgGetAddr) addr message (MsgAddr)
|
|
||||||
getblocks message (MsgGetBlocks) inv message (MsgInv)
|
|
||||||
inv message (MsgInv) getdata message (MsgGetData)
|
|
||||||
getdata message (MsgGetData) block message (MsgBlock) -or-
|
|
||||||
tx message (MsgTx) -or-
|
|
||||||
notfound message (MsgNotFound)
|
|
||||||
getheaders message (MsgGetHeaders) headers message (MsgHeaders)
|
|
||||||
ping message (MsgPing) pong message (MsgHeaders)* -or-
|
|
||||||
(none -- Ability to send message is enough)
|
|
||||||
|
|
||||||
NOTES:
|
|
||||||
* The pong message was not added until later protocol versions as defined
|
|
||||||
in BIP0031. The BIP0031Version constant can be used to detect a recent
|
|
||||||
enough protocol version for this purpose (version > BIP0031Version).
|
|
||||||
|
|
||||||
Common Parameters
|
|
||||||
|
|
||||||
There are several common parameters that arise when using this package to read
|
|
||||||
and write bitcoin messages. The following sections provide a quick overview of
|
|
||||||
these parameters so the next sections can build on them.
|
|
||||||
|
|
||||||
Protocol Version
|
|
||||||
|
|
||||||
The protocol version should be negotiated with the remote peer at a higher
|
|
||||||
level than this package via the version (MsgVersion) message exchange, however,
|
|
||||||
this package provides the wire.ProtocolVersion constant which indicates the
|
|
||||||
latest protocol version this package supports and is typically the value to use
|
|
||||||
for all outbound connections before a potentially lower protocol version is
|
|
||||||
negotiated.
|
|
||||||
|
|
||||||
Bitcoin Network
|
|
||||||
|
|
||||||
The bitcoin network is a magic number which is used to identify the start of a
|
|
||||||
message and which bitcoin network the message applies to. This package provides
|
|
||||||
the following constants:
|
|
||||||
|
|
||||||
wire.MainNet
|
|
||||||
wire.TestNet (Regression test network)
|
|
||||||
wire.TestNet3 (Test network version 3)
|
|
||||||
wire.SimNet (Simulation test network)
|
|
||||||
|
|
||||||
Determining Message Type
|
|
||||||
|
|
||||||
As discussed in the bitcoin message overview section, this package reads
|
|
||||||
and writes bitcoin messages using a generic interface named Message. In
|
|
||||||
order to determine the actual concrete type of the message, use a type
|
|
||||||
switch or type assertion. An example of a type switch follows:
|
|
||||||
|
|
||||||
// Assumes msg is already a valid concrete message such as one created
|
|
||||||
// via NewMsgVersion or read via ReadMessage.
|
|
||||||
switch msg := msg.(type) {
|
|
||||||
case *wire.MsgVersion:
|
|
||||||
// The message is a pointer to a MsgVersion struct.
|
|
||||||
fmt.Printf("Protocol version: %v", msg.ProtocolVersion)
|
|
||||||
case *wire.MsgBlock:
|
|
||||||
// The message is a pointer to a MsgBlock struct.
|
|
||||||
fmt.Printf("Number of tx in block: %v", msg.Header.TxnCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
Reading Messages
|
|
||||||
|
|
||||||
In order to unmarshall bitcoin messages from the wire, use the ReadMessage
|
|
||||||
function. It accepts any io.Reader, but typically this will be a net.Conn to
|
|
||||||
a remote node running a bitcoin peer. Example syntax is:
|
|
||||||
|
|
||||||
// Reads and validates the next bitcoin message from conn using the
|
|
||||||
// protocol version pver and the bitcoin network btcnet. The returns
|
|
||||||
// are a wire.Message, a []byte which contains the unmarshalled
|
|
||||||
// raw payload, and a possible error.
|
|
||||||
msg, rawPayload, err := wire.ReadMessage(conn, pver, btcnet)
|
|
||||||
if err != nil {
|
|
||||||
// Log and handle the error
|
|
||||||
}
|
|
||||||
|
|
||||||
Writing Messages
|
|
||||||
|
|
||||||
In order to marshall bitcoin messages to the wire, use the WriteMessage
|
|
||||||
function. It accepts any io.Writer, but typically this will be a net.Conn to
|
|
||||||
a remote node running a bitcoin peer. Example syntax to request addresses
|
|
||||||
from a remote peer is:
|
|
||||||
|
|
||||||
// Create a new getaddr bitcoin message.
|
|
||||||
msg := wire.NewMsgGetAddr()
|
|
||||||
|
|
||||||
// Writes a bitcoin message msg to conn using the protocol version
|
|
||||||
// pver, and the bitcoin network btcnet. The return is a possible
|
|
||||||
// error.
|
|
||||||
err := wire.WriteMessage(conn, msg, pver, btcnet)
|
|
||||||
if err != nil {
|
|
||||||
// Log and handle the error
|
|
||||||
}
|
|
||||||
|
|
||||||
Errors
|
|
||||||
|
|
||||||
Errors returned by this package are either the raw errors provided by underlying
|
|
||||||
calls to read/write from streams such as io.EOF, io.ErrUnexpectedEOF, and
|
|
||||||
io.ErrShortWrite, or of type wire.MessageError. This allows the caller to
|
|
||||||
differentiate between general IO errors and malformed messages through type
|
|
||||||
assertions.
|
|
||||||
|
|
||||||
Bitcoin Improvement Proposals
|
|
||||||
|
|
||||||
This package includes spec changes outlined by the following BIPs:
|
|
||||||
|
|
||||||
BIP0014 (https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki)
|
|
||||||
BIP0031 (https://github.com/bitcoin/bips/blob/master/bip-0031.mediawiki)
|
|
||||||
BIP0035 (https://github.com/bitcoin/bips/blob/master/bip-0035.mediawiki)
|
|
||||||
BIP0037 (https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki)
|
|
||||||
BIP0111 (https://github.com/bitcoin/bips/blob/master/bip-0111.mediawiki)
|
|
||||||
BIP0130 (https://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki)
|
|
||||||
BIP0133 (https://github.com/bitcoin/bips/blob/master/bip-0133.mediawiki)
|
|
||||||
*/
|
|
||||||
package wire
|
|
@ -1,34 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MessageError describes an issue with a message.
|
|
||||||
// An example of some potential issues are messages from the wrong bitcoin
|
|
||||||
// network, invalid commands, mismatched checksums, and exceeding max payloads.
|
|
||||||
//
|
|
||||||
// This provides a mechanism for the caller to type assert the error to
|
|
||||||
// differentiate between general io errors such as io.EOF and issues that
|
|
||||||
// resulted from malformed messages.
|
|
||||||
type MessageError struct {
|
|
||||||
Func string // Function name
|
|
||||||
Description string // Human readable description of the issue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error satisfies the error interface and prints human-readable errors.
|
|
||||||
func (e *MessageError) Error() string {
|
|
||||||
if e.Func != "" {
|
|
||||||
return fmt.Sprintf("%v: %v", e.Func, e.Description)
|
|
||||||
}
|
|
||||||
return e.Description
|
|
||||||
}
|
|
||||||
|
|
||||||
// messageError creates an error for the given function and description.
|
|
||||||
func messageError(f string, desc string) *MessageError {
|
|
||||||
return &MessageError{Func: f, Description: desc}
|
|
||||||
}
|
|
@ -1,86 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// MaxInvPerMsg is the maximum number of inventory vectors that can be in a
|
|
||||||
// single bitcoin inv message.
|
|
||||||
MaxInvPerMsg = 50000
|
|
||||||
|
|
||||||
// Maximum payload size for an inventory vector.
|
|
||||||
maxInvVectPayload = 4 + chainhash.HashSize
|
|
||||||
|
|
||||||
// InvWitnessFlag denotes that the inventory vector type is requesting,
|
|
||||||
// or sending a version which includes witness data.
|
|
||||||
InvWitnessFlag = 1 << 30
|
|
||||||
)
|
|
||||||
|
|
||||||
// InvType represents the allowed types of inventory vectors. See InvVect.
|
|
||||||
type InvType uint32
|
|
||||||
|
|
||||||
// These constants define the various supported inventory vector types.
|
|
||||||
const (
|
|
||||||
InvTypeError InvType = 0
|
|
||||||
InvTypeTx InvType = 1
|
|
||||||
InvTypeBlock InvType = 2
|
|
||||||
InvTypeFilteredBlock InvType = 3
|
|
||||||
InvTypeWitnessBlock InvType = InvTypeBlock | InvWitnessFlag
|
|
||||||
InvTypeWitnessTx InvType = InvTypeTx | InvWitnessFlag
|
|
||||||
InvTypeFilteredWitnessBlock InvType = InvTypeFilteredBlock | InvWitnessFlag
|
|
||||||
)
|
|
||||||
|
|
||||||
// Map of service flags back to their constant names for pretty printing.
|
|
||||||
var ivStrings = map[InvType]string{
|
|
||||||
InvTypeError: "ERROR",
|
|
||||||
InvTypeTx: "MSG_TX",
|
|
||||||
InvTypeBlock: "MSG_BLOCK",
|
|
||||||
InvTypeFilteredBlock: "MSG_FILTERED_BLOCK",
|
|
||||||
InvTypeWitnessBlock: "MSG_WITNESS_BLOCK",
|
|
||||||
InvTypeWitnessTx: "MSG_WITNESS_TX",
|
|
||||||
InvTypeFilteredWitnessBlock: "MSG_FILTERED_WITNESS_BLOCK",
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the InvType in human-readable form.
|
|
||||||
func (invtype InvType) String() string {
|
|
||||||
if s, ok := ivStrings[invtype]; ok {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("Unknown InvType (%d)", uint32(invtype))
|
|
||||||
}
|
|
||||||
|
|
||||||
// InvVect defines a bitcoin inventory vector which is used to describe data,
|
|
||||||
// as specified by the Type field, that a peer wants, has, or does not have to
|
|
||||||
// another peer.
|
|
||||||
type InvVect struct {
|
|
||||||
Type InvType // Type of data
|
|
||||||
Hash chainhash.Hash // Hash of the data
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewInvVect returns a new InvVect using the provided type and hash.
|
|
||||||
func NewInvVect(typ InvType, hash *chainhash.Hash) *InvVect {
|
|
||||||
return &InvVect{
|
|
||||||
Type: typ,
|
|
||||||
Hash: *hash,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// readInvVect reads an encoded InvVect from r depending on the protocol
|
|
||||||
// version.
|
|
||||||
func readInvVect(r io.Reader, pver uint32, iv *InvVect) error {
|
|
||||||
return readElements(r, &iv.Type, &iv.Hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeInvVect serializes an InvVect to w depending on the protocol version.
|
|
||||||
func writeInvVect(w io.Writer, pver uint32, iv *InvVect) error {
|
|
||||||
return writeElements(w, iv.Type, &iv.Hash)
|
|
||||||
}
|
|
@ -1,436 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MessageHeaderSize is the number of bytes in a bitcoin message header.
|
|
||||||
// Bitcoin network (magic) 4 bytes + command 12 bytes + payload length 4 bytes +
|
|
||||||
// checksum 4 bytes.
|
|
||||||
const MessageHeaderSize = 24
|
|
||||||
|
|
||||||
// CommandSize is the fixed size of all commands in the common bitcoin message
|
|
||||||
// header. Shorter commands must be zero padded.
|
|
||||||
const CommandSize = 12
|
|
||||||
|
|
||||||
// MaxMessagePayload is the maximum bytes a message can be regardless of other
|
|
||||||
// individual limits imposed by messages themselves.
|
|
||||||
const MaxMessagePayload = (1024 * 1024 * 32) // 32MB
|
|
||||||
|
|
||||||
// Commands used in bitcoin message headers which describe the type of message.
|
|
||||||
const (
|
|
||||||
CmdVersion = "version"
|
|
||||||
CmdVerAck = "verack"
|
|
||||||
CmdGetAddr = "getaddr"
|
|
||||||
CmdAddr = "addr"
|
|
||||||
CmdGetBlocks = "getblocks"
|
|
||||||
CmdInv = "inv"
|
|
||||||
CmdGetData = "getdata"
|
|
||||||
CmdNotFound = "notfound"
|
|
||||||
CmdBlock = "block"
|
|
||||||
CmdTx = "tx"
|
|
||||||
CmdGetHeaders = "getheaders"
|
|
||||||
CmdHeaders = "headers"
|
|
||||||
CmdPing = "ping"
|
|
||||||
CmdPong = "pong"
|
|
||||||
CmdAlert = "alert"
|
|
||||||
CmdMemPool = "mempool"
|
|
||||||
CmdFilterAdd = "filteradd"
|
|
||||||
CmdFilterClear = "filterclear"
|
|
||||||
CmdFilterLoad = "filterload"
|
|
||||||
CmdMerkleBlock = "merkleblock"
|
|
||||||
CmdReject = "reject"
|
|
||||||
CmdSendHeaders = "sendheaders"
|
|
||||||
CmdFeeFilter = "feefilter"
|
|
||||||
CmdGetCFilters = "getcfilters"
|
|
||||||
CmdGetCFHeaders = "getcfheaders"
|
|
||||||
CmdGetCFCheckpt = "getcfcheckpt"
|
|
||||||
CmdCFilter = "cfilter"
|
|
||||||
CmdCFHeaders = "cfheaders"
|
|
||||||
CmdCFCheckpt = "cfcheckpt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MessageEncoding represents the wire message encoding format to be used.
|
|
||||||
type MessageEncoding uint32
|
|
||||||
|
|
||||||
const (
|
|
||||||
// BaseEncoding encodes all messages in the default format specified
|
|
||||||
// for the Bitcoin wire protocol.
|
|
||||||
BaseEncoding MessageEncoding = 1 << iota
|
|
||||||
|
|
||||||
// WitnessEncoding encodes all messages other than transaction messages
|
|
||||||
// using the default Bitcoin wire protocol specification. For transaction
|
|
||||||
// messages, the new encoding format detailed in BIP0144 will be used.
|
|
||||||
WitnessEncoding
|
|
||||||
)
|
|
||||||
|
|
||||||
// LatestEncoding is the most recently specified encoding for the Bitcoin wire
|
|
||||||
// protocol.
|
|
||||||
var LatestEncoding = WitnessEncoding
|
|
||||||
|
|
||||||
// Message is an interface that describes a bitcoin message. A type that
|
|
||||||
// implements Message has complete control over the representation of its data
|
|
||||||
// and may therefore contain additional or fewer fields than those which
|
|
||||||
// are used directly in the protocol encoded message.
|
|
||||||
type Message interface {
|
|
||||||
BtcDecode(io.Reader, uint32, MessageEncoding) error
|
|
||||||
BtcEncode(io.Writer, uint32, MessageEncoding) error
|
|
||||||
Command() string
|
|
||||||
MaxPayloadLength(uint32) uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// makeEmptyMessage creates a message of the appropriate concrete type based
|
|
||||||
// on the command.
|
|
||||||
func makeEmptyMessage(command string) (Message, error) {
|
|
||||||
var msg Message
|
|
||||||
switch command {
|
|
||||||
case CmdVersion:
|
|
||||||
msg = &MsgVersion{}
|
|
||||||
|
|
||||||
case CmdVerAck:
|
|
||||||
msg = &MsgVerAck{}
|
|
||||||
|
|
||||||
case CmdGetAddr:
|
|
||||||
msg = &MsgGetAddr{}
|
|
||||||
|
|
||||||
case CmdAddr:
|
|
||||||
msg = &MsgAddr{}
|
|
||||||
|
|
||||||
case CmdGetBlocks:
|
|
||||||
msg = &MsgGetBlocks{}
|
|
||||||
|
|
||||||
case CmdBlock:
|
|
||||||
msg = &MsgBlock{}
|
|
||||||
|
|
||||||
case CmdInv:
|
|
||||||
msg = &MsgInv{}
|
|
||||||
|
|
||||||
case CmdGetData:
|
|
||||||
msg = &MsgGetData{}
|
|
||||||
|
|
||||||
case CmdNotFound:
|
|
||||||
msg = &MsgNotFound{}
|
|
||||||
|
|
||||||
case CmdTx:
|
|
||||||
msg = &MsgTx{}
|
|
||||||
|
|
||||||
case CmdPing:
|
|
||||||
msg = &MsgPing{}
|
|
||||||
|
|
||||||
case CmdPong:
|
|
||||||
msg = &MsgPong{}
|
|
||||||
|
|
||||||
case CmdGetHeaders:
|
|
||||||
msg = &MsgGetHeaders{}
|
|
||||||
|
|
||||||
case CmdHeaders:
|
|
||||||
msg = &MsgHeaders{}
|
|
||||||
|
|
||||||
case CmdAlert:
|
|
||||||
msg = &MsgAlert{}
|
|
||||||
|
|
||||||
case CmdMemPool:
|
|
||||||
msg = &MsgMemPool{}
|
|
||||||
|
|
||||||
case CmdFilterAdd:
|
|
||||||
msg = &MsgFilterAdd{}
|
|
||||||
|
|
||||||
case CmdFilterClear:
|
|
||||||
msg = &MsgFilterClear{}
|
|
||||||
|
|
||||||
case CmdFilterLoad:
|
|
||||||
msg = &MsgFilterLoad{}
|
|
||||||
|
|
||||||
case CmdMerkleBlock:
|
|
||||||
msg = &MsgMerkleBlock{}
|
|
||||||
|
|
||||||
case CmdReject:
|
|
||||||
msg = &MsgReject{}
|
|
||||||
|
|
||||||
case CmdSendHeaders:
|
|
||||||
msg = &MsgSendHeaders{}
|
|
||||||
|
|
||||||
case CmdFeeFilter:
|
|
||||||
msg = &MsgFeeFilter{}
|
|
||||||
|
|
||||||
case CmdGetCFilters:
|
|
||||||
msg = &MsgGetCFilters{}
|
|
||||||
|
|
||||||
case CmdGetCFHeaders:
|
|
||||||
msg = &MsgGetCFHeaders{}
|
|
||||||
|
|
||||||
case CmdGetCFCheckpt:
|
|
||||||
msg = &MsgGetCFCheckpt{}
|
|
||||||
|
|
||||||
case CmdCFilter:
|
|
||||||
msg = &MsgCFilter{}
|
|
||||||
|
|
||||||
case CmdCFHeaders:
|
|
||||||
msg = &MsgCFHeaders{}
|
|
||||||
|
|
||||||
case CmdCFCheckpt:
|
|
||||||
msg = &MsgCFCheckpt{}
|
|
||||||
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unhandled command [%s]", command)
|
|
||||||
}
|
|
||||||
return msg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// messageHeader defines the header structure for all bitcoin protocol messages.
|
|
||||||
type messageHeader struct {
|
|
||||||
magic BitcoinNet // 4 bytes
|
|
||||||
command string // 12 bytes
|
|
||||||
length uint32 // 4 bytes
|
|
||||||
checksum [4]byte // 4 bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
// readMessageHeader reads a bitcoin message header from r.
|
|
||||||
func readMessageHeader(r io.Reader) (int, *messageHeader, error) {
|
|
||||||
// Since readElements doesn't return the amount of bytes read, attempt
|
|
||||||
// to read the entire header into a buffer first in case there is a
|
|
||||||
// short read so the proper amount of read bytes are known. This works
|
|
||||||
// since the header is a fixed size.
|
|
||||||
var headerBytes [MessageHeaderSize]byte
|
|
||||||
n, err := io.ReadFull(r, headerBytes[:])
|
|
||||||
if err != nil {
|
|
||||||
return n, nil, err
|
|
||||||
}
|
|
||||||
hr := bytes.NewReader(headerBytes[:])
|
|
||||||
|
|
||||||
// Create and populate a messageHeader struct from the raw header bytes.
|
|
||||||
hdr := messageHeader{}
|
|
||||||
var command [CommandSize]byte
|
|
||||||
readElements(hr, &hdr.magic, &command, &hdr.length, &hdr.checksum)
|
|
||||||
|
|
||||||
// Strip trailing zeros from command string.
|
|
||||||
hdr.command = string(bytes.TrimRight(command[:], string(0)))
|
|
||||||
|
|
||||||
return n, &hdr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// discardInput reads n bytes from reader r in chunks and discards the read
|
|
||||||
// bytes. This is used to skip payloads when various errors occur and helps
|
|
||||||
// prevent rogue nodes from causing massive memory allocation through forging
|
|
||||||
// header length.
|
|
||||||
func discardInput(r io.Reader, n uint32) {
|
|
||||||
maxSize := uint32(10 * 1024) // 10k at a time
|
|
||||||
numReads := n / maxSize
|
|
||||||
bytesRemaining := n % maxSize
|
|
||||||
if n > 0 {
|
|
||||||
buf := make([]byte, maxSize)
|
|
||||||
for i := uint32(0); i < numReads; i++ {
|
|
||||||
io.ReadFull(r, buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if bytesRemaining > 0 {
|
|
||||||
buf := make([]byte, bytesRemaining)
|
|
||||||
io.ReadFull(r, buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteMessageN writes a bitcoin Message to w including the necessary header
|
|
||||||
// information and returns the number of bytes written. This function is the
|
|
||||||
// same as WriteMessage except it also returns the number of bytes written.
|
|
||||||
func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (int, error) {
|
|
||||||
return WriteMessageWithEncodingN(w, msg, pver, btcnet, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteMessage writes a bitcoin Message to w including the necessary header
|
|
||||||
// information. This function is the same as WriteMessageN except it doesn't
|
|
||||||
// doesn't return the number of bytes written. This function is mainly provided
|
|
||||||
// for backwards compatibility with the original API, but it's also useful for
|
|
||||||
// callers that don't care about byte counts.
|
|
||||||
func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) error {
|
|
||||||
_, err := WriteMessageN(w, msg, pver, btcnet)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteMessageWithEncodingN writes a bitcoin Message to w including the
|
|
||||||
// necessary header information and returns the number of bytes written.
|
|
||||||
// This function is the same as WriteMessageN except it also allows the caller
|
|
||||||
// to specify the message encoding format to be used when serializing wire
|
|
||||||
// messages.
|
|
||||||
func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
|
|
||||||
btcnet BitcoinNet, encoding MessageEncoding) (int, error) {
|
|
||||||
|
|
||||||
totalBytes := 0
|
|
||||||
|
|
||||||
// Enforce max command size.
|
|
||||||
var command [CommandSize]byte
|
|
||||||
cmd := msg.Command()
|
|
||||||
if len(cmd) > CommandSize {
|
|
||||||
str := fmt.Sprintf("command [%s] is too long [max %v]",
|
|
||||||
cmd, CommandSize)
|
|
||||||
return totalBytes, messageError("WriteMessage", str)
|
|
||||||
}
|
|
||||||
copy(command[:], []byte(cmd))
|
|
||||||
|
|
||||||
// Encode the message payload.
|
|
||||||
var bw bytes.Buffer
|
|
||||||
err := msg.BtcEncode(&bw, pver, encoding)
|
|
||||||
if err != nil {
|
|
||||||
return totalBytes, err
|
|
||||||
}
|
|
||||||
payload := bw.Bytes()
|
|
||||||
lenp := len(payload)
|
|
||||||
|
|
||||||
// Enforce maximum overall message payload.
|
|
||||||
if lenp > MaxMessagePayload {
|
|
||||||
str := fmt.Sprintf("message payload is too large - encoded "+
|
|
||||||
"%d bytes, but maximum message payload is %d bytes",
|
|
||||||
lenp, MaxMessagePayload)
|
|
||||||
return totalBytes, messageError("WriteMessage", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enforce maximum message payload based on the message type.
|
|
||||||
mpl := msg.MaxPayloadLength(pver)
|
|
||||||
if uint32(lenp) > mpl {
|
|
||||||
str := fmt.Sprintf("message payload is too large - encoded "+
|
|
||||||
"%d bytes, but maximum message payload size for "+
|
|
||||||
"messages of type [%s] is %d.", lenp, cmd, mpl)
|
|
||||||
return totalBytes, messageError("WriteMessage", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create header for the message.
|
|
||||||
hdr := messageHeader{}
|
|
||||||
hdr.magic = btcnet
|
|
||||||
hdr.command = cmd
|
|
||||||
hdr.length = uint32(lenp)
|
|
||||||
copy(hdr.checksum[:], chainhash.DoubleHashB(payload)[0:4])
|
|
||||||
|
|
||||||
// Encode the header for the message. This is done to a buffer
|
|
||||||
// rather than directly to the writer since writeElements doesn't
|
|
||||||
// return the number of bytes written.
|
|
||||||
hw := bytes.NewBuffer(make([]byte, 0, MessageHeaderSize))
|
|
||||||
writeElements(hw, hdr.magic, command, hdr.length, hdr.checksum)
|
|
||||||
|
|
||||||
// Write header.
|
|
||||||
n, err := w.Write(hw.Bytes())
|
|
||||||
totalBytes += n
|
|
||||||
if err != nil {
|
|
||||||
return totalBytes, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write payload.
|
|
||||||
n, err = w.Write(payload)
|
|
||||||
totalBytes += n
|
|
||||||
return totalBytes, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadMessageWithEncodingN reads, validates, and parses the next bitcoin Message
|
|
||||||
// from r for the provided protocol version and bitcoin network. It returns the
|
|
||||||
// number of bytes read in addition to the parsed Message and raw bytes which
|
|
||||||
// comprise the message. This function is the same as ReadMessageN except it
|
|
||||||
// allows the caller to specify which message encoding is to to consult when
|
|
||||||
// decoding wire messages.
|
|
||||||
func ReadMessageWithEncodingN(r io.Reader, pver uint32, btcnet BitcoinNet,
|
|
||||||
enc MessageEncoding) (int, Message, []byte, error) {
|
|
||||||
|
|
||||||
totalBytes := 0
|
|
||||||
n, hdr, err := readMessageHeader(r)
|
|
||||||
totalBytes += n
|
|
||||||
if err != nil {
|
|
||||||
return totalBytes, nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enforce maximum message payload.
|
|
||||||
if hdr.length > MaxMessagePayload {
|
|
||||||
str := fmt.Sprintf("message payload is too large - header "+
|
|
||||||
"indicates %d bytes, but max message payload is %d "+
|
|
||||||
"bytes.", hdr.length, MaxMessagePayload)
|
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for messages from the wrong bitcoin network.
|
|
||||||
if hdr.magic != btcnet {
|
|
||||||
discardInput(r, hdr.length)
|
|
||||||
str := fmt.Sprintf("message from other network [%v]", hdr.magic)
|
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for malformed commands.
|
|
||||||
command := hdr.command
|
|
||||||
if !utf8.ValidString(command) {
|
|
||||||
discardInput(r, hdr.length)
|
|
||||||
str := fmt.Sprintf("invalid command %v", []byte(command))
|
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create struct of appropriate message type based on the command.
|
|
||||||
msg, err := makeEmptyMessage(command)
|
|
||||||
if err != nil {
|
|
||||||
discardInput(r, hdr.length)
|
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage",
|
|
||||||
err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for maximum length based on the message type as a malicious client
|
|
||||||
// could otherwise create a well-formed header and set the length to max
|
|
||||||
// numbers in order to exhaust the machine's memory.
|
|
||||||
mpl := msg.MaxPayloadLength(pver)
|
|
||||||
if hdr.length > mpl {
|
|
||||||
discardInput(r, hdr.length)
|
|
||||||
str := fmt.Sprintf("payload exceeds max length - header "+
|
|
||||||
"indicates %v bytes, but max payload size for "+
|
|
||||||
"messages of type [%v] is %v.", hdr.length, command, mpl)
|
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read payload.
|
|
||||||
payload := make([]byte, hdr.length)
|
|
||||||
n, err = io.ReadFull(r, payload)
|
|
||||||
totalBytes += n
|
|
||||||
if err != nil {
|
|
||||||
return totalBytes, nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test checksum.
|
|
||||||
checksum := chainhash.DoubleHashB(payload)[0:4]
|
|
||||||
if !bytes.Equal(checksum[:], hdr.checksum[:]) {
|
|
||||||
str := fmt.Sprintf("payload checksum failed - header "+
|
|
||||||
"indicates %v, but actual checksum is %v.",
|
|
||||||
hdr.checksum, checksum)
|
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshal message. NOTE: This must be a *bytes.Buffer since the
|
|
||||||
// MsgVersion BtcDecode function requires it.
|
|
||||||
pr := bytes.NewBuffer(payload)
|
|
||||||
err = msg.BtcDecode(pr, pver, enc)
|
|
||||||
if err != nil {
|
|
||||||
return totalBytes, nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return totalBytes, msg, payload, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadMessageN reads, validates, and parses the next bitcoin Message from r for
|
|
||||||
// the provided protocol version and bitcoin network. It returns the number of
|
|
||||||
// bytes read in addition to the parsed Message and raw bytes which comprise the
|
|
||||||
// message. This function is the same as ReadMessage except it also returns the
|
|
||||||
// number of bytes read.
|
|
||||||
func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []byte, error) {
|
|
||||||
return ReadMessageWithEncodingN(r, pver, btcnet, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadMessage reads, validates, and parses the next bitcoin Message from r for
|
|
||||||
// the provided protocol version and bitcoin network. It returns the parsed
|
|
||||||
// Message and raw bytes which comprise the message. This function only differs
|
|
||||||
// from ReadMessageN in that it doesn't return the number of bytes read. This
|
|
||||||
// function is mainly provided for backwards compatibility with the original
|
|
||||||
// API, but it's also useful for callers that don't care about byte counts.
|
|
||||||
func ReadMessage(r io.Reader, pver uint32, btcnet BitcoinNet) (Message, []byte, error) {
|
|
||||||
_, msg, buf, err := ReadMessageN(r, pver, btcnet)
|
|
||||||
return msg, buf, err
|
|
||||||
}
|
|
@ -1,143 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxAddrPerMsg is the maximum number of addresses that can be in a single
|
|
||||||
// bitcoin addr message (MsgAddr).
|
|
||||||
const MaxAddrPerMsg = 1000
|
|
||||||
|
|
||||||
// MsgAddr implements the Message interface and represents a bitcoin
|
|
||||||
// addr message. It is used to provide a list of known active peers on the
|
|
||||||
// network. An active peer is considered one that has transmitted a message
|
|
||||||
// within the last 3 hours. Nodes which have not transmitted in that time
|
|
||||||
// frame should be forgotten. Each message is limited to a maximum number of
|
|
||||||
// addresses, which is currently 1000. As a result, multiple messages must
|
|
||||||
// be used to relay the full list.
|
|
||||||
//
|
|
||||||
// Use the AddAddress function to build up the list of known addresses when
|
|
||||||
// sending an addr message to another peer.
|
|
||||||
type MsgAddr struct {
|
|
||||||
AddrList []*NetAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddAddress adds a known active peer to the message.
|
|
||||||
func (msg *MsgAddr) AddAddress(na *NetAddress) error {
|
|
||||||
if len(msg.AddrList)+1 > MaxAddrPerMsg {
|
|
||||||
str := fmt.Sprintf("too many addresses in message [max %v]",
|
|
||||||
MaxAddrPerMsg)
|
|
||||||
return messageError("MsgAddr.AddAddress", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.AddrList = append(msg.AddrList, na)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddAddresses adds multiple known active peers to the message.
|
|
||||||
func (msg *MsgAddr) AddAddresses(netAddrs ...*NetAddress) error {
|
|
||||||
for _, na := range netAddrs {
|
|
||||||
err := msg.AddAddress(na)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearAddresses removes all addresses from the message.
|
|
||||||
func (msg *MsgAddr) ClearAddresses() {
|
|
||||||
msg.AddrList = []*NetAddress{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgAddr) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max addresses per message.
|
|
||||||
if count > MaxAddrPerMsg {
|
|
||||||
str := fmt.Sprintf("too many addresses for message "+
|
|
||||||
"[count %v, max %v]", count, MaxAddrPerMsg)
|
|
||||||
return messageError("MsgAddr.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
addrList := make([]NetAddress, count)
|
|
||||||
msg.AddrList = make([]*NetAddress, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
na := &addrList[i]
|
|
||||||
err := readNetAddress(r, pver, na, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddAddress(na)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgAddr) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// Protocol versions before MultipleAddressVersion only allowed 1 address
|
|
||||||
// per message.
|
|
||||||
count := len(msg.AddrList)
|
|
||||||
if pver < MultipleAddressVersion && count > 1 {
|
|
||||||
str := fmt.Sprintf("too many addresses for message of "+
|
|
||||||
"protocol version %v [count %v, max 1]", pver, count)
|
|
||||||
return messageError("MsgAddr.BtcEncode", str)
|
|
||||||
|
|
||||||
}
|
|
||||||
if count > MaxAddrPerMsg {
|
|
||||||
str := fmt.Sprintf("too many addresses for message "+
|
|
||||||
"[count %v, max %v]", count, MaxAddrPerMsg)
|
|
||||||
return messageError("MsgAddr.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, na := range msg.AddrList {
|
|
||||||
err = writeNetAddress(w, pver, na, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgAddr) Command() string {
|
|
||||||
return CmdAddr
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgAddr) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
if pver < MultipleAddressVersion {
|
|
||||||
// Num addresses (varInt) + a single net addresses.
|
|
||||||
return MaxVarIntPayload + maxNetAddressPayload(pver)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Num addresses (varInt) + max allowed addresses.
|
|
||||||
return MaxVarIntPayload + (MaxAddrPerMsg * maxNetAddressPayload(pver))
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgAddr returns a new bitcoin addr message that conforms to the
|
|
||||||
// Message interface. See MsgAddr for details.
|
|
||||||
func NewMsgAddr() *MsgAddr {
|
|
||||||
return &MsgAddr{
|
|
||||||
AddrList: make([]*NetAddress, 0, MaxAddrPerMsg),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,407 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgAlert contains a payload and a signature:
|
|
||||||
//
|
|
||||||
// ===============================================
|
|
||||||
// | Field | Data Type | Size |
|
|
||||||
// ===============================================
|
|
||||||
// | payload | []uchar | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | signature | []uchar | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
//
|
|
||||||
// Here payload is an Alert serialized into a byte array to ensure that
|
|
||||||
// versions using incompatible alert formats can still relay
|
|
||||||
// alerts among one another.
|
|
||||||
//
|
|
||||||
// An Alert is the payload deserialized as follows:
|
|
||||||
//
|
|
||||||
// ===============================================
|
|
||||||
// | Field | Data Type | Size |
|
|
||||||
// ===============================================
|
|
||||||
// | Version | int32 | 4 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | RelayUntil | int64 | 8 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | Expiration | int64 | 8 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | ID | int32 | 4 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | Cancel | int32 | 4 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | SetCancel | set<int32> | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | MinVer | int32 | 4 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | MaxVer | int32 | 4 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | SetSubVer | set<string> | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | Priority | int32 | 4 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | Comment | string | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | StatusBar | string | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | Reserved | string | ? |
|
|
||||||
// -----------------------------------------------
|
|
||||||
// | Total (Fixed) | 45 |
|
|
||||||
// -----------------------------------------------
|
|
||||||
//
|
|
||||||
// NOTE:
|
|
||||||
// * string is a VarString i.e VarInt length followed by the string itself
|
|
||||||
// * set<string> is a VarInt followed by as many number of strings
|
|
||||||
// * set<int32> is a VarInt followed by as many number of ints
|
|
||||||
// * fixedAlertSize = 40 + 5*min(VarInt) = 40 + 5*1 = 45
|
|
||||||
//
|
|
||||||
// Now we can define bounds on Alert size, SetCancel and SetSubVer
|
|
||||||
|
|
||||||
// Fixed size of the alert payload
|
|
||||||
const fixedAlertSize = 45
|
|
||||||
|
|
||||||
// maxSignatureSize is the max size of an ECDSA signature.
|
|
||||||
// NOTE: Since this size is fixed and < 255, the size of VarInt required = 1.
|
|
||||||
const maxSignatureSize = 72
|
|
||||||
|
|
||||||
// maxAlertSize is the maximum size an alert.
|
|
||||||
//
|
|
||||||
// MessagePayload = VarInt(Alert) + Alert + VarInt(Signature) + Signature
|
|
||||||
// MaxMessagePayload = maxAlertSize + max(VarInt) + maxSignatureSize + 1
|
|
||||||
const maxAlertSize = MaxMessagePayload - maxSignatureSize - MaxVarIntPayload - 1
|
|
||||||
|
|
||||||
// maxCountSetCancel is the maximum number of cancel IDs that could possibly
|
|
||||||
// fit into a maximum size alert.
|
|
||||||
//
|
|
||||||
// maxAlertSize = fixedAlertSize + max(SetCancel) + max(SetSubVer) + 3*(string)
|
|
||||||
// for caculating maximum number of cancel IDs, set all other var sizes to 0
|
|
||||||
// maxAlertSize = fixedAlertSize + (MaxVarIntPayload-1) + x*sizeOf(int32)
|
|
||||||
// x = (maxAlertSize - fixedAlertSize - MaxVarIntPayload + 1) / 4
|
|
||||||
const maxCountSetCancel = (maxAlertSize - fixedAlertSize - MaxVarIntPayload + 1) / 4
|
|
||||||
|
|
||||||
// maxCountSetSubVer is the maximum number of subversions that could possibly
|
|
||||||
// fit into a maximum size alert.
|
|
||||||
//
|
|
||||||
// maxAlertSize = fixedAlertSize + max(SetCancel) + max(SetSubVer) + 3*(string)
|
|
||||||
// for caculating maximum number of subversions, set all other var sizes to 0
|
|
||||||
// maxAlertSize = fixedAlertSize + (MaxVarIntPayload-1) + x*sizeOf(string)
|
|
||||||
// x = (maxAlertSize - fixedAlertSize - MaxVarIntPayload + 1) / sizeOf(string)
|
|
||||||
// subversion would typically be something like "/Satoshi:0.7.2/" (15 bytes)
|
|
||||||
// so assuming < 255 bytes, sizeOf(string) = sizeOf(uint8) + 255 = 256
|
|
||||||
const maxCountSetSubVer = (maxAlertSize - fixedAlertSize - MaxVarIntPayload + 1) / 256
|
|
||||||
|
|
||||||
// Alert contains the data deserialized from the MsgAlert payload.
|
|
||||||
type Alert struct {
|
|
||||||
// Alert format version
|
|
||||||
Version int32
|
|
||||||
|
|
||||||
// Timestamp beyond which nodes should stop relaying this alert
|
|
||||||
RelayUntil int64
|
|
||||||
|
|
||||||
// Timestamp beyond which this alert is no longer in effect and
|
|
||||||
// should be ignored
|
|
||||||
Expiration int64
|
|
||||||
|
|
||||||
// A unique ID number for this alert
|
|
||||||
ID int32
|
|
||||||
|
|
||||||
// All alerts with an ID less than or equal to this number should
|
|
||||||
// cancelled, deleted and not accepted in the future
|
|
||||||
Cancel int32
|
|
||||||
|
|
||||||
// All alert IDs contained in this set should be cancelled as above
|
|
||||||
SetCancel []int32
|
|
||||||
|
|
||||||
// This alert only applies to versions greater than or equal to this
|
|
||||||
// version. Other versions should still relay it.
|
|
||||||
MinVer int32
|
|
||||||
|
|
||||||
// This alert only applies to versions less than or equal to this version.
|
|
||||||
// Other versions should still relay it.
|
|
||||||
MaxVer int32
|
|
||||||
|
|
||||||
// If this set contains any elements, then only nodes that have their
|
|
||||||
// subVer contained in this set are affected by the alert. Other versions
|
|
||||||
// should still relay it.
|
|
||||||
SetSubVer []string
|
|
||||||
|
|
||||||
// Relative priority compared to other alerts
|
|
||||||
Priority int32
|
|
||||||
|
|
||||||
// A comment on the alert that is not displayed
|
|
||||||
Comment string
|
|
||||||
|
|
||||||
// The alert message that is displayed to the user
|
|
||||||
StatusBar string
|
|
||||||
|
|
||||||
// Reserved
|
|
||||||
Reserved string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize encodes the alert to w using the alert protocol encoding format.
|
|
||||||
func (alert *Alert) Serialize(w io.Writer, pver uint32) error {
|
|
||||||
err := writeElements(w, alert.Version, alert.RelayUntil,
|
|
||||||
alert.Expiration, alert.ID, alert.Cancel)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
count := len(alert.SetCancel)
|
|
||||||
if count > maxCountSetCancel {
|
|
||||||
str := fmt.Sprintf("too many cancel alert IDs for alert "+
|
|
||||||
"[count %v, max %v]", count, maxCountSetCancel)
|
|
||||||
return messageError("Alert.Serialize", str)
|
|
||||||
}
|
|
||||||
err = WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
err = writeElement(w, alert.SetCancel[i])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElements(w, alert.MinVer, alert.MaxVer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
count = len(alert.SetSubVer)
|
|
||||||
if count > maxCountSetSubVer {
|
|
||||||
str := fmt.Sprintf("too many sub versions for alert "+
|
|
||||||
"[count %v, max %v]", count, maxCountSetSubVer)
|
|
||||||
return messageError("Alert.Serialize", str)
|
|
||||||
}
|
|
||||||
err = WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
err = WriteVarString(w, pver, alert.SetSubVer[i])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, alert.Priority)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = WriteVarString(w, pver, alert.Comment)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = WriteVarString(w, pver, alert.StatusBar)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return WriteVarString(w, pver, alert.Reserved)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes from r into the receiver using the alert protocol
|
|
||||||
// encoding format.
|
|
||||||
func (alert *Alert) Deserialize(r io.Reader, pver uint32) error {
|
|
||||||
err := readElements(r, &alert.Version, &alert.RelayUntil,
|
|
||||||
&alert.Expiration, &alert.ID, &alert.Cancel)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCancel: first read a VarInt that contains
|
|
||||||
// count - the number of Cancel IDs, then
|
|
||||||
// iterate count times and read them
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > maxCountSetCancel {
|
|
||||||
str := fmt.Sprintf("too many cancel alert IDs for alert "+
|
|
||||||
"[count %v, max %v]", count, maxCountSetCancel)
|
|
||||||
return messageError("Alert.Deserialize", str)
|
|
||||||
}
|
|
||||||
alert.SetCancel = make([]int32, count)
|
|
||||||
for i := 0; i < int(count); i++ {
|
|
||||||
err := readElement(r, &alert.SetCancel[i])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readElements(r, &alert.MinVer, &alert.MaxVer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetSubVer: similar to SetCancel
|
|
||||||
// but read count number of sub-version strings
|
|
||||||
count, err = ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > maxCountSetSubVer {
|
|
||||||
str := fmt.Sprintf("too many sub versions for alert "+
|
|
||||||
"[count %v, max %v]", count, maxCountSetSubVer)
|
|
||||||
return messageError("Alert.Deserialize", str)
|
|
||||||
}
|
|
||||||
alert.SetSubVer = make([]string, count)
|
|
||||||
for i := 0; i < int(count); i++ {
|
|
||||||
alert.SetSubVer[i], err = ReadVarString(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readElement(r, &alert.Priority)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
alert.Comment, err = ReadVarString(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
alert.StatusBar, err = ReadVarString(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
alert.Reserved, err = ReadVarString(r, pver)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAlert returns an new Alert with values provided.
|
|
||||||
func NewAlert(version int32, relayUntil int64, expiration int64,
|
|
||||||
id int32, cancel int32, setCancel []int32, minVer int32,
|
|
||||||
maxVer int32, setSubVer []string, priority int32, comment string,
|
|
||||||
statusBar string) *Alert {
|
|
||||||
return &Alert{
|
|
||||||
Version: version,
|
|
||||||
RelayUntil: relayUntil,
|
|
||||||
Expiration: expiration,
|
|
||||||
ID: id,
|
|
||||||
Cancel: cancel,
|
|
||||||
SetCancel: setCancel,
|
|
||||||
MinVer: minVer,
|
|
||||||
MaxVer: maxVer,
|
|
||||||
SetSubVer: setSubVer,
|
|
||||||
Priority: priority,
|
|
||||||
Comment: comment,
|
|
||||||
StatusBar: statusBar,
|
|
||||||
Reserved: "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAlertFromPayload returns an Alert with values deserialized from the
|
|
||||||
// serialized payload.
|
|
||||||
func NewAlertFromPayload(serializedPayload []byte, pver uint32) (*Alert, error) {
|
|
||||||
var alert Alert
|
|
||||||
r := bytes.NewReader(serializedPayload)
|
|
||||||
err := alert.Deserialize(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &alert, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MsgAlert implements the Message interface and defines a bitcoin alert
|
|
||||||
// message.
|
|
||||||
//
|
|
||||||
// This is a signed message that provides notifications that the client should
|
|
||||||
// display if the signature matches the key. bitcoind/bitcoin-qt only checks
|
|
||||||
// against a signature from the core developers.
|
|
||||||
type MsgAlert struct {
|
|
||||||
// SerializedPayload is the alert payload serialized as a string so that the
|
|
||||||
// version can change but the Alert can still be passed on by older
|
|
||||||
// clients.
|
|
||||||
SerializedPayload []byte
|
|
||||||
|
|
||||||
// Signature is the ECDSA signature of the message.
|
|
||||||
Signature []byte
|
|
||||||
|
|
||||||
// Deserialized Payload
|
|
||||||
Payload *Alert
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgAlert) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
msg.SerializedPayload, err = ReadVarBytes(r, pver, MaxMessagePayload,
|
|
||||||
"alert serialized payload")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Payload, err = NewAlertFromPayload(msg.SerializedPayload, pver)
|
|
||||||
if err != nil {
|
|
||||||
msg.Payload = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Signature, err = ReadVarBytes(r, pver, MaxMessagePayload,
|
|
||||||
"alert signature")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgAlert) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
var err error
|
|
||||||
var serializedpayload []byte
|
|
||||||
if msg.Payload != nil {
|
|
||||||
// try to Serialize Payload if possible
|
|
||||||
r := new(bytes.Buffer)
|
|
||||||
err = msg.Payload.Serialize(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
// Serialize failed - ignore & fallback
|
|
||||||
// to SerializedPayload
|
|
||||||
serializedpayload = msg.SerializedPayload
|
|
||||||
} else {
|
|
||||||
serializedpayload = r.Bytes()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
serializedpayload = msg.SerializedPayload
|
|
||||||
}
|
|
||||||
slen := uint64(len(serializedpayload))
|
|
||||||
if slen == 0 {
|
|
||||||
return messageError("MsgAlert.BtcEncode", "empty serialized payload")
|
|
||||||
}
|
|
||||||
err = WriteVarBytes(w, pver, serializedpayload)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return WriteVarBytes(w, pver, msg.Signature)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgAlert) Command() string {
|
|
||||||
return CmdAlert
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgAlert) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Since this can vary depending on the message, make it the max
|
|
||||||
// size allowed.
|
|
||||||
return MaxMessagePayload
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgAlert returns a new bitcoin alert message that conforms to the Message
|
|
||||||
// interface. See MsgAlert for details.
|
|
||||||
func NewMsgAlert(serializedPayload []byte, signature []byte) *MsgAlert {
|
|
||||||
return &MsgAlert{
|
|
||||||
SerializedPayload: serializedPayload,
|
|
||||||
Signature: signature,
|
|
||||||
Payload: nil,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,290 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// defaultTransactionAlloc is the default size used for the backing array
|
|
||||||
// for transactions. The transaction array will dynamically grow as needed, but
|
|
||||||
// this figure is intended to provide enough space for the number of
|
|
||||||
// transactions in the vast majority of blocks without needing to grow the
|
|
||||||
// backing array multiple times.
|
|
||||||
const defaultTransactionAlloc = 2048
|
|
||||||
|
|
||||||
// MaxBlocksPerMsg is the maximum number of blocks allowed per message.
|
|
||||||
const MaxBlocksPerMsg = 500
|
|
||||||
|
|
||||||
// MaxBlockPayload is the maximum bytes a block message can be in bytes.
|
|
||||||
// After Segregated Witness, the max block payload has been raised to 4MB.
|
|
||||||
const MaxBlockPayload = 4000000
|
|
||||||
|
|
||||||
// maxTxPerBlock is the maximum number of transactions that could
|
|
||||||
// possibly fit into a block.
|
|
||||||
const maxTxPerBlock = (MaxBlockPayload / minTxPayload) + 1
|
|
||||||
|
|
||||||
// TxLoc holds locator data for the offset and length of where a transaction is
|
|
||||||
// located within a MsgBlock data buffer.
|
|
||||||
type TxLoc struct {
|
|
||||||
TxStart int
|
|
||||||
TxLen int
|
|
||||||
}
|
|
||||||
|
|
||||||
// MsgBlock implements the Message interface and represents a bitcoin
|
|
||||||
// block message. It is used to deliver block and transaction information in
|
|
||||||
// response to a getdata message (MsgGetData) for a given block hash.
|
|
||||||
type MsgBlock struct {
|
|
||||||
Header BlockHeader
|
|
||||||
Transactions []*MsgTx
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddTransaction adds a transaction to the message.
|
|
||||||
func (msg *MsgBlock) AddTransaction(tx *MsgTx) error {
|
|
||||||
msg.Transactions = append(msg.Transactions, tx)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearTransactions removes all transactions from the message.
|
|
||||||
func (msg *MsgBlock) ClearTransactions() {
|
|
||||||
msg.Transactions = make([]*MsgTx, 0, defaultTransactionAlloc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Deserialize for decoding blocks stored to disk, such as in a database, as
|
|
||||||
// opposed to decoding blocks from the wire.
|
|
||||||
func (msg *MsgBlock) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
err := readBlockHeader(r, pver, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
txCount, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent more transactions than could possibly fit into a block.
|
|
||||||
// It would be possible to cause memory exhaustion and panics without
|
|
||||||
// a sane upper bound on this count.
|
|
||||||
if txCount > maxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many transactions to fit into a block "+
|
|
||||||
"[count %d, max %d]", txCount, maxTxPerBlock)
|
|
||||||
return messageError("MsgBlock.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Transactions = make([]*MsgTx, 0, txCount)
|
|
||||||
for i := uint64(0); i < txCount; i++ {
|
|
||||||
tx := MsgTx{}
|
|
||||||
err := tx.BtcDecode(r, pver, enc)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.Transactions = append(msg.Transactions, &tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a block from r into the receiver using a format that is
|
|
||||||
// suitable for long-term storage such as a database while respecting the
|
|
||||||
// Version field in the block. This function differs from BtcDecode in that
|
|
||||||
// BtcDecode decodes from the bitcoin wire protocol as it was sent across the
|
|
||||||
// network. The wire encoding can technically differ depending on the protocol
|
|
||||||
// version and doesn't even really need to match the format of a stored block at
|
|
||||||
// all. As of the time this comment was written, the encoded block is the same
|
|
||||||
// in both instances, but there is a distinct difference and separating the two
|
|
||||||
// allows the API to be flexible enough to deal with changes.
|
|
||||||
func (msg *MsgBlock) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of BtcDecode.
|
|
||||||
//
|
|
||||||
// Passing an encoding type of WitnessEncoding to BtcEncode for the
|
|
||||||
// MessageEncoding parameter indicates that the transactions within the
|
|
||||||
// block are expected to be serialized according to the new
|
|
||||||
// serialization structure defined in BIP0141.
|
|
||||||
return msg.BtcDecode(r, 0, WitnessEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeserializeNoWitness decodes a block from r into the receiver similar to
|
|
||||||
// Deserialize, however DeserializeWitness strips all (if any) witness data
|
|
||||||
// from the transactions within the block before encoding them.
|
|
||||||
func (msg *MsgBlock) DeserializeNoWitness(r io.Reader) error {
|
|
||||||
return msg.BtcDecode(r, 0, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeserializeTxLoc decodes r in the same manner Deserialize does, but it takes
|
|
||||||
// a byte buffer instead of a generic reader and returns a slice containing the
|
|
||||||
// start and length of each transaction within the raw data that is being
|
|
||||||
// deserialized.
|
|
||||||
func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, error) {
|
|
||||||
fullLen := r.Len()
|
|
||||||
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of existing wire protocol functions.
|
|
||||||
err := readBlockHeader(r, 0, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
txCount, err := ReadVarInt(r, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent more transactions than could possibly fit into a block.
|
|
||||||
// It would be possible to cause memory exhaustion and panics without
|
|
||||||
// a sane upper bound on this count.
|
|
||||||
if txCount > maxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many transactions to fit into a block "+
|
|
||||||
"[count %d, max %d]", txCount, maxTxPerBlock)
|
|
||||||
return nil, messageError("MsgBlock.DeserializeTxLoc", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize each transaction while keeping track of its location
|
|
||||||
// within the byte stream.
|
|
||||||
msg.Transactions = make([]*MsgTx, 0, txCount)
|
|
||||||
txLocs := make([]TxLoc, txCount)
|
|
||||||
for i := uint64(0); i < txCount; i++ {
|
|
||||||
txLocs[i].TxStart = fullLen - r.Len()
|
|
||||||
tx := MsgTx{}
|
|
||||||
err := tx.Deserialize(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
msg.Transactions = append(msg.Transactions, &tx)
|
|
||||||
txLocs[i].TxLen = (fullLen - r.Len()) - txLocs[i].TxStart
|
|
||||||
}
|
|
||||||
|
|
||||||
return txLocs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Serialize for encoding blocks to be stored to disk, such as in a
|
|
||||||
// database, as opposed to encoding blocks for the wire.
|
|
||||||
func (msg *MsgBlock) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
err := writeBlockHeader(w, pver, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarInt(w, pver, uint64(len(msg.Transactions)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
err = tx.BtcEncode(w, pver, enc)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize encodes the block to w using a format that suitable for long-term
|
|
||||||
// storage such as a database while respecting the Version field in the block.
|
|
||||||
// This function differs from BtcEncode in that BtcEncode encodes the block to
|
|
||||||
// the bitcoin wire protocol in order to be sent across the network. The wire
|
|
||||||
// encoding can technically differ depending on the protocol version and doesn't
|
|
||||||
// even really need to match the format of a stored block at all. As of the
|
|
||||||
// time this comment was written, the encoded block is the same in both
|
|
||||||
// instances, but there is a distinct difference and separating the two allows
|
|
||||||
// the API to be flexible enough to deal with changes.
|
|
||||||
func (msg *MsgBlock) Serialize(w io.Writer) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of BtcEncode.
|
|
||||||
//
|
|
||||||
// Passing WitnessEncoding as the encoding type here indicates that
|
|
||||||
// each of the transactions should be serialized using the witness
|
|
||||||
// serialization structure defined in BIP0141.
|
|
||||||
return msg.BtcEncode(w, 0, WitnessEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SerializeNoWitness encodes a block to w using an identical format to
|
|
||||||
// Serialize, with all (if any) witness data stripped from all transactions.
|
|
||||||
// This method is provided in additon to the regular Serialize, in order to
|
|
||||||
// allow one to selectively encode transaction witness data to non-upgraded
|
|
||||||
// peers which are unaware of the new encoding.
|
|
||||||
func (msg *MsgBlock) SerializeNoWitness(w io.Writer) error {
|
|
||||||
return msg.BtcEncode(w, 0, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SerializeSize returns the number of bytes it would take to serialize the
|
|
||||||
// block, factoring in any witness data within transaction.
|
|
||||||
func (msg *MsgBlock) SerializeSize() int {
|
|
||||||
// Block header bytes + Serialized varint size for the number of
|
|
||||||
// transactions.
|
|
||||||
n := blockHeaderLen + VarIntSerializeSize(uint64(len(msg.Transactions)))
|
|
||||||
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
n += tx.SerializeSize()
|
|
||||||
}
|
|
||||||
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// SerializeSizeStripped returns the number of bytes it would take to serialize
|
|
||||||
// the block, excluding any witness data (if any).
|
|
||||||
func (msg *MsgBlock) SerializeSizeStripped() int {
|
|
||||||
// Block header bytes + Serialized varint size for the number of
|
|
||||||
// transactions.
|
|
||||||
n := blockHeaderLen + VarIntSerializeSize(uint64(len(msg.Transactions)))
|
|
||||||
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
n += tx.SerializeSizeStripped()
|
|
||||||
}
|
|
||||||
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgBlock) Command() string {
|
|
||||||
return CmdBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgBlock) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Block header at 80 bytes + transaction count + max transactions
|
|
||||||
// which can vary up to the MaxBlockPayload (including the block header
|
|
||||||
// and transaction count).
|
|
||||||
return MaxBlockPayload
|
|
||||||
}
|
|
||||||
|
|
||||||
// BlockHash computes the block identifier hash for this block.
|
|
||||||
func (msg *MsgBlock) BlockHash() chainhash.Hash {
|
|
||||||
return msg.Header.BlockHash()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TxHashes returns a slice of hashes of all of transactions in this block.
|
|
||||||
func (msg *MsgBlock) TxHashes() ([]chainhash.Hash, error) {
|
|
||||||
hashList := make([]chainhash.Hash, 0, len(msg.Transactions))
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
hashList = append(hashList, tx.TxHash())
|
|
||||||
}
|
|
||||||
return hashList, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgBlock returns a new bitcoin block message that conforms to the
|
|
||||||
// Message interface. See MsgBlock for details.
|
|
||||||
func NewMsgBlock(blockHeader *BlockHeader) *MsgBlock {
|
|
||||||
return &MsgBlock{
|
|
||||||
Header: *blockHeader,
|
|
||||||
Transactions: make([]*MsgTx, 0, defaultTransactionAlloc),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,164 +0,0 @@
|
|||||||
// Copyright (c) 2018 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// CFCheckptInterval is the gap (in number of blocks) between each
|
|
||||||
// filter header checkpoint.
|
|
||||||
CFCheckptInterval = 1000
|
|
||||||
|
|
||||||
// maxCFHeadersLen is the max number of filter headers we will attempt
|
|
||||||
// to decode.
|
|
||||||
maxCFHeadersLen = 100000
|
|
||||||
)
|
|
||||||
|
|
||||||
// ErrInsaneCFHeaderCount signals that we were asked to decode an
|
|
||||||
// unreasonable number of cfilter headers.
|
|
||||||
var ErrInsaneCFHeaderCount = errors.New(
|
|
||||||
"refusing to decode unreasonable number of filter headers")
|
|
||||||
|
|
||||||
// MsgCFCheckpt implements the Message interface and represents a bitcoin
|
|
||||||
// cfcheckpt message. It is used to deliver committed filter header information
|
|
||||||
// in response to a getcfcheckpt message (MsgGetCFCheckpt). See MsgGetCFCheckpt
|
|
||||||
// for details on requesting the headers.
|
|
||||||
type MsgCFCheckpt struct {
|
|
||||||
FilterType FilterType
|
|
||||||
StopHash chainhash.Hash
|
|
||||||
FilterHeaders []*chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddCFHeader adds a new committed filter header to the message.
|
|
||||||
func (msg *MsgCFCheckpt) AddCFHeader(header *chainhash.Hash) error {
|
|
||||||
if len(msg.FilterHeaders) == cap(msg.FilterHeaders) {
|
|
||||||
str := fmt.Sprintf("FilterHeaders has insufficient capacity for "+
|
|
||||||
"additional header: len = %d", len(msg.FilterHeaders))
|
|
||||||
return messageError("MsgCFCheckpt.AddCFHeader", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.FilterHeaders = append(msg.FilterHeaders, header)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFCheckpt) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
|
||||||
// Read filter type
|
|
||||||
err := readElement(r, &msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read stop hash
|
|
||||||
err = readElement(r, &msg.StopHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read number of filter headers
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Refuse to decode an insane number of cfheaders.
|
|
||||||
if count > maxCFHeadersLen {
|
|
||||||
return ErrInsaneCFHeaderCount
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of hashes to deserialize into in order to
|
|
||||||
// reduce the number of allocations.
|
|
||||||
msg.FilterHeaders = make([]*chainhash.Hash, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
var cfh chainhash.Hash
|
|
||||||
err := readElement(r, &cfh)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.FilterHeaders[i] = &cfh
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFCheckpt) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
|
||||||
// Write filter type
|
|
||||||
err := writeElement(w, msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write stop hash
|
|
||||||
err = writeElement(w, msg.StopHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write length of FilterHeaders slice
|
|
||||||
count := len(msg.FilterHeaders)
|
|
||||||
err = WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, cfh := range msg.FilterHeaders {
|
|
||||||
err := writeElement(w, cfh)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a filter header from r into the receiver using a format
|
|
||||||
// that is suitable for long-term storage such as a database. This function
|
|
||||||
// differs from BtcDecode in that BtcDecode decodes from the bitcoin wire
|
|
||||||
// protocol as it was sent across the network. The wire encoding can
|
|
||||||
// technically differ depending on the protocol version and doesn't even really
|
|
||||||
// need to match the format of a stored filter header at all. As of the time
|
|
||||||
// this comment was written, the encoded filter header is the same in both
|
|
||||||
// instances, but there is a distinct difference and separating the two allows
|
|
||||||
// the API to be flexible enough to deal with changes.
|
|
||||||
func (msg *MsgCFCheckpt) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// and the stable long-term storage format. As a result, make use of
|
|
||||||
// BtcDecode.
|
|
||||||
return msg.BtcDecode(r, 0, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgCFCheckpt) Command() string {
|
|
||||||
return CmdCFCheckpt
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFCheckpt) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Message size depends on the blockchain height, so return general limit
|
|
||||||
// for all messages.
|
|
||||||
return MaxMessagePayload
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgCFCheckpt returns a new bitcoin cfheaders message that conforms to
|
|
||||||
// the Message interface. See MsgCFCheckpt for details.
|
|
||||||
func NewMsgCFCheckpt(filterType FilterType, stopHash *chainhash.Hash,
|
|
||||||
headersCount int) *MsgCFCheckpt {
|
|
||||||
return &MsgCFCheckpt{
|
|
||||||
FilterType: filterType,
|
|
||||||
StopHash: *stopHash,
|
|
||||||
FilterHeaders: make([]*chainhash.Hash, 0, headersCount),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,180 +0,0 @@
|
|||||||
// Copyright (c) 2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// MaxCFHeaderPayload is the maximum byte size of a committed
|
|
||||||
// filter header.
|
|
||||||
MaxCFHeaderPayload = chainhash.HashSize
|
|
||||||
|
|
||||||
// MaxCFHeadersPerMsg is the maximum number of committed filter headers
|
|
||||||
// that can be in a single bitcoin cfheaders message.
|
|
||||||
MaxCFHeadersPerMsg = 2000
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgCFHeaders implements the Message interface and represents a bitcoin
|
|
||||||
// cfheaders message. It is used to deliver committed filter header information
|
|
||||||
// in response to a getcfheaders message (MsgGetCFHeaders). The maximum number
|
|
||||||
// of committed filter headers per message is currently 2000. See
|
|
||||||
// MsgGetCFHeaders for details on requesting the headers.
|
|
||||||
type MsgCFHeaders struct {
|
|
||||||
FilterType FilterType
|
|
||||||
StopHash chainhash.Hash
|
|
||||||
PrevFilterHeader chainhash.Hash
|
|
||||||
FilterHashes []*chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddCFHash adds a new filter hash to the message.
|
|
||||||
func (msg *MsgCFHeaders) AddCFHash(hash *chainhash.Hash) error {
|
|
||||||
if len(msg.FilterHashes)+1 > MaxCFHeadersPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block headers in message [max %v]",
|
|
||||||
MaxBlockHeadersPerMsg)
|
|
||||||
return messageError("MsgCFHeaders.AddCFHash", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.FilterHashes = append(msg.FilterHashes, hash)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFHeaders) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
|
||||||
// Read filter type
|
|
||||||
err := readElement(r, &msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read stop hash
|
|
||||||
err = readElement(r, &msg.StopHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read prev filter header
|
|
||||||
err = readElement(r, &msg.PrevFilterHeader)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read number of filter headers
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max committed filter headers per message.
|
|
||||||
if count > MaxCFHeadersPerMsg {
|
|
||||||
str := fmt.Sprintf("too many committed filter headers for "+
|
|
||||||
"message [count %v, max %v]", count,
|
|
||||||
MaxBlockHeadersPerMsg)
|
|
||||||
return messageError("MsgCFHeaders.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of hashes to deserialize into in order to
|
|
||||||
// reduce the number of allocations.
|
|
||||||
msg.FilterHashes = make([]*chainhash.Hash, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
var cfh chainhash.Hash
|
|
||||||
err := readElement(r, &cfh)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddCFHash(&cfh)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
|
||||||
// Write filter type
|
|
||||||
err := writeElement(w, msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write stop hash
|
|
||||||
err = writeElement(w, msg.StopHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write prev filter header
|
|
||||||
err = writeElement(w, msg.PrevFilterHeader)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max committed headers per message.
|
|
||||||
count := len(msg.FilterHashes)
|
|
||||||
if count > MaxCFHeadersPerMsg {
|
|
||||||
str := fmt.Sprintf("too many committed filter headers for "+
|
|
||||||
"message [count %v, max %v]", count,
|
|
||||||
MaxBlockHeadersPerMsg)
|
|
||||||
return messageError("MsgCFHeaders.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, cfh := range msg.FilterHashes {
|
|
||||||
err := writeElement(w, cfh)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a filter header from r into the receiver using a format
|
|
||||||
// that is suitable for long-term storage such as a database. This function
|
|
||||||
// differs from BtcDecode in that BtcDecode decodes from the bitcoin wire
|
|
||||||
// protocol as it was sent across the network. The wire encoding can
|
|
||||||
// technically differ depending on the protocol version and doesn't even really
|
|
||||||
// need to match the format of a stored filter header at all. As of the time
|
|
||||||
// this comment was written, the encoded filter header is the same in both
|
|
||||||
// instances, but there is a distinct difference and separating the two allows
|
|
||||||
// the API to be flexible enough to deal with changes.
|
|
||||||
func (msg *MsgCFHeaders) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// and the stable long-term storage format. As a result, make use of
|
|
||||||
// BtcDecode.
|
|
||||||
return msg.BtcDecode(r, 0, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgCFHeaders) Command() string {
|
|
||||||
return CmdCFHeaders
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Hash size + filter type + num headers (varInt) +
|
|
||||||
// (header size * max headers).
|
|
||||||
return 1 + chainhash.HashSize + chainhash.HashSize + MaxVarIntPayload +
|
|
||||||
(MaxCFHeaderPayload * MaxCFHeadersPerMsg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgCFHeaders returns a new bitcoin cfheaders message that conforms to
|
|
||||||
// the Message interface. See MsgCFHeaders for details.
|
|
||||||
func NewMsgCFHeaders() *MsgCFHeaders {
|
|
||||||
return &MsgCFHeaders{
|
|
||||||
FilterHashes: make([]*chainhash.Hash, 0, MaxCFHeadersPerMsg),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,119 +0,0 @@
|
|||||||
// Copyright (c) 2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FilterType is used to represent a filter type.
|
|
||||||
type FilterType uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
// GCSFilterRegular is the regular filter type.
|
|
||||||
GCSFilterRegular FilterType = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// MaxCFilterDataSize is the maximum byte size of a committed filter.
|
|
||||||
// The maximum size is currently defined as 256KiB.
|
|
||||||
MaxCFilterDataSize = 256 * 1024
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgCFilter implements the Message interface and represents a bitcoin cfilter
|
|
||||||
// message. It is used to deliver a committed filter in response to a
|
|
||||||
// getcfilters (MsgGetCFilters) message.
|
|
||||||
type MsgCFilter struct {
|
|
||||||
FilterType FilterType
|
|
||||||
BlockHash chainhash.Hash
|
|
||||||
Data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFilter) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
|
||||||
// Read filter type
|
|
||||||
err := readElement(r, &msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the hash of the filter's block
|
|
||||||
err = readElement(r, &msg.BlockHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read filter data
|
|
||||||
msg.Data, err = ReadVarBytes(r, pver, MaxCFilterDataSize,
|
|
||||||
"cfilter data")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFilter) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
|
||||||
size := len(msg.Data)
|
|
||||||
if size > MaxCFilterDataSize {
|
|
||||||
str := fmt.Sprintf("cfilter size too large for message "+
|
|
||||||
"[size %v, max %v]", size, MaxCFilterDataSize)
|
|
||||||
return messageError("MsgCFilter.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := writeElement(w, msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, msg.BlockHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return WriteVarBytes(w, pver, msg.Data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a filter from r into the receiver using a format that is
|
|
||||||
// suitable for long-term storage such as a database. This function differs
|
|
||||||
// from BtcDecode in that BtcDecode decodes from the bitcoin wire protocol as
|
|
||||||
// it was sent across the network. The wire encoding can technically differ
|
|
||||||
// depending on the protocol version and doesn't even really need to match the
|
|
||||||
// format of a stored filter at all. As of the time this comment was written,
|
|
||||||
// the encoded filter is the same in both instances, but there is a distinct
|
|
||||||
// difference and separating the two allows the API to be flexible enough to
|
|
||||||
// deal with changes.
|
|
||||||
func (msg *MsgCFilter) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the wire encoding
|
|
||||||
// and the stable long-term storage format. As a result, make use of
|
|
||||||
// BtcDecode.
|
|
||||||
return msg.BtcDecode(r, 0, BaseEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgCFilter) Command() string {
|
|
||||||
return CmdCFilter
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgCFilter) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return uint32(VarIntSerializeSize(MaxCFilterDataSize)) +
|
|
||||||
MaxCFilterDataSize + chainhash.HashSize + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgCFilter returns a new bitcoin cfilter message that conforms to the
|
|
||||||
// Message interface. See MsgCFilter for details.
|
|
||||||
func NewMsgCFilter(filterType FilterType, blockHash *chainhash.Hash,
|
|
||||||
data []byte) *MsgCFilter {
|
|
||||||
return &MsgCFilter{
|
|
||||||
FilterType: filterType,
|
|
||||||
BlockHash: *blockHash,
|
|
||||||
Data: data,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgFeeFilter implements the Message interface and represents a bitcoin
|
|
||||||
// feefilter message. It is used to request the receiving peer does not
|
|
||||||
// announce any transactions below the specified minimum fee rate.
|
|
||||||
//
|
|
||||||
// This message was not added until protocol versions starting with
|
|
||||||
// FeeFilterVersion.
|
|
||||||
type MsgFeeFilter struct {
|
|
||||||
MinFee int64
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFeeFilter) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < FeeFilterVersion {
|
|
||||||
str := fmt.Sprintf("feefilter message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFeeFilter.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.MinFee)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFeeFilter) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < FeeFilterVersion {
|
|
||||||
str := fmt.Sprintf("feefilter message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFeeFilter.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, msg.MinFee)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgFeeFilter) Command() string {
|
|
||||||
return CmdFeeFilter
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFeeFilter) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return 8
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgFeeFilter returns a new bitcoin feefilter message that conforms to
|
|
||||||
// the Message interface. See MsgFeeFilter for details.
|
|
||||||
func NewMsgFeeFilter(minfee int64) *MsgFeeFilter {
|
|
||||||
return &MsgFeeFilter{
|
|
||||||
MinFee: minfee,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,81 +0,0 @@
|
|||||||
// Copyright (c) 2014-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// MaxFilterAddDataSize is the maximum byte size of a data
|
|
||||||
// element to add to the Bloom filter. It is equal to the
|
|
||||||
// maximum element size of a script.
|
|
||||||
MaxFilterAddDataSize = 520
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgFilterAdd implements the Message interface and represents a bitcoin
|
|
||||||
// filteradd message. It is used to add a data element to an existing Bloom
|
|
||||||
// filter.
|
|
||||||
//
|
|
||||||
// This message was not added until protocol version BIP0037Version.
|
|
||||||
type MsgFilterAdd struct {
|
|
||||||
Data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterAdd) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("filteradd message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFilterAdd.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
msg.Data, err = ReadVarBytes(r, pver, MaxFilterAddDataSize,
|
|
||||||
"filteradd data")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterAdd) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("filteradd message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFilterAdd.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
size := len(msg.Data)
|
|
||||||
if size > MaxFilterAddDataSize {
|
|
||||||
str := fmt.Sprintf("filteradd size too large for message "+
|
|
||||||
"[size %v, max %v]", size, MaxFilterAddDataSize)
|
|
||||||
return messageError("MsgFilterAdd.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return WriteVarBytes(w, pver, msg.Data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterAdd) Command() string {
|
|
||||||
return CmdFilterAdd
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterAdd) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return uint32(VarIntSerializeSize(MaxFilterAddDataSize)) +
|
|
||||||
MaxFilterAddDataSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgFilterAdd returns a new bitcoin filteradd message that conforms to the
|
|
||||||
// Message interface. See MsgFilterAdd for details.
|
|
||||||
func NewMsgFilterAdd(data []byte) *MsgFilterAdd {
|
|
||||||
return &MsgFilterAdd{
|
|
||||||
Data: data,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
// Copyright (c) 2014-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgFilterClear implements the Message interface and represents a bitcoin
|
|
||||||
// filterclear message which is used to reset a Bloom filter.
|
|
||||||
//
|
|
||||||
// This message was not added until protocol version BIP0037Version and has
|
|
||||||
// no payload.
|
|
||||||
type MsgFilterClear struct{}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterClear) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("filterclear message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFilterClear.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterClear) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("filterclear message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFilterClear.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterClear) Command() string {
|
|
||||||
return CmdFilterClear
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterClear) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgFilterClear returns a new bitcoin filterclear message that conforms to the Message
|
|
||||||
// interface. See MsgFilterClear for details.
|
|
||||||
func NewMsgFilterClear() *MsgFilterClear {
|
|
||||||
return &MsgFilterClear{}
|
|
||||||
}
|
|
@ -1,136 +0,0 @@
|
|||||||
// Copyright (c) 2014-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BloomUpdateType specifies how the filter is updated when a match is found
|
|
||||||
type BloomUpdateType uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
// BloomUpdateNone indicates the filter is not adjusted when a match is
|
|
||||||
// found.
|
|
||||||
BloomUpdateNone BloomUpdateType = 0
|
|
||||||
|
|
||||||
// BloomUpdateAll indicates if the filter matches any data element in a
|
|
||||||
// public key script, the outpoint is serialized and inserted into the
|
|
||||||
// filter.
|
|
||||||
BloomUpdateAll BloomUpdateType = 1
|
|
||||||
|
|
||||||
// BloomUpdateP2PubkeyOnly indicates if the filter matches a data
|
|
||||||
// element in a public key script and the script is of the standard
|
|
||||||
// pay-to-pubkey or multisig, the outpoint is serialized and inserted
|
|
||||||
// into the filter.
|
|
||||||
BloomUpdateP2PubkeyOnly BloomUpdateType = 2
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// MaxFilterLoadHashFuncs is the maximum number of hash functions to
|
|
||||||
// load into the Bloom filter.
|
|
||||||
MaxFilterLoadHashFuncs = 50
|
|
||||||
|
|
||||||
// MaxFilterLoadFilterSize is the maximum size in bytes a filter may be.
|
|
||||||
MaxFilterLoadFilterSize = 36000
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgFilterLoad implements the Message interface and represents a bitcoin
|
|
||||||
// filterload message which is used to reset a Bloom filter.
|
|
||||||
//
|
|
||||||
// This message was not added until protocol version BIP0037Version.
|
|
||||||
type MsgFilterLoad struct {
|
|
||||||
Filter []byte
|
|
||||||
HashFuncs uint32
|
|
||||||
Tweak uint32
|
|
||||||
Flags BloomUpdateType
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterLoad) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("filterload message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFilterLoad.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
msg.Filter, err = ReadVarBytes(r, pver, MaxFilterLoadFilterSize,
|
|
||||||
"filterload filter size")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readElements(r, &msg.HashFuncs, &msg.Tweak, &msg.Flags)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.HashFuncs > MaxFilterLoadHashFuncs {
|
|
||||||
str := fmt.Sprintf("too many filter hash functions for message "+
|
|
||||||
"[count %v, max %v]", msg.HashFuncs, MaxFilterLoadHashFuncs)
|
|
||||||
return messageError("MsgFilterLoad.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterLoad) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("filterload message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgFilterLoad.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
size := len(msg.Filter)
|
|
||||||
if size > MaxFilterLoadFilterSize {
|
|
||||||
str := fmt.Sprintf("filterload filter size too large for message "+
|
|
||||||
"[size %v, max %v]", size, MaxFilterLoadFilterSize)
|
|
||||||
return messageError("MsgFilterLoad.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.HashFuncs > MaxFilterLoadHashFuncs {
|
|
||||||
str := fmt.Sprintf("too many filter hash functions for message "+
|
|
||||||
"[count %v, max %v]", msg.HashFuncs, MaxFilterLoadHashFuncs)
|
|
||||||
return messageError("MsgFilterLoad.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := WriteVarBytes(w, pver, msg.Filter)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElements(w, msg.HashFuncs, msg.Tweak, msg.Flags)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterLoad) Command() string {
|
|
||||||
return CmdFilterLoad
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgFilterLoad) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Num filter bytes (varInt) + filter + 4 bytes hash funcs +
|
|
||||||
// 4 bytes tweak + 1 byte flags.
|
|
||||||
return uint32(VarIntSerializeSize(MaxFilterLoadFilterSize)) +
|
|
||||||
MaxFilterLoadFilterSize + 9
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgFilterLoad returns a new bitcoin filterload message that conforms to
|
|
||||||
// the Message interface. See MsgFilterLoad for details.
|
|
||||||
func NewMsgFilterLoad(filter []byte, hashFuncs uint32, tweak uint32, flags BloomUpdateType) *MsgFilterLoad {
|
|
||||||
return &MsgFilterLoad{
|
|
||||||
Filter: filter,
|
|
||||||
HashFuncs: hashFuncs,
|
|
||||||
Tweak: tweak,
|
|
||||||
Flags: flags,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgGetAddr implements the Message interface and represents a bitcoin
|
|
||||||
// getaddr message. It is used to request a list of known active peers on the
|
|
||||||
// network from a peer to help identify potential nodes. The list is returned
|
|
||||||
// via one or more addr messages (MsgAddr).
|
|
||||||
//
|
|
||||||
// This message has no payload.
|
|
||||||
type MsgGetAddr struct{}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetAddr) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetAddr) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetAddr) Command() string {
|
|
||||||
return CmdGetAddr
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetAddr) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetAddr returns a new bitcoin getaddr message that conforms to the
|
|
||||||
// Message interface. See MsgGetAddr for details.
|
|
||||||
func NewMsgGetAddr() *MsgGetAddr {
|
|
||||||
return &MsgGetAddr{}
|
|
||||||
}
|
|
@ -1,139 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed
|
|
||||||
// per message.
|
|
||||||
const MaxBlockLocatorsPerMsg = 500
|
|
||||||
|
|
||||||
// MsgGetBlocks implements the Message interface and represents a bitcoin
|
|
||||||
// getblocks message. It is used to request a list of blocks starting after the
|
|
||||||
// last known hash in the slice of block locator hashes. The list is returned
|
|
||||||
// via an inv message (MsgInv) and is limited by a specific hash to stop at or
|
|
||||||
// the maximum number of blocks per message, which is currently 500.
|
|
||||||
//
|
|
||||||
// Set the HashStop field to the hash at which to stop and use
|
|
||||||
// AddBlockLocatorHash to build up the list of block locator hashes.
|
|
||||||
//
|
|
||||||
// The algorithm for building the block locator hashes should be to add the
|
|
||||||
// hashes in reverse order until you reach the genesis block. In order to keep
|
|
||||||
// the list of locator hashes to a reasonable number of entries, first add the
|
|
||||||
// most recent 10 block hashes, then double the step each loop iteration to
|
|
||||||
// exponentially decrease the number of hashes the further away from head and
|
|
||||||
// closer to the genesis block you get.
|
|
||||||
type MsgGetBlocks struct {
|
|
||||||
ProtocolVersion uint32
|
|
||||||
BlockLocatorHashes []*chainhash.Hash
|
|
||||||
HashStop chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddBlockLocatorHash adds a new block locator hash to the message.
|
|
||||||
func (msg *MsgGetBlocks) AddBlockLocatorHash(hash *chainhash.Hash) error {
|
|
||||||
if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block locator hashes for message [max %v]",
|
|
||||||
MaxBlockLocatorsPerMsg)
|
|
||||||
return messageError("MsgGetBlocks.AddBlockLocatorHash", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetBlocks) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
err := readElement(r, &msg.ProtocolVersion)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read num block locator hashes and limit to max.
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > MaxBlockLocatorsPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block locator hashes for message "+
|
|
||||||
"[count %v, max %v]", count, MaxBlockLocatorsPerMsg)
|
|
||||||
return messageError("MsgGetBlocks.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of hashes to deserialize into in order to
|
|
||||||
// reduce the number of allocations.
|
|
||||||
locatorHashes := make([]chainhash.Hash, count)
|
|
||||||
msg.BlockLocatorHashes = make([]*chainhash.Hash, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
hash := &locatorHashes[i]
|
|
||||||
err := readElement(r, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddBlockLocatorHash(hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.HashStop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetBlocks) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
count := len(msg.BlockLocatorHashes)
|
|
||||||
if count > MaxBlockLocatorsPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block locator hashes for message "+
|
|
||||||
"[count %v, max %v]", count, MaxBlockLocatorsPerMsg)
|
|
||||||
return messageError("MsgGetBlocks.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := writeElement(w, msg.ProtocolVersion)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, hash := range msg.BlockLocatorHashes {
|
|
||||||
err = writeElement(w, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, &msg.HashStop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetBlocks) Command() string {
|
|
||||||
return CmdGetBlocks
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetBlocks) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Protocol version 4 bytes + num hashes (varInt) + max block locator
|
|
||||||
// hashes + hash stop.
|
|
||||||
return 4 + MaxVarIntPayload + (MaxBlockLocatorsPerMsg * chainhash.HashSize) + chainhash.HashSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetBlocks returns a new bitcoin getblocks message that conforms to the
|
|
||||||
// Message interface using the passed parameters and defaults for the remaining
|
|
||||||
// fields.
|
|
||||||
func NewMsgGetBlocks(hashStop *chainhash.Hash) *MsgGetBlocks {
|
|
||||||
return &MsgGetBlocks{
|
|
||||||
ProtocolVersion: ProtocolVersion,
|
|
||||||
BlockLocatorHashes: make([]*chainhash.Hash, 0, MaxBlockLocatorsPerMsg),
|
|
||||||
HashStop: *hashStop,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
// Copyright (c) 2018 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgGetCFCheckpt is a request for filter headers at evenly spaced intervals
|
|
||||||
// throughout the blockchain history. It allows to set the FilterType field to
|
|
||||||
// get headers in the chain of basic (0x00) or extended (0x01) headers.
|
|
||||||
type MsgGetCFCheckpt struct {
|
|
||||||
FilterType FilterType
|
|
||||||
StopHash chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFCheckpt) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
|
||||||
err := readElement(r, &msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.StopHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFCheckpt) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
|
||||||
err := writeElement(w, msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, &msg.StopHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFCheckpt) Command() string {
|
|
||||||
return CmdGetCFCheckpt
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFCheckpt) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Filter type + uint32 + block hash
|
|
||||||
return 1 + chainhash.HashSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetCFCheckpt returns a new bitcoin getcfcheckpt message that conforms
|
|
||||||
// to the Message interface using the passed parameters and defaults for the
|
|
||||||
// remaining fields.
|
|
||||||
func NewMsgGetCFCheckpt(filterType FilterType, stopHash *chainhash.Hash) *MsgGetCFCheckpt {
|
|
||||||
return &MsgGetCFCheckpt{
|
|
||||||
FilterType: filterType,
|
|
||||||
StopHash: *stopHash,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
// Copyright (c) 2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgGetCFHeaders is a message similar to MsgGetHeaders, but for committed
|
|
||||||
// filter headers. It allows to set the FilterType field to get headers in the
|
|
||||||
// chain of basic (0x00) or extended (0x01) headers.
|
|
||||||
type MsgGetCFHeaders struct {
|
|
||||||
FilterType FilterType
|
|
||||||
StartHeight uint32
|
|
||||||
StopHash chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
|
||||||
err := readElement(r, &msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readElement(r, &msg.StartHeight)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.StopHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
|
||||||
err := writeElement(w, msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, &msg.StartHeight)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, &msg.StopHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFHeaders) Command() string {
|
|
||||||
return CmdGetCFHeaders
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Filter type + uint32 + block hash
|
|
||||||
return 1 + 4 + chainhash.HashSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetCFHeaders returns a new bitcoin getcfheader message that conforms to
|
|
||||||
// the Message interface using the passed parameters and defaults for the
|
|
||||||
// remaining fields.
|
|
||||||
func NewMsgGetCFHeaders(filterType FilterType, startHeight uint32,
|
|
||||||
stopHash *chainhash.Hash) *MsgGetCFHeaders {
|
|
||||||
return &MsgGetCFHeaders{
|
|
||||||
FilterType: filterType,
|
|
||||||
StartHeight: startHeight,
|
|
||||||
StopHash: *stopHash,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,81 +0,0 @@
|
|||||||
// Copyright (c) 2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxGetCFiltersReqRange the maximum number of filters that may be requested in
|
|
||||||
// a getcfheaders message.
|
|
||||||
const MaxGetCFiltersReqRange = 1000
|
|
||||||
|
|
||||||
// MsgGetCFilters implements the Message interface and represents a bitcoin
|
|
||||||
// getcfilters message. It is used to request committed filters for a range of
|
|
||||||
// blocks.
|
|
||||||
type MsgGetCFilters struct {
|
|
||||||
FilterType FilterType
|
|
||||||
StartHeight uint32
|
|
||||||
StopHash chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFilters) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
|
||||||
err := readElement(r, &msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readElement(r, &msg.StartHeight)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.StopHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFilters) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
|
||||||
err := writeElement(w, msg.FilterType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, &msg.StartHeight)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, &msg.StopHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFilters) Command() string {
|
|
||||||
return CmdGetCFilters
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetCFilters) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Filter type + uint32 + block hash
|
|
||||||
return 1 + 4 + chainhash.HashSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetCFilters returns a new bitcoin getcfilters message that conforms to
|
|
||||||
// the Message interface using the passed parameters and defaults for the
|
|
||||||
// remaining fields.
|
|
||||||
func NewMsgGetCFilters(filterType FilterType, startHeight uint32,
|
|
||||||
stopHash *chainhash.Hash) *MsgGetCFilters {
|
|
||||||
return &MsgGetCFilters{
|
|
||||||
FilterType: filterType,
|
|
||||||
StartHeight: startHeight,
|
|
||||||
StopHash: *stopHash,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,133 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgGetData implements the Message interface and represents a bitcoin
|
|
||||||
// getdata message. It is used to request data such as blocks and transactions
|
|
||||||
// from another peer. It should be used in response to the inv (MsgInv) message
|
|
||||||
// to request the actual data referenced by each inventory vector the receiving
|
|
||||||
// peer doesn't already have. Each message is limited to a maximum number of
|
|
||||||
// inventory vectors, which is currently 50,000. As a result, multiple messages
|
|
||||||
// must be used to request larger amounts of data.
|
|
||||||
//
|
|
||||||
// Use the AddInvVect function to build up the list of inventory vectors when
|
|
||||||
// sending a getdata message to another peer.
|
|
||||||
type MsgGetData struct {
|
|
||||||
InvList []*InvVect
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddInvVect adds an inventory vector to the message.
|
|
||||||
func (msg *MsgGetData) AddInvVect(iv *InvVect) error {
|
|
||||||
if len(msg.InvList)+1 > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [max %v]",
|
|
||||||
MaxInvPerMsg)
|
|
||||||
return messageError("MsgGetData.AddInvVect", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.InvList = append(msg.InvList, iv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetData) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max inventory vectors per message.
|
|
||||||
if count > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
||||||
return messageError("MsgGetData.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of inventory vectors to deserialize into in
|
|
||||||
// order to reduce the number of allocations.
|
|
||||||
invList := make([]InvVect, count)
|
|
||||||
msg.InvList = make([]*InvVect, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
iv := &invList[i]
|
|
||||||
err := readInvVect(r, pver, iv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddInvVect(iv)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetData) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// Limit to max inventory vectors per message.
|
|
||||||
count := len(msg.InvList)
|
|
||||||
if count > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
||||||
return messageError("MsgGetData.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, iv := range msg.InvList {
|
|
||||||
err := writeInvVect(w, pver, iv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetData) Command() string {
|
|
||||||
return CmdGetData
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetData) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Num inventory vectors (varInt) + max allowed inventory vectors.
|
|
||||||
return MaxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetData returns a new bitcoin getdata message that conforms to the
|
|
||||||
// Message interface. See MsgGetData for details.
|
|
||||||
func NewMsgGetData() *MsgGetData {
|
|
||||||
return &MsgGetData{
|
|
||||||
InvList: make([]*InvVect, 0, defaultInvListAlloc),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetDataSizeHint returns a new bitcoin getdata message that conforms to
|
|
||||||
// the Message interface. See MsgGetData for details. This function differs
|
|
||||||
// from NewMsgGetData in that it allows a default allocation size for the
|
|
||||||
// backing array which houses the inventory vector list. This allows callers
|
|
||||||
// who know in advance how large the inventory list will grow to avoid the
|
|
||||||
// overhead of growing the internal backing array several times when appending
|
|
||||||
// large amounts of inventory vectors with AddInvVect. Note that the specified
|
|
||||||
// hint is just that - a hint that is used for the default allocation size.
|
|
||||||
// Adding more (or less) inventory vectors will still work properly. The size
|
|
||||||
// hint is limited to MaxInvPerMsg.
|
|
||||||
func NewMsgGetDataSizeHint(sizeHint uint) *MsgGetData {
|
|
||||||
// Limit the specified hint to the maximum allow per message.
|
|
||||||
if sizeHint > MaxInvPerMsg {
|
|
||||||
sizeHint = MaxInvPerMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
return &MsgGetData{
|
|
||||||
InvList: make([]*InvVect, 0, sizeHint),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,136 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgGetHeaders implements the Message interface and represents a bitcoin
|
|
||||||
// getheaders message. It is used to request a list of block headers for
|
|
||||||
// blocks starting after the last known hash in the slice of block locator
|
|
||||||
// hashes. The list is returned via a headers message (MsgHeaders) and is
|
|
||||||
// limited by a specific hash to stop at or the maximum number of block headers
|
|
||||||
// per message, which is currently 2000.
|
|
||||||
//
|
|
||||||
// Set the HashStop field to the hash at which to stop and use
|
|
||||||
// AddBlockLocatorHash to build up the list of block locator hashes.
|
|
||||||
//
|
|
||||||
// The algorithm for building the block locator hashes should be to add the
|
|
||||||
// hashes in reverse order until you reach the genesis block. In order to keep
|
|
||||||
// the list of locator hashes to a resonable number of entries, first add the
|
|
||||||
// most recent 10 block hashes, then double the step each loop iteration to
|
|
||||||
// exponentially decrease the number of hashes the further away from head and
|
|
||||||
// closer to the genesis block you get.
|
|
||||||
type MsgGetHeaders struct {
|
|
||||||
ProtocolVersion uint32
|
|
||||||
BlockLocatorHashes []*chainhash.Hash
|
|
||||||
HashStop chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddBlockLocatorHash adds a new block locator hash to the message.
|
|
||||||
func (msg *MsgGetHeaders) AddBlockLocatorHash(hash *chainhash.Hash) error {
|
|
||||||
if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block locator hashes for message [max %v]",
|
|
||||||
MaxBlockLocatorsPerMsg)
|
|
||||||
return messageError("MsgGetHeaders.AddBlockLocatorHash", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetHeaders) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
err := readElement(r, &msg.ProtocolVersion)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read num block locator hashes and limit to max.
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > MaxBlockLocatorsPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block locator hashes for message "+
|
|
||||||
"[count %v, max %v]", count, MaxBlockLocatorsPerMsg)
|
|
||||||
return messageError("MsgGetHeaders.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of hashes to deserialize into in order to
|
|
||||||
// reduce the number of allocations.
|
|
||||||
locatorHashes := make([]chainhash.Hash, count)
|
|
||||||
msg.BlockLocatorHashes = make([]*chainhash.Hash, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
hash := &locatorHashes[i]
|
|
||||||
err := readElement(r, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddBlockLocatorHash(hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.HashStop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetHeaders) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// Limit to max block locator hashes per message.
|
|
||||||
count := len(msg.BlockLocatorHashes)
|
|
||||||
if count > MaxBlockLocatorsPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block locator hashes for message "+
|
|
||||||
"[count %v, max %v]", count, MaxBlockLocatorsPerMsg)
|
|
||||||
return messageError("MsgGetHeaders.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := writeElement(w, msg.ProtocolVersion)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, hash := range msg.BlockLocatorHashes {
|
|
||||||
err := writeElement(w, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, &msg.HashStop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgGetHeaders) Command() string {
|
|
||||||
return CmdGetHeaders
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgGetHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Version 4 bytes + num block locator hashes (varInt) + max allowed block
|
|
||||||
// locators + hash stop.
|
|
||||||
return 4 + MaxVarIntPayload + (MaxBlockLocatorsPerMsg *
|
|
||||||
chainhash.HashSize) + chainhash.HashSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgGetHeaders returns a new bitcoin getheaders message that conforms to
|
|
||||||
// the Message interface. See MsgGetHeaders for details.
|
|
||||||
func NewMsgGetHeaders() *MsgGetHeaders {
|
|
||||||
return &MsgGetHeaders{
|
|
||||||
BlockLocatorHashes: make([]*chainhash.Hash, 0,
|
|
||||||
MaxBlockLocatorsPerMsg),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,136 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxBlockHeadersPerMsg is the maximum number of block headers that can be in
|
|
||||||
// a single bitcoin headers message.
|
|
||||||
const MaxBlockHeadersPerMsg = 2000
|
|
||||||
|
|
||||||
// MsgHeaders implements the Message interface and represents a bitcoin headers
|
|
||||||
// message. It is used to deliver block header information in response
|
|
||||||
// to a getheaders message (MsgGetHeaders). The maximum number of block headers
|
|
||||||
// per message is currently 2000. See MsgGetHeaders for details on requesting
|
|
||||||
// the headers.
|
|
||||||
type MsgHeaders struct {
|
|
||||||
Headers []*BlockHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddBlockHeader adds a new block header to the message.
|
|
||||||
func (msg *MsgHeaders) AddBlockHeader(bh *BlockHeader) error {
|
|
||||||
if len(msg.Headers)+1 > MaxBlockHeadersPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block headers in message [max %v]",
|
|
||||||
MaxBlockHeadersPerMsg)
|
|
||||||
return messageError("MsgHeaders.AddBlockHeader", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Headers = append(msg.Headers, bh)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max block headers per message.
|
|
||||||
if count > MaxBlockHeadersPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block headers for message "+
|
|
||||||
"[count %v, max %v]", count, MaxBlockHeadersPerMsg)
|
|
||||||
return messageError("MsgHeaders.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of headers to deserialize into in order to
|
|
||||||
// reduce the number of allocations.
|
|
||||||
headers := make([]BlockHeader, count)
|
|
||||||
msg.Headers = make([]*BlockHeader, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
bh := &headers[i]
|
|
||||||
err := readBlockHeader(r, pver, bh)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
txCount, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the transaction count is zero for headers.
|
|
||||||
if txCount > 0 {
|
|
||||||
str := fmt.Sprintf("block headers may not contain "+
|
|
||||||
"transactions [count %v]", txCount)
|
|
||||||
return messageError("MsgHeaders.BtcDecode", str)
|
|
||||||
}
|
|
||||||
msg.AddBlockHeader(bh)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgHeaders) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// Limit to max block headers per message.
|
|
||||||
count := len(msg.Headers)
|
|
||||||
if count > MaxBlockHeadersPerMsg {
|
|
||||||
str := fmt.Sprintf("too many block headers for message "+
|
|
||||||
"[count %v, max %v]", count, MaxBlockHeadersPerMsg)
|
|
||||||
return messageError("MsgHeaders.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, bh := range msg.Headers {
|
|
||||||
err := writeBlockHeader(w, pver, bh)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// The wire protocol encoding always includes a 0 for the number
|
|
||||||
// of transactions on header messages. This is really just an
|
|
||||||
// artifact of the way the original implementation serializes
|
|
||||||
// block headers, but it is required.
|
|
||||||
err = WriteVarInt(w, pver, 0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgHeaders) Command() string {
|
|
||||||
return CmdHeaders
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Num headers (varInt) + max allowed headers (header length + 1 byte
|
|
||||||
// for the number of transactions which is always 0).
|
|
||||||
return MaxVarIntPayload + ((MaxBlockHeaderPayload + 1) *
|
|
||||||
MaxBlockHeadersPerMsg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgHeaders returns a new bitcoin headers message that conforms to the
|
|
||||||
// Message interface. See MsgHeaders for details.
|
|
||||||
func NewMsgHeaders() *MsgHeaders {
|
|
||||||
return &MsgHeaders{
|
|
||||||
Headers: make([]*BlockHeader, 0, MaxBlockHeadersPerMsg),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,141 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// defaultInvListAlloc is the default size used for the backing array for an
|
|
||||||
// inventory list. The array will dynamically grow as needed, but this
|
|
||||||
// figure is intended to provide enough space for the max number of inventory
|
|
||||||
// vectors in a *typical* inventory message without needing to grow the backing
|
|
||||||
// array multiple times. Technically, the list can grow to MaxInvPerMsg, but
|
|
||||||
// rather than using that large figure, this figure more accurately reflects the
|
|
||||||
// typical case.
|
|
||||||
const defaultInvListAlloc = 1000
|
|
||||||
|
|
||||||
// MsgInv implements the Message interface and represents a bitcoin inv message.
|
|
||||||
// It is used to advertise a peer's known data such as blocks and transactions
|
|
||||||
// through inventory vectors. It may be sent unsolicited to inform other peers
|
|
||||||
// of the data or in response to a getblocks message (MsgGetBlocks). Each
|
|
||||||
// message is limited to a maximum number of inventory vectors, which is
|
|
||||||
// currently 50,000.
|
|
||||||
//
|
|
||||||
// Use the AddInvVect function to build up the list of inventory vectors when
|
|
||||||
// sending an inv message to another peer.
|
|
||||||
type MsgInv struct {
|
|
||||||
InvList []*InvVect
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddInvVect adds an inventory vector to the message.
|
|
||||||
func (msg *MsgInv) AddInvVect(iv *InvVect) error {
|
|
||||||
if len(msg.InvList)+1 > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [max %v]",
|
|
||||||
MaxInvPerMsg)
|
|
||||||
return messageError("MsgInv.AddInvVect", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.InvList = append(msg.InvList, iv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgInv) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max inventory vectors per message.
|
|
||||||
if count > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
||||||
return messageError("MsgInv.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of inventory vectors to deserialize into in
|
|
||||||
// order to reduce the number of allocations.
|
|
||||||
invList := make([]InvVect, count)
|
|
||||||
msg.InvList = make([]*InvVect, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
iv := &invList[i]
|
|
||||||
err := readInvVect(r, pver, iv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddInvVect(iv)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgInv) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// Limit to max inventory vectors per message.
|
|
||||||
count := len(msg.InvList)
|
|
||||||
if count > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
||||||
return messageError("MsgInv.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, iv := range msg.InvList {
|
|
||||||
err := writeInvVect(w, pver, iv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgInv) Command() string {
|
|
||||||
return CmdInv
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgInv) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Num inventory vectors (varInt) + max allowed inventory vectors.
|
|
||||||
return MaxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgInv returns a new bitcoin inv message that conforms to the Message
|
|
||||||
// interface. See MsgInv for details.
|
|
||||||
func NewMsgInv() *MsgInv {
|
|
||||||
return &MsgInv{
|
|
||||||
InvList: make([]*InvVect, 0, defaultInvListAlloc),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgInvSizeHint returns a new bitcoin inv message that conforms to the
|
|
||||||
// Message interface. See MsgInv for details. This function differs from
|
|
||||||
// NewMsgInv in that it allows a default allocation size for the backing array
|
|
||||||
// which houses the inventory vector list. This allows callers who know in
|
|
||||||
// advance how large the inventory list will grow to avoid the overhead of
|
|
||||||
// growing the internal backing array several times when appending large amounts
|
|
||||||
// of inventory vectors with AddInvVect. Note that the specified hint is just
|
|
||||||
// that - a hint that is used for the default allocation size. Adding more
|
|
||||||
// (or less) inventory vectors will still work properly. The size hint is
|
|
||||||
// limited to MaxInvPerMsg.
|
|
||||||
func NewMsgInvSizeHint(sizeHint uint) *MsgInv {
|
|
||||||
// Limit the specified hint to the maximum allow per message.
|
|
||||||
if sizeHint > MaxInvPerMsg {
|
|
||||||
sizeHint = MaxInvPerMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
return &MsgInv{
|
|
||||||
InvList: make([]*InvVect, 0, sizeHint),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgMemPool implements the Message interface and represents a bitcoin mempool
|
|
||||||
// message. It is used to request a list of transactions still in the active
|
|
||||||
// memory pool of a relay.
|
|
||||||
//
|
|
||||||
// This message has no payload and was not added until protocol versions
|
|
||||||
// starting with BIP0035Version.
|
|
||||||
type MsgMemPool struct{}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgMemPool) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0035Version {
|
|
||||||
str := fmt.Sprintf("mempool message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgMemPool.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgMemPool) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0035Version {
|
|
||||||
str := fmt.Sprintf("mempool message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgMemPool.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgMemPool) Command() string {
|
|
||||||
return CmdMemPool
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgMemPool) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgMemPool returns a new bitcoin pong message that conforms to the Message
|
|
||||||
// interface. See MsgPong for details.
|
|
||||||
func NewMsgMemPool() *MsgMemPool {
|
|
||||||
return &MsgMemPool{}
|
|
||||||
}
|
|
@ -1,159 +0,0 @@
|
|||||||
// Copyright (c) 2014-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// maxFlagsPerMerkleBlock is the maximum number of flag bytes that could
|
|
||||||
// possibly fit into a merkle block. Since each transaction is represented by
|
|
||||||
// a single bit, this is the max number of transactions per block divided by
|
|
||||||
// 8 bits per byte. Then an extra one to cover partials.
|
|
||||||
const maxFlagsPerMerkleBlock = maxTxPerBlock / 8
|
|
||||||
|
|
||||||
// MsgMerkleBlock implements the Message interface and represents a bitcoin
|
|
||||||
// merkleblock message which is used to reset a Bloom filter.
|
|
||||||
//
|
|
||||||
// This message was not added until protocol version BIP0037Version.
|
|
||||||
type MsgMerkleBlock struct {
|
|
||||||
Header BlockHeader
|
|
||||||
Transactions uint32
|
|
||||||
Hashes []*chainhash.Hash
|
|
||||||
Flags []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddTxHash adds a new transaction hash to the message.
|
|
||||||
func (msg *MsgMerkleBlock) AddTxHash(hash *chainhash.Hash) error {
|
|
||||||
if len(msg.Hashes)+1 > maxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many tx hashes for message [max %v]",
|
|
||||||
maxTxPerBlock)
|
|
||||||
return messageError("MsgMerkleBlock.AddTxHash", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Hashes = append(msg.Hashes, hash)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgMerkleBlock) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("merkleblock message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgMerkleBlock.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := readBlockHeader(r, pver, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readElement(r, &msg.Transactions)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read num block locator hashes and limit to max.
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > maxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many transaction hashes for message "+
|
|
||||||
"[count %v, max %v]", count, maxTxPerBlock)
|
|
||||||
return messageError("MsgMerkleBlock.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of hashes to deserialize into in order to
|
|
||||||
// reduce the number of allocations.
|
|
||||||
hashes := make([]chainhash.Hash, count)
|
|
||||||
msg.Hashes = make([]*chainhash.Hash, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
hash := &hashes[i]
|
|
||||||
err := readElement(r, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddTxHash(hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Flags, err = ReadVarBytes(r, pver, maxFlagsPerMerkleBlock,
|
|
||||||
"merkle block flags size")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgMerkleBlock) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < BIP0037Version {
|
|
||||||
str := fmt.Sprintf("merkleblock message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgMerkleBlock.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read num transaction hashes and limit to max.
|
|
||||||
numHashes := len(msg.Hashes)
|
|
||||||
if numHashes > maxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many transaction hashes for message "+
|
|
||||||
"[count %v, max %v]", numHashes, maxTxPerBlock)
|
|
||||||
return messageError("MsgMerkleBlock.BtcDecode", str)
|
|
||||||
}
|
|
||||||
numFlagBytes := len(msg.Flags)
|
|
||||||
if numFlagBytes > maxFlagsPerMerkleBlock {
|
|
||||||
str := fmt.Sprintf("too many flag bytes for message [count %v, "+
|
|
||||||
"max %v]", numFlagBytes, maxFlagsPerMerkleBlock)
|
|
||||||
return messageError("MsgMerkleBlock.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := writeBlockHeader(w, pver, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, msg.Transactions)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarInt(w, pver, uint64(numHashes))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, hash := range msg.Hashes {
|
|
||||||
err = writeElement(w, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return WriteVarBytes(w, pver, msg.Flags)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgMerkleBlock) Command() string {
|
|
||||||
return CmdMerkleBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgMerkleBlock) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return MaxBlockPayload
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgMerkleBlock returns a new bitcoin merkleblock message that conforms to
|
|
||||||
// the Message interface. See MsgMerkleBlock for details.
|
|
||||||
func NewMsgMerkleBlock(bh *BlockHeader) *MsgMerkleBlock {
|
|
||||||
return &MsgMerkleBlock{
|
|
||||||
Header: *bh,
|
|
||||||
Transactions: 0,
|
|
||||||
Hashes: make([]*chainhash.Hash, 0),
|
|
||||||
Flags: make([]byte, 0),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,110 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgNotFound defines a bitcoin notfound message which is sent in response to
|
|
||||||
// a getdata message if any of the requested data in not available on the peer.
|
|
||||||
// Each message is limited to a maximum number of inventory vectors, which is
|
|
||||||
// currently 50,000.
|
|
||||||
//
|
|
||||||
// Use the AddInvVect function to build up the list of inventory vectors when
|
|
||||||
// sending a notfound message to another peer.
|
|
||||||
type MsgNotFound struct {
|
|
||||||
InvList []*InvVect
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddInvVect adds an inventory vector to the message.
|
|
||||||
func (msg *MsgNotFound) AddInvVect(iv *InvVect) error {
|
|
||||||
if len(msg.InvList)+1 > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [max %v]",
|
|
||||||
MaxInvPerMsg)
|
|
||||||
return messageError("MsgNotFound.AddInvVect", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.InvList = append(msg.InvList, iv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgNotFound) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
count, err := ReadVarInt(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit to max inventory vectors per message.
|
|
||||||
if count > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
||||||
return messageError("MsgNotFound.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contiguous slice of inventory vectors to deserialize into in
|
|
||||||
// order to reduce the number of allocations.
|
|
||||||
invList := make([]InvVect, count)
|
|
||||||
msg.InvList = make([]*InvVect, 0, count)
|
|
||||||
for i := uint64(0); i < count; i++ {
|
|
||||||
iv := &invList[i]
|
|
||||||
err := readInvVect(r, pver, iv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.AddInvVect(iv)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgNotFound) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// Limit to max inventory vectors per message.
|
|
||||||
count := len(msg.InvList)
|
|
||||||
if count > MaxInvPerMsg {
|
|
||||||
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
||||||
return messageError("MsgNotFound.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := WriteVarInt(w, pver, uint64(count))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, iv := range msg.InvList {
|
|
||||||
err := writeInvVect(w, pver, iv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgNotFound) Command() string {
|
|
||||||
return CmdNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgNotFound) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// Max var int 9 bytes + max InvVects at 36 bytes each.
|
|
||||||
// Num inventory vectors (varInt) + max allowed inventory vectors.
|
|
||||||
return MaxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgNotFound returns a new bitcoin notfound message that conforms to the
|
|
||||||
// Message interface. See MsgNotFound for details.
|
|
||||||
func NewMsgNotFound() *MsgNotFound {
|
|
||||||
return &MsgNotFound{
|
|
||||||
InvList: make([]*InvVect, 0, defaultInvListAlloc),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,87 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgPing implements the Message interface and represents a bitcoin ping
|
|
||||||
// message.
|
|
||||||
//
|
|
||||||
// For versions BIP0031Version and earlier, it is used primarily to confirm
|
|
||||||
// that a connection is still valid. A transmission error is typically
|
|
||||||
// interpreted as a closed connection and that the peer should be removed.
|
|
||||||
// For versions AFTER BIP0031Version it contains an identifier which can be
|
|
||||||
// returned in the pong message to determine network timing.
|
|
||||||
//
|
|
||||||
// The payload for this message just consists of a nonce used for identifying
|
|
||||||
// it later.
|
|
||||||
type MsgPing struct {
|
|
||||||
// Unique value associated with message that is used to identify
|
|
||||||
// specific ping message.
|
|
||||||
Nonce uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgPing) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
// There was no nonce for BIP0031Version and earlier.
|
|
||||||
// NOTE: > is not a mistake here. The BIP0031 was defined as AFTER
|
|
||||||
// the version unlike most others.
|
|
||||||
if pver > BIP0031Version {
|
|
||||||
err := readElement(r, &msg.Nonce)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgPing) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// There was no nonce for BIP0031Version and earlier.
|
|
||||||
// NOTE: > is not a mistake here. The BIP0031 was defined as AFTER
|
|
||||||
// the version unlike most others.
|
|
||||||
if pver > BIP0031Version {
|
|
||||||
err := writeElement(w, msg.Nonce)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgPing) Command() string {
|
|
||||||
return CmdPing
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgPing) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
plen := uint32(0)
|
|
||||||
// There was no nonce for BIP0031Version and earlier.
|
|
||||||
// NOTE: > is not a mistake here. The BIP0031 was defined as AFTER
|
|
||||||
// the version unlike most others.
|
|
||||||
if pver > BIP0031Version {
|
|
||||||
// Nonce 8 bytes.
|
|
||||||
plen += 8
|
|
||||||
}
|
|
||||||
|
|
||||||
return plen
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgPing returns a new bitcoin ping message that conforms to the Message
|
|
||||||
// interface. See MsgPing for details.
|
|
||||||
func NewMsgPing(nonce uint64) *MsgPing {
|
|
||||||
return &MsgPing{
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgPong implements the Message interface and represents a bitcoin pong
|
|
||||||
// message which is used primarily to confirm that a connection is still valid
|
|
||||||
// in response to a bitcoin ping message (MsgPing).
|
|
||||||
//
|
|
||||||
// This message was not added until protocol versions AFTER BIP0031Version.
|
|
||||||
type MsgPong struct {
|
|
||||||
// Unique value associated with message that is used to identify
|
|
||||||
// specific ping message.
|
|
||||||
Nonce uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgPong) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
// NOTE: <= is not a mistake here. The BIP0031 was defined as AFTER
|
|
||||||
// the version unlike most others.
|
|
||||||
if pver <= BIP0031Version {
|
|
||||||
str := fmt.Sprintf("pong message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgPong.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return readElement(r, &msg.Nonce)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgPong) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
// NOTE: <= is not a mistake here. The BIP0031 was defined as AFTER
|
|
||||||
// the version unlike most others.
|
|
||||||
if pver <= BIP0031Version {
|
|
||||||
str := fmt.Sprintf("pong message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgPong.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return writeElement(w, msg.Nonce)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgPong) Command() string {
|
|
||||||
return CmdPong
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgPong) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
plen := uint32(0)
|
|
||||||
// The pong message did not exist for BIP0031Version and earlier.
|
|
||||||
// NOTE: > is not a mistake here. The BIP0031 was defined as AFTER
|
|
||||||
// the version unlike most others.
|
|
||||||
if pver > BIP0031Version {
|
|
||||||
// Nonce 8 bytes.
|
|
||||||
plen += 8
|
|
||||||
}
|
|
||||||
|
|
||||||
return plen
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgPong returns a new bitcoin pong message that conforms to the Message
|
|
||||||
// interface. See MsgPong for details.
|
|
||||||
func NewMsgPong(nonce uint64) *MsgPong {
|
|
||||||
return &MsgPong{
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,186 +0,0 @@
|
|||||||
// Copyright (c) 2014-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RejectCode represents a numeric value by which a remote peer indicates
|
|
||||||
// why a message was rejected.
|
|
||||||
type RejectCode uint8
|
|
||||||
|
|
||||||
// These constants define the various supported reject codes.
|
|
||||||
const (
|
|
||||||
RejectMalformed RejectCode = 0x01
|
|
||||||
RejectInvalid RejectCode = 0x10
|
|
||||||
RejectObsolete RejectCode = 0x11
|
|
||||||
RejectDuplicate RejectCode = 0x12
|
|
||||||
RejectNonstandard RejectCode = 0x40
|
|
||||||
RejectDust RejectCode = 0x41
|
|
||||||
RejectInsufficientFee RejectCode = 0x42
|
|
||||||
RejectCheckpoint RejectCode = 0x43
|
|
||||||
)
|
|
||||||
|
|
||||||
// Map of reject codes back strings for pretty printing.
|
|
||||||
var rejectCodeStrings = map[RejectCode]string{
|
|
||||||
RejectMalformed: "REJECT_MALFORMED",
|
|
||||||
RejectInvalid: "REJECT_INVALID",
|
|
||||||
RejectObsolete: "REJECT_OBSOLETE",
|
|
||||||
RejectDuplicate: "REJECT_DUPLICATE",
|
|
||||||
RejectNonstandard: "REJECT_NONSTANDARD",
|
|
||||||
RejectDust: "REJECT_DUST",
|
|
||||||
RejectInsufficientFee: "REJECT_INSUFFICIENTFEE",
|
|
||||||
RejectCheckpoint: "REJECT_CHECKPOINT",
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the RejectCode in human-readable form.
|
|
||||||
func (code RejectCode) String() string {
|
|
||||||
if s, ok := rejectCodeStrings[code]; ok {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("Unknown RejectCode (%d)", uint8(code))
|
|
||||||
}
|
|
||||||
|
|
||||||
// MsgReject implements the Message interface and represents a bitcoin reject
|
|
||||||
// message.
|
|
||||||
//
|
|
||||||
// This message was not added until protocol version RejectVersion.
|
|
||||||
type MsgReject struct {
|
|
||||||
// Cmd is the command for the message which was rejected such as
|
|
||||||
// as CmdBlock or CmdTx. This can be obtained from the Command function
|
|
||||||
// of a Message.
|
|
||||||
Cmd string
|
|
||||||
|
|
||||||
// RejectCode is a code indicating why the command was rejected. It
|
|
||||||
// is encoded as a uint8 on the wire.
|
|
||||||
Code RejectCode
|
|
||||||
|
|
||||||
// Reason is a human-readable string with specific details (over and
|
|
||||||
// above the reject code) about why the command was rejected.
|
|
||||||
Reason string
|
|
||||||
|
|
||||||
// Hash identifies a specific block or transaction that was rejected
|
|
||||||
// and therefore only applies the MsgBlock and MsgTx messages.
|
|
||||||
Hash chainhash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgReject) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < RejectVersion {
|
|
||||||
str := fmt.Sprintf("reject message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgReject.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command that was rejected.
|
|
||||||
cmd, err := ReadVarString(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.Cmd = cmd
|
|
||||||
|
|
||||||
// Code indicating why the command was rejected.
|
|
||||||
err = readElement(r, &msg.Code)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Human readable string with specific details (over and above the
|
|
||||||
// reject code above) about why the command was rejected.
|
|
||||||
reason, err := ReadVarString(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.Reason = reason
|
|
||||||
|
|
||||||
// CmdBlock and CmdTx messages have an additional hash field that
|
|
||||||
// identifies the specific block or transaction.
|
|
||||||
if msg.Cmd == CmdBlock || msg.Cmd == CmdTx {
|
|
||||||
err := readElement(r, &msg.Hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgReject) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < RejectVersion {
|
|
||||||
str := fmt.Sprintf("reject message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgReject.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command that was rejected.
|
|
||||||
err := WriteVarString(w, pver, msg.Cmd)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Code indicating why the command was rejected.
|
|
||||||
err = writeElement(w, msg.Code)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Human readable string with specific details (over and above the
|
|
||||||
// reject code above) about why the command was rejected.
|
|
||||||
err = WriteVarString(w, pver, msg.Reason)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// CmdBlock and CmdTx messages have an additional hash field that
|
|
||||||
// identifies the specific block or transaction.
|
|
||||||
if msg.Cmd == CmdBlock || msg.Cmd == CmdTx {
|
|
||||||
err := writeElement(w, &msg.Hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgReject) Command() string {
|
|
||||||
return CmdReject
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgReject) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
plen := uint32(0)
|
|
||||||
// The reject message did not exist before protocol version
|
|
||||||
// RejectVersion.
|
|
||||||
if pver >= RejectVersion {
|
|
||||||
// Unfortunately the bitcoin protocol does not enforce a sane
|
|
||||||
// limit on the length of the reason, so the max payload is the
|
|
||||||
// overall maximum message payload.
|
|
||||||
plen = MaxMessagePayload
|
|
||||||
}
|
|
||||||
|
|
||||||
return plen
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgReject returns a new bitcoin reject message that conforms to the
|
|
||||||
// Message interface. See MsgReject for details.
|
|
||||||
func NewMsgReject(command string, code RejectCode, reason string) *MsgReject {
|
|
||||||
return &MsgReject{
|
|
||||||
Cmd: command,
|
|
||||||
Code: code,
|
|
||||||
Reason: reason,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
// Copyright (c) 2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgSendHeaders implements the Message interface and represents a bitcoin
|
|
||||||
// sendheaders message. It is used to request the peer send block headers
|
|
||||||
// rather than inventory vectors.
|
|
||||||
//
|
|
||||||
// This message has no payload and was not added until protocol versions
|
|
||||||
// starting with SendHeadersVersion.
|
|
||||||
type MsgSendHeaders struct{}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgSendHeaders) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < SendHeadersVersion {
|
|
||||||
str := fmt.Sprintf("sendheaders message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgSendHeaders.BtcDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgSendHeaders) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
if pver < SendHeadersVersion {
|
|
||||||
str := fmt.Sprintf("sendheaders message invalid for protocol "+
|
|
||||||
"version %d", pver)
|
|
||||||
return messageError("MsgSendHeaders.BtcEncode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgSendHeaders) Command() string {
|
|
||||||
return CmdSendHeaders
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgSendHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgSendHeaders returns a new bitcoin sendheaders message that conforms to
|
|
||||||
// the Message interface. See MsgSendHeaders for details.
|
|
||||||
func NewMsgSendHeaders() *MsgSendHeaders {
|
|
||||||
return &MsgSendHeaders{}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -1,46 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgVerAck defines a bitcoin verack message which is used for a peer to
|
|
||||||
// acknowledge a version message (MsgVersion) after it has used the information
|
|
||||||
// to negotiate parameters. It implements the Message interface.
|
|
||||||
//
|
|
||||||
// This message has no payload.
|
|
||||||
type MsgVerAck struct{}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgVerAck) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgVerAck) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgVerAck) Command() string {
|
|
||||||
return CmdVerAck
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgVerAck) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgVerAck returns a new bitcoin verack message that conforms to the
|
|
||||||
// Message interface.
|
|
||||||
func NewMsgVerAck() *MsgVerAck {
|
|
||||||
return &MsgVerAck{}
|
|
||||||
}
|
|
@ -1,269 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxUserAgentLen is the maximum allowed length for the user agent field in a
|
|
||||||
// version message (MsgVersion).
|
|
||||||
const MaxUserAgentLen = 256
|
|
||||||
|
|
||||||
// DefaultUserAgent for wire in the stack
|
|
||||||
const DefaultUserAgent = "/btcwire:0.5.0/"
|
|
||||||
|
|
||||||
// MsgVersion implements the Message interface and represents a bitcoin version
|
|
||||||
// message. It is used for a peer to advertise itself as soon as an outbound
|
|
||||||
// connection is made. The remote peer then uses this information along with
|
|
||||||
// its own to negotiate. The remote peer must then respond with a version
|
|
||||||
// message of its own containing the negotiated values followed by a verack
|
|
||||||
// message (MsgVerAck). This exchange must take place before any further
|
|
||||||
// communication is allowed to proceed.
|
|
||||||
type MsgVersion struct {
|
|
||||||
// Version of the protocol the node is using.
|
|
||||||
ProtocolVersion int32
|
|
||||||
|
|
||||||
// Bitfield which identifies the enabled services.
|
|
||||||
Services ServiceFlag
|
|
||||||
|
|
||||||
// Time the message was generated. This is encoded as an int64 on the wire.
|
|
||||||
Timestamp time.Time
|
|
||||||
|
|
||||||
// Address of the remote peer.
|
|
||||||
AddrYou NetAddress
|
|
||||||
|
|
||||||
// Address of the local peer.
|
|
||||||
AddrMe NetAddress
|
|
||||||
|
|
||||||
// Unique value associated with message that is used to detect self
|
|
||||||
// connections.
|
|
||||||
Nonce uint64
|
|
||||||
|
|
||||||
// The user agent that generated messsage. This is a encoded as a varString
|
|
||||||
// on the wire. This has a max length of MaxUserAgentLen.
|
|
||||||
UserAgent string
|
|
||||||
|
|
||||||
// Last block seen by the generator of the version message.
|
|
||||||
LastBlock int32
|
|
||||||
|
|
||||||
// Don't announce transactions to peer.
|
|
||||||
DisableRelayTx bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasService returns whether the specified service is supported by the peer
|
|
||||||
// that generated the message.
|
|
||||||
func (msg *MsgVersion) HasService(service ServiceFlag) bool {
|
|
||||||
return msg.Services&service == service
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddService adds service as a supported service by the peer generating the
|
|
||||||
// message.
|
|
||||||
func (msg *MsgVersion) AddService(service ServiceFlag) {
|
|
||||||
msg.Services |= service
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
||||||
// The version message is special in that the protocol version hasn't been
|
|
||||||
// negotiated yet. As a result, the pver field is ignored and any fields which
|
|
||||||
// are added in new versions are optional. This also mean that r must be a
|
|
||||||
// *bytes.Buffer so the number of remaining bytes can be ascertained.
|
|
||||||
//
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgVersion) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
|
||||||
buf, ok := r.(*bytes.Buffer)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("MsgVersion.BtcDecode reader is not a " +
|
|
||||||
"*bytes.Buffer")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := readElements(buf, &msg.ProtocolVersion, &msg.Services,
|
|
||||||
(*int64Time)(&msg.Timestamp))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = readNetAddress(buf, pver, &msg.AddrYou, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Protocol versions >= 106 added a from address, nonce, and user agent
|
|
||||||
// field and they are only considered present if there are bytes
|
|
||||||
// remaining in the message.
|
|
||||||
if buf.Len() > 0 {
|
|
||||||
err = readNetAddress(buf, pver, &msg.AddrMe, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if buf.Len() > 0 {
|
|
||||||
err = readElement(buf, &msg.Nonce)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if buf.Len() > 0 {
|
|
||||||
userAgent, err := ReadVarString(buf, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = validateUserAgent(userAgent)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.UserAgent = userAgent
|
|
||||||
}
|
|
||||||
|
|
||||||
// Protocol versions >= 209 added a last known block field. It is only
|
|
||||||
// considered present if there are bytes remaining in the message.
|
|
||||||
if buf.Len() > 0 {
|
|
||||||
err = readElement(buf, &msg.LastBlock)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// There was no relay transactions field before BIP0037Version, but
|
|
||||||
// the default behavior prior to the addition of the field was to always
|
|
||||||
// relay transactions.
|
|
||||||
if buf.Len() > 0 {
|
|
||||||
// It's safe to ignore the error here since the buffer has at
|
|
||||||
// least one byte and that byte will result in a boolean value
|
|
||||||
// regardless of its value. Also, the wire encoding for the
|
|
||||||
// field is true when transactions should be relayed, so reverse
|
|
||||||
// it for the DisableRelayTx field.
|
|
||||||
var relayTx bool
|
|
||||||
readElement(r, &relayTx)
|
|
||||||
msg.DisableRelayTx = !relayTx
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgVersion) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
|
||||||
err := validateUserAgent(msg.UserAgent)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElements(w, msg.ProtocolVersion, msg.Services,
|
|
||||||
msg.Timestamp.Unix())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeNetAddress(w, pver, &msg.AddrYou, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeNetAddress(w, pver, &msg.AddrMe, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, msg.Nonce)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarString(w, pver, msg.UserAgent)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeElement(w, msg.LastBlock)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// There was no relay transactions field before BIP0037Version. Also,
|
|
||||||
// the wire encoding for the field is true when transactions should be
|
|
||||||
// relayed, so reverse it from the DisableRelayTx field.
|
|
||||||
if pver >= BIP0037Version {
|
|
||||||
err = writeElement(w, !msg.DisableRelayTx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgVersion) Command() string {
|
|
||||||
return CmdVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgVersion) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
// XXX: <= 106 different
|
|
||||||
|
|
||||||
// Protocol version 4 bytes + services 8 bytes + timestamp 8 bytes +
|
|
||||||
// remote and local net addresses + nonce 8 bytes + length of user
|
|
||||||
// agent (varInt) + max allowed useragent length + last block 4 bytes +
|
|
||||||
// relay transactions flag 1 byte.
|
|
||||||
return 33 + (maxNetAddressPayload(pver) * 2) + MaxVarIntPayload +
|
|
||||||
MaxUserAgentLen
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgVersion returns a new bitcoin version message that conforms to the
|
|
||||||
// Message interface using the passed parameters and defaults for the remaining
|
|
||||||
// fields.
|
|
||||||
func NewMsgVersion(me *NetAddress, you *NetAddress, nonce uint64,
|
|
||||||
lastBlock int32) *MsgVersion {
|
|
||||||
|
|
||||||
// Limit the timestamp to one second precision since the protocol
|
|
||||||
// doesn't support better.
|
|
||||||
return &MsgVersion{
|
|
||||||
ProtocolVersion: int32(ProtocolVersion),
|
|
||||||
Services: 0,
|
|
||||||
Timestamp: time.Unix(time.Now().Unix(), 0),
|
|
||||||
AddrYou: *you,
|
|
||||||
AddrMe: *me,
|
|
||||||
Nonce: nonce,
|
|
||||||
UserAgent: DefaultUserAgent,
|
|
||||||
LastBlock: lastBlock,
|
|
||||||
DisableRelayTx: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// validateUserAgent checks userAgent length against MaxUserAgentLen
|
|
||||||
func validateUserAgent(userAgent string) error {
|
|
||||||
if len(userAgent) > MaxUserAgentLen {
|
|
||||||
str := fmt.Sprintf("user agent too long [len %v, max %v]",
|
|
||||||
len(userAgent), MaxUserAgentLen)
|
|
||||||
return messageError("MsgVersion", str)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddUserAgent adds a user agent to the user agent string for the version
|
|
||||||
// message. The version string is not defined to any strict format, although
|
|
||||||
// it is recommended to use the form "major.minor.revision" e.g. "2.6.41".
|
|
||||||
func (msg *MsgVersion) AddUserAgent(name string, version string,
|
|
||||||
comments ...string) error {
|
|
||||||
|
|
||||||
newUserAgent := fmt.Sprintf("%s:%s", name, version)
|
|
||||||
if len(comments) != 0 {
|
|
||||||
newUserAgent = fmt.Sprintf("%s(%s)", newUserAgent,
|
|
||||||
strings.Join(comments, "; "))
|
|
||||||
}
|
|
||||||
newUserAgent = fmt.Sprintf("%s%s/", msg.UserAgent, newUserAgent)
|
|
||||||
err := validateUserAgent(newUserAgent)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.UserAgent = newUserAgent
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,149 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// maxNetAddressPayload returns the max payload size for a bitcoin NetAddress
|
|
||||||
// based on the protocol version.
|
|
||||||
func maxNetAddressPayload(pver uint32) uint32 {
|
|
||||||
// Services 8 bytes + ip 16 bytes + port 2 bytes.
|
|
||||||
plen := uint32(26)
|
|
||||||
|
|
||||||
// NetAddressTimeVersion added a timestamp field.
|
|
||||||
if pver >= NetAddressTimeVersion {
|
|
||||||
// Timestamp 4 bytes.
|
|
||||||
plen += 4
|
|
||||||
}
|
|
||||||
|
|
||||||
return plen
|
|
||||||
}
|
|
||||||
|
|
||||||
// NetAddress defines information about a peer on the network including the time
|
|
||||||
// it was last seen, the services it supports, its IP address, and port.
|
|
||||||
type NetAddress struct {
|
|
||||||
// Last time the address was seen. This is, unfortunately, encoded as a
|
|
||||||
// uint32 on the wire and therefore is limited to 2106. This field is
|
|
||||||
// not present in the bitcoin version message (MsgVersion) nor was it
|
|
||||||
// added until protocol version >= NetAddressTimeVersion.
|
|
||||||
Timestamp time.Time
|
|
||||||
|
|
||||||
// Bitfield which identifies the services supported by the address.
|
|
||||||
Services ServiceFlag
|
|
||||||
|
|
||||||
// IP address of the peer.
|
|
||||||
IP net.IP
|
|
||||||
|
|
||||||
// Port the peer is using. This is encoded in big endian on the wire
|
|
||||||
// which differs from most everything else.
|
|
||||||
Port uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasService returns whether the specified service is supported by the address.
|
|
||||||
func (na *NetAddress) HasService(service ServiceFlag) bool {
|
|
||||||
return na.Services&service == service
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddService adds service as a supported service by the peer generating the
|
|
||||||
// message.
|
|
||||||
func (na *NetAddress) AddService(service ServiceFlag) {
|
|
||||||
na.Services |= service
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and
|
|
||||||
// supported services with defaults for the remaining fields.
|
|
||||||
func NewNetAddressIPPort(ip net.IP, port uint16, services ServiceFlag) *NetAddress {
|
|
||||||
return NewNetAddressTimestamp(time.Now(), services, ip, port)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNetAddressTimestamp returns a new NetAddress using the provided
|
|
||||||
// timestamp, IP, port, and supported services. The timestamp is rounded to
|
|
||||||
// single second precision.
|
|
||||||
func NewNetAddressTimestamp(
|
|
||||||
timestamp time.Time, services ServiceFlag, ip net.IP, port uint16) *NetAddress {
|
|
||||||
// Limit the timestamp to one second precision since the protocol
|
|
||||||
// doesn't support better.
|
|
||||||
na := NetAddress{
|
|
||||||
Timestamp: time.Unix(timestamp.Unix(), 0),
|
|
||||||
Services: services,
|
|
||||||
IP: ip,
|
|
||||||
Port: port,
|
|
||||||
}
|
|
||||||
return &na
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNetAddress returns a new NetAddress using the provided TCP address and
|
|
||||||
// supported services with defaults for the remaining fields.
|
|
||||||
func NewNetAddress(addr *net.TCPAddr, services ServiceFlag) *NetAddress {
|
|
||||||
return NewNetAddressIPPort(addr.IP, uint16(addr.Port), services)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readNetAddress reads an encoded NetAddress from r depending on the protocol
|
|
||||||
// version and whether or not the timestamp is included per ts. Some messages
|
|
||||||
// like version do not include the timestamp.
|
|
||||||
func readNetAddress(r io.Reader, pver uint32, na *NetAddress, ts bool) error {
|
|
||||||
var ip [16]byte
|
|
||||||
|
|
||||||
// NOTE: The bitcoin protocol uses a uint32 for the timestamp so it will
|
|
||||||
// stop working somewhere around 2106. Also timestamp wasn't added until
|
|
||||||
// protocol version >= NetAddressTimeVersion
|
|
||||||
if ts && pver >= NetAddressTimeVersion {
|
|
||||||
err := readElement(r, (*uint32Time)(&na.Timestamp))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err := readElements(r, &na.Services, &ip)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Sigh. Bitcoin protocol mixes little and big endian.
|
|
||||||
port, err := binarySerializer.Uint16(r, bigEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*na = NetAddress{
|
|
||||||
Timestamp: na.Timestamp,
|
|
||||||
Services: na.Services,
|
|
||||||
IP: net.IP(ip[:]),
|
|
||||||
Port: port,
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeNetAddress serializes a NetAddress to w depending on the protocol
|
|
||||||
// version and whether or not the timestamp is included per ts. Some messages
|
|
||||||
// like version do not include the timestamp.
|
|
||||||
func writeNetAddress(w io.Writer, pver uint32, na *NetAddress, ts bool) error {
|
|
||||||
// NOTE: The bitcoin protocol uses a uint32 for the timestamp so it will
|
|
||||||
// stop working somewhere around 2106. Also timestamp wasn't added until
|
|
||||||
// until protocol version >= NetAddressTimeVersion.
|
|
||||||
if ts && pver >= NetAddressTimeVersion {
|
|
||||||
err := writeElement(w, uint32(na.Timestamp.Unix()))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure to always write 16 bytes even if the ip is nil.
|
|
||||||
var ip [16]byte
|
|
||||||
if na.IP != nil {
|
|
||||||
copy(ip[:], na.IP.To16())
|
|
||||||
}
|
|
||||||
err := writeElements(w, na.Services, ip)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sigh. Bitcoin protocol mixes little and big endian.
|
|
||||||
return binary.Write(w, bigEndian, na.Port)
|
|
||||||
}
|
|
@ -1,178 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package wire
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// XXX pedro: we will probably need to bump this.
|
|
||||||
const (
|
|
||||||
// ProtocolVersion is the latest protocol version this package supports.
|
|
||||||
ProtocolVersion uint32 = 70013
|
|
||||||
|
|
||||||
// MultipleAddressVersion is the protocol version which added multiple
|
|
||||||
// addresses per message (pver >= MultipleAddressVersion).
|
|
||||||
MultipleAddressVersion uint32 = 209
|
|
||||||
|
|
||||||
// NetAddressTimeVersion is the protocol version which added the
|
|
||||||
// timestamp field (pver >= NetAddressTimeVersion).
|
|
||||||
NetAddressTimeVersion uint32 = 31402
|
|
||||||
|
|
||||||
// BIP0031Version is the protocol version AFTER which a pong message
|
|
||||||
// and nonce field in ping were added (pver > BIP0031Version).
|
|
||||||
BIP0031Version uint32 = 60000
|
|
||||||
|
|
||||||
// BIP0035Version is the protocol version which added the mempool
|
|
||||||
// message (pver >= BIP0035Version).
|
|
||||||
BIP0035Version uint32 = 60002
|
|
||||||
|
|
||||||
// BIP0037Version is the protocol version which added new connection
|
|
||||||
// bloom filtering related messages and extended the version message
|
|
||||||
// with a relay flag (pver >= BIP0037Version).
|
|
||||||
BIP0037Version uint32 = 70001
|
|
||||||
|
|
||||||
// RejectVersion is the protocol version which added a new reject
|
|
||||||
// message.
|
|
||||||
RejectVersion uint32 = 70002
|
|
||||||
|
|
||||||
// BIP0111Version is the protocol version which added the SFNodeBloom
|
|
||||||
// service flag.
|
|
||||||
BIP0111Version uint32 = 70011
|
|
||||||
|
|
||||||
// SendHeadersVersion is the protocol version which added a new
|
|
||||||
// sendheaders message.
|
|
||||||
SendHeadersVersion uint32 = 70012
|
|
||||||
|
|
||||||
// FeeFilterVersion is the protocol version which added a new
|
|
||||||
// feefilter message.
|
|
||||||
FeeFilterVersion uint32 = 70013
|
|
||||||
)
|
|
||||||
|
|
||||||
// ServiceFlag identifies services supported by a bitcoin peer.
|
|
||||||
type ServiceFlag uint64
|
|
||||||
|
|
||||||
const (
|
|
||||||
// SFNodeNetwork is a flag used to indicate a peer is a full node.
|
|
||||||
SFNodeNetwork ServiceFlag = 1 << iota
|
|
||||||
|
|
||||||
// SFNodeGetUTXO is a flag used to indicate a peer supports the
|
|
||||||
// getutxos and utxos commands (BIP0064).
|
|
||||||
SFNodeGetUTXO
|
|
||||||
|
|
||||||
// SFNodeBloom is a flag used to indicate a peer supports bloom
|
|
||||||
// filtering.
|
|
||||||
SFNodeBloom
|
|
||||||
|
|
||||||
// SFNodeWitness is a flag used to indicate a peer supports blocks
|
|
||||||
// and transactions including witness data (BIP0144).
|
|
||||||
SFNodeWitness
|
|
||||||
|
|
||||||
// SFNodeXthin is a flag used to indicate a peer supports xthin blocks.
|
|
||||||
SFNodeXthin
|
|
||||||
|
|
||||||
// SFNodeBit5 is a flag used to indicate a peer supports a service
|
|
||||||
// defined by bit 5.
|
|
||||||
SFNodeBit5
|
|
||||||
|
|
||||||
// SFNodeCF is a flag used to indicate a peer supports committed
|
|
||||||
// filters (CFs).
|
|
||||||
SFNodeCF
|
|
||||||
|
|
||||||
// SFNode2X is a flag used to indicate a peer is running the Segwit2X
|
|
||||||
// software.
|
|
||||||
SFNode2X
|
|
||||||
)
|
|
||||||
|
|
||||||
// Map of service flags back to their constant names for pretty printing.
|
|
||||||
var sfStrings = map[ServiceFlag]string{
|
|
||||||
SFNodeNetwork: "SFNodeNetwork",
|
|
||||||
SFNodeGetUTXO: "SFNodeGetUTXO",
|
|
||||||
SFNodeBloom: "SFNodeBloom",
|
|
||||||
SFNodeWitness: "SFNodeWitness",
|
|
||||||
SFNodeXthin: "SFNodeXthin",
|
|
||||||
SFNodeBit5: "SFNodeBit5",
|
|
||||||
SFNodeCF: "SFNodeCF",
|
|
||||||
SFNode2X: "SFNode2X",
|
|
||||||
}
|
|
||||||
|
|
||||||
// orderedSFStrings is an ordered list of service flags from highest to
|
|
||||||
// lowest.
|
|
||||||
var orderedSFStrings = []ServiceFlag{
|
|
||||||
SFNodeNetwork,
|
|
||||||
SFNodeGetUTXO,
|
|
||||||
SFNodeBloom,
|
|
||||||
SFNodeWitness,
|
|
||||||
SFNodeXthin,
|
|
||||||
SFNodeBit5,
|
|
||||||
SFNodeCF,
|
|
||||||
SFNode2X,
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the ServiceFlag in human-readable form.
|
|
||||||
func (f ServiceFlag) String() string {
|
|
||||||
// No flags are set.
|
|
||||||
if f == 0 {
|
|
||||||
return "0x0"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add individual bit flags.
|
|
||||||
s := ""
|
|
||||||
for _, flag := range orderedSFStrings {
|
|
||||||
if f&flag == flag {
|
|
||||||
s += sfStrings[flag] + "|"
|
|
||||||
f -= flag
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add any remaining flags which aren't accounted for as hex.
|
|
||||||
s = strings.TrimRight(s, "|")
|
|
||||||
if f != 0 {
|
|
||||||
s += "|0x" + strconv.FormatUint(uint64(f), 16)
|
|
||||||
}
|
|
||||||
s = strings.TrimLeft(s, "|")
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// BitcoinNet represents which bitcoin network a message belongs to.
|
|
||||||
type BitcoinNet uint32
|
|
||||||
|
|
||||||
// Constants used to indicate the message bitcoin network. They can also be
|
|
||||||
// used to seek to the next message when a stream's state is unknown, but
|
|
||||||
// this package does not provide that functionality since it's generally a
|
|
||||||
// better idea to simply disconnect clients that are misbehaving over TCP.
|
|
||||||
const (
|
|
||||||
// MainNet represents the main bitcoin network.
|
|
||||||
MainNet BitcoinNet = 0xd9b4bef9
|
|
||||||
|
|
||||||
// TestNet represents the regression test network.
|
|
||||||
TestNet BitcoinNet = 0xdab5bffa
|
|
||||||
|
|
||||||
// TestNet3 represents the test network (version 3).
|
|
||||||
TestNet3 BitcoinNet = 0x0709110b
|
|
||||||
|
|
||||||
// SimNet represents the simulation test network.
|
|
||||||
SimNet BitcoinNet = 0x12141c16
|
|
||||||
)
|
|
||||||
|
|
||||||
// bnStrings is a map of bitcoin networks back to their constant names for
|
|
||||||
// pretty printing.
|
|
||||||
var bnStrings = map[BitcoinNet]string{
|
|
||||||
MainNet: "MainNet",
|
|
||||||
TestNet: "TestNet",
|
|
||||||
TestNet3: "TestNet3",
|
|
||||||
SimNet: "SimNet",
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the BitcoinNet in human-readable form.
|
|
||||||
func (n BitcoinNet) String() string {
|
|
||||||
if s, ok := bnStrings[n]; ok {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("Unknown BitcoinNet (%d)", uint32(n))
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
# Temp files
|
|
||||||
*~
|
|
||||||
|
|
||||||
# Log files
|
|
||||||
*.log
|
|
||||||
|
|
||||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
|
||||||
*.o
|
|
||||||
*.a
|
|
||||||
*.so
|
|
||||||
|
|
||||||
# Folders
|
|
||||||
_obj
|
|
||||||
_test
|
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
|
||||||
*.[568vq]
|
|
||||||
[568vq].out
|
|
||||||
|
|
||||||
*.cgo1.go
|
|
||||||
*.cgo2.c
|
|
||||||
_cgo_defun.c
|
|
||||||
_cgo_gotypes.go
|
|
||||||
_cgo_export.*
|
|
||||||
|
|
||||||
_testmain.go
|
|
||||||
|
|
||||||
*.exe
|
|
@ -1,13 +0,0 @@
|
|||||||
language: go
|
|
||||||
go:
|
|
||||||
- 1.7.x
|
|
||||||
- 1.8.x
|
|
||||||
sudo: false
|
|
||||||
install:
|
|
||||||
- go get -d -t -v ./...
|
|
||||||
- go get -v golang.org/x/tools/cmd/cover
|
|
||||||
- go get -v github.com/bradfitz/goimports
|
|
||||||
- go get -v github.com/golang/lint/golint
|
|
||||||
script:
|
|
||||||
- export PATH=$PATH:$HOME/gopath/bin
|
|
||||||
- ./goclean.sh
|
|
@ -1,15 +0,0 @@
|
|||||||
ISC License
|
|
||||||
|
|
||||||
Copyright (c) 2013-2014 Conformal Systems LLC.
|
|
||||||
|
|
||||||
Permission to use, copy, modify, and distribute this software for any
|
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
|
||||||
copyright notice and this permission notice appear in all copies.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@ -1,40 +0,0 @@
|
|||||||
btclog
|
|
||||||
======
|
|
||||||
|
|
||||||
[![Build Status](http://img.shields.io/travis/btcsuite/btclog.svg)](https://travis-ci.org/btcsuite/btclog)
|
|
||||||
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
|
||||||
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btclog)
|
|
||||||
|
|
||||||
Package btclog defines a logger interface and provides a default implementation
|
|
||||||
of a subsystem-aware leveled logger implementing the same interface.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get github.com/btcsuite/btclog
|
|
||||||
```
|
|
||||||
|
|
||||||
## GPG Verification Key
|
|
||||||
|
|
||||||
All official release tags are signed by Conformal so users can ensure the code
|
|
||||||
has not been tampered with and is coming from the btcsuite developers. To
|
|
||||||
verify the signature perform the following:
|
|
||||||
|
|
||||||
- Download the public key from the Conformal website at
|
|
||||||
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
|
|
||||||
|
|
||||||
- Import the public key into your GPG keyring:
|
|
||||||
```bash
|
|
||||||
gpg --import GIT-GPG-KEY-conformal.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
- Verify the release tag with the following command where `TAG_NAME` is a
|
|
||||||
placeholder for the specific tag:
|
|
||||||
```bash
|
|
||||||
git tag -v TAG_NAME
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Package btclog is licensed under the [copyfree](http://copyfree.org) ISC
|
|
||||||
License.
|
|
@ -1,27 +0,0 @@
|
|||||||
// Copyright (c) 2013-2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package btclog defines an interface and default implementation for subsystem
|
|
||||||
logging.
|
|
||||||
|
|
||||||
Log level verbosity may be modified at runtime for each individual subsystem
|
|
||||||
logger.
|
|
||||||
|
|
||||||
The default implementation in this package must be created by the Backend type.
|
|
||||||
Backends can write to any io.Writer, including multi-writers created by
|
|
||||||
io.MultiWriter. Multi-writers allow log output to be written to many writers,
|
|
||||||
including standard output and log files.
|
|
||||||
|
|
||||||
Optional logging behavior can be specified by using the LOGFLAGS environment
|
|
||||||
variable and overridden per-Backend by using the WithFlags call option. Multiple
|
|
||||||
LOGFLAGS options can be specified, separated by commas. The following options
|
|
||||||
are recognized:
|
|
||||||
|
|
||||||
longfile: Include the full filepath and line number in all log messages
|
|
||||||
|
|
||||||
shortfile: Include the filename and line number in all log messages.
|
|
||||||
Overrides longfile.
|
|
||||||
*/
|
|
||||||
package btclog
|
|
@ -1,37 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# The script does automatic checking on a Go package and its sub-packages, including:
|
|
||||||
# 1. gofmt (http://golang.org/cmd/gofmt/)
|
|
||||||
# 2. goimports (https://github.com/bradfitz/goimports)
|
|
||||||
# 3. golint (https://github.com/golang/lint)
|
|
||||||
# 4. go vet (http://golang.org/cmd/vet)
|
|
||||||
# 5. race detector (http://blog.golang.org/race-detector)
|
|
||||||
# 6. test coverage (http://blog.golang.org/cover)
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Automatic checks
|
|
||||||
test -z $(gofmt -l -w . | tee /dev/stderr)
|
|
||||||
test -z $(goimports -l -w . | tee /dev/stderr)
|
|
||||||
test -z $(golint ./... | tee /dev/stderr)
|
|
||||||
go vet ./...
|
|
||||||
env GORACE="halt_on_error=1" go test -v -race ./...
|
|
||||||
|
|
||||||
# Run test coverage on each subdirectories and merge the coverage profile.
|
|
||||||
|
|
||||||
echo "mode: count" > profile.cov
|
|
||||||
|
|
||||||
# Standard go tooling behavior is to ignore dirs with leading underscores.
|
|
||||||
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d)
|
|
||||||
do
|
|
||||||
if ls $dir/*.go &> /dev/null; then
|
|
||||||
go test -covermode=count -coverprofile=$dir/profile.tmp $dir
|
|
||||||
if [ -f $dir/profile.tmp ]; then
|
|
||||||
cat $dir/profile.tmp | tail -n +2 >> profile.cov
|
|
||||||
rm $dir/profile.tmp
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# To submit the test coverage result to coveralls.io,
|
|
||||||
# use goveralls (https://github.com/mattn/goveralls)
|
|
||||||
# goveralls -coverprofile=profile.cov -service=travis-ci
|
|
@ -1,64 +0,0 @@
|
|||||||
// Copyright (c) 2013-2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package btclog
|
|
||||||
|
|
||||||
// Logger is an interface which describes a level-based logger. A default
|
|
||||||
// implementation of Logger is implemented by this package and can be created
|
|
||||||
// by calling (*Backend).Logger.
|
|
||||||
type Logger interface {
|
|
||||||
// Tracef formats message according to format specifier and writes to
|
|
||||||
// to log with LevelTrace.
|
|
||||||
Tracef(format string, params ...interface{})
|
|
||||||
|
|
||||||
// Debugf formats message according to format specifier and writes to
|
|
||||||
// log with LevelDebug.
|
|
||||||
Debugf(format string, params ...interface{})
|
|
||||||
|
|
||||||
// Infof formats message according to format specifier and writes to
|
|
||||||
// log with LevelInfo.
|
|
||||||
Infof(format string, params ...interface{})
|
|
||||||
|
|
||||||
// Warnf formats message according to format specifier and writes to
|
|
||||||
// to log with LevelWarn.
|
|
||||||
Warnf(format string, params ...interface{})
|
|
||||||
|
|
||||||
// Errorf formats message according to format specifier and writes to
|
|
||||||
// to log with LevelError.
|
|
||||||
Errorf(format string, params ...interface{})
|
|
||||||
|
|
||||||
// Criticalf formats message according to format specifier and writes to
|
|
||||||
// log with LevelCritical.
|
|
||||||
Criticalf(format string, params ...interface{})
|
|
||||||
|
|
||||||
// Trace formats message using the default formats for its operands
|
|
||||||
// and writes to log with LevelTrace.
|
|
||||||
Trace(v ...interface{})
|
|
||||||
|
|
||||||
// Debug formats message using the default formats for its operands
|
|
||||||
// and writes to log with LevelDebug.
|
|
||||||
Debug(v ...interface{})
|
|
||||||
|
|
||||||
// Info formats message using the default formats for its operands
|
|
||||||
// and writes to log with LevelInfo.
|
|
||||||
Info(v ...interface{})
|
|
||||||
|
|
||||||
// Warn formats message using the default formats for its operands
|
|
||||||
// and writes to log with LevelWarn.
|
|
||||||
Warn(v ...interface{})
|
|
||||||
|
|
||||||
// Error formats message using the default formats for its operands
|
|
||||||
// and writes to log with LevelError.
|
|
||||||
Error(v ...interface{})
|
|
||||||
|
|
||||||
// Critical formats message using the default formats for its operands
|
|
||||||
// and writes to log with LevelCritical.
|
|
||||||
Critical(v ...interface{})
|
|
||||||
|
|
||||||
// Level returns the current logging level.
|
|
||||||
Level() Level
|
|
||||||
|
|
||||||
// SetLevel changes the logging level to the passed level.
|
|
||||||
SetLevel(level Level)
|
|
||||||
}
|
|
@ -1,480 +0,0 @@
|
|||||||
// Copyright (c) 2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2009 The Go Authors. All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions are
|
|
||||||
// met:
|
|
||||||
//
|
|
||||||
// * Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
// * Redistributions in binary form must reproduce the above
|
|
||||||
// copyright notice, this list of conditions and the following disclaimer
|
|
||||||
// in the documentation and/or other materials provided with the
|
|
||||||
// distribution.
|
|
||||||
// * Neither the name of Google Inc. nor the names of its
|
|
||||||
// contributors may be used to endorse or promote products derived from
|
|
||||||
// this software without specific prior written permission.
|
|
||||||
//
|
|
||||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
package btclog
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// defaultFlags specifies changes to the default logger behavior. It is set
|
|
||||||
// during package init and configured using the LOGFLAGS environment variable.
|
|
||||||
// New logger backends can override these default flags using WithFlags.
|
|
||||||
var defaultFlags uint32
|
|
||||||
|
|
||||||
// Flags to modify Backend's behavior.
|
|
||||||
const (
|
|
||||||
// Llongfile modifies the logger output to include full path and line number
|
|
||||||
// of the logging callsite, e.g. /a/b/c/main.go:123.
|
|
||||||
Llongfile uint32 = 1 << iota
|
|
||||||
|
|
||||||
// Lshortfile modifies the logger output to include filename and line number
|
|
||||||
// of the logging callsite, e.g. main.go:123. Overrides Llongfile.
|
|
||||||
Lshortfile
|
|
||||||
)
|
|
||||||
|
|
||||||
// Read logger flags from the LOGFLAGS environment variable. Multiple flags can
|
|
||||||
// be set at once, separated by commas.
|
|
||||||
func init() {
|
|
||||||
for _, f := range strings.Split(os.Getenv("LOGFLAGS"), ",") {
|
|
||||||
switch f {
|
|
||||||
case "longfile":
|
|
||||||
defaultFlags |= Llongfile
|
|
||||||
case "shortfile":
|
|
||||||
defaultFlags |= Lshortfile
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Level is the level at which a logger is configured. All messages sent
|
|
||||||
// to a level which is below the current level are filtered.
|
|
||||||
type Level uint32
|
|
||||||
|
|
||||||
// Level constants.
|
|
||||||
const (
|
|
||||||
LevelTrace Level = iota
|
|
||||||
LevelDebug
|
|
||||||
LevelInfo
|
|
||||||
LevelWarn
|
|
||||||
LevelError
|
|
||||||
LevelCritical
|
|
||||||
LevelOff
|
|
||||||
)
|
|
||||||
|
|
||||||
// levelStrs defines the human-readable names for each logging level.
|
|
||||||
var levelStrs = [...]string{"TRC", "DBG", "INF", "WRN", "ERR", "CRT", "OFF"}
|
|
||||||
|
|
||||||
// LevelFromString returns a level based on the input string s. If the input
|
|
||||||
// can't be interpreted as a valid log level, the info level and false is
|
|
||||||
// returned.
|
|
||||||
func LevelFromString(s string) (l Level, ok bool) {
|
|
||||||
switch strings.ToLower(s) {
|
|
||||||
case "trace", "trc":
|
|
||||||
return LevelTrace, true
|
|
||||||
case "debug", "dbg":
|
|
||||||
return LevelDebug, true
|
|
||||||
case "info", "inf":
|
|
||||||
return LevelInfo, true
|
|
||||||
case "warn", "wrn":
|
|
||||||
return LevelWarn, true
|
|
||||||
case "error", "err":
|
|
||||||
return LevelError, true
|
|
||||||
case "critical", "crt":
|
|
||||||
return LevelCritical, true
|
|
||||||
case "off":
|
|
||||||
return LevelOff, true
|
|
||||||
default:
|
|
||||||
return LevelInfo, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the tag of the logger used in log messages, or "OFF" if
|
|
||||||
// the level will not produce any log output.
|
|
||||||
func (l Level) String() string {
|
|
||||||
if l >= LevelOff {
|
|
||||||
return "OFF"
|
|
||||||
}
|
|
||||||
return levelStrs[l]
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBackend creates a logger backend from a Writer.
|
|
||||||
func NewBackend(w io.Writer, opts ...BackendOption) *Backend {
|
|
||||||
b := &Backend{w: w, flag: defaultFlags}
|
|
||||||
for _, o := range opts {
|
|
||||||
o(b)
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backend is a logging backend. Subsystems created from the backend write to
|
|
||||||
// the backend's Writer. Backend provides atomic writes to the Writer from all
|
|
||||||
// subsystems.
|
|
||||||
type Backend struct {
|
|
||||||
w io.Writer
|
|
||||||
mu sync.Mutex // ensures atomic writes
|
|
||||||
flag uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// BackendOption is a function used to modify the behavior of a Backend.
|
|
||||||
type BackendOption func(b *Backend)
|
|
||||||
|
|
||||||
// WithFlags configures a Backend to use the specified flags rather than using
|
|
||||||
// the package's defaults as determined through the LOGFLAGS environment
|
|
||||||
// variable.
|
|
||||||
func WithFlags(flags uint32) BackendOption {
|
|
||||||
return func(b *Backend) {
|
|
||||||
b.flag = flags
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// bufferPool defines a concurrent safe free list of byte slices used to provide
|
|
||||||
// temporary buffers for formatting log messages prior to outputting them.
|
|
||||||
var bufferPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
b := make([]byte, 0, 120)
|
|
||||||
return &b // pointer to slice to avoid boxing alloc
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// buffer returns a byte slice from the free list. A new buffer is allocated if
|
|
||||||
// there are not any available on the free list. The returned byte slice should
|
|
||||||
// be returned to the fee list by using the recycleBuffer function when the
|
|
||||||
// caller is done with it.
|
|
||||||
func buffer() *[]byte {
|
|
||||||
return bufferPool.Get().(*[]byte)
|
|
||||||
}
|
|
||||||
|
|
||||||
// recycleBuffer puts the provided byte slice, which should have been obtain via
|
|
||||||
// the buffer function, back on the free list.
|
|
||||||
func recycleBuffer(b *[]byte) {
|
|
||||||
*b = (*b)[:0]
|
|
||||||
bufferPool.Put(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// From stdlib log package.
|
|
||||||
// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid
|
|
||||||
// zero-padding.
|
|
||||||
func itoa(buf *[]byte, i int, wid int) {
|
|
||||||
// Assemble decimal in reverse order.
|
|
||||||
var b [20]byte
|
|
||||||
bp := len(b) - 1
|
|
||||||
for i >= 10 || wid > 1 {
|
|
||||||
wid--
|
|
||||||
q := i / 10
|
|
||||||
b[bp] = byte('0' + i - q*10)
|
|
||||||
bp--
|
|
||||||
i = q
|
|
||||||
}
|
|
||||||
// i < 10
|
|
||||||
b[bp] = byte('0' + i)
|
|
||||||
*buf = append(*buf, b[bp:]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appends a header in the default format 'YYYY-MM-DD hh:mm:ss.sss [LVL] TAG: '.
|
|
||||||
// If either of the Lshortfile or Llongfile flags are specified, the file named
|
|
||||||
// and line number are included after the tag and before the final colon.
|
|
||||||
func formatHeader(buf *[]byte, t time.Time, lvl, tag string, file string, line int) {
|
|
||||||
year, month, day := t.Date()
|
|
||||||
hour, min, sec := t.Clock()
|
|
||||||
ms := t.Nanosecond() / 1e6
|
|
||||||
|
|
||||||
itoa(buf, year, 4)
|
|
||||||
*buf = append(*buf, '-')
|
|
||||||
itoa(buf, int(month), 2)
|
|
||||||
*buf = append(*buf, '-')
|
|
||||||
itoa(buf, day, 2)
|
|
||||||
*buf = append(*buf, ' ')
|
|
||||||
itoa(buf, hour, 2)
|
|
||||||
*buf = append(*buf, ':')
|
|
||||||
itoa(buf, min, 2)
|
|
||||||
*buf = append(*buf, ':')
|
|
||||||
itoa(buf, sec, 2)
|
|
||||||
*buf = append(*buf, '.')
|
|
||||||
itoa(buf, ms, 3)
|
|
||||||
*buf = append(*buf, " ["...)
|
|
||||||
*buf = append(*buf, lvl...)
|
|
||||||
*buf = append(*buf, "] "...)
|
|
||||||
*buf = append(*buf, tag...)
|
|
||||||
if file != "" {
|
|
||||||
*buf = append(*buf, ' ')
|
|
||||||
*buf = append(*buf, file...)
|
|
||||||
*buf = append(*buf, ':')
|
|
||||||
itoa(buf, line, -1)
|
|
||||||
}
|
|
||||||
*buf = append(*buf, ": "...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// calldepth is the call depth of the callsite function relative to the
|
|
||||||
// caller of the subsystem logger. It is used to recover the filename and line
|
|
||||||
// number of the logging call if either the short or long file flags are
|
|
||||||
// specified.
|
|
||||||
const calldepth = 3
|
|
||||||
|
|
||||||
// callsite returns the file name and line number of the callsite to the
|
|
||||||
// subsystem logger.
|
|
||||||
func callsite(flag uint32) (string, int) {
|
|
||||||
_, file, line, ok := runtime.Caller(calldepth)
|
|
||||||
if !ok {
|
|
||||||
return "???", 0
|
|
||||||
}
|
|
||||||
if flag&Lshortfile != 0 {
|
|
||||||
short := file
|
|
||||||
for i := len(file) - 1; i > 0; i-- {
|
|
||||||
if os.IsPathSeparator(file[i]) {
|
|
||||||
short = file[i+1:]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file = short
|
|
||||||
}
|
|
||||||
return file, line
|
|
||||||
}
|
|
||||||
|
|
||||||
// print outputs a log message to the writer associated with the backend after
|
|
||||||
// creating a prefix for the given level and tag according to the formatHeader
|
|
||||||
// function and formatting the provided arguments using the default formatting
|
|
||||||
// rules.
|
|
||||||
func (b *Backend) print(lvl, tag string, args ...interface{}) {
|
|
||||||
t := time.Now() // get as early as possible
|
|
||||||
|
|
||||||
bytebuf := buffer()
|
|
||||||
|
|
||||||
var file string
|
|
||||||
var line int
|
|
||||||
if b.flag&(Lshortfile|Llongfile) != 0 {
|
|
||||||
file, line = callsite(b.flag)
|
|
||||||
}
|
|
||||||
|
|
||||||
formatHeader(bytebuf, t, lvl, tag, file, line)
|
|
||||||
buf := bytes.NewBuffer(*bytebuf)
|
|
||||||
fmt.Fprintln(buf, args...)
|
|
||||||
*bytebuf = buf.Bytes()
|
|
||||||
|
|
||||||
b.mu.Lock()
|
|
||||||
b.w.Write(*bytebuf)
|
|
||||||
b.mu.Unlock()
|
|
||||||
|
|
||||||
recycleBuffer(bytebuf)
|
|
||||||
}
|
|
||||||
|
|
||||||
// printf outputs a log message to the writer associated with the backend after
|
|
||||||
// creating a prefix for the given level and tag according to the formatHeader
|
|
||||||
// function and formatting the provided arguments according to the given format
|
|
||||||
// specifier.
|
|
||||||
func (b *Backend) printf(lvl, tag string, format string, args ...interface{}) {
|
|
||||||
t := time.Now() // get as early as possible
|
|
||||||
|
|
||||||
bytebuf := buffer()
|
|
||||||
|
|
||||||
var file string
|
|
||||||
var line int
|
|
||||||
if b.flag&(Lshortfile|Llongfile) != 0 {
|
|
||||||
file, line = callsite(b.flag)
|
|
||||||
}
|
|
||||||
|
|
||||||
formatHeader(bytebuf, t, lvl, tag, file, line)
|
|
||||||
buf := bytes.NewBuffer(*bytebuf)
|
|
||||||
fmt.Fprintf(buf, format, args...)
|
|
||||||
*bytebuf = append(buf.Bytes(), '\n')
|
|
||||||
|
|
||||||
b.mu.Lock()
|
|
||||||
b.w.Write(*bytebuf)
|
|
||||||
b.mu.Unlock()
|
|
||||||
|
|
||||||
recycleBuffer(bytebuf)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Logger returns a new logger for a particular subsystem that writes to the
|
|
||||||
// Backend b. A tag describes the subsystem and is included in all log
|
|
||||||
// messages. The logger uses the info verbosity level by default.
|
|
||||||
func (b *Backend) Logger(subsystemTag string) Logger {
|
|
||||||
return &slog{LevelInfo, subsystemTag, b}
|
|
||||||
}
|
|
||||||
|
|
||||||
// slog is a subsystem logger for a Backend. Implements the Logger interface.
|
|
||||||
type slog struct {
|
|
||||||
lvl Level // atomic
|
|
||||||
tag string
|
|
||||||
b *Backend
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trace formats message using the default formats for its operands, prepends
|
|
||||||
// the prefix as necessary, and writes to log with LevelTrace.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Trace(args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelTrace {
|
|
||||||
l.b.print("TRC", l.tag, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tracef formats message according to format specifier, prepends the prefix as
|
|
||||||
// necessary, and writes to log with LevelTrace.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Tracef(format string, args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelTrace {
|
|
||||||
l.b.printf("TRC", l.tag, format, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug formats message using the default formats for its operands, prepends
|
|
||||||
// the prefix as necessary, and writes to log with LevelDebug.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Debug(args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelDebug {
|
|
||||||
l.b.print("DBG", l.tag, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debugf formats message according to format specifier, prepends the prefix as
|
|
||||||
// necessary, and writes to log with LevelDebug.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Debugf(format string, args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelDebug {
|
|
||||||
l.b.printf("DBG", l.tag, format, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Info formats message using the default formats for its operands, prepends
|
|
||||||
// the prefix as necessary, and writes to log with LevelInfo.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Info(args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelInfo {
|
|
||||||
l.b.print("INF", l.tag, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Infof formats message according to format specifier, prepends the prefix as
|
|
||||||
// necessary, and writes to log with LevelInfo.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Infof(format string, args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelInfo {
|
|
||||||
l.b.printf("INF", l.tag, format, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warn formats message using the default formats for its operands, prepends
|
|
||||||
// the prefix as necessary, and writes to log with LevelWarn.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Warn(args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelWarn {
|
|
||||||
l.b.print("WRN", l.tag, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warnf formats message according to format specifier, prepends the prefix as
|
|
||||||
// necessary, and writes to log with LevelWarn.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Warnf(format string, args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelWarn {
|
|
||||||
l.b.printf("WRN", l.tag, format, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error formats message using the default formats for its operands, prepends
|
|
||||||
// the prefix as necessary, and writes to log with LevelError.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Error(args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelError {
|
|
||||||
l.b.print("ERR", l.tag, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Errorf formats message according to format specifier, prepends the prefix as
|
|
||||||
// necessary, and writes to log with LevelError.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Errorf(format string, args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelError {
|
|
||||||
l.b.printf("ERR", l.tag, format, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Critical formats message using the default formats for its operands, prepends
|
|
||||||
// the prefix as necessary, and writes to log with LevelCritical.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Critical(args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelCritical {
|
|
||||||
l.b.print("CRT", l.tag, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Criticalf formats message according to format specifier, prepends the prefix
|
|
||||||
// as necessary, and writes to log with LevelCritical.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Criticalf(format string, args ...interface{}) {
|
|
||||||
lvl := l.Level()
|
|
||||||
if lvl <= LevelCritical {
|
|
||||||
l.b.printf("CRT", l.tag, format, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Level returns the current logging level
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) Level() Level {
|
|
||||||
return Level(atomic.LoadUint32((*uint32)(&l.lvl)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLevel changes the logging level to the passed level.
|
|
||||||
//
|
|
||||||
// This is part of the Logger interface implementation.
|
|
||||||
func (l *slog) SetLevel(level Level) {
|
|
||||||
atomic.StoreUint32((*uint32)(&l.lvl), uint32(level))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disabled is a Logger that will never output anything.
|
|
||||||
var Disabled Logger
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
Disabled = &slog{lvl: LevelOff, b: NewBackend(ioutil.Discard)}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
ISC License
|
|
||||||
|
|
||||||
Copyright (c) 2013-2017 The btcsuite developers
|
|
||||||
Copyright (c) 2015-2016 The Decred developers
|
|
||||||
|
|
||||||
Permission to use, copy, modify, and distribute this software for any
|
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
|
||||||
copyright notice and this permission notice appear in all copies.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@ -1,17 +0,0 @@
|
|||||||
// Copyright (c) 2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package zero
|
|
||||||
|
|
||||||
// Bytea32 clears the 32-byte array by filling it with the zero value.
|
|
||||||
// This is used to explicitly clear private key material from memory.
|
|
||||||
func Bytea32(b *[32]byte) {
|
|
||||||
*b = [32]byte{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bytea64 clears the 64-byte array by filling it with the zero value.
|
|
||||||
// This is used to explicitly clear sensitive material from memory.
|
|
||||||
func Bytea64(b *[64]byte) {
|
|
||||||
*b = [64]byte{}
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
// Package zero contains functions to clear data from byte slices and
|
|
||||||
// multi-precision integers.
|
|
||||||
package zero
|
|
@ -1,34 +0,0 @@
|
|||||||
// Copyright (c) 2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// This file implements range-based zeroing, which as of Go 1.5 is
|
|
||||||
// optimized using a Duff's device.
|
|
||||||
|
|
||||||
package zero
|
|
||||||
|
|
||||||
import "math/big"
|
|
||||||
|
|
||||||
// Bytes sets all bytes in the passed slice to zero. This is used to
|
|
||||||
// explicitly clear private key material from memory.
|
|
||||||
//
|
|
||||||
// In general, prefer to use the fixed-sized zeroing functions (Bytea*)
|
|
||||||
// when zeroing bytes as they are much more efficient than the variable
|
|
||||||
// sized zeroing func Bytes.
|
|
||||||
func Bytes(b []byte) {
|
|
||||||
for i := range b {
|
|
||||||
b[i] = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BigInt sets all bytes in the passed big int to zero and then sets the
|
|
||||||
// value to 0. This differs from simply setting the value in that it
|
|
||||||
// specifically clears the underlying bytes whereas simply setting the value
|
|
||||||
// does not. This is mostly useful to forcefully clear private keys.
|
|
||||||
func BigInt(x *big.Int) {
|
|
||||||
b := x.Bits()
|
|
||||||
for i := range b {
|
|
||||||
b[i] = 0
|
|
||||||
}
|
|
||||||
x.SetInt64(0)
|
|
||||||
}
|
|
@ -1,248 +0,0 @@
|
|||||||
// Copyright (c) 2014-2017 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package snacl
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
|
||||||
"crypto/subtle"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"runtime/debug"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcwallet/internal/zero"
|
|
||||||
"github.com/btcsuite/golangcrypto/nacl/secretbox"
|
|
||||||
"github.com/btcsuite/golangcrypto/scrypt"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
prng = rand.Reader
|
|
||||||
)
|
|
||||||
|
|
||||||
// Error types and messages.
|
|
||||||
var (
|
|
||||||
ErrInvalidPassword = errors.New("invalid password")
|
|
||||||
ErrMalformed = errors.New("malformed data")
|
|
||||||
ErrDecryptFailed = errors.New("unable to decrypt")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Various constants needed for encryption scheme.
|
|
||||||
const (
|
|
||||||
// Expose secretbox's Overhead const here for convenience.
|
|
||||||
Overhead = secretbox.Overhead
|
|
||||||
KeySize = 32
|
|
||||||
NonceSize = 24
|
|
||||||
DefaultN = 16384 // 2^14
|
|
||||||
DefaultR = 8
|
|
||||||
DefaultP = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
// CryptoKey represents a secret key which can be used to encrypt and decrypt
|
|
||||||
// data.
|
|
||||||
type CryptoKey [KeySize]byte
|
|
||||||
|
|
||||||
// Encrypt encrypts the passed data.
|
|
||||||
func (ck *CryptoKey) Encrypt(in []byte) ([]byte, error) {
|
|
||||||
var nonce [NonceSize]byte
|
|
||||||
_, err := io.ReadFull(prng, nonce[:])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
blob := secretbox.Seal(nil, in, &nonce, (*[KeySize]byte)(ck))
|
|
||||||
return append(nonce[:], blob...), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrypt decrypts the passed data. The must be the output of the Encrypt
|
|
||||||
// function.
|
|
||||||
func (ck *CryptoKey) Decrypt(in []byte) ([]byte, error) {
|
|
||||||
if len(in) < NonceSize {
|
|
||||||
return nil, ErrMalformed
|
|
||||||
}
|
|
||||||
|
|
||||||
var nonce [NonceSize]byte
|
|
||||||
copy(nonce[:], in[:NonceSize])
|
|
||||||
blob := in[NonceSize:]
|
|
||||||
|
|
||||||
opened, ok := secretbox.Open(nil, blob, &nonce, (*[KeySize]byte)(ck))
|
|
||||||
if !ok {
|
|
||||||
return nil, ErrDecryptFailed
|
|
||||||
}
|
|
||||||
|
|
||||||
return opened, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zero clears the key by manually zeroing all memory. This is for security
|
|
||||||
// conscience application which wish to zero the memory after they've used it
|
|
||||||
// rather than waiting until it's reclaimed by the garbage collector. The
|
|
||||||
// key is no longer usable after this call.
|
|
||||||
func (ck *CryptoKey) Zero() {
|
|
||||||
zero.Bytea32((*[KeySize]byte)(ck))
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateCryptoKey generates a new crypotgraphically random key.
|
|
||||||
func GenerateCryptoKey() (*CryptoKey, error) {
|
|
||||||
var key CryptoKey
|
|
||||||
_, err := io.ReadFull(prng, key[:])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &key, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parameters are not secret and can be stored in plain text.
|
|
||||||
type Parameters struct {
|
|
||||||
Salt [KeySize]byte
|
|
||||||
Digest [sha256.Size]byte
|
|
||||||
N int
|
|
||||||
R int
|
|
||||||
P int
|
|
||||||
}
|
|
||||||
|
|
||||||
// SecretKey houses a crypto key and the parameters needed to derive it from a
|
|
||||||
// passphrase. It should only be used in memory.
|
|
||||||
type SecretKey struct {
|
|
||||||
Key *CryptoKey
|
|
||||||
Parameters Parameters
|
|
||||||
}
|
|
||||||
|
|
||||||
// deriveKey fills out the Key field.
|
|
||||||
func (sk *SecretKey) deriveKey(password *[]byte) error {
|
|
||||||
key, err := scrypt.Key(*password, sk.Parameters.Salt[:],
|
|
||||||
sk.Parameters.N,
|
|
||||||
sk.Parameters.R,
|
|
||||||
sk.Parameters.P,
|
|
||||||
len(sk.Key))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
copy(sk.Key[:], key)
|
|
||||||
zero.Bytes(key)
|
|
||||||
|
|
||||||
// I'm not a fan of forced garbage collections, but scrypt allocates a
|
|
||||||
// ton of memory and calling it back to back without a GC cycle in
|
|
||||||
// between means you end up needing twice the amount of memory. For
|
|
||||||
// example, if your scrypt parameters are such that you require 1GB and
|
|
||||||
// you call it twice in a row, without this you end up allocating 2GB
|
|
||||||
// since the first GB probably hasn't been released yet.
|
|
||||||
debug.FreeOSMemory()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marshal returns the Parameters field marshalled into a format suitable for
|
|
||||||
// storage. This result of this can be stored in clear text.
|
|
||||||
func (sk *SecretKey) Marshal() []byte {
|
|
||||||
params := &sk.Parameters
|
|
||||||
|
|
||||||
// The marshalled format for the the params is as follows:
|
|
||||||
// <salt><digest><N><R><P>
|
|
||||||
//
|
|
||||||
// KeySize + sha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
|
|
||||||
marshalled := make([]byte, KeySize+sha256.Size+24)
|
|
||||||
|
|
||||||
b := marshalled
|
|
||||||
copy(b[:KeySize], params.Salt[:])
|
|
||||||
b = b[KeySize:]
|
|
||||||
copy(b[:sha256.Size], params.Digest[:])
|
|
||||||
b = b[sha256.Size:]
|
|
||||||
binary.LittleEndian.PutUint64(b[:8], uint64(params.N))
|
|
||||||
b = b[8:]
|
|
||||||
binary.LittleEndian.PutUint64(b[:8], uint64(params.R))
|
|
||||||
b = b[8:]
|
|
||||||
binary.LittleEndian.PutUint64(b[:8], uint64(params.P))
|
|
||||||
|
|
||||||
return marshalled
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshal unmarshalls the parameters needed to derive the secret key from a
|
|
||||||
// passphrase into sk.
|
|
||||||
func (sk *SecretKey) Unmarshal(marshalled []byte) error {
|
|
||||||
if sk.Key == nil {
|
|
||||||
sk.Key = (*CryptoKey)(&[KeySize]byte{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// The marshalled format for the the params is as follows:
|
|
||||||
// <salt><digest><N><R><P>
|
|
||||||
//
|
|
||||||
// KeySize + sha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
|
|
||||||
if len(marshalled) != KeySize+sha256.Size+24 {
|
|
||||||
return ErrMalformed
|
|
||||||
}
|
|
||||||
|
|
||||||
params := &sk.Parameters
|
|
||||||
copy(params.Salt[:], marshalled[:KeySize])
|
|
||||||
marshalled = marshalled[KeySize:]
|
|
||||||
copy(params.Digest[:], marshalled[:sha256.Size])
|
|
||||||
marshalled = marshalled[sha256.Size:]
|
|
||||||
params.N = int(binary.LittleEndian.Uint64(marshalled[:8]))
|
|
||||||
marshalled = marshalled[8:]
|
|
||||||
params.R = int(binary.LittleEndian.Uint64(marshalled[:8]))
|
|
||||||
marshalled = marshalled[8:]
|
|
||||||
params.P = int(binary.LittleEndian.Uint64(marshalled[:8]))
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zero zeroes the underlying secret key while leaving the parameters intact.
|
|
||||||
// This effectively makes the key unusable until it is derived again via the
|
|
||||||
// DeriveKey function.
|
|
||||||
func (sk *SecretKey) Zero() {
|
|
||||||
sk.Key.Zero()
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeriveKey derives the underlying secret key and ensures it matches the
|
|
||||||
// expected digest. This should only be called after previously calling the
|
|
||||||
// Zero function or on an initial Unmarshal.
|
|
||||||
func (sk *SecretKey) DeriveKey(password *[]byte) error {
|
|
||||||
if err := sk.deriveKey(password); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// verify password
|
|
||||||
digest := sha256.Sum256(sk.Key[:])
|
|
||||||
if subtle.ConstantTimeCompare(digest[:], sk.Parameters.Digest[:]) != 1 {
|
|
||||||
return ErrInvalidPassword
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encrypt encrypts in bytes and returns a JSON blob.
|
|
||||||
func (sk *SecretKey) Encrypt(in []byte) ([]byte, error) {
|
|
||||||
return sk.Key.Encrypt(in)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrypt takes in a JSON blob and returns it's decrypted form.
|
|
||||||
func (sk *SecretKey) Decrypt(in []byte) ([]byte, error) {
|
|
||||||
return sk.Key.Decrypt(in)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSecretKey returns a SecretKey structure based on the passed parameters.
|
|
||||||
func NewSecretKey(password *[]byte, N, r, p int) (*SecretKey, error) {
|
|
||||||
sk := SecretKey{
|
|
||||||
Key: (*CryptoKey)(&[KeySize]byte{}),
|
|
||||||
}
|
|
||||||
// setup parameters
|
|
||||||
sk.Parameters.N = N
|
|
||||||
sk.Parameters.R = r
|
|
||||||
sk.Parameters.P = p
|
|
||||||
_, err := io.ReadFull(prng, sk.Parameters.Salt[:])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// derive key
|
|
||||||
err = sk.deriveKey(password)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// store digest
|
|
||||||
sk.Parameters.Digest = sha256.Sum256(sk.Key[:])
|
|
||||||
|
|
||||||
return &sk, nil
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
# This source code refers to The Go Authors for copyright purposes.
|
|
||||||
# The master list of authors is in the main Go distribution,
|
|
||||||
# visible at http://tip.golang.org/AUTHORS.
|
|
@ -1,3 +0,0 @@
|
|||||||
# This source code was written by the Go contributors.
|
|
||||||
# The master list of contributors is in the main Go distribution,
|
|
||||||
# visible at http://tip.golang.org/CONTRIBUTORS.
|
|
@ -1,27 +0,0 @@
|
|||||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are
|
|
||||||
met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
* Redistributions in binary form must reproduce the above
|
|
||||||
copyright notice, this list of conditions and the following disclaimer
|
|
||||||
in the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
* Neither the name of Google Inc. nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -1,22 +0,0 @@
|
|||||||
Additional IP Rights Grant (Patents)
|
|
||||||
|
|
||||||
"This implementation" means the copyrightable works distributed by
|
|
||||||
Google as part of the Go project.
|
|
||||||
|
|
||||||
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
||||||
no-charge, royalty-free, irrevocable (except as stated in this section)
|
|
||||||
patent license to make, have made, use, offer to sell, sell, import,
|
|
||||||
transfer and otherwise run, modify and propagate the contents of this
|
|
||||||
implementation of Go, where such license applies only to those patent
|
|
||||||
claims, both currently owned or controlled by Google and acquired in
|
|
||||||
the future, licensable by Google that are necessarily infringed by this
|
|
||||||
implementation of Go. This grant does not include claims that would be
|
|
||||||
infringed only as a consequence of further modification of this
|
|
||||||
implementation. If you or your agent or exclusive licensee institute or
|
|
||||||
order or agree to the institution of patent litigation against any
|
|
||||||
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
|
||||||
that this implementation of Go or any code incorporated within this
|
|
||||||
implementation of Go constitutes direct or contributory patent
|
|
||||||
infringement, or inducement of patent infringement, then any patent
|
|
||||||
rights granted to you under this License for this implementation of Go
|
|
||||||
shall terminate as of the date such litigation is filed.
|
|
@ -1,149 +0,0 @@
|
|||||||
// Copyright 2012 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package secretbox encrypts and authenticates small messages.
|
|
||||||
|
|
||||||
Secretbox uses XSalsa20 and Poly1305 to encrypt and authenticate messages with
|
|
||||||
secret-key cryptography. The length of messages is not hidden.
|
|
||||||
|
|
||||||
It is the caller's responsibility to ensure the uniqueness of nonces—for
|
|
||||||
example, by using nonce 1 for the first message, nonce 2 for the second
|
|
||||||
message, etc. Nonces are long enough that randomly generated nonces have
|
|
||||||
negligible risk of collision.
|
|
||||||
|
|
||||||
This package is interoperable with NaCl: http://nacl.cr.yp.to/secretbox.html.
|
|
||||||
*/
|
|
||||||
package secretbox // import "github.com/btcsuite/golangcrypto/nacl/secretbox"
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/btcsuite/golangcrypto/poly1305"
|
|
||||||
"github.com/btcsuite/golangcrypto/salsa20/salsa"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Overhead is the number of bytes of overhead when boxing a message.
|
|
||||||
const Overhead = poly1305.TagSize
|
|
||||||
|
|
||||||
// setup produces a sub-key and Salsa20 counter given a nonce and key.
|
|
||||||
func setup(subKey *[32]byte, counter *[16]byte, nonce *[24]byte, key *[32]byte) {
|
|
||||||
// We use XSalsa20 for encryption so first we need to generate a
|
|
||||||
// key and nonce with HSalsa20.
|
|
||||||
var hNonce [16]byte
|
|
||||||
copy(hNonce[:], nonce[:])
|
|
||||||
salsa.HSalsa20(subKey, &hNonce, key, &salsa.Sigma)
|
|
||||||
|
|
||||||
// The final 8 bytes of the original nonce form the new nonce.
|
|
||||||
copy(counter[:], nonce[16:])
|
|
||||||
}
|
|
||||||
|
|
||||||
// sliceForAppend takes a slice and a requested number of bytes. It returns a
|
|
||||||
// slice with the contents of the given slice followed by that many bytes and a
|
|
||||||
// second slice that aliases into it and contains only the extra bytes. If the
|
|
||||||
// original slice has sufficient capacity then no allocation is performed.
|
|
||||||
func sliceForAppend(in []byte, n int) (head, tail []byte) {
|
|
||||||
if total := len(in) + n; cap(in) >= total {
|
|
||||||
head = in[:total]
|
|
||||||
} else {
|
|
||||||
head = make([]byte, total)
|
|
||||||
copy(head, in)
|
|
||||||
}
|
|
||||||
tail = head[len(in):]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Seal appends an encrypted and authenticated copy of message to out, which
|
|
||||||
// must not overlap message. The key and nonce pair must be unique for each
|
|
||||||
// distinct message and the output will be Overhead bytes longer than message.
|
|
||||||
func Seal(out, message []byte, nonce *[24]byte, key *[32]byte) []byte {
|
|
||||||
var subKey [32]byte
|
|
||||||
var counter [16]byte
|
|
||||||
setup(&subKey, &counter, nonce, key)
|
|
||||||
|
|
||||||
// The Poly1305 key is generated by encrypting 32 bytes of zeros. Since
|
|
||||||
// Salsa20 works with 64-byte blocks, we also generate 32 bytes of
|
|
||||||
// keystream as a side effect.
|
|
||||||
var firstBlock [64]byte
|
|
||||||
salsa.XORKeyStream(firstBlock[:], firstBlock[:], &counter, &subKey)
|
|
||||||
|
|
||||||
var poly1305Key [32]byte
|
|
||||||
copy(poly1305Key[:], firstBlock[:])
|
|
||||||
|
|
||||||
ret, out := sliceForAppend(out, len(message)+poly1305.TagSize)
|
|
||||||
|
|
||||||
// We XOR up to 32 bytes of message with the keystream generated from
|
|
||||||
// the first block.
|
|
||||||
firstMessageBlock := message
|
|
||||||
if len(firstMessageBlock) > 32 {
|
|
||||||
firstMessageBlock = firstMessageBlock[:32]
|
|
||||||
}
|
|
||||||
|
|
||||||
tagOut := out
|
|
||||||
out = out[poly1305.TagSize:]
|
|
||||||
for i, x := range firstMessageBlock {
|
|
||||||
out[i] = firstBlock[32+i] ^ x
|
|
||||||
}
|
|
||||||
message = message[len(firstMessageBlock):]
|
|
||||||
ciphertext := out
|
|
||||||
out = out[len(firstMessageBlock):]
|
|
||||||
|
|
||||||
// Now encrypt the rest.
|
|
||||||
counter[8] = 1
|
|
||||||
salsa.XORKeyStream(out, message, &counter, &subKey)
|
|
||||||
|
|
||||||
var tag [poly1305.TagSize]byte
|
|
||||||
poly1305.Sum(&tag, ciphertext, &poly1305Key)
|
|
||||||
copy(tagOut, tag[:])
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open authenticates and decrypts a box produced by Seal and appends the
|
|
||||||
// message to out, which must not overlap box. The output will be Overhead
|
|
||||||
// bytes smaller than box.
|
|
||||||
func Open(out []byte, box []byte, nonce *[24]byte, key *[32]byte) ([]byte, bool) {
|
|
||||||
if len(box) < Overhead {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
var subKey [32]byte
|
|
||||||
var counter [16]byte
|
|
||||||
setup(&subKey, &counter, nonce, key)
|
|
||||||
|
|
||||||
// The Poly1305 key is generated by encrypting 32 bytes of zeros. Since
|
|
||||||
// Salsa20 works with 64-byte blocks, we also generate 32 bytes of
|
|
||||||
// keystream as a side effect.
|
|
||||||
var firstBlock [64]byte
|
|
||||||
salsa.XORKeyStream(firstBlock[:], firstBlock[:], &counter, &subKey)
|
|
||||||
|
|
||||||
var poly1305Key [32]byte
|
|
||||||
copy(poly1305Key[:], firstBlock[:])
|
|
||||||
var tag [poly1305.TagSize]byte
|
|
||||||
copy(tag[:], box)
|
|
||||||
|
|
||||||
if !poly1305.Verify(&tag, box[poly1305.TagSize:], &poly1305Key) {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
ret, out := sliceForAppend(out, len(box)-Overhead)
|
|
||||||
|
|
||||||
// We XOR up to 32 bytes of box with the keystream generated from
|
|
||||||
// the first block.
|
|
||||||
box = box[Overhead:]
|
|
||||||
firstMessageBlock := box
|
|
||||||
if len(firstMessageBlock) > 32 {
|
|
||||||
firstMessageBlock = firstMessageBlock[:32]
|
|
||||||
}
|
|
||||||
for i, x := range firstMessageBlock {
|
|
||||||
out[i] = firstBlock[32+i] ^ x
|
|
||||||
}
|
|
||||||
|
|
||||||
box = box[len(firstMessageBlock):]
|
|
||||||
out = out[len(firstMessageBlock):]
|
|
||||||
|
|
||||||
// Now decrypt the rest.
|
|
||||||
counter[8] = 1
|
|
||||||
salsa.XORKeyStream(out, box, &counter, &subKey)
|
|
||||||
|
|
||||||
return ret, true
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
// Copyright 2012 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package pbkdf2 implements the key derivation function PBKDF2 as defined in RFC
|
|
||||||
2898 / PKCS #5 v2.0.
|
|
||||||
|
|
||||||
A key derivation function is useful when encrypting data based on a password
|
|
||||||
or any other not-fully-random data. It uses a pseudorandom function to derive
|
|
||||||
a secure encryption key based on the password.
|
|
||||||
|
|
||||||
While v2.0 of the standard defines only one pseudorandom function to use,
|
|
||||||
HMAC-SHA1, the drafted v2.1 specification allows use of all five FIPS Approved
|
|
||||||
Hash Functions SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512 for HMAC. To
|
|
||||||
choose, you can pass the `New` functions from the different SHA packages to
|
|
||||||
pbkdf2.Key.
|
|
||||||
*/
|
|
||||||
package pbkdf2 // import "github.com/btcsuite/golangcrypto/pbkdf2"
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/hmac"
|
|
||||||
"hash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Key derives a key from the password, salt and iteration count, returning a
|
|
||||||
// []byte of length keylen that can be used as cryptographic key. The key is
|
|
||||||
// derived based on the method described as PBKDF2 with the HMAC variant using
|
|
||||||
// the supplied hash function.
|
|
||||||
//
|
|
||||||
// For example, to use a HMAC-SHA-1 based PBKDF2 key derivation function, you
|
|
||||||
// can get a derived key for e.g. AES-256 (which needs a 32-byte key) by
|
|
||||||
// doing:
|
|
||||||
//
|
|
||||||
// dk := pbkdf2.Key([]byte("some password"), salt, 4096, 32, sha1.New)
|
|
||||||
//
|
|
||||||
// Remember to get a good random salt. At least 8 bytes is recommended by the
|
|
||||||
// RFC.
|
|
||||||
//
|
|
||||||
// Using a higher iteration count will increase the cost of an exhaustive
|
|
||||||
// search but will also make derivation proportionally slower.
|
|
||||||
func Key(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
|
|
||||||
prf := hmac.New(h, password)
|
|
||||||
hashLen := prf.Size()
|
|
||||||
numBlocks := (keyLen + hashLen - 1) / hashLen
|
|
||||||
|
|
||||||
var buf [4]byte
|
|
||||||
dk := make([]byte, 0, numBlocks*hashLen)
|
|
||||||
U := make([]byte, hashLen)
|
|
||||||
for block := 1; block <= numBlocks; block++ {
|
|
||||||
// N.B.: || means concatenation, ^ means XOR
|
|
||||||
// for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
|
|
||||||
// U_1 = PRF(password, salt || uint(i))
|
|
||||||
prf.Reset()
|
|
||||||
prf.Write(salt)
|
|
||||||
buf[0] = byte(block >> 24)
|
|
||||||
buf[1] = byte(block >> 16)
|
|
||||||
buf[2] = byte(block >> 8)
|
|
||||||
buf[3] = byte(block)
|
|
||||||
prf.Write(buf[:4])
|
|
||||||
dk = prf.Sum(dk)
|
|
||||||
T := dk[len(dk)-hashLen:]
|
|
||||||
copy(U, T)
|
|
||||||
|
|
||||||
// U_n = PRF(password, U_(n-1))
|
|
||||||
for n := 2; n <= iter; n++ {
|
|
||||||
prf.Reset()
|
|
||||||
prf.Write(U)
|
|
||||||
U = U[:0]
|
|
||||||
U = prf.Sum(U)
|
|
||||||
for x := range U {
|
|
||||||
T[x] ^= U[x]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dk[:keyLen]
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
// Copyright 2012 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// This code was translated into a form compatible with 6a from the public
|
|
||||||
// domain sources in SUPERCOP: http://bench.cr.yp.to/supercop.html
|
|
||||||
|
|
||||||
// +build amd64,!gccgo
|
|
||||||
|
|
||||||
DATA ·SCALE(SB)/8, $0x37F4000000000000
|
|
||||||
GLOBL ·SCALE(SB), 8, $8
|
|
||||||
DATA ·TWO32(SB)/8, $0x41F0000000000000
|
|
||||||
GLOBL ·TWO32(SB), 8, $8
|
|
||||||
DATA ·TWO64(SB)/8, $0x43F0000000000000
|
|
||||||
GLOBL ·TWO64(SB), 8, $8
|
|
||||||
DATA ·TWO96(SB)/8, $0x45F0000000000000
|
|
||||||
GLOBL ·TWO96(SB), 8, $8
|
|
||||||
DATA ·ALPHA32(SB)/8, $0x45E8000000000000
|
|
||||||
GLOBL ·ALPHA32(SB), 8, $8
|
|
||||||
DATA ·ALPHA64(SB)/8, $0x47E8000000000000
|
|
||||||
GLOBL ·ALPHA64(SB), 8, $8
|
|
||||||
DATA ·ALPHA96(SB)/8, $0x49E8000000000000
|
|
||||||
GLOBL ·ALPHA96(SB), 8, $8
|
|
||||||
DATA ·ALPHA130(SB)/8, $0x4C08000000000000
|
|
||||||
GLOBL ·ALPHA130(SB), 8, $8
|
|
||||||
DATA ·DOFFSET0(SB)/8, $0x4330000000000000
|
|
||||||
GLOBL ·DOFFSET0(SB), 8, $8
|
|
||||||
DATA ·DOFFSET1(SB)/8, $0x4530000000000000
|
|
||||||
GLOBL ·DOFFSET1(SB), 8, $8
|
|
||||||
DATA ·DOFFSET2(SB)/8, $0x4730000000000000
|
|
||||||
GLOBL ·DOFFSET2(SB), 8, $8
|
|
||||||
DATA ·DOFFSET3(SB)/8, $0x4930000000000000
|
|
||||||
GLOBL ·DOFFSET3(SB), 8, $8
|
|
||||||
DATA ·DOFFSET3MINUSTWO128(SB)/8, $0x492FFFFE00000000
|
|
||||||
GLOBL ·DOFFSET3MINUSTWO128(SB), 8, $8
|
|
||||||
DATA ·HOFFSET0(SB)/8, $0x43300001FFFFFFFB
|
|
||||||
GLOBL ·HOFFSET0(SB), 8, $8
|
|
||||||
DATA ·HOFFSET1(SB)/8, $0x45300001FFFFFFFE
|
|
||||||
GLOBL ·HOFFSET1(SB), 8, $8
|
|
||||||
DATA ·HOFFSET2(SB)/8, $0x47300001FFFFFFFE
|
|
||||||
GLOBL ·HOFFSET2(SB), 8, $8
|
|
||||||
DATA ·HOFFSET3(SB)/8, $0x49300003FFFFFFFE
|
|
||||||
GLOBL ·HOFFSET3(SB), 8, $8
|
|
||||||
DATA ·ROUNDING(SB)/2, $0x137f
|
|
||||||
GLOBL ·ROUNDING(SB), 8, $2
|
|
@ -1,32 +0,0 @@
|
|||||||
// Copyright 2012 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package poly1305 implements Poly1305 one-time message authentication code as specified in http://cr.yp.to/mac/poly1305-20050329.pdf.
|
|
||||||
|
|
||||||
Poly1305 is a fast, one-time authentication function. It is infeasible for an
|
|
||||||
attacker to generate an authenticator for a message without the key. However, a
|
|
||||||
key must only be used for a single message. Authenticating two different
|
|
||||||
messages with the same key allows an attacker to forge authenticators for other
|
|
||||||
messages with the same key.
|
|
||||||
|
|
||||||
Poly1305 was originally coupled with AES in order to make Poly1305-AES. AES was
|
|
||||||
used with a fixed key in order to generate one-time keys from an nonce.
|
|
||||||
However, in this package AES isn't used and the one-time key is specified
|
|
||||||
directly.
|
|
||||||
*/
|
|
||||||
package poly1305 // import "github.com/btcsuite/golangcrypto/poly1305"
|
|
||||||
|
|
||||||
import "crypto/subtle"
|
|
||||||
|
|
||||||
// TagSize is the size, in bytes, of a poly1305 authenticator.
|
|
||||||
const TagSize = 16
|
|
||||||
|
|
||||||
// Verify returns true if mac is a valid authenticator for m with the given
|
|
||||||
// key.
|
|
||||||
func Verify(mac *[16]byte, m []byte, key *[32]byte) bool {
|
|
||||||
var tmp [16]byte
|
|
||||||
Sum(&tmp, m, key)
|
|
||||||
return subtle.ConstantTimeCompare(tmp[:], mac[:]) == 1
|
|
||||||
}
|
|
@ -1,497 +0,0 @@
|
|||||||
// Copyright 2012 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// This code was translated into a form compatible with 6a from the public
|
|
||||||
// domain sources in SUPERCOP: http://bench.cr.yp.to/supercop.html
|
|
||||||
|
|
||||||
// +build amd64,!gccgo
|
|
||||||
|
|
||||||
// func poly1305(out *[16]byte, m *byte, mlen uint64, key *[32]key)
|
|
||||||
TEXT ·poly1305(SB),0,$224-32
|
|
||||||
MOVQ out+0(FP),DI
|
|
||||||
MOVQ m+8(FP),SI
|
|
||||||
MOVQ mlen+16(FP),DX
|
|
||||||
MOVQ key+24(FP),CX
|
|
||||||
|
|
||||||
MOVQ SP,R11
|
|
||||||
MOVQ $31,R9
|
|
||||||
NOTQ R9
|
|
||||||
ANDQ R9,SP
|
|
||||||
ADDQ $32,SP
|
|
||||||
|
|
||||||
MOVQ R11,32(SP)
|
|
||||||
MOVQ R12,40(SP)
|
|
||||||
MOVQ R13,48(SP)
|
|
||||||
MOVQ R14,56(SP)
|
|
||||||
MOVQ R15,64(SP)
|
|
||||||
MOVQ BX,72(SP)
|
|
||||||
MOVQ BP,80(SP)
|
|
||||||
FLDCW ·ROUNDING(SB)
|
|
||||||
MOVL 0(CX),R8
|
|
||||||
MOVL 4(CX),R9
|
|
||||||
MOVL 8(CX),AX
|
|
||||||
MOVL 12(CX),R10
|
|
||||||
MOVQ DI,88(SP)
|
|
||||||
MOVQ CX,96(SP)
|
|
||||||
MOVL $0X43300000,108(SP)
|
|
||||||
MOVL $0X45300000,116(SP)
|
|
||||||
MOVL $0X47300000,124(SP)
|
|
||||||
MOVL $0X49300000,132(SP)
|
|
||||||
ANDL $0X0FFFFFFF,R8
|
|
||||||
ANDL $0X0FFFFFFC,R9
|
|
||||||
ANDL $0X0FFFFFFC,AX
|
|
||||||
ANDL $0X0FFFFFFC,R10
|
|
||||||
MOVL R8,104(SP)
|
|
||||||
MOVL R9,112(SP)
|
|
||||||
MOVL AX,120(SP)
|
|
||||||
MOVL R10,128(SP)
|
|
||||||
FMOVD 104(SP), F0
|
|
||||||
FSUBD ·DOFFSET0(SB), F0
|
|
||||||
FMOVD 112(SP), F0
|
|
||||||
FSUBD ·DOFFSET1(SB), F0
|
|
||||||
FMOVD 120(SP), F0
|
|
||||||
FSUBD ·DOFFSET2(SB), F0
|
|
||||||
FMOVD 128(SP), F0
|
|
||||||
FSUBD ·DOFFSET3(SB), F0
|
|
||||||
FXCHD F0, F3
|
|
||||||
FMOVDP F0, 136(SP)
|
|
||||||
FXCHD F0, F1
|
|
||||||
FMOVD F0, 144(SP)
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVDP F0, 152(SP)
|
|
||||||
FMOVD F0, 160(SP)
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVDP F0, 168(SP)
|
|
||||||
FMOVD F0, 176(SP)
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVDP F0, 184(SP)
|
|
||||||
FLDZ
|
|
||||||
FLDZ
|
|
||||||
FLDZ
|
|
||||||
FLDZ
|
|
||||||
CMPQ DX,$16
|
|
||||||
JB ADDATMOST15BYTES
|
|
||||||
INITIALATLEAST16BYTES:
|
|
||||||
MOVL 12(SI),DI
|
|
||||||
MOVL 8(SI),CX
|
|
||||||
MOVL 4(SI),R8
|
|
||||||
MOVL 0(SI),R9
|
|
||||||
MOVL DI,128(SP)
|
|
||||||
MOVL CX,120(SP)
|
|
||||||
MOVL R8,112(SP)
|
|
||||||
MOVL R9,104(SP)
|
|
||||||
ADDQ $16,SI
|
|
||||||
SUBQ $16,DX
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDD 128(SP), F0
|
|
||||||
FSUBD ·DOFFSET3MINUSTWO128(SB), F0
|
|
||||||
FXCHD F0, F1
|
|
||||||
FADDD 112(SP), F0
|
|
||||||
FSUBD ·DOFFSET1(SB), F0
|
|
||||||
FXCHD F0, F2
|
|
||||||
FADDD 120(SP), F0
|
|
||||||
FSUBD ·DOFFSET2(SB), F0
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDD 104(SP), F0
|
|
||||||
FSUBD ·DOFFSET0(SB), F0
|
|
||||||
CMPQ DX,$16
|
|
||||||
JB MULTIPLYADDATMOST15BYTES
|
|
||||||
MULTIPLYADDATLEAST16BYTES:
|
|
||||||
MOVL 12(SI),DI
|
|
||||||
MOVL 8(SI),CX
|
|
||||||
MOVL 4(SI),R8
|
|
||||||
MOVL 0(SI),R9
|
|
||||||
MOVL DI,128(SP)
|
|
||||||
MOVL CX,120(SP)
|
|
||||||
MOVL R8,112(SP)
|
|
||||||
MOVL R9,104(SP)
|
|
||||||
ADDQ $16,SI
|
|
||||||
SUBQ $16,DX
|
|
||||||
FMOVD ·ALPHA130(SB), F0
|
|
||||||
FADDD F2,F0
|
|
||||||
FSUBD ·ALPHA130(SB), F0
|
|
||||||
FSUBD F0,F2
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVD ·ALPHA32(SB), F0
|
|
||||||
FADDD F2,F0
|
|
||||||
FSUBD ·ALPHA32(SB), F0
|
|
||||||
FSUBD F0,F2
|
|
||||||
FXCHD F0, F2
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD ·ALPHA64(SB), F0
|
|
||||||
FADDD F4,F0
|
|
||||||
FSUBD ·ALPHA64(SB), F0
|
|
||||||
FSUBD F0,F4
|
|
||||||
FMOVD ·ALPHA96(SB), F0
|
|
||||||
FADDD F6,F0
|
|
||||||
FSUBD ·ALPHA96(SB), F0
|
|
||||||
FSUBD F0,F6
|
|
||||||
FXCHD F0, F6
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F5
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 176(SP), F0
|
|
||||||
FMULD F3,F0
|
|
||||||
FMOVD 160(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULDP F0,F6
|
|
||||||
FMOVD 160(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULDP F0,F4
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F5
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 168(SP), F0
|
|
||||||
FMULDP F0,F4
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F4
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F3
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FXCHD F0, F1
|
|
||||||
FMOVD 168(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 152(SP), F0
|
|
||||||
FMULDP F0,F5
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F1
|
|
||||||
CMPQ DX,$16
|
|
||||||
FXCHD F0, F2
|
|
||||||
FMOVD 128(SP), F0
|
|
||||||
FSUBD ·DOFFSET3MINUSTWO128(SB), F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F1
|
|
||||||
FMOVD 120(SP), F0
|
|
||||||
FSUBD ·DOFFSET2(SB), F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F3
|
|
||||||
FMOVD 112(SP), F0
|
|
||||||
FSUBD ·DOFFSET1(SB), F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F2
|
|
||||||
FMOVD 104(SP), F0
|
|
||||||
FSUBD ·DOFFSET0(SB), F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
JAE MULTIPLYADDATLEAST16BYTES
|
|
||||||
MULTIPLYADDATMOST15BYTES:
|
|
||||||
FMOVD ·ALPHA130(SB), F0
|
|
||||||
FADDD F2,F0
|
|
||||||
FSUBD ·ALPHA130(SB), F0
|
|
||||||
FSUBD F0,F2
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVD ·ALPHA32(SB), F0
|
|
||||||
FADDD F2,F0
|
|
||||||
FSUBD ·ALPHA32(SB), F0
|
|
||||||
FSUBD F0,F2
|
|
||||||
FMOVD ·ALPHA64(SB), F0
|
|
||||||
FADDD F5,F0
|
|
||||||
FSUBD ·ALPHA64(SB), F0
|
|
||||||
FSUBD F0,F5
|
|
||||||
FMOVD ·ALPHA96(SB), F0
|
|
||||||
FADDD F7,F0
|
|
||||||
FSUBD ·ALPHA96(SB), F0
|
|
||||||
FSUBD F0,F7
|
|
||||||
FXCHD F0, F7
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F5
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F5
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 176(SP), F0
|
|
||||||
FMULD F1,F0
|
|
||||||
FMOVD 160(SP), F0
|
|
||||||
FMULD F2,F0
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F3,F0
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULDP F0,F4
|
|
||||||
FMOVD 160(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULDP F0,F5
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F4
|
|
||||||
FMOVD 168(SP), F0
|
|
||||||
FMULDP F0,F5
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F4
|
|
||||||
FMOVD 168(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 152(SP), F0
|
|
||||||
FMULDP F0,F5
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F1
|
|
||||||
ADDATMOST15BYTES:
|
|
||||||
CMPQ DX,$0
|
|
||||||
JE NOMOREBYTES
|
|
||||||
MOVL $0,0(SP)
|
|
||||||
MOVL $0, 4 (SP)
|
|
||||||
MOVL $0, 8 (SP)
|
|
||||||
MOVL $0, 12 (SP)
|
|
||||||
LEAQ 0(SP),DI
|
|
||||||
MOVQ DX,CX
|
|
||||||
REP; MOVSB
|
|
||||||
MOVB $1,0(DI)
|
|
||||||
MOVL 12 (SP),DI
|
|
||||||
MOVL 8 (SP),SI
|
|
||||||
MOVL 4 (SP),DX
|
|
||||||
MOVL 0(SP),CX
|
|
||||||
MOVL DI,128(SP)
|
|
||||||
MOVL SI,120(SP)
|
|
||||||
MOVL DX,112(SP)
|
|
||||||
MOVL CX,104(SP)
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDD 128(SP), F0
|
|
||||||
FSUBD ·DOFFSET3(SB), F0
|
|
||||||
FXCHD F0, F2
|
|
||||||
FADDD 120(SP), F0
|
|
||||||
FSUBD ·DOFFSET2(SB), F0
|
|
||||||
FXCHD F0, F1
|
|
||||||
FADDD 112(SP), F0
|
|
||||||
FSUBD ·DOFFSET1(SB), F0
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDD 104(SP), F0
|
|
||||||
FSUBD ·DOFFSET0(SB), F0
|
|
||||||
FMOVD ·ALPHA130(SB), F0
|
|
||||||
FADDD F3,F0
|
|
||||||
FSUBD ·ALPHA130(SB), F0
|
|
||||||
FSUBD F0,F3
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVD ·ALPHA32(SB), F0
|
|
||||||
FADDD F2,F0
|
|
||||||
FSUBD ·ALPHA32(SB), F0
|
|
||||||
FSUBD F0,F2
|
|
||||||
FMOVD ·ALPHA64(SB), F0
|
|
||||||
FADDD F6,F0
|
|
||||||
FSUBD ·ALPHA64(SB), F0
|
|
||||||
FSUBD F0,F6
|
|
||||||
FMOVD ·ALPHA96(SB), F0
|
|
||||||
FADDD F5,F0
|
|
||||||
FSUBD ·ALPHA96(SB), F0
|
|
||||||
FSUBD F0,F5
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F3
|
|
||||||
FXCHD F0, F6
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F5
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 176(SP), F0
|
|
||||||
FMULD F3,F0
|
|
||||||
FMOVD 160(SP), F0
|
|
||||||
FMULD F4,F0
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULDP F0,F6
|
|
||||||
FMOVD 160(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F5,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULDP F0,F5
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F5
|
|
||||||
FMOVD 144(SP), F0
|
|
||||||
FMULD F6,F0
|
|
||||||
FADDDP F0,F2
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F6,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULD F6,F0
|
|
||||||
FADDDP F0,F4
|
|
||||||
FMOVD 168(SP), F0
|
|
||||||
FMULDP F0,F6
|
|
||||||
FXCHD F0, F5
|
|
||||||
FADDDP F0,F4
|
|
||||||
FMOVD 136(SP), F0
|
|
||||||
FMULD F2,F0
|
|
||||||
FADDDP F0,F1
|
|
||||||
FMOVD 184(SP), F0
|
|
||||||
FMULD F2,F0
|
|
||||||
FADDDP F0,F5
|
|
||||||
FMOVD 168(SP), F0
|
|
||||||
FMULD F2,F0
|
|
||||||
FADDDP F0,F3
|
|
||||||
FMOVD 152(SP), F0
|
|
||||||
FMULDP F0,F2
|
|
||||||
FXCHD F0, F1
|
|
||||||
FADDDP F0,F3
|
|
||||||
FXCHD F0, F3
|
|
||||||
FXCHD F0, F2
|
|
||||||
NOMOREBYTES:
|
|
||||||
MOVL $0,R10
|
|
||||||
FMOVD ·ALPHA130(SB), F0
|
|
||||||
FADDD F4,F0
|
|
||||||
FSUBD ·ALPHA130(SB), F0
|
|
||||||
FSUBD F0,F4
|
|
||||||
FMULD ·SCALE(SB), F0
|
|
||||||
FMOVD ·ALPHA32(SB), F0
|
|
||||||
FADDD F2,F0
|
|
||||||
FSUBD ·ALPHA32(SB), F0
|
|
||||||
FSUBD F0,F2
|
|
||||||
FMOVD ·ALPHA64(SB), F0
|
|
||||||
FADDD F4,F0
|
|
||||||
FSUBD ·ALPHA64(SB), F0
|
|
||||||
FSUBD F0,F4
|
|
||||||
FMOVD ·ALPHA96(SB), F0
|
|
||||||
FADDD F6,F0
|
|
||||||
FSUBD ·ALPHA96(SB), F0
|
|
||||||
FXCHD F0, F6
|
|
||||||
FSUBD F6,F0
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F3
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F1
|
|
||||||
FXCHD F0, F2
|
|
||||||
FADDDP F0,F3
|
|
||||||
FXCHD F0, F4
|
|
||||||
FADDDP F0,F3
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDD ·HOFFSET0(SB), F0
|
|
||||||
FXCHD F0, F3
|
|
||||||
FADDD ·HOFFSET1(SB), F0
|
|
||||||
FXCHD F0, F1
|
|
||||||
FADDD ·HOFFSET2(SB), F0
|
|
||||||
FXCHD F0, F2
|
|
||||||
FADDD ·HOFFSET3(SB), F0
|
|
||||||
FXCHD F0, F3
|
|
||||||
FMOVDP F0, 104(SP)
|
|
||||||
FMOVDP F0, 112(SP)
|
|
||||||
FMOVDP F0, 120(SP)
|
|
||||||
FMOVDP F0, 128(SP)
|
|
||||||
MOVL 108(SP),DI
|
|
||||||
ANDL $63,DI
|
|
||||||
MOVL 116(SP),SI
|
|
||||||
ANDL $63,SI
|
|
||||||
MOVL 124(SP),DX
|
|
||||||
ANDL $63,DX
|
|
||||||
MOVL 132(SP),CX
|
|
||||||
ANDL $63,CX
|
|
||||||
MOVL 112(SP),R8
|
|
||||||
ADDL DI,R8
|
|
||||||
MOVQ R8,112(SP)
|
|
||||||
MOVL 120(SP),DI
|
|
||||||
ADCL SI,DI
|
|
||||||
MOVQ DI,120(SP)
|
|
||||||
MOVL 128(SP),DI
|
|
||||||
ADCL DX,DI
|
|
||||||
MOVQ DI,128(SP)
|
|
||||||
MOVL R10,DI
|
|
||||||
ADCL CX,DI
|
|
||||||
MOVQ DI,136(SP)
|
|
||||||
MOVQ $5,DI
|
|
||||||
MOVL 104(SP),SI
|
|
||||||
ADDL SI,DI
|
|
||||||
MOVQ DI,104(SP)
|
|
||||||
MOVL R10,DI
|
|
||||||
MOVQ 112(SP),DX
|
|
||||||
ADCL DX,DI
|
|
||||||
MOVQ DI,112(SP)
|
|
||||||
MOVL R10,DI
|
|
||||||
MOVQ 120(SP),CX
|
|
||||||
ADCL CX,DI
|
|
||||||
MOVQ DI,120(SP)
|
|
||||||
MOVL R10,DI
|
|
||||||
MOVQ 128(SP),R8
|
|
||||||
ADCL R8,DI
|
|
||||||
MOVQ DI,128(SP)
|
|
||||||
MOVQ $0XFFFFFFFC,DI
|
|
||||||
MOVQ 136(SP),R9
|
|
||||||
ADCL R9,DI
|
|
||||||
SARL $16,DI
|
|
||||||
MOVQ DI,R9
|
|
||||||
XORL $0XFFFFFFFF,R9
|
|
||||||
ANDQ DI,SI
|
|
||||||
MOVQ 104(SP),AX
|
|
||||||
ANDQ R9,AX
|
|
||||||
ORQ AX,SI
|
|
||||||
ANDQ DI,DX
|
|
||||||
MOVQ 112(SP),AX
|
|
||||||
ANDQ R9,AX
|
|
||||||
ORQ AX,DX
|
|
||||||
ANDQ DI,CX
|
|
||||||
MOVQ 120(SP),AX
|
|
||||||
ANDQ R9,AX
|
|
||||||
ORQ AX,CX
|
|
||||||
ANDQ DI,R8
|
|
||||||
MOVQ 128(SP),DI
|
|
||||||
ANDQ R9,DI
|
|
||||||
ORQ DI,R8
|
|
||||||
MOVQ 88(SP),DI
|
|
||||||
MOVQ 96(SP),R9
|
|
||||||
ADDL 16(R9),SI
|
|
||||||
ADCL 20(R9),DX
|
|
||||||
ADCL 24(R9),CX
|
|
||||||
ADCL 28(R9),R8
|
|
||||||
MOVL SI,0(DI)
|
|
||||||
MOVL DX,4(DI)
|
|
||||||
MOVL CX,8(DI)
|
|
||||||
MOVL R8,12(DI)
|
|
||||||
MOVQ 32(SP),R11
|
|
||||||
MOVQ 40(SP),R12
|
|
||||||
MOVQ 48(SP),R13
|
|
||||||
MOVQ 56(SP),R14
|
|
||||||
MOVQ 64(SP),R15
|
|
||||||
MOVQ 72(SP),BX
|
|
||||||
MOVQ 80(SP),BP
|
|
||||||
MOVQ R11,SP
|
|
||||||
RET
|
|
@ -1,24 +0,0 @@
|
|||||||
// Copyright 2012 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build amd64,!gccgo
|
|
||||||
|
|
||||||
package poly1305
|
|
||||||
|
|
||||||
// This function is implemented in poly1305_amd64.s
|
|
||||||
|
|
||||||
//go:noescape
|
|
||||||
|
|
||||||
func poly1305(out *[16]byte, m *byte, mlen uint64, key *[32]byte)
|
|
||||||
|
|
||||||
// Sum generates an authenticator for m using a one-time key and puts the
|
|
||||||
// 16-byte result into out. Authenticating two different messages with the same
|
|
||||||
// key allows an attacker to forge messages at will.
|
|
||||||
func Sum(out *[16]byte, m []byte, key *[32]byte) {
|
|
||||||
var mPtr *byte
|
|
||||||
if len(m) > 0 {
|
|
||||||
mPtr = &m[0]
|
|
||||||
}
|
|
||||||
poly1305(out, mPtr, uint64(len(m)), key)
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue