Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.

Commit

Permalink
Stable version to replace the shadow service in darwinia.js (#7)
Browse files Browse the repository at this point in the history
* update: adapt lowercase request method with slash

* add: double node with merkle proof format

* add: basic scale lib

* add: proof encode

* update: adapt darwinia ethereum header

* update: support scale codec for darwinia ethereum header

* update: README

* update: add homebrew guide
  • Loading branch information
clearloop authored Apr 29, 2020
1 parent de18d20 commit 7ddcfb0
Show file tree
Hide file tree
Showing 14 changed files with 1,063 additions and 266 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
target
go.sum
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

[![Golang CI][workflow-badge]][github]

## Install

```
go get install "github.com/darwinia-network/darwinia.go/dargo"
```

## Config

`dargo` use the same config file with `darwinia.js`, if you don't know what
Expand All @@ -20,12 +14,28 @@ mkdir ~/.darwinia
echo '{"eth": { "api": "infura api with your key"}}' > ~/.darwinia/config.json
```

## Installation

Just supports OSX now

```
# Tap darwinia homebrew
brew tap darwinia-network/darwinia
# Install
brew install dargo
```

## Contribute and Build

```
# Clone darwinia.go
git clone https://github.com/darwinia-network/darwinia.go.git
cd darwinia.go/dargo
make
# Make the binary
cd darwinia.go/dargo && make
# Check the version
./target/dargo version
```

Expand Down Expand Up @@ -65,23 +75,25 @@ Fill the `~/.darwinia/config.json`
}
```

Run the service
## Shadow Service

```
# Start shadow service at port 3000
dargo shadow 3000
```

The shadow service of dargo follows the [spec][spec].

### Shadow.GetEthHeaderByNumber

```
curl -d '{"method":"Shadow.GetEthHeaderByNumber","params":[{"number": 0}], "id": 0}' http://127.0.0.1:3000
curl -d '{"method":"shadow_getEthHeaderByNumber","params":{"block_num": 0}, "id": 0}' http://127.0.0.1:3000
```

### Shadow.GetEthHeaderWithProofByNumber

```
curl -d '{"method":"Shadow.GetEthHeaderWithProofByNumber","params":[{"number": 1, "transcation": false, "options": {"format": "json"}}], "id": 0}' http://127.0.0.1:3000
curl -d '{"method":"shadow_getEthHeaderWithProofByNumber","params":{"block_num": 1, "transcation": false, "options": {"format": "json"}}, "id": 0}' http://127.0.0.1:3000
```

## Trouble Shooting
Expand All @@ -95,4 +107,5 @@ GPL-3.0


[github]: https://github.com/darwinia-network/darwinia.go
[spec]: https://github.com/darwinia-network/darwinia/wiki/Darwinia-offchain-worker-shadow-service-spec
[workflow-badge]: https://github.com/darwinia-network/darwinia.go/workflows/Golang%20CI/badge.svg
6 changes: 3 additions & 3 deletions cmd/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/darwinia-network/darwinia.go/core"
"github.com/darwinia-network/darwinia.go/lib"
"github.com/darwinia-network/darwinia.go/rpc"
"github.com/darwinia-network/darwinia.go/util"
"github.com/spf13/cobra"
)
Expand All @@ -19,8 +19,8 @@ var cmdShadow = &cobra.Command{
args = []string{"3000"}
}

fmt.Printf("Shadow service start at %s", args[0])
err := lib.Serve(
fmt.Printf("Shadow service start at %s\n", args[0])
err := rpc.ServeHTTP(
new(core.Shadow),
fmt.Sprintf(":%s", args[0]),
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ var cmdVersion = &cobra.Command{
Short: "Print the version number of dargo",
Long: `All software has versions. This is dargo's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("dargo 0.9")
fmt.Println("dargo 0.1.1")
},
}
70 changes: 70 additions & 0 deletions core/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package core

import (
"encoding/binary"
"encoding/hex"
"strings"

"github.com/darwinia-network/darwinia.go/util"
)

// Pack encode proof
func encodeProofArray(arr []util.DoubleNodeWithMerkleProof) string {
hex := "0x0101"
for _, v := range arr {
hex += encodeProof(v)
}

return hex
}

// Encode proof to hex with exist hex
func encodeProof(dnmp util.DoubleNodeWithMerkleProof) string {
hex := ""
for _, v := range dnmp.DagNodes {
hex += v[2:]
}

// pad the length
hex += "64"
for _, v := range dnmp.Proof {
hex += v[2:]
}

return hex
}

// Encode Darwinia Eth Header
func encodeDarwiniaEthHeader(header util.DarwiniaEthHeader) string {
hex := "0x"
hex += header.ParentHash[2:]
hex += encodeUint(header.TimeStamp, 64)
hex += encodeUint(header.Number, 64)
hex += strings.ToLower(header.Author[2:])
hex += header.TransactionsRoot[2:]
hex += header.UnclesHash[2:]
hex += "7c"
hex += header.ExtraData[2:]
hex += header.StateRoot[2:]
hex += header.ReceiptsRoot[2:]
hex += header.LogBloom[2:]
hex += encodeUint(header.GasUsed, 256)
hex += encodeUint(header.GasLimited, 256)
hex += encodeUint(header.Difficulty, 256)
hex += "0884"
hex += header.Seal[0][2:]
hex += "24"
hex += header.Seal[1][2:]
hex += "01"
hex += header.Hash[2:]

return hex
}

// Encode uint to hex
func encodeUint(n uint64, d int16) string {
b := make([]byte, d/8)
binary.LittleEndian.PutUint64(b, n)

return hex.EncodeToString(b)
}
54 changes: 47 additions & 7 deletions core/shadow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package core

import (
// "encoding/hex"
// "fmt"

"github.com/darwinia-network/darwinia.go/util"
"github.com/ethereum/go-ethereum/core/types"
)
Expand Down Expand Up @@ -31,27 +34,64 @@ func (s *Shadow) GetEthHeaderByNumber(
/**
* GetEthHeaderWithProofByNumber
*/
type GetEthHeaderWithProofByNumberOptions struct {
Format string `json:"format"`
}

type GetEthHeaderWithProofByNumberParams struct {
Number uint64 `json:"number"`
Number uint64 `json:"block_num"`
Options GetEthHeaderWithProofByNumberOptions `json:"options"`
}

type GetEthHeaderWithProofByNumberRawResp struct {
Header util.DarwiniaEthHeader `json:"eth_header"`
Proof []util.DoubleNodeWithMerkleProof `json:"proof"`
}

type GetEthHeaderWithProofByNumberJSONResp struct {
Header util.DarwiniaEthHeaderHexFormat `json:"eth_header"`
Proof []util.DoubleNodeWithMerkleProof `json:"proof"`
}

type GetEthHeaderWithProofByNumberResp struct {
Header types.Header `json:"header"`
Proof util.ProofOutput `json:"proof"`
type GetEthHeaderWithProofByNumberCodecResp struct {
Header string `json:"header"`
Proof string `json:"proof"`
}

func (s *Shadow) GetEthHeaderWithProofByNumber(
params GetEthHeaderWithProofByNumberParams,
resp *GetEthHeaderWithProofByNumberResp,
resp *interface{},
) error {
header, err := util.Header(params.Number)
resp.Header = header
if err != nil {
return err
}

rawResp := GetEthHeaderWithProofByNumberRawResp{}
rawResp.Header, err = util.IntoDarwiniaEthHeader(header)
if err != nil {
return err
}

// Proof header
proof, err := util.Proof(&header)
resp.Proof = proof
rawResp.Proof = proof.Format()

// Set response
*resp = rawResp

// Check if need codec
if params.Options.Format == "scale" {
*resp = GetEthHeaderWithProofByNumberCodecResp{
encodeDarwiniaEthHeader(rawResp.Header),
encodeProofArray(rawResp.Proof),
}
} else if params.Options.Format == "json" {
*resp = GetEthHeaderWithProofByNumberJSONResp{
rawResp.Header.HexFormat(),
rawResp.Proof,
}
}

return err
}
Loading

0 comments on commit 7ddcfb0

Please sign in to comment.