The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/rosetta/services/account_test.go

73 lines
1.7 KiB

Rosetta Implementation Cleanup (Stage 3 of Node API Overhaul) (#3390) * [core] Add FindLogsWithTopic & unit test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Add GetDetailedBlockSignerInfo Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Add IsCommitteeSelectionBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Add test transaction creation helpers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Refactor account.go & add tests * Move TestNewAccountIdentifier & TestGetAddress to account_test.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move Operation & Tx formatting to own files * Move Respective unit tests to own files * Expose GetOperations & GetStakingOperations * Expose FormatTransaction, FormatCrossShardReceiverTransaction, FormatGenesisTransaction, FormatPreStakingRewardTransaction & FormatUndelegationPayoutTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move TransactionMetadata to transaction_construction.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update construction to use new helpers & formatters * Make docs consistent for mempool.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move all special tx & blk handling to own file Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Remove all moved fns, methods & tests from block.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Fix lint & imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename all tx related files for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename DefaultSenderAddress to FormatDefaultSenderAddress Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename Currency to NativeCurrency * This is in anticipation of HRC20 token support with rosetta * Rename various native operation functions accordingly * Add documentation to explain what a native token is Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix pre-staking block reward calculation * Move getPreStakingRewardTransactionIdentifiers to block_special.go * Add epoch to block metadata * Update unit tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Add IsLastBlockInEpoch method to Block & Header * Refactor all uses of length check `ShardState` * [hmy] Refactor IsCommitteeSelectionBlock to use chain.IsCommitteeSelectionBlock * Address PR comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update var names in preStakingRewardBlockTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
package services
import (
"reflect"
"testing"
"github.com/coinbase/rosetta-sdk-go/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
internalCommon "github.com/harmony-one/harmony/internal/common"
)
func TestNewAccountIdentifier(t *testing.T) {
key, err := crypto.GenerateKey()
if err != nil {
t.Fatalf(err.Error())
}
addr := crypto.PubkeyToAddress(key.PublicKey)
b32Addr, err := internalCommon.AddressToBech32(addr)
if err != nil {
t.Fatalf(err.Error())
}
metadata, err := types.MarshalMap(AccountMetadata{Address: addr.String()})
if err != nil {
t.Fatalf(err.Error())
}
referenceAccID := &types.AccountIdentifier{
Address: b32Addr,
Metadata: metadata,
}
testAccID, rosettaError := newAccountIdentifier(addr)
if rosettaError != nil {
t.Fatalf("unexpected rosetta error: %v", rosettaError)
}
if !reflect.DeepEqual(referenceAccID, testAccID) {
t.Errorf("reference ID %v != testID %v", referenceAccID, testAccID)
}
}
func TestGetAddress(t *testing.T) {
key, err := crypto.GenerateKey()
if err != nil {
t.Fatalf(err.Error())
}
addr := crypto.PubkeyToAddress(key.PublicKey)
b32Addr, err := internalCommon.AddressToBech32(addr)
if err != nil {
t.Fatalf(err.Error())
}
testAccID := &types.AccountIdentifier{
Address: b32Addr,
}
testAddr, err := getAddress(testAccID)
if err != nil {
t.Fatal(err)
}
if testAddr != addr {
t.Errorf("expected %v to be %v", testAddr.String(), addr.String())
}
defaultAddr := ethcommon.Address{}
testAddr, err = getAddress(nil)
if err == nil {
t.Error("expected err for nil identifier")
}
if testAddr != defaultAddr {
t.Errorf("expected errored addres to be %v not %v", defaultAddr.String(), testAddr.String())
}
}