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

Jq/update setup docs #119

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
add get json methods for node manager (#7)
Co-authored-by: Charis Eryeer <eryeer@163.com>
  • Loading branch information
chenquanyu and eryeer authored Apr 29, 2022
commit 91ff1eb00dc64aced311573569402a9bb3641897
112 changes: 100 additions & 12 deletions contracts/native/go_abi/node_manager_abi/node_manager_abi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions contracts/native/governance/node_manager/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,33 @@ func emitEpochChange(s *native.NativeContract, curEpoch, nextEpoch *EpochInfo) e
func emitConsensusSign(s *native.NativeContract, sign *ConsensusSign, signer common.Address, num int) error {
return s.AddNotify(ABI, []string{EventConsensusSigned}, sign.Method, sign.Input, signer, uint64(num))
}

type MethodGetEpochListJsonInput struct {
EpochID uint64
}

func (m *MethodGetEpochListJsonInput) Encode() ([]byte, error) {
return utils.PackMethod(ABI, MethodGetEpochListJson, m.EpochID)
}
func (m *MethodGetEpochListJsonInput) Decode(payload []byte) error {
var data struct {
EpochID uint64
}
if err := utils.UnpackMethod(ABI, MethodGetEpochListJson, &data, payload); err != nil {
return err
}
m.EpochID = data.EpochID
return nil
}

type MethodGetJsonOutput struct {
Result string
}

func (m *MethodGetJsonOutput) Encode(methodName string) ([]byte, error) {
return utils.PackOutputs(ABI, methodName, m.Result)
}

func (m *MethodGetJsonOutput) Decode(payload []byte, methodName string) error {
return utils.UnpackOutputs(ABI, methodName, m, payload)
}
27 changes: 27 additions & 0 deletions contracts/native/governance/node_manager/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,30 @@ func TestABIMethodProofOutput(t *testing.T) {

assert.Equal(t, expect, got)
}

func TestABIMethodGetEpochListJsonInput(t *testing.T) {
expect := new(MethodGetEpochListJsonInput)
expect.EpochID = uint64(9932)
enc, err := expect.Encode()
assert.NoError(t, err)

got := new(MethodGetEpochListJsonInput)
assert.NoError(t, got.Decode(enc))

assert.Equal(t, expect, got)
}

func TestABIMethodGetJsonOutput(t *testing.T) {
expect := new(MethodGetJsonOutput)
epochID := uint64(2)
peers := generateTestPeers(7)
epoch := &EpochInfo{ID: epochID, Proposer: peers.List[0].Address, Peers: peers, StartHeight: 60}
expect.Result = epoch.Json()
enc, err := expect.Encode(MethodGetEpochListJson)
assert.NoError(t, err)

got := new(MethodGetJsonOutput)
assert.NoError(t, got.Decode(enc, MethodGetEpochListJson))

assert.Equal(t, expect, got)
}
68 changes: 57 additions & 11 deletions contracts/native/governance/node_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package node_manager
import (
"fmt"
"sort"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -48,6 +49,10 @@ var (
MethodGetEpochByID: 0,
MethodProof: 0,
MethodGetChangingEpoch: 0,

MethodGetChangingEpochJson: 0,
MethodGetCurrentEpochJson: 0,
MethodGetEpochListJson: 0,
}
)

Expand Down Expand Up @@ -83,6 +88,10 @@ func RegisterNodeManagerContract(s *native.NativeContract) {
s.Register(MethodGetEpochByID, GetEpochByID)
s.Register(MethodProof, GetEpochProof)
s.Register(MethodGetChangingEpoch, GetChangingEpoch)

s.Register(MethodGetChangingEpochJson, GetChangingEpochJson)
s.Register(MethodGetCurrentEpochJson, GetCurrentEpochJson)
s.Register(MethodGetEpochListJson, GetEpochListJson)
}

func Name(s *native.NativeContract) ([]byte, error) {
Expand Down Expand Up @@ -378,20 +387,10 @@ func GetEpochWithStateDB(db *state.StateDB) (*EpochInfo, error) {
}

func GetChangingEpoch(s *native.NativeContract) ([]byte, error) {
curEpochHash, err := getCurrentEpochHash(s)
if err != nil {
return utils.ByteFailed, err
}
epoch, err := getEpoch(s, curEpochHash)
epoch, err := getChangingEpoch(s)
if err != nil {
return utils.ByteFailed, err
}

height := s.ContractRef().BlockHeight().Uint64()
if height > epoch.StartHeight {
log.Warn("getChangingEpoch", "epoch changing invalidation, start height", epoch.StartHeight, "current height", height)
return utils.ByteFailed, fmt.Errorf("epoch invalid")
}
output := &MethodEpochOutput{Epoch: epoch}
return output.Encode()
}
Expand All @@ -416,6 +415,53 @@ func GetEpochByID(s *native.NativeContract) ([]byte, error) {
return output.Encode()
}

func GetEpochListJson(s *native.NativeContract) ([]byte, error) {
ctx := s.ContractRef().CurrentContext()

// decode input
input := new(MethodGetEpochListJsonInput)
if err := input.Decode(ctx.Payload); err != nil {
log.Trace("GetEpochListJson", "decode input failed", err)
return utils.ByteFailed, ErrInvalidInput
}

epochList, err := getEpochListByID(s, input.EpochID)
if err != nil {
log.Trace("GetEpochListJson", "get history epoch failed", err)
return utils.ByteFailed, ErrEpochNotExist
}
var str strings.Builder
str.WriteString("[")
for i, v := range epochList {
str.WriteString(v.Json())
if i != len(epochList)-1 {
str.WriteString(",")
}
}
str.WriteString("]")
output := MethodGetJsonOutput{Result: str.String()}
return output.Encode(MethodGetEpochListJson)
}

func GetCurrentEpochJson(s *native.NativeContract) ([]byte, error) {
epoch, err := getCurrentEpoch(s)
if err != nil {
log.Trace("epoch", "get current epoch failed", err)
return utils.ByteFailed, ErrEpochNotExist
}
output := &MethodGetJsonOutput{Result: epoch.Json()}
return output.Encode(MethodGetCurrentEpochJson)
}

func GetChangingEpochJson(s *native.NativeContract) ([]byte, error) {
epoch, err := getChangingEpoch(s)
if err != nil {
return utils.ByteFailed, err
}
output := &MethodGetJsonOutput{Result: epoch.Json()}
return output.Encode(MethodGetChangingEpochJson)
}

func GetEpochProof(s *native.NativeContract) ([]byte, error) {
ctx := s.ContractRef().CurrentContext()

Expand Down
55 changes: 55 additions & 0 deletions contracts/native/governance/node_manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package node_manager

import (
"crypto/rand"
"encoding/json"
"math/big"
"os"
"sort"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/contracts/native"
"github.com/ethereum/go-ethereum/contracts/native/go_abi/node_manager_abi"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -528,6 +530,17 @@ func TestProposalPassed(t *testing.T) {
curEpoch, err = getEpoch(ctx, epoch.Hash())
assert.NoError(t, err)
assert.Equal(t, ProposalStatusPassed, curEpoch.Status)

// get changing epoch
changingInput := &MethodGetChangingEpochInput{}
changingEpochPayload, err := changingInput.Encode()
assert.Nil(t, err)
ctx = generateNativeContract(common.EmptyAddress, int(proposalStartHeight-1))
enc, _, err := ctx.ContractRef().NativeCall(common.EmptyAddress, this, changingEpochPayload)
changingOutPut := &MethodEpochOutput{}
changingOutPut.Decode(enc)
assert.NoError(t, err)
assert.Equal(t, curEpoch.Hash(), changingOutPut.Epoch.Hash())
}

func TestDirtyJob(t *testing.T) {
Expand Down Expand Up @@ -608,6 +621,48 @@ func TestGetEpochByID(t *testing.T) {
assert.Equal(t, epoch.Hash(), output.Epoch.Hash())
}

func TestGetEpochListJson(t *testing.T) {
resetTestContext()

s := testEmptyCtx
epochID := uint64(2)
peers := generateTestPeers(12)
voters := []common.Address{peers.List[2].Address, peers.List[3].Address}

// store last epoch
lastEpoch := &EpochInfo{ID: epochID - 1, Proposer: peers.List[0].Address, Peers: peers, StartHeight: 60}
assert.NoError(t, storeEpoch(s, lastEpoch))
assert.NoError(t, storeProposal(s, lastEpoch.ID, lastEpoch.Hash()))

// store current useless epoch and votes
eps := []*EpochInfo{
{ID: epochID, Proposer: peers.List[0].Address, Peers: &Peers{List: peers.List[:5]}, StartHeight: 270},
{ID: epochID, Proposer: peers.List[1].Address, Peers: &Peers{List: peers.List[:6]}, StartHeight: 290},
}
for i, v := range eps {
assert.NoError(t, storeEpoch(s, v))
assert.NoError(t, storeProposal(s, v.ID, v.Hash()))
assert.NoError(t, storeVote(s, v.Hash(), voters[i]))
storeVoteTo(s, v.ID, voters[i], v.Hash())
}

input := new(MethodGetEpochListJsonInput)
input.EpochID = epochID
payload, err := input.Encode()
assert.NoError(t, err)
ctx := generateNativeContract(common.EmptyAddress, int(61))
enc, _, err := ctx.ContractRef().NativeCall(common.EmptyAddress, this, payload)
assert.NoError(t, err)

output := new(MethodGetJsonOutput)
assert.NoError(t, output.Decode(enc, node_manager_abi.MethodGetEpochListJson))
var outputEpochs []*EpochInfo
assert.NoError(t, json.Unmarshal([]byte(output.Result), &outputEpochs))
assert.Equal(t, eps[0].Hash(), outputEpochs[0].Hash())
assert.Equal(t, eps[1].Hash(), outputEpochs[1].Hash())
assert.Equal(t, 2, len(outputEpochs))
}

func TestGetProofByID(t *testing.T) {
resetTestContext()

Expand Down
Loading