-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_release_fund.go
118 lines (96 loc) · 3.81 KB
/
tx_release_fund.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package execution
import (
"fmt"
"math/big"
"github.com/dnerochain/dnero/common"
"github.com/dnerochain/dnero/common/result"
"github.com/dnerochain/dnero/core"
st "github.com/dnerochain/dnero/ledger/state"
"github.com/dnerochain/dnero/ledger/types"
)
var _ TxExecutor = (*ReleaseFundTxExecutor)(nil)
// ------------------------------- ReleaseFundTx Transaction -----------------------------------
// ReleaseFundTxExecutor implements the TxExecutor interface
type ReleaseFundTxExecutor struct {
state *st.LedgerState
}
// NewReleaseFundTxExecutor creates a new instance of ReleaseFundTxExecutor
func NewReleaseFundTxExecutor(state *st.LedgerState) *ReleaseFundTxExecutor {
return &ReleaseFundTxExecutor{
state: state,
}
}
func (exec *ReleaseFundTxExecutor) sanityCheck(chainID string, view *st.StoreView, transaction types.Tx) result.Result {
tx := transaction.(*types.ReleaseFundTx)
// Validate source, basic
res := tx.Source.ValidateBasic()
if res.IsError() {
return res
}
// Get input account
sourceAccount, success := getInput(view, tx.Source)
if success.IsError() {
return result.Error("Unknown address: %v", tx.Source.Address)
}
// Validate input, advanced
signBytes := tx.SignBytes(chainID)
res = validateInputAdvanced(sourceAccount, signBytes, tx.Source)
if res.IsError() {
logger.Debugf(fmt.Sprintf("validateSourceAdvanced failed on %v: %v", tx.Source.Address.Hex(), res))
return res
}
blockHeight := view.Height() + 1 // the view points to the parent of the current block
if minTxFee, success := sanityCheckForFee(tx.Fee, blockHeight); !success {
return result.Error("Insufficient fee. Transaction fee needs to be at least %v DTokenWei",
minTxFee).WithErrorCode(result.CodeInvalidFee)
}
minimalBalance := tx.Fee
if !sourceAccount.Balance.IsGTE(minimalBalance) {
logger.Infof(fmt.Sprintf("Source did not have enough balance %v", tx.Source.Address.Hex()))
return result.Error("Source balance is %v, but required minimal balance is %v",
sourceAccount.Balance, minimalBalance).WithErrorCode(result.CodeInsufficientFund)
}
currentBlockHeight := exec.state.Height()
reserveSequence := tx.ReserveSequence
err := sourceAccount.CheckReleaseFund(currentBlockHeight, reserveSequence)
if err != nil {
return result.Error(err.Error()).WithErrorCode(result.CodeReleaseFundCheckFailed)
}
return result.OK
}
func (exec *ReleaseFundTxExecutor) process(chainID string, view *st.StoreView, transaction types.Tx) (common.Hash, result.Result) {
tx := transaction.(*types.ReleaseFundTx)
sourceInputs := []types.TxInput{tx.Source}
accounts, success := getInputs(view, sourceInputs)
if success.IsError() {
// TODO: revisit whether we should panic or just log the error.
return common.Hash{}, result.Error("Failed to get the source account")
}
sourceAddress := tx.Source.Address
sourceAccount := accounts[string(sourceAddress[:])]
reserveSequence := tx.ReserveSequence
currentBlockHeight := exec.state.Height()
sourceAccount.ReleaseFund(currentBlockHeight, reserveSequence)
if !chargeFee(sourceAccount, tx.Fee) {
return common.Hash{}, result.Error("failed to charge transaction fee")
}
sourceAccount.Sequence++
view.SetAccount(sourceAddress, sourceAccount)
txHash := types.TxID(chainID, tx)
return txHash, result.OK
}
func (exec *ReleaseFundTxExecutor) getTxInfo(transaction types.Tx) *core.TxInfo {
tx := transaction.(*types.ReleaseFundTx)
return &core.TxInfo{
Address: tx.Source.Address,
Sequence: tx.Source.Sequence,
EffectiveGasPrice: exec.calculateEffectiveGasPrice(transaction),
}
}
func (exec *ReleaseFundTxExecutor) calculateEffectiveGasPrice(transaction types.Tx) *big.Int {
tx := transaction.(*types.ReleaseFundTx)
fee := tx.Fee
gas := new(big.Int).SetUint64(getRegularTxGas(exec.state))
effectiveGasPrice := new(big.Int).Div(fee.DTokenWei, gas)
return effectiveGasPrice
}