Skip to content

Commit 13a7ae0

Browse files
authored
IOTEX-248 Define the producer rewarding protocol APIs (#493)
1 parent 731bc58 commit 13a7ae0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2727
-332
lines changed

action/protocol/account/protocol_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestLoadOrCreateAccountState(t *testing.T) {
4545
gasLimit := testutil.TestGasLimit
4646
ctx := protocol.WithRunActionsCtx(context.Background(),
4747
protocol.RunActionsCtx{
48-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
48+
Producer: testaddress.Addrinfo["producer"],
4949
GasLimit: &gasLimit,
5050
EnableGasCharge: testutil.EnableGasCharge,
5151
})

action/protocol/account/transfer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func (p *Protocol) handleTransfer(act action.Action, raCtx protocol.RunActionsCt
4040

4141
if raCtx.EnableGasCharge {
4242
// Load or create account for producer
43-
producer, err := LoadOrCreateAccount(sm, raCtx.ProducerAddr, big.NewInt(0))
43+
producer, err := LoadOrCreateAccount(sm, raCtx.Producer.Bech32(), big.NewInt(0))
4444
if err != nil {
45-
return errors.Wrapf(err, "failed to load or create the account of block producer %s", raCtx.ProducerAddr)
45+
return errors.Wrapf(err, "failed to load or create the account of block producer %s", raCtx.Producer.Bech32())
4646
}
4747
gas, err := tsf.IntrinsicGas()
4848
if err != nil {
@@ -66,7 +66,7 @@ func (p *Protocol) handleTransfer(act action.Action, raCtx protocol.RunActionsCt
6666
return errors.Wrapf(err, "failed to compensate gas to producer")
6767
}
6868
// Put updated producer's state to trie
69-
if err := StoreAccount(sm, raCtx.ProducerAddr, producer); err != nil {
69+
if err := StoreAccount(sm, raCtx.Producer.Bech32(), producer); err != nil {
7070
return errors.Wrap(err, "failed to update pending account changes to trie")
7171
}
7272
*raCtx.GasLimit -= gas

action/protocol/context.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ package protocol
99
import (
1010
"context"
1111

12+
"github.com/iotexproject/iotex-core/address"
13+
1214
"github.com/iotexproject/iotex-core/pkg/hash"
13-
"github.com/iotexproject/iotex-core/pkg/keypair"
1415
)
1516

1617
type runActionsCtxKey struct{}
@@ -19,20 +20,26 @@ type validateActionsCtxKey struct{}
1920

2021
// RunActionsCtx provides the runactions with auxiliary information.
2122
type RunActionsCtx struct {
23+
// EpochNumber is the epoch number
24+
EpochNumber uint64
2225
// height of block containing those actions
2326
BlockHeight uint64
2427
// hash of block containing those actions
2528
BlockHash hash.Hash32B
26-
// public key of producer who compose those actions
27-
ProducerPubKey keypair.PublicKey
2829
// timestamp of block containing those actions
2930
BlockTimeStamp int64
30-
// producer who compose those actions
31-
ProducerAddr string
3231
// gas Limit for perform those actions
3332
GasLimit *uint64
3433
// whether disable gas charge
3534
EnableGasCharge bool
35+
// Producer is the address of whom composes the block containing this action
36+
Producer address.Address
37+
// Caller is the address of whom issues this action
38+
Caller address.Address
39+
// ActionHash is the hash of the action with the sealed envelope
40+
ActionHash hash.Hash32B
41+
// Nonce is the nonce of the action
42+
Nonce uint64
3643
}
3744

3845
// ValidateActionsCtx provides action validators with auxiliary information.
@@ -41,6 +48,8 @@ type ValidateActionsCtx struct {
4148
BlockHeight uint64
4249
// public key of producer who compose those actions
4350
ProducerAddr string
51+
// Caller is the address of whom issues the action
52+
Caller address.Address
4453
}
4554

4655
// WithRunActionsCtx add RunActionsCtx into context.
@@ -55,12 +64,12 @@ func GetRunActionsCtx(ctx context.Context) (RunActionsCtx, bool) {
5564
}
5665

5766
// WithValidateActionsCtx add ValidateActionsCtx into context.
58-
func WithValidateActionsCtx(ctx context.Context, va *ValidateActionsCtx) context.Context {
67+
func WithValidateActionsCtx(ctx context.Context, va ValidateActionsCtx) context.Context {
5968
return context.WithValue(ctx, validateActionsCtxKey{}, va)
6069
}
6170

6271
// GetValidateActionsCtx gets validateActions context
63-
func GetValidateActionsCtx(ctx context.Context) (*ValidateActionsCtx, bool) {
64-
va, ok := ctx.Value(validateActionsCtxKey{}).(*ValidateActionsCtx)
72+
func GetValidateActionsCtx(ctx context.Context) (ValidateActionsCtx, bool) {
73+
va, ok := ctx.Value(validateActionsCtxKey{}).(ValidateActionsCtx)
6574
return va, ok
6675
}

action/protocol/execution/evm/contract_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestCreateContract(t *testing.T) {
7777
gasLimit := testutil.TestGasLimit
7878
ctx := protocol.WithRunActionsCtx(context.Background(),
7979
protocol.RunActionsCtx{
80-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
80+
Producer: testaddress.Addrinfo["producer"],
8181
GasLimit: &gasLimit,
8282
EnableGasCharge: testutil.EnableGasCharge,
8383
})
@@ -181,7 +181,7 @@ func TestLoadStoreContract(t *testing.T) {
181181
gasLimit := testutil.TestGasLimit
182182
ctx := protocol.WithRunActionsCtx(context.Background(),
183183
protocol.RunActionsCtx{
184-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
184+
Producer: testaddress.Addrinfo["producer"],
185185
GasLimit: &gasLimit,
186186
EnableGasCharge: testutil.EnableGasCharge,
187187
})

action/protocol/execution/evm/evm.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/iotexproject/iotex-core/address"
2121
"github.com/iotexproject/iotex-core/blockchain/genesis"
2222
"github.com/iotexproject/iotex-core/pkg/hash"
23-
"github.com/iotexproject/iotex-core/pkg/keypair"
2423
"github.com/iotexproject/iotex-core/pkg/log"
2524
)
2625

@@ -57,7 +56,7 @@ type Params struct {
5756
}
5857

5958
// NewParams creates a new context for use in the EVM.
60-
func NewParams(blkHeight uint64, producerPubKey keypair.PublicKey, blkTimeStamp int64, execution *action.Execution, stateDB *StateDBAdapter) (*Params, error) {
59+
func NewParams(blkHeight uint64, producerAddr address.Address, blkTimeStamp int64, execution *action.Execution, stateDB *StateDBAdapter) (*Params, error) {
6160
// If we don't have an explicit author (i.e. not mining), extract from the header
6261
/*
6362
var beneficiary common.Address
@@ -81,8 +80,7 @@ func NewParams(blkHeight uint64, producerPubKey keypair.PublicKey, blkTimeStamp
8180
contractAddr := common.BytesToAddress(contract.Payload())
8281
contractAddrPointer = &contractAddr
8382
}
84-
producerHash := keypair.HashPubKey(producerPubKey)
85-
producer := common.BytesToAddress(producerHash[:])
83+
producer := common.BytesToAddress(producerAddr.Payload())
8684
context := vm.Context{
8785
CanTransfer: CanTransfer,
8886
Transfer: MakeTransfer,
@@ -142,7 +140,7 @@ func securityDeposit(ps *Params, stateDB vm.StateDB, gasLimit *uint64) error {
142140
func ExecuteContract(
143141
blkHeight uint64,
144142
blkHash hash.Hash32B,
145-
producerPubKey keypair.PublicKey,
143+
producer address.Address,
146144
blkTimeStamp int64,
147145
sm protocol.StateManager,
148146
execution *action.Execution,
@@ -151,7 +149,7 @@ func ExecuteContract(
151149
enableGasCharge bool,
152150
) (*action.Receipt, error) {
153151
stateDB := NewStateDBAdapter(cm, sm, blkHeight, blkHash, execution.Hash())
154-
ps, err := NewParams(blkHeight, producerPubKey, blkTimeStamp, execution, stateDB)
152+
ps, err := NewParams(blkHeight, producer, blkTimeStamp, execution, stateDB)
155153
if err != nil {
156154
return nil, err
157155
}

action/protocol/execution/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
3939
if !ok {
4040
return nil, errors.New("failed to get RunActionsCtx")
4141
}
42-
receipt, err := evm.ExecuteContract(raCtx.BlockHeight, raCtx.BlockHash, raCtx.ProducerPubKey, raCtx.BlockTimeStamp,
42+
receipt, err := evm.ExecuteContract(raCtx.BlockHeight, raCtx.BlockHash, raCtx.Producer, raCtx.BlockTimeStamp,
4343
sm, exec, p.cm, raCtx.GasLimit, raCtx.EnableGasCharge)
4444

4545
if err != nil {

action/protocol/execution/protocol_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (sct *smartContractTest) prepareBlockchain(
145145
gasLimit := uint64(10000000)
146146
ctx = protocol.WithRunActionsCtx(ctx,
147147
protocol.RunActionsCtx{
148-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
148+
Producer: testaddress.Addrinfo["producer"],
149149
GasLimit: &gasLimit,
150150
EnableGasCharge: testutil.EnableGasCharge,
151151
})
@@ -238,7 +238,7 @@ func TestProtocol_Handle(t *testing.T) {
238238
gasLimit := testutil.TestGasLimit
239239
ctx = protocol.WithRunActionsCtx(ctx,
240240
protocol.RunActionsCtx{
241-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
241+
Producer: testaddress.Addrinfo["producer"],
242242
GasLimit: &gasLimit,
243243
EnableGasCharge: testutil.EnableGasCharge,
244244
})
@@ -445,7 +445,7 @@ func TestProtocol_Handle(t *testing.T) {
445445
gasLimit := testutil.TestGasLimit
446446
ctx = protocol.WithRunActionsCtx(ctx,
447447
protocol.RunActionsCtx{
448-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
448+
Producer: testaddress.Addrinfo["producer"],
449449
GasLimit: &gasLimit,
450450
EnableGasCharge: testutil.EnableGasCharge,
451451
})
@@ -622,7 +622,7 @@ func TestProtocol_Handle(t *testing.T) {
622622
gasLimit := uint64(10000000)
623623
ctx = protocol.WithRunActionsCtx(ctx,
624624
protocol.RunActionsCtx{
625-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
625+
Producer: testaddress.Addrinfo["producer"],
626626
GasLimit: &gasLimit,
627627
EnableGasCharge: testutil.EnableGasCharge,
628628
})

action/protocol/multichain/mainchain/createdeposit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestValidateDeposit(t *testing.T) {
6969
gasLimit := testutil.TestGasLimit
7070
ctx = protocol.WithRunActionsCtx(ctx,
7171
protocol.RunActionsCtx{
72-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
72+
Producer: testaddress.Addrinfo["producer"],
7373
GasLimit: &gasLimit,
7474
EnableGasCharge: testutil.EnableGasCharge,
7575
})

action/protocol/multichain/mainchain/putblock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestHandlePutBlock(t *testing.T) {
5555
gasLimit := testutil.TestGasLimit
5656
ctx = protocol.WithRunActionsCtx(ctx,
5757
protocol.RunActionsCtx{
58-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
58+
Producer: testaddress.Addrinfo["producer"],
5959
GasLimit: &gasLimit,
6060
EnableGasCharge: testutil.EnableGasCharge,
6161
})

action/protocol/multichain/mainchain/startsubchain_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/stretchr/testify/require"
1818

1919
"fmt"
20+
2021
"github.com/iotexproject/iotex-core/action"
2122
"github.com/iotexproject/iotex-core/action/protocol"
2223
"github.com/iotexproject/iotex-core/action/protocol/account"
@@ -269,7 +270,7 @@ func TestHandleStartSubChain(t *testing.T) {
269270
gasLimit := testutil.TestGasLimit
270271
ctx = protocol.WithRunActionsCtx(ctx,
271272
protocol.RunActionsCtx{
272-
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
273+
Producer: testaddress.Addrinfo["producer"],
273274
GasLimit: &gasLimit,
274275
EnableGasCharge: testutil.EnableGasCharge,
275276
})

0 commit comments

Comments
 (0)