这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tendermint=${ROOTDIR}/tendermint/tendermint

pack=pack
packfile=lk-chain
tarfile=lk-chain-linux-x64.tar.gz
packdst=pack/$packfile

if [ ! -e $BIN ]; then
Expand Down Expand Up @@ -90,7 +91,7 @@ function do_pack()
cp tools/genesis.json $packdst/data
cp ethermint_init_satate/state.db_20190510.tar.gz $packdst/data/state.db.tar.gz

cd pack ; tar zcf $packfile.tar.gz $packfile ; echo "done $packdst.tar.gz";
cd pack ; tar zcf $tarfile $packfile ; echo "done $tarfile";
}

function main()
Expand Down
Binary file modified doc/json-rpc.md
Binary file not shown.
2 changes: 2 additions & 0 deletions ethereum/go-ethereum/accounts/abi/bind/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type ContractTransactor interface {
// transactions may be added or removed by miners, but it should provide a basis
// for setting a reasonable default.
EstimateGas(ctx context.Context, call ethereum.CallMsg) (usedGas *big.Int, err error)

EstimateSweepGas(ctx context.Context, account common.Address) (*types.SweepGas, err error)
// SendTransaction injects the transaction into the pending pool for execution.
SendTransaction(ctx context.Context, tx *types.Transaction) error
}
Expand Down
96 changes: 96 additions & 0 deletions ethereum/go-ethereum/common/lkfee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package common

import (
"errors"
"math/big"
)

/*
手续费为交易金额的千分之五,单笔最低为0.05链克,最高为500链克;在计算时,不足
1链克的小数部分按照1链克计算,例如一笔交易金额为1000.1链克的交易的手续费为
5.005链克,1001链克的交易手续费也为5.005链克

if (x % (1e+18) != 0) {
fee = (x/(1e+18)+1) * rate *(1e+13)
} else {
fee = (x/(1e+18)) * rate *(1e+13)
}
if fee < min {
fee = min
}
if max != 0 && fee > max {
fee = max
}
*/

const (
MaxGasLimit = 5e9 // max gas limit (500 lianke)
MinGasLimit = 5e5 // min gas limit (0.05 lianke)

everLiankeFee = 5e4 // ever poundage fee unit(gas)
gasToLiankeRate = 1e7 // lianke = 1e+7 gas
gasPrice = 1e11
)

func CalNewFee(value *big.Int) uint64 {
var liankeCount *big.Int
lianke := new(big.Int).Mul(big.NewInt(gasPrice), big.NewInt(gasToLiankeRate))
if new(big.Int).Mod(value, lianke).Uint64() != 0 {
liankeCount = new(big.Int).Div(value, lianke)
liankeCount.Add(liankeCount, big.NewInt(1))
} else {
liankeCount = new(big.Int).Div(value, lianke)
}
calFeeGas := new(big.Int).Mul(big.NewInt(everLiankeFee), liankeCount)

if calFeeGas.Cmp(big.NewInt(MinGasLimit)) < 0 {
calFeeGas.Set(big.NewInt(MinGasLimit))
}
if MaxGasLimit != 0 && calFeeGas.Cmp(big.NewInt(MaxGasLimit)) > 0 {
calFeeGas.Set(big.NewInt(MaxGasLimit))
}
return calFeeGas.Uint64()
}

func calAllFee(balance *big.Int, amount *big.Int, min *big.Int, max *big.Int) (am *big.Int, changeAm *big.Int, fee uint64) {
fee = CalNewFee(amount)

gasUsed := new(big.Int).Mul(big.NewInt(gasPrice), new(big.Int).SetUint64(fee))
newBalance := new(big.Int).Add(amount, gasUsed)
changeAmount := new(big.Int).Sub(balance, newBalance)
if max.Cmp(min) <= 0 || amount.Cmp(min) == 0 {
am = amount
changeAm = changeAmount
return
}

if changeAmount.Sign() == 0 {
am = amount
changeAm = changeAmount
return
} else if changeAmount.Sign() > 0 {
// amount is less
min = new(big.Int).Set(amount)
fix := new(big.Int).Add(amount, max)
amount = new(big.Int).Div(fix, big.NewInt(2))

return calAllFee(balance, amount, min, max)
} else {
// amount is more
max = new(big.Int).Set(amount)
fix := new(big.Int).Add(amount, min)
amount = new(big.Int).Div(fix, big.NewInt(2))

return calAllFee(balance, amount, min, max)
}
}

func CalSweepBalanceFee(balance *big.Int) (amount *big.Int, changeAmount *big.Int, fee uint64, err error) {
minGasUsed := new(big.Int).Mul(big.NewInt(gasPrice), big.NewInt(MinGasLimit))
if balance.Cmp(minGasUsed) <= 0 {
return big.NewInt(0), big.NewInt(0), 0, errors.New("balance too low")
}

amount, changeAmount, fee = calAllFee(balance, new(big.Int).Set(balance), big.NewInt(0), new(big.Int).Set(balance))
return
}
111 changes: 111 additions & 0 deletions ethereum/go-ethereum/common/lkfee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package common

import (
"fmt"
"math/big"
"math/rand"
"testing"
)

func TestCalNewFee(t *testing.T) {
type gasTest struct {
val *big.Int
gasFee uint64
}

lianke := new(big.Int).Mul(big.NewInt(gasPrice), big.NewInt(gasToLiankeRate))

// test msg
gasTestMsg := []gasTest{
{big.NewInt(0), MinGasLimit},
{big.NewInt(10), MinGasLimit},
{lianke, MinGasLimit},
{new(big.Int).Mul(big.NewInt(10), lianke), MinGasLimit},
{new(big.Int).Mul(big.NewInt(100), lianke), 5e6},
{new(big.Int).Mul(big.NewInt(1e10), lianke), MaxGasLimit},
{new(big.Int).Mul(big.NewInt(1e11), lianke), MaxGasLimit},
}

// check
for _, v := range gasTestMsg {
calFee := CalNewFee(v.val)
if v.gasFee != calFee {
t.Fatal("CalNewFee failed.", "val", v.val, "gasFee", v.gasFee, "calFee", calFee)
}
}
}

func newBig(v string) *big.Int {
b, _ := new(big.Int).SetString(v, 0)
return b
}

// /*
func TestCalSweepBalanceFee(t *testing.T) {
for index := 0; index < 100000; index++ {
a := rand.Int63n(100000)
b := rand.Int63()
min := int64(50000000000000000)
if b < min {
b = b + min
}
balance := new(big.Int).Add(new(big.Int).Mul(big.NewInt(a), big.NewInt(1e+18)), big.NewInt(b))
amount, fee, _ := CalSweepBalanceFee(balance)
calFee := CalNewFee(amount)
if fee != fee {
t.Fatal(fmt.Sprintf("CalNewFee != SweepBalanceFee,balance:%s, %d != %d ", balance.String(), calFee, fee))
}
b2 := new(big.Int).Add(amount, new(big.Int).Mul(new(big.Int).SetUint64(fee), big.NewInt(gasPrice)))
diff := new(big.Int).Sub(balance, b2)
fmt.Printf("%d, balance: %s ,amount: %s ,fee %d ,diff: %s\n", index, balance.String(), amount.String(), fee, diff.String())
if diff.Cmp(big.NewInt(3e+16)) > 0 {
t.Fatal("diff more then 0.05")
}
}
}

// */
/*
func TestCalSweepBalanceFee(t *testing.T) {
type TestBalanceFee struct {
Balance *big.Int
Amount *big.Int
Fee uint64
Err error
}

testFeeArr := []TestBalanceFee{
// 1000000000000000000 ,1 lk
{newBig("50000000000000000"), newBig("1"), uint64(500000), errors.New("balance too low")},
{newBig("50000000000000001"), newBig("1"), uint64(500000), nil},
{newBig("78997651234567899"), newBig("28997651234567899"), uint64(500000), nil},
{newBig("100012512345678995"), newBig("50012512345678995"), uint64(500000), nil},
{newBig("500000000000000000"), newBig("450000000000000000"), uint64(500000), nil},
{newBig("1345125123456789956"), newBig("1295125123456789956"), uint64(500000), nil},
{newBig("11345125123456789956"), newBig("11285125123456789956"), uint64(600000), nil},
{newBig("119000000000000000000"), newBig("118405000000000000000"), uint64(5950000), nil},
{newBig("1190000000000000000000"), newBig("1184075000000000000000"), uint64(59250000), nil},
{newBig("11900000000000123456789"), newBig("11840795000000123456789"), uint64(59250000), nil},
{newBig("119000000000000123456789"), newBig("118500000000000123456789"), uint64(5000000000), nil},
{newBig("1191234567899765123456789"), newBig("1190734567899765123456789"), uint64(5000000000), nil},
}

for _, v := range testFeeArr {
amount, fee, err := CalSweepBalanceFee(v.Balance)
if v.Err != nil {
if err != nil {
fmt.Println(err.Error())
continue
} else {
t.Fatal("SweepBalanceFee failed. balance : ", v.Balance.String())
}
}
if err != nil {
t.Fatal("SweepBalanceFee failed. balance : ", v.Balance.String())
}

diff := new(big.Int).Sub(v.Balance, new(big.Int).Add(amount, new(big.Int).Mul(new(big.Int).SetUint64(fee), big.NewInt(gasPrice))))
fmt.Printf("balance: %s ,amount: %s ,fee %d ,diff: %d\n", v.Balance.String(), amount.String(), fee, diff.Uint64())
}
}
*/
8 changes: 1 addition & 7 deletions ethereum/go-ethereum/consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
if expected.Cmp(header.Difficulty) != 0 {
return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, expected)
}
// Verify that the gas limit is <= 2^63-1
if header.GasLimit.Cmp(math.MaxBig63) > 0 {
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, math.MaxBig63)
}

// Verify that the gasUsed is <= gasLimit
if header.GasUsed.Cmp(header.GasLimit) > 0 {
return fmt.Errorf("invalid gasUsed: have %v, gasLimit %v", header.GasUsed, header.GasLimit)
Expand All @@ -285,9 +282,6 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
limit := new(big.Int).Set(parent.GasLimit)
limit = limit.Div(limit, params.GasLimitBoundDivisor)

if diff.Cmp(limit) >= 0 || header.GasLimit.Cmp(params.MinGasLimit) < 0 {
return fmt.Errorf("invalid gas limit: have %v, want %v += %v", header.GasLimit, parent.GasLimit, limit)
}
// Verify that the block number is parent's +1
if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
return consensus.ErrInvalidNumber
Expand Down
10 changes: 2 additions & 8 deletions ethereum/go-ethereum/core/tx_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ type txList struct {
txs *txSortedMap // Heap indexed sorted hash map of the transactions

costcap *big.Int // Price of the highest costing transaction (reset only if exceeds balance)
gascap *big.Int // Gas limit of the highest spending transaction (reset only if exceeds block limit)
}

// newTxList create a new transaction list for maintaining nonce-indexable fast,
Expand All @@ -234,7 +233,6 @@ func newTxList(strict bool) *txList {
strict: strict,
txs: newTxSortedMap(),
costcap: new(big.Int),
gascap: new(big.Int),
}
}

Expand Down Expand Up @@ -266,9 +264,6 @@ func (l *txList) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Tran
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
l.costcap = cost
}
if gas := tx.Gas(); l.gascap.Cmp(gas) < 0 {
l.gascap = gas
}
return true, old
}

Expand All @@ -290,14 +285,13 @@ func (l *txList) Forward(threshold uint64) types.Transactions {
// the newly invalidated transactions.
func (l *txList) Filter(costLimit, gasLimit *big.Int) (types.Transactions, types.Transactions) {
// If all transactions are below the threshold, short circuit
if l.costcap.Cmp(costLimit) <= 0 && l.gascap.Cmp(gasLimit) <= 0 {
if l.costcap.Cmp(costLimit) <= 0 {
return nil, nil
}
l.costcap = new(big.Int).Set(costLimit) // Lower the caps to the thresholds
l.gascap = new(big.Int).Set(gasLimit)

// Filter out all the transactions above the account's funds
removed := l.txs.Filter(func(tx *types.Transaction) bool { return tx.Cost().Cmp(costLimit) > 0 || tx.Gas().Cmp(gasLimit) > 0 })
removed := l.txs.Filter(func(tx *types.Transaction) bool { return tx.Cost().Cmp(costLimit) > 0 })

// If the list was strict, filter anything above the lowest nonce
var invalids types.Transactions
Expand Down
26 changes: 16 additions & 10 deletions ethereum/go-ethereum/core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"sync"
"time"

"third_part/lklog"
"third_part/reporttrans"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -32,8 +35,6 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
"third_part/lklog"
"third_part/reporttrans"
)

const (
Expand Down Expand Up @@ -158,7 +159,8 @@ type TxPoolConfig struct {
AccountQueue uint64 // Maximum number of non-executable transaction slots permitted per account
GlobalQueue uint64 // Maximum number of non-executable transaction slots for all accounts

Lifetime time.Duration // Maximum amount of time non-executable transaction are queued
Lifetime time.Duration // Maximum amount of time non-executable transaction are queued
FeeUpdateTime uint64
}

// DefaultTxPoolConfig contains the default configurations for the transaction
Expand Down Expand Up @@ -652,7 +654,17 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
reportData.Cost = tx.Cost().String()
reportData.Gas = tx.Gas().String()
reportData.Nonce = tx.Nonce()
if tx.IllegalGasLimitOrGasPrice(hashcode) {

lastBlockTime := pool.chain.CurrentBlock().Time().Uint64()
feeUpdateTime := pool.config.FeeUpdateTime
var gasFail = false
if feeUpdateTime != 0 && lastBlockTime >= feeUpdateTime {
gasFail = tx.IllegalGasLimitOrGasPrice(hashcode, true)
} else {
gasFail = tx.IllegalGasLimitOrGasPrice(hashcode, false)
}

if gasFail {
reportData.Result = reporttrans.ERR_GAS_LIMIT_OR_GAS_PRICE
reporttrans.Report(reportData)
log.Error("Unallowed value", "gasLimit", tx.Gas(), "gasPrice", tx.GasPrice())
Expand All @@ -673,12 +685,6 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
reporttrans.Report(reportData)
return ErrNegativeValue
}
// Ensure the transaction doesn't exceed the current block limit gas.
if pool.currentMaxGas.Cmp(tx.Gas()) < 0 {
reportData.Result = reporttrans.ERR_GAS_LIMIT
reporttrans.Report(reportData)
return ErrGasLimit
}

if tx.To() != nil {
log.Debug("Transfer transaction occurring", "from", from.String(), "to", tx.To().String(), "amount", tx.Value(), "price", tx.GasPrice(), "nonce", tx.Nonce())
Expand Down
10 changes: 6 additions & 4 deletions ethereum/go-ethereum/core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type receiptStorageRLP struct {
ContractAddress common.Address
Logs []*LogForStorage
GasUsed *big.Int
Status uint
}

// NewReceipt creates a barebone transaction receipt, copying the init fields.
Expand Down Expand Up @@ -160,6 +161,7 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
ContractAddress: r.ContractAddress,
Logs: make([]*LogForStorage, len(r.Logs)),
GasUsed: r.GasUsed,
Status: r.Status,
}
for i, log := range r.Logs {
enc.Logs[i] = (*LogForStorage)(log)
Expand All @@ -174,17 +176,17 @@ func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode(&dec); err != nil {
return err
}
if err := (*Receipt)(r).setStatus(dec.PostStateOrStatus); err != nil {
return err
}
// if err := (*Receipt)(r).setStatus(dec.PostStateOrStatus); err != nil {
// return err
// }
// Assign the consensus fields
r.CumulativeGasUsed, r.Bloom = dec.CumulativeGasUsed, dec.Bloom
r.Logs = make([]*Log, len(dec.Logs))
for i, log := range dec.Logs {
r.Logs[i] = (*Log)(log)
}
// Assign the implementation fields
r.TxHash, r.ContractAddress, r.GasUsed = dec.TxHash, dec.ContractAddress, dec.GasUsed
r.TxHash, r.ContractAddress, r.GasUsed, r.Status = dec.TxHash, dec.ContractAddress, dec.GasUsed, dec.Status
return nil
}

Expand Down
Loading