Skip to content

Commit

Permalink
debugs & add versionUpdate docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iiinotlll committed May 19, 2023
1 parent 66d53c5 commit 0b9359f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# block-emulator
This is the official handbook of **BlockEmulator**(version 1.0).

# Version Updates
View the updates of **BlockEmulator** here:
**[ Version Updates ](https://github.com/HuangLab-SYSU/block-emulator/blob/main/docs/versionUpdate.md)**

# Introduction

## 1. The Background of BlockEmulator
Expand Down
16 changes: 13 additions & 3 deletions chain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (bc *BlockChain) GetUpdateStatusTrie(txs []*core.Transaction) common.Hash {
for i, tx := range txs {
// fmt.Printf("tx %d: %s, %s\n", i, tx.Sender, tx.Recipient)
// senderIn := false
if bc.Get_PartitionMap(tx.Sender) == bc.ChainConfig.ShardID || tx.HasBroker {
if !tx.Relayed && (bc.Get_PartitionMap(tx.Sender) == bc.ChainConfig.ShardID || tx.HasBroker) {
// senderIn = true
// fmt.Printf("the sender %s is in this shard %d, \n", tx.Sender, bc.ChainConfig.ShardID)
// modify local accountstate
Expand All @@ -100,7 +100,7 @@ func (bc *BlockChain) GetUpdateStatusTrie(txs []*core.Transaction) common.Hash {
s_state = core.DecodeAS(s_state_enc)
}
s_balance := s_state.Balance
if s_balance.Cmp(tx.Value) < 0 {
if s_balance.Cmp(tx.Value) == -1 {
fmt.Printf("the balance is less than the transfer amount\n")
continue
}
Expand Down Expand Up @@ -294,12 +294,20 @@ func (bc *BlockChain) AddAccounts(ac []string, as []*core.AccountState) {
log.Panic(err)
}
// handle transactions, the signature check is ignored here
cnt := 0
for i, addr := range ac {
if bc.Get_PartitionMap(addr) != bc.ChainConfig.ShardID {
log.Panic("err account")
}
if as[i].Balance.Cmp(params.Init_Balance) == 0 {
cnt++
}
err = st.Update([]byte(addr), as[i].Encode())
if err != nil {
log.Panic(err)
}
}
fmt.Println("accounts' balance are equal to init", cnt)
// commit the memory trie to the database in the disk
rt, ns := st.Commit(false)
err = bc.triedb.Update(trie.NewWithNodeSet(ns))
Expand All @@ -311,7 +319,7 @@ func (bc *BlockChain) AddAccounts(ac []string, as []*core.AccountState) {
log.Panic(err)
}
}

rt = bc.GetUpdateStatusTrie([]*core.Transaction{})
bh := &core.BlockHeader{
ParentBlockHash: bc.CurrentBlock.Hash,
StateRoot: rt.Bytes(),
Expand All @@ -335,6 +343,7 @@ func (bc *BlockChain) FetchAccounts(addrs []string) []*core.AccountState {
if err != nil {
log.Panic(err)
}
cnt := 0
for _, addr := range addrs {
asenc, _ := st.Get([]byte(addr))
var state_a *core.AccountState
Expand All @@ -345,6 +354,7 @@ func (bc *BlockChain) FetchAccounts(addrs []string) []*core.AccountState {
Nonce: uint64(0),
Balance: ib,
}
cnt++
} else {
state_a = core.DecodeAS(asenc)
}
Expand Down
22 changes: 18 additions & 4 deletions consensus_shard/pbft_all/accountTransfer_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (cphm *CLPAPbftInsideExtraHandleMod) sendAccounts_and_Txs() {
asFetched := cphm.pbftNode.CurChain.FetchAccounts(accountToFetch)
// send the accounts to other shards
cphm.pbftNode.CurChain.Txpool.GetLocked()
cphm.pbftNode.pl.Plog.Println("The size of tx pool is: ", len(cphm.pbftNode.CurChain.Txpool.TxQueue))
for i := uint64(0); i < cphm.pbftNode.pbftChainConfig.ShardNums; i++ {
if i == cphm.pbftNode.ShardID {
continue
Expand All @@ -88,11 +89,11 @@ func (cphm *CLPAPbftInsideExtraHandleMod) sendAccounts_and_Txs() {
for head < tail {
ptx := cphm.pbftNode.CurChain.Txpool.TxQueue[head]
// if this is a normal transaction or ctx1 before re-sharding && the addr is correspond
_, ok := addrSet[ptx.Sender]
condition1 := ok && !ptx.Relayed
_, ok1 := addrSet[ptx.Sender]
condition1 := ok1 && !ptx.Relayed
// if this tx is ctx2
_, ok = addrSet[ptx.Recipient]
condition2 := ok && ptx.Relayed
_, ok2 := addrSet[ptx.Recipient]
condition2 := ok2 && ptx.Relayed
if condition1 || condition2 {
txSend = append(txSend, ptx)
tail--
Expand All @@ -118,6 +119,7 @@ func (cphm *CLPAPbftInsideExtraHandleMod) sendAccounts_and_Txs() {
networks.TcpDial(send_msg, cphm.pbftNode.ip_nodeTable[i][0])
cphm.pbftNode.pl.Plog.Printf("The message to shard %d is sent\n", i)
}
cphm.pbftNode.pl.Plog.Println("after sending, The size of tx pool is: ", len(cphm.pbftNode.CurChain.Txpool.TxQueue))
cphm.pbftNode.CurChain.Txpool.GetUnlocked()
}

Expand All @@ -139,7 +141,17 @@ func (cphm *CLPAPbftInsideExtraHandleMod) proposePartition() (bool, *message.Req
cphm.cdm.ReceivedNewTx = append(cphm.cdm.ReceivedNewTx, at.Txs...)
}
// propose, send all txs to other nodes in shard
cphm.pbftNode.pl.Plog.Println("The number of ReceivedNewTx: ", len(cphm.cdm.ReceivedNewTx))
for _, tx := range cphm.cdm.ReceivedNewTx {
if !tx.Relayed && cphm.cdm.ModifiedMap[cphm.cdm.AccountTransferRound][tx.Sender] != cphm.pbftNode.ShardID {
log.Panic("error tx")
}
if tx.Relayed && cphm.cdm.ModifiedMap[cphm.cdm.AccountTransferRound][tx.Recipient] != cphm.pbftNode.ShardID {
log.Panic("error tx")
}
}
cphm.pbftNode.CurChain.Txpool.AddTxs2Pool(cphm.cdm.ReceivedNewTx)
cphm.pbftNode.pl.Plog.Println("The size of txpool: ", len(cphm.pbftNode.CurChain.Txpool.TxQueue))

atmaddr := make([]string, 0)
atmAs := make([]*core.AccountState, 0)
Expand Down Expand Up @@ -174,6 +186,8 @@ func (cphm *CLPAPbftInsideExtraHandleMod) accountTransfer_do(atm *message.Accoun
}
cphm.pbftNode.pl.Plog.Printf("%d key-vals are updated\n", cnt)
// add the account into the state trie
cphm.pbftNode.pl.Plog.Printf("%d addrs to add\n", len(atm.Addrs))
cphm.pbftNode.pl.Plog.Printf("%d accountstates to add\n", len(atm.AccountState))
cphm.pbftNode.CurChain.AddAccounts(atm.Addrs, atm.AccountState)

if uint64(len(cphm.cdm.ModifiedMap)) != atm.ATid {
Expand Down
4 changes: 2 additions & 2 deletions core/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func (as *AccountState) Deduct(val *big.Int) bool {
if as.Balance.Cmp(val) < 0 {
return false
}
as.Balance = as.Balance.Sub(as.Balance, val)
as.Balance.Sub(as.Balance, val)
return true
}

// Increase the balance of an account
func (s *AccountState) Deposit(value *big.Int) {
s.Balance = s.Balance.Add(s.Balance, value)
s.Balance.Add(s.Balance, value)
}

// Encode AccountState in order to store in the MPT
Expand Down
7 changes: 7 additions & 0 deletions docs/versionUpdate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Version Updates

## 2023/05/19
Debugs:
1. **Problem**: The init_balances of some accounts are wrong.
- **Reason**: The init_balances are set by shallow copy ("="), but *Balance* is a pointer (*big.Int)
- **Solution**: Replace the shallow copy with deep copy (with an "adding-zero" form)

0 comments on commit 0b9359f

Please sign in to comment.