Skip to content

Commit

Permalink
IOTEX-381 Add receipt for putpollresult (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhefeng authored and zjshen14 committed Apr 3, 2019
1 parent 04d8bf0 commit ed955b7
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
14 changes: 12 additions & 2 deletions action/protocol/account/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@ import (
"math/big"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
)

// ProtocolID is the protocol ID
// TODO: it works only for one instance per protocol definition now
const ProtocolID = "account"

// Protocol defines the protocol of handling account
type Protocol struct{}
type Protocol struct{ addr address.Address }

// NewProtocol instantiates the protocol of account
func NewProtocol() *Protocol { return &Protocol{} }
func NewProtocol() *Protocol {
h := hash.Hash160b([]byte(ProtocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of account protocol", zap.Error(err))
}
return &Protocol{addr: addr}
}

// Handle handles an account
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
Expand Down
9 changes: 5 additions & 4 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
}
}
return &action.Receipt{
Status: action.SuccessReceiptStatus,
BlockHeight: raCtx.BlockHeight,
ActionHash: raCtx.ActionHash,
GasConsumed: raCtx.IntrinsicGas,
Status: action.SuccessReceiptStatus,
BlockHeight: raCtx.BlockHeight,
ActionHash: raCtx.ActionHash,
GasConsumed: raCtx.IntrinsicGas,
ContractAddress: p.addr.String(),
}, nil
}

Expand Down
15 changes: 13 additions & 2 deletions action/protocol/execution/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
"context"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
)

const (
Expand All @@ -27,11 +30,19 @@ const (

// Protocol defines the protocol of handling executions
type Protocol struct {
cm protocol.ChainManager
cm protocol.ChainManager
addr address.Address
}

// NewProtocol instantiates the protocol of exeuction
func NewProtocol(cm protocol.ChainManager) *Protocol { return &Protocol{cm: cm} }
func NewProtocol(cm protocol.ChainManager) *Protocol {
h := hash.Hash160b([]byte(ProtocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
}
return &Protocol{cm: cm, addr: addr}
}

// Handle handles an execution
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
Expand Down
1 change: 1 addition & 0 deletions action/protocol/multichain/mainchain/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
return nil, errors.Wrapf(err, "error when handling stop sub-chain action")
}
}
// TODO: consider add receipt later
// The action is not handled by this handler or no error
return nil, nil
}
Expand Down
1 change: 1 addition & 0 deletions action/protocol/multichain/subchain/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
return nil, errors.Wrapf(err, "error when handling deposit settlement action")
}
}
// TODO: consider add receipt later
return nil, nil
}

Expand Down
35 changes: 30 additions & 5 deletions action/protocol/poll/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/crypto"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
Expand Down Expand Up @@ -60,6 +61,7 @@ type Protocol interface {

type lifeLongDelegatesProtocol struct {
delegates state.CandidateList
addr address.Address
}

// NewLifeLongDelegatesProtocol creates a poll protocol with life long delegates
Expand All @@ -77,7 +79,12 @@ func NewLifeLongDelegatesProtocol(delegates []genesis.Delegate) Protocol {
RewardAddress: rewardAddress.String(),
})
}
return &lifeLongDelegatesProtocol{delegates: l}
h := hash.Hash160b([]byte(ProtocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of poll protocol", zap.Error(err))
}
return &lifeLongDelegatesProtocol{delegates: l, addr: addr}
}

func (p *lifeLongDelegatesProtocol) Initialize(
Expand All @@ -89,7 +96,7 @@ func (p *lifeLongDelegatesProtocol) Initialize(
}

func (p *lifeLongDelegatesProtocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
return handle(ctx, act, sm)
return handle(ctx, act, sm, p.addr.String())
}

func (p *lifeLongDelegatesProtocol) Validate(ctx context.Context, act action.Action) error {
Expand Down Expand Up @@ -134,6 +141,7 @@ type governanceChainCommitteeProtocol struct {
initGravityChainHeight uint64
numCandidateDelegates uint64
numDelegates uint64
addr address.Address
}

// NewGovernanceChainCommitteeProtocol creates a Poll Protocol which fetch result from governance chain
Expand Down Expand Up @@ -161,6 +169,12 @@ func NewGovernanceChainCommitteeProtocol(
return nil, errors.New("getEpochNum api is not provided")
}

h := hash.Hash160b([]byte(ProtocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of poll protocol", zap.Error(err))
}

return &governanceChainCommitteeProtocol{
cm: cm,
electionCommittee: electionCommittee,
Expand All @@ -170,6 +184,7 @@ func NewGovernanceChainCommitteeProtocol(
getEpochNum: getEpochNum,
numCandidateDelegates: numCandidateDelegates,
numDelegates: numDelegates,
addr: addr,
}, nil
}

Expand All @@ -191,7 +206,7 @@ func (p *governanceChainCommitteeProtocol) Initialize(
}

func (p *governanceChainCommitteeProtocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
return handle(ctx, act, sm)
return handle(ctx, act, sm, p.addr.String())
}

func (p *governanceChainCommitteeProtocol) Validate(ctx context.Context, act action.Action) error {
Expand Down Expand Up @@ -363,14 +378,24 @@ func validateDelegates(cs state.CandidateList) error {
return nil
}

func handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
func handle(ctx context.Context, act action.Action, sm protocol.StateManager, protocolAddr string) (*action.Receipt, error) {
raCtx := protocol.MustGetRunActionsCtx(ctx)
r, ok := act.(*action.PutPollResult)
if !ok {
return nil, nil
}
zap.L().Debug("Handle PutPollResult Action", zap.Uint64("height", r.Height()))

return nil, setCandidates(sm, r.Candidates(), r.Height())
if err := setCandidates(sm, r.Candidates(), r.Height()); err != nil {
return nil, errors.Wrap(err, "failed to set candidates")
}
return &action.Receipt{
Status: action.SuccessReceiptStatus,
ActionHash: raCtx.ActionHash,
BlockHeight: raCtx.BlockHeight,
GasConsumed: raCtx.IntrinsicGas,
ContractAddress: protocolAddr,
}, nil
}

func validate(ctx context.Context, p Protocol, act action.Action) error {
Expand Down
24 changes: 18 additions & 6 deletions action/protocol/vote/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (
"math/big"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
"github.com/iotexproject/iotex-core/action/protocol/vote/candidatesutil"
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
)

Expand All @@ -32,11 +35,19 @@ const (

// Protocol defines the protocol of handling votes
type Protocol struct {
cm protocol.ChainManager
cm protocol.ChainManager
addr address.Address
}

// NewProtocol instantiates the protocol of vote
func NewProtocol(cm protocol.ChainManager) *Protocol { return &Protocol{cm: cm} }
func NewProtocol(cm protocol.ChainManager) *Protocol {
h := hash.Hash160b([]byte(ProtocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
}
return &Protocol{cm: cm, addr: addr}
}

// Initialize initializes the rewarding protocol by setting the original admin, block and epoch reward
func (p *Protocol) Initialize(ctx context.Context, sm protocol.StateManager, addrs []address.Address) error {
Expand Down Expand Up @@ -161,10 +172,11 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
}

return &action.Receipt{
Status: action.SuccessReceiptStatus,
BlockHeight: raCtx.BlockHeight,
ActionHash: raCtx.ActionHash,
GasConsumed: raCtx.IntrinsicGas,
Status: action.SuccessReceiptStatus,
BlockHeight: raCtx.BlockHeight,
ActionHash: raCtx.ActionHash,
GasConsumed: raCtx.IntrinsicGas,
ContractAddress: p.addr.String(),
}, nil
}

Expand Down

0 comments on commit ed955b7

Please sign in to comment.