Skip to content

Commit

Permalink
tx: use CommonOperation interface instead of *Tx in some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xiphon committed Mar 31, 2019
1 parent b817a16 commit a065a95
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
10 changes: 5 additions & 5 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (this *Blockchain) processNewBlocksUnsafe(blocks []safebox.SerializedBlock,
if err != nil {
return err
}
txMetadata := transaction.GetMetadata(txIndexInsideBlock, block.GetIndex(), block.GetTimestamp())
txMetadata := tx.GetMetadata(transaction, txIndexInsideBlock, block.GetIndex(), block.GetTimestamp())
if err = s.StoreTxMetadata(ctx, txID, utils.Serialize(txMetadata)); err != nil {
return err
}
Expand Down Expand Up @@ -546,10 +546,10 @@ func (this *Blockchain) TxPoolForEach(fn func(meta *tx.TxMetadata, tx *tx.Tx) bo
i := uint32(0)
time := uint32(time.Now().Unix())
this.txPool.Range(func(key, value interface{}) bool {
tx := value.(tx.Tx)
meta := tx.GetMetadata(i, 0, time)
transaction := value.(tx.Tx)
meta := tx.GetMetadata(transaction, i, 0, time)
i += 1
return fn(&meta, &tx)
return fn(&meta, &transaction)
})
}

Expand Down Expand Up @@ -660,7 +660,7 @@ func (this *Blockchain) BlockOperationsForEach(index uint32, fn func(meta *tx.Tx

operations := block.GetOperations()
for index, _ := range operations {
meta := operations[index].GetMetadata(uint32(index), block.GetIndex(), block.GetTimestamp())
meta := tx.GetMetadata(operations[index], uint32(index), block.GetIndex(), block.GetTimestamp())
if !fn(&meta, &operations[index]) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions safebox/safebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (this *Safebox) Validate(operation tx.CommonOperation) error {

// TODO: code duplicaion
height := this.accounter.GetHeight()
_, err := operation.Validate(func(number uint32) *accounter.Account {
_, err := tx.Validate(operation, func(number uint32) *accounter.Account {
accountPack := number / uint32(defaults.AccountsPerBlock)
if accountPack+defaults.MaturationHeight < height {
return this.accounter.GetAccount(number)
Expand Down Expand Up @@ -168,7 +168,7 @@ func (this *Safebox) ProcessOperations(miner *crypto.Public, timestamp uint32, o

affectedByTxes := make(map[*accounter.Account]map[uint32]uint32)
for index := range operations {
context, err := operations[index].Validate(getMaturedAccountUnsafe)
context, err := tx.Validate(operations[index], getMaturedAccountUnsafe)
if err != nil {
return nil, err
}
Expand Down
18 changes: 7 additions & 11 deletions safebox/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type CommonOperation interface {
GetPayload() []byte
GetType() txType

Validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error)
Apply(index uint32, context interface{}, accounter *accounter.Accounter) ([]uint32, error)

Serialize(w io.Writer) error
Expand All @@ -62,6 +61,7 @@ type CommonOperation interface {
getBufferToSign() []byte
getSignature() *crypto.SignatureSerialized
getSourceInfo() (number uint32, operationId uint32, publicKey *crypto.Public)
validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error)
}

// TODO: rename to transaction
Expand Down Expand Up @@ -108,17 +108,13 @@ func Sign(tx CommonOperation, priv *ecdsa.PrivateKey) (txID string, raw []byte,
return GetTxIdString(tx), serialized.Bytes(), nil
}

func (this *Tx) GetFee() uint64 {
return this.CommonOperation.GetFee()
}

func ValidateSignature(tx CommonOperation) error {
_, _, publicKey := tx.getSourceInfo()
return checkSignature(publicKey, tx.getBufferToSign(), tx.getSignature())
}

func (this *Tx) Validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error) {
number, _, publicKey := this.CommonOperation.getSourceInfo()
func Validate(tx CommonOperation, getAccount func(number uint32) *accounter.Account) (context interface{}, err error) {
number, _, publicKey := tx.getSourceInfo()

source := getAccount(number)
if source == nil {
Expand All @@ -128,7 +124,7 @@ func (this *Tx) Validate(getAccount func(number uint32) *accounter.Account) (con
return nil, errors.New("Source account invalid public key")
}

return this.CommonOperation.Validate(getAccount)
return tx.validate(getAccount)
}

func GetRipemd16Hash(tx CommonOperation) []byte {
Expand Down Expand Up @@ -188,13 +184,13 @@ func checkSignature(public *crypto.Public, data []byte, signatureSerialized *cry
return nil
}

func (this *Tx) GetMetadata(txIndexInsideBlock uint32, blockIndex uint32, time uint32) TxMetadata {
func GetMetadata(tx CommonOperation, txIndexInsideBlock uint32, blockIndex uint32, time uint32) TxMetadata {
return TxMetadata{
BlockIndex: blockIndex,
Index: txIndexInsideBlock,
Time: time,
Type: uint8(this.Type),
TxRaw: utils.Serialize(this),
Type: uint8(tx.GetType()),
TxRaw: utils.Serialize(tx),
}
}

Expand Down
2 changes: 1 addition & 1 deletion safebox/tx/txchangekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (this *ChangeKey) GetPayload() []byte {
return this.Payload
}

func (this *ChangeKey) Validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error) {
func (this *ChangeKey) validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error) {
source := getAccount(this.Source)
if source == nil {
return nil, fmt.Errorf("Source account %d not found", this.Source)
Expand Down
2 changes: 1 addition & 1 deletion safebox/tx/txtransfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (this *Transfer) GetPayload() []byte {
return this.Payload
}

func (this *Transfer) Validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error) {
func (this *Transfer) validate(getAccount func(number uint32) *accounter.Account) (context interface{}, err error) {
destination := getAccount(this.Destination)
if destination == nil {
return nil, fmt.Errorf("Destination account %d not found", this.Destination)
Expand Down

0 comments on commit a065a95

Please sign in to comment.