-
-
Notifications
You must be signed in to change notification settings - Fork 714
Add Gnosis #925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kaladinlight
wants to merge
1
commit into
trezor:master
Choose a base branch
from
kaladinlight:add-gnosis
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Gnosis #925
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package gnosis | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum/go-ethereum" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/ethclient" | ||
| "github.com/ethereum/go-ethereum/rpc" | ||
| "github.com/trezor/blockbook/bchain" | ||
| ) | ||
|
|
||
| // GnosisClient wraps a client to implement the EVMClient interface | ||
| type GnosisClient struct { | ||
| *ethclient.Client | ||
| *GnosisRPCClient | ||
| } | ||
|
|
||
| // HeaderByNumber returns a block header that implements the EVMHeader interface | ||
| func (c *GnosisClient) HeaderByNumber(ctx context.Context, number *big.Int) (bchain.EVMHeader, error) { | ||
| var head *Header | ||
| err := c.GnosisRPCClient.CallContext(ctx, &head, "eth_getBlockByNumber", bchain.ToBlockNumArg(number), false) | ||
| if err == nil && head == nil { | ||
| err = ethereum.NotFound | ||
| } | ||
| return &GnosisHeader{Header: head}, err | ||
| } | ||
|
|
||
| // EstimateGas returns the current estimated gas cost for executing a transaction | ||
| func (c *GnosisClient) EstimateGas(ctx context.Context, msg interface{}) (uint64, error) { | ||
| return c.Client.EstimateGas(ctx, msg.(ethereum.CallMsg)) | ||
| } | ||
|
|
||
| // BalanceAt returns the balance for the given account at a specific block, or latest known block if no block number is provided | ||
| func (c *GnosisClient) BalanceAt(ctx context.Context, addrDesc bchain.AddressDescriptor, blockNumber *big.Int) (*big.Int, error) { | ||
| return c.Client.BalanceAt(ctx, common.BytesToAddress(addrDesc), blockNumber) | ||
| } | ||
|
|
||
| // NonceAt returns the nonce for the given account at a specific block, or latest known block if no block number is provided | ||
| func (c *GnosisClient) NonceAt(ctx context.Context, addrDesc bchain.AddressDescriptor, blockNumber *big.Int) (uint64, error) { | ||
| return c.Client.NonceAt(ctx, common.BytesToAddress(addrDesc), blockNumber) | ||
| } | ||
|
|
||
| // GnosisRPCClient wraps an rpc client to implement the EVMRPCClient interface | ||
| type GnosisRPCClient struct { | ||
| *rpc.Client | ||
| } | ||
|
|
||
| // EthSubscribe subscribes to events and returns a client subscription that implements the EVMClientSubscription interface | ||
| func (c *GnosisRPCClient) EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (bchain.EVMClientSubscription, error) { | ||
| sub, err := c.Client.EthSubscribe(ctx, channel, args...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &GnosisClientSubscription{ClientSubscription: sub}, nil | ||
| } | ||
|
|
||
| // GnosisHeader wraps a block header to implement the EVMHeader interface | ||
| type GnosisHeader struct { | ||
| *Header | ||
| } | ||
|
|
||
| // Hash returns the block hash as a hex string | ||
| func (h *GnosisHeader) Hash() string { | ||
| return h.Header.Hash().Hex() | ||
| } | ||
|
|
||
| // Number returns the block number | ||
| func (h *GnosisHeader) Number() *big.Int { | ||
| return h.Header.Number | ||
| } | ||
|
|
||
| // Difficulty returns the block difficulty | ||
| func (h *GnosisHeader) Difficulty() *big.Int { | ||
| return h.Header.Difficulty | ||
| } | ||
|
|
||
| // GnosisHash wraps a transaction hash to implement the EVMHash interface | ||
| type GnosisHash struct { | ||
| common.Hash | ||
| } | ||
|
|
||
| // GnosisClientSubscription wraps a client subcription to implement the EVMClientSubscription interface | ||
| type GnosisClientSubscription struct { | ||
| *rpc.ClientSubscription | ||
| } | ||
|
|
||
| // GnosisNewBlock wraps a block header channel to implement the EVMNewBlockSubscriber interface | ||
| type GnosisNewBlock struct { | ||
| channel chan *Header | ||
| } | ||
|
|
||
| // NewGnosisNewBlock returns an initialized GnosisNewBlock struct | ||
| func NewGnosisNewBlock() *GnosisNewBlock { | ||
| return &GnosisNewBlock{channel: make(chan *Header)} | ||
| } | ||
|
|
||
| // Channel returns the underlying channel as an empty interface | ||
| func (s *GnosisNewBlock) Channel() interface{} { | ||
| return s.channel | ||
| } | ||
|
|
||
| // Read from the underlying channel and return a block header that implements the EVMHeader interface | ||
| func (s *GnosisNewBlock) Read() (bchain.EVMHeader, bool) { | ||
| h, ok := <-s.channel | ||
| return &GnosisHeader{Header: h}, ok | ||
| } | ||
|
|
||
| // Close the underlying channel | ||
| func (s *GnosisNewBlock) Close() { | ||
| close(s.channel) | ||
| } | ||
|
|
||
| // GnosisNewTx wraps a transaction hash channel to implement the EVMNewTxSubscriber interface | ||
| type GnosisNewTx struct { | ||
| channel chan common.Hash | ||
| } | ||
|
|
||
| // NewGnosisNewTx returns an initialized GnosisNewTx struct | ||
| func NewGnosisNewTx() *GnosisNewTx { | ||
| return &GnosisNewTx{channel: make(chan common.Hash)} | ||
| } | ||
|
|
||
| // Channel returns the underlying channel as an empty interface | ||
| func (s *GnosisNewTx) Channel() interface{} { | ||
| return s.channel | ||
| } | ||
|
|
||
| // Read from the underlying channel and return a transaction hash that implements the EVMHash interface | ||
| func (s *GnosisNewTx) Read() (bchain.EVMHash, bool) { | ||
| h, ok := <-s.channel | ||
| return &GnosisHash{Hash: h}, ok | ||
| } | ||
|
|
||
| // Close the underlying channel | ||
| func (s *GnosisNewTx) Close() { | ||
| close(s.channel) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package gnosis | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "math/big" | ||
| "strings" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| "github.com/ethereum/go-ethereum/ethclient" | ||
| "github.com/ethereum/go-ethereum/rpc" | ||
| "github.com/golang/glog" | ||
| "github.com/juju/errors" | ||
| "github.com/trezor/blockbook/bchain" | ||
| "github.com/trezor/blockbook/bchain/coins/eth" | ||
| ) | ||
|
|
||
| const ( | ||
| // MainNet is production network | ||
| MainNet eth.Network = 100 | ||
| ) | ||
|
|
||
| // GnosisRPC is an interface to JSON-RPC bsc service. | ||
| type GnosisRPC struct { | ||
| *eth.EthereumRPC | ||
| } | ||
|
|
||
| // NewGnosisRPC returns new GnosisRPC instance. | ||
| func NewGnosisRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { | ||
| c, err := eth.NewEthereumRPC(config, pushHandler) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| s := &GnosisRPC{ | ||
| EthereumRPC: c.(*eth.EthereumRPC), | ||
| } | ||
|
|
||
| // use trace_block for internal data (debug_trace memory overhead is too expensive) | ||
| c.(*eth.EthereumRPC).GetInternalDataForBlock = s.getInternalDataForBlock | ||
|
|
||
| return s, nil | ||
| } | ||
|
|
||
| // Initialize GnosisRPC interface | ||
| func (b *GnosisRPC) Initialize() error { | ||
| b.OpenRPC = func(url string) (bchain.EVMRPCClient, bchain.EVMClient, error) { | ||
| r, err := rpc.Dial(url) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| rc := &GnosisRPCClient{Client: r} | ||
| ec := &GnosisClient{Client: ethclient.NewClient(r), GnosisRPCClient: rc} | ||
| return rc, ec, nil | ||
| } | ||
|
|
||
| rc, ec, err := b.OpenRPC(b.ChainConfig.RPCURL) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // set chain specific | ||
| b.Client = ec | ||
| b.RPC = rc | ||
| b.MainNetChainID = MainNet | ||
| b.NewBlock = &GnosisNewBlock{channel: make(chan *Header)} | ||
| b.NewTx = &GnosisNewTx{channel: make(chan common.Hash)} | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), b.Timeout) | ||
| defer cancel() | ||
|
|
||
| id, err := b.Client.NetworkID(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // parameters for getInfo request | ||
| switch eth.Network(id.Uint64()) { | ||
| case MainNet: | ||
| b.Testnet = false | ||
| b.Network = "livenet" | ||
| default: | ||
| return errors.Errorf("Unknown network id %v", id) | ||
| } | ||
|
|
||
| glog.Info("rpc: block chain ", b.Network) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type action struct { | ||
| Author string `json:"author"` | ||
| CallType string `json:"callType"` | ||
| From string `json:"from"` | ||
| Gas string `json:"gas"` | ||
| Input string `json:"input"` | ||
| RewardType string `json:"rewardType"` | ||
| To string `json:"to"` | ||
| Value string `json:"value"` | ||
| } | ||
|
|
||
| type traceBlockResult struct { | ||
| Action action `json:"action"` | ||
| BlockHash string `json:"blockHash"` | ||
| BlockNumber int `json:"blockNumber"` | ||
| Error string `json:"error"` | ||
| Result struct { | ||
| GasUsed string `json:"gasUsed"` | ||
| Output string `json:"output"` | ||
| } `json:"result"` | ||
| Subtraces int `json:"subtraces"` | ||
| TraceAddress []int `json:"traceAddress"` | ||
| TransactionHash string `json:"transactionHash"` | ||
| TransactionPosition int `json:"transactionPosition"` | ||
| Type string `json:"type"` | ||
| } | ||
|
|
||
| // getInternalDataForBlock extracts internal transfers and creation or destruction of contracts using the parity trace module | ||
| func (b *GnosisRPC) getInternalDataForBlock(blockHash string, blockHeight uint32, transactions []bchain.RpcTransaction) ([]bchain.EthereumInternalData, []bchain.ContractInfo, error) { | ||
kaladinlight marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data := make([]bchain.EthereumInternalData, len(transactions)) | ||
| contracts := make([]bchain.ContractInfo, 0) | ||
| if b.EthereumRPC.ChainConfig.ProcessInternalTransactions { | ||
| var n big.Int | ||
| n.SetUint64(uint64(blockHeight)) | ||
| ctx, cancel := context.WithTimeout(context.Background(), b.Timeout) | ||
| defer cancel() | ||
| var trace []traceBlockResult | ||
| err := b.RPC.CallContext(ctx, &trace, "trace_block", bchain.ToBlockNumArg(&n)) | ||
| if err != nil { | ||
| glog.Error("trace_block ", blockHash, ", error ", err) | ||
| return data, contracts, err | ||
| } | ||
| for _, t := range trace { | ||
| // initiating call does not have any trace addresses and is not an internal transfer | ||
| if len(t.TraceAddress) == 0 { | ||
| continue | ||
| } | ||
| d := &data[t.TransactionPosition] | ||
| action := t.Action | ||
| callType := strings.ToUpper(action.CallType) | ||
| traceType := strings.ToUpper(t.Type) | ||
| value, err := hexutil.DecodeBig(action.Value) | ||
| if traceType == "CREATE" || traceType == "CREATE2" { | ||
| d.Type = bchain.CREATE | ||
| d.Contract = action.To | ||
| d.Transfers = append(d.Transfers, bchain.EthereumInternalTransfer{ | ||
| Type: bchain.CREATE, | ||
| Value: *value, | ||
| From: action.From, | ||
| To: action.To, // new contract address | ||
| }) | ||
| contracts = append(contracts, *b.GetCreationContractInfo(d.Contract, blockHeight)) | ||
| } else if t.Type == "SELFDESTRUCT" { | ||
| d.Type = bchain.SELFDESTRUCT | ||
| d.Transfers = append(d.Transfers, bchain.EthereumInternalTransfer{ | ||
| Type: bchain.SELFDESTRUCT, | ||
| Value: *value, | ||
| From: action.From, // destroyed contract address | ||
| To: action.To, | ||
| }) | ||
| contracts = append(contracts, bchain.ContractInfo{Contract: action.From, DestructedInBlock: blockHeight}) | ||
| } else if callType == "DELEGATECALL" { | ||
| // ignore DELEGATECALL (geth v1.11 the changed tracer behavior) | ||
| // https://github.com/ethereum/go-ethereum/issues/26726 | ||
| } else if t.Type == "REWARD" { | ||
| // ignore REWARD as block rewards are not associated with any specific transaction | ||
| } else if err == nil && (value.BitLen() > 0 || b.ChainConfig.ProcessZeroInternalTransactions) { | ||
| d.Transfers = append(d.Transfers, bchain.EthereumInternalTransfer{ | ||
| Value: *value, | ||
| From: action.From, | ||
| To: action.To, | ||
| }) | ||
| } | ||
| if t.Error != "" { | ||
| e := eth.PackInternalTransactionError(t.Error) | ||
| if len(e) > 1 { | ||
| d.Error = strings.ToUpper(e[:1]) + e[1:] + ". " | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return data, contracts, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.