Skip to content

Commit

Permalink
feat: add events to abi and pack events from it
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed Jan 26, 2023
1 parent 271cb9b commit 0d93aad
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 127 deletions.
28 changes: 28 additions & 0 deletions accounts/abi/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package abi

import (
"fmt"
"reflect"
"strings"

"github.com/Taraxa-project/taraxa-evm/common"
"github.com/Taraxa-project/taraxa-evm/core/vm"
"github.com/Taraxa-project/taraxa-evm/crypto"
)

Expand Down Expand Up @@ -55,3 +57,29 @@ func (e Event) Id() common.Hash {
}
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
}

func (e Event) MakeLog(args ...interface{}) (*vm.LogRecord, error) {
if len(e.Inputs) != len(args) {
return nil, fmt.Errorf("MakeLog: %v: expected %v arguments, but got %v", e.Name, len(e.Inputs), len(args))
}
log := new(vm.LogRecord)
log.Topics = append(log.Topics, e.Id())
data_set := false
for index, input := range e.Inputs {
bytes, err := input.Type.pack(reflect.ValueOf(args[index]))
if err != nil {
return nil, err
}
if input.Indexed {
log.Topics = append(log.Topics, common.BytesToHash(bytes))
} else {
if data_set {
return nil, fmt.Errorf("Only one not indexed param is supported right now")
}
data_set = true
log.Data = bytes
}

}
return log, nil
}
29 changes: 15 additions & 14 deletions taraxa/state/dpos/precompiled/dpos_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ type Contract struct {
// delayed storage for PBFT
delayedStorage Reader
// ABI of the contract
Abi abi.ABI

evm *vm.EVM
Abi abi.ABI
logs Logs
evm *vm.EVM

// Iterable storages
validators Validators
Expand Down Expand Up @@ -282,6 +282,7 @@ func (self *Contract) lazy_init() {
}

self.Abi, _ = abi.JSON(strings.NewReader(sol.TaraxaDposClientMetaData))
self.logs = *new(Logs).Init(self.Abi.Events)

self.validators.Init(&self.storage, field_validators)
self.delegations.Init(&self.storage, field_delegations)
Expand Down Expand Up @@ -715,7 +716,7 @@ func (self *Contract) delegate(ctx vm.CallFrame, block types.BlockNum, args sol.
state.Count++
self.state_put(&state_k, state)
self.validators.ModifyValidator(&args.Validator, validator)
self.evm.AddLog(MakeDelegatedLog(ctx.CallerAccount.Address(), &args.Validator, ctx.Value))
self.evm.AddLog(self.logs.MakeDelegatedLog(ctx.CallerAccount.Address(), &args.Validator, ctx.Value))

return nil
}
Expand Down Expand Up @@ -792,7 +793,7 @@ func (self *Contract) undelegate(ctx vm.CallFrame, block types.BlockNum, args so
self.state_put(&state_k, state)
self.validators.ModifyValidator(&args.Validator, validator)
}
self.evm.AddLog(MakeUndelegatedLog(ctx.CallerAccount.Address(), &args.Validator, args.Amount))
self.evm.AddLog(self.logs.MakeUndelegatedLog(ctx.CallerAccount.Address(), &args.Validator, args.Amount))

return nil
}
Expand All @@ -810,7 +811,7 @@ func (self *Contract) confirmUndelegate(ctx vm.CallFrame, block types.BlockNum,
self.undelegations.RemoveUndelegation(ctx.CallerAccount.Address(), &args.Validator)
// TODO slashing of balance
ctx.CallerAccount.AddBalance(undelegation.Amount)
self.evm.AddLog(MakeUndelegateConfirmedLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount))
self.evm.AddLog(self.logs.MakeUndelegateConfirmedLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount))

return nil
}
Expand Down Expand Up @@ -866,7 +867,7 @@ func (self *Contract) cancelUndelegate(ctx vm.CallFrame, block types.BlockNum, a
state.Count++
self.state_put(&state_k, state)
self.validators.ModifyValidator(&args.Validator, validator)
self.evm.AddLog(MakeUndelegateCanceledLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount))
self.evm.AddLog(self.logs.MakeUndelegateCanceledLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount))

return nil
}
Expand Down Expand Up @@ -983,7 +984,7 @@ func (self *Contract) redelegate(ctx vm.CallFrame, block types.BlockNum, args so
state.Count++
self.state_put(&state_k, state)
self.validators.ModifyValidator(&args.ValidatorTo, validator_to)
self.evm.AddLog(MakeRedelegatedLog(ctx.CallerAccount.Address(), &args.ValidatorFrom, &args.ValidatorTo, args.Amount))
self.evm.AddLog(self.logs.MakeRedelegatedLog(ctx.CallerAccount.Address(), &args.ValidatorFrom, &args.ValidatorTo, args.Amount))
return nil
}

Expand Down Expand Up @@ -1018,7 +1019,7 @@ func (self *Contract) claimRewards(ctx vm.CallFrame, block types.BlockNum, args

state.Count++
self.state_put(&state_k, state)
self.evm.AddLog(MakeRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator))
self.evm.AddLog(self.logs.MakeRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator))

return nil
}
Expand All @@ -1043,7 +1044,7 @@ func (self *Contract) claimCommissionRewards(ctx vm.CallFrame, block types.Block
} else {
self.validators.ModifyValidator(&args.Validator, validator)
}
self.evm.AddLog(MakeComissionRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator))
self.evm.AddLog(self.logs.MakeCommissionRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator))

return nil
}
Expand Down Expand Up @@ -1111,10 +1112,10 @@ func (self *Contract) registerValidatorWithoutChecks(ctx vm.CallFrame, block typ
// Creates validator related objects in storage
validator := self.validators.CreateValidator(owner_address, &args.Validator, args.VrfKey, block, args.Commission, args.Description, args.Endpoint)
state.Count++
self.evm.AddLog(MakeValidatorRegisteredLog(&args.Validator))
self.evm.AddLog(self.logs.MakeValidatorRegisteredLog(&args.Validator))

if ctx.Value.Cmp(big.NewInt(0)) == 1 {
self.evm.AddLog(MakeDelegatedLog(owner_address, &args.Validator, ctx.Value))
self.evm.AddLog(self.logs.MakeDelegatedLog(owner_address, &args.Validator, ctx.Value))
self.delegations.CreateDelegation(owner_address, &args.Validator, block, ctx.Value)
self.delegate_update_values(ctx, validator, 0)
self.validators.ModifyValidator(&args.Validator, validator)
Expand Down Expand Up @@ -1162,7 +1163,7 @@ func (self *Contract) setValidatorInfo(ctx vm.CallFrame, args sol.SetValidatorIn
validator_info.Endpoint = args.Endpoint

self.validators.ModifyValidatorInfo(&args.Validator, validator_info)
self.evm.AddLog(MakeValidatorInfoSetLog(&args.Validator))
self.evm.AddLog(self.logs.MakeValidatorInfoSetLog(&args.Validator))

return nil
}
Expand Down Expand Up @@ -1193,7 +1194,7 @@ func (self *Contract) setCommission(ctx vm.CallFrame, block types.BlockNum, args
validator.Commission = args.Commission
validator.LastCommissionChange = block
self.validators.ModifyValidator(&args.Validator, validator)
self.evm.AddLog(MakeCommissionSetLog(&args.Validator, args.Commission))
self.evm.AddLog(self.logs.MakeCommissionSetLog(&args.Validator, args.Commission))

return nil
}
Expand Down
134 changes: 51 additions & 83 deletions taraxa/state/dpos/precompiled/dpos_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,130 +3,98 @@ package dpos
import (
"math/big"

"github.com/Taraxa-project/taraxa-evm/accounts/abi"
"github.com/Taraxa-project/taraxa-evm/common"
"github.com/Taraxa-project/taraxa-evm/core/vm"
"github.com/Taraxa-project/taraxa-evm/taraxa/util/keccak256"
)

func getEventHash(str string) *common.Hash {
return keccak256.Hash([]byte(str))
func checkError(log *vm.LogRecord, err error) *vm.LogRecord {
if err != nil {
panic("Update logs methods to correspond ABI: " + err.Error())
}
return log
}

// event Delegated(address indexed delegator, address indexed validator, uint256 amount);
var DelegatedEventHash = getEventHash("Delegated(address,address,uint256)")
type Logs struct {
Events map[string]abi.Event
}

func MakeDelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
topics := make([]common.Hash, 3)
topics[0] = *DelegatedEventHash
topics[1] = delegator.Hash()
topics[2] = validator.Hash()
func (self *Logs) Init(events map[string]abi.Event) *Logs {
self.Events = events

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()}
return self
}

// event Undelegated(address indexed delegator, address indexed validator, uint256 amount);
var UndelegatedEventHash = getEventHash("Undelegated(address,address,uint256)")
// All Make functions below are making log records for events.
// All hashes and data types should be the same as we have in solidity interface in ../solidity/dpos_contract_interface.sol
// If some event will be added or changed TestMakeLogsCheckTopics test should be modified

func MakeUndelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
topics := make([]common.Hash, 3)
topics[0] = *UndelegatedEventHash
topics[1] = delegator.Hash()
topics[2] = validator.Hash()
// event Delegated(address indexed delegator, address indexed validator, uint256 amount);
func (self *Logs) MakeDelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
event := self.Events["Delegated"]

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()}
return *checkError(event.MakeLog(delegator, validator, amount))
}

// event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount);
var UndelegateConfirmedEventHash = getEventHash("UndelegateConfirmed(address,uint256)")
// event Undelegated(address indexed delegator, address indexed validator, uint256 amount);
func (self *Logs) MakeUndelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
event := self.Events["Undelegated"]

return *checkError(event.MakeLog(delegator, validator, amount))
}

func MakeUndelegateConfirmedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
topics := make([]common.Hash, 3)
topics[0] = *UndelegateConfirmedEventHash
topics[1] = delegator.Hash()
topics[2] = validator.Hash()
// event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount);
func (self *Logs) MakeUndelegateConfirmedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
event := self.Events["UndelegateConfirmed"]

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()}
return *checkError(event.MakeLog(delegator, validator, amount))
}

// event UndelegateCanceled(address indexed delegator, address indexed validator, uint256 amount);
var UndelegateCanceledEventHash = getEventHash("UndelegateCanceled(address,uint256)")

func MakeUndelegateCanceledLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
topics := make([]common.Hash, 3)
topics[0] = *UndelegateCanceledEventHash
topics[1] = delegator.Hash()
topics[2] = validator.Hash()
func (self *Logs) MakeUndelegateCanceledLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord {
event := self.Events["UndelegateCanceled"]

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()}
return *checkError(event.MakeLog(delegator, validator, amount))
}

// event Redelegated(address indexed delegator, address indexed from, address indexed to, uint256 amount);
var RedelegatedEventHash = getEventHash("UndelegateCanceled(address,address,address,uint256)")
func (self *Logs) MakeRedelegatedLog(delegator, from, to *common.Address, amount *big.Int) vm.LogRecord {
event := self.Events["Redelegated"]

func MakeRedelegatedLog(delegator, from, to *common.Address, amount *big.Int) vm.LogRecord {
topics := make([]common.Hash, 4)
topics[0] = *RedelegatedEventHash
topics[1] = delegator.Hash()
topics[2] = from.Hash()
topics[3] = to.Hash()

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()}
return *checkError(event.MakeLog(delegator, from, to, amount))
}

// event RewardsClaimed(address indexed account, address indexed validator);
var RewardsClaimedEventHash = getEventHash("RewardsClaimed(address,address)")

func MakeRewardsClaimedLog(account, validator *common.Address) vm.LogRecord {
topics := make([]common.Hash, 3)
topics[0] = *RewardsClaimedEventHash
topics[1] = account.Hash()
topics[2] = validator.Hash()
func (self *Logs) MakeRewardsClaimedLog(account, validator *common.Address) vm.LogRecord {
event := self.Events["RewardsClaimed"]

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}}
return *checkError(event.MakeLog(account, validator))
}

// event CommissionRewardsClaimed(address indexed account, address indexed validator);
var ComissionRewardsClaimedEventHash = getEventHash("CommissionRewardsClaimed(address,address)")
func (self *Logs) MakeCommissionRewardsClaimedLog(account, validator *common.Address) vm.LogRecord {
event := self.Events["CommissionRewardsClaimed"]

func MakeComissionRewardsClaimedLog(account, validator *common.Address) vm.LogRecord {
topics := make([]common.Hash, 3)
topics[0] = *ComissionRewardsClaimedEventHash
topics[1] = account.Hash()
topics[2] = validator.Hash()

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}}
return *checkError(event.MakeLog(account, validator))
}

// event CommissionSet(address indexed validator, uint16 comission);
var CommissionSetEventHash = getEventHash("CommissionSet(address,uint16)")

func MakeCommissionSetLog(account *common.Address, amount uint16) vm.LogRecord {
topics := make([]common.Hash, 2)
topics[0] = *CommissionSetEventHash
topics[1] = account.Hash()
// event CommissionSet(address indexed validator, uint16 commission);
func (self *Logs) MakeCommissionSetLog(account *common.Address, amount uint16) vm.LogRecord {
event := self.Events["CommissionSet"]

big_amount := big.NewInt(int64(amount))
return vm.LogRecord{Address: *contract_address, Topics: topics, Data: big_amount.Bytes()}
return *checkError(event.MakeLog(account, amount))
}

// event ValidatorRegistered(address indexed validator);
var ValidatorRegisteredEventHash = getEventHash("ValidatorRegistered(address)")
func (self *Logs) MakeValidatorRegisteredLog(account *common.Address) vm.LogRecord {
event := self.Events["ValidatorRegistered"]

func MakeValidatorRegisteredLog(account *common.Address) vm.LogRecord {
topics := make([]common.Hash, 2)
topics[0] = *ValidatorRegisteredEventHash
topics[1] = account.Hash()

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}}
return *checkError(event.MakeLog(account))
}

// event ValidatorInfoSet(address indexed validator);
var ValidatorInfoSetEventHash = getEventHash("ValidatorInfoSet(address)")

func MakeValidatorInfoSetLog(account *common.Address) vm.LogRecord {
topics := make([]common.Hash, 2)
topics[0] = *ValidatorInfoSetEventHash
topics[1] = account.Hash()
func (self *Logs) MakeValidatorInfoSetLog(account *common.Address) vm.LogRecord {
event := self.Events["ValidatorInfoSet"]

return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}}
return *checkError(event.MakeLog(account))
}
Loading

0 comments on commit 0d93aad

Please sign in to comment.