Skip to content

Commit

Permalink
refactor(transaction): change transaction from struct to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Oct 27, 2021
1 parent 76916bc commit 8552d00
Show file tree
Hide file tree
Showing 151 changed files with 1,394 additions and 1,781 deletions.
10 changes: 5 additions & 5 deletions account/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"github.com/elastos/Elastos.ELA/core/types/transactions"
"math/rand"
"os"
"sort"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/core/contract"
pg "github.com/elastos/Elastos.ELA/core/contract/program"
"github.com/elastos/Elastos.ELA/core/types"
"github.com/elastos/Elastos.ELA/crypto"
"github.com/elastos/Elastos.ELA/utils"
"github.com/elastos/Elastos.ELA/utils/signal"
Expand Down Expand Up @@ -105,7 +105,7 @@ func Open(path string, password []byte) (*Client, error) {
return client, nil
}

func (cl *Client) Sign(txn *types.Transaction) (*types.Transaction, error) {
func (cl *Client) Sign(txn *transactions.BaseTransaction) (*transactions.BaseTransaction, error) {
var signedPrograms []*pg.Program
for _, program := range txn.Programs {
// Get sign type
Expand Down Expand Up @@ -421,7 +421,7 @@ func (cl *Client) HandleInterrupt() {
}
}

func SignBySigner(txn *types.Transaction, acc *Account) ([]byte, error) {
func SignBySigner(txn *transactions.BaseTransaction, acc *Account) ([]byte, error) {
buf := new(bytes.Buffer)
if err := txn.SerializeUnsigned(buf); err != nil {
return nil, err
Expand All @@ -433,7 +433,7 @@ func SignBySigner(txn *types.Transaction, acc *Account) ([]byte, error) {
return signature, nil
}

func SignStandardTransaction(txn *types.Transaction, program *pg.Program,
func SignStandardTransaction(txn *transactions.BaseTransaction, program *pg.Program,
accounts map[common.Uint160]*Account) (*pg.Program, error) {
code := program.Code
acct, ok := accounts[*common.ToCodeHash(code)]
Expand All @@ -459,7 +459,7 @@ func SignStandardTransaction(txn *types.Transaction, program *pg.Program,
return signedProgram, nil
}

func SignMultiSignTransaction(txn *types.Transaction, program *pg.Program,
func SignMultiSignTransaction(txn *transactions.BaseTransaction, program *pg.Program,
accounts map[common.Uint160]*Account) (*pg.Program, error) {
code := program.Code
param := program.Parameter
Expand Down
6 changes: 3 additions & 3 deletions benchmark/common/tx/fixamontassigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/elastos/Elastos.ELA/benchmark/common/utils"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/common/config"
"github.com/elastos/Elastos.ELA/core/types"
common2 "github.com/elastos/Elastos.ELA/core/types/common"
"github.com/elastos/Elastos.ELA/core/types/outputpayload"
"github.com/elastos/Elastos.ELA/core/types/transactions"
)

const (
Expand All @@ -21,10 +21,10 @@ const (

type fixAmountAssigner struct {
account *account.Account
utxo *types.UTXO
utxo *common2.UTXO
}

func (a *fixAmountAssigner) SignAndChange(tx *types.Transaction) error {
func (a *fixAmountAssigner) SignAndChange(tx *transactions.BaseTransaction) error {
tx.Inputs = []*common2.Input{
{
Previous: common2.OutPoint{
Expand Down
8 changes: 4 additions & 4 deletions benchmark/common/tx/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package tx

import (
"github.com/elastos/Elastos.ELA/account"
"github.com/elastos/Elastos.ELA/core/types"
common2 "github.com/elastos/Elastos.ELA/core/types/common"
"github.com/elastos/Elastos.ELA/core/types/transactions"
)

type AssignerType byte
Expand All @@ -19,11 +19,11 @@ const (
)

type Generator interface {
Generate() *types.Transaction
Generate() *transactions.BaseTransaction
}

type Assigner interface {
SignAndChange(tx *types.Transaction) error
SignAndChange(tx *transactions.BaseTransaction) error
}

func NewGenerator(txType common2.TxType, ac ...*account.Account) Generator {
Expand All @@ -36,7 +36,7 @@ func NewGenerator(txType common2.TxType, ac ...*account.Account) Generator {
}

func NewAssigner(assignerType AssignerType, ac *account.Account,
utxo *types.UTXO) Assigner {
utxo *common2.UTXO) Assigner {
switch assignerType {
case NoChanges:
return &noChangesEvenAssigner{account: ac, utxo: utxo}
Expand Down
6 changes: 3 additions & 3 deletions benchmark/common/tx/nochangesevenassigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/elastos/Elastos.ELA/account"
"github.com/elastos/Elastos.ELA/benchmark/common/utils"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/core/types"
common2 "github.com/elastos/Elastos.ELA/core/types/common"
"github.com/elastos/Elastos.ELA/core/types/transactions"
)

const (
Expand All @@ -19,10 +19,10 @@ const (

type noChangesEvenAssigner struct {
account *account.Account
utxo *types.UTXO
utxo *common2.UTXO
}

func (a *noChangesEvenAssigner) SignAndChange(tx *types.Transaction) error {
func (a *noChangesEvenAssigner) SignAndChange(tx *transactions.BaseTransaction) error {
tx.Inputs = []*common2.Input{
{
Previous: common2.OutPoint{
Expand Down
6 changes: 3 additions & 3 deletions benchmark/common/tx/transferasset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/elastos/Elastos.ELA/account"
"github.com/elastos/Elastos.ELA/benchmark/common/utils"
"github.com/elastos/Elastos.ELA/common/config"
"github.com/elastos/Elastos.ELA/core/types"
common2 "github.com/elastos/Elastos.ELA/core/types/common"
"github.com/elastos/Elastos.ELA/core/types/outputpayload"
"github.com/elastos/Elastos.ELA/core/types/payload"
"github.com/elastos/Elastos.ELA/core/types/transactions"
)

const (
Expand All @@ -23,8 +23,8 @@ type transferAssetGenerator struct {
account []*account.Account
}

func (g *transferAssetGenerator) Generate() *types.Transaction {
txn := &types.Transaction{
func (g *transferAssetGenerator) Generate() *transactions.BaseTransaction {
txn := &transactions.BaseTransaction{
Version: common2.TxVersion09,
TxType: common2.TransferAsset,
PayloadVersion: 0,
Expand Down
4 changes: 2 additions & 2 deletions benchmark/common/utils/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/elastos/Elastos.ELA/account"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/core/contract/program"
"github.com/elastos/Elastos.ELA/core/types"
"github.com/elastos/Elastos.ELA/core/types/transactions"
)

func SignStandardTx(tx *types.Transaction, ac *account.Account) (err error) {
func SignStandardTx(tx *transactions.BaseTransaction, ac *account.Account) (err error) {
accounts := map[common.Uint160]*account.Account{}
accounts[ac.ProgramHash.ToCodeHash()] = ac

Expand Down
14 changes: 8 additions & 6 deletions benchmark/tools/generator/chain/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"bytes"
"errors"
"fmt"
common2 "github.com/elastos/Elastos.ELA/core/types/common"
"github.com/elastos/Elastos.ELA/core/types/transactions"
"io/ioutil"
"math"
"os"
Expand Down Expand Up @@ -98,7 +100,7 @@ func (g *DataGen) Generate(height uint32) (err error) {
}

func (g *DataGen) fastProcess(height uint32) (err error) {
var txs []*types.Transaction
var txs []*transactions.BaseTransaction
if txs, err = g.generateTxs(height); err != nil {
return
}
Expand All @@ -120,7 +122,7 @@ func (g *DataGen) fastProcess(height uint32) (err error) {
}

func (g *DataGen) normalProcess(height uint32) (err error) {
var txs []*types.Transaction
var txs []*transactions.BaseTransaction
if txs, err = g.generateTxs(height); err != nil {
return
}
Expand All @@ -142,7 +144,7 @@ func (g *DataGen) normalProcess(height uint32) (err error) {
}

func (g *DataGen) minimalProcess(height uint32) (err error) {
var txs []*types.Transaction
var txs []*transactions.BaseTransaction
if txs, err = g.generateTxs(height); err != nil {
return
}
Expand Down Expand Up @@ -174,7 +176,7 @@ func (g *DataGen) countProcess(counter *TimeCounter, action func()) {
}

func (g *DataGen) generateTxs(
height uint32) (txs []*types.Transaction, err error) {
height uint32) (txs []*transactions.BaseTransaction, err error) {
if g.pressure {
return g.txRepo.GeneratePressureTxs(height, g.pressureTxSize)
} else {
Expand All @@ -183,7 +185,7 @@ func (g *DataGen) generateTxs(
}

func (g *DataGen) generateBlock(
txs []*types.Transaction) (block *types.Block, err error) {
txs []*transactions.BaseTransaction) (block *types.Block, err error) {
g.countProcess(g.addToTxPoolCount, func() {
for _, v := range txs {
if err = g.txPool.AppendToTxPool(v); err != nil {
Expand Down Expand Up @@ -273,7 +275,7 @@ func FromTxRepository(dataDir string, interrupt <-chan struct{},

if initFoundationUTXO {
fundTx := chainParams.GenesisBlock.Transactions[0]
repo.SetFoundationUTXO(&types.UTXO{
repo.SetFoundationUTXO(&common2.UTXO{
TxID: fundTx.Hash(),
Index: 0,
Value: fundTx.Outputs[0].Value,
Expand Down
Loading

0 comments on commit 8552d00

Please sign in to comment.