├── .gitignore ├── btcctl.conf ├── Makefile ├── btcctl-wallet.conf ├── btcwallet.conf ├── btcd.conf ├── node ├── cmd_verack.go ├── cmd_pong.go ├── cmd_ping.go ├── cmd_tx.go ├── cmd_block.go ├── cmd_inv.go ├── url.go ├── cmd_version.go ├── mempool.go ├── url_test.go ├── peer.go └── node.go ├── go.mod ├── main.go ├── protocol ├── msg_verack.go ├── protocol.go ├── cmd_test.go ├── msg_getdata.go ├── msg_pingpong.go ├── msg_inv.go ├── cmd.go ├── netaddr.go ├── msg_version.go ├── msg_verack_test.go ├── varint.go ├── msg_test.go ├── msg_block_test.go ├── msg.go ├── msg_block.go ├── varint_test.go ├── msg_tx.go └── msg_tx_test.go ├── .github └── workflows │ └── go.yml ├── cmd ├── showmempool.go └── tinybit.go ├── rpc ├── client.go ├── rpc.go └── server.go ├── README.md ├── binary ├── marshaler.go ├── marshaler_test.go ├── unmarshaler.go └── unmarshaler_test.go └── go.sum /.gitignore: -------------------------------------------------------------------------------- 1 | btcd 2 | btcwallet 3 | tinybit 4 | -------------------------------------------------------------------------------- /btcctl.conf: -------------------------------------------------------------------------------- 1 | simnet=1 2 | rpcuser=woot 3 | rpcpass=woot -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | go test ./... 3 | 4 | cleanup: 5 | rm -rf btcd/data 6 | rm -rf btcwallet/simnet -------------------------------------------------------------------------------- /btcctl-wallet.conf: -------------------------------------------------------------------------------- 1 | simnet=1 2 | wallet=1 3 | rpcuser=woot 4 | rpcpass=woot 5 | rpccert=./btcwallet/rpc.cert -------------------------------------------------------------------------------- /btcwallet.conf: -------------------------------------------------------------------------------- 1 | simnet=1 2 | username=woot 3 | password=woot 4 | appdata=./btcwallet 5 | rpccert=./btcwallet/rpc.cert 6 | rpckey=./btcwallet/rpc.key -------------------------------------------------------------------------------- /btcd.conf: -------------------------------------------------------------------------------- 1 | [Application Options] 2 | datadir=./btcd/data 3 | 4 | rpcuser=woot 5 | rpcpass=woot 6 | listen=127.0.0.1:9333 7 | 8 | simnet=1 9 | 10 | nobanning=1 11 | 12 | debuglevel=debug -------------------------------------------------------------------------------- /node/cmd_verack.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/Jeiwan/tinybit/protocol" 7 | ) 8 | 9 | func (n Node) handleVerack(header *protocol.MessageHeader, conn io.ReadWriter) error { 10 | return nil 11 | } 12 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Jeiwan/tinybit 2 | 3 | go 1.12 4 | 5 | require ( 6 | github.com/google/go-cmp v0.3.1 7 | github.com/sirupsen/logrus v1.4.2 8 | github.com/spf13/cobra v0.0.5 9 | golang.org/x/sys v0.0.0-20190904154756-749cb33beabd // indirect 10 | ) 11 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/Jeiwan/tinybit/cmd" 7 | "github.com/sirupsen/logrus" 8 | ) 9 | 10 | func main() { 11 | if os.Getenv("DEBUG") != "" { 12 | logrus.SetLevel(logrus.DebugLevel) 13 | } 14 | 15 | cmd.Execute() 16 | } 17 | -------------------------------------------------------------------------------- /protocol/msg_verack.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | // NewVerackMsg returns a new 'verack' message. 4 | func NewVerackMsg(network string) (*Message, error) { 5 | msg, err := NewMessage("verack", network, []byte{}) 6 | if err != nil { 7 | return nil, err 8 | } 9 | 10 | return msg, nil 11 | } 12 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Go 2 | on: [push] 3 | jobs: 4 | build: 5 | name: Test 6 | runs-on: ubuntu-latest 7 | steps: 8 | 9 | - name: Set up Go 1.13 10 | uses: actions/setup-go@v1 11 | with: 12 | go-version: 1.13 13 | id: go 14 | 15 | - name: Check out code into the Go module directory 16 | uses: actions/checkout@v1 17 | 18 | - name: Test 19 | run: go test -v ./... -------------------------------------------------------------------------------- /node/cmd_pong.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/Jeiwan/tinybit/binary" 7 | "github.com/Jeiwan/tinybit/protocol" 8 | ) 9 | 10 | func (n Node) handlePong(header *protocol.MessageHeader, conn io.ReadWriter) error { 11 | var pong protocol.MsgPing 12 | 13 | lr := io.LimitReader(conn, int64(header.Length)) 14 | if err := binary.NewDecoder(lr).Decode(&pong); err != nil { 15 | return err 16 | } 17 | 18 | n.PongCh <- pong.Nonce 19 | 20 | return nil 21 | } 22 | -------------------------------------------------------------------------------- /protocol/protocol.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | const ( 4 | // Version ... 5 | Version = 70015 6 | 7 | // SrvNodeNetwork This node can be asked for full blocks instead of just headers. 8 | SrvNodeNetwork = 1 9 | // SrvNodeGetUTXO See BIP 0064 10 | SrvNodeGetUTXO = 2 11 | // SrvNodeBloom See BIP 0111 12 | SrvNodeBloom = 4 13 | // SrvNodeWitness See BIP 0144 14 | SrvNodeWitness = 8 15 | // SrvNodeNetworkLimited See BIP 0159 16 | SrvNodeNetworkLimited = 1024 17 | ) 18 | 19 | // NewUserAgent ... 20 | func NewUserAgent(userAgent string) VarStr { 21 | return newVarStr(userAgent) 22 | } 23 | -------------------------------------------------------------------------------- /protocol/cmd_test.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import "testing" 4 | 5 | func TestNewCommand(t *testing.T) { 6 | tests := []struct { 7 | input string 8 | expected [commandLength]byte 9 | fails bool 10 | }{ 11 | {input: "test", expected: [commandLength]byte{0x74, 0x65, 0x73, 0x74, 0, 0, 0, 0, 0, 0, 0, 0}, fails: false}, 12 | } 13 | 14 | for _, test := range tests { 15 | t.Run(test.input, func(tt *testing.T) { 16 | actual := newCommand(test.input) 17 | 18 | if actual != test.expected { 19 | t.Errorf("expected %x, got %x", test.expected, actual) 20 | } 21 | }) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cmd/showmempool.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/Jeiwan/tinybit/rpc" 7 | "github.com/spf13/cobra" 8 | ) 9 | 10 | func init() { 11 | showMempoolCmd.Flags().IntVar(&jsonrpcPort, "jsonrpc-port", 9334, "JSON-RPC port to connect to.") 12 | } 13 | 14 | var showMempoolCmd = &cobra.Command{ 15 | Use: "showmempool", 16 | RunE: func(cmd *cobra.Command, args []string) error { 17 | c, err := rpc.NewClient(jsonrpcPort) 18 | if err != nil { 19 | return err 20 | } 21 | defer c.Close() 22 | 23 | var reply string 24 | if err := c.Call("RPC.GetMempool", nil, &reply); err != nil { 25 | return err 26 | } 27 | 28 | fmt.Println(reply) 29 | 30 | return nil 31 | }, 32 | } 33 | -------------------------------------------------------------------------------- /node/cmd_ping.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/Jeiwan/tinybit/binary" 7 | "github.com/Jeiwan/tinybit/protocol" 8 | ) 9 | 10 | func (n Node) handlePing(header *protocol.MessageHeader, conn io.ReadWriter) error { 11 | var ping protocol.MsgPing 12 | 13 | lr := io.LimitReader(conn, int64(header.Length)) 14 | if err := binary.NewDecoder(lr).Decode(&ping); err != nil { 15 | return err 16 | } 17 | 18 | pong, err := protocol.NewPongMsg(n.Network, ping.Nonce) 19 | if err != nil { 20 | return err 21 | } 22 | 23 | msg, err := binary.Marshal(pong) 24 | if err != nil { 25 | return err 26 | } 27 | 28 | if _, err := conn.Write(msg); err != nil { 29 | return err 30 | } 31 | 32 | return nil 33 | } 34 | -------------------------------------------------------------------------------- /node/cmd_tx.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | 7 | "github.com/Jeiwan/tinybit/binary" 8 | "github.com/Jeiwan/tinybit/protocol" 9 | "github.com/sirupsen/logrus" 10 | ) 11 | 12 | func (no Node) handleTx(header *protocol.MessageHeader, conn io.ReadWriter) error { 13 | var tx protocol.MsgTx 14 | 15 | lr := io.LimitReader(conn, int64(header.Length)) 16 | if err := binary.NewDecoder(lr).Decode(&tx); err != nil { 17 | return err 18 | } 19 | 20 | hash, err := tx.Hash() 21 | if err != nil { 22 | return fmt.Errorf("tx.Hash: %+v", err) 23 | } 24 | 25 | logrus.Debugf("transaction: %x", hash) 26 | 27 | if err := tx.Verify(); err != nil { 28 | return fmt.Errorf("rejected invalid transaction %x", hash) 29 | } 30 | 31 | no.mempool.NewTxCh <- tx 32 | 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /node/cmd_block.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | 7 | "github.com/Jeiwan/tinybit/binary" 8 | "github.com/Jeiwan/tinybit/protocol" 9 | "github.com/sirupsen/logrus" 10 | ) 11 | 12 | func (no Node) handleBlock(header *protocol.MessageHeader, conn io.ReadWriter) error { 13 | var block protocol.MsgBlock 14 | 15 | lr := io.LimitReader(conn, int64(header.Length)) 16 | if err := binary.NewDecoder(lr).Decode(&block); err != nil { 17 | return err 18 | } 19 | 20 | hash, err := block.Hash() 21 | if err != nil { 22 | return fmt.Errorf("block.Hash: %+v", err) 23 | } 24 | 25 | logrus.Debugf("block: %x", hash) 26 | 27 | if err := block.Verify(); err != nil { 28 | return fmt.Errorf("rejected invalid block %x", hash) 29 | } 30 | 31 | no.mempool.NewBlockCh <- block 32 | 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /node/cmd_inv.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/Jeiwan/tinybit/binary" 7 | "github.com/Jeiwan/tinybit/protocol" 8 | ) 9 | 10 | func (no Node) handleInv(header *protocol.MessageHeader, conn io.ReadWriter) error { 11 | var inv protocol.MsgInv 12 | 13 | lr := io.LimitReader(conn, int64(header.Length)) 14 | if err := binary.NewDecoder(lr).Decode(&inv); err != nil { 15 | return err 16 | } 17 | 18 | var getData protocol.MsgGetData 19 | getData.Inventory = inv.Inventory 20 | getData.Count = inv.Count 21 | 22 | getDataMsg, err := protocol.NewMessage("getdata", no.Network, getData) 23 | if err != nil { 24 | return err 25 | } 26 | 27 | msg, err := binary.Marshal(getDataMsg) 28 | if err != nil { 29 | return err 30 | } 31 | 32 | if _, err := conn.Write(msg); err != nil { 33 | return err 34 | } 35 | 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /protocol/msg_getdata.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "bytes" 5 | 6 | "github.com/Jeiwan/tinybit/binary" 7 | ) 8 | 9 | // MsgGetData represents 'getdata' message. 10 | type MsgGetData struct { 11 | Count uint8 // TODO: Change to var_int 12 | Inventory []InvVector 13 | } 14 | 15 | // MarshalBinary implements binary.Marshaler interface. 16 | func (gd MsgGetData) MarshalBinary() ([]byte, error) { 17 | buf := bytes.NewBuffer([]byte{}) 18 | 19 | b, err := binary.Marshal(gd.Count) 20 | if err != nil { 21 | return nil, err 22 | } 23 | 24 | if _, err := buf.Write(b); err != nil { 25 | return nil, err 26 | } 27 | 28 | for _, i := range gd.Inventory { 29 | b, err := binary.Marshal(i) 30 | if err != nil { 31 | return nil, err 32 | } 33 | 34 | if _, err := buf.Write(b); err != nil { 35 | return nil, err 36 | } 37 | } 38 | 39 | return buf.Bytes(), nil 40 | } 41 | -------------------------------------------------------------------------------- /rpc/client.go: -------------------------------------------------------------------------------- 1 | package rpc 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "net/rpc" 7 | "net/rpc/jsonrpc" 8 | ) 9 | 10 | // Client is a JSON-RPC client. 11 | type Client struct { 12 | conn net.Conn 13 | jsonrpc *rpc.Client 14 | } 15 | 16 | // NewClient returns a new Client. 17 | func NewClient(port int) (*Client, error) { 18 | conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port)) 19 | if err != nil { 20 | return nil, err 21 | } 22 | 23 | c := jsonrpc.NewClient(conn) 24 | 25 | client := &Client{ 26 | conn: conn, 27 | jsonrpc: c, 28 | } 29 | 30 | return client, nil 31 | } 32 | 33 | // Call calls a remote RPC method. 34 | func (c Client) Call(serviceMethod string, args interface{}, reply interface{}) error { 35 | return c.jsonrpc.Call(serviceMethod, args, reply) 36 | } 37 | 38 | // Close closes a connection. 39 | func (c Client) Close() { 40 | c.conn.Close() 41 | } 42 | -------------------------------------------------------------------------------- /protocol/msg_pingpong.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | // MsgPing describes 'ping' message. 8 | type MsgPing struct { 9 | Nonce uint64 10 | } 11 | 12 | // MsgPong describes 'pong' message. 13 | type MsgPong struct { 14 | Nonce uint64 15 | } 16 | 17 | // NewPingMsg returns a new MsgPing. 18 | func NewPingMsg(network string) (*Message, uint64, error) { 19 | nonce := rand.Uint64() 20 | payload := MsgPing{ 21 | Nonce: nonce, 22 | } 23 | 24 | msg, err := NewMessage("ping", network, payload) 25 | if err != nil { 26 | return nil, 0, err 27 | } 28 | 29 | return msg, nonce, nil 30 | } 31 | 32 | // NewPongMsg returns a new MsgPong. 33 | func NewPongMsg(network string, nonce uint64) (*Message, error) { 34 | payload := MsgPong{ 35 | Nonce: nonce, 36 | } 37 | 38 | msg, err := NewMessage("pong", network, payload) 39 | if err != nil { 40 | return nil, err 41 | } 42 | 43 | return msg, nil 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## tinybit 2 | 3 | Tiny Bitcoin Node. In early development, probably will never be finished. Hopefully, will have some useful features. 4 | 5 | Implementation is explained in a series of blog posts: 6 | 7 | 1. [Programming Bitcoin Network](https://jeiwan.net/posts/programming-bitcoin-network/) 8 | 1. [Programming Bitcoin Network, part 2](https://jeiwan.net/posts/programming-bitcoin-network-2/) 9 | 1. [Programming Bitcoin Network, part 3](https://jeiwan.net/posts/programming-bitcoin-network-3/) 10 | 1. [Programming Bitcoin Network, part 4](https://jeiwan.net/posts/programming-bitcoin-network-4/) 11 | 12 | ### Running 13 | 1. Install [btcd](https://github.com/btcsuite/btcd). 14 | 1. `btcd --configfile ./btcd.conf` 15 | 1. `go build` 16 | 1. `./tinybit` 17 | 18 | 19 | ### Implemented so far 20 | 21 | 1. messages serialization and deserialization (see `binary` package) 22 | 1. 'version', 'verack' messages 23 | 1. 'inv' 24 | 1. 'tx' 25 | 1. 'getdata' 26 | 1. 'ping', 'pong' 27 | 1. 'block' 28 | 1. very basic mempool -------------------------------------------------------------------------------- /protocol/msg_inv.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/Jeiwan/tinybit/binary" 7 | ) 8 | 9 | const ( 10 | DataObjectError = iota 11 | DataObjectTx 12 | DataObjectBlock 13 | DataObjectFilterBlock 14 | DataObjectCmpctBlock 15 | ) 16 | 17 | // MsgInv represents 'inv' message. 18 | type MsgInv struct { 19 | Count uint8 // TODO: Change to var_int 20 | Inventory []InvVector 21 | } 22 | 23 | // UnmarshalBinary implements binary.Unmarshaler interface. 24 | func (inv *MsgInv) UnmarshalBinary(r io.Reader) error { 25 | d := binary.NewDecoder(r) 26 | 27 | if err := d.Decode(&inv.Count); err != nil { 28 | return err 29 | } 30 | 31 | for i := uint8(0); i < inv.Count; i++ { 32 | var v InvVector 33 | 34 | if err := d.Decode(&v); err != nil { 35 | return err 36 | } 37 | 38 | inv.Inventory = append(inv.Inventory, v) 39 | } 40 | 41 | return nil 42 | } 43 | 44 | // InvVector represents inventory vector. 45 | type InvVector struct { 46 | Type uint32 47 | Hash [32]byte 48 | } 49 | -------------------------------------------------------------------------------- /node/url.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "errors" 5 | "math" 6 | "net" 7 | "strconv" 8 | "strings" 9 | 10 | "github.com/Jeiwan/tinybit/protocol" 11 | ) 12 | 13 | // Addr ... 14 | type Addr struct { 15 | IP protocol.IPv4 16 | Port uint16 17 | } 18 | 19 | // ParseNodeAddr ... 20 | func ParseNodeAddr(nodeAddr string) (*Addr, error) { 21 | parts := strings.Split(nodeAddr, ":") 22 | if len(parts) != 2 { 23 | return nil, errors.New("malformed node address") 24 | } 25 | 26 | hostnamePart := parts[0] 27 | portPart := parts[1] 28 | if hostnamePart == "" || portPart == "" { 29 | return nil, errors.New("malformed node address") 30 | } 31 | 32 | port, err := strconv.Atoi(portPart) 33 | if err != nil { 34 | return nil, errors.New("malformed node address") 35 | } 36 | 37 | if port < 0 || port > math.MaxUint16 { 38 | return nil, errors.New("malformed node address") 39 | } 40 | 41 | var addr Addr 42 | ip := net.ParseIP(hostnamePart) 43 | copy(addr.IP[:], []byte(ip.To4())) 44 | 45 | addr.Port = uint16(port) 46 | 47 | return &addr, nil 48 | } 49 | -------------------------------------------------------------------------------- /rpc/rpc.go: -------------------------------------------------------------------------------- 1 | package rpc 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/Jeiwan/tinybit/protocol" 7 | ) 8 | 9 | // Node defines the interface of interaction between JSON-RPC server and a node. 10 | type Node interface { 11 | Mempool() map[string]*protocol.MsgTx 12 | } 13 | 14 | // RPC implements RPC interface of the node. 15 | type RPC struct { 16 | node Node 17 | } 18 | 19 | // MempoolArgs are arguments of Mempool method. 20 | type MempoolArgs interface{} 21 | 22 | // MempoolReply is reply of Mempool method. 23 | type MempoolReply string 24 | 25 | // GetMempool returns current mempool state information. 26 | func (r RPC) GetMempool(args *MempoolArgs, reply *MempoolReply) error { 27 | txs := r.node.Mempool() 28 | 29 | *reply = MempoolReply(formatMempoolReply(txs)) 30 | 31 | return nil 32 | } 33 | 34 | func formatMempoolReply(txs map[string]*protocol.MsgTx) string { 35 | var result string 36 | 37 | for k := range txs { 38 | result += fmt.Sprintf("%s\n", k) 39 | } 40 | result += fmt.Sprintf("Total %d transactions", len(txs)) 41 | 42 | return result 43 | } 44 | -------------------------------------------------------------------------------- /rpc/server.go: -------------------------------------------------------------------------------- 1 | package rpc 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "net/rpc" 7 | "net/rpc/jsonrpc" 8 | 9 | "github.com/sirupsen/logrus" 10 | ) 11 | 12 | // Server is a JSON-RPC server. 13 | type Server struct { 14 | port int 15 | rpc *rpc.Server 16 | } 17 | 18 | // NewServer returns a new Server. 19 | func NewServer(port int, node Node) (*Server, error) { 20 | rpcs := rpc.NewServer() 21 | 22 | handlers := RPC{node: node} 23 | if err := rpcs.Register(handlers); err != nil { 24 | return nil, err 25 | } 26 | 27 | s := Server{ 28 | port: port, 29 | rpc: rpcs, 30 | } 31 | 32 | return &s, nil 33 | } 34 | 35 | // Run runs the Server. 36 | func (s Server) Run() { 37 | l, err := net.Listen("tcp", fmt.Sprintf(":%d", s.port)) 38 | if err != nil { 39 | logrus.Errorf("failed to run JSON-RPC server: %+v", err) 40 | return 41 | } 42 | 43 | for { 44 | conn, err := l.Accept() 45 | if err != nil { 46 | logrus.Errorf("JSON-RPC connection failed: %+v", err) 47 | return 48 | } 49 | 50 | go s.rpc.ServeCodec(jsonrpc.NewServerCodec(conn)) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /protocol/cmd.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import "github.com/sirupsen/logrus" 4 | 5 | const ( 6 | cmdBlock = "block" 7 | cmdGetData = "getdata" 8 | cmdInv = "inv" 9 | cmdPing = "ping" 10 | cmdPong = "pong" 11 | cmdTx = "tx" 12 | cmdVerack = "verack" 13 | cmdVersion = "version" 14 | commandLength = 12 15 | ) 16 | 17 | var commands = map[string][commandLength]byte{ 18 | cmdBlock: newCommand(cmdBlock), 19 | cmdGetData: newCommand(cmdGetData), 20 | cmdInv: newCommand(cmdInv), 21 | cmdPing: newCommand(cmdPing), 22 | cmdPong: newCommand(cmdPong), 23 | cmdTx: newCommand(cmdTx), 24 | cmdVerack: newCommand(cmdVerack), 25 | cmdVersion: newCommand(cmdVersion), 26 | } 27 | 28 | func newCommand(command string) [commandLength]byte { 29 | l := len(command) 30 | if l > commandLength { 31 | logrus.Fatalf("command %s is too long", command) 32 | } 33 | 34 | var packed [commandLength]byte 35 | buf := make([]byte, commandLength-l) 36 | copy(packed[:], append([]byte(command), buf...)[:]) 37 | 38 | return packed 39 | } 40 | -------------------------------------------------------------------------------- /node/cmd_version.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "io" 5 | "net" 6 | 7 | "github.com/Jeiwan/tinybit/binary" 8 | "github.com/Jeiwan/tinybit/protocol" 9 | "github.com/sirupsen/logrus" 10 | ) 11 | 12 | func (n Node) handleVersion(header *protocol.MessageHeader, conn net.Conn) error { 13 | var version protocol.MsgVersion 14 | 15 | lr := io.LimitReader(conn, int64(header.Length)) 16 | if err := binary.NewDecoder(lr).Decode(&version); err != nil { 17 | return err 18 | } 19 | 20 | peer := Peer{ 21 | Address: conn.RemoteAddr(), 22 | Connection: conn, 23 | PongCh: make(chan uint64), 24 | Services: version.Services, 25 | UserAgent: version.UserAgent.String, 26 | Version: version.Version, 27 | } 28 | 29 | n.Peers[peer.ID()] = &peer 30 | go n.monitorPeer(&peer) 31 | 32 | logrus.Debugf("new peer %s", peer) 33 | 34 | verack, err := protocol.NewVerackMsg(n.Network) 35 | if err != nil { 36 | return err 37 | } 38 | 39 | msg, err := binary.Marshal(verack) 40 | if err != nil { 41 | return err 42 | } 43 | 44 | if _, err := conn.Write(msg); err != nil { 45 | return err 46 | } 47 | 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /protocol/netaddr.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | ) 8 | 9 | // IPv4 ... 10 | type IPv4 [4]byte 11 | 12 | // VersionNetAddr ... 13 | type VersionNetAddr struct { 14 | Services uint64 15 | IP IPv4 16 | Port uint16 17 | } 18 | 19 | // NewIPv4 ... 20 | func NewIPv4(a, b, c, d uint8) IPv4 { 21 | return IPv4{a, b, c, d} 22 | } 23 | 24 | // MarshalBinary implements the binary.Marshaler interface 25 | func (ip IPv4) MarshalBinary() ([]byte, error) { 26 | return append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF}, ip[:]...), nil 27 | } 28 | 29 | // Strings returns the string representation of IPv4. 30 | func (ip IPv4) String() string { 31 | return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]) 32 | } 33 | 34 | // UnmarshalBinary implements the binary.Marshaler interface 35 | func (ip IPv4) UnmarshalBinary(r io.Reader) error { 36 | data := make([]byte, 16) 37 | if _, err := r.Read(data); err != nil { 38 | return fmt.Errorf("unmarshal IPv4: %+v", err) 39 | } 40 | 41 | if len(data) != 16 { 42 | return errors.New("invalid IPv4: wrong length") 43 | } 44 | 45 | ipv4 := data[12:16] 46 | copy(ip[:], ipv4) 47 | 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /protocol/msg_version.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "math/rand" 5 | "time" 6 | ) 7 | 8 | // MsgVersion ... 9 | type MsgVersion struct { 10 | Version int32 11 | Services uint64 12 | Timestamp int64 13 | AddrRecv VersionNetAddr 14 | AddrFrom VersionNetAddr 15 | Nonce uint64 16 | UserAgent VarStr 17 | StartHeight int32 18 | Relay bool 19 | } 20 | 21 | // NewVersionMsg returns a new MsgVersion. 22 | func NewVersionMsg(network, userAgent string, peerIP IPv4, peerPort uint16) (*Message, error) { 23 | payload := MsgVersion{ 24 | Version: Version, 25 | Services: SrvNodeNetwork, 26 | Timestamp: time.Now().UTC().Unix(), 27 | AddrRecv: VersionNetAddr{ 28 | Services: SrvNodeNetwork, 29 | IP: peerIP, 30 | Port: peerPort, 31 | }, 32 | AddrFrom: VersionNetAddr{ 33 | Services: SrvNodeNetwork, 34 | IP: NewIPv4(127, 0, 0, 1), 35 | Port: 9334, 36 | }, 37 | Nonce: rand.Uint64(), 38 | UserAgent: NewUserAgent(userAgent), 39 | StartHeight: -1, 40 | Relay: true, 41 | } 42 | 43 | msg, err := NewMessage("version", network, payload) 44 | if err != nil { 45 | return nil, err 46 | } 47 | 48 | return msg, nil 49 | } 50 | -------------------------------------------------------------------------------- /cmd/tinybit.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/Jeiwan/tinybit/node" 7 | "github.com/Jeiwan/tinybit/rpc" 8 | "github.com/sirupsen/logrus" 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | const ( 13 | userAgent = "/Satoshi:5.64/tinybit:0.0.1/" 14 | ) 15 | 16 | var ( 17 | network string 18 | nodeAddr string 19 | jsonrpcPort int 20 | ) 21 | 22 | func init() { 23 | tinybitCmd.Flags().IntVar(&jsonrpcPort, "jsonrpc-port", 9334, "Port to listen JSON-RPC connections on") 24 | tinybitCmd.Flags().StringVar(&nodeAddr, "node-addr", "127.0.0.1:9333", "TCP address of a Bitcoin node to connect to") 25 | tinybitCmd.Flags().StringVar(&network, "network", "simnet", "Bitcoin network (simnet, mainnet)") 26 | } 27 | 28 | var tinybitCmd = &cobra.Command{ 29 | Use: "tinybit", 30 | RunE: func(cmd *cobra.Command, args []string) error { 31 | node, err := node.New(network, userAgent) 32 | if err != nil { 33 | return err 34 | } 35 | 36 | rpc, err := rpc.NewServer(jsonrpcPort, node) 37 | if err != nil { 38 | return err 39 | } 40 | 41 | logrus.Infof("Running JSON-RPC server on port %d", jsonrpcPort) 42 | go rpc.Run() 43 | 44 | return node.Run(nodeAddr) 45 | }, 46 | } 47 | 48 | // Execute ... 49 | func Execute() { 50 | tinybitCmd.AddCommand(showMempoolCmd) 51 | 52 | if err := tinybitCmd.Execute(); err != nil { 53 | logrus.Fatalln(err) 54 | os.Exit(1) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /node/mempool.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "encoding/hex" 5 | 6 | "github.com/Jeiwan/tinybit/protocol" 7 | "github.com/sirupsen/logrus" 8 | ) 9 | 10 | // Mempool represents mempool. 11 | type Mempool struct { 12 | NewBlockCh chan protocol.MsgBlock 13 | NewTxCh chan protocol.MsgTx 14 | 15 | txs map[string]*protocol.MsgTx 16 | } 17 | 18 | // NewMempool returns a new Mempool. 19 | func NewMempool() *Mempool { 20 | return &Mempool{ 21 | NewBlockCh: make(chan protocol.MsgBlock), 22 | NewTxCh: make(chan protocol.MsgTx), 23 | txs: make(map[string]*protocol.MsgTx), 24 | } 25 | } 26 | 27 | // Run starts mempool state handling. 28 | func (m Mempool) Run() { 29 | for { 30 | select { 31 | case tx := <-m.NewTxCh: 32 | hash, err := tx.Hash() 33 | if err != nil { 34 | logrus.Errorf("failed to calculate tx hash: %+v", err) 35 | break 36 | } 37 | 38 | txid := hex.EncodeToString(hash) 39 | m.txs[txid] = &tx 40 | case block := <-m.NewBlockCh: 41 | for _, tx := range block.Txs { 42 | hash, err := tx.Hash() 43 | if err != nil { 44 | logrus.Errorf("failed to calculate tx hash: %+v", err) 45 | break 46 | } 47 | 48 | txid := hex.EncodeToString(hash) 49 | delete(m.txs, txid) 50 | } 51 | } 52 | } 53 | } 54 | 55 | // Mempool ... 56 | func (n Node) Mempool() map[string]*protocol.MsgTx { 57 | m := make(map[string]*protocol.MsgTx) 58 | 59 | for k, v := range n.mempool.txs { 60 | m[string(k)] = v 61 | } 62 | 63 | return m 64 | } 65 | -------------------------------------------------------------------------------- /protocol/msg_verack_test.go: -------------------------------------------------------------------------------- 1 | package protocol_test 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | "testing" 7 | 8 | "github.com/Jeiwan/tinybit/protocol" 9 | ) 10 | 11 | func TestNewVerackMsg(t *testing.T) { 12 | tests := []struct { 13 | name string 14 | input string 15 | err error 16 | expected *protocol.Message 17 | }{ 18 | {name: "ok", 19 | input: "mainnet", 20 | err: nil, 21 | expected: &protocol.Message{ 22 | MessageHeader: protocol.MessageHeader{ 23 | Magic: [4]byte{249, 190, 180, 217}, 24 | Command: [12]byte{118, 101, 114, 97, 99, 107, 0, 0, 0, 0, 0, 0}, 25 | Length: uint32(0), 26 | Checksum: [4]byte{93, 246, 224, 226}, 27 | }, 28 | Payload: []byte{}, 29 | }}, 30 | {name: "unsupported network", 31 | input: "unknown", 32 | err: errors.New("unsupported network 'unknown'"), 33 | expected: nil}, 34 | } 35 | 36 | for _, test := range tests { 37 | t.Run(test.name, func(tt *testing.T) { 38 | actual, err := protocol.NewVerackMsg(test.input) 39 | if err != nil && test.err == nil { 40 | t.Errorf("unexpected error: %+v", err) 41 | } 42 | 43 | if err == nil && test.err != nil { 44 | t.Errorf("expected error: %+v, got: %+v", err, actual) 45 | } 46 | 47 | if err != nil && test.err != nil && err.Error() != test.err.Error() { 48 | t.Errorf("expected error: %+v, got: %+v", err, test.err) 49 | } 50 | 51 | if !reflect.DeepEqual(actual, test.expected) { 52 | t.Errorf("expected: %+v, actual: %+v", test.expected, actual) 53 | } 54 | }) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /node/url_test.go: -------------------------------------------------------------------------------- 1 | package node_test 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | "testing" 7 | 8 | "github.com/Jeiwan/tinybit/node" 9 | "github.com/Jeiwan/tinybit/protocol" 10 | ) 11 | 12 | func TestParseNodeAddr(t *testing.T) { 13 | tests := []struct { 14 | name string 15 | input string 16 | err error 17 | expected *node.Addr 18 | }{ 19 | {name: "ok", 20 | input: "127.0.0.1:8333", 21 | err: nil, 22 | expected: &node.Addr{IP: protocol.NewIPv4(127, 0, 0, 1), Port: 8333}}, 23 | {name: "empty input", 24 | input: "", 25 | err: errors.New("malformed node address"), 26 | expected: nil}, 27 | {name: "missing port", 28 | input: "127.0.0.1", 29 | err: errors.New("malformed node address"), 30 | expected: nil}, 31 | {name: "missing ip", 32 | input: ":1234", 33 | err: errors.New("malformed node address"), 34 | expected: nil}, 35 | {name: "invalid ip", 36 | input: "300.300.300.300:1234", 37 | err: nil, 38 | expected: &node.Addr{IP: protocol.NewIPv4(0, 0, 0, 0), Port: 1234}}, 39 | } 40 | 41 | for _, test := range tests { 42 | t.Run(test.name, func(tt *testing.T) { 43 | actual, err := node.ParseNodeAddr(test.input) 44 | if err != nil && test.err == nil { 45 | t.Errorf("unexpected error: %+v", err) 46 | } 47 | 48 | if err == nil && test.err != nil { 49 | t.Errorf("expected error: %+v, got: %+v", err, actual) 50 | } 51 | 52 | if err != nil && test.err != nil && err.Error() != test.err.Error() { 53 | t.Errorf("expected error: %+v, got: %+v", err, test.err) 54 | } 55 | 56 | if !reflect.DeepEqual(actual, test.expected) { 57 | t.Errorf("expected: %+v, actual: %+v", test.expected, actual) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /protocol/varint.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "errors" 5 | "io" 6 | "math" 7 | 8 | "github.com/Jeiwan/tinybit/binary" 9 | ) 10 | 11 | var errInvalidVarIntValue = errors.New("invalid varint value") 12 | 13 | // VarInt is variable length integer. 14 | type VarInt struct { 15 | value interface{} 16 | } 17 | 18 | // Int returns returns value as 'int'. 19 | func (vi VarInt) Int() (int, error) { 20 | switch v := vi.value.(type) { 21 | case uint8: 22 | return int(v), nil 23 | 24 | case uint16: 25 | return int(v), nil 26 | 27 | case uint32: 28 | return int(v), nil 29 | 30 | case uint64: 31 | // Assume we'll never get value more than MaxInt64. 32 | if v > math.MaxInt64 { 33 | return math.MaxInt64, nil 34 | } 35 | 36 | return int(v), nil 37 | } 38 | 39 | return 0, errInvalidVarIntValue 40 | } 41 | 42 | // UnmarshalBinary implements binary.Unmarshaler interface. 43 | func (vi *VarInt) UnmarshalBinary(r io.Reader) error { 44 | var b uint8 45 | 46 | lr := io.LimitReader(r, 1) 47 | if err := binary.NewDecoder(lr).Decode(&b); err != nil { 48 | return err 49 | } 50 | 51 | if b < 0xFD { 52 | vi.value = b 53 | return nil 54 | } 55 | 56 | if b == 0xFD { 57 | var v uint16 58 | lr := io.LimitReader(r, 2) 59 | if err := binary.NewDecoder(lr).Decode(&v); err != nil { 60 | return err 61 | } 62 | 63 | vi.value = v 64 | return nil 65 | } 66 | 67 | if b == 0xFE { 68 | var v uint32 69 | lr := io.LimitReader(r, 4) 70 | if err := binary.NewDecoder(lr).Decode(&v); err != nil { 71 | return err 72 | } 73 | 74 | vi.value = v 75 | return nil 76 | } 77 | 78 | if b == 0xFF { 79 | var v uint64 80 | lr := io.LimitReader(r, 8) 81 | if err := binary.NewDecoder(lr).Decode(&v); err != nil { 82 | return err 83 | } 84 | 85 | vi.value = v 86 | return nil 87 | } 88 | 89 | return nil 90 | } 91 | -------------------------------------------------------------------------------- /node/peer.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net" 7 | "time" 8 | 9 | "github.com/Jeiwan/tinybit/binary" 10 | "github.com/Jeiwan/tinybit/protocol" 11 | "github.com/sirupsen/logrus" 12 | ) 13 | 14 | const ( 15 | pingIntervalSec = 120 16 | pingTimeoutSec = 30 17 | ) 18 | 19 | // Peer describes a peer node in a network. 20 | type Peer struct { 21 | Address net.Addr 22 | Connection io.ReadWriteCloser 23 | PongCh chan uint64 24 | Services uint64 25 | UserAgent string 26 | Version int32 27 | } 28 | 29 | // ID returns peer ID. 30 | func (p Peer) ID() PeerID { 31 | return PeerID(p.Address.String()) 32 | } 33 | 34 | func (p Peer) String() string { 35 | return fmt.Sprintf("%s (%s)", p.UserAgent, p.Address) 36 | } 37 | 38 | type peerPing struct { 39 | nonce uint64 40 | peerID PeerID 41 | } 42 | 43 | func (n Node) monitorPeers() { 44 | // TODO: make this concurrent 45 | peerPings := make(map[uint64]PeerID) 46 | 47 | for { 48 | select { 49 | case nonce := <-n.PongCh: 50 | peerID := peerPings[nonce] 51 | if peerID == "" { 52 | break 53 | } 54 | peer := n.Peers[peerID] 55 | if peer == nil { 56 | break 57 | } 58 | 59 | peer.PongCh <- nonce 60 | delete(peerPings, nonce) 61 | 62 | case pp := <-n.PingCh: 63 | peerPings[pp.nonce] = pp.peerID 64 | 65 | case peerID := <-n.DisconCh: 66 | n.disconnectPeer(peerID) 67 | 68 | for k, v := range peerPings { 69 | if v == peerID { 70 | delete(peerPings, k) 71 | break 72 | } 73 | } 74 | } 75 | } 76 | } 77 | 78 | func (n *Node) monitorPeer(peer *Peer) { 79 | for { 80 | time.Sleep(pingIntervalSec * time.Second) 81 | 82 | ping, nonce, err := protocol.NewPingMsg(n.Network) 83 | if err != nil { 84 | logrus.Fatalf("monitorPeer, NewPingMsg: %v", err) 85 | } 86 | 87 | msg, err := binary.Marshal(ping) 88 | if err != nil { 89 | logrus.Fatalf("monitorPeer, binary.Marshal: %v", err) 90 | } 91 | 92 | if _, err := peer.Connection.Write(msg); err != nil { 93 | n.disconnectPeer(peer.ID()) 94 | } 95 | 96 | logrus.Debugf("sent 'ping' to %s", peer) 97 | 98 | n.PingCh <- peerPing{ 99 | nonce: nonce, 100 | peerID: peer.ID(), 101 | } 102 | 103 | t := time.NewTimer(pingTimeoutSec * time.Second) 104 | 105 | select { 106 | case pn := <-peer.PongCh: 107 | if pn != nonce { 108 | logrus.Errorf("nonce doesn't match for %s: want %d, got %d", peer, nonce, pn) 109 | n.DisconCh <- peer.ID() 110 | return 111 | } 112 | logrus.Debugf("got 'pong' from %s", peer) 113 | case <-t.C: 114 | n.DisconCh <- peer.ID() 115 | return 116 | } 117 | 118 | t.Stop() 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /binary/marshaler.go: -------------------------------------------------------------------------------- 1 | package binary 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "fmt" 7 | "reflect" 8 | ) 9 | 10 | const ( 11 | commandLength = 12 12 | hashLength = 32 13 | magicAndChecksumLength = 4 14 | ) 15 | 16 | // Marshaler is the interface implemented by types that can marshal themselves into binary. 17 | type Marshaler interface { 18 | MarshalBinary() ([]byte, error) 19 | } 20 | 21 | // Marshal returns the binary encoding of v. 22 | func Marshal(v interface{}) ([]byte, error) { 23 | buf := bytes.NewBuffer([]byte{}) 24 | 25 | if reflect.TypeOf(v).Kind() == reflect.Ptr { 26 | v = reflect.ValueOf(v).Elem().Interface() 27 | } 28 | 29 | switch val := v.(type) { 30 | case uint8, int32, uint32, int64, uint64, bool: 31 | if err := binary.Write(buf, binary.LittleEndian, val); err != nil { 32 | return nil, err 33 | } 34 | 35 | // port 36 | case uint16: 37 | if err := binary.Write(buf, binary.BigEndian, val); err != nil { 38 | return nil, err 39 | } 40 | 41 | case [magicAndChecksumLength]byte: 42 | if _, err := buf.Write(val[:]); err != nil { 43 | return nil, err 44 | } 45 | 46 | case [commandLength]byte: 47 | if _, err := buf.Write(val[:]); err != nil { 48 | return nil, err 49 | } 50 | 51 | case [hashLength]byte: 52 | if _, err := buf.Write(val[:]); err != nil { 53 | return nil, err 54 | } 55 | 56 | case []byte: 57 | if _, err := buf.Write(val); err != nil { 58 | return nil, err 59 | } 60 | 61 | // VarStr.String 62 | case string: 63 | if _, err := buf.Write([]byte(val)); err != nil { 64 | return nil, err 65 | } 66 | 67 | case Marshaler: 68 | b, err := val.MarshalBinary() 69 | if err != nil { 70 | return nil, err 71 | } 72 | 73 | if _, err := buf.Write(b); err != nil { 74 | return nil, err 75 | } 76 | 77 | default: 78 | // is it a struct? 79 | if reflect.ValueOf(v).Kind() == reflect.Struct { 80 | b, err := marshalStruct(v) 81 | if err != nil { 82 | return nil, err 83 | } 84 | 85 | if _, err := buf.Write(b); err != nil { 86 | return nil, err 87 | } 88 | 89 | break 90 | } 91 | 92 | return nil, fmt.Errorf("unsupported type %s", reflect.TypeOf(v).String()) 93 | } 94 | 95 | return buf.Bytes(), nil 96 | } 97 | 98 | func marshalStruct(v interface{}) ([]byte, error) { 99 | buf := bytes.NewBuffer([]byte{}) 100 | vv := reflect.ValueOf(v) 101 | 102 | for i := 0; i < vv.NumField(); i++ { 103 | s, err := Marshal(reflect.Indirect(vv.Field(i)).Interface()) 104 | if err != nil { 105 | f := reflect.TypeOf(v).Field(i).Name 106 | return nil, fmt.Errorf("failed to marshal field %s: %v", f, err) 107 | } 108 | 109 | if _, err := buf.Write(s); err != nil { 110 | return nil, err 111 | } 112 | } 113 | 114 | return buf.Bytes(), nil 115 | } 116 | -------------------------------------------------------------------------------- /protocol/msg_test.go: -------------------------------------------------------------------------------- 1 | package protocol_test 2 | 3 | import ( 4 | "encoding/hex" 5 | "testing" 6 | "time" 7 | 8 | "github.com/Jeiwan/tinybit/binary" 9 | "github.com/Jeiwan/tinybit/protocol" 10 | ) 11 | 12 | func TestMessageSerialization(t *testing.T) { 13 | version := protocol.MsgVersion{ 14 | Version: protocol.Version, 15 | Services: protocol.SrvNodeNetwork, 16 | Timestamp: time.Date(2019, 11, 11, 0, 0, 0, 0, time.UTC).Unix(), 17 | AddrRecv: protocol.VersionNetAddr{ 18 | Services: protocol.SrvNodeNetwork, 19 | IP: protocol.NewIPv4(127, 0, 0, 1), 20 | Port: 9333, 21 | }, 22 | AddrFrom: protocol.VersionNetAddr{ 23 | Services: protocol.SrvNodeNetwork, 24 | IP: protocol.NewIPv4(127, 0, 0, 1), 25 | Port: 9334, 26 | }, 27 | Nonce: 31337, 28 | UserAgent: protocol.NewUserAgent("/Satoshi:5.64/tinybit:0.0.1/"), 29 | StartHeight: -1, 30 | Relay: true, 31 | } 32 | msg, err := protocol.NewMessage("version", "simnet", version) 33 | if err != nil { 34 | t.Errorf("unexpected error: %+v", err) 35 | return 36 | } 37 | 38 | msgSerialized, err := binary.Marshal(msg) 39 | if err != nil { 40 | t.Errorf("unexpected error: %+v", err) 41 | return 42 | } 43 | 44 | actual := hex.EncodeToString(msgSerialized) 45 | expected := "161c141276657273696f6e000000000072000000463d41ca7f110100010000000000000080a4c85d00000000010000000000000000000000000000000000ffff7f0000012475010000000000000000000000000000000000ffff7f0000012476697a0000000000001c2f5361746f7368693a352e36342f74696e796269743a302e302e312fffffffff01" 46 | if actual != expected { 47 | t.Errorf("expected: %s, actual: %s", expected, actual) 48 | } 49 | 50 | } 51 | 52 | func TestHasValidCommand(t *testing.T) { 53 | tests := []struct { 54 | name string 55 | expected bool 56 | }{ 57 | {"version", true}, 58 | {"invalid", false}, 59 | } 60 | 61 | for _, test := range tests { 62 | t.Run(test.name, func(tt *testing.T) { 63 | var packed [12]byte 64 | buf := make([]byte, 12-len(test.name)) 65 | copy(packed[:], append([]byte(test.name), buf...)[:]) 66 | 67 | mh := protocol.MessageHeader{ 68 | Command: packed, 69 | } 70 | actual := mh.HasValidCommand() 71 | 72 | if actual != test.expected { 73 | t.Errorf("expected: %v, actual: %v", test.expected, actual) 74 | } 75 | }) 76 | } 77 | } 78 | 79 | func TestHasValidMagic(t *testing.T) { 80 | magicMainnet := [4]byte{0xf9, 0xbe, 0xb4, 0xd9} 81 | magicSimnet := [4]byte{0x16, 0x1c, 0x14, 0x12} 82 | 83 | tests := []struct { 84 | name string 85 | magic [4]byte 86 | expected bool 87 | }{ 88 | {"mainner", magicMainnet, true}, 89 | {"simner", magicSimnet, true}, 90 | {"invalid", [4]byte{0xde, 0xad, 0xbe, 0xef}, false}, 91 | } 92 | 93 | for _, test := range tests { 94 | t.Run(test.name, func(tt *testing.T) { 95 | mh := protocol.MessageHeader{ 96 | Magic: test.magic, 97 | } 98 | actual := mh.HasValidMagic() 99 | 100 | if actual != test.expected { 101 | t.Errorf("expected: %v, actual: %v", test.expected, actual) 102 | } 103 | }) 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /protocol/msg_block_test.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestMsgBlockVerify(t *testing.T) { 8 | tests := []struct { 9 | name string 10 | block MsgBlock 11 | expected error 12 | }{ 13 | {name: "ok", 14 | block: MsgBlock{Version: 536870912, 15 | PrevBlock: [32]byte{172, 4, 9, 193, 214, 108, 234, 44, 154, 29, 10, 102, 174, 80, 88, 194, 234, 43, 37, 146, 145, 71, 180, 34, 119, 42, 87, 208, 209, 150, 233, 97}, 16 | MerkleRoot: [32]byte{177, 101, 232, 30, 115, 112, 196, 254, 111, 9, 84, 131, 131, 183, 20, 35, 189, 41, 123, 171, 233, 60, 151, 170, 229, 8, 240, 37, 236, 173, 138, 127}, 17 | Timestamp: 1579005423, 18 | Bits: [4]byte{0xff, 0xff, 0x7f, 0x20}, 19 | Nonce: 0, 20 | TxCount: 1, 21 | Txs: []MsgTx{ 22 | {Version: 1, 23 | Flag: 0, 24 | TxInCount: 1, 25 | TxIn: []TxInput{ 26 | {PreviousOutput: OutPoint{ 27 | Hash: [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 28 | Index: 4294967295}, 29 | ScriptLength: 24, 30 | SignatureScript: []byte{2, 146, 2, 8, 34, 125, 127, 126, 105, 249, 169, 94, 11, 47, 80, 50, 83, 72, 47, 98, 116, 99, 100, 47}, 31 | Sequence: 4294967295}}, 32 | TxOutCount: 1, 33 | TxOut: []TxOutput{ 34 | {Value: 5000000000, 35 | PkScriptLength: 25, 36 | PkScript: []byte{118, 169, 20, 146, 142, 130, 144, 21, 113, 202, 194, 219, 252, 254, 137, 153, 27, 24, 23, 160, 102, 44, 148, 136, 172}}}, 37 | TxWitness: TxWitnessData{ 38 | Count: 0, 39 | Witness: []TxWitness{}}, 40 | LockTime: 0}}}, 41 | expected: nil}, 42 | {name: "invalid hash", 43 | block: MsgBlock{Version: 536870912, 44 | PrevBlock: [32]byte{172, 4, 9, 193, 214, 108, 234, 44, 154, 29, 10, 102, 174, 80, 88, 194, 234, 43, 37, 146, 145, 71, 180, 34, 119, 42, 87, 208, 209, 150, 233, 97}, 45 | MerkleRoot: [32]byte{177, 101, 232, 30, 115, 112, 196, 254, 111, 9, 84, 131, 131, 183, 20, 35, 189, 41, 123, 171, 233, 60, 151, 170, 229, 8, 240, 37, 236, 173, 138, 127}, 46 | Timestamp: 1579005423, 47 | Bits: [4]byte{0xff, 0xff, 0x7f, 0x10}, 48 | Nonce: 0, 49 | TxCount: 1, 50 | Txs: []MsgTx{ 51 | {Version: 1, 52 | Flag: 0, 53 | TxInCount: 1, 54 | TxIn: []TxInput{ 55 | {PreviousOutput: OutPoint{ 56 | Hash: [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 57 | Index: 4294967295}, 58 | ScriptLength: 24, 59 | SignatureScript: []byte{2, 146, 2, 8, 34, 125, 127, 126, 105, 249, 169, 94, 11, 47, 80, 50, 83, 72, 47, 98, 116, 99, 100, 47}, 60 | Sequence: 4294967295}}, 61 | TxOutCount: 1, 62 | TxOut: []TxOutput{ 63 | {Value: 5000000000, 64 | PkScriptLength: 25, 65 | PkScript: []byte{118, 169, 20, 146, 142, 130, 144, 21, 113, 202, 194, 219, 252, 254, 137, 153, 27, 24, 23, 160, 102, 44, 148, 136, 172}}}, 66 | TxWitness: TxWitnessData{ 67 | Count: 0, 68 | Witness: []TxWitness{}}, 69 | LockTime: 0}}}, 70 | expected: errInvalidBlockHash}, 71 | } 72 | 73 | for _, test := range tests { 74 | t.Run(test.name, func(tt *testing.T) { 75 | got := test.block.Verify() 76 | 77 | if got != test.expected { 78 | tt.Errorf("expected: %+v, got: %+v", test.expected, got) 79 | } 80 | }) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /protocol/msg.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "crypto/sha256" 5 | "fmt" 6 | "io" 7 | "strings" 8 | 9 | "github.com/Jeiwan/tinybit/binary" 10 | ) 11 | 12 | const ( 13 | checksumLength = 4 14 | nodeNetwork = 1 15 | magicLength = 4 16 | 17 | // MsgHeaderLength specifies the length of Message in bytes 18 | MsgHeaderLength = magicLength + commandLength + checksumLength + 4 // 4 - payload length value 19 | ) 20 | 21 | var ( 22 | MagicMainnet Magic = [magicLength]byte{0xf9, 0xbe, 0xb4, 0xd9} 23 | MagicSimnet Magic = [magicLength]byte{0x16, 0x1c, 0x14, 0x12} 24 | Networks = map[string][magicLength]byte{ 25 | "mainnet": MagicMainnet, 26 | "simnet": MagicSimnet, 27 | } 28 | ) 29 | 30 | type Magic [magicLength]byte 31 | 32 | // MessageHeader ... 33 | type MessageHeader struct { 34 | Magic [magicLength]byte 35 | Command [commandLength]byte 36 | Length uint32 37 | Checksum [checksumLength]byte 38 | } 39 | 40 | // Message ... 41 | type Message struct { 42 | MessageHeader 43 | Payload []byte 44 | } 45 | 46 | // NewMessage returns a new Message. 47 | func NewMessage(cmd, network string, payload interface{}) (*Message, error) { 48 | serializedPayload, err := binary.Marshal(payload) 49 | if err != nil { 50 | return nil, err 51 | } 52 | 53 | command, ok := commands[cmd] 54 | if !ok { 55 | return nil, fmt.Errorf("unsupported command %s", cmd) 56 | } 57 | 58 | magic, ok := Networks[network] 59 | if !ok { 60 | return nil, fmt.Errorf("unsupported network '%s'", network) 61 | } 62 | 63 | msg := Message{ 64 | MessageHeader: MessageHeader{ 65 | Magic: magic, 66 | Command: command, 67 | Length: uint32(len(serializedPayload)), 68 | Checksum: checksum(serializedPayload), 69 | }, 70 | Payload: serializedPayload, 71 | } 72 | 73 | return &msg, nil 74 | } 75 | 76 | // CommandString returns command as a string with zero bytes removed. 77 | func (mh MessageHeader) CommandString() string { 78 | return strings.Trim(string(mh.Command[:]), string(0)) 79 | } 80 | 81 | // Validate ... 82 | func (mh MessageHeader) Validate() error { 83 | if !mh.HasValidMagic() { 84 | return fmt.Errorf("invalid magic: %x", mh.Magic) 85 | } 86 | 87 | if !mh.HasValidCommand() { 88 | return fmt.Errorf("invalid command: %s", mh.CommandString()) 89 | } 90 | return nil 91 | } 92 | 93 | // HasValidCommand returns true if the message header contains a supported command. 94 | // Returns false otherwise. 95 | func (mh MessageHeader) HasValidCommand() bool { 96 | _, ok := commands[mh.CommandString()] 97 | return ok 98 | } 99 | 100 | // HasValidMagic returns true if the message header contains a supported magic. 101 | // Returns false otherwise. 102 | func (mh MessageHeader) HasValidMagic() bool { 103 | switch mh.Magic { 104 | case MagicMainnet, MagicSimnet: 105 | return true 106 | } 107 | 108 | return false 109 | } 110 | 111 | // VarStr ... 112 | type VarStr struct { 113 | Length uint8 114 | String string 115 | } 116 | 117 | func newVarStr(str string) VarStr { 118 | return VarStr{ 119 | Length: uint8(len(str)), // TODO: implement var_int 120 | String: str, 121 | } 122 | } 123 | 124 | // UnmarshalBinary implements the binary.Unmarshaler interface 125 | func (v *VarStr) UnmarshalBinary(r io.Reader) error { 126 | lengthBuf := make([]byte, 1) 127 | if _, err := r.Read(lengthBuf); err != nil { 128 | return fmt.Errorf("varStr.UnmarshalBinary: %+v", err) 129 | } 130 | v.Length = uint8(lengthBuf[0]) 131 | 132 | stringBuf := make([]byte, v.Length) 133 | if _, err := r.Read(stringBuf); err != nil { 134 | return fmt.Errorf("varStr.UnmarshalBinary: %+v", err) 135 | } 136 | v.String = string(stringBuf) 137 | 138 | return nil 139 | } 140 | 141 | func checksum(data []byte) [checksumLength]byte { 142 | hash := sha256.Sum256(data) 143 | hash = sha256.Sum256(hash[:]) 144 | var hashArr [checksumLength]byte 145 | copy(hashArr[:], hash[0:checksumLength]) 146 | 147 | return hashArr 148 | } 149 | -------------------------------------------------------------------------------- /node/node.go: -------------------------------------------------------------------------------- 1 | package node 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io" 7 | "net" 8 | 9 | "github.com/Jeiwan/tinybit/binary" 10 | "github.com/Jeiwan/tinybit/protocol" 11 | "github.com/sirupsen/logrus" 12 | ) 13 | 14 | // PeerID is peer IP address. 15 | type PeerID string 16 | 17 | // Node implements a Bitcoin node. 18 | type Node struct { 19 | Network string 20 | NetworkMagic protocol.Magic 21 | Peers map[PeerID]*Peer 22 | PingCh chan peerPing 23 | PongCh chan uint64 24 | DisconCh chan PeerID 25 | UserAgent string 26 | 27 | mempool *Mempool 28 | } 29 | 30 | // New returns a new Node. 31 | func New(network, userAgent string) (*Node, error) { 32 | networkMagic, ok := protocol.Networks[network] 33 | if !ok { 34 | return nil, fmt.Errorf("unsupported network %s", network) 35 | } 36 | 37 | return &Node{ 38 | Network: network, 39 | NetworkMagic: networkMagic, 40 | Peers: make(map[PeerID]*Peer), 41 | PingCh: make(chan peerPing), 42 | DisconCh: make(chan PeerID), 43 | PongCh: make(chan uint64), 44 | UserAgent: userAgent, 45 | 46 | mempool: NewMempool(), 47 | }, nil 48 | } 49 | 50 | // Run starts a node. 51 | func (no Node) Run(nodeAddr string) error { 52 | peerAddr, err := ParseNodeAddr(nodeAddr) 53 | if err != nil { 54 | return err 55 | } 56 | 57 | version, err := protocol.NewVersionMsg( 58 | no.Network, 59 | no.UserAgent, 60 | peerAddr.IP, 61 | peerAddr.Port, 62 | ) 63 | if err != nil { 64 | return err 65 | } 66 | 67 | msgSerialized, err := binary.Marshal(version) 68 | if err != nil { 69 | return err 70 | } 71 | 72 | conn, err := net.Dial("tcp", nodeAddr) 73 | if err != nil { 74 | return err 75 | } 76 | defer conn.Close() 77 | 78 | _, err = conn.Write(msgSerialized) 79 | if err != nil { 80 | return err 81 | } 82 | 83 | go no.monitorPeers() 84 | go no.mempool.Run() 85 | 86 | tmp := make([]byte, protocol.MsgHeaderLength) 87 | 88 | Loop: 89 | for { 90 | n, err := conn.Read(tmp) 91 | if err != nil { 92 | if err != io.EOF { 93 | return err 94 | } 95 | break Loop 96 | } 97 | 98 | var msgHeader protocol.MessageHeader 99 | if err := binary.NewDecoder(bytes.NewReader(tmp[:n])).Decode(&msgHeader); err != nil { 100 | logrus.Errorf("invalid header: %+v", err) 101 | continue 102 | } 103 | 104 | if err := msgHeader.Validate(); err != nil { 105 | logrus.Error(err) 106 | continue 107 | } 108 | 109 | logrus.Debugf("received message: %s", msgHeader.Command) 110 | 111 | switch msgHeader.CommandString() { 112 | case "version": 113 | if err := no.handleVersion(&msgHeader, conn); err != nil { 114 | logrus.Errorf("failed to handle 'version': %+v", err) 115 | continue 116 | } 117 | case "verack": 118 | if err := no.handleVerack(&msgHeader, conn); err != nil { 119 | logrus.Errorf("failed to handle 'verack': %+v", err) 120 | continue 121 | } 122 | case "ping": 123 | if err := no.handlePing(&msgHeader, conn); err != nil { 124 | logrus.Errorf("failed to handle 'ping': %+v", err) 125 | continue 126 | } 127 | case "pong": 128 | if err := no.handlePong(&msgHeader, conn); err != nil { 129 | logrus.Errorf("failed to handle 'pong': %+v", err) 130 | continue 131 | } 132 | case "inv": 133 | if err := no.handleInv(&msgHeader, conn); err != nil { 134 | logrus.Errorf("failed to handle 'inv': %+v", err) 135 | continue 136 | } 137 | case "tx": 138 | if err := no.handleTx(&msgHeader, conn); err != nil { 139 | logrus.Errorf("failed to handle 'tx': %+v", err) 140 | continue 141 | } 142 | case "block": 143 | if err := no.handleBlock(&msgHeader, conn); err != nil { 144 | logrus.Errorf("failed to handle 'block': %+v", err) 145 | continue 146 | } 147 | } 148 | } 149 | 150 | return nil 151 | } 152 | 153 | func (no Node) disconnectPeer(peerID PeerID) { 154 | logrus.Debugf("disconnecting peer %s", peerID) 155 | 156 | peer := no.Peers[peerID] 157 | if peer == nil { 158 | return 159 | } 160 | 161 | peer.Connection.Close() 162 | delete(no.Peers, peerID) 163 | } 164 | -------------------------------------------------------------------------------- /protocol/msg_block.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "bytes" 5 | "crypto/sha256" 6 | "errors" 7 | "io" 8 | "math/big" 9 | "sort" 10 | 11 | "github.com/Jeiwan/tinybit/binary" 12 | ) 13 | 14 | var errInvalidBlockHash = errors.New("invalid block hash") 15 | 16 | // MsgBlock represents 'block' message. 17 | type MsgBlock struct { 18 | Version int32 19 | PrevBlock [32]byte 20 | MerkleRoot [32]byte 21 | Timestamp uint32 22 | Bits [4]byte 23 | Nonce uint32 24 | TxCount uint8 // TODO: Convert to var_int 25 | Txs []MsgTx 26 | } 27 | 28 | // Hash calculates and returns block hash. 29 | func (blck MsgBlock) Hash() ([]byte, error) { 30 | raw, err := blck.MarshalHeader() 31 | if err != nil { 32 | return nil, err 33 | } 34 | 35 | hash := sha256.Sum256(raw) 36 | hash = sha256.Sum256(hash[:]) 37 | blockHash := hash[:] 38 | 39 | sort.SliceStable(blockHash, func(i, j int) bool { return true }) 40 | 41 | return blockHash, nil 42 | } 43 | 44 | // MarshalHeader returns serialized block header. 45 | func (blck *MsgBlock) MarshalHeader() ([]byte, error) { 46 | buf := bytes.NewBuffer([]byte{}) 47 | 48 | b, err := binary.Marshal(blck.Version) 49 | if err != nil { 50 | return nil, err 51 | } 52 | if _, err := buf.Write(b); err != nil { 53 | return nil, err 54 | } 55 | 56 | b, err = binary.Marshal(blck.PrevBlock) 57 | if err != nil { 58 | return nil, err 59 | } 60 | if _, err := buf.Write(b); err != nil { 61 | return nil, err 62 | } 63 | 64 | b, err = binary.Marshal(blck.MerkleRoot) 65 | if err != nil { 66 | return nil, err 67 | } 68 | if _, err := buf.Write(b); err != nil { 69 | return nil, err 70 | } 71 | 72 | b, err = binary.Marshal(blck.Timestamp) 73 | if err != nil { 74 | return nil, err 75 | } 76 | if _, err := buf.Write(b); err != nil { 77 | return nil, err 78 | } 79 | 80 | b, err = binary.Marshal(blck.Bits) 81 | if err != nil { 82 | return nil, err 83 | } 84 | if _, err := buf.Write(b); err != nil { 85 | return nil, err 86 | } 87 | 88 | b, err = binary.Marshal(blck.Nonce) 89 | if err != nil { 90 | return nil, err 91 | } 92 | if _, err := buf.Write(b); err != nil { 93 | return nil, err 94 | } 95 | 96 | return buf.Bytes(), nil 97 | } 98 | 99 | // UnmarshalBinary implements binary.Unmarshaler 100 | func (blck *MsgBlock) UnmarshalBinary(r io.Reader) error { 101 | d := binary.NewDecoder(r) 102 | 103 | if err := d.Decode(&blck.Version); err != nil { 104 | return err 105 | } 106 | 107 | if err := d.Decode(&blck.PrevBlock); err != nil { 108 | return err 109 | } 110 | 111 | if err := d.Decode(&blck.MerkleRoot); err != nil { 112 | return err 113 | } 114 | 115 | if err := d.Decode(&blck.Timestamp); err != nil { 116 | return err 117 | } 118 | 119 | if err := d.Decode(&blck.Bits); err != nil { 120 | return err 121 | } 122 | 123 | if err := d.Decode(&blck.Nonce); err != nil { 124 | return err 125 | } 126 | 127 | if err := d.Decode(&blck.TxCount); err != nil { 128 | return err 129 | } 130 | 131 | for i := uint8(0); i < blck.TxCount; i++ { 132 | var tx MsgTx 133 | 134 | if err := d.Decode(&tx); err != nil { 135 | return err 136 | } 137 | 138 | blck.Txs = append(blck.Txs, tx) 139 | } 140 | 141 | return nil 142 | } 143 | 144 | func (blck MsgBlock) unpackBits() []byte { 145 | bits := make([]byte, len(blck.Bits)) 146 | copy(bits, blck.Bits[:]) 147 | sort.SliceStable(bits, func(i, j int) bool { return true }) 148 | 149 | target := make([]byte, 32) 150 | i := 32 - bits[0] 151 | target[i] = bits[1] 152 | target[i+1] = bits[2] 153 | target[i+2] = bits[3] 154 | 155 | return target 156 | } 157 | 158 | // Verify checks if the block message is valid. 159 | func (blck MsgBlock) Verify() error { 160 | target := blck.unpackBits() 161 | 162 | hash, err := blck.Hash() 163 | if err != nil { 164 | return err 165 | } 166 | 167 | targetNum := big.NewInt(0).SetBytes(target) 168 | hashNum := big.NewInt(0).SetBytes(hash) 169 | 170 | // Block hash must be <= target threshold 171 | if hashNum.Cmp(targetNum) > 0 { 172 | return errInvalidBlockHash 173 | } 174 | 175 | return nil 176 | } 177 | -------------------------------------------------------------------------------- /protocol/varint_test.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "bytes" 5 | "math" 6 | "testing" 7 | 8 | "github.com/google/go-cmp/cmp" 9 | ) 10 | 11 | func TestVarintUnmarshalBinary(t *testing.T) { 12 | tests := []struct { 13 | name string 14 | raw []byte 15 | expected interface{} 16 | err error 17 | }{ 18 | {name: "1 byte min", 19 | raw: []byte{0x00}, 20 | expected: uint8(0), 21 | err: nil}, 22 | {name: "1 byte max", 23 | raw: []byte{0xFC}, 24 | expected: uint8(0xfc), 25 | err: nil}, 26 | {name: "2 bytes min", 27 | raw: []byte{0xFD, 0x00, 0x00}, 28 | expected: uint16(0x0000), 29 | err: nil}, 30 | {name: "2 bytes max", 31 | raw: []byte{0xFD, 0xFF, 0xFF}, 32 | expected: uint16(0xFFFF), 33 | err: nil}, 34 | {name: "4 bytes min", 35 | raw: []byte{0xFE, 0x00, 0x00, 0x00, 0x00}, 36 | expected: uint32(0x00000000), 37 | err: nil}, 38 | {name: "4 bytes max", 39 | raw: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF}, 40 | expected: uint32(0xFFFFFFFF), 41 | err: nil}, 42 | {name: "8 bytes min", 43 | raw: []byte{0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 44 | expected: uint64(0x0000000000000000), 45 | err: nil}, 46 | {name: "8 bytes max", 47 | raw: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 48 | expected: uint64(0xFFFFFFFFFFFFFFFF), 49 | err: nil}, 50 | } 51 | 52 | for _, test := range tests { 53 | t.Run(test.name, func(tt *testing.T) { 54 | r := bytes.NewBuffer(test.raw) 55 | varint := VarInt{} 56 | err := varint.UnmarshalBinary(r) 57 | 58 | if err == nil && test.err != nil { 59 | tt.Errorf("expected error: %+v, got: %+v", test.err, err) 60 | return 61 | } 62 | 63 | if err != nil && test.err == nil { 64 | tt.Errorf("unexpected error: %+v", err) 65 | return 66 | } 67 | 68 | if err != nil && test.err != nil && err != test.err { 69 | tt.Errorf("expected error: %+v, got: %+v", test.err, err) 70 | return 71 | } 72 | 73 | got := varint.value 74 | if diff := cmp.Diff(test.expected, got); diff != "" { 75 | tt.Errorf("varint.UnmarshalBinary() mismatch (-want +got):\n%s", diff) 76 | return 77 | } 78 | }) 79 | } 80 | } 81 | 82 | func TestVarintInt(t *testing.T) { 83 | tests := []struct { 84 | name string 85 | value interface{} 86 | expected int 87 | err error 88 | }{ 89 | {name: "1 byte min", 90 | value: uint8(0), 91 | expected: 0, 92 | err: nil}, 93 | {name: "1 byte max", 94 | value: uint8(0xFF), 95 | expected: 0xFF, 96 | err: nil}, 97 | {name: "2 bytes min", 98 | value: uint16(0x0000), 99 | expected: 0, 100 | err: nil}, 101 | {name: "2 bytes max", 102 | value: uint16(0xFFFF), 103 | expected: 0xffff, 104 | err: nil}, 105 | {name: "4 bytes min", 106 | value: uint32(0x00000000), 107 | expected: 0, 108 | err: nil}, 109 | {name: "4 bytes max", 110 | value: uint32(0xFFFFFFFF), 111 | expected: 0xffffffff, 112 | err: nil}, 113 | {name: "8 bytes min", 114 | value: uint64(0x0000000000000000), 115 | expected: 0, 116 | err: nil}, 117 | {name: "8 bytes max", 118 | value: uint64(0xFFFFFFFFFFFFFFFF), 119 | expected: math.MaxInt64, 120 | err: nil}, 121 | } 122 | 123 | for _, test := range tests { 124 | t.Run(test.name, func(tt *testing.T) { 125 | varint := VarInt{value: test.value} 126 | got, err := varint.Int() 127 | 128 | if err == nil && test.err != nil { 129 | tt.Errorf("expected error: %+v, got: %+v", test.err, err) 130 | return 131 | } 132 | 133 | if err != nil && test.err == nil { 134 | tt.Errorf("unexpected error: %+v", err) 135 | return 136 | } 137 | 138 | if err != nil && test.err != nil && err != test.err { 139 | tt.Errorf("expected error: %+v, got: %+v", test.err, err) 140 | return 141 | } 142 | 143 | if got != test.expected { 144 | tt.Errorf("expected: %v, got: %v", test.expected, got) 145 | return 146 | } 147 | }) 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /binary/marshaler_test.go: -------------------------------------------------------------------------------- 1 | package binary_test 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | "testing" 7 | 8 | "github.com/Jeiwan/tinybit/binary" 9 | ) 10 | 11 | type customType []byte 12 | 13 | func (ct customType) MarshalBinary() ([]byte, error) { 14 | return []byte{0xde, 0xad, 0xbe, 0xef}, nil 15 | } 16 | 17 | func TestMarshal(t *testing.T) { 18 | tests := []struct { 19 | name string 20 | input interface{} 21 | err error 22 | expected []byte 23 | }{ 24 | {name: "uint8", 25 | input: uint8(255), 26 | err: nil, 27 | expected: []byte{0xFF}}, 28 | 29 | {name: "int32", 30 | input: int32(1337), 31 | err: nil, 32 | expected: []byte{0x39, 0x05, 0, 0}}, 33 | 34 | {name: "uint32", 35 | input: uint32(1337), 36 | err: nil, 37 | expected: []byte{0x39, 0x05, 0, 0}}, 38 | 39 | {name: "int64", 40 | input: int64(1337), 41 | err: nil, 42 | expected: []byte{0x39, 0x05, 0, 0, 0, 0, 0, 0}}, 43 | 44 | {name: "uint64", 45 | input: int64(1337), 46 | err: nil, 47 | expected: []byte{0x39, 0x05, 0, 0, 0, 0, 0, 0}}, 48 | 49 | {name: "bool true", 50 | input: true, 51 | err: nil, 52 | expected: []byte{0x01}}, 53 | 54 | {name: "bool false", 55 | input: false, 56 | err: nil, 57 | expected: []byte{0}}, 58 | 59 | {name: "magic or checksum", 60 | input: [4]byte{0x31, 0x33, 0x70, 0}, 61 | err: nil, 62 | expected: []byte{0x31, 0x33, 0x70, 0}}, 63 | 64 | {name: "command", 65 | input: [12]byte{0x74, 0x65, 0x73, 0x74, 0, 0, 0, 0, 0, 0, 0, 0}, 66 | err: nil, 67 | expected: []byte{0x74, 0x65, 0x73, 0x74, 0, 0, 0, 0, 0, 0, 0, 0}}, 68 | 69 | {name: "hash", 70 | input: [32]byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}, 71 | err: nil, 72 | expected: []byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}}, 73 | 74 | {name: "bytes slice", 75 | input: []byte{0x12, 0x34, 0x56, 0x78}, 76 | err: nil, 77 | expected: []byte{0x12, 0x34, 0x56, 0x78}}, 78 | 79 | {name: "empty bytes slice", 80 | input: []byte{}, 81 | err: nil, 82 | expected: []byte{}}, 83 | 84 | {name: "struct", 85 | input: struct { 86 | Test uint32 87 | Magic [4]byte 88 | Data []byte 89 | }{ 90 | Test: 31337, 91 | Magic: [4]byte{0x12, 0x34, 0x56, 0x78}, 92 | Data: []byte{0xde, 0xad, 0xbe, 0xef}}, 93 | err: nil, 94 | expected: []byte{0x69, 0x7A, 0, 0, 0x12, 0x34, 0x56, 0x78, 0xde, 0xad, 0xbe, 0xef}}, 95 | 96 | {name: "struct with a pointer", 97 | input: struct { 98 | Test uint32 99 | Pointer *struct { 100 | Test uint32 101 | } 102 | }{ 103 | Test: 31337, 104 | Pointer: &struct { 105 | Test uint32 106 | }{ 107 | Test: 31337, 108 | }}, 109 | err: nil, 110 | expected: []byte{0x69, 0x7A, 0, 0, 0x69, 0x7A, 0, 0}}, 111 | 112 | {name: "struct with a string", 113 | input: struct { 114 | Test uint32 115 | String string 116 | }{ 117 | Test: 31337, 118 | String: "test"}, 119 | err: nil, 120 | expected: []byte{0x69, 0x7a, 0, 0, 0x74, 0x65, 0x73, 0x74}}, 121 | 122 | {name: "custom marshaler", 123 | input: customType{}, 124 | err: nil, 125 | expected: []byte{0xde, 0xad, 0xbe, 0xef}}, 126 | 127 | {name: "unsupported type", 128 | input: [3]byte{0x12, 0x34, 0x56}, 129 | err: errors.New("unsupported type [3]uint8"), 130 | expected: nil}, 131 | } 132 | 133 | for _, test := range tests { 134 | t.Run(test.name, func(tt *testing.T) { 135 | actual, err := binary.Marshal(test.input) 136 | if err == nil && test.err != nil { 137 | tt.Errorf("expected error: %v, actual: %s", test.err, actual) 138 | return 139 | } 140 | 141 | if err != nil && test.err == nil { 142 | tt.Errorf("didn't expect an error: %v", err) 143 | return 144 | } 145 | 146 | if !reflect.DeepEqual(actual, test.expected) { 147 | tt.Errorf("expected: %v, actual %v", test.expected, actual) 148 | } 149 | }) 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /binary/unmarshaler.go: -------------------------------------------------------------------------------- 1 | package binary 2 | 3 | import ( 4 | "encoding/binary" 5 | "fmt" 6 | "io" 7 | "reflect" 8 | ) 9 | 10 | // Unmarshaler is the interface implemented by types that can unmarshal themselves from binary. 11 | type Unmarshaler interface { 12 | UnmarshalBinary(r io.Reader) error 13 | } 14 | 15 | // Decoder reads and decodes binary data from an input stream. 16 | type Decoder struct { 17 | r io.Reader 18 | } 19 | 20 | // NewDecoder returns a new Decoder that reads from r. 21 | func NewDecoder(r io.Reader) *Decoder { 22 | return &Decoder{ 23 | r: r, 24 | } 25 | } 26 | 27 | // Decode ... 28 | func (d Decoder) Decode(v interface{}) error { 29 | switch val := v.(type) { 30 | case *bool: 31 | if err := d.decodeBool(val); err != nil { 32 | return err 33 | } 34 | 35 | case *int32: 36 | if err := d.decodeInt32(val); err != nil { 37 | return err 38 | } 39 | 40 | case *int64: 41 | if err := d.decodeInt64(val); err != nil { 42 | return err 43 | } 44 | 45 | case *uint8: 46 | if err := d.decodeUint8(val); err != nil { 47 | return err 48 | } 49 | 50 | case *uint16: 51 | if err := d.decodeUint16(val); err != nil { 52 | return err 53 | } 54 | 55 | case *uint32: 56 | if err := d.decodeUint32(val); err != nil { 57 | return err 58 | } 59 | 60 | case *uint64: 61 | if err := d.decodeUint64(val); err != nil { 62 | return err 63 | } 64 | 65 | case *[magicAndChecksumLength]byte: 66 | if err := d.decodeArray(magicAndChecksumLength, val[:]); err != nil { 67 | return err 68 | } 69 | 70 | case *[commandLength]byte: 71 | if err := d.decodeArray(commandLength, val[:]); err != nil { 72 | return err 73 | } 74 | 75 | case *[hashLength]byte: 76 | if err := d.decodeArray(hashLength, val[:]); err != nil { 77 | return err 78 | } 79 | 80 | case Unmarshaler: 81 | if err := val.UnmarshalBinary(d.r); err != nil { 82 | return err 83 | } 84 | 85 | default: 86 | if reflect.ValueOf(v).Kind() == reflect.Ptr && 87 | reflect.ValueOf(v).Elem().Kind() == reflect.Struct { 88 | if err := d.decodeStruct(v); err != nil { 89 | return err 90 | } 91 | break 92 | } 93 | 94 | return fmt.Errorf("unsupported type %s", reflect.TypeOf(v).String()) 95 | } 96 | 97 | return nil 98 | } 99 | 100 | func (d Decoder) decodeArray(len int64, out []byte) error { 101 | if _, err := io.LimitReader(d.r, len).Read(out); err != nil { 102 | return err 103 | } 104 | 105 | return nil 106 | } 107 | 108 | func (d Decoder) decodeStruct(v interface{}) error { 109 | val := reflect.Indirect(reflect.ValueOf(v)) 110 | 111 | for i := 0; i < val.NumField(); i++ { 112 | if err := d.Decode(val.Field(i).Addr().Interface()); err != nil { 113 | return err 114 | } 115 | 116 | } 117 | 118 | return nil 119 | } 120 | 121 | func (d Decoder) decodeBool(out *bool) error { 122 | lr := io.LimitReader(d.r, 1) 123 | 124 | if err := binary.Read(lr, binary.LittleEndian, out); err != nil { 125 | return err 126 | } 127 | 128 | return nil 129 | } 130 | 131 | func (d Decoder) decodeInt32(out *int32) error { 132 | lr := io.LimitReader(d.r, 4) 133 | 134 | if err := binary.Read(lr, binary.LittleEndian, out); err != nil { 135 | return err 136 | } 137 | 138 | return nil 139 | } 140 | 141 | func (d Decoder) decodeInt64(out *int64) error { 142 | lr := io.LimitReader(d.r, 8) 143 | 144 | if err := binary.Read(lr, binary.LittleEndian, out); err != nil { 145 | return err 146 | } 147 | 148 | return nil 149 | } 150 | 151 | func (d Decoder) decodeUint8(out *uint8) error { 152 | lr := io.LimitReader(d.r, 1) 153 | 154 | if err := binary.Read(lr, binary.LittleEndian, out); err != nil { 155 | return err 156 | } 157 | 158 | return nil 159 | } 160 | 161 | func (d Decoder) decodeUint16(out *uint16) error { 162 | lr := io.LimitReader(d.r, 2) 163 | 164 | if err := binary.Read(lr, binary.BigEndian, out); err != nil { 165 | return err 166 | } 167 | 168 | return nil 169 | } 170 | 171 | func (d Decoder) decodeUint32(out *uint32) error { 172 | lr := io.LimitReader(d.r, 4) 173 | 174 | if err := binary.Read(lr, binary.LittleEndian, out); err != nil { 175 | return err 176 | } 177 | 178 | return nil 179 | } 180 | 181 | func (d Decoder) decodeUint64(out *uint64) error { 182 | lr := io.LimitReader(d.r, 8) 183 | 184 | if err := binary.Read(lr, binary.LittleEndian, out); err != nil { 185 | return err 186 | } 187 | 188 | return nil 189 | } 190 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 3 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 4 | github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= 5 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 6 | github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= 7 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 8 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 9 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 10 | github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= 11 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 12 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 13 | github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= 14 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 15 | github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= 16 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 17 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 18 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 19 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 20 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 21 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 22 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 23 | github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 24 | github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= 25 | github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= 26 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 27 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 28 | github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= 29 | github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= 30 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 31 | github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= 32 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 33 | github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= 34 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 35 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 36 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 37 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= 38 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 39 | golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 40 | golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 41 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= 42 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 43 | golang.org/x/sys v0.0.0-20190904154756-749cb33beabd h1:DBH9mDw0zluJT/R+nGuV3jWFWLFaHyYZWD4tOT+cjn0= 44 | golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 45 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 46 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 47 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 48 | -------------------------------------------------------------------------------- /binary/unmarshaler_test.go: -------------------------------------------------------------------------------- 1 | package binary_test 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "math" 7 | "reflect" 8 | "testing" 9 | 10 | "github.com/Jeiwan/tinybit/binary" 11 | "github.com/google/go-cmp/cmp" 12 | ) 13 | 14 | type customUnmarshaler struct { 15 | Value int 16 | } 17 | 18 | func (u *customUnmarshaler) UnmarshalBinary(r io.Reader) error { 19 | data := make([]byte, 1) 20 | if _, err := r.Read(data); err != nil { 21 | return err 22 | } 23 | 24 | u.Value = int(data[0]) 25 | return nil 26 | } 27 | 28 | func TestUnmarshal(t *testing.T) { 29 | 30 | tests := []struct { 31 | name string 32 | input []byte 33 | err error 34 | actual func() interface{} 35 | expected interface{} 36 | }{ 37 | {name: "bool true", 38 | input: []byte{0x01}, 39 | err: nil, 40 | actual: func() interface{} { var x bool; return &x }, 41 | expected: true}, 42 | 43 | {name: "bool false", 44 | input: []byte{0}, 45 | err: nil, 46 | actual: func() interface{} { var x bool; return &x }, 47 | expected: false}, 48 | 49 | {name: "int32", 50 | input: []byte{0xFF, 0xFF, 0xFF, 0x7F}, 51 | err: nil, 52 | actual: func() interface{} { var x int32; return &x }, 53 | expected: int32(math.MaxInt32)}, 54 | 55 | {name: "int64", 56 | input: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}, 57 | err: nil, 58 | actual: func() interface{} { var x int64; return &x }, 59 | expected: int64(math.MaxInt64)}, 60 | 61 | {name: "uint8", 62 | input: []byte{0xFF}, 63 | err: nil, 64 | actual: func() interface{} { var x uint8; return &x }, 65 | expected: uint8(math.MaxUint8)}, 66 | 67 | {name: "uint16", 68 | input: []byte{0xFF, 0xFF}, 69 | err: nil, 70 | actual: func() interface{} { var x uint16; return &x }, 71 | expected: uint16(math.MaxUint16)}, 72 | 73 | {name: "uint32", 74 | input: []byte{0xFF, 0xFF, 0xFF, 0xFF}, 75 | err: nil, 76 | actual: func() interface{} { var x uint32; return &x }, 77 | expected: uint32(math.MaxUint32)}, 78 | 79 | {name: "uint64", 80 | input: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 81 | err: nil, 82 | actual: func() interface{} { var x uint64; return &x }, 83 | expected: uint64(math.MaxUint64)}, 84 | 85 | {name: "magic or checksum", 86 | input: []byte{0xde, 0xad, 0xbe, 0xef}, 87 | err: nil, 88 | actual: func() interface{} { var x [4]byte; return &x }, 89 | expected: [4]byte{0xde, 0xad, 0xbe, 0xef}}, 90 | 91 | {name: "hash", 92 | input: []byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}, 93 | err: nil, 94 | actual: func() interface{} { var x [32]byte; return &x }, 95 | expected: [32]byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}}, 96 | 97 | {name: "command", 98 | input: []byte{0xde, 0xad, 0xbe, 0xef, 0, 0, 0, 0, 0, 0, 0, 0}, 99 | err: nil, 100 | actual: func() interface{} { var x [12]byte; return &x }, 101 | expected: [12]byte{0xde, 0xad, 0xbe, 0xef, 0, 0, 0, 0, 0, 0, 0, 0}}, 102 | 103 | {name: "Unmarshaler", 104 | input: []byte{0x01, 0x02, 0x03}, 105 | err: nil, 106 | actual: func() interface{} { var x customUnmarshaler; return &x }, 107 | expected: customUnmarshaler{Value: 1}}, 108 | 109 | {name: "struct", 110 | input: []byte{ 111 | 11, 112 | 0, 22, 113 | 0xde, 0xad, 0xbe, 0xef, 114 | }, 115 | err: nil, 116 | actual: func() interface{} { 117 | x := struct { 118 | A uint8 119 | B uint16 120 | C [4]byte 121 | }{} 122 | return &x 123 | }, 124 | expected: struct { 125 | A uint8 126 | B uint16 127 | C [4]byte 128 | }{11, 22, [4]byte{0xde, 0xad, 0xbe, 0xef}}}, 129 | } 130 | 131 | for _, test := range tests { 132 | t.Run(test.name, func(tt *testing.T) { 133 | actualPtr := test.actual() 134 | err := binary.NewDecoder(bytes.NewReader(test.input)).Decode(actualPtr) 135 | actual := reflect.ValueOf(actualPtr).Elem().Interface() 136 | 137 | if err == nil && test.err != nil { 138 | tt.Errorf("expected error: %v, actual: %v", test.err, actual) 139 | return 140 | } 141 | 142 | if err != nil && test.err == nil { 143 | tt.Errorf("didn't expect an error: %v", err) 144 | return 145 | } 146 | 147 | if diff := cmp.Diff(test.expected, actual); diff != "" { 148 | tt.Errorf("Decode() mismatch(-want +got):\n%s", diff) 149 | return 150 | } 151 | }) 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /protocol/msg_tx.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "bytes" 5 | "crypto/sha256" 6 | "errors" 7 | "fmt" 8 | "io" 9 | "sort" 10 | 11 | "github.com/Jeiwan/tinybit/binary" 12 | ) 13 | 14 | var errInvalidTransaction = errors.New("invalid transaction") 15 | 16 | // MsgTx represents 'tx' message. 17 | type MsgTx struct { 18 | Version int32 19 | Flag uint16 20 | TxInCount uint8 // TODO: Convert to var_int 21 | TxIn []TxInput 22 | TxOutCount uint8 // TODO: Convert to var_int 23 | TxOut []TxOutput 24 | TxWitness TxWitnessData 25 | LockTime uint32 26 | } 27 | 28 | // Hash returns transaction ID. 29 | func (tx MsgTx) Hash() ([]byte, error) { 30 | serialized, err := tx.MarshalBinary() 31 | if err != nil { 32 | return nil, fmt.Errorf("tx.MarshalBinary: %+v", err) 33 | } 34 | 35 | hash := sha256.Sum256(serialized) 36 | hash = sha256.Sum256(hash[:]) 37 | 38 | txid := hash[:] 39 | 40 | sort.SliceStable(txid, func(i, j int) bool { 41 | return true 42 | }) 43 | 44 | return txid, nil 45 | } 46 | 47 | // MarshalBinary implements binary.Marshaler interface. 48 | func (tx MsgTx) MarshalBinary() ([]byte, error) { 49 | buf := bytes.NewBuffer([]byte{}) 50 | 51 | b, err := binary.Marshal(tx.Version) 52 | if err != nil { 53 | return nil, err 54 | } 55 | if _, err := buf.Write(b); err != nil { 56 | return nil, err 57 | } 58 | 59 | if tx.Flag == uint16(1) { 60 | b, err := binary.Marshal(tx.Flag) 61 | if err != nil { 62 | return nil, err 63 | } 64 | if _, err := buf.Write(b); err != nil { 65 | return nil, err 66 | } 67 | } 68 | 69 | b, err = binary.Marshal(tx.TxInCount) 70 | if err != nil { 71 | return nil, err 72 | } 73 | if _, err := buf.Write(b); err != nil { 74 | return nil, err 75 | } 76 | 77 | for _, txin := range tx.TxIn { 78 | b, err = binary.Marshal(txin) 79 | if err != nil { 80 | return nil, err 81 | } 82 | if _, err := buf.Write(b); err != nil { 83 | return nil, err 84 | } 85 | } 86 | 87 | b, err = binary.Marshal(tx.TxOutCount) 88 | if err != nil { 89 | return nil, err 90 | } 91 | if _, err := buf.Write(b); err != nil { 92 | return nil, err 93 | } 94 | 95 | for _, txout := range tx.TxOut { 96 | b, err = binary.Marshal(txout) 97 | if err != nil { 98 | return nil, err 99 | } 100 | if _, err := buf.Write(b); err != nil { 101 | return nil, err 102 | } 103 | } 104 | 105 | if tx.Flag == uint16(1) { 106 | b, err = binary.Marshal(tx.TxWitness) 107 | if err != nil { 108 | return nil, err 109 | } 110 | if _, err := buf.Write(b); err != nil { 111 | return nil, err 112 | } 113 | } 114 | 115 | b, err = binary.Marshal(tx.LockTime) 116 | if err != nil { 117 | return nil, err 118 | } 119 | if _, err := buf.Write(b); err != nil { 120 | return nil, err 121 | } 122 | 123 | return buf.Bytes(), nil 124 | } 125 | 126 | // UnmarshalBinary implements binary.Unmarshaler 127 | func (tx *MsgTx) UnmarshalBinary(r io.Reader) error { 128 | d := binary.NewDecoder(r) 129 | 130 | if err := d.Decode(&tx.Version); err != nil { 131 | return err 132 | } 133 | 134 | var flagPrefix byte 135 | if err := d.Decode(&flagPrefix); err != nil { 136 | return err 137 | } 138 | 139 | if flagPrefix == 0 { 140 | var flag byte 141 | if err := d.Decode(&flag); err != nil { 142 | return err 143 | } 144 | 145 | r := bytes.NewBuffer([]byte{flagPrefix, flag}) 146 | if err := binary.NewDecoder(r).Decode(&tx.Flag); err != nil { 147 | return err 148 | } 149 | 150 | if err := d.Decode(&tx.TxInCount); err != nil { 151 | return err 152 | } 153 | } else { 154 | tx.TxInCount = flagPrefix 155 | } 156 | 157 | for i := uint8(0); i < tx.TxInCount; i++ { 158 | var txin TxInput 159 | 160 | if err := d.Decode(&txin); err != nil { 161 | return err 162 | } 163 | 164 | tx.TxIn = append(tx.TxIn, txin) 165 | } 166 | 167 | if err := d.Decode(&tx.TxOutCount); err != nil { 168 | return err 169 | } 170 | 171 | for i := uint8(0); i < tx.TxOutCount; i++ { 172 | var txout TxOutput 173 | 174 | if err := d.Decode(&txout); err != nil { 175 | return err 176 | } 177 | 178 | tx.TxOut = append(tx.TxOut, txout) 179 | } 180 | 181 | if tx.Flag == 1 { 182 | if err := d.Decode(&tx.TxWitness); err != nil { 183 | return err 184 | } 185 | } 186 | 187 | if err := d.Decode(&tx.LockTime); err != nil { 188 | return err 189 | } 190 | 191 | return nil 192 | } 193 | 194 | // Verify returns an error if transaction is invalid. 195 | func (tx MsgTx) Verify() error { 196 | if len(tx.TxIn) == 0 || tx.TxInCount == 0 { 197 | return errInvalidTransaction 198 | } 199 | 200 | if len(tx.TxOut) == 0 || tx.TxOutCount == 0 { 201 | return errInvalidTransaction 202 | } 203 | 204 | return nil 205 | } 206 | 207 | // TxInput represents transaction input. 208 | type TxInput struct { 209 | PreviousOutput OutPoint 210 | ScriptLength uint8 // TODO: Convert to var_int 211 | SignatureScript []byte 212 | Sequence uint32 213 | } 214 | 215 | // TxOutput represents transaction output. 216 | type TxOutput struct { 217 | Value int64 218 | PkScriptLength uint8 // TODO: Convert to var_int 219 | PkScript []byte 220 | } 221 | 222 | // TxWitnessData represents transaction witness data. 223 | type TxWitnessData struct { 224 | Count uint8 // TODO: Convert to var_int 225 | Witness []TxWitness 226 | } 227 | 228 | // TxWitness represents a component of transaction witness data. 229 | type TxWitness struct { 230 | Length uint8 // TODO: Convert to var_int 231 | Data []byte 232 | } 233 | 234 | // OutPoint represents previous output transaction reference. 235 | type OutPoint struct { 236 | Hash [32]byte 237 | Index uint32 238 | } 239 | 240 | // MarshalBinary implements binary.Marshaler interface. 241 | func (txw TxWitnessData) MarshalBinary() ([]byte, error) { 242 | var buf = bytes.NewBuffer([]byte{}) 243 | 244 | b, err := binary.Marshal(txw.Count) 245 | if err != nil { 246 | return nil, err 247 | } 248 | if _, err := buf.Write(b); err != nil { 249 | return nil, err 250 | } 251 | 252 | for _, w := range txw.Witness { 253 | b, err := binary.Marshal(w) 254 | if err != nil { 255 | return nil, err 256 | } 257 | if _, err := buf.Write(b); err != nil { 258 | return nil, err 259 | } 260 | } 261 | 262 | return buf.Bytes(), nil 263 | } 264 | 265 | // UnmarshalBinary implements binary.Unmarshaler interface. 266 | func (txw *TxWitnessData) UnmarshalBinary(r io.Reader) error { 267 | d := binary.NewDecoder(r) 268 | 269 | if err := d.Decode(&txw.Count); err != nil { 270 | return err 271 | } 272 | 273 | txw.Witness = nil 274 | 275 | for i := uint8(0); i < txw.Count; i++ { 276 | var w TxWitness 277 | 278 | if err := d.Decode(&w); err != nil { 279 | return err 280 | } 281 | 282 | txw.Witness = append(txw.Witness, w) 283 | } 284 | 285 | return nil 286 | } 287 | 288 | // MarshalBinary implements binary.Marshaler interface. 289 | func (txw *TxWitness) MarshalBinary() ([]byte, error) { 290 | var buf = bytes.NewBuffer([]byte{}) 291 | 292 | b, err := binary.Marshal(txw.Length) 293 | if err != nil { 294 | return nil, err 295 | } 296 | if _, err := buf.Write(b); err != nil { 297 | return nil, err 298 | } 299 | 300 | b, err = binary.Marshal(txw.Data) 301 | if err != nil { 302 | return nil, err 303 | } 304 | if _, err := buf.Write(b); err != nil { 305 | return nil, err 306 | } 307 | 308 | return buf.Bytes(), nil 309 | } 310 | 311 | // UnmarshalBinary implements binary.Unmarshaler interface. 312 | func (txw *TxWitness) UnmarshalBinary(r io.Reader) error { 313 | if err := binary.NewDecoder(r).Decode(&txw.Length); err != nil { 314 | return err 315 | } 316 | 317 | if txw.Length == 0 { 318 | return nil 319 | } 320 | 321 | txw.Data = make([]byte, txw.Length) 322 | n, err := io.LimitReader(r, int64(txw.Length)).Read(txw.Data) 323 | if err != nil { 324 | return err 325 | } 326 | 327 | if int64(n) != int64(txw.Length) { 328 | return fmt.Errorf("invalid witness data was read: want %d bytes, got %d bytes", txw.Length, n) 329 | } 330 | 331 | return nil 332 | } 333 | 334 | // UnmarshalBinary implements binary.Unmarshaler interface. 335 | func (txin *TxInput) UnmarshalBinary(r io.Reader) error { 336 | d := binary.NewDecoder(r) 337 | 338 | if err := d.Decode(&txin.PreviousOutput); err != nil { 339 | return err 340 | } 341 | 342 | if err := d.Decode(&txin.ScriptLength); err != nil { 343 | return err 344 | } 345 | 346 | if txin.ScriptLength != 0 { 347 | txin.SignatureScript = make([]byte, txin.ScriptLength) 348 | n, err := io.LimitReader(r, int64(txin.ScriptLength)).Read(txin.SignatureScript) 349 | if err != nil { 350 | return err 351 | } 352 | 353 | if int64(n) != int64(txin.ScriptLength) { 354 | return fmt.Errorf("invalid input script was read: want %d bytes, got %d bytes", txin.ScriptLength, n) 355 | } 356 | } 357 | 358 | if err := d.Decode(&txin.Sequence); err != nil { 359 | return err 360 | } 361 | 362 | return nil 363 | } 364 | 365 | // UnmarshalBinary implements binary.Unmarshaler interface. 366 | func (txout *TxOutput) UnmarshalBinary(r io.Reader) error { 367 | d := binary.NewDecoder(r) 368 | 369 | if err := d.Decode(&txout.Value); err != nil { 370 | return err 371 | } 372 | 373 | if err := d.Decode(&txout.PkScriptLength); err != nil { 374 | return err 375 | } 376 | 377 | txout.PkScript = make([]byte, txout.PkScriptLength) 378 | n, err := io.LimitReader(r, int64(txout.PkScriptLength)).Read(txout.PkScript) 379 | if err != nil { 380 | return err 381 | } 382 | 383 | if int64(n) != int64(txout.PkScriptLength) { 384 | return fmt.Errorf("invalid output script was read: want %d bytes, got %d bytes", txout.PkScriptLength, n) 385 | } 386 | 387 | return nil 388 | } 389 | -------------------------------------------------------------------------------- /protocol/msg_tx_test.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "bytes" 5 | "encoding/hex" 6 | "testing" 7 | 8 | "github.com/google/go-cmp/cmp" 9 | ) 10 | 11 | func TestMsgHash(t *testing.T) { 12 | tests := []struct { 13 | name string 14 | txmsg string 15 | expected string 16 | err error 17 | }{ 18 | {name: "ok", 19 | txmsg: "010000000141ccfc44eab10fa75d60dd9dd90e6c8c41cb880a10ef99bb3896bca6af796199000000006a47304402201276349a2710b36068447612019edb3c55c4e2a2720dfd22550da91f41cdfe2a0220347037d93994cf0032647f8ab64c9aeffdf73edb044818e438a4825e8648dc800121036b9b2e7bf8a167f2e26db84a6c30f4501bf25d5c53e6414291a724c32146763affffffff02020f1c00000000001976a9146e62bcf043873843bf5ba5245c6f8b185664956c88ac4bb12104000000001976a91427c22ca26f837c70e74a7b881b8a1d1405f8350988ac00000000", 20 | expected: "51c564bb064c2e98eef0dd79cc1ecd4cbd64acb2d1878aeffb8bf039a016d9c4", 21 | err: nil}, 22 | } 23 | 24 | for _, test := range tests { 25 | t.Run(test.name, func(tt *testing.T) { 26 | tx := MsgTx{} 27 | 28 | txmsg, err := hex.DecodeString(test.txmsg) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | 33 | r := bytes.NewBuffer(txmsg) 34 | err = tx.UnmarshalBinary(r) 35 | if err != nil { 36 | tt.Fatalf("unexpected error: %+v", err) 37 | } 38 | 39 | txhash, err := tx.Hash() 40 | if err != nil && test.err == nil { 41 | tt.Errorf("unexpected error: %+v", err) 42 | return 43 | } 44 | 45 | if err == nil && test.err != nil { 46 | tt.Errorf("expected error: %+v, got:%+v", test.err, tx) 47 | return 48 | } 49 | 50 | got := hex.EncodeToString(txhash) 51 | if diff := cmp.Diff(test.expected, got); diff != "" { 52 | tt.Errorf("Hash() mismatch(-want +got):\n%s", diff) 53 | return 54 | } 55 | }) 56 | } 57 | } 58 | 59 | func TestMsgTxMarshalBinary(t *testing.T) { 60 | tests := []struct { 61 | name string 62 | txmsg string 63 | err error 64 | }{ 65 | {name: "legacy", 66 | txmsg: "0100000001317c144ae5b5a224370bd68c928b9f9e152d9829235ffbecec5ee64113662fc4000000006a47304402203c6ef3cba423365b37c031d235a674a10cf06b14fccda68bb5c35cbda5a2969b02207da3f69ea61c4a98eb488dac9d8a421dda9000e8afdc4a90cc2ebf93fbefb84f012102e248c2b8e9a5b78f2406c60b75ef1c4e88a06c7c36ad31e009db256505e27e79ffffffff0388270c00000000001976a914fe46ec55e937e584005b337495d76464b6b1cdba88ac22020000000000001976a914bdcccc7ce08a732ce55dcc3c1d8890e372bf7c1d88ac0000000000000000166a146f6d6e69000000000000001f0000886c98b7600000000000", 67 | err: nil}, 68 | {name: "segwit", 69 | txmsg: "0100000000010145b87f940bc57475403a3928ecf4cb3b86d2ba192039d4d703126edad14487ca0100000000ffffffff0200093d000000000017a91469f375f23b3d5d37bd942f3c31d7ae5a0cb61f5e87c8db030000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d0400473044022025863cfe71648bc8703f9f0607558cb7e79fcbebadc080ef1f0d7bfdd6ab1afa0220101ffaeb01b70e3360e87d6b3616886e547593e41c8a09d00cf8803601a9cc7901473044022031caba2ba6b079bc0d995e04f3651f977b5fc22dacceab1046a311fa2fb83898022030f5a852b425bdeb156be3a0a3de5bbc764302fbc1b7ca77058740cd511d49a9016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000", 70 | err: nil}, 71 | } 72 | 73 | for _, test := range tests { 74 | t.Run(test.name, func(tt *testing.T) { 75 | tx := MsgTx{} 76 | 77 | txmsg, err := hex.DecodeString(test.txmsg) 78 | if err != nil { 79 | t.Fatal(err) 80 | } 81 | 82 | r := bytes.NewBuffer(txmsg) 83 | err = tx.UnmarshalBinary(r) 84 | if err != nil { 85 | tt.Fatalf("unexpected error: %+v", err) 86 | } 87 | 88 | got, err := tx.MarshalBinary() 89 | if err != nil && test.err == nil { 90 | tt.Errorf("unexpected error: %+v", err) 91 | return 92 | } 93 | 94 | if err == nil && test.err != nil { 95 | tt.Errorf("expected error: %+v, got:%+v", test.err, tx) 96 | return 97 | } 98 | 99 | if diff := cmp.Diff(txmsg, got); diff != "" { 100 | tt.Errorf("MarshalBinary() mismatch(-want +got):\n%s", diff) 101 | return 102 | } 103 | }) 104 | } 105 | 106 | } 107 | 108 | func TestMsgTxUnmarshalBinary(t *testing.T) { 109 | tests := []struct { 110 | name string 111 | input string 112 | expected *MsgTx 113 | err error 114 | }{ 115 | {name: "legacy", 116 | input: "0100000001317c144ae5b5a224370bd68c928b9f9e152d9829235ffbecec5ee64113662fc4000000006a47304402203c6ef3cba423365b37c031d235a674a10cf06b14fccda68bb5c35cbda5a2969b02207da3f69ea61c4a98eb488dac9d8a421dda9000e8afdc4a90cc2ebf93fbefb84f012102e248c2b8e9a5b78f2406c60b75ef1c4e88a06c7c36ad31e009db256505e27e79ffffffff0388270c00000000001976a914fe46ec55e937e584005b337495d76464b6b1cdba88ac22020000000000001976a914bdcccc7ce08a732ce55dcc3c1d8890e372bf7c1d88ac0000000000000000166a146f6d6e69000000000000001f0000886c98b7600000000000", 117 | expected: &MsgTx{ 118 | Version: 1, 119 | Flag: 0, 120 | TxInCount: 1, 121 | TxIn: []TxInput{ 122 | {PreviousOutput: OutPoint{ 123 | Hash: [32]byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}, 124 | Index: 0}, 125 | ScriptLength: 106, 126 | SignatureScript: []byte{ 127 | 0x47, 0x30, 0x44, 0x02, 0x20, 0x3c, 0x6e, 0xf3, 0xcb, 0xa4, 0x23, 0x36, 0x5b, 0x37, 0xc0, 0x31, 128 | 0xd2, 0x35, 0xa6, 0x74, 0xa1, 0x0c, 0xf0, 0x6b, 0x14, 0xfc, 0xcd, 0xa6, 0x8b, 0xb5, 0xc3, 0x5c, 129 | 0xbd, 0xa5, 0xa2, 0x96, 0x9b, 0x02, 0x20, 0x7d, 0xa3, 0xf6, 0x9e, 0xa6, 0x1c, 0x4a, 0x98, 0xeb, 130 | 0x48, 0x8d, 0xac, 0x9d, 0x8a, 0x42, 0x1d, 0xda, 0x90, 0x00, 0xe8, 0xaf, 0xdc, 0x4a, 0x90, 0xcc, 131 | 0x2e, 0xbf, 0x93, 0xfb, 0xef, 0xb8, 0x4f, 0x01, 0x21, 0x02, 0xe2, 0x48, 0xc2, 0xb8, 0xe9, 0xa5, 132 | 0xb7, 0x8f, 0x24, 0x06, 0xc6, 0x0b, 0x75, 0xef, 0x1c, 0x4e, 0x88, 0xa0, 0x6c, 0x7c, 0x36, 0xad, 133 | 0x31, 0xe0, 0x09, 0xdb, 0x25, 0x65, 0x05, 0xe2, 0x7e, 0x79, 134 | }, 135 | Sequence: 4294967295}}, 136 | TxOutCount: 3, 137 | TxOut: []TxOutput{ 138 | {Value: 796552, 139 | PkScriptLength: 25, 140 | PkScript: []byte{ 141 | 0x76, 0xa9, 0x14, 0xfe, 0x46, 0xec, 0x55, 0xe9, 0x37, 0xe5, 0x84, 0x00, 0x5b, 0x33, 0x74, 0x95, 142 | 0xd7, 0x64, 0x64, 0xb6, 0xb1, 0xcd, 0xba, 0x88, 0xac, 143 | }, 144 | }, 145 | {Value: 546, 146 | PkScriptLength: 25, 147 | PkScript: []byte{ 148 | 0x76, 0xa9, 0x14, 0xbd, 0xcc, 0xcc, 0x7c, 0xe0, 0x8a, 0x73, 0x2c, 0xe5, 0x5d, 0xcc, 0x3c, 0x1d, 149 | 0x88, 0x90, 0xe3, 0x72, 0xbf, 0x7c, 0x1d, 0x88, 0xac, 150 | }, 151 | }, 152 | {Value: 0, 153 | PkScriptLength: 22, 154 | PkScript: []byte{ 155 | 0x6a, 0x14, 0x6f, 0x6d, 0x6e, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 156 | 0x88, 0x6c, 0x98, 0xb7, 0x60, 0x00, 157 | }, 158 | }, 159 | }, 160 | TxWitness: TxWitnessData{Count: 0, Witness: nil}, 161 | LockTime: 0}, 162 | err: nil}, 163 | {name: "segwit", 164 | input: "0100000000010145b87f940bc57475403a3928ecf4cb3b86d2ba192039d4d703126edad14487ca0100000000ffffffff0200093d000000000017a91469f375f23b3d5d37bd942f3c31d7ae5a0cb61f5e87c8db030000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d0400473044022025863cfe71648bc8703f9f0607558cb7e79fcbebadc080ef1f0d7bfdd6ab1afa0220101ffaeb01b70e3360e87d6b3616886e547593e41c8a09d00cf8803601a9cc7901473044022031caba2ba6b079bc0d995e04f3651f977b5fc22dacceab1046a311fa2fb83898022030f5a852b425bdeb156be3a0a3de5bbc764302fbc1b7ca77058740cd511d49a9016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000", 165 | expected: &MsgTx{ 166 | Version: 1, 167 | Flag: 1, 168 | TxInCount: 1, 169 | TxIn: []TxInput{ 170 | {PreviousOutput: OutPoint{ 171 | Hash: [32]uint8{0x45, 0xb8, 0x7f, 0x94, 0x0b, 0xc5, 0x74, 0x75, 0x40, 0x3a, 0x39, 0x28, 0xec, 0xf4, 0xcb, 0x3b, 0x86, 0xd2, 0xba, 0x19, 0x20, 0x39, 0xd4, 0xd7, 0x03, 0x12, 0x6e, 0xda, 0xd1, 0x44, 0x87, 0xca}, 172 | Index: 0x01}, 173 | Sequence: 0xffffffff}, 174 | }, 175 | TxOutCount: 2, 176 | TxOut: []TxOutput{ 177 | {Value: 4000000, 178 | PkScriptLength: 0x17, 179 | PkScript: []uint8{ 180 | 0xa9, 0x14, 0x69, 0xf3, 0x75, 0xf2, 0x3b, 0x3d, 0x5d, 0x37, 0xbd, 0x94, 0x2f, 0x3c, 0x31, 0xd7, 181 | 0xae, 0x5a, 0x0c, 0xb6, 0x1f, 0x5e, 0x87, 182 | }, 183 | }, 184 | { 185 | Value: 252872, 186 | PkScriptLength: 0x22, 187 | PkScript: []byte{ 188 | 0x00, 0x20, 0x70, 0x1a, 0x8d, 0x40, 0x1c, 0x84, 0xfb, 0x13, 0xe6, 0xba, 0xf1, 0x69, 0xd5, 0x96, 189 | 0x84, 0xe1, 0x7a, 0xbd, 0x9f, 0xa2, 0x16, 0xc8, 0xcc, 0x5b, 0x9f, 0xc6, 0x3d, 0x62, 0x2f, 0xf8, 190 | 0xc5, 0x8d, 191 | }, 192 | }, 193 | }, 194 | TxWitness: TxWitnessData{ 195 | Count: 0x04, 196 | Witness: []TxWitness{ 197 | {}, 198 | { 199 | Length: 0x47, 200 | Data: []uint8{ 201 | 0x30, 0x44, 0x02, 0x20, 0x25, 0x86, 0x3c, 0xfe, 0x71, 0x64, 0x8b, 0xc8, 0x70, 0x3f, 0x9f, 0x06, 202 | 0x07, 0x55, 0x8c, 0xb7, 0xe7, 0x9f, 0xcb, 0xeb, 0xad, 0xc0, 0x80, 0xef, 0x1f, 0x0d, 0x7b, 0xfd, 203 | 0xd6, 0xab, 0x1a, 0xfa, 0x02, 0x20, 0x10, 0x1f, 0xfa, 0xeb, 0x01, 0xb7, 0x0e, 0x33, 0x60, 0xe8, 204 | 0x7d, 0x6b, 0x36, 0x16, 0x88, 0x6e, 0x54, 0x75, 0x93, 0xe4, 0x1c, 0x8a, 0x09, 0xd0, 0x0c, 0xf8, 205 | 0x80, 0x36, 0x01, 0xa9, 0xcc, 0x79, 0x01, 206 | }, 207 | }, 208 | { 209 | Length: 0x47, 210 | Data: []byte{ 211 | 0x30, 0x44, 0x02, 0x20, 0x31, 0xca, 0xba, 0x2b, 0xa6, 0xb0, 0x79, 0xbc, 0x0d, 0x99, 0x5e, 0x04, 212 | 0xf3, 0x65, 0x1f, 0x97, 0x7b, 0x5f, 0xc2, 0x2d, 0xac, 0xce, 0xab, 0x10, 0x46, 0xa3, 0x11, 0xfa, 213 | 0x2f, 0xb8, 0x38, 0x98, 0x02, 0x20, 0x30, 0xf5, 0xa8, 0x52, 0xb4, 0x25, 0xbd, 0xeb, 0x15, 0x6b, 214 | 0xe3, 0xa0, 0xa3, 0xde, 0x5b, 0xbc, 0x76, 0x43, 0x02, 0xfb, 0xc1, 0xb7, 0xca, 0x77, 0x05, 0x87, 215 | 0x40, 0xcd, 0x51, 0x1d, 0x49, 0xa9, 0x01, 216 | }, 217 | }, 218 | { 219 | Length: 0x69, 220 | Data: []uint8{ 221 | 0x52, 0x21, 0x03, 0x75, 0xe0, 0x0e, 0xb7, 0x2e, 0x29, 0xda, 0x82, 0xb8, 0x93, 0x67, 0x94, 0x7f, 222 | 0x29, 0xef, 0x34, 0xaf, 0xb7, 0x5e, 0x86, 0x54, 0xf6, 0xea, 0x36, 0x8e, 0x0a, 0xcd, 0xfd, 0x92, 223 | 0x97, 0x6b, 0x7c, 0x21, 0x03, 0xa1, 0xb2, 0x63, 0x13, 0xf4, 0x30, 0xc4, 0xb1, 0x5b, 0xb1, 0xfd, 224 | 0xce, 0x66, 0x32, 0x07, 0x65, 0x9d, 0x8c, 0xac, 0x74, 0x9a, 0x0e, 0x53, 0xd7, 0x0e, 0xff, 0x01, 225 | 0x87, 0x44, 0x96, 0xfe, 0xff, 0x21, 0x03, 0xc9, 0x6d, 0x49, 0x5b, 0xfd, 0xd5, 0xba, 0x41, 0x45, 226 | 0xe3, 0xe0, 0x46, 0xfe, 0xe4, 0x5e, 0x84, 0xa8, 0xa4, 0x8a, 0xd0, 0x5b, 0xd8, 0xdb, 0xb3, 0x95, 227 | 0xc0, 0x11, 0xa3, 0x2c, 0xf9, 0xf8, 0x80, 0x53, 0xae, 228 | }, 229 | }, 230 | }, 231 | }, 232 | LockTime: 0}, 233 | err: nil}, 234 | } 235 | 236 | for _, test := range tests { 237 | t.Run(test.name, func(tt *testing.T) { 238 | tx := MsgTx{} 239 | 240 | input, err := hex.DecodeString(test.input) 241 | if err != nil { 242 | t.Fatal(err) 243 | } 244 | 245 | r := bytes.NewBuffer(input) 246 | err = tx.UnmarshalBinary(r) 247 | 248 | if err != nil && test.err == nil { 249 | tt.Errorf("unexpected error:%+v", err) 250 | return 251 | } 252 | 253 | if err == nil && test.err != nil { 254 | tt.Errorf("expected error: %+v, got:%+v", test.err, tx) 255 | return 256 | } 257 | 258 | if diff := cmp.Diff(test.expected, &tx); diff != "" { 259 | tt.Errorf("UnmarshalBinary() mismatch(-want +got):\n%s", diff) 260 | return 261 | } 262 | }) 263 | } 264 | 265 | } 266 | 267 | func TestMsgTxVerify(t *testing.T) { 268 | tests := []struct { 269 | name string 270 | tx *MsgTx 271 | expected error 272 | }{ 273 | {name: "ok", 274 | tx: &MsgTx{ 275 | Version: 1, 276 | Flag: 0, 277 | TxInCount: 1, 278 | TxIn: []TxInput{ 279 | {PreviousOutput: OutPoint{ 280 | Hash: [32]byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}, 281 | Index: 0}, 282 | ScriptLength: 106, 283 | SignatureScript: []byte{ 284 | 0x47, 0x30, 0x44, 0x02, 0x20, 0x3c, 0x6e, 0xf3, 0xcb, 0xa4, 0x23, 0x36, 0x5b, 0x37, 0xc0, 0x31, 285 | 0xd2, 0x35, 0xa6, 0x74, 0xa1, 0x0c, 0xf0, 0x6b, 0x14, 0xfc, 0xcd, 0xa6, 0x8b, 0xb5, 0xc3, 0x5c, 286 | 0xbd, 0xa5, 0xa2, 0x96, 0x9b, 0x02, 0x20, 0x7d, 0xa3, 0xf6, 0x9e, 0xa6, 0x1c, 0x4a, 0x98, 0xeb, 287 | 0x48, 0x8d, 0xac, 0x9d, 0x8a, 0x42, 0x1d, 0xda, 0x90, 0x00, 0xe8, 0xaf, 0xdc, 0x4a, 0x90, 0xcc, 288 | 0x2e, 0xbf, 0x93, 0xfb, 0xef, 0xb8, 0x4f, 0x01, 0x21, 0x02, 0xe2, 0x48, 0xc2, 0xb8, 0xe9, 0xa5, 289 | 0xb7, 0x8f, 0x24, 0x06, 0xc6, 0x0b, 0x75, 0xef, 0x1c, 0x4e, 0x88, 0xa0, 0x6c, 0x7c, 0x36, 0xad, 290 | 0x31, 0xe0, 0x09, 0xdb, 0x25, 0x65, 0x05, 0xe2, 0x7e, 0x79, 291 | }, 292 | Sequence: 4294967295}}, 293 | TxOutCount: 3, 294 | TxOut: []TxOutput{ 295 | {Value: 796552, 296 | PkScriptLength: 25, 297 | PkScript: []byte{ 298 | 0x76, 0xa9, 0x14, 0xfe, 0x46, 0xec, 0x55, 0xe9, 0x37, 0xe5, 0x84, 0x00, 0x5b, 0x33, 0x74, 0x95, 299 | 0xd7, 0x64, 0x64, 0xb6, 0xb1, 0xcd, 0xba, 0x88, 0xac, 300 | }, 301 | }, 302 | {Value: 546, 303 | PkScriptLength: 25, 304 | PkScript: []byte{ 305 | 0x76, 0xa9, 0x14, 0xbd, 0xcc, 0xcc, 0x7c, 0xe0, 0x8a, 0x73, 0x2c, 0xe5, 0x5d, 0xcc, 0x3c, 0x1d, 306 | 0x88, 0x90, 0xe3, 0x72, 0xbf, 0x7c, 0x1d, 0x88, 0xac, 307 | }, 308 | }, 309 | {Value: 0, 310 | PkScriptLength: 22, 311 | PkScript: []byte{ 312 | 0x6a, 0x14, 0x6f, 0x6d, 0x6e, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 313 | 0x88, 0x6c, 0x98, 0xb7, 0x60, 0x00, 314 | }, 315 | }, 316 | }, 317 | TxWitness: TxWitnessData{Count: 0, Witness: nil}, 318 | LockTime: 0}, 319 | expected: nil}, 320 | {name: "empty inputs", 321 | tx: &MsgTx{ 322 | Version: 1, 323 | Flag: 0, 324 | TxInCount: 0, 325 | TxIn: []TxInput{}, 326 | TxOutCount: 3, 327 | TxOut: []TxOutput{ 328 | {Value: 796552, 329 | PkScriptLength: 25, 330 | PkScript: []byte{ 331 | 0x76, 0xa9, 0x14, 0xfe, 0x46, 0xec, 0x55, 0xe9, 0x37, 0xe5, 0x84, 0x00, 0x5b, 0x33, 0x74, 0x95, 332 | 0xd7, 0x64, 0x64, 0xb6, 0xb1, 0xcd, 0xba, 0x88, 0xac, 333 | }, 334 | }, 335 | {Value: 546, 336 | PkScriptLength: 25, 337 | PkScript: []byte{ 338 | 0x76, 0xa9, 0x14, 0xbd, 0xcc, 0xcc, 0x7c, 0xe0, 0x8a, 0x73, 0x2c, 0xe5, 0x5d, 0xcc, 0x3c, 0x1d, 339 | 0x88, 0x90, 0xe3, 0x72, 0xbf, 0x7c, 0x1d, 0x88, 0xac, 340 | }, 341 | }, 342 | {Value: 0, 343 | PkScriptLength: 22, 344 | PkScript: []byte{ 345 | 0x6a, 0x14, 0x6f, 0x6d, 0x6e, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 346 | 0x88, 0x6c, 0x98, 0xb7, 0x60, 0x00, 347 | }, 348 | }, 349 | }, 350 | TxWitness: TxWitnessData{Count: 0, Witness: nil}, 351 | LockTime: 0}, 352 | expected: errInvalidTransaction}, 353 | {name: "empty outputs", 354 | tx: &MsgTx{ 355 | Version: 1, 356 | Flag: 0, 357 | TxInCount: 1, 358 | TxIn: []TxInput{ 359 | {PreviousOutput: OutPoint{ 360 | Hash: [32]byte{0x31, 0x7c, 0x14, 0x4a, 0xe5, 0xb5, 0xa2, 0x24, 0x37, 0x0b, 0xd6, 0x8c, 0x92, 0x8b, 0x9f, 0x9e, 0x15, 0x2d, 0x98, 0x29, 0x23, 0x5f, 0xfb, 0xec, 0xec, 0x5e, 0xe6, 0x41, 0x13, 0x66, 0x2f, 0xc4}, 361 | Index: 0}, 362 | ScriptLength: 106, 363 | SignatureScript: []byte{ 364 | 0x47, 0x30, 0x44, 0x02, 0x20, 0x3c, 0x6e, 0xf3, 0xcb, 0xa4, 0x23, 0x36, 0x5b, 0x37, 0xc0, 0x31, 365 | 0xd2, 0x35, 0xa6, 0x74, 0xa1, 0x0c, 0xf0, 0x6b, 0x14, 0xfc, 0xcd, 0xa6, 0x8b, 0xb5, 0xc3, 0x5c, 366 | 0xbd, 0xa5, 0xa2, 0x96, 0x9b, 0x02, 0x20, 0x7d, 0xa3, 0xf6, 0x9e, 0xa6, 0x1c, 0x4a, 0x98, 0xeb, 367 | 0x48, 0x8d, 0xac, 0x9d, 0x8a, 0x42, 0x1d, 0xda, 0x90, 0x00, 0xe8, 0xaf, 0xdc, 0x4a, 0x90, 0xcc, 368 | 0x2e, 0xbf, 0x93, 0xfb, 0xef, 0xb8, 0x4f, 0x01, 0x21, 0x02, 0xe2, 0x48, 0xc2, 0xb8, 0xe9, 0xa5, 369 | 0xb7, 0x8f, 0x24, 0x06, 0xc6, 0x0b, 0x75, 0xef, 0x1c, 0x4e, 0x88, 0xa0, 0x6c, 0x7c, 0x36, 0xad, 370 | 0x31, 0xe0, 0x09, 0xdb, 0x25, 0x65, 0x05, 0xe2, 0x7e, 0x79, 371 | }, 372 | Sequence: 4294967295}}, 373 | TxOutCount: 0, 374 | TxOut: []TxOutput{}, 375 | TxWitness: TxWitnessData{Count: 0, Witness: nil}, 376 | LockTime: 0}, 377 | expected: errInvalidTransaction}, 378 | } 379 | 380 | for _, test := range tests { 381 | t.Run(test.name, func(tt *testing.T) { 382 | got := test.tx.Verify() 383 | 384 | if got != test.expected { 385 | tt.Errorf("expected: %+v, got: %+v", test.expected, got) 386 | } 387 | }) 388 | } 389 | } 390 | --------------------------------------------------------------------------------