Skip to content

Commit

Permalink
Merge pull request #282 from AschPlatform/2018-12-14/optimize_transac…
Browse files Browse the repository at this point in the history
…tion_fee

2018 12 14/optimize transaction fee
  • Loading branch information
sqfasd authored Jan 4, 2019
2 parents 0972d31 + 7b255d1 commit f5b3769
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 2 deletions.
83 changes: 83 additions & 0 deletions src/contract/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,87 @@ module.exports = {
}
return null
},

async pledge(netAmount, energyAmount) {
if (!Number.isInteger(netAmount) || !Number.isInteger(energyAmount)) return 'Amount should be integer'
if (netAmount < 0 || energyAmount < 0) return 'Amount should be positive number'
const sender = this.sender
const totalAmount = netAmount + energyAmount
if (sender.xas < totalAmount) return 'Insufficient balance in Accounts'
sender.xas -= totalAmount
app.sdb.update('Account', sender, { address: sender.address })
let pledgeAccount = await app.sdb.load('AccountPledge', sender.address)
if (!pledgeAccount) {
const currentDay = Number.parseInt(this.block.height / app.util.constants.blocksPerDay, 10)
app.sdb.create('AccountPledge', {
address: sender.address,
lastFreeNetUpdateDay: currentDay,
lastNetUpdateDay: currentDay,
lastEnergyUpdateDay: currentDay,
heightOffset: this.block.height % app.util.constants.blocksPerDay,
})
pledgeAccount = await app.sdb.load('AccountPledge', sender.address)
}

const totalPledges = await app.sdb.loadMany('AccountTotalPledge', { })
let totalPledge
if (totalPledges.length === 0) {
app.sdb.create('AccountTotalPledge', {
tid: this.trs.id,
netPerXAS: app.util.constants.netPerXAS,
energyPerXAS: app.util.constants.energyPerXAS,
netPerPledgedXAS: app.util.constants.netPerPledgedXAS,
energyPerPledgedXAS: app.util.constants.energyPerPledgedXAS,
freeNetLimit: app.util.constants.freeNetLimitPerDay,
gasPrice: app.util.constants.energyPerGas,
})
totalPledge = await app.sdb.load('AccountTotalPledge', this.trs.id)
} else {
totalPledge = totalPledges[0]
}
if (netAmount > 0) {
pledgeAccount.pledgeAmountForNet += netAmount
totalPledge.totalPledgeForNet += netAmount
pledgeAccount.netLockHeight = this.block.height
}
if (energyAmount > 0) {
pledgeAccount.pledgeAmountForEnergy += energyAmount
totalPledge.totalPledgeForEnergy += energyAmount
pledgeAccount.energyLockHeight = this.block.height
}
app.sdb.update('AccountPledge', pledgeAccount, { address: sender.address })
app.sdb.update('AccountTotalPledge', totalPledge, { tid: totalPledge.tid })

return null
},

async unpledge(netAmount, energyAmount) {
if (!Number.isInteger(netAmount) || !Number.isInteger(energyAmount)) return 'Amount should be integer'
if (netAmount < 0 || energyAmount < 0) return 'Amount should be positive number'
const sender = this.sender
const pledgeAccount = await app.sdb.load('AccountPledge', sender.address)
if (!pledgeAccount) return `No pledege for account ${sender.address}`
const totalPledges = await app.sdb.loadMany('AccountTotalPledge', { })
if (totalPledges.length === 0) return 'Total pledge is not set'
if (totalPledges[0].totalPledgeForNet < netAmount || totalPledges[0].totalPledgeForEnergy < energyAmount) return 'Insufficient balance in AccountTotalPledges'
const totalAmount = netAmount + energyAmount
sender.xas += totalAmount
app.sdb.update('Account', sender, { address: sender.address })
if (netAmount > 0) {
if (pledgeAccount.netLockHeight > (this.block.height - app.util.constants.pledageDays * app.util.constants.blocksPerDay)) return 'Pledge duration should be greater than 3 day'
if (pledgeAccount.pledgeAmountForNet < netAmount) return 'Insufficient balance in AccountPledges'
pledgeAccount.pledgeAmountForNet -= netAmount
totalPledges[0].totalPledgeForNet -= netAmount
}
if (energyAmount > 0) {
if (pledgeAccount.energyLockHeight > (this.block.height - app.util.constants.pledageDays * app.util.constants.blocksPerDay)) return 'Pledge duration should be greater than 3 day'
if (pledgeAccount.pledgeAmountForEnergy < energyAmount) return 'Insufficient balance in AccountPledges'
pledgeAccount.pledgeAmountForEnergy -= energyAmount
totalPledges[0].totalPledgeForEnergy -= energyAmount
}
app.sdb.update('AccountPledge', pledgeAccount, { address: sender.address })
app.sdb.update('AccountTotalPledge', totalPledges[0], { tid: totalPledges[0].tid })

return null
},
}
4 changes: 2 additions & 2 deletions src/contract/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ module.exports = {
amount = app.util.bignumber(String(amount))
const senderId = this.sender.address
const addr = app.util.address.generateLockedAddress(senderId)
const lockAccount = app.sdb.createOrLoad('Account', { xas: 0, address: addr, name: null })
if (amount.plus(lockAccount.entity.xas).lt(app.util.constants.initialDeposit)) return `Deposit amount should be greater than ${app.util.constants.initialDeposit - lockAccount.entity.xas}`
const lockAccount = app.sdb.createOrLoad('Account', { xas: 0, address: addr, name: null }).entity
if (amount.plus(lockAccount.xas).lt(app.util.constants.initialDeposit)) return `Deposit amount should be greater than ${app.util.constants.initialDeposit - lockAccount.xas}`
const m = await app.util.gateway.getGatewayMember(gatewayName, senderId)
if (!m) return 'Please register as a gateway member before deposit bail'
if (amount.gt(String(this.sender.xas))) return 'Insufficient balance'
Expand Down
82 changes: 82 additions & 0 deletions src/contract/proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const VALID_TOPICS = [
'gateway_revoke',
'gateway_claim',
'bancor_init',
'update_net_energy_per_xas',
'update_net_energy_per_pledged_xas',
'update_gasprice',
'update_free_net_limit',
]

async function doGatewayRegister(params, context) {
Expand Down Expand Up @@ -179,6 +183,36 @@ async function doBancorInit(params, context) {
}
}

async function doUpdateNetEnergyPerXAS(params) {
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
const totalPledge = totalPledges[0]
totalPledge.netPerXAS = params.netPerXAS
totalPledge.energyPerXAS = params.energyPerXAS
app.sdb.update('AccountTotalPledge', totalPledge, { tid: totalPledge.tid })
}

async function doUpdateNetEnergyPerPledgedXAS(params) {
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
const totalPledge = totalPledges[0]
totalPledge.netPerPledgedXAS = params.netPerPledgedXAS
totalPledge.energyPerPledgedXAS = params.energyPerPledgedXAS
app.sdb.update('AccountTotalPledge', totalPledge, { tid: totalPledge.tid })
}

async function doUpdateGasprice(params) {
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
const totalPledge = totalPledges[0]
totalPledge.gasprice = params.gasprice
app.sdb.update('AccountTotalPledge', totalPledge, { tid: totalPledge.tid })
}

async function doUpdateFreeNetLimit(params) {
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
const totalPledge = totalPledges[0]
totalPledge.freeNetLimit = params.freeNetLimit
app.sdb.update('AccountTotalPledge', totalPledge, { tid: totalPledge.tid })
}

async function validateGatewayRegister(content/* , context */) {
if (!content.name || !/^[A-Za-z0-9]{3,16}$/.test(content.name)) {
throw new Error('Invalid gateway name')
Expand Down Expand Up @@ -315,6 +349,38 @@ async function validateBancorContent(content/* , context */) {
}
}

async function validateNetEnergyPerXAS(content/* , context */) {
app.validate('amount', String(content.netPerXAS))
app.validate('amount', String(content.energyPerXAS))
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
if (totalPledges.length === 0) throw new Error('Total pledge is not set')
if (content.netPerXAS < 0) throw new Error('Net per XAS should be positive number')
if (content.energyPerXAS < 0) throw new Error('Energy per XAS should be positive number')
}

async function validateNetEnergyPerPledgedXAS(content/* , context */) {
app.validate('amount', String(content.netPerPledgedXAS))
app.validate('amount', String(content.energyPerPledgedXAS))
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
if (totalPledges.length === 0) throw new Error('Total pledge is not set')
if (content.netPerPledgedXAS < 0) throw new Error('Net per pledged XAS should be positive number')
if (content.energyPerPledgedXAS < 0) throw new Error('Energy per pledged XAS should be positive number')
}

async function validateGasprice(content/* , context */) {
app.validate('amount', String(content.gasprice))
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
if (totalPledges.length === 0) throw new Error('Total pledge is not set')
if (content.gasprice < 0) throw new Error('Gas price should be positive number')
}

async function validateFreeNetLimit(content/* , context */) {
app.validate('amount', String(content.freeNetLimit))
const totalPledges = await app.sdb.findAll('AccountTotalPledge', { })
if (totalPledges.length === 0) throw new Error('Total pledge is not set')
if (content.freeNetLimit < 0) throw new Error('Free net limit per day should be positive number')
}

module.exports = {
async propose(title, desc, topic, content, endHeight) {
if (!/^[A-Za-z0-9_\-+!@$% ]{10,100}$/.test(title)) return 'Invalid proposal title'
Expand All @@ -335,6 +401,14 @@ module.exports = {
await validateGatewayClaim(content, this)
} else if (topic === 'bancor_init') {
await validateBancorContent(content, this)
} else if (topic === 'update_net_energy_per_xas') {
await validateNetEnergyPerXAS(content, this)
} else if (topic === 'update_net_energy_per_pledged_xas') {
await validateNetEnergyPerPledgedXAS(content, this)
} else if (topic === 'update_gasprice') {
await validateGasprice(content, this)
} else if (topic === 'update_free_net_limit') {
await validateFreeNetLimit(content, this)
}

app.sdb.create('Proposal', {
Expand Down Expand Up @@ -399,6 +473,14 @@ module.exports = {
await doGatewayClaim(content, this)
} else if (topic === 'bancor_init') {
await doBancorInit(content, this)
} else if (topic === 'update_net_energy_per_xas') {
await doUpdateNetEnergyPerXAS(content, this)
} else if (topic === 'update_net_energy_per_pledged_xas') {
await doUpdateNetEnergyPerPledgedXAS(content, this)
} else if (topic === 'update_gasprice') {
await doUpdateGasprice(content, this)
} else if (topic === 'update_free_net_limit') {
await doUpdateFreeNetLimit(content, this)
} else {
unknownTopic = true
}
Expand Down
13 changes: 13 additions & 0 deletions src/interface/pledges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
async function getPledges(req) {
let pledge
if (req.query.address) {
pledge = await app.util.pledges.getNetEnergyLimit(req.query.address)
} else {
pledge = await app.util.pledges.getPledgeConfig()
}
return pledge
}

module.exports = (router) => {
router.get('/', getPledges)
}
17 changes: 17 additions & 0 deletions src/model/account-pledge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
table: 'account_pledges',
tableFields: [
{ name: 'address', type: 'String', length: 50, primary_key: true, not_null: true },
{ name: 'freeNetUsed', type: 'BigInt', default: 0 },
{ name: 'netUsed', type: 'BigInt', default: 0 },
{ name: 'energyUsed', type: 'BigInt', default: 0 },
{ name: 'pledgeAmountForNet', type: 'BigInt', default: 0 },
{ name: 'pledgeAmountForEnergy', type: 'BigInt', default: 0 },
{ name: 'netLockHeight', type: 'BigInt', default: 0 },
{ name: 'energyLockHeight', type: 'BigInt', default: 0 },
{ name: 'lastFreeNetUpdateDay', type: 'BigInt', default: 0 },
{ name: 'lastNetUpdateDay', type: 'BigInt', default: 0 },
{ name: 'lastEnergyUpdateDay', type: 'BigInt', default: 0 },
{ name: 'heightOffset', type: 'BigInt', default: 0 },
]
}
14 changes: 14 additions & 0 deletions src/model/account-total-pledge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
table: 'account_total_pledges',
tableFields: [
{ name: 'tid', type: 'String', length: 64, not_null: true, primary_key: true },
{ name: 'totalPledgeForNet', type: 'BigInt', default: 0 },
{ name: 'totalPledgeForEnergy', type: 'BigInt', default: 0 },
{ name: 'netPerXAS', type: 'BigInt', default: 0 },
{ name: 'energyPerXAS', type: 'BigInt', default: 0 },
{ name: 'netPerPledgedXAS', type: 'BigInt', default: 0 },
{ name: 'energyPerPledgedXAS', type: 'BigInt', default: 0 },
{ name: 'gasprice', type: 'Number', default: 1.0 },
{ name: 'freeNetLimit', type: 'BigInt', default: 0 },
]
}
12 changes: 12 additions & 0 deletions src/model/netenergyconsumption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
table: 'netenergyconsumptions',
tableFields: [
{ name: 'tid', type: 'String', length: 64, not_null: true, primary_key: true },
{ name: 'netUsed', type: 'BigInt', default: 0 },
{ name: 'energyUsed', type: 'BigInt', default: 0 },
{ name: 'fee', type: 'BigInt', default: 0 },
{ name: 'isFeeDeduct', type: 'Number', default: 0 },
{ name: 'height', type: 'BigInt', not_null: true },
{ name: 'address', type: 'String', length: 50, not_null: true },
]
}

0 comments on commit f5b3769

Please sign in to comment.