@@ -22,6 +22,7 @@ import (
22
22
"github.com/aergoio/aergo/v2/internal/enc/base58"
23
23
"github.com/aergoio/aergo/v2/internal/enc/proto"
24
24
"github.com/aergoio/aergo/v2/state"
25
+ "github.com/aergoio/aergo/v2/state/statedb"
25
26
"github.com/aergoio/aergo/v2/types"
26
27
"github.com/aergoio/aergo/v2/types/message"
27
28
)
@@ -136,6 +137,72 @@ func (cs *ChainService) getReceipt(txHash []byte) (*types.Receipt, error) {
136
137
return r , nil
137
138
}
138
139
140
+ func (cs * ChainService ) getReceipts (blockHash []byte ) (* types.Receipts , error ) {
141
+ block , err := cs .cdb .getBlock (blockHash )
142
+ if err != nil {
143
+ return nil , & ErrNoBlock {blockHash }
144
+ }
145
+
146
+ blockInMainChain , err := cs .cdb .GetBlockByNo (block .Header .BlockNo )
147
+ if ! bytes .Equal (block .BlockHash (), blockInMainChain .BlockHash ()) {
148
+ return nil , errors .New ("cannot find a receipt" )
149
+ }
150
+
151
+ receipts , err := cs .cdb .getReceipts (block .BlockHash (), block .GetHeader ().BlockNo , cs .cfg .Hardfork )
152
+ if err != nil {
153
+ return nil , err
154
+ }
155
+
156
+ for idx , r := range receipts .Get () {
157
+ r .SetMemoryInfo (blockHash , block .Header .BlockNo , int32 (idx ))
158
+
159
+ r .ContractAddress = types .AddressOrigin (r .ContractAddress )
160
+
161
+ for _ , tx := range block .GetBody ().GetTxs () {
162
+ if bytes .Equal (r .GetTxHash (), tx .GetHash ()) {
163
+ r .From = tx .GetBody ().GetAccount ()
164
+ r .To = tx .GetBody ().GetRecipient ()
165
+ break
166
+ }
167
+ }
168
+ }
169
+
170
+ return receipts , nil
171
+ }
172
+
173
+ func (cs * ChainService ) getReceiptsByNo (blockNo types.BlockNo ) (* types.Receipts , error ) {
174
+ blockInMainChain , err := cs .cdb .GetBlockByNo (blockNo )
175
+ if err != nil {
176
+ return nil , & ErrNoBlock {blockNo }
177
+ }
178
+
179
+ block , err := cs .cdb .getBlock (blockInMainChain .BlockHash ())
180
+ if ! bytes .Equal (block .BlockHash (), blockInMainChain .BlockHash ()) {
181
+ return nil , errors .New ("cannot find a receipt" )
182
+ }
183
+
184
+ receipts , err := cs .cdb .getReceipts (block .BlockHash (), block .GetHeader ().BlockNo , cs .cfg .Hardfork )
185
+ if err != nil {
186
+ return nil , err
187
+ }
188
+
189
+ for idx , r := range receipts .Get () {
190
+ r .SetMemoryInfo (blockInMainChain .BlockHash (), blockNo , int32 (idx ))
191
+
192
+ r .ContractAddress = types .AddressOrigin (r .ContractAddress )
193
+
194
+ for _ , tx := range block .GetBody ().GetTxs () {
195
+ if bytes .Equal (r .GetTxHash (), tx .GetHash ()) {
196
+ r .From = tx .GetBody ().GetAccount ()
197
+ r .To = tx .GetBody ().GetRecipient ()
198
+ break
199
+ }
200
+ }
201
+ }
202
+
203
+ return receipts , nil
204
+ }
205
+
139
206
func (cs * ChainService ) getEvents (events * []* types.Event , blkNo types.BlockNo , filter * types.FilterInfo ,
140
207
argFilter []types.ArgFilter ) uint64 {
141
208
blkHash , err := cs .cdb .getHashByNo (blkNo )
@@ -624,7 +691,7 @@ func newBlockExecutor(cs *ChainService, bState *state.BlockState, block *types.B
624
691
}
625
692
626
693
// NewTxExecutor returns a new TxExecFn.
627
- func NewTxExecutor (execCtx context.Context , ccc consensus.ChainConsensusCluster , cdb contract.ChainAccessor , bi * types.BlockHeaderInfo , preloadService int ) TxExecFn {
694
+ func NewTxExecutor (execCtx context.Context , ccc consensus.ChainConsensusCluster , cdb contract.ChainAccessor , bi * types.BlockHeaderInfo , executionMode int ) TxExecFn {
628
695
return func (bState * state.BlockState , tx types.Transaction ) error {
629
696
if bState == nil {
630
697
logger .Error ().Msg ("bstate is nil in txExec" )
@@ -636,7 +703,7 @@ func NewTxExecutor(execCtx context.Context, ccc consensus.ChainConsensusCluster,
636
703
}
637
704
blockSnap := bState .Snapshot ()
638
705
639
- err := executeTx (execCtx , ccc , cdb , bState , tx , bi , preloadService )
706
+ err := executeTx (execCtx , ccc , cdb , bState , tx , bi , executionMode )
640
707
if err != nil {
641
708
logger .Error ().Err (err ).Str ("hash" , base58 .Encode (tx .GetHash ())).Msg ("tx failed" )
642
709
if err2 := bState .Rollback (blockSnap ); err2 != nil {
@@ -653,22 +720,14 @@ func (e *blockExecutor) execute() error {
653
720
// Receipt must be committed unconditionally.
654
721
if ! e .commitOnly {
655
722
defer contract .CloseDatabase ()
656
- var preloadTx * types.Tx
657
- numTxs := len (e .txs )
658
- for i , tx := range e .txs {
659
- // if tx is not the last one, preload the next tx
660
- if i != numTxs - 1 {
661
- preloadTx = e .txs [i + 1 ]
662
- contract .RequestPreload (e .BlockState , e .bi , preloadTx , tx , contract .ChainService )
663
- }
723
+ logger .Trace ().Int ("txCount" , len (e .txs )).Msg ("executing txs" )
724
+ for _ , tx := range e .txs {
664
725
// execute the transaction
665
726
if err := e .execTx (e .BlockState , types .NewTransaction (tx )); err != nil {
666
727
//FIXME maybe system error. restart or panic
667
728
// all txs have executed successfully in BP node
668
729
return err
669
730
}
670
- // mark the next preload tx to be executed
671
- contract .SetPreloadTx (preloadTx , contract .ChainService )
672
731
}
673
732
674
733
if e .validateSignWait != nil {
@@ -870,7 +929,7 @@ func adjustRv(ret string) string {
870
929
return ret
871
930
}
872
931
873
- func resetAccount (account * state.V , fee * big.Int , nonce * uint64 ) error {
932
+ func resetAccount (account * state.AccountState , fee * big.Int , nonce * uint64 ) error {
874
933
account .Reset ()
875
934
if fee != nil {
876
935
if account .Balance ().Cmp (fee ) < 0 {
@@ -884,7 +943,7 @@ func resetAccount(account *state.V, fee *big.Int, nonce *uint64) error {
884
943
return account .PutState ()
885
944
}
886
945
887
- func executeTx (execCtx context.Context , ccc consensus.ChainConsensusCluster , cdb contract.ChainAccessor , bs * state.BlockState , tx types.Transaction , bi * types.BlockHeaderInfo , preloadService int ) error {
946
+ func executeTx (execCtx context.Context , ccc consensus.ChainConsensusCluster , cdb contract.ChainAccessor , bs * state.BlockState , tx types.Transaction , bi * types.BlockHeaderInfo , executionMode int ) error {
888
947
var (
889
948
txBody = tx .GetBody ()
890
949
isQuirkTx = types .IsQuirkTx (tx .GetHash ())
@@ -910,7 +969,7 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
910
969
return err
911
970
}
912
971
913
- sender , err := bs . GetAccountStateV (account )
972
+ sender , err := state . GetAccountState (account , bs . StateDB )
914
973
if err != nil {
915
974
return err
916
975
}
@@ -923,16 +982,16 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
923
982
if recipient , err = name .Resolve (bs , txBody .Recipient , isQuirkTx ); err != nil {
924
983
return err
925
984
}
926
- var receiver * state.V
985
+ var receiver * state.AccountState
927
986
status := "SUCCESS"
928
987
if len (recipient ) > 0 {
929
- receiver , err = bs . GetAccountStateV (recipient )
988
+ receiver , err = state . GetAccountState (recipient , bs . StateDB )
930
989
if receiver != nil && txBody .Type == types .TxType_REDEPLOY {
931
990
status = "RECREATED"
932
991
receiver .SetRedeploy ()
933
992
}
934
993
} else {
935
- receiver , err = bs . CreateAccountStateV (contract .CreateContractID (txBody .Account , txBody .Nonce ))
994
+ receiver , err = state . CreateAccountState (contract .CreateContractID (txBody .Account , txBody .Nonce ), bs . StateDB )
936
995
status = "CREATED"
937
996
}
938
997
if err != nil {
@@ -944,7 +1003,7 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
944
1003
var events []* types.Event
945
1004
switch txBody .Type {
946
1005
case types .TxType_NORMAL , types .TxType_REDEPLOY , types .TxType_TRANSFER , types .TxType_CALL , types .TxType_DEPLOY :
947
- rv , events , txFee , err = contract .Execute (execCtx , bs , cdb , tx .GetTx (), sender , receiver , bi , preloadService , false )
1006
+ rv , events , txFee , err = contract .Execute (execCtx , bs , cdb , tx .GetTx (), sender , receiver , bi , executionMode , false )
948
1007
sender .SubBalance (txFee )
949
1008
case types .TxType_GOVERNANCE :
950
1009
txFee = new (big.Int ).SetUint64 (0 )
@@ -958,8 +1017,8 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
958
1017
return err
959
1018
}
960
1019
961
- var contractState * state .ContractState
962
- contractState , err = bs .OpenContractState (receiver .AccountID (), receiver .State ())
1020
+ var contractState * statedb .ContractState
1021
+ contractState , err = statedb .OpenContractState (receiver .ID (), receiver .State (), bs . StateDB )
963
1022
if err != nil {
964
1023
return err
965
1024
}
@@ -972,7 +1031,7 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
972
1031
}
973
1032
return types .ErrNotAllowedFeeDelegation
974
1033
}
975
- rv , events , txFee , err = contract .Execute (execCtx , bs , cdb , tx .GetTx (), sender , receiver , bi , preloadService , true )
1034
+ rv , events , txFee , err = contract .Execute (execCtx , bs , cdb , tx .GetTx (), sender , receiver , bi , executionMode , true )
976
1035
receiver .SubBalance (txFee )
977
1036
}
978
1037
@@ -1055,22 +1114,19 @@ func sendRewardCoinbase(bState *state.BlockState, coinbaseAccount []byte) error
1055
1114
return nil
1056
1115
}
1057
1116
1058
- receiverID := types . ToAccountID ( coinbaseAccount )
1059
- receiverState , err := bState .GetAccountState (receiverID )
1117
+ // add bp reward to coinbase account
1118
+ coinbaseAccountState , err := state .GetAccountState (coinbaseAccount , bState . StateDB )
1060
1119
if err != nil {
1061
1120
return err
1062
1121
}
1063
-
1064
- receiverChange := types .State (* receiverState )
1065
- receiverChange .Balance = new (big.Int ).Add (receiverChange .GetBalanceBigInt (), bpReward ).Bytes ()
1066
-
1067
- err = bState .PutState (receiverID , & receiverChange )
1122
+ coinbaseAccountState .AddBalance (bpReward )
1123
+ err = coinbaseAccountState .PutState ()
1068
1124
if err != nil {
1069
1125
return err
1070
1126
}
1071
1127
1072
1128
logger .Debug ().Str ("reward" , bpReward .String ()).
1073
- Str ("newbalance" , receiverChange . GetBalanceBigInt ().String ()).Msg ("send reward to coinbase account" )
1129
+ Str ("newbalance" , coinbaseAccountState . Balance ().String ()).Msg ("send reward to coinbase account" )
1074
1130
1075
1131
return nil
1076
1132
}
0 commit comments