Skip to content

Commit

Permalink
disable bigint wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy committed Apr 25, 2024
1 parent 7dd63f4 commit 86738c4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func Unlock(native *native.NativeService) ([]byte, error) {
return utils.BYTE_TRUE, nil
}
// unlock ont or ong from current proxy contract into toAddress
transferInput := getTransferInput(ont.TransferState{contract, toAddress, args.Value})
transferInput := getTransferInput(ont.TransferState{From: contract, To: toAddress, Value: args.Value})
if _, err = native.NativeCall(assetAddress, ont.TRANSFER_NAME, transferInput); err != nil {
return utils.BYTE_FALSE, fmt.Errorf("[Unlock] NativeCall contract:%s 'transfer(%s, %s, %d)' error:%s", hex.EncodeToString(assetAddress[:]), hex.EncodeToString(contract[:]), toAddress.ToBase58(), args.Value, err)
}
Expand Down
5 changes: 4 additions & 1 deletion smartcontract/service/native/ont/ont.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func doTransfer(native *native.NativeService, transfers *TransferStatesV2) ([]by

func OntTransfer(native *native.NativeService) ([]byte, error) {
var transfers TransferStates
if native.Height <= 17370000 {
transfers.uint64Wrapping = true
}
source := common.NewZeroCopySource(native.Input)
if err := transfers.Deserialization(source); err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode, "[Transfer] TransferStates deserialize error!")
Expand Down Expand Up @@ -511,7 +514,7 @@ func getTransferArgs(contract, address common.Address, value uint64) ([]byte, er
To: address,
Value: value,
}
transfers := TransferStates{[]TransferState{state}}
transfers := TransferStates{States: []TransferState{state}}

transfers.Serialization(bf)
return bf.Bytes(), nil
Expand Down
17 changes: 12 additions & 5 deletions smartcontract/service/native/ont/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (

// TransferStates
type TransferStates struct {
States []TransferState
States []TransferState
uint64Wrapping bool
}

type TransferStatesV2 struct {
Expand Down Expand Up @@ -58,6 +59,7 @@ func (this *TransferStates) Deserialization(source *common.ZeroCopySource) error
}
for i := 0; uint64(i) < n; i++ {
var state TransferState
state.uint64Wrapping = this.uint64Wrapping
if err := state.Deserialization(source); err != nil {
return err
}
Expand Down Expand Up @@ -125,9 +127,10 @@ func (this *TransferStateV2) Deserialization(source *common.ZeroCopySource) erro
}

type TransferState struct {
From common.Address
To common.Address
Value uint64
From common.Address
To common.Address
Value uint64
uint64Wrapping bool
}

func (this *TransferState) ToV2() *TransferStateV2 {
Expand Down Expand Up @@ -156,7 +159,11 @@ func (this *TransferState) Deserialization(source *common.ZeroCopySource) error
return err
}

this.Value, err = utils.DecodeVarUint(source)
if this.uint64Wrapping {
this.Value, err = utils.DecodeVarUintWrapping(source)
} else {
this.Value, err = utils.DecodeVarUint(source)
}

return err
}
Expand Down
17 changes: 16 additions & 1 deletion smartcontract/service/native/utils/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func EncodeAddress(sink *common.ZeroCopySink, addr common.Address) (size uint64)
}

func EncodeVarUint(sink *common.ZeroCopySink, value uint64) (size uint64) {
return sink.WriteVarBytes(common.BigIntToNeoBytes(big.NewInt(int64(value))))
return sink.WriteVarBytes(common.BigIntToNeoBytes(big.NewInt(0).SetUint64(value)))
}

func EncodeVarBytes(sink *common.ZeroCopySink, v []byte) (size uint64) {
Expand All @@ -47,6 +47,21 @@ func EncodeBool(sink *common.ZeroCopySink, value bool) {
}

func DecodeVarUint(source *common.ZeroCopySource) (uint64, error) {
value, _, irregular, eof := source.NextVarBytes()
if eof {
return 0, io.ErrUnexpectedEOF
}
if irregular {
return 0, common.ErrIrregularData
}
v := common.BigIntFromNeoBytes(value)
if v.Cmp(big.NewInt(0)) < 0 || !v.IsUint64() {
return 0, fmt.Errorf("%s", "value not uint64")
}
return v.Uint64(), nil
}

func DecodeVarUintWrapping(source *common.ZeroCopySource) (uint64, error) {
value, _, irregular, eof := source.NextVarBytes()
if eof {
return 0, io.ErrUnexpectedEOF
Expand Down
11 changes: 11 additions & 0 deletions txnpool/proc/txnpool_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,17 @@ func (s *TXPoolServer) verifyBlock(req *tc.VerifyBlockReq, sender *actor.PID) {
// Check whether a tx's gas price is lower than the required, if yes, just return error
txs := make(map[common.Uint256]*txtypes.Transaction, len(req.Txs))
for _, t := range req.Txs {
if isSenderLimited(t.GetSignatureAddresses()) {
entry := &tc.VerifyTxResult{
Height: req.Height,
Tx: t,
ErrCode: errors.ErrNoAccount,
}
processedTxs = append(processedTxs, entry)
sender.Tell(&tc.VerifyBlockRsp{TxnPool: processedTxs})
log.Warnf("no sender account for transaction: %x", t.ToArray())
return
}
if t.GasPrice < s.gasPrice {
entry := &tc.VerifyTxResult{
Height: req.Height,
Expand Down

0 comments on commit 86738c4

Please sign in to comment.