Skip to content

Commit ed955b7

Browse files
lizhefengzjshen14
authored andcommitted
IOTEX-381 Add receipt for putpollresult (#927)
1 parent 04d8bf0 commit ed955b7

File tree

7 files changed

+80
-19
lines changed

7 files changed

+80
-19
lines changed

action/protocol/account/protocol.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,32 @@ import (
1111
"math/big"
1212

1313
"github.com/pkg/errors"
14+
"go.uber.org/zap"
1415

1516
"github.com/iotexproject/iotex-core/action"
1617
"github.com/iotexproject/iotex-core/action/protocol"
1718
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
1819
"github.com/iotexproject/iotex-core/address"
20+
"github.com/iotexproject/iotex-core/pkg/hash"
21+
"github.com/iotexproject/iotex-core/pkg/log"
1922
)
2023

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

2528
// Protocol defines the protocol of handling account
26-
type Protocol struct{}
29+
type Protocol struct{ addr address.Address }
2730

2831
// NewProtocol instantiates the protocol of account
29-
func NewProtocol() *Protocol { return &Protocol{} }
32+
func NewProtocol() *Protocol {
33+
h := hash.Hash160b([]byte(ProtocolID))
34+
addr, err := address.FromBytes(h[:])
35+
if err != nil {
36+
log.L().Panic("Error when constructing the address of account protocol", zap.Error(err))
37+
}
38+
return &Protocol{addr: addr}
39+
}
3040

3141
// Handle handles an account
3242
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {

action/protocol/account/transfer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
133133
}
134134
}
135135
return &action.Receipt{
136-
Status: action.SuccessReceiptStatus,
137-
BlockHeight: raCtx.BlockHeight,
138-
ActionHash: raCtx.ActionHash,
139-
GasConsumed: raCtx.IntrinsicGas,
136+
Status: action.SuccessReceiptStatus,
137+
BlockHeight: raCtx.BlockHeight,
138+
ActionHash: raCtx.ActionHash,
139+
GasConsumed: raCtx.IntrinsicGas,
140+
ContractAddress: p.addr.String(),
140141
}, nil
141142
}
142143

action/protocol/execution/protocol.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import (
1010
"context"
1111

1212
"github.com/pkg/errors"
13+
"go.uber.org/zap"
1314

1415
"github.com/iotexproject/iotex-core/action"
1516
"github.com/iotexproject/iotex-core/action/protocol"
1617
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
1718
"github.com/iotexproject/iotex-core/address"
19+
"github.com/iotexproject/iotex-core/pkg/hash"
20+
"github.com/iotexproject/iotex-core/pkg/log"
1821
)
1922

2023
const (
@@ -27,11 +30,19 @@ const (
2730

2831
// Protocol defines the protocol of handling executions
2932
type Protocol struct {
30-
cm protocol.ChainManager
33+
cm protocol.ChainManager
34+
addr address.Address
3135
}
3236

3337
// NewProtocol instantiates the protocol of exeuction
34-
func NewProtocol(cm protocol.ChainManager) *Protocol { return &Protocol{cm: cm} }
38+
func NewProtocol(cm protocol.ChainManager) *Protocol {
39+
h := hash.Hash160b([]byte(ProtocolID))
40+
addr, err := address.FromBytes(h[:])
41+
if err != nil {
42+
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
43+
}
44+
return &Protocol{cm: cm, addr: addr}
45+
}
3546

3647
// Handle handles an execution
3748
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {

action/protocol/multichain/mainchain/protocol.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
7171
return nil, errors.Wrapf(err, "error when handling stop sub-chain action")
7272
}
7373
}
74+
// TODO: consider add receipt later
7475
// The action is not handled by this handler or no error
7576
return nil, nil
7677
}

action/protocol/multichain/subchain/protocol.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
5454
return nil, errors.Wrapf(err, "error when handling deposit settlement action")
5555
}
5656
}
57+
// TODO: consider add receipt later
5758
return nil, nil
5859
}
5960

action/protocol/poll/protocol.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/iotexproject/iotex-core/address"
2323
"github.com/iotexproject/iotex-core/blockchain/genesis"
2424
"github.com/iotexproject/iotex-core/crypto"
25+
"github.com/iotexproject/iotex-core/pkg/hash"
2526
"github.com/iotexproject/iotex-core/pkg/log"
2627
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
2728
"github.com/iotexproject/iotex-core/state"
@@ -60,6 +61,7 @@ type Protocol interface {
6061

6162
type lifeLongDelegatesProtocol struct {
6263
delegates state.CandidateList
64+
addr address.Address
6365
}
6466

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

8390
func (p *lifeLongDelegatesProtocol) Initialize(
@@ -89,7 +96,7 @@ func (p *lifeLongDelegatesProtocol) Initialize(
8996
}
9097

9198
func (p *lifeLongDelegatesProtocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
92-
return handle(ctx, act, sm)
99+
return handle(ctx, act, sm, p.addr.String())
93100
}
94101

95102
func (p *lifeLongDelegatesProtocol) Validate(ctx context.Context, act action.Action) error {
@@ -134,6 +141,7 @@ type governanceChainCommitteeProtocol struct {
134141
initGravityChainHeight uint64
135142
numCandidateDelegates uint64
136143
numDelegates uint64
144+
addr address.Address
137145
}
138146

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

172+
h := hash.Hash160b([]byte(ProtocolID))
173+
addr, err := address.FromBytes(h[:])
174+
if err != nil {
175+
log.L().Panic("Error when constructing the address of poll protocol", zap.Error(err))
176+
}
177+
164178
return &governanceChainCommitteeProtocol{
165179
cm: cm,
166180
electionCommittee: electionCommittee,
@@ -170,6 +184,7 @@ func NewGovernanceChainCommitteeProtocol(
170184
getEpochNum: getEpochNum,
171185
numCandidateDelegates: numCandidateDelegates,
172186
numDelegates: numDelegates,
187+
addr: addr,
173188
}, nil
174189
}
175190

@@ -191,7 +206,7 @@ func (p *governanceChainCommitteeProtocol) Initialize(
191206
}
192207

193208
func (p *governanceChainCommitteeProtocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
194-
return handle(ctx, act, sm)
209+
return handle(ctx, act, sm, p.addr.String())
195210
}
196211

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

366-
func handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
381+
func handle(ctx context.Context, act action.Action, sm protocol.StateManager, protocolAddr string) (*action.Receipt, error) {
382+
raCtx := protocol.MustGetRunActionsCtx(ctx)
367383
r, ok := act.(*action.PutPollResult)
368384
if !ok {
369385
return nil, nil
370386
}
371387
zap.L().Debug("Handle PutPollResult Action", zap.Uint64("height", r.Height()))
372388

373-
return nil, setCandidates(sm, r.Candidates(), r.Height())
389+
if err := setCandidates(sm, r.Candidates(), r.Height()); err != nil {
390+
return nil, errors.Wrap(err, "failed to set candidates")
391+
}
392+
return &action.Receipt{
393+
Status: action.SuccessReceiptStatus,
394+
ActionHash: raCtx.ActionHash,
395+
BlockHeight: raCtx.BlockHeight,
396+
GasConsumed: raCtx.IntrinsicGas,
397+
ContractAddress: protocolAddr,
398+
}, nil
374399
}
375400

376401
func validate(ctx context.Context, p Protocol, act action.Action) error {

action/protocol/vote/protocol.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import (
1111
"math/big"
1212

1313
"github.com/pkg/errors"
14+
"go.uber.org/zap"
1415

1516
"github.com/iotexproject/iotex-core/action"
1617
"github.com/iotexproject/iotex-core/action/protocol"
1718
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
1819
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
1920
"github.com/iotexproject/iotex-core/action/protocol/vote/candidatesutil"
2021
"github.com/iotexproject/iotex-core/address"
22+
"github.com/iotexproject/iotex-core/pkg/hash"
23+
"github.com/iotexproject/iotex-core/pkg/log"
2124
"github.com/iotexproject/iotex-core/state"
2225
)
2326

@@ -32,11 +35,19 @@ const (
3235

3336
// Protocol defines the protocol of handling votes
3437
type Protocol struct {
35-
cm protocol.ChainManager
38+
cm protocol.ChainManager
39+
addr address.Address
3640
}
3741

3842
// NewProtocol instantiates the protocol of vote
39-
func NewProtocol(cm protocol.ChainManager) *Protocol { return &Protocol{cm: cm} }
43+
func NewProtocol(cm protocol.ChainManager) *Protocol {
44+
h := hash.Hash160b([]byte(ProtocolID))
45+
addr, err := address.FromBytes(h[:])
46+
if err != nil {
47+
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
48+
}
49+
return &Protocol{cm: cm, addr: addr}
50+
}
4051

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

163174
return &action.Receipt{
164-
Status: action.SuccessReceiptStatus,
165-
BlockHeight: raCtx.BlockHeight,
166-
ActionHash: raCtx.ActionHash,
167-
GasConsumed: raCtx.IntrinsicGas,
175+
Status: action.SuccessReceiptStatus,
176+
BlockHeight: raCtx.BlockHeight,
177+
ActionHash: raCtx.ActionHash,
178+
GasConsumed: raCtx.IntrinsicGas,
179+
ContractAddress: p.addr.String(),
168180
}, nil
169181
}
170182

0 commit comments

Comments
 (0)