Skip to content

Commit

Permalink
fix: ExampleGetTransactionType test was failing when run as part of t…
Browse files Browse the repository at this point in the history
…he unittest suite, but working properly when run individually.

This is because TransactionTypes map was being modified somewhere. Instead of investigating what changed it (non-obvious), I simply wrapped TransactionTypes map in a function to provide the const map behaviour originally intended.
  • Loading branch information
randomshinichi committed Oct 10, 2019
1 parent 0bbef47 commit a7fdfae
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ import (
)

// TransactionTypes is a map between the ObjectTags defined above and the
// corresponding Tx struct
var TransactionTypes = map[uint]Transaction{
ObjectTagSignedTransaction: &SignedTx{},
ObjectTagSpendTransaction: &SpendTx{},
ObjectTagNameServiceClaimTransaction: &NameClaimTx{},
ObjectTagNameServicePreclaimTransaction: &NamePreclaimTx{},
ObjectTagNameServiceUpdateTransaction: &NameUpdateTx{},
ObjectTagNameServiceRevokeTransaction: &NameRevokeTx{},
ObjectTagNameServiceTransferTransaction: &NameTransferTx{},
ObjectTagOracleRegisterTransaction: &OracleRegisterTx{},
ObjectTagOracleQueryTransaction: &OracleQueryTx{},
ObjectTagOracleResponseTransaction: &OracleRespondTx{},
ObjectTagOracleExtendTransaction: &OracleExtendTx{},
ObjectTagContractCreateTransaction: &ContractCreateTx{},
ObjectTagContractCallTransaction: &ContractCallTx{},
ObjectTagGeneralizedAccountAttachTransaction: &GAAttachTx{},
ObjectTagGeneralizedAccountMetaTransaction: &GAMetaTx{},
// corresponding Tx struct. It is wrapped by a function to guarantee you cannot
// modify this map, because Golang does not have const maps.
func TransactionTypes() map[uint]Transaction {
return map[uint]Transaction{
ObjectTagSignedTransaction: &SignedTx{},
ObjectTagSpendTransaction: &SpendTx{},
ObjectTagNameServiceClaimTransaction: &NameClaimTx{},
ObjectTagNameServicePreclaimTransaction: &NamePreclaimTx{},
ObjectTagNameServiceUpdateTransaction: &NameUpdateTx{},
ObjectTagNameServiceRevokeTransaction: &NameRevokeTx{},
ObjectTagNameServiceTransferTransaction: &NameTransferTx{},
ObjectTagOracleRegisterTransaction: &OracleRegisterTx{},
ObjectTagOracleQueryTransaction: &OracleQueryTx{},
ObjectTagOracleResponseTransaction: &OracleRespondTx{},
ObjectTagOracleExtendTransaction: &OracleExtendTx{},
ObjectTagContractCreateTransaction: &ContractCreateTx{},
ObjectTagContractCallTransaction: &ContractCallTx{},
ObjectTagGeneralizedAccountAttachTransaction: &GAAttachTx{},
ObjectTagGeneralizedAccountMetaTransaction: &GAMetaTx{},
}
}

// RLP message version used in RLP serialization
Expand Down Expand Up @@ -354,7 +357,7 @@ func DeserializeTx(rawRLP []byte) (Transaction, error) {
func GetTransactionType(rawRLP []byte) (tx Transaction, err error) {
f := binary.DecodeRLPMessage(rawRLP)[0] // [33] interface, needs to be cast to []uint8
objTag := uint(f.([]uint8)[0]) // [33] cast to []uint8, get rid of the slice, cast to uint
return TransactionTypes[objTag], nil
return TransactionTypes()[objTag], nil
}

// SignedTx wraps around other Tx structs to hold the signature.
Expand Down

0 comments on commit a7fdfae

Please sign in to comment.