Skip to content

Commit

Permalink
IOTEX-248 Define the producer rewarding protocol APIs (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjshen14 authored Feb 6, 2019
1 parent 731bc58 commit 13a7ae0
Show file tree
Hide file tree
Showing 41 changed files with 2,727 additions and 332 deletions.
2 changes: 1 addition & 1 deletion action/protocol/account/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestLoadOrCreateAccountState(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx := protocol.WithRunActionsCtx(context.Background(),
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down
6 changes: 3 additions & 3 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func (p *Protocol) handleTransfer(act action.Action, raCtx protocol.RunActionsCt

if raCtx.EnableGasCharge {
// Load or create account for producer
producer, err := LoadOrCreateAccount(sm, raCtx.ProducerAddr, big.NewInt(0))
producer, err := LoadOrCreateAccount(sm, raCtx.Producer.Bech32(), big.NewInt(0))
if err != nil {
return errors.Wrapf(err, "failed to load or create the account of block producer %s", raCtx.ProducerAddr)
return errors.Wrapf(err, "failed to load or create the account of block producer %s", raCtx.Producer.Bech32())
}
gas, err := tsf.IntrinsicGas()
if err != nil {
Expand All @@ -66,7 +66,7 @@ func (p *Protocol) handleTransfer(act action.Action, raCtx protocol.RunActionsCt
return errors.Wrapf(err, "failed to compensate gas to producer")
}
// Put updated producer's state to trie
if err := StoreAccount(sm, raCtx.ProducerAddr, producer); err != nil {
if err := StoreAccount(sm, raCtx.Producer.Bech32(), producer); err != nil {
return errors.Wrap(err, "failed to update pending account changes to trie")
}
*raCtx.GasLimit -= gas
Expand Down
25 changes: 17 additions & 8 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ package protocol
import (
"context"

"github.com/iotexproject/iotex-core/address"

"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/keypair"
)

type runActionsCtxKey struct{}
Expand All @@ -19,20 +20,26 @@ type validateActionsCtxKey struct{}

// RunActionsCtx provides the runactions with auxiliary information.
type RunActionsCtx struct {
// EpochNumber is the epoch number
EpochNumber uint64
// height of block containing those actions
BlockHeight uint64
// hash of block containing those actions
BlockHash hash.Hash32B
// public key of producer who compose those actions
ProducerPubKey keypair.PublicKey
// timestamp of block containing those actions
BlockTimeStamp int64
// producer who compose those actions
ProducerAddr string
// gas Limit for perform those actions
GasLimit *uint64
// whether disable gas charge
EnableGasCharge bool
// Producer is the address of whom composes the block containing this action
Producer address.Address
// Caller is the address of whom issues this action
Caller address.Address
// ActionHash is the hash of the action with the sealed envelope
ActionHash hash.Hash32B
// Nonce is the nonce of the action
Nonce uint64
}

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

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

// WithValidateActionsCtx add ValidateActionsCtx into context.
func WithValidateActionsCtx(ctx context.Context, va *ValidateActionsCtx) context.Context {
func WithValidateActionsCtx(ctx context.Context, va ValidateActionsCtx) context.Context {
return context.WithValue(ctx, validateActionsCtxKey{}, va)
}

// GetValidateActionsCtx gets validateActions context
func GetValidateActionsCtx(ctx context.Context) (*ValidateActionsCtx, bool) {
va, ok := ctx.Value(validateActionsCtxKey{}).(*ValidateActionsCtx)
func GetValidateActionsCtx(ctx context.Context) (ValidateActionsCtx, bool) {
va, ok := ctx.Value(validateActionsCtxKey{}).(ValidateActionsCtx)
return va, ok
}
4 changes: 2 additions & 2 deletions action/protocol/execution/evm/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestCreateContract(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx := protocol.WithRunActionsCtx(context.Background(),
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestLoadStoreContract(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx := protocol.WithRunActionsCtx(context.Background(),
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down
10 changes: 4 additions & 6 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/keypair"
"github.com/iotexproject/iotex-core/pkg/log"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ type Params struct {
}

// NewParams creates a new context for use in the EVM.
func NewParams(blkHeight uint64, producerPubKey keypair.PublicKey, blkTimeStamp int64, execution *action.Execution, stateDB *StateDBAdapter) (*Params, error) {
func NewParams(blkHeight uint64, producerAddr address.Address, blkTimeStamp int64, execution *action.Execution, stateDB *StateDBAdapter) (*Params, error) {
// If we don't have an explicit author (i.e. not mining), extract from the header
/*
var beneficiary common.Address
Expand All @@ -81,8 +80,7 @@ func NewParams(blkHeight uint64, producerPubKey keypair.PublicKey, blkTimeStamp
contractAddr := common.BytesToAddress(contract.Payload())
contractAddrPointer = &contractAddr
}
producerHash := keypair.HashPubKey(producerPubKey)
producer := common.BytesToAddress(producerHash[:])
producer := common.BytesToAddress(producerAddr.Payload())
context := vm.Context{
CanTransfer: CanTransfer,
Transfer: MakeTransfer,
Expand Down Expand Up @@ -142,7 +140,7 @@ func securityDeposit(ps *Params, stateDB vm.StateDB, gasLimit *uint64) error {
func ExecuteContract(
blkHeight uint64,
blkHash hash.Hash32B,
producerPubKey keypair.PublicKey,
producer address.Address,
blkTimeStamp int64,
sm protocol.StateManager,
execution *action.Execution,
Expand All @@ -151,7 +149,7 @@ func ExecuteContract(
enableGasCharge bool,
) (*action.Receipt, error) {
stateDB := NewStateDBAdapter(cm, sm, blkHeight, blkHash, execution.Hash())
ps, err := NewParams(blkHeight, producerPubKey, blkTimeStamp, execution, stateDB)
ps, err := NewParams(blkHeight, producer, blkTimeStamp, execution, stateDB)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/execution/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
if !ok {
return nil, errors.New("failed to get RunActionsCtx")
}
receipt, err := evm.ExecuteContract(raCtx.BlockHeight, raCtx.BlockHash, raCtx.ProducerPubKey, raCtx.BlockTimeStamp,
receipt, err := evm.ExecuteContract(raCtx.BlockHeight, raCtx.BlockHash, raCtx.Producer, raCtx.BlockTimeStamp,
sm, exec, p.cm, raCtx.GasLimit, raCtx.EnableGasCharge)

if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (sct *smartContractTest) prepareBlockchain(
gasLimit := uint64(10000000)
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestProtocol_Handle(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down Expand Up @@ -445,7 +445,7 @@ func TestProtocol_Handle(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down Expand Up @@ -622,7 +622,7 @@ func TestProtocol_Handle(t *testing.T) {
gasLimit := uint64(10000000)
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/multichain/mainchain/createdeposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestValidateDeposit(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/multichain/mainchain/putblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestHandlePutBlock(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down
3 changes: 2 additions & 1 deletion action/protocol/multichain/mainchain/startsubchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"

"fmt"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/account"
Expand Down Expand Up @@ -269,7 +270,7 @@ func TestHandleStartSubChain(t *testing.T) {
gasLimit := testutil.TestGasLimit
ctx = protocol.WithRunActionsCtx(ctx,
protocol.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].Bech32(),
Producer: testaddress.Addrinfo["producer"],
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
Expand Down
Loading

0 comments on commit 13a7ae0

Please sign in to comment.