Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4 Baking and Endorsing Fixes #181

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
ad2d661
Added Jenkinsfile
DefinitelyNotAGoat Aug 3, 2019
c2a26cc
Added Jenkinsfile
DefinitelyNotAGoat Aug 3, 2019
72fbf44
delegate: adding reward calc by projected earnings
DefinitelyNotAGoat Sep 8, 2019
a82e816
added ability to get endorsing rights at a specific hash/level
utdrmac Oct 5, 2019
21571a4
add delegate for endorsing slots
utdrmac Oct 7, 2019
75a3b05
add GetEndorsingRightsAtLevel
utdrmac Oct 7, 2019
246d546
missing tick
utdrmac Oct 8, 2019
6e60b2f
fixed docs
utdrmac Oct 8, 2019
6e9f718
cannot return nil for object
utdrmac Oct 8, 2019
6eb73dd
added GetBakingRightsAtLevel
utdrmac Oct 10, 2019
28f283d
added param for baking rights
utdrmac Oct 10, 2019
2f8a6ee
added double baking evidence
utdrmac Oct 14, 2019
de7c2b0
Merge branch 'endorsingRights' of https://github.com/utdrmac/go-tezos…
utdrmac Oct 14, 2019
86e4988
added getting the delegate for address at block
utdrmac Oct 16, 2019
da0b47e
added GetBakingRightsForDelegateAtHashLevel
utdrmac Oct 16, 2019
538279d
added ability to forge/sign/inject endorsements
Oct 16, 2019
cf067d5
Merge branch 'master' of https://github.com/DefinitelyNotAGoat/go-tezos
utdrmac Oct 23, 2019
15c3457
support for forging endorsements
Oct 23, 2019
d8c45eb
Merge branch 'master' into create_endorsements
Oct 23, 2019
fa4cb7e
work on forging blocks
Dec 14, 2019
4b8c606
merged endorsingRights, create_endorsements
Dec 14, 2019
6819c5c
merge v2 upstream
utdrmac Aug 22, 2020
1e24ba1
Add support for signing endorsement operations
utdrmac Aug 23, 2020
8cdd1ca
Make B58cencode a public function
utdrmac Aug 23, 2020
5a36dde
updates for bakings
utdrmac Sep 16, 2020
4dbd183
exposes Tezos NetworkConstants for use by library users
utdrmac Sep 16, 2020
d236342
not sure what that was; refreshed crypto.go from upstream master
utdrmac Sep 16, 2020
5a0027d
Merge branch 'expose_constants' into baking
utdrmac Sep 16, 2020
d8ec19f
added block forging and baking abilities
utdrmac Sep 30, 2020
0ae2124
merge v3
utdrmac Sep 30, 2020
a69f8e9
reverting master to upstream@6fecc94
utdrmac Sep 30, 2020
04ab5cc
Update go.mod
utdrmac Oct 1, 2020
d895a4e
Update contracts.go
utdrmac Oct 1, 2020
a56e179
leanring go mods
utdrmac Oct 1, 2020
463ef5e
Update go.mod
utdrmac Oct 1, 2020
4efe249
Update go.mod
utdrmac Oct 9, 2020
866eec6
Update go.mod
utdrmac Oct 9, 2020
8ebe21a
moved crypto to root
utdrmac Oct 9, 2020
429f50c
rename goat to utdrmac
utdrmac Oct 9, 2020
7a91306
bigEndian forgeInt32
utdrmac Oct 9, 2020
1eb4048
metadata omitempty
utdrmac Oct 10, 2020
ba512ed
make networkconstants exported
utdrmac Oct 10, 2020
e1bfed7
merging v2 baking functions and structs
utdrmac Oct 10, 2020
2dd2924
more v2->v3 baking
utdrmac Oct 10, 2020
6dbcf46
more v2->v3 baking
utdrmac Oct 10, 2020
6878b55
more v2->v3 baking
utdrmac Oct 10, 2020
b11ee3f
back to goat-systems
utdrmac Oct 11, 2020
c11c290
Change []Errors to []Error
utdrmac Oct 13, 2020
e5634de
Merge branch 'errors_to_error' into v3_endorsing_baking
utdrmac Oct 13, 2020
4aa6d58
return rpc response on failure
utdrmac Oct 19, 2020
9cf6c6c
unmarshal time_between_blocks to int
utdrmac Oct 19, 2020
d162a9f
Merge branch 'master' of https://github.com/goat-systems/go-tezos
utdrmac Jan 20, 2021
c497a24
mergerd
utdrmac Jan 20, 2021
20c2604
merge master
utdrmac Jan 20, 2021
5cb3a6e
Merge branch 'v3_endorsing_baking' into v4_baking_endorsing
utdrmac Jan 20, 2021
c7646ec
- Fix spelling of constants
utdrmac Jan 21, 2021
8a642df
fix README examples
utdrmac Jan 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package main

import (
"fmt"
goTezos "github.com/goat-systems/go-tezos/v4/rpc"
"github.com/goat-systems/go-tezos/v4/rpc"
)

func main() {
rpc, err := client.New("http://127.0.0.1:8732")
client, err := rpc.New("http://127.0.0.1:8732")
if err != nil {
fmt.Printf("failed tp connect to network: %v", err)
}
Expand All @@ -31,7 +31,7 @@ func main() {
fmt.Printf("failed to get (%s) head block: %s\n", resp.Status(), err.Error())
os.Exit(1)
}
fmt.Println(block)
fmt.Println("Current Head Hash:", head.Hash)
}
```

Expand Down
File renamed without changes.
50 changes: 49 additions & 1 deletion rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,38 @@ type Operations struct {
Signature string `json:"signature,omitempty"`
}

/*
OperationsAlt represents a JSON array containing an opHash at index 0, and
an Operation object at index 1. The RPC does not properly objectify Refused,
BranchRefused, BranchDelayed, and Unprocessed sections of the mempool,
so we must parse them manually.
Code hints used from github.com/blockwatch-cc/tzindex/rpc/mempool.go
*/
type OperationsAlt Operations

func (o *OperationsAlt) UnmarshalJSON(buf []byte) error {
return unmarshalNamedJSONArray(buf, &o.Hash, (*Operations)(o))
}

func unmarshalNamedJSONArray(data []byte, v ...interface{}) error {
var raw []json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}

if len(raw) < len(v) {
return fmt.Errorf("JSON array is too short, expected %d, got %d", len(v), len(raw))
}

for i, vv := range v {
if err := json.Unmarshal(raw[i], vv); err != nil {
return err
}
}

return nil
}

/*
OrganizedContents represents the contents in Tezos operations orginized by kind.

Expand Down Expand Up @@ -2017,6 +2049,22 @@ type MinimalValidTimeInput struct {
EndorsingPower int
}

func (i *MinimalValidTimeInput) contructRPCOptions() []rpcOptions {
var opts []rpcOptions
opts = append(opts, rpcOptions{
"priority",
strconv.Itoa(i.Priority),
})

// Endorsing power
opts = append(opts, rpcOptions{
"endorsing_power",
strconv.Itoa(i.EndorsingPower),
})
return opts
}


/*
MinimalValidTime returns the minimal valid time for a block given a priority and an endorsing power.

Expand All @@ -2026,7 +2074,7 @@ RPC
https://tezos.gitlab.io/008/rpc.html#get-block-id-minimal-valid-time
*/
func (c *Client) MinimalValidTime(input MinimalValidTimeInput) (*resty.Response, time.Time, error) {
resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/minimal_valid_time", c.chain, input.BlockID.ID()))
resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/minimal_valid_time", c.chain, input.BlockID.ID()), input.contructRPCOptions()...)
if err != nil {
return resp, time.Time{}, errors.Wrapf(err, "failed to get minimal valid time at '%s'", input.BlockID.ID())
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (c *Client) SetChain(chain string) {
c.chain = chain
}

// CurrentContstants returns the constants used on the client
func (c *Client) CurrentContstants() Constants {
// CurrentConstants returns the constants used on the client
func (c *Client) CurrentConstants() Constants {
return *c.networkConstants
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Test_New(t *testing.T) {
r, err := rpc.New(server.URL)
checkErr(t, tt.wantErr, "", err)
if err == nil {
assert.Equal(t, *tt.wantConstants, r.CurrentContstants())
assert.Equal(t, *tt.wantConstants, r.CurrentConstants())
}
})
}
Expand All @@ -51,7 +51,7 @@ func Test_SetConstants(t *testing.T) {
var constants rpc.Constants
r.SetConstants(constants)

assert.Equal(t, constants, r.CurrentContstants())
assert.Equal(t, constants, r.CurrentConstants())
}

func gtGoldenHTTPMock(next http.Handler) http.Handler {
Expand Down
4 changes: 2 additions & 2 deletions rpc/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ type Constants struct {
BlocksPerCommitment int `json:"blocks_per_commitment"`
BlocksPerRollSnapshot int `json:"blocks_per_roll_snapshot"`
BlocksPerVotingPeriod int `json:"blocks_per_voting_period"`
TimeBetweenBlocks []string `json:"time_between_blocks"`
TimeBetweenBlocks IntArray `json:"time_between_blocks"`
EndorsersPerBlock int `json:"endorsers_per_block"`
HardGasLimitPerOperation int `json:"hard_gas_limit_per_operation,string"`
HardGasLimitPerBlock int `json:"hard_gas_limit_per_block,string"`
ProofOfWorkThreshold string `json:"proof_of_work_threshold"`
ProofOfWorkThreshold uint64 `json:"proof_of_work_threshold,string"`
TokensPerRoll string `json:"tokens_per_roll"`
MichelsonMaximumTypeSize int `json:"michelson_maximum_type_size"`
SeedNonceRevelationTip string `json:"seed_nonce_revelation_tip"`
Expand Down
10 changes: 5 additions & 5 deletions rpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ RPC:
*/
type ForgeBlockHeaderBody struct {
Level int `json:"level"`
Proto string `json:"proto"`
Proto int `json:"proto"`
Predecessor string `json:"predecessor"`
Timestamp time.Time `json:"timestamp"`
ValidationPass int `json:"validation_pass"`
Expand Down Expand Up @@ -697,14 +697,14 @@ func (p *PreapplyBlockInput) constructRPCOptions() []rpcOptions {
if p.Sort {
options = append(options, rpcOptions{
"sort",
"True",
"true",
})
}

if p.Timestamp != nil {
options = append(options, rpcOptions{
"timestamp",
p.Timestamp.String(),
strconv.FormatInt(p.Timestamp.Unix(), 10),
})
}

Expand Down Expand Up @@ -732,7 +732,7 @@ type PreapplyBlockProtocolData struct {
Protocol string `json:"protocol"`
Priority int `json:"priority"`
ProofOfWorkNonce string `json:"proof_of_work_nonce"`
SeedNonceHash string `json:"seed_nonce_hash"`
SeedNonceHash string `json:"seed_nonce_hash,omitempty"`
Signature string `json:"signature"`
}

Expand All @@ -744,7 +744,7 @@ RPC:
*/
type PreappliedBlock struct {
ShellHeader HeaderShell `json:"shell_header"`
Operations []PreappliedBlockOperations `json:"oeprations"`
Operations []PreappliedBlockOperations `json:"operations"`
}

/*
Expand Down
128 changes: 125 additions & 3 deletions rpc/independent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"encoding/json"
"fmt"
"time"

validator "github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -90,8 +91,13 @@ RPC:
https://tezos.gitlab.io/shell/rpc.html#post-injection-block
*/
type InjectionBlockInput struct {
// Block to inject
Block *Block `validate:"required"`

// Block header signature
SignedBlock string `validate:"required"`

// Operations to include in the block.
// This is not the same as operations found in mempool and also not like preapply result
Operations [][]interface{} `validate:"required"`

// If ?async is true, the function returns immediately.
Async bool
Expand Down Expand Up @@ -150,10 +156,25 @@ func (c *Client) InjectionBlock(input InjectionBlockInput) (*resty.Response, err
return nil, errors.Wrap(err, "failed to inject block: invalid input")
}

resp, err := c.post("/injection/block", *input.Block, input.contructRPCOptions()...)
// Create an anonymous struct containing the data required by RPC
newBlock := struct {
SignedBlock string `json:"data"`
Ops [][]interface{} `json:"operations"`
}{
input.SignedBlock,
input.Operations,
}

v, err := json.Marshal(newBlock)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal new block")
}

resp, err := c.post("/injection/block", v, input.contructRPCOptions()...)
if err != nil {
return resp, errors.Wrap(err, "failed to inject block")
}

return resp, nil
}

Expand Down Expand Up @@ -250,3 +271,104 @@ func (c *Client) ActiveChains() (*resty.Response, ActiveChains, error) {

return resp, activeChains, nil
}

/*
MempoolInput is the input for the goTezos.Mempool function.
Function:
func (c *Client) Mempool(input *MempoolInput) (Mempool, error) {}
*/
type MempoolInput struct {
// Mempool filters
Applied bool
BranchDelayed bool
Refused bool
BranchRefused bool
}

/*
Mempool represents the contents of the Tezos mempool.
RPC:
/chains/<chain_id>/mempool/pending_operations (GET)
*/
type Mempool struct {
Applied []Operations `json:"applied"`
Refused []OperationsAlt `json:"refused"`
BranchRefused []OperationsAlt `json:"branch_refused"`
BranchDelayed []OperationsAlt `json:"branch_delayed"`
Unprocessed []OperationsAlt `json:"unprocessed"`
}

/*
Mempool fetches the current contents of main the chain mempool.
Path:
/chains/<chain_id>/mempool/pending_operations (GET)
Parameters:
None
*/
func (c *Client) Mempool(input MempoolInput) (*resty.Response, *Mempool, error) {
resp, err := c.get(fmt.Sprintf("/chains/%s/mempool/pending_operations", c.chain), input.constructRPCOptions()...)
if err != nil {
return resp, &Mempool{}, errors.Wrap(err, "failed to fetch mempool contents")
}

var mempool Mempool
err = json.Unmarshal(resp.Body(), &mempool)
if err != nil {
return resp, &mempool, errors.Wrap(err, "failed to unmarshal mempool contents")
}

return resp, &mempool, nil
}

func (m *MempoolInput) constructRPCOptions() []rpcOptions {
var opts []rpcOptions
if m.Applied {
opts = append(opts, rpcOptions{
"applied",
"true",
})
} else {
opts = append(opts, rpcOptions{
"applied",
"false",
})
}

if m.BranchDelayed {
opts = append(opts, rpcOptions{
"branch_delayed",
"true",
})
} else {
opts = append(opts, rpcOptions{
"branch_delayed",
"false",
})
}

if m.Refused {
opts = append(opts, rpcOptions{
"refused",
"true",
})
} else {
opts = append(opts, rpcOptions{
"refused",
"false",
})
}

if m.BranchRefused {
opts = append(opts, rpcOptions{
"branch_refused",
"true",
})
} else {
opts = append(opts, rpcOptions{
"branch_refused",
"false",
})
}

return opts
}